From 440ad5828b003e98472ff88732c62c000b0c0d34 Mon Sep 17 00:00:00 2001 From: Everton Hermann Date: Sun, 19 Jul 2020 16:45:00 +0200 Subject: [PATCH 1/2] Add aidl interface to lock and unlock the screen. For instance it call allow a service to wake up the navigation using media buttons --- .../osmand/aidlapi/IOsmAndAidlInterface.aidl | 6 +++ .../aidlapi/lock/SetLockStateParams.aidl | 3 ++ .../aidlapi/lock/SetLockStateParams.java | 41 +++++++++++++++++++ .../net/osmand/aidl/IOsmAndAidlInterface.aidl | 5 +++ OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java | 30 +++++++++++++- .../net/osmand/aidl/OsmandAidlService.java | 11 +++++ .../net/osmand/aidl/OsmandAidlServiceV2.java | 12 ++++++ .../net/osmand/plus/helpers/LockHelper.java | 4 +- 8 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 OsmAnd-api/src/net/osmand/aidlapi/lock/SetLockStateParams.aidl create mode 100644 OsmAnd-api/src/net/osmand/aidlapi/lock/SetLockStateParams.java diff --git a/OsmAnd-api/src/net/osmand/aidlapi/IOsmAndAidlInterface.aidl b/OsmAnd-api/src/net/osmand/aidlapi/IOsmAndAidlInterface.aidl index 45bf3907f7..1b1433178a 100644 --- a/OsmAnd-api/src/net/osmand/aidlapi/IOsmAndAidlInterface.aidl +++ b/OsmAnd-api/src/net/osmand/aidlapi/IOsmAndAidlInterface.aidl @@ -95,6 +95,7 @@ import net.osmand.aidlapi.mapmarker.RemoveMapMarkersParams; import net.osmand.aidlapi.quickaction.QuickActionParams; import net.osmand.aidlapi.quickaction.QuickActionInfoParams; +import net.osmand.aidlapi.lock.SetLockStateParams; // NOTE: Add new methods at the end of file!!! @@ -841,4 +842,9 @@ interface IOsmAndAidlInterface { boolean executeQuickAction(in QuickActionParams params); boolean getQuickActionsInfo(out List quickActions); + /** + * Toggle Lock/Unlock screen. + */ + + boolean setLockState(in SetLockStateParams params); } \ No newline at end of file diff --git a/OsmAnd-api/src/net/osmand/aidlapi/lock/SetLockStateParams.aidl b/OsmAnd-api/src/net/osmand/aidlapi/lock/SetLockStateParams.aidl new file mode 100644 index 0000000000..332f768344 --- /dev/null +++ b/OsmAnd-api/src/net/osmand/aidlapi/lock/SetLockStateParams.aidl @@ -0,0 +1,3 @@ +package net.osmand.aidlapi.lock; + +parcelable SetLockStateParams; \ No newline at end of file diff --git a/OsmAnd-api/src/net/osmand/aidlapi/lock/SetLockStateParams.java b/OsmAnd-api/src/net/osmand/aidlapi/lock/SetLockStateParams.java new file mode 100644 index 0000000000..babb64c8b5 --- /dev/null +++ b/OsmAnd-api/src/net/osmand/aidlapi/lock/SetLockStateParams.java @@ -0,0 +1,41 @@ +package net.osmand.aidlapi.lock; + +import android.os.Bundle; +import android.os.Parcel; + +import net.osmand.aidlapi.AidlParams; + +public class SetLockStateParams extends AidlParams { + private boolean lock; + public SetLockStateParams(boolean lock) { + this.lock = lock; + } + + public SetLockStateParams(Parcel in) { + readFromParcel(in); + } + + public static final Creator CREATOR = new Creator() { + @Override + public SetLockStateParams createFromParcel(Parcel in) { + return new SetLockStateParams(in); + } + + @Override + public SetLockStateParams[] newArray(int size) { + return new SetLockStateParams[size]; + } + }; + public boolean getLockState(){ + return lock; + } + @Override + public void writeToBundle(Bundle bundle) { + bundle.putBoolean("lock", this.lock); + } + + @Override + protected void readFromBundle(Bundle bundle) { + lock = bundle.getBoolean("lock"); + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl b/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl index 91410a35c7..6a79918aad 100644 --- a/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl +++ b/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl @@ -96,6 +96,7 @@ import net.osmand.aidl.mapmarker.RemoveMapMarkersParams; import net.osmand.aidl.quickaction.QuickActionParams; import net.osmand.aidl.quickaction.QuickActionInfoParams; +import net.osmand.aidlapi.lock.SetLockStateParams; // NOTE: Add new methods at the end of file!!! @@ -856,4 +857,8 @@ interface IOsmAndAidlInterface { boolean executeQuickAction(in QuickActionParams params); boolean getQuickActionsInfo(out List quickActions); + /** + * Toggle Lock/Unlock screen. + */ + boolean setLockState(in SetLockStateParams params); } diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index 96686c9956..8168e5dd1c 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -44,6 +44,7 @@ import net.osmand.plus.AppInitializer; import net.osmand.plus.AppInitializer.AppInitializeListener; import net.osmand.plus.AppInitializer.InitEvents; import net.osmand.plus.dialogs.GpxAppearanceAdapter; +import net.osmand.plus.helpers.LockHelper; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.ContextMenuAdapter; import net.osmand.plus.ContextMenuItem; @@ -177,6 +178,7 @@ public class OsmandAidlApi { private static final String AIDL_EXECUTE_QUICK_ACTION = "aidl_execute_quick_action"; private static final String AIDL_QUICK_ACTION_NUMBER = "aidl_quick_action_number"; + private static final String AIDL_LOCK_STATE = "lock_state"; private static final ApplicationMode DEFAULT_PROFILE = ApplicationMode.CAR; @@ -228,6 +230,7 @@ public class OsmandAidlApi { registerShowSqliteDbFileReceiver(mapActivity); registerHideSqliteDbFileReceiver(mapActivity); registerExecuteQuickActionReceiver(mapActivity); + registerLockStateReceiver(mapActivity); initOsmandTelegram(); app.getAppCustomization().addListener(mapActivity); aMapPointUpdateListener = mapActivity; @@ -853,7 +856,24 @@ public class OsmandAidlApi { }; registerReceiver(executeQuickActionReceiver, mapActivity, AIDL_EXECUTE_QUICK_ACTION); } - + + private void registerLockStateReceiver(MapActivity mapActivity) { + BroadcastReceiver lockStateReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + boolean lock = intent.getBooleanExtra(AIDL_LOCK_STATE,false); + LockHelper lh = app.getLockHelper(); + if(lock) { + lh.lock(); + } + else{ + lh.unlock(); + } + } + }; + registerReceiver(lockStateReceiver, mapActivity, AIDL_LOCK_STATE); + } + public void registerMapLayers(@NonNull MapActivity mapActivity) { for (ConnectedApp connectedApp : connectedApps.values()) { connectedApp.registerMapLayers(mapActivity); @@ -1691,7 +1711,13 @@ public class OsmandAidlApi { app.sendBroadcast(intent); return true; } - + boolean setLockState(boolean lock) { + Intent intent = new Intent(); + intent.setAction(AIDL_LOCK_STATE); + intent.putExtra(AIDL_LOCK_STATE, lock); + app.sendBroadcast(intent); + return true; + } boolean search(final String searchQuery, final int searchType, final double latitude, final double longitude, final int radiusLevel, final int totalLimit, final SearchCompleteCallback callback) { if (Algorithms.isEmpty(searchQuery) || latitude == 0 || longitude == 0 || callback == null) { diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java index 8763156abe..c9fa139f98 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java @@ -85,6 +85,7 @@ import net.osmand.aidl.quickaction.QuickActionParams; import net.osmand.aidl.search.SearchParams; import net.osmand.aidl.search.SearchResult; import net.osmand.aidl.tiles.ASqliteDbFile; +import net.osmand.aidlapi.lock.SetLockStateParams; import net.osmand.data.LatLon; import net.osmand.plus.settings.backend.OsmAndAppCustomization; import net.osmand.plus.OsmandApplication; @@ -1326,6 +1327,16 @@ public class OsmandAidlService extends Service implements AidlCallbackListener { return false; } } + @Override + public boolean setLockState(SetLockStateParams params) { + try { + OsmandAidlApi api = getApi("setLockState"); + return api != null && api.setLockState(params.getLockState()); + } catch (Exception e) { + handleException(e); + return false; + } + } }; private void setCustomization(OsmandAidlApi api, CustomizationInfoParams params) { diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlServiceV2.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlServiceV2.java index 1953e185fd..be86e7a5f5 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlServiceV2.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlServiceV2.java @@ -15,6 +15,7 @@ import net.osmand.PlatformUtil; import net.osmand.aidl.OsmandAidlApi.GpxBitmapCreatedCallback; import net.osmand.aidl.OsmandAidlApi.OsmandAppInitCallback; import net.osmand.aidl.OsmandAidlApi.SearchCompleteCallback; +import net.osmand.aidlapi.lock.SetLockStateParams; import net.osmand.aidlapi.IOsmAndAidlCallback; import net.osmand.aidlapi.IOsmAndAidlInterface; import net.osmand.aidlapi.calculateroute.CalculateRouteParams; @@ -1259,6 +1260,17 @@ public class OsmandAidlServiceV2 extends Service implements AidlCallbackListener return false; } } + + @Override + public boolean setLockState(SetLockStateParams params) { + try { + OsmandAidlApi api = getApi("setLockState"); + return api != null && api.setLockState(params.getLockState()); + } catch (Exception e) { + handleException(e); + return false; + } + } }; private void setCustomization(OsmandAidlApi api, CustomizationInfoParams params) { diff --git a/OsmAnd/src/net/osmand/plus/helpers/LockHelper.java b/OsmAnd/src/net/osmand/plus/helpers/LockHelper.java index 9bf628803a..b0f34058d3 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/LockHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/LockHelper.java @@ -104,7 +104,7 @@ public class LockHelper implements SensorEventListener { } @SuppressLint("WakelockTimeout") - private void unlock() { + public void unlock() { if (lockUIAdapter != null) { lockUIAdapter.unlock(); } @@ -116,7 +116,7 @@ public class LockHelper implements SensorEventListener { } } - private void lock() { + public void lock() { releaseWakeLocks(); if (lockUIAdapter != null) { boolean useSystemTimeout = useSystemScreenTimeout.get(); From b5c6593743cb58d31dcaba927989cbbfa9a3134e Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Mon, 20 Jul 2020 21:49:00 +0300 Subject: [PATCH 2/2] Fix support for old aidl api --- .../aidlapi/lock/SetLockStateParams.java | 6 ++- .../net/osmand/aidl/IOsmAndAidlInterface.aidl | 2 +- OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java | 31 ++++++------- .../net/osmand/aidl/OsmandAidlService.java | 2 +- .../osmand/aidl/lock/SetLockStateParams.aidl | 3 ++ .../osmand/aidl/lock/SetLockStateParams.java | 45 +++++++++++++++++++ 6 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 OsmAnd/src/net/osmand/aidl/lock/SetLockStateParams.aidl create mode 100644 OsmAnd/src/net/osmand/aidl/lock/SetLockStateParams.java diff --git a/OsmAnd-api/src/net/osmand/aidlapi/lock/SetLockStateParams.java b/OsmAnd-api/src/net/osmand/aidlapi/lock/SetLockStateParams.java index babb64c8b5..0b71c750df 100644 --- a/OsmAnd-api/src/net/osmand/aidlapi/lock/SetLockStateParams.java +++ b/OsmAnd-api/src/net/osmand/aidlapi/lock/SetLockStateParams.java @@ -6,7 +6,9 @@ import android.os.Parcel; import net.osmand.aidlapi.AidlParams; public class SetLockStateParams extends AidlParams { + private boolean lock; + public SetLockStateParams(boolean lock) { this.lock = lock; } @@ -26,9 +28,11 @@ public class SetLockStateParams extends AidlParams { return new SetLockStateParams[size]; } }; - public boolean getLockState(){ + + public boolean getLockState() { return lock; } + @Override public void writeToBundle(Bundle bundle) { bundle.putBoolean("lock", this.lock); diff --git a/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl b/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl index 6a79918aad..153c0591cd 100644 --- a/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl +++ b/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl @@ -96,7 +96,7 @@ import net.osmand.aidl.mapmarker.RemoveMapMarkersParams; import net.osmand.aidl.quickaction.QuickActionParams; import net.osmand.aidl.quickaction.QuickActionInfoParams; -import net.osmand.aidlapi.lock.SetLockStateParams; +import net.osmand.aidl.lock.SetLockStateParams; // NOTE: Add new methods at the end of file!!! diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index 8168e5dd1c..6b3b4e5373 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -43,9 +43,6 @@ import net.osmand.data.PointDescription; import net.osmand.plus.AppInitializer; import net.osmand.plus.AppInitializer.AppInitializeListener; import net.osmand.plus.AppInitializer.InitEvents; -import net.osmand.plus.dialogs.GpxAppearanceAdapter; -import net.osmand.plus.helpers.LockHelper; -import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.ContextMenuAdapter; import net.osmand.plus.ContextMenuItem; import net.osmand.plus.FavouritesDbHelper; @@ -54,16 +51,15 @@ import net.osmand.plus.GpxSelectionHelper; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.MapMarkersHelper; import net.osmand.plus.MapMarkersHelper.MapMarker; -import net.osmand.plus.settings.backend.OsmAndAppCustomization; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; -import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.SQLiteTileSource; -import net.osmand.plus.settings.backend.SettingsHelper; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.audionotes.AudioVideoNotesPlugin; +import net.osmand.plus.dialogs.GpxAppearanceAdapter; import net.osmand.plus.helpers.ColorDialogs; import net.osmand.plus.helpers.ExternalApiHelper; +import net.osmand.plus.helpers.LockHelper; import net.osmand.plus.mapcontextmenu.MapContextMenu; import net.osmand.plus.mapcontextmenu.other.IContextMenuButtonListener; import net.osmand.plus.monitoring.OsmandMonitoringPlugin; @@ -75,6 +71,10 @@ import net.osmand.plus.routing.IRoutingDataUpdateListener; import net.osmand.plus.routing.RouteCalculationResult.NextDirectionInfo; import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.routing.VoiceRouter; +import net.osmand.plus.settings.backend.ApplicationMode; +import net.osmand.plus.settings.backend.OsmAndAppCustomization; +import net.osmand.plus.settings.backend.OsmandSettings; +import net.osmand.plus.settings.backend.SettingsHelper; import net.osmand.plus.views.AidlMapLayer; import net.osmand.plus.views.MapInfoLayer; import net.osmand.plus.views.OsmandMapLayer; @@ -856,24 +856,23 @@ public class OsmandAidlApi { }; registerReceiver(executeQuickActionReceiver, mapActivity, AIDL_EXECUTE_QUICK_ACTION); } - + private void registerLockStateReceiver(MapActivity mapActivity) { BroadcastReceiver lockStateReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - boolean lock = intent.getBooleanExtra(AIDL_LOCK_STATE,false); - LockHelper lh = app.getLockHelper(); - if(lock) { - lh.lock(); - } - else{ - lh.unlock(); + LockHelper lockHelper = app.getLockHelper(); + boolean lock = intent.getBooleanExtra(AIDL_LOCK_STATE, false); + if (lock) { + lockHelper.lock(); + } else { + lockHelper.unlock(); } } }; registerReceiver(lockStateReceiver, mapActivity, AIDL_LOCK_STATE); } - + public void registerMapLayers(@NonNull MapActivity mapActivity) { for (ConnectedApp connectedApp : connectedApps.values()) { connectedApp.registerMapLayers(mapActivity); @@ -1711,6 +1710,7 @@ public class OsmandAidlApi { app.sendBroadcast(intent); return true; } + boolean setLockState(boolean lock) { Intent intent = new Intent(); intent.setAction(AIDL_LOCK_STATE); @@ -1718,6 +1718,7 @@ public class OsmandAidlApi { app.sendBroadcast(intent); return true; } + boolean search(final String searchQuery, final int searchType, final double latitude, final double longitude, final int radiusLevel, final int totalLimit, final SearchCompleteCallback callback) { if (Algorithms.isEmpty(searchQuery) || latitude == 0 || longitude == 0 || callback == null) { diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java index c9fa139f98..5dcd6626bf 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java @@ -85,7 +85,7 @@ import net.osmand.aidl.quickaction.QuickActionParams; import net.osmand.aidl.search.SearchParams; import net.osmand.aidl.search.SearchResult; import net.osmand.aidl.tiles.ASqliteDbFile; -import net.osmand.aidlapi.lock.SetLockStateParams; +import net.osmand.aidl.lock.SetLockStateParams; import net.osmand.data.LatLon; import net.osmand.plus.settings.backend.OsmAndAppCustomization; import net.osmand.plus.OsmandApplication; diff --git a/OsmAnd/src/net/osmand/aidl/lock/SetLockStateParams.aidl b/OsmAnd/src/net/osmand/aidl/lock/SetLockStateParams.aidl new file mode 100644 index 0000000000..c6ffbd4be8 --- /dev/null +++ b/OsmAnd/src/net/osmand/aidl/lock/SetLockStateParams.aidl @@ -0,0 +1,3 @@ +package net.osmand.aidl.lock; + +parcelable SetLockStateParams; \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/aidl/lock/SetLockStateParams.java b/OsmAnd/src/net/osmand/aidl/lock/SetLockStateParams.java new file mode 100644 index 0000000000..5de87873e2 --- /dev/null +++ b/OsmAnd/src/net/osmand/aidl/lock/SetLockStateParams.java @@ -0,0 +1,45 @@ +package net.osmand.aidl.lock; + +import android.os.Parcel; +import android.os.Parcelable; + +public class SetLockStateParams implements Parcelable { + + private boolean lock; + + public SetLockStateParams(boolean lock) { + this.lock = lock; + } + + public SetLockStateParams(Parcel in) { + readFromParcel(in); + } + + public static final Creator CREATOR = new Creator() { + @Override + public SetLockStateParams createFromParcel(Parcel in) { + return new SetLockStateParams(in); + } + + @Override + public SetLockStateParams[] newArray(int size) { + return new SetLockStateParams[size]; + } + }; + + public boolean getLockState() { + return lock; + } + + public void writeToParcel(Parcel out, int flags) { + out.writeByte((byte) (lock ? 1 : 0)); + } + + private void readFromParcel(Parcel in) { + lock = in.readByte() == 1; + } + + public int describeContents() { + return 0; + } +} \ No newline at end of file