-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20902 from osmandapp/lock_screen
Add lock screen quick action
- Loading branch information
Showing
9 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
OsmAnd/src/net/osmand/plus/helpers/LockGestureDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
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; | ||
QuickAction lockAction; | ||
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; | ||
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
OsmAnd/src/net/osmand/plus/quickaction/actions/LockScreenAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package net.osmand.plus.quickaction.actions; | ||
|
||
import static net.osmand.plus.quickaction.QuickActionIds.LOCK_SCREEN_ACTION; | ||
|
||
import android.content.Context; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.TextView; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import net.osmand.plus.OsmandApplication; | ||
import net.osmand.plus.R; | ||
import net.osmand.plus.activities.MapActivity; | ||
import net.osmand.plus.quickaction.QuickAction; | ||
import net.osmand.plus.quickaction.QuickActionType; | ||
|
||
public class LockScreenAction extends QuickAction { | ||
|
||
public static final QuickActionType TYPE = new QuickActionType(LOCK_SCREEN_ACTION, | ||
"lock_screen_action", LockScreenAction.class) | ||
.nameRes(R.string.lock_screen) | ||
.iconRes(R.drawable.ic_action_touch_screen_lock) | ||
.category(QuickActionType.INTERFACE) | ||
.nameActionRes(R.string.quick_action_verb_turn_on_off); | ||
|
||
public LockScreenAction() { | ||
super(TYPE); | ||
} | ||
|
||
public LockScreenAction(QuickAction quickAction) { | ||
super(quickAction); | ||
} | ||
|
||
@Override | ||
public void execute(@NonNull MapActivity mapActivity) { | ||
mapActivity.getMyApplication().getLockHelper().toggleLockScreen(); | ||
mapActivity.getMapLayers().getMapQuickActionLayer().refreshLayer(); | ||
} | ||
|
||
@Override | ||
public void drawUI(@NonNull ViewGroup parent, @NonNull MapActivity mapActivity) { | ||
View view = LayoutInflater.from(parent.getContext()) | ||
.inflate(R.layout.quick_action_with_text, parent, false); | ||
((TextView) view.findViewById(R.id.text)).setText(mapActivity.getString(R.string.lock_screen_description)); | ||
parent.addView(view); | ||
} | ||
|
||
@Override | ||
public int getIconRes(Context context) { | ||
OsmandApplication app = (OsmandApplication) context.getApplicationContext(); | ||
return app.getLockHelper().isScreenLocked() ? R.drawable.ic_action_touch_screen_unlock : R.drawable.ic_action_touch_screen_lock; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters