Skip to content

Commit

Permalink
code review fix
Browse files Browse the repository at this point in the history
  • Loading branch information
0xRe1nk0 committed Sep 30, 2024
1 parent 50883fb commit 23c63c3
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 96 deletions.
86 changes: 86 additions & 0 deletions OsmAnd/src/net/osmand/plus/helpers/LockGestureDetector.java
Original file line number Diff line number Diff line change
@@ -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<QuickAction, QuickActionButton> 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<QuickAction, QuickActionButton> 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<QuickAction, QuickActionButton> 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));
}
}
92 changes: 4 additions & 88 deletions OsmAnd/src/net/osmand/plus/helpers/LockHelper.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<QuickAction> actions = buttonState.getQuickActions();
layer.onActionSelected(buttonState, actions.get(0));
} else if (!showTutorialIfNeeded()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ private void updateButtons() {
}
}

@NonNull
public List<QuickActionButton> getActionButtons() {
return actionButtons;
}
Expand Down

0 comments on commit 23c63c3

Please sign in to comment.