fix hanging UI on MapWorker.pause/.proceed
This commit is contained in:
parent
165221f42a
commit
4cae011d85
@ -93,7 +93,7 @@ public class MapView extends GLSurfaceView {
|
||||
private IMapRenderer mMapRenderer;
|
||||
private JobQueue mJobQueue;
|
||||
private MapWorker mMapWorkers[];
|
||||
private int mNumMapWorkers = 6;
|
||||
private int mNumMapWorkers = 4;
|
||||
private JobParameters mJobParameters;
|
||||
private DebugSettings mDebugSettings;
|
||||
private String mMapFile;
|
||||
@ -301,15 +301,15 @@ public class MapView extends GLSurfaceView {
|
||||
/**
|
||||
* Calculates all necessary tiles and adds jobs accordingly.
|
||||
*/
|
||||
public synchronized void redrawTiles() {
|
||||
if (getWidth() <= 0 || getHeight() <= 0)
|
||||
public void redrawTiles() {
|
||||
if (getWidth() > 0 && getHeight() > 0)
|
||||
return;
|
||||
|
||||
mMapRenderer.redrawTiles(false);
|
||||
}
|
||||
|
||||
void clearAndRedrawMapView() {
|
||||
if (getWidth() <= 0 || getHeight() <= 0)
|
||||
if (getWidth() > 0 && getHeight() > 0)
|
||||
return;
|
||||
|
||||
mMapRenderer.redrawTiles(true);
|
||||
@ -525,7 +525,6 @@ public class MapView extends GLSurfaceView {
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, e.getMessage());
|
||||
} finally {
|
||||
mapWorkersProceed();
|
||||
try {
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
@ -533,7 +532,9 @@ public class MapView extends GLSurfaceView {
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, e.getMessage());
|
||||
}
|
||||
mapWorkersProceed();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -604,13 +605,13 @@ public class MapView extends GLSurfaceView {
|
||||
@Override
|
||||
protected synchronized void onSizeChanged(int width, int height, int oldWidth,
|
||||
int oldHeight) {
|
||||
for (MapWorker mapWorker : mMapWorkers) {
|
||||
mapWorker.pause();
|
||||
mapWorker.awaitPausing();
|
||||
super.onSizeChanged(width, height, oldWidth, oldHeight);
|
||||
mapWorker.proceed();
|
||||
}
|
||||
// redrawTiles();
|
||||
mJobQueue.clear();
|
||||
|
||||
mapWorkersPause();
|
||||
|
||||
super.onSizeChanged(width, height, oldWidth, oldHeight);
|
||||
|
||||
mapWorkersProceed();
|
||||
}
|
||||
|
||||
void destroy() {
|
||||
@ -618,6 +619,7 @@ public class MapView extends GLSurfaceView {
|
||||
// mZoomAnimator.interrupt();
|
||||
|
||||
for (MapWorker mapWorker : mMapWorkers) {
|
||||
mapWorker.pause();
|
||||
mapWorker.interrupt();
|
||||
|
||||
try {
|
||||
@ -754,10 +756,12 @@ public class MapView extends GLSurfaceView {
|
||||
|
||||
private void mapWorkersPause() {
|
||||
for (MapWorker mapWorker : mMapWorkers) {
|
||||
if (!mapWorker.isPausing()) {
|
||||
if (!mapWorker.isPausing())
|
||||
mapWorker.pause();
|
||||
}
|
||||
for (MapWorker mapWorker : mMapWorkers) {
|
||||
if (!mapWorker.isPausing())
|
||||
mapWorker.awaitPausing();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -28,6 +28,8 @@ public class MapWorker extends PausableThread {
|
||||
private final IMapGenerator mMapGenerator;
|
||||
private final IMapRenderer mMapRenderer;
|
||||
|
||||
// private final int mPrio;
|
||||
|
||||
/**
|
||||
* @param id
|
||||
* thread id
|
||||
@ -46,6 +48,7 @@ public class MapWorker extends PausableThread {
|
||||
mMapRenderer = mapRenderer;
|
||||
|
||||
THREAD_NAME = "MapWorker" + id;
|
||||
// mPrio = Math.max(Thread.MIN_PRIORITY + id, Thread.NORM_PRIORITY - 1);
|
||||
}
|
||||
|
||||
public IMapGenerator getMapGenerator() {
|
||||
@ -63,7 +66,6 @@ public class MapWorker extends PausableThread {
|
||||
|
||||
if (mMapGenerator == null || mapGeneratorJob == null)
|
||||
return;
|
||||
// Log.d(THREAD_NAME, "processing: " + mapGeneratorJob.tile);
|
||||
|
||||
boolean success = mMapGenerator.executeJob(mapGeneratorJob);
|
||||
|
||||
@ -77,10 +79,15 @@ public class MapWorker extends PausableThread {
|
||||
return THREAD_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void takeabreak() {
|
||||
mMapGenerator.getMapDatabase().cancel();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getThreadPriority() {
|
||||
// return (Thread.NORM_PRIORITY + Thread.MIN_PRIORITY) / 2;
|
||||
return Thread.MIN_PRIORITY;
|
||||
return (Thread.NORM_PRIORITY + Thread.MIN_PRIORITY) / 2;
|
||||
// return mPrio;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -58,6 +58,7 @@ public abstract class PausableThread extends Thread {
|
||||
public final synchronized void pause() {
|
||||
if (!mShouldPause) {
|
||||
mShouldPause = true;
|
||||
takeabreak();
|
||||
notify();
|
||||
}
|
||||
}
|
||||
@ -116,6 +117,10 @@ public abstract class PausableThread extends Thread {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
protected void takeabreak() {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
/**
|
||||
* Called once at the end of the {@link #run()} method. The default implementation is empty.
|
||||
*/
|
||||
|
||||
@ -71,4 +71,6 @@ public interface IMapDatabase {
|
||||
*/
|
||||
public abstract String readString(int position);
|
||||
|
||||
public abstract void cancel();
|
||||
|
||||
}
|
||||
|
||||
@ -173,4 +173,10 @@ public class MapDatabase implements IMapDatabase {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -976,4 +976,10 @@ public class MapDatabase implements IMapDatabase {
|
||||
|
||||
return zoomTable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,6 +17,8 @@ package org.mapsforge.database.pbmap;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.SocketException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.HashMap;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
@ -24,8 +26,17 @@ import org.apache.http.Header;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.params.HttpClientParams;
|
||||
import org.apache.http.conn.scheme.PlainSocketFactory;
|
||||
import org.apache.http.conn.scheme.Scheme;
|
||||
import org.apache.http.conn.scheme.SchemeRegistry;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.params.BasicHttpParams;
|
||||
import org.apache.http.params.HttpConnectionParams;
|
||||
import org.apache.http.params.HttpParams;
|
||||
import org.mapsforge.core.BoundingBox;
|
||||
import org.mapsforge.core.GeoPoint;
|
||||
import org.mapsforge.core.Tag;
|
||||
@ -36,7 +47,6 @@ import org.mapsforge.database.IMapDatabase;
|
||||
import org.mapsforge.database.IMapDatabaseCallback;
|
||||
import org.mapsforge.database.MapFileInfo;
|
||||
|
||||
import android.net.http.AndroidHttpClient;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
@ -63,12 +73,17 @@ public class MapDatabase implements IMapDatabase {
|
||||
private Tag[] curTags = new Tag[1000];
|
||||
private int mCurTagCnt;
|
||||
|
||||
private AndroidHttpClient mClient;
|
||||
// private AndroidHttpClient mClient;
|
||||
private HttpClient mClient;
|
||||
private IMapDatabaseCallback mMapGenerator;
|
||||
private float mScaleFactor;
|
||||
|
||||
@Override
|
||||
public void executeQuery(Tile tile, IMapDatabaseCallback mapDatabaseCallback) {
|
||||
mCanceled = false;
|
||||
|
||||
if (mClient == null)
|
||||
createClient();
|
||||
|
||||
String url = String.format(URL, Integer.valueOf(tile.zoomLevel),
|
||||
Long.valueOf(tile.tileX), Long.valueOf(tile.tileY));
|
||||
@ -100,6 +115,7 @@ public class MapDatabase implements IMapDatabase {
|
||||
InputStream is = null;
|
||||
GZIPInputStream zis = null;
|
||||
try {
|
||||
// is = AndroidHttpClient.getUngzippedContent(entity);
|
||||
|
||||
is = entity.getContent();
|
||||
zis = new GZIPInputStream(is);
|
||||
@ -113,6 +129,10 @@ public class MapDatabase implements IMapDatabase {
|
||||
is.close();
|
||||
entity.consumeContent();
|
||||
}
|
||||
} catch (SocketException ex) {
|
||||
Log.d(TAG, "Socket exception: " + ex.getMessage());
|
||||
} catch (SocketTimeoutException ex) {
|
||||
Log.d(TAG, "Socket exception: " + ex.getMessage());
|
||||
} catch (Exception ex) {
|
||||
getRequest.abort();
|
||||
ex.printStackTrace();
|
||||
@ -134,10 +154,27 @@ public class MapDatabase implements IMapDatabase {
|
||||
return mOpenFile;
|
||||
}
|
||||
|
||||
private void createClient() {
|
||||
// mClient = AndroidHttpClient.newInstance("Android");
|
||||
|
||||
mOpenFile = true;
|
||||
HttpParams params = new BasicHttpParams();
|
||||
HttpConnectionParams.setStaleCheckingEnabled(params, false);
|
||||
|
||||
HttpConnectionParams.setConnectionTimeout(params, 10 * 1000);
|
||||
HttpConnectionParams.setSoTimeout(params, 60 * 1000);
|
||||
HttpConnectionParams.setSocketBufferSize(params, 8192);
|
||||
mClient = new DefaultHttpClient(params);
|
||||
HttpClientParams.setRedirecting(params, false);
|
||||
SchemeRegistry schemeRegistry = new SchemeRegistry();
|
||||
schemeRegistry.register(new Scheme("http",
|
||||
PlainSocketFactory.getSocketFactory(), 80));
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileOpenResult openFile(File mapFile) {
|
||||
mOpenFile = true;
|
||||
mClient = AndroidHttpClient.newInstance("Android");
|
||||
createClient();
|
||||
|
||||
return new FileOpenResult();
|
||||
}
|
||||
|
||||
@ -145,7 +182,7 @@ public class MapDatabase implements IMapDatabase {
|
||||
public void closeFile() {
|
||||
mOpenFile = false;
|
||||
if (mClient != null)
|
||||
mClient.close();
|
||||
mClient.getConnectionManager().shutdown();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -361,20 +398,18 @@ public class MapDatabase implements IMapDatabase {
|
||||
private int decodeWayCoordinates() throws IOException {
|
||||
int bytes = decodeVarint32();
|
||||
|
||||
int cnt = 0;
|
||||
// int end = bytesRead + bytes;
|
||||
|
||||
readBuffer(bytes);
|
||||
|
||||
int pos = bufferPos;
|
||||
int end = pos + bytes;
|
||||
float[] coords = tmpCoords;
|
||||
byte[] buf = buffer;
|
||||
int cnt = 0;
|
||||
int result;
|
||||
|
||||
// read repeated sint32
|
||||
while (pos < end) {
|
||||
if (cnt >= MAX_WAY_COORDS) {
|
||||
// Log.d(TAG, "increase coords array " + MAX_WAY_COORDS);
|
||||
MAX_WAY_COORDS += 128;
|
||||
float[] tmp = new float[MAX_WAY_COORDS];
|
||||
System.arraycopy(coords, 0, tmp, 0, cnt);
|
||||
@ -457,10 +492,10 @@ public class MapDatabase implements IMapDatabase {
|
||||
return;
|
||||
|
||||
if (size > BUFFER_SIZE) {
|
||||
Log.d(TAG, "EEEK too large");
|
||||
// FIXME throw exception, but frankly better sanitize tile data on compilation
|
||||
// this only happen with Strings larger than 32kb
|
||||
return;
|
||||
// FIXME throw exception for now, but frankly better
|
||||
// sanitize tile data on compilation.
|
||||
// this only happen with strings or coordinates larger than 64kb
|
||||
throw new IOException("EEEK requested size too large");
|
||||
}
|
||||
|
||||
if ((size - bufferSize) + bufferPos > BUFFER_SIZE) {
|
||||
@ -574,4 +609,13 @@ public class MapDatabase implements IMapDatabase {
|
||||
return (n >>> 1) ^ -(n & 1);
|
||||
}
|
||||
|
||||
private boolean mCanceled;
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
mCanceled = true;
|
||||
mClient.getConnectionManager().shutdown();
|
||||
mClient = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -366,4 +366,10 @@ public class MapDatabase implements IMapDatabase {
|
||||
int count = data.getInt();
|
||||
parseGeometryArray(data, count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user