Skip to content

Commit

Permalink
Add init intent support (API v2)
Browse files Browse the repository at this point in the history
For proper dynamic permission support
  • Loading branch information
mar-v-in committed Feb 22, 2016
1 parent b26d513 commit bd671ae
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import static org.microg.nlp.api.Constants.ACTION_RELOAD_SETTINGS;

public abstract class AbstractGeocodeService extends AbstractProviderService<GeocodeProvider> {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -126,7 +132,7 @@ List<BackendInfo> 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;
Expand Down Expand Up @@ -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),
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,7 +38,7 @@ public GeocoderBackendPreference(Context context, AttributeSet attrs) {

@Override
protected void onValueChanged() {
AbstractGeocodeService.reloadLocationService(getContext());
AbstractGeocodeService.reloadGeocodeService(getContext());
}

@Override
Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
}

0 comments on commit bd671ae

Please sign in to comment.