Autodetect OsmAnd package name and make it configurable

This commit is contained in:
Arjan Schrijver 2023-11-02 17:33:01 +01:00
parent 2ef44e766e
commit 8add6c4da9
4 changed files with 73 additions and 28 deletions

View File

@ -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<CharSequence> 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<CharSequence> findInstalledOsmandPackages() {
List<CharSequence> 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;
}
}
}

View File

@ -3116,6 +3116,19 @@
<item>de.dennisguse.opentracks.nightly</item>
</string-array>
<string-array name="osmand_package_names">
<item>@string/automatic</item>
<item>net.osmand.plus</item>
<item>net.osmand</item>
<item>net.osmand.dev</item>
</string-array>
<string-array name="osmand_package_names_values">
<item>autodetect</item>
<item>net.osmand.plus</item>
<item>net.osmand</item>
<item>net.osmand.dev</item>
</string-array>
<string-array name="pref_transliteration_languages">
<item>@string/arabic</item>
<item>@string/bengali</item>

View File

@ -2386,7 +2386,6 @@
<string name="prefs_wena3_vibration_siren">Siren</string>
<string name="prefs_wena3_vibration_short">Short</string>
<string name="prefs_wena3_led_none">No LED</string>
<string name="temperature_scale_cf">Temperature scale</string>
<string name="temperature_scale_cf_summary">Select whether device uses Celsius or Fahrenheit scale.</string>
<string name="temperature_scale_celsius">Celsius</string>
@ -2397,7 +2396,8 @@
<string name="call_rejection_method_ignore">Ignore (silence)</string>
<string name="pref_call_rejection_method_title">Call rejection method</string>
<string name="pref_call_rejection_method_summary">Which action is taken when an incoming call is rejected from the watch</string>
<string name="pref_title_prefix_notification_with_app">App name in notification</string>
<string name="pref_summary_prefix_notification_with_app">Prefix notification title with name of source application</string>
<string name="pref_title_osmand_packagename">OsmAnd package name</string>
<string name="pref_summary_osmand_packagename">Used for selecting the version of OsmAnd to connect to</string>
</resources>

View File

@ -218,6 +218,14 @@
android:layout="@layout/preference_checkbox"
android:title="@string/pref_title_whenscreenon"
app:iconSpaceReserved="false" />
<ListPreference
android:defaultValue="autodetect"
android:entries="@array/osmand_package_names"
android:entryValues="@array/osmand_package_names_values"
android:key="pref_key_osmand_packagename"
android:summary="@string/pref_summary_osmand_packagename"
android:title="@string/pref_title_osmand_packagename"
app:iconSpaceReserved="false" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/preferences_category_device_specific_settings"