From 8add6c4da94e56256ed5ed1de1d3dfa4f43bfe42 Mon Sep 17 00:00:00 2001 From: Arjan Schrijver Date: Thu, 2 Nov 2023 17:33:01 +0100 Subject: [PATCH] Autodetect OsmAnd package name and make it configurable --- .../externalevents/OsmandEventReceiver.java | 76 ++++++++++++------- app/src/main/res/values/arrays.xml | 13 ++++ app/src/main/res/values/strings.xml | 4 +- app/src/main/res/xml/preferences.xml | 8 ++ 4 files changed, 73 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/OsmandEventReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/OsmandEventReceiver.java index dcb9ada87..5dbb2c8d4 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/OsmandEventReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/OsmandEventReceiver.java @@ -5,11 +5,11 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; +import android.content.pm.PackageManager; import android.os.IBinder; import android.os.PowerManager; import android.os.RemoteException; import android.view.KeyEvent; -import android.widget.Toast; import net.osmand.aidlapi.IOsmAndAidlCallback; import net.osmand.aidlapi.IOsmAndAidlInterface; @@ -23,19 +23,17 @@ import net.osmand.aidlapi.search.SearchResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; import java.util.List; import nodomain.freeyourgadget.gadgetbridge.GBApplication; +import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.model.NavigationInfoSpec; -import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class OsmandEventReceiver { private static final Logger LOG = LoggerFactory.getLogger(OsmandEventReceiver.class); - private static final String OSMAND_PLUS_PACKAGE_NAME = "net.osmand.plus"; - private static final String OSMAND_PACKAGE_NAME = OSMAND_PLUS_PACKAGE_NAME; - private final Application app; private IOsmAndAidlInterface mIOsmAndAidlInterface; @@ -117,14 +115,22 @@ public class OsmandEventReceiver { private boolean bindService() { if (mIOsmAndAidlInterface == null) { + List installedOsmandPackages = findInstalledOsmandPackages(); + if (installedOsmandPackages.isEmpty()) { + LOG.warn("OsmAnd is not installed"); + return false; + } + Prefs prefs = GBApplication.getPrefs(); + String packageName = prefs.getString("pref_key_osmand_packagename", "autodetect"); + if (packageName.equals("autodetect")) packageName = installedOsmandPackages.get(0).toString(); Intent intent = new Intent("net.osmand.aidl.OsmandAidlServiceV2"); - intent.setPackage(OSMAND_PACKAGE_NAME); + intent.setPackage(packageName); boolean res = app.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); if (res) { - LOG.info("Bound to OsmAnd service"); + LOG.info("Bound to OsmAnd service (package "+packageName+")"); return true; } else { - LOG.warn("Could not bind to OsmAnd service"); + LOG.warn("Could not bind to OsmAnd service (package "+packageName+")"); return false; } } else { @@ -147,7 +153,7 @@ public class OsmandEventReceiver { return mIOsmAndAidlInterface.registerForNavigationUpdates(params, mIOsmAndAidlCallback); } catch (RemoteException e) { - LOG.error("could not subscribe to navication updates", e); + LOG.error("could not subscribe to navigation updates", e); } } return -1L; @@ -173,23 +179,41 @@ public class OsmandEventReceiver { return true; } - /** - * Method to register for Voice Router voice messages during navigation. Notifies user about voice messages. - * - * @param subscribeToUpdates (boolean) - boolean flag to subscribe or unsubscribe from messages - * @param callbackId (long) - id of callback, needed to unsubscribe from messages - */ - public long registerForVoiceRouterMessages(boolean subscribeToUpdates, long callbackId) { - ANavigationVoiceRouterMessageParams params = new ANavigationVoiceRouterMessageParams(); - params.setCallbackId(callbackId); - params.setSubscribeToUpdates(subscribeToUpdates); - if (mIOsmAndAidlInterface != null) { - try { - return mIOsmAndAidlInterface.registerForVoiceRouterMessages(params, mIOsmAndAidlCallback); - } catch (RemoteException e) { - e.printStackTrace(); - } + /** + * Method to register for Voice Router voice messages during navigation. Notifies user about voice messages. + * + * @param subscribeToUpdates (boolean) - boolean flag to subscribe or unsubscribe from messages + * @param callbackId (long) - id of callback, needed to unsubscribe from messages + */ + public long registerForVoiceRouterMessages(boolean subscribeToUpdates, long callbackId) { + ANavigationVoiceRouterMessageParams params = new ANavigationVoiceRouterMessageParams(); + params.setCallbackId(callbackId); + params.setSubscribeToUpdates(subscribeToUpdates); + if (mIOsmAndAidlInterface != null) { + try { + return mIOsmAndAidlInterface.registerForVoiceRouterMessages(params, mIOsmAndAidlCallback); + } catch (RemoteException e) { + LOG.error("could not register for voice router messages", e); } - return -1L; } + return -1L; + } + + public List findInstalledOsmandPackages() { + List installedPackages = new ArrayList<>(); + for (String knownPackage : app.getBaseContext().getResources().getStringArray(R.array.osmand_package_names)) { + if (isPackageInstalled(knownPackage)) { + installedPackages.add(knownPackage); + } + } + return installedPackages; + } + + private boolean isPackageInstalled(final String packageName) { + try { + return app.getBaseContext().getPackageManager().getApplicationInfo(packageName, 0).enabled; + } catch (PackageManager.NameNotFoundException e) { + return false; + } + } } \ No newline at end of file diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 9f0cb3fcb..f832b4e14 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -3116,6 +3116,19 @@ de.dennisguse.opentracks.nightly + + @string/automatic + net.osmand.plus + net.osmand + net.osmand.dev + + + autodetect + net.osmand.plus + net.osmand + net.osmand.dev + + @string/arabic @string/bengali diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2d7151688..ac6b5a32e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2386,7 +2386,6 @@ Siren Short No LED - Temperature scale Select whether device uses Celsius or Fahrenheit scale. Celsius @@ -2397,7 +2396,8 @@ Ignore (silence) Call rejection method Which action is taken when an incoming call is rejected from the watch - App name in notification Prefix notification title with name of source application + OsmAnd package name + Used for selecting the version of OsmAnd to connect to diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index c6f9a2b05..4d9be5373 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -218,6 +218,14 @@ android:layout="@layout/preference_checkbox" android:title="@string/pref_title_whenscreenon" app:iconSpaceReserved="false" /> +