Index: services/location/src/org/chromium/services/location/LocationServiceApp.java |
diff --git a/services/location/src/org/chromium/services/location/LocationServiceApp.java b/services/location/src/org/chromium/services/location/LocationServiceApp.java |
index 545b92756bce9dd768be9d1e267dfdb72c7c94a9..1617e9bd447d6ee3724a5f788dc8b636fdf80f9b 100644 |
--- a/services/location/src/org/chromium/services/location/LocationServiceApp.java |
+++ b/services/location/src/org/chromium/services/location/LocationServiceApp.java |
@@ -4,32 +4,49 @@ |
package org.chromium.services.location; |
+import android.app.PendingIntent; |
import android.content.Context; |
+import android.content.Intent; |
import android.os.Looper; |
+import android.os.Parcel; |
import android.util.Log; |
import com.google.android.gms.common.ConnectionResult; |
import com.google.android.gms.common.api.GoogleApiClient; |
+import com.google.android.gms.location.Geofence; |
+import com.google.android.gms.location.GeofencingEvent; |
+import com.google.android.gms.location.GeofencingRequest; |
import com.google.android.gms.location.LocationServices; |
import org.chromium.mojo.application.ApplicationConnection; |
import org.chromium.mojo.application.ApplicationDelegate; |
+import org.chromium.mojo.application.ApplicationHelper; |
import org.chromium.mojo.application.ApplicationRunner; |
import org.chromium.mojo.application.ServiceFactoryBinder; |
+import org.chromium.mojo.intent.IntentManager; |
+import org.chromium.mojo.intent.IntentManager.GetIntentResponse; |
+import org.chromium.mojo.intent.IntentReceiver; |
import org.chromium.mojo.system.Core; |
import org.chromium.mojo.system.MessagePipeHandle; |
+import org.chromium.mojo.system.MojoException; |
import org.chromium.mojom.mojo.LocationService; |
import org.chromium.mojom.mojo.Shell; |
+import java.util.ArrayList; |
+import java.util.List; |
+ |
/** |
- * Android service application implementing the LocationService interface using Google play |
- * services API. |
+ * Android service application implementing the LocationService interface using Google play services |
+ * API. |
*/ |
class LocationServiceApp implements ApplicationDelegate { |
- private static final String TAG = "LocationServiceApp"; |
+ private static final String TAG = "chromium"; |
private final GoogleApiClient mGoogleApiClient; |
+ private final Context mContext; |
private final Core mCore; |
+ private IntentManager.Proxy mIntentManager; |
+ private PendingIntent mPendingIntent; |
/** |
* Android looper thread class to get callbacks from google play services api. |
@@ -73,12 +90,14 @@ class LocationServiceApp implements ApplicationDelegate { |
mLooper.quitSafely(); |
} |
} |
+ |
private final LooperThread mLooperThread = new LooperThread(); |
public LocationServiceApp(Context context, Core core) { |
// TODO(alhaad): Create a test mode which uses mock locations instead of real locations. |
mGoogleApiClient = |
new GoogleApiClient.Builder(context).addApi(LocationServices.API).build(); |
+ mContext = context; |
mCore = core; |
} |
@@ -95,6 +114,62 @@ class LocationServiceApp implements ApplicationDelegate { |
return; |
} |
mLooperThread.start(); |
+ |
+ mIntentManager = ApplicationHelper.connectToService( |
+ mCore, shell, "mojo:android_handler", IntentManager.MANAGER); |
+ mIntentManager.getIntent( |
+ new IntentReceiver() { |
+ |
+ @Override |
+ public void onConnectionError(MojoException e) {} |
+ |
+ @Override |
+ public void close() {} |
+ |
+ @Override |
+ public void onIntent(byte[] serializedIntent) { |
+ GeofencingEvent g = GeofencingEvent.fromIntent(getIntent(serializedIntent)); |
+ Log.w(TAG, g.toString() + ": " + this); |
+ } |
+ }, |
+ new GetIntentResponse() { |
+ |
+ @Override |
+ public void call(byte[] serializedIntent) { |
+ mPendingIntent = PendingIntent.getService(mContext, 0, |
+ getIntent(serializedIntent), PendingIntent.FLAG_UPDATE_CURRENT); |
+ |
+ LocationServices.GeofencingApi.addGeofences( |
+ mGoogleApiClient, getGeofencingRequest(), mPendingIntent); |
+ } |
+ }); |
+ } |
+ |
+ private GeofencingRequest getGeofencingRequest() { |
+ List<Geofence> list = new ArrayList<>(); |
+ list.add(new Geofence.Builder() |
+ // Set the request ID of the geofence. This is a string to identify this |
+ // geofence. |
+ .setRequestId("a") |
+ |
+ .setCircularRegion(48.8769798, 2.3300510999999915, 10000) |
+ .setExpirationDuration(1000000) |
+ .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
+ | Geofence.GEOFENCE_TRANSITION_EXIT) |
+ .build()); |
+ GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); |
+ builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); |
+ builder.addGeofences(list); |
+ return builder.build(); |
+ } |
+ |
+ public static Intent getIntent(byte[] serializedIntent) { |
+ Parcel p = Parcel.obtain(); |
+ p.unmarshall(serializedIntent, 0, serializedIntent.length); |
+ p.setDataPosition(0); |
+ Intent intent = Intent.CREATOR.createFromParcel(p); |
+ p.recycle(); |
+ return intent; |
} |
/** |
@@ -125,7 +200,13 @@ class LocationServiceApp implements ApplicationDelegate { |
*/ |
@Override |
public void quit() { |
+ if (mPendingIntent != null) { |
+ LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, |
+ // This is the same pending intent that was used in addGeofences(). |
+ mPendingIntent); |
+ } |
mGoogleApiClient.disconnect(); |
+ mIntentManager.close(); |
mLooperThread.quitSoon(); |
try { |
mLooperThread.join(); |