fixed bug OsmAnd-androidAND-85 in Jira. One click with 2 fingers, automatically zoom out

This commit is contained in:
unknown 2014-06-24 15:00:23 +03:00
parent 19eaf9694e
commit 32897b7323
2 changed files with 156 additions and 87 deletions

View file

@ -0,0 +1,50 @@
package net.osmand.plus.helpers;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
/**
* Created by Barsik on 24.06.2014.
*/
public abstract class SimpleTwoFingerTapDetector {
private static final int TIMEOUT = ViewConfiguration.getDoubleTapTimeout() + 100;
private long mFirstDownTime = 0;
private byte mTwoFingerTapCount = 0;
private MotionEvent firstEvent = null;
private void reset(long time) {
mFirstDownTime = time;
mTwoFingerTapCount = 0;
}
public boolean onTouchEvent(MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
if (mFirstDownTime == 0 || event.getEventTime() - mFirstDownTime > TIMEOUT){
reset(event.getDownTime());
}
break;
case MotionEvent.ACTION_POINTER_UP:
if (event.getPointerCount() == 2) {
mTwoFingerTapCount++;
firstEvent = MotionEvent.obtain(event);
}
else{
mFirstDownTime = 0;
firstEvent = null;
}
break;
case MotionEvent.ACTION_UP:
if (mTwoFingerTapCount == 1 && event.getEventTime() - mFirstDownTime < TIMEOUT) {
onTwoFingerTap(firstEvent, event);
mFirstDownTime = 0;
firstEvent = null;
return true;
}
}
return false;
}
public abstract void onTwoFingerTap(MotionEvent firstevent, MotionEvent secondevent);
}

View file

@ -6,6 +6,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import android.view.*;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.access.AccessibilityActionsProvider; import net.osmand.access.AccessibilityActionsProvider;
import net.osmand.access.AccessibleToast; import net.osmand.access.AccessibleToast;
@ -22,6 +23,7 @@ import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.helpers.SimpleTwoFingerTapDetector;
import net.osmand.plus.views.MultiTouchSupport.MultiTouchZoomListener; import net.osmand.plus.views.MultiTouchSupport.MultiTouchZoomListener;
import net.osmand.plus.views.OsmandMapLayer.DrawSettings; import net.osmand.plus.views.OsmandMapLayer.DrawSettings;
import net.osmand.util.MapUtils; import net.osmand.util.MapUtils;
@ -42,15 +44,9 @@ import android.os.Message;
import android.os.SystemClock; import android.os.SystemClock;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener; import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener; import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback; import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.Toast; import android.widget.Toast;
public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCallback, Callback { public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCallback, Callback {
@ -62,6 +58,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
private boolean MEASURE_FPS = false; private boolean MEASURE_FPS = false;
private FPSMeasurement main = new FPSMeasurement(); private FPSMeasurement main = new FPSMeasurement();
private FPSMeasurement additional = new FPSMeasurement(); private FPSMeasurement additional = new FPSMeasurement();
private class FPSMeasurement { private class FPSMeasurement {
int fpsMeasureCount = 0; int fpsMeasureCount = 0;
int fpsMeasureMs = 0; int fpsMeasureMs = 0;
@ -125,7 +122,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
// UI Part // UI Part
// handler to refresh map (in ui thread - ui thread is not necessary, but msg queue is required). // handler to refresh map (in ui thread - ui thread is not necessary, but msg queue is required).
protected Handler handler ; protected Handler handler;
private Handler baseHandler; private Handler baseHandler;
private AnimateDraggingMapThread animatedDraggingThread; private AnimateDraggingMapThread animatedDraggingThread;
@ -151,6 +148,20 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
private Paint paintImg; private Paint paintImg;
private boolean afterTwoFingerTap = false;
SimpleTwoFingerTapDetector twoFingerTapDetector = new SimpleTwoFingerTapDetector() {
@Override
public void onTwoFingerTap(MotionEvent firstevent, MotionEvent secondevent) {
afterTwoFingerTap = true;
final RotatedTileBox tb = getCurrentRotatedTileBox();
float midx = (firstevent.getX() + secondevent.getX()) / 2;
float midy = (firstevent.getY() + secondevent.getY()) / 2;
final double lat = tb.getLatFromPixel(midx, midy);
final double lon = tb.getLonFromPixel(midx, midy);
getAnimatedDraggingThread().startMoving(lat, lon, getZoom() - 1, true);
}
};
public OsmandMapTileView(Context context, AttributeSet attrs) { public OsmandMapTileView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
@ -173,7 +184,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
// when map rotate // when map rotate
paintGrayFill.setAntiAlias(true); paintGrayFill.setAntiAlias(true);
paintBlackFill= new Paint(); paintBlackFill = new Paint();
paintBlackFill.setColor(Color.BLACK); paintBlackFill.setColor(Color.BLACK);
paintBlackFill.setStyle(Style.FILL); paintBlackFill.setStyle(Style.FILL);
// when map rotate // when map rotate
@ -243,7 +254,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
public float getZorder(OsmandMapLayer layer) { public float getZorder(OsmandMapLayer layer) {
Float z = zOrders.get(layer); Float z = zOrders.get(layer);
if(z == null) { if (z == null) {
return 10; return 10;
} }
return z; return z;
@ -262,7 +273,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
} }
public synchronized void removeLayer(OsmandMapLayer layer) { public synchronized void removeLayer(OsmandMapLayer layer) {
while(layers.remove(layer)); while (layers.remove(layer)) ;
zOrders.remove(layer); zOrders.remove(layer);
layer.destroyLayer(); layer.destroyLayer();
} }
@ -273,8 +284,8 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends OsmandMapLayer> T getLayerByClass(Class<T> cl) { public <T extends OsmandMapLayer> T getLayerByClass(Class<T> cl) {
for(OsmandMapLayer lr : layers) { for (OsmandMapLayer lr : layers) {
if(cl.isInstance(lr)){ if (cl.isInstance(lr)) {
return (T) lr; return (T) lr;
} }
} }
@ -290,7 +301,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
if (mainLayer != null && zoom <= mainLayer.getMaximumShownMapZoom() && zoom >= mainLayer.getMinimumShownMapZoom()) { if (mainLayer != null && zoom <= mainLayer.getMaximumShownMapZoom() && zoom >= mainLayer.getMinimumShownMapZoom()) {
animatedDraggingThread.stopAnimating(); animatedDraggingThread.stopAnimating();
currentViewport.setZoomAndAnimation(zoom, 0); currentViewport.setZoomAndAnimation(zoom, 0);
currentViewport.setRotate(zoom > LOWEST_ZOOM_TO_ROTATE ? rotate : 0 ); currentViewport.setRotate(zoom > LOWEST_ZOOM_TO_ROTATE ? rotate : 0);
refreshMap(); refreshMap();
} }
} }
@ -299,13 +310,13 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
if (mainLayer != null && zoom <= mainLayer.getMaximumShownMapZoom() && zoom >= mainLayer.getMinimumShownMapZoom()) { if (mainLayer != null && zoom <= mainLayer.getMaximumShownMapZoom() && zoom >= mainLayer.getMinimumShownMapZoom()) {
animatedDraggingThread.stopAnimating(); animatedDraggingThread.stopAnimating();
currentViewport.setZoom(zoom, scale, 0); currentViewport.setZoom(zoom, scale, 0);
currentViewport.setRotate(zoom > LOWEST_ZOOM_TO_ROTATE ? rotate : 0 ); currentViewport.setRotate(zoom > LOWEST_ZOOM_TO_ROTATE ? rotate : 0);
refreshMap(); refreshMap();
} }
} }
public boolean isMapRotateEnabled(){ public boolean isMapRotateEnabled() {
return getZoom() > LOWEST_ZOOM_TO_ROTATE; return getZoom() > LOWEST_ZOOM_TO_ROTATE;
} }
@ -349,7 +360,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
return currentViewport.getZoom(); return currentViewport.getZoom();
} }
public float getSettingsZoomScale(){ public float getSettingsZoomScale() {
return settings.getSettingsZoomScale(getDensity()); return settings.getSettingsZoomScale(getDensity());
} }
@ -357,7 +368,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
return currentViewport.getZoomScale(); return currentViewport.getZoomScale();
} }
public boolean isZooming(){ public boolean isZooming() {
return currentViewport.isZoomAnimated(); return currentViewport.isZoomAnimated();
} }
@ -394,16 +405,16 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
this.mapPosition = type; this.mapPosition = type;
} }
public OsmandSettings getSettings(){ public OsmandSettings getSettings() {
if(settings == null){ if (settings == null) {
settings = getApplication().getSettings(); settings = getApplication().getSettings();
} }
return settings; return settings;
} }
private void drawBasemap(Canvas canvas) { private void drawBasemap(Canvas canvas) {
if(bufferImgLoc != null) { if (bufferImgLoc != null) {
float rot = - bufferImgLoc.getRotate(); float rot = -bufferImgLoc.getRotate();
canvas.rotate(rot, currentViewport.getCenterPixelX(), currentViewport.getCenterPixelY()); canvas.rotate(rot, currentViewport.getCenterPixelX(), currentViewport.getCenterPixelY());
final RotatedTileBox calc = currentViewport.copy(); final RotatedTileBox calc = currentViewport.copy();
calc.setRotate(bufferImgLoc.getRotate()); calc.setRotate(bufferImgLoc.getRotate());
@ -415,7 +426,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
final float x2 = calc.getPixXFromTile(rb.x, rb.y, cz); final float x2 = calc.getPixXFromTile(rb.x, rb.y, cz);
final float y1 = calc.getPixYFromTile(lt.x, lt.y, cz); final float y1 = calc.getPixYFromTile(lt.x, lt.y, cz);
final float y2 = calc.getPixYFromTile(rb.x, rb.y, cz); final float y2 = calc.getPixYFromTile(rb.x, rb.y, cz);
if(!bufferBitmap.isRecycled()){ if (!bufferBitmap.isRecycled()) {
RectF rct = new RectF(x1, y1, x2, y2); RectF rct = new RectF(x1, y1, x2, y2);
canvas.drawBitmap(bufferBitmap, null, rct, paintImg); canvas.drawBitmap(bufferBitmap, null, rct, paintImg);
} }
@ -424,10 +435,10 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
} }
private void refreshBaseMapInternal(RotatedTileBox tileBox, DrawSettings drawSettings) { private void refreshBaseMapInternal(RotatedTileBox tileBox, DrawSettings drawSettings) {
if(tileBox.getPixHeight() == 0 || tileBox.getPixWidth() == 0){ if (tileBox.getPixHeight() == 0 || tileBox.getPixWidth() == 0) {
return; return;
} }
if(bufferBitmapTmp == null || tileBox.getPixHeight() != bufferBitmapTmp.getHeight() if (bufferBitmapTmp == null || tileBox.getPixHeight() != bufferBitmapTmp.getHeight()
|| tileBox.getPixWidth() != bufferBitmapTmp.getWidth()) { || tileBox.getPixWidth() != bufferBitmapTmp.getWidth()) {
bufferBitmapTmp = Bitmap.createBitmap(tileBox.getPixWidth(), tileBox.getPixHeight(), Config.RGB_565); bufferBitmapTmp = Bitmap.createBitmap(tileBox.getPixWidth(), tileBox.getPixHeight(), Config.RGB_565);
} }
@ -468,7 +479,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
try { try {
final float ratioy = mapPosition == OsmandSettings.BOTTOM_CONSTANT ? 0.85f : 0.5f; final float ratioy = mapPosition == OsmandSettings.BOTTOM_CONSTANT ? 0.85f : 0.5f;
final int cy = (int) (ratioy * getHeight()); final int cy = (int) (ratioy * getHeight());
if(currentViewport.getPixWidth() != getWidth() || currentViewport.getPixHeight() != getHeight() || if (currentViewport.getPixWidth() != getWidth() || currentViewport.getPixHeight() != getHeight() ||
currentViewport.getCenterPixelY() != cy) { currentViewport.getCenterPixelY() != cy) {
currentViewport.setPixelDimensions(getWidth(), getHeight(), 0.5f, ratioy); currentViewport.setPixelDimensions(getWidth(), getHeight(), 0.5f, ratioy);
refreshBufferImage(drawSettings); refreshBufferImage(drawSettings);
@ -504,10 +515,11 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
MEASURE_FPS = measureFPS; MEASURE_FPS = measureFPS;
} }
public float getFPS(){ public float getFPS() {
return main.fps; return main.fps;
} }
public float getSecondaryFPS(){
public float getSecondaryFPS() {
return additional.fps; return additional.fps;
} }
@ -558,7 +570,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
} }
refreshBaseMapInternal(currentViewport.copy(), param); refreshBaseMapInternal(currentViewport.copy(), param);
sendRefreshMapMsg(param, 0); sendRefreshMapMsg(param, 0);
} catch(Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} }
} }
@ -630,6 +642,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
} }
return scaleCoefficient; return scaleCoefficient;
} }
/** /**
* These methods do not consider rotating * These methods do not consider rotating
*/ */
@ -670,7 +683,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
protected void zoomToAnimate(float tzoom, boolean notify) { protected void zoomToAnimate(float tzoom, boolean notify) {
int zoom = getZoom(); int zoom = getZoom();
float zoomToAnimate = tzoom - zoom - getZoomScale(); float zoomToAnimate = tzoom - zoom - getZoomScale();
if(zoomToAnimate >= 1) { if (zoomToAnimate >= 1) {
zoom += (int) zoomToAnimate; zoom += (int) zoomToAnimate;
zoomToAnimate -= (int) zoomToAnimate; zoomToAnimate -= (int) zoomToAnimate;
} }
@ -680,7 +693,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
} }
if (mainLayer != null && mainLayer.getMaximumShownMapZoom() >= zoom && mainLayer.getMinimumShownMapZoom() <= zoom) { if (mainLayer != null && mainLayer.getMaximumShownMapZoom() >= zoom && mainLayer.getMinimumShownMapZoom() <= zoom) {
currentViewport.setZoomAndAnimation(zoom, zoomToAnimate); currentViewport.setZoomAndAnimation(zoom, zoomToAnimate);
currentViewport.setRotate(zoom > LOWEST_ZOOM_TO_ROTATE ? rotate : 0 ); currentViewport.setRotate(zoom > LOWEST_ZOOM_TO_ROTATE ? rotate : 0);
refreshMap(); refreshMap();
if (notify && locationListener != null) { if (notify && locationListener != null) {
locationListener.locationChanged(getLatitude(), getLongitude(), this); locationListener.locationChanged(getLatitude(), getLongitude(), this);
@ -699,16 +712,21 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
@Override @Override
public boolean onTouchEvent(MotionEvent event) { public boolean onTouchEvent(MotionEvent event) {
if (twoFingerTapDetector.onTouchEvent(event)) {
return true;
}
if (event.getAction() == MotionEvent.ACTION_DOWN) { if (event.getAction() == MotionEvent.ACTION_DOWN) {
animatedDraggingThread.stopAnimating(); animatedDraggingThread.stopAnimating();
} }
for(int i=layers.size() - 1; i >= 0; i--) { for (int i = layers.size() - 1; i >= 0; i--) {
if(layers.get(i).onTouchEvent(event, getCurrentRotatedTileBox())) { if (layers.get(i).onTouchEvent(event, getCurrentRotatedTileBox())) {
return true; return true;
} }
} }
if (!multiTouchSupport.onTouchEvent(event)) { if (!multiTouchSupport.onTouchEvent(event)) {
/* return */gestureDetector.onTouchEvent(event); /* return */
gestureDetector.onTouchEvent(event);
} }
return true; return true;
} }
@ -738,7 +756,6 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
} }
public AnimateDraggingMapThread getAnimatedDraggingThread() { public AnimateDraggingMapThread getAnimatedDraggingThread() {
return animatedDraggingThread; return animatedDraggingThread;
} }
@ -769,8 +786,8 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
// 1.5 works better even on dm.density=1 devices // 1.5 works better even on dm.density=1 devices
float dz = (float) (Math.log(relativeToStart) / Math.log(2)) * 1.5f; float dz = (float) (Math.log(relativeToStart) / Math.log(2)) * 1.5f;
setIntZoom(Math.round(dz) + initialViewport.getZoom()); setIntZoom(Math.round(dz) + initialViewport.getZoom());
if(Math.abs(angleRelative) < ANGLE_THRESHOLD * relativeToStart || if (Math.abs(angleRelative) < ANGLE_THRESHOLD * relativeToStart ||
Math.abs(angleRelative) < ANGLE_THRESHOLD / relativeToStart){ Math.abs(angleRelative) < ANGLE_THRESHOLD / relativeToStart) {
angleRelative = 0; angleRelative = 0;
} }
rotateToAnimate(initialViewport.getRotate() + angleRelative); rotateToAnimate(initialViewport.getRotate() + angleRelative);
@ -781,7 +798,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
} else { } else {
final LatLon p1 = initialViewport.getLatLonFromPixel(x1, y1); final LatLon p1 = initialViewport.getLatLonFromPixel(x1, y1);
final LatLon p2 = initialViewport.getLatLonFromPixel(x2, y2); final LatLon p2 = initialViewport.getLatLonFromPixel(x2, y2);
showMessage(OsmAndFormatter.getFormattedDistance((float)MapUtils.getDistance(p1.getLatitude(), p1.getLongitude(), p2.getLatitude(), p2.getLongitude()), application)); showMessage(OsmAndFormatter.getFormattedDistance((float) MapUtils.getDistance(p1.getLatitude(), p1.getLongitude(), p2.getLatitude(), p2.getLongitude()), application));
} }
} }
} }
@ -810,12 +827,12 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
// keep only rotating // keep only rotating
dz = 0; dz = 0;
} }
if(Math.abs(relAngle) < ANGLE_THRESHOLD && !startRotating) { if (Math.abs(relAngle) < ANGLE_THRESHOLD && !startRotating) {
relAngle = 0; relAngle = 0;
} else { } else {
startRotating = true; startRotating = true;
} }
if(dz != 0 || relAngle != 0) { if (dz != 0 || relAngle != 0) {
changeZoomPosition((float) dz, relAngle); changeZoomPosition((float) dz, relAngle);
} }
@ -823,8 +840,8 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
private void changeZoomPosition(float dz, float angle) { private void changeZoomPosition(float dz, float angle) {
final QuadPoint cp = initialViewport.getCenterPixelPoint(); final QuadPoint cp = initialViewport.getCenterPixelPoint();
float dx = cp.x - initialMultiTouchCenterPoint.x ; float dx = cp.x - initialMultiTouchCenterPoint.x;
float dy = cp.y - initialMultiTouchCenterPoint.y ; float dy = cp.y - initialMultiTouchCenterPoint.y;
final RotatedTileBox calc = initialViewport.copy(); final RotatedTileBox calc = initialViewport.copy();
calc.setLatLonCenter(initialCenterLatLon.getLatitude(), initialCenterLatLon.getLongitude()); calc.setLatLonCenter(initialCenterLatLon.getLatitude(), initialCenterLatLon.getLongitude());
@ -855,14 +872,15 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
@Override @Override
public void onLongPress(MotionEvent e) { public void onLongPress(MotionEvent e) {
if (multiTouchSupport.isInZoomMode()) { if (multiTouchSupport.isInZoomMode() || afterTwoFingerTap) {
afterTwoFingerTap = false;
return; return;
} }
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("On long click event " + e.getX() + " " + e.getY()); //$NON-NLS-1$ //$NON-NLS-2$ log.debug("On long click event " + e.getX() + " " + e.getY()); //$NON-NLS-1$ //$NON-NLS-2$
} }
PointF point = new PointF(e.getX(), e.getY()); PointF point = new PointF(e.getX(), e.getY());
if ((accessibilityActions != null) && accessibilityActions.onLongClick(point, getCurrentRotatedTileBox() )) { if ((accessibilityActions != null) && accessibilityActions.onLongClick(point, getCurrentRotatedTileBox())) {
return; return;
} }
for (int i = layers.size() - 1; i >= 0; i--) { for (int i = layers.size() - 1; i >= 0; i--) {
@ -928,4 +946,5 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
} }
} }
} }