From bd671aeeff6b13dd1f45d8e3aed27a824cfa62ab Mon Sep 17 00:00:00 2001 From: Marvin W Date: Mon, 22 Feb 2016 23:10:22 +0100 Subject: [PATCH] Add init intent support (API v2) For proper dynamic permission support --- extern/MicroGUiTools | 2 +- extern/UnifiedNlpApi | 2 +- .../java/org/microg/nlp/PackageReceiver.java | 2 +- .../nlp/geocode/AbstractGeocodeService.java | 2 +- .../nlp/ui/AbstractBackendPreference.java | 36 ++++++++++++++++++- .../nlp/ui/GeocoderBackendPreference.java | 15 +++++++- .../nlp/ui/LocationBackendPreference.java | 14 ++++++++ 7 files changed, 67 insertions(+), 6 deletions(-) diff --git a/extern/MicroGUiTools b/extern/MicroGUiTools index 18fb8cb..c6a81f4 160000 --- a/extern/MicroGUiTools +++ b/extern/MicroGUiTools @@ -1 +1 @@ -Subproject commit 18fb8cb8a189d675f8bfbddcfac3f4396c2aba0a +Subproject commit c6a81f4d7d978418352648cecd2545ea575a8caf diff --git a/extern/UnifiedNlpApi b/extern/UnifiedNlpApi index a818694..af1743c 160000 --- a/extern/UnifiedNlpApi +++ b/extern/UnifiedNlpApi @@ -1 +1 @@ -Subproject commit a818694d1d27601d9f00d0fbf36f8eff7f6a15fd +Subproject commit af1743ca443aa005805b5ab2d9f66eb6e7d886e6 diff --git a/unifiednlp-base/src/main/java/org/microg/nlp/PackageReceiver.java b/unifiednlp-base/src/main/java/org/microg/nlp/PackageReceiver.java index d5be49d..194ab99 100644 --- a/unifiednlp-base/src/main/java/org/microg/nlp/PackageReceiver.java +++ b/unifiednlp-base/src/main/java/org/microg/nlp/PackageReceiver.java @@ -38,7 +38,7 @@ public void onReceive(Context context, Intent intent) { } if (preferences.getGeocoderBackends().contains(packageName)) { Log.d(TAG, "Reloading geocoding service for " + packageName); - AbstractGeocodeService.reloadLocationService(context); + AbstractGeocodeService.reloadGeocodeService(context); } } } diff --git a/unifiednlp-base/src/main/java/org/microg/nlp/geocode/AbstractGeocodeService.java b/unifiednlp-base/src/main/java/org/microg/nlp/geocode/AbstractGeocodeService.java index 6e9fb49..b661e3e 100644 --- a/unifiednlp-base/src/main/java/org/microg/nlp/geocode/AbstractGeocodeService.java +++ b/unifiednlp-base/src/main/java/org/microg/nlp/geocode/AbstractGeocodeService.java @@ -24,7 +24,7 @@ import static org.microg.nlp.api.Constants.ACTION_RELOAD_SETTINGS; public abstract class AbstractGeocodeService extends AbstractProviderService { - public static void reloadLocationService(Context context) { + public static void reloadGeocodeService(Context context) { Intent intent = new Intent(ACTION_RELOAD_SETTINGS); intent.setClass(context, GeocodeServiceV1.class); context.startService(intent); diff --git a/unifiednlp-base/src/main/java/org/microg/nlp/ui/AbstractBackendPreference.java b/unifiednlp-base/src/main/java/org/microg/nlp/ui/AbstractBackendPreference.java index 68294fa..1634ca0 100644 --- a/unifiednlp-base/src/main/java/org/microg/nlp/ui/AbstractBackendPreference.java +++ b/unifiednlp-base/src/main/java/org/microg/nlp/ui/AbstractBackendPreference.java @@ -16,11 +16,14 @@ package org.microg.nlp.ui; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.ServiceConnection; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.content.pm.ServiceInfo; +import android.os.IBinder; import android.preference.DialogPreference; import android.util.AttributeSet; import android.view.LayoutInflater; @@ -37,7 +40,10 @@ import java.util.ArrayList; import java.util.List; +import static android.content.Context.BIND_AUTO_CREATE; +import static android.content.Intent.ACTION_VIEW; import static org.microg.nlp.api.Constants.METADATA_BACKEND_ABOUT_ACTIVITY; +import static org.microg.nlp.api.Constants.METADATA_BACKEND_INIT_ACTIVITY; import static org.microg.nlp.api.Constants.METADATA_BACKEND_SETTINGS_ACTIVITY; import static org.microg.nlp.api.Constants.METADATA_BACKEND_SUMMARY; @@ -126,7 +132,7 @@ List intentToKnownBackends(Intent intent) { } private Intent createExternalIntent(BackendInfo backendInfo, String metaName) { - Intent intent = new Intent(Intent.ACTION_VIEW); + Intent intent = new Intent(ACTION_VIEW); intent.setPackage(backendInfo.serviceInfo.packageName); intent.setClassName(backendInfo.serviceInfo.packageName, backendInfo.getMeta(metaName)); return intent; @@ -166,6 +172,7 @@ public View getView(int position, View convertView, ViewGroup parent) { @Override public void onClick(View v) { backend.enabled = checkbox.isChecked(); + if (backend.enabled) enableBackend(backend); } }); configureExternalButton(backend, v.findViewById(android.R.id.button1), @@ -191,6 +198,31 @@ public void onClick(View v) { } } + protected void enableBackend(BackendInfo backendInfo) { + if (backendInfo.getMeta(METADATA_BACKEND_INIT_ACTIVITY) != null) { + getContext().startActivity(createExternalIntent(backendInfo, METADATA_BACKEND_INIT_ACTIVITY)); + } else { + Intent intent = buildBackendIntent(); + intent.setPackage(backendInfo.serviceInfo.packageName); + intent.setClassName(backendInfo.serviceInfo.packageName, backendInfo.serviceInfo.name); + getContext().bindService(intent, new ServiceConnection() { + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + Intent i = getBackendInitIntent(service); + if (i != null) { + getContext().startActivity(i); + } + getContext().unbindService(this); + } + + @Override + public void onServiceDisconnected(ComponentName name) { + + } + }, BIND_AUTO_CREATE); + } + } + @Override protected void onDialogClosed(boolean positiveResult) { if (positiveResult) { @@ -205,6 +237,8 @@ protected void onDialogClosed(boolean positiveResult) { protected abstract String defaultValue(); + protected abstract Intent getBackendInitIntent(IBinder service); + private class BackendInfo { private final ServiceInfo serviceInfo; private final String simpleName; diff --git a/unifiednlp-base/src/main/java/org/microg/nlp/ui/GeocoderBackendPreference.java b/unifiednlp-base/src/main/java/org/microg/nlp/ui/GeocoderBackendPreference.java index 66b690f..44c320c 100644 --- a/unifiednlp-base/src/main/java/org/microg/nlp/ui/GeocoderBackendPreference.java +++ b/unifiednlp-base/src/main/java/org/microg/nlp/ui/GeocoderBackendPreference.java @@ -18,10 +18,13 @@ import android.content.Context; import android.content.Intent; +import android.os.IBinder; +import android.os.RemoteException; import android.util.AttributeSet; import org.microg.nlp.Preferences; import org.microg.nlp.R; +import org.microg.nlp.api.GeocoderBackend; import org.microg.nlp.geocode.AbstractGeocodeService; import static org.microg.nlp.api.Constants.ACTION_GEOCODER_BACKEND; @@ -35,7 +38,7 @@ public GeocoderBackendPreference(Context context, AttributeSet attrs) { @Override protected void onValueChanged() { - AbstractGeocodeService.reloadLocationService(getContext()); + AbstractGeocodeService.reloadGeocodeService(getContext()); } @Override @@ -47,4 +50,14 @@ protected Intent buildBackendIntent() { protected String defaultValue() { return new Preferences(getContext()).getDefaultGeocoderBackends(); } + + @Override + protected Intent getBackendInitIntent(IBinder service) { + GeocoderBackend backend = GeocoderBackend.Stub.asInterface(service); + try { + return backend.getInitIntent(); + } catch (RemoteException e) { + return null; + } + } } diff --git a/unifiednlp-base/src/main/java/org/microg/nlp/ui/LocationBackendPreference.java b/unifiednlp-base/src/main/java/org/microg/nlp/ui/LocationBackendPreference.java index ed62a3c..f9baca8 100644 --- a/unifiednlp-base/src/main/java/org/microg/nlp/ui/LocationBackendPreference.java +++ b/unifiednlp-base/src/main/java/org/microg/nlp/ui/LocationBackendPreference.java @@ -18,10 +18,14 @@ import android.content.Context; import android.content.Intent; +import android.os.IBinder; +import android.os.RemoteException; import android.util.AttributeSet; import org.microg.nlp.Preferences; import org.microg.nlp.R; +import org.microg.nlp.api.GeocoderBackend; +import org.microg.nlp.api.LocationBackend; import org.microg.nlp.location.AbstractLocationService; import static org.microg.nlp.api.Constants.ACTION_LOCATION_BACKEND; @@ -47,4 +51,14 @@ protected Intent buildBackendIntent() { protected String defaultValue() { return new Preferences(getContext()).getDefaultLocationBackends(); } + + @Override + protected Intent getBackendInitIntent(IBinder service) { + LocationBackend backend = LocationBackend.Stub.asInterface(service); + try { + return backend.getInitIntent(); + } catch (RemoteException e) { + return null; + } + } }