Merge pull request #711 from Bars107/master

fixed bug OsmAnd-androidAND-85 in Jira. One click with 2 fingers, automa...
This commit is contained in:
vshcherb 2014-06-24 15:23:51 +02:00
commit 2be802e7f2
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.Map;
import android.view.*;
import net.osmand.PlatformUtil;
import net.osmand.access.AccessibilityActionsProvider;
import net.osmand.access.AccessibleToast;
@ -22,6 +23,7 @@ import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.helpers.SimpleTwoFingerTapDetector;
import net.osmand.plus.views.MultiTouchSupport.MultiTouchZoomListener;
import net.osmand.plus.views.OsmandMapLayer.DrawSettings;
import net.osmand.util.MapUtils;
@ -42,15 +44,9 @@ import android.os.Message;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
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.SurfaceView;
import android.view.WindowManager;
import android.widget.Toast;
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 FPSMeasurement main = new FPSMeasurement();
private FPSMeasurement additional = new FPSMeasurement();
private class FPSMeasurement {
int fpsMeasureCount = 0;
int fpsMeasureMs = 0;
@ -125,7 +122,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
// UI Part
// 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 AnimateDraggingMapThread animatedDraggingThread;
@ -151,6 +148,20 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
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) {
super(context, attrs);
@ -173,7 +184,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
// when map rotate
paintGrayFill.setAntiAlias(true);
paintBlackFill= new Paint();
paintBlackFill = new Paint();
paintBlackFill.setColor(Color.BLACK);
paintBlackFill.setStyle(Style.FILL);
// when map rotate
@ -243,7 +254,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
public float getZorder(OsmandMapLayer layer) {
Float z = zOrders.get(layer);
if(z == null) {
if (z == null) {
return 10;
}
return z;
@ -262,7 +273,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
}
public synchronized void removeLayer(OsmandMapLayer layer) {
while(layers.remove(layer));
while (layers.remove(layer)) ;
zOrders.remove(layer);
layer.destroyLayer();
}
@ -273,8 +284,8 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
@SuppressWarnings("unchecked")
public <T extends OsmandMapLayer> T getLayerByClass(Class<T> cl) {
for(OsmandMapLayer lr : layers) {
if(cl.isInstance(lr)){
for (OsmandMapLayer lr : layers) {
if (cl.isInstance(lr)) {
return (T) lr;
}
}
@ -290,7 +301,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
if (mainLayer != null && zoom <= mainLayer.getMaximumShownMapZoom() && zoom >= mainLayer.getMinimumShownMapZoom()) {
animatedDraggingThread.stopAnimating();
currentViewport.setZoomAndAnimation(zoom, 0);
currentViewport.setRotate(zoom > LOWEST_ZOOM_TO_ROTATE ? rotate : 0 );
currentViewport.setRotate(zoom > LOWEST_ZOOM_TO_ROTATE ? rotate : 0);
refreshMap();
}
}
@ -299,13 +310,13 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
if (mainLayer != null && zoom <= mainLayer.getMaximumShownMapZoom() && zoom >= mainLayer.getMinimumShownMapZoom()) {
animatedDraggingThread.stopAnimating();
currentViewport.setZoom(zoom, scale, 0);
currentViewport.setRotate(zoom > LOWEST_ZOOM_TO_ROTATE ? rotate : 0 );
currentViewport.setRotate(zoom > LOWEST_ZOOM_TO_ROTATE ? rotate : 0);
refreshMap();
}
}
public boolean isMapRotateEnabled(){
public boolean isMapRotateEnabled() {
return getZoom() > LOWEST_ZOOM_TO_ROTATE;
}
@ -349,7 +360,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
return currentViewport.getZoom();
}
public float getSettingsZoomScale(){
public float getSettingsZoomScale() {
return settings.getSettingsZoomScale(getDensity());
}
@ -357,7 +368,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
return currentViewport.getZoomScale();
}
public boolean isZooming(){
public boolean isZooming() {
return currentViewport.isZoomAnimated();
}
@ -394,16 +405,16 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
this.mapPosition = type;
}
public OsmandSettings getSettings(){
if(settings == null){
public OsmandSettings getSettings() {
if (settings == null) {
settings = getApplication().getSettings();
}
return settings;
}
private void drawBasemap(Canvas canvas) {
if(bufferImgLoc != null) {
float rot = - bufferImgLoc.getRotate();
if (bufferImgLoc != null) {
float rot = -bufferImgLoc.getRotate();
canvas.rotate(rot, currentViewport.getCenterPixelX(), currentViewport.getCenterPixelY());
final RotatedTileBox calc = currentViewport.copy();
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 y1 = calc.getPixYFromTile(lt.x, lt.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);
canvas.drawBitmap(bufferBitmap, null, rct, paintImg);
}
@ -424,10 +435,10 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
}
private void refreshBaseMapInternal(RotatedTileBox tileBox, DrawSettings drawSettings) {
if(tileBox.getPixHeight() == 0 || tileBox.getPixWidth() == 0){
if (tileBox.getPixHeight() == 0 || tileBox.getPixWidth() == 0) {
return;
}
if(bufferBitmapTmp == null || tileBox.getPixHeight() != bufferBitmapTmp.getHeight()
if (bufferBitmapTmp == null || tileBox.getPixHeight() != bufferBitmapTmp.getHeight()
|| tileBox.getPixWidth() != bufferBitmapTmp.getWidth()) {
bufferBitmapTmp = Bitmap.createBitmap(tileBox.getPixWidth(), tileBox.getPixHeight(), Config.RGB_565);
}
@ -468,7 +479,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
try {
final float ratioy = mapPosition == OsmandSettings.BOTTOM_CONSTANT ? 0.85f : 0.5f;
final int cy = (int) (ratioy * getHeight());
if(currentViewport.getPixWidth() != getWidth() || currentViewport.getPixHeight() != getHeight() ||
if (currentViewport.getPixWidth() != getWidth() || currentViewport.getPixHeight() != getHeight() ||
currentViewport.getCenterPixelY() != cy) {
currentViewport.setPixelDimensions(getWidth(), getHeight(), 0.5f, ratioy);
refreshBufferImage(drawSettings);
@ -504,10 +515,11 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
MEASURE_FPS = measureFPS;
}
public float getFPS(){
public float getFPS() {
return main.fps;
}
public float getSecondaryFPS(){
public float getSecondaryFPS() {
return additional.fps;
}
@ -558,7 +570,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
}
refreshBaseMapInternal(currentViewport.copy(), param);
sendRefreshMapMsg(param, 0);
} catch(Exception e) {
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
@ -630,6 +642,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
}
return scaleCoefficient;
}
/**
* These methods do not consider rotating
*/
@ -670,7 +683,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
protected void zoomToAnimate(float tzoom, boolean notify) {
int zoom = getZoom();
float zoomToAnimate = tzoom - zoom - getZoomScale();
if(zoomToAnimate >= 1) {
if (zoomToAnimate >= 1) {
zoom += (int) zoomToAnimate;
zoomToAnimate -= (int) zoomToAnimate;
}
@ -679,8 +692,8 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
zoomToAnimate += 1;
}
if (mainLayer != null && mainLayer.getMaximumShownMapZoom() >= zoom && mainLayer.getMinimumShownMapZoom() <= zoom) {
currentViewport.setZoomAndAnimation(zoom, zoomToAnimate);
currentViewport.setRotate(zoom > LOWEST_ZOOM_TO_ROTATE ? rotate : 0 );
currentViewport.setZoomAndAnimation(zoom, zoomToAnimate);
currentViewport.setRotate(zoom > LOWEST_ZOOM_TO_ROTATE ? rotate : 0);
refreshMap();
if (notify && locationListener != null) {
locationListener.locationChanged(getLatitude(), getLongitude(), this);
@ -699,16 +712,21 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
@Override
public boolean onTouchEvent(MotionEvent event) {
if (twoFingerTapDetector.onTouchEvent(event)) {
return true;
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
animatedDraggingThread.stopAnimating();
}
for(int i=layers.size() - 1; i >= 0; i--) {
if(layers.get(i).onTouchEvent(event, getCurrentRotatedTileBox())) {
for (int i = layers.size() - 1; i >= 0; i--) {
if (layers.get(i).onTouchEvent(event, getCurrentRotatedTileBox())) {
return true;
}
}
if (!multiTouchSupport.onTouchEvent(event)) {
/* return */gestureDetector.onTouchEvent(event);
/* return */
gestureDetector.onTouchEvent(event);
}
return true;
}
@ -738,7 +756,6 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
}
public AnimateDraggingMapThread getAnimatedDraggingThread() {
return animatedDraggingThread;
}
@ -765,12 +782,12 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
private static final float ANGLE_THRESHOLD = 15;
@Override
public void onZoomEnded(double relativeToStart, float angleRelative) {
public void onZoomEnded(double relativeToStart, float angleRelative) {
// 1.5 works better even on dm.density=1 devices
float dz = (float) (Math.log(relativeToStart) / Math.log(2)) * 1.5f;
setIntZoom(Math.round(dz) + initialViewport.getZoom());
if(Math.abs(angleRelative) < ANGLE_THRESHOLD * relativeToStart ||
Math.abs(angleRelative) < ANGLE_THRESHOLD / relativeToStart){
if (Math.abs(angleRelative) < ANGLE_THRESHOLD * relativeToStart ||
Math.abs(angleRelative) < ANGLE_THRESHOLD / relativeToStart) {
angleRelative = 0;
}
rotateToAnimate(initialViewport.getRotate() + angleRelative);
@ -781,7 +798,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
} else {
final LatLon p1 = initialViewport.getLatLonFromPixel(x1, y1);
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
dz = 0;
}
if(Math.abs(relAngle) < ANGLE_THRESHOLD && !startRotating) {
if (Math.abs(relAngle) < ANGLE_THRESHOLD && !startRotating) {
relAngle = 0;
} else {
startRotating = true;
}
if(dz != 0 || relAngle != 0) {
if (dz != 0 || relAngle != 0) {
changeZoomPosition((float) dz, relAngle);
}
@ -823,8 +840,8 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
private void changeZoomPosition(float dz, float angle) {
final QuadPoint cp = initialViewport.getCenterPixelPoint();
float dx = cp.x - initialMultiTouchCenterPoint.x ;
float dy = cp.y - initialMultiTouchCenterPoint.y ;
float dx = cp.x - initialMultiTouchCenterPoint.x;
float dy = cp.y - initialMultiTouchCenterPoint.y;
final RotatedTileBox calc = initialViewport.copy();
calc.setLatLonCenter(initialCenterLatLon.getLatitude(), initialCenterLatLon.getLongitude());
@ -849,20 +866,21 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
animatedDraggingThread.startDragging(velocityX, velocityY,
e1.getX(), e1.getY(), e2.getX(), e2.getY(), true);
e1.getX(), e1.getY(), e2.getX(), e2.getY(), true);
return true;
}
@Override
public void onLongPress(MotionEvent e) {
if (multiTouchSupport.isInZoomMode()) {
if (multiTouchSupport.isInZoomMode() || afterTwoFingerTap) {
afterTwoFingerTap = false;
return;
}
if (log.isDebugEnabled()) {
log.debug("On long click event " + e.getX() + " " + e.getY()); //$NON-NLS-1$ //$NON-NLS-2$
}
PointF point = new PointF(e.getX(), e.getY());
if ((accessibilityActions != null) && accessibilityActions.onLongClick(point, getCurrentRotatedTileBox() )) {
if ((accessibilityActions != null) && accessibilityActions.onLongClick(point, getCurrentRotatedTileBox())) {
return;
}
for (int i = layers.size() - 1; i >= 0; i--) {
@ -928,4 +946,5 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
}
}
}