Added onOpen/onClose to LocationBackendService and third sample

This commit is contained in:
mar-v-in 2014-03-07 13:31:18 +01:00
parent 9446feaacb
commit f3daae1be9
3 changed files with 80 additions and 3 deletions

View file

@ -13,7 +13,7 @@ public abstract class LocationBackendService extends Service {
private Location waiting;
/**
* This method is called, whenever an app requires a location update. This can be a single or a repeated request.
* Called, whenever an app requires a location update. This can be a single or a repeated request.
* <p/>
* You may return null if your backend has no newer location available then the last one.
* Do not send the same {@link android.location.Location} twice, if it's not based on updated/refreshed data.
@ -22,7 +22,9 @@ public abstract class LocationBackendService extends Service {
*
* @return a new {@link android.location.Location} instance or null if not available.
*/
protected abstract Location update();
protected Location update() {
return null;
}
/**
* Directly report a {@link android.location.Location} to the requesting apps. Use this if your updates are based
@ -47,6 +49,20 @@ public abstract class LocationBackendService extends Service {
return backend;
}
/**
* Called after a connection was setup
*/
protected void onOpen() {
}
/**
* Called before connection closure
*/
protected void onClose() {
}
private class Backend extends LocationBackend.Stub {
@Override
@ -56,6 +72,7 @@ public abstract class LocationBackendService extends Service {
callback.report(waiting);
waiting = null;
}
onOpen();
}
@Override
@ -65,6 +82,7 @@ public abstract class LocationBackendService extends Service {
@Override
public void close() throws RemoteException {
onClose();
callback = null;
}
}

View file

@ -1,11 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="org.microg.nlp.api.sample"
<manifest
package="org.microg.nlp.api.sample"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="19" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<service
android:name=".SampleBackendService"
android:exported="true"
@ -14,6 +16,7 @@
<action android:name="org.microg.nlp.LOCATION_BACKEND" />
</intent-filter>
</service>
<service
android:name=".SecondSampleService"
android:exported="true"
@ -30,5 +33,14 @@
android:name=".SecondSettings"
android:exported="true"
android:label="Second Settings Activity" />
<service
android:name=".ThirdSampleService"
android:exported="true"
android:label="NLPV2-Random">
<intent-filter>
<action android:name="org.microg.nlp.LOCATION_BACKEND" />
</intent-filter>
</service>
</application>
</manifest>

View file

@ -0,0 +1,47 @@
package org.microg.nlp.api.sample;
import android.location.Location;
import android.util.Log;
import org.microg.nlp.api.LocationBackendService;
import java.util.Random;
public class ThirdSampleService extends LocationBackendService {
private static final String TAG = ThirdSampleService.class.getName();
private Thread regular;
private Random random = new Random();
@Override
protected void onOpen() {
super.onOpen();
regular = new Thread(new Runnable() {
@Override
public void run() {
synchronized (regular) {
while (!regular.isInterrupted()) {
try {
regular.wait(60000);
} catch (InterruptedException e) {
return;
}
Location location = new Location("random");
location.setLatitude(random.nextDouble() * 90);
location.setLongitude(random.nextDouble() * 90);
location.setAccuracy(random.nextFloat() * 90);
Log.d(TAG, "Just reported: " + location);
report(location);
}
}
}
});
regular.start();
}
@Override
protected void onClose() {
if (regular != null && regular.isAlive()) {
regular.interrupt();
}
}
}