Skip to content

Commit

Permalink
Add dynamic intent, onInit and dynamic permission support.
Browse files Browse the repository at this point in the history
This increases API version to 2
  • Loading branch information
mar-v-in committed Feb 22, 2016
1 parent a818694 commit af1743c
Show file tree
Hide file tree
Showing 18 changed files with 237 additions and 73 deletions.
2 changes: 1 addition & 1 deletion unifiednlp-api/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 microG Project Team
* Copyright 2013-2016 microG Project Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion unifiednlp-api/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2013-2015 microG Project Team
# Copyright 2013-2016 microG Project Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
14 changes: 9 additions & 5 deletions unifiednlp-api/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2013-2015 microG Project Team
~ Copyright 2013-2016 microG Project Team
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
Expand All @@ -14,14 +14,18 @@
~ limitations under the License.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.microg.nlp.api">
<manifest package="org.microg.nlp.api"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-sdk android:minSdkVersion="9" />
<uses-sdk android:minSdkVersion="9"/>

<application>
<meta-data
android:name="org.microg.nlp.API_VERSION"
android:value="@string/nlp_api_version" />
android:value="@string/nlp_api_version"/>
<activity
android:name=".MPermissionHelperActivity"
android:exported="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ interface GeocoderBackend {
double lowerLeftLongitude, double upperRightLatitude, double upperRightLongitude,
String locale);
void close();
Intent getInitIntent();
Intent getSettingsIntent();
Intent getAboutIntent();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ interface LocationBackend {
void open(LocationCallback callback);
Location update();
void close();
Intent getInitIntent();
Intent getSettingsIntent();
Intent getAboutIntent();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 microG Project Team
* Copyright 2013-2016 microG Project Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -58,5 +58,9 @@ public synchronized void onClose() {
public synchronized void onUpdate() {
}

public String[] getRequiredPermissions() {
return new String[0];
}

protected enum State {DISABLED, WAITING, SCANNING, DISABLING}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 microG Project Team
* Copyright 2013-2016 microG Project Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,6 +41,18 @@ protected void onClose() {

}

protected Intent getInitIntent() {
return null;
}

protected Intent getSettingsIntent() {
return null;
}

protected Intent getAboutIntent() {
return null;
}

@Override
public boolean onUnbind(Intent intent) {
disconnect();
Expand Down
114 changes: 64 additions & 50 deletions unifiednlp-api/src/main/java/org/microg/nlp/api/CellBackendHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 microG Project Team
* Copyright 2013-2016 microG Project Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,6 +47,9 @@
import java.util.List;
import java.util.Set;

import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
import static android.Manifest.permission.READ_PHONE_STATE;

/**
* Utility class to support backends that use Cells for geolocation.
* <p/>
Expand Down Expand Up @@ -76,44 +79,6 @@ public CellBackendHelper(Context context, Listener listener) {
throw new IllegalArgumentException("listener must not be null");
this.listener = listener;
this.telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

// For some reason the Constructor of PhoneStateListener accepting a Looper is hidden,
// so we have to change the looper manually...
Handler mainHandler = new Handler(context.getMainLooper());
mainHandler.post(new Runnable() {
@Override
public void run() {
phoneStateListener = new PhoneStateListener() {

@Override
public void onCellInfoChanged(List<CellInfo> cellInfo) {
if (cellInfo != null) {
onCellsChanged(cellInfo);
} else if (supportsCellInfoChanged) {
supportsCellInfoChanged = false;
onSignalStrengthsChanged(null);
}
}

@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
if (!supportsCellInfoChanged) {
List<CellInfo> allCellInfo = telephonyManager.getAllCellInfo();
if ((allCellInfo == null || allCellInfo.isEmpty()) && telephonyManager.getNetworkType() > 0) {
allCellInfo = new ArrayList<CellInfo>();
CellLocation cellLocation = telephonyManager.getCellLocation();
if (cellLocation != null) {
CellInfo cellInfo = fromCellLocation(cellLocation);
if (cellInfo != null) allCellInfo.add(cellInfo);
}
}
onCellInfoChanged(allCellInfo);
}
}
};
registerPhoneStateListener();
}
});
}

private int getMcc() {
Expand Down Expand Up @@ -348,13 +313,67 @@ public synchronized Set<Cell> getCells() {
@Override
public synchronized void onOpen() {
super.onOpen();
if (phoneStateListener != null) registerPhoneStateListener();

if (phoneStateListener == null) {
Handler mainHandler = new Handler(context.getMainLooper());
mainHandler.post(new Runnable() {
@Override
public void run() {
phoneStateListener = new PhoneStateListener() {

@Override
public void onCellInfoChanged(List<CellInfo> cellInfo) {
if (cellInfo != null) {
onCellsChanged(cellInfo);
} else if (supportsCellInfoChanged) {
supportsCellInfoChanged = false;
onSignalStrengthsChanged(null);
}
}

@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
if (!supportsCellInfoChanged) {
List<CellInfo> allCellInfo = telephonyManager.getAllCellInfo();
if ((allCellInfo == null || allCellInfo.isEmpty()) && telephonyManager.getNetworkType() > 0) {
allCellInfo = new ArrayList<CellInfo>();
CellLocation cellLocation = telephonyManager.getCellLocation();
if (cellLocation != null) {
CellInfo cellInfo = fromCellLocation(cellLocation);
if (cellInfo != null) allCellInfo.add(cellInfo);
}
}
onCellInfoChanged(allCellInfo);
}
}
};
registerPhoneStateListener();
}
});
} else {
registerPhoneStateListener();
}
}

private synchronized void registerPhoneStateListener() {
telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_CELL_INFO
| PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
try {
telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_CELL_INFO
| PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
} catch (Exception e) {
// Can't listen
phoneStateListener = null;
}
}

/**
* Call this in {@link org.microg.nlp.api.LocationBackendService#onClose()}.
*/
@Override
public synchronized void onClose() {
super.onClose();
if (phoneStateListener != null)
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
}

@Override
Expand All @@ -366,14 +385,9 @@ public synchronized void onUpdate() {
}
}

/**
* Call this in {@link org.microg.nlp.api.LocationBackendService#onClose()}.
*/
@Override
public synchronized void onClose() {
super.onClose();
if (phoneStateListener != null)
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
public String[] getRequiredPermissions() {
return new String[]{READ_PHONE_STATE, ACCESS_COARSE_LOCATION};
}

public interface Listener {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 microG Project Team
* Copyright 2013-2016 microG Project Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,7 +28,8 @@ public class Constants {
public static final String LOCATION_EXTRA_OTHER_BACKENDS = "OTHER_BACKEND_RESULTS";
public static final String METADATA_BACKEND_SETTINGS_ACTIVITY = "org.microg.nlp.BACKEND_SETTINGS_ACTIVITY";
public static final String METADATA_BACKEND_ABOUT_ACTIVITY = "org.microg.nlp.BACKEND_ABOUT_ACTIVITY";
public static final String METADATA_BACKEND_INIT_ACTIVITY = "org.microg.nlp.BACKEND_INIT_ACTIVITY";
public static final String METADATA_BACKEND_SUMMARY = "org.microg.nlp.BACKEND_SUMMARY";
public static final String METADATA_API_VERSION = "org.microg.nlp.API_VERSION";
public static final String API_VERSION = "1";
public static final String API_VERSION = "2";
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 microG Project Team
* Copyright 2013-2016 microG Project Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@

package org.microg.nlp.api;

import android.content.Intent;
import android.location.Address;
import android.os.IBinder;
import android.os.RemoteException;
Expand Down Expand Up @@ -85,5 +86,20 @@ public List<Address> getFromLocationName(String locationName, int maxResults,
public void close() throws RemoteException {
disconnect();
}

@Override
public Intent getInitIntent() throws RemoteException {
return GeocoderBackendService.this.getInitIntent();
}

@Override
public Intent getSettingsIntent() throws RemoteException {
return GeocoderBackendService.this.getSettingsIntent();
}

@Override
public Intent getAboutIntent() throws RemoteException {
return GeocoderBackendService.this.getAboutIntent();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 microG Project Team
* Copyright 2013-2016 microG Project Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,15 +16,23 @@

package org.microg.nlp.api;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.util.Log;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public abstract class HelperLocationBackendService extends LocationBackendService {

private boolean opened;
private Set<AbstractBackendHelper> helpers = new HashSet<AbstractBackendHelper>();
private final Set<AbstractBackendHelper> helpers = new HashSet<AbstractBackendHelper>();

public synchronized void addHelper(AbstractBackendHelper helper) {
helpers.add(helper);
Expand Down Expand Up @@ -65,4 +73,26 @@ protected synchronized Location update() {
}
return null;
}

@Override
protected Intent getInitIntent() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Consider permissions
List<String> perms = new LinkedList<String>();
for (AbstractBackendHelper helper : helpers) {
perms.addAll(Arrays.asList(helper.getRequiredPermissions()));
}
for (Iterator<String> iterator = perms.iterator(); iterator.hasNext(); ) {
String perm = iterator.next();
if (checkSelfPermission(perm) == PackageManager.PERMISSION_GRANTED) {
iterator.remove();
}
}
if (perms.isEmpty()) return null;
Intent intent = new Intent(this, MPermissionHelperActivity.class);
intent.putExtra(MPermissionHelperActivity.EXTRA_PERMISSIONS, perms.toArray(new String[perms.size()]));
return intent;
}
return super.getInitIntent();
}
}
Loading

0 comments on commit af1743c

Please sign in to comment.