diff --git a/OsmAnd/src/net/osmand/plus/helpers/LockGestureDetector.java b/OsmAnd/src/net/osmand/plus/helpers/LockGestureDetector.java new file mode 100644 index 00000000000..c8e46621207 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/helpers/LockGestureDetector.java @@ -0,0 +1,86 @@ +package net.osmand.plus.helpers; + +import android.util.Pair; +import android.view.GestureDetector; +import android.view.MotionEvent; + +import androidx.annotation.NonNull; + +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.quickaction.QuickAction; +import net.osmand.plus.quickaction.actions.LockScreenAction; +import net.osmand.plus.utils.AndroidUtils; +import net.osmand.plus.views.controls.maphudbuttons.QuickActionButton; +import net.osmand.plus.views.layers.MapQuickActionLayer; +import net.osmand.plus.views.mapwidgets.configure.buttons.QuickActionButtonState; + +public class LockGestureDetector extends GestureDetector { + private final MapActivity mapActivity; + private Pair pressedLockAction; + + public LockGestureDetector(@NonNull MapActivity mapActivity, @NonNull OnGestureListener listener) { + super(mapActivity, listener); + this.mapActivity = mapActivity; + } + + @Override + public boolean onTouchEvent(MotionEvent ev) { + if (ev.getAction() == MotionEvent.ACTION_DOWN) { + pressedLockAction = getPressedLockAction(mapActivity, ev); + if (pressedLockAction != null) { + pressedLockAction.second.setPressed(true); + } + } else if (ev.getAction() == MotionEvent.ACTION_UP) { + if (pressedLockAction != null) { + pressedLockAction.second.setPressed(false); + } + } + return super.onTouchEvent(ev); + } + + private static Pair getPressedLockAction(@NonNull MapActivity mapActivity, @NonNull MotionEvent ev) { + float x = ev.getRawX(); + float y = ev.getRawY(); + QuickActionButton quickActionButton = null; + QuickAction lockAction = null; + MapQuickActionLayer quickActionLayer = mapActivity.getMapLayers().getMapQuickActionLayer(); + for (QuickActionButton actionButton : quickActionLayer.getActionButtons()) { + QuickActionButtonState buttonState = actionButton.getButtonState(); + if (buttonState != null) { + for (QuickAction action : buttonState.getQuickActions()) { + if (action instanceof LockScreenAction && AndroidUtils.getViewBoundOnScreen(actionButton).contains((int) x, (int) y)) { + quickActionButton = actionButton; + lockAction = action; + } + } + } + } + if (lockAction != null) { + return new Pair<>(lockAction, quickActionButton); + } + return null; + } + + private static OnGestureListener getOnGestureListener(@NonNull MapActivity mapActivity) { + return new GestureDetector.SimpleOnGestureListener() { + @Override + public boolean onSingleTapConfirmed(@NonNull MotionEvent e) { + Pair pressedLockAction = getPressedLockAction(mapActivity, e); + + if (pressedLockAction != null) { + QuickAction lockAction = pressedLockAction.first; + QuickActionButtonState buttonState = pressedLockAction.second.getButtonState(); + if (buttonState != null) { + mapActivity.getMapLayers().getMapQuickActionLayer().onActionSelected(buttonState, lockAction); + return true; + } + } + return false; + } + }; + } + + public static LockGestureDetector getDetector(@NonNull MapActivity mapActivity) { + return new LockGestureDetector(mapActivity, getOnGestureListener(mapActivity)); + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/helpers/LockHelper.java b/OsmAnd/src/net/osmand/plus/helpers/LockHelper.java index ce2010aac69..888e466133d 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/LockHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/LockHelper.java @@ -1,7 +1,5 @@ package net.osmand.plus.helpers; -import static net.osmand.plus.helpers.AndroidUiHelper.isViewInBounds; - import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; @@ -28,9 +26,6 @@ import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.backend.preferences.CommonPreference; import net.osmand.plus.routing.VoiceRouter.VoiceMessageListener; -import net.osmand.plus.views.controls.maphudbuttons.QuickActionButton; -import net.osmand.plus.views.layers.MapQuickActionLayer; -import net.osmand.plus.views.mapwidgets.configure.buttons.QuickActionButtonState; import org.apache.commons.logging.Log; @@ -42,7 +37,7 @@ public class LockHelper implements SensorEventListener { private static final int SENSOR_SENSITIVITY = 4; - private static boolean LOCK_SCREEN = false; + private static boolean lockScreen = false; @Nullable private WakeLock wakeLock; @@ -137,11 +132,11 @@ public void lock() { } public void toggleLockScreen(){ - LOCK_SCREEN = !LOCK_SCREEN; + lockScreen = !lockScreen; } public boolean isScreenLocked(){ - return LOCK_SCREEN; + return lockScreen; } private void timedUnlock(long millis) { @@ -225,87 +220,8 @@ public void setLockUIAdapter(@Nullable LockUIAdapter adapter) { public GestureDetector getLockGestureDetector(@NonNull MapActivity mapActivity) { if (gestureDetector == null) { - gestureDetector = new LockGestureDetector(mapActivity, new GestureDetector.OnGestureListener() { - @Override - public boolean onDown(@NonNull MotionEvent e) { - return false; - } - - @Override - public void onShowPress(@NonNull MotionEvent e) { - - } - - @Override - public boolean onSingleTapUp(@NonNull MotionEvent e) { - QuickActionButton actionButton = getPressedQuickActionButton(mapActivity, e); - if (actionButton != null) { - actionButton.performClick(); - return true; - } - return false; - } - - @Override - public boolean onScroll(@Nullable MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY) { - return false; - } - - @Override - public void onLongPress(@NonNull MotionEvent e) { - - } - - @Override - public boolean onFling(@Nullable MotionEvent e1, @NonNull MotionEvent e2, float velocityX, float velocityY) { - return false; - } - }); + gestureDetector = LockGestureDetector.getDetector(mapActivity); } - return gestureDetector; } - - public class LockGestureDetector extends GestureDetector { - private final MapActivity mapActivity; - private QuickActionButton pressedActionButton; - - public LockGestureDetector(@NonNull MapActivity mapActivity, @NonNull OnGestureListener listener) { - super(mapActivity, listener); - this.mapActivity = mapActivity; - } - - @Override - public boolean onTouchEvent(MotionEvent ev) { - if (ev.getAction() == MotionEvent.ACTION_DOWN) { - pressedActionButton = getPressedQuickActionButton(mapActivity, ev); - if (pressedActionButton != null) { - pressedActionButton.setPressed(true); - } - } else if (ev.getAction() == MotionEvent.ACTION_UP) { - if (pressedActionButton != null) { - pressedActionButton.setPressed(false); - } - } - return super.onTouchEvent(ev); - } - } - - private QuickActionButton getPressedQuickActionButton(@NonNull MapActivity mapActivity, @NonNull MotionEvent ev) { - float x = ev.getX(); - float y = ev.getY(); - - MapQuickActionLayer quickActionLayer = mapActivity.getMapLayers().getMapQuickActionLayer(); - for (QuickActionButton actionButton : quickActionLayer.getActionButtons()) { - QuickActionButtonState buttonState = actionButton.getButtonState(); - if (buttonState != null) { - for (QuickAction action : buttonState.getQuickActions()) { - if (action instanceof LockScreenAction && isViewInBounds(actionButton, x, y)) { - return actionButton; - } - } - } - } - return null; - } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/views/controls/maphudbuttons/QuickActionButton.java b/OsmAnd/src/net/osmand/plus/views/controls/maphudbuttons/QuickActionButton.java index 4e6d472192d..d7e6cdd5094 100644 --- a/OsmAnd/src/net/osmand/plus/views/controls/maphudbuttons/QuickActionButton.java +++ b/OsmAnd/src/net/osmand/plus/views/controls/maphudbuttons/QuickActionButton.java @@ -14,7 +14,6 @@ import net.osmand.plus.R; import net.osmand.plus.quickaction.QuickAction; -import net.osmand.plus.quickaction.actions.LockScreenAction; import net.osmand.plus.views.layers.MapQuickActionLayer; import net.osmand.plus.views.mapwidgets.configure.buttons.QuickActionButtonState; @@ -42,13 +41,7 @@ public QuickActionButton(@NonNull Context context, @Nullable AttributeSet attrs, setOnClickListener(v -> { mapActivity.getFragmentsHelper().dismissCardDialog(); - if (app.getLockHelper().isScreenLocked()) { - for (QuickAction action : buttonState.getQuickActions()) { - if (action instanceof LockScreenAction) { - layer.onActionSelected(buttonState, action); - } - } - } else if (!buttonState.isDefaultButton() && buttonState.isSingleAction()) { + if (!buttonState.isDefaultButton() && buttonState.isSingleAction()) { List actions = buttonState.getQuickActions(); layer.onActionSelected(buttonState, actions.get(0)); } else if (!showTutorialIfNeeded()) { diff --git a/OsmAnd/src/net/osmand/plus/views/layers/MapQuickActionLayer.java b/OsmAnd/src/net/osmand/plus/views/layers/MapQuickActionLayer.java index 383a71b0ca1..747329d0b55 100644 --- a/OsmAnd/src/net/osmand/plus/views/layers/MapQuickActionLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/layers/MapQuickActionLayer.java @@ -139,6 +139,7 @@ private void updateButtons() { } } + @NonNull public List getActionButtons() { return actionButtons; }