Fixing this sample since it has been using a deprecated API that is no
longer available in Nacho. I am not the original author of this sample
so please review carefully since I am not familiar with GeoFencing.
Bug: 18460460
Change-Id: I8887fe46ede534d2e753c24cb5cad2b838a8d7e6
diff --git a/wearable/wear/Geofencing/Application/src/main/java/com/example/android/wearable/geofencing/GeofenceTransitionsIntentService.java b/wearable/wear/Geofencing/Application/src/main/java/com/example/android/wearable/geofencing/GeofenceTransitionsIntentService.java
index 5d1ea48..8ae0cbc 100644
--- a/wearable/wear/Geofencing/Application/src/main/java/com/example/android/wearable/geofencing/GeofenceTransitionsIntentService.java
+++ b/wearable/wear/Geofencing/Application/src/main/java/com/example/android/wearable/geofencing/GeofenceTransitionsIntentService.java
@@ -26,11 +26,12 @@
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
+import android.widget.Toast;
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.LocationClient;
+import com.google.android.gms.location.GeofencingEvent;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.Wearable;
@@ -61,44 +62,46 @@
/**
* Handles incoming intents.
* @param intent The Intent sent by Location Services. This Intent is provided to Location
- * Services (inside a PendingIntent) when addGeofences() is called.
+ * Services (inside a PendingIntent) when addGeofences() is called.
*/
@Override
protected void onHandleIntent(Intent intent) {
- // First check for errors.
- if (LocationClient.hasError(intent)) {
- int errorCode = LocationClient.getErrorCode(intent);
+ GeofencingEvent geoFenceEvent = GeofencingEvent.fromIntent(intent);
+ if (geoFenceEvent.hasError()) {
+ int errorCode = geoFenceEvent.getErrorCode();
Log.e(TAG, "Location Services error: " + errorCode);
} else {
- // Get the type of geofence transition (i.e. enter or exit in this sample).
- int transitionType = LocationClient.getGeofenceTransition(intent);
- // Create a DataItem when a user enters one of the geofences. The wearable app will
- // receive this and create a notification to prompt him/her to check in.
+
+ int transitionType = geoFenceEvent.getGeofenceTransition();
if (Geofence.GEOFENCE_TRANSITION_ENTER == transitionType) {
// Connect to the Google Api service in preparation for sending a DataItem.
mGoogleApiClient.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
// Get the geofence id triggered. Note that only one geofence can be triggered at a
// time in this example, but in some cases you might want to consider the full list
// of geofences triggered.
- String triggeredGeofenceId = LocationClient.getTriggeringGeofences(intent).get(0)
+ String triggeredGeoFenceId = geoFenceEvent.getTriggeringGeofences().get(0)
.getRequestId();
// Create a DataItem with this geofence's id. The wearable can use this to create
// a notification.
final PutDataMapRequest putDataMapRequest =
PutDataMapRequest.create(GEOFENCE_DATA_ITEM_PATH);
- putDataMapRequest.getDataMap().putString(KEY_GEOFENCE_ID, triggeredGeofenceId);
+ putDataMapRequest.getDataMap().putString(KEY_GEOFENCE_ID, triggeredGeoFenceId);
if (mGoogleApiClient.isConnected()) {
Wearable.DataApi.putDataItem(
- mGoogleApiClient, putDataMapRequest.asPutDataRequest()).await();
+ mGoogleApiClient, putDataMapRequest.asPutDataRequest()).await();
} else {
Log.e(TAG, "Failed to send data item: " + putDataMapRequest
- + " - Client disconnected from Google Play Services");
+ + " - Client disconnected from Google Play Services");
}
+ Toast.makeText(this, getString(R.string.entering_geofence),
+ Toast.LENGTH_SHORT).show();
mGoogleApiClient.disconnect();
} else if (Geofence.GEOFENCE_TRANSITION_EXIT == transitionType) {
// Delete the data item when leaving a geofence region.
mGoogleApiClient.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
Wearable.DataApi.deleteDataItems(mGoogleApiClient, GEOFENCE_DATA_ITEM_URI).await();
+ Toast.makeText(this, getString(R.string.exiting_geofence),
+ Toast.LENGTH_SHORT).show();
mGoogleApiClient.disconnect();
}
}
diff --git a/wearable/wear/Geofencing/Application/src/main/java/com/example/android/wearable/geofencing/MainActivity.java b/wearable/wear/Geofencing/Application/src/main/java/com/example/android/wearable/geofencing/MainActivity.java
index baef217..350c9c5 100644
--- a/wearable/wear/Geofencing/Application/src/main/java/com/example/android/wearable/geofencing/MainActivity.java
+++ b/wearable/wear/Geofencing/Application/src/main/java/com/example/android/wearable/geofencing/MainActivity.java
@@ -39,17 +39,16 @@
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesUtil;
+import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.Geofence;
-import com.google.android.gms.location.LocationClient;
-import com.google.android.gms.location.LocationClient.OnAddGeofencesResultListener;
-import com.google.android.gms.location.LocationStatusCodes;
+import com.google.android.gms.location.LocationServices;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity implements ConnectionCallbacks,
- OnConnectionFailedListener, OnAddGeofencesResultListener {
+ OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
// Internal List of Geofence objects. In a real app, these might be provided by an API based on
// locations within the user's proximity.
@@ -62,35 +61,39 @@
// Persistent storage for geofences.
private SimpleGeofenceStore mGeofenceStorage;
- private LocationClient mLocationClient;
+ private LocationServices mLocationService;
// Stores the PendingIntent used to request geofence monitoring.
private PendingIntent mGeofenceRequestIntent;
+ private GoogleApiClient mApiClient;
// Defines the allowable request types (in this example, we only add geofences).
private enum REQUEST_TYPE {ADD}
private REQUEST_TYPE mRequestType;
- // Flag that indicates if a request is underway.
- private boolean mInProgress;
-
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Rather than displayng this activity, simply display a toast indicating that the geofence
// service is being created. This should happen in less than a second.
- Toast.makeText(this, getString(R.string.start_geofence_service), Toast.LENGTH_SHORT).show();
+ if (!isGooglePlayServicesAvailable()) {
+ Log.e(TAG, "Google Play services unavailable.");
+ finish();
+ return;
+ }
+
+ mApiClient = new GoogleApiClient.Builder(this)
+ .addApi(LocationServices.API)
+ .addConnectionCallbacks(this)
+ .addOnConnectionFailedListener(this)
+ .build();
+
+ mApiClient.connect();
// Instantiate a new geofence storage area.
mGeofenceStorage = new SimpleGeofenceStore(this);
// Instantiate the current List of geofences.
mGeofenceList = new ArrayList<Geofence>();
- // Start with the request flag set to false.
- mInProgress = false;
-
createGeofences();
- addGeofences();
-
- finish();
}
/**
@@ -123,37 +126,9 @@
mGeofenceList.add(mYerbaBuenaGeofence.toGeofence());
}
- /**
- * Start a request for geofence monitoring by calling LocationClient.connect().
- */
- public void addGeofences() {
- // Start a request to add geofences.
- mRequestType = REQUEST_TYPE.ADD;
- // Test for Google Play services after setting the request type.
- if (!isGooglePlayServicesAvailable()) {
- Log.e(TAG, "Unable to add geofences - Google Play services unavailable.");
- return;
- }
- // Create a new location client object. Since this activity class implements
- // ConnectionCallbacks and OnConnectionFailedListener, it can be used as the listener for
- // both parameters.
- mLocationClient = new LocationClient(this, this, this);
- // If a request is not already underway.
- if (!mInProgress) {
- // Indicate that a request is underway.
- mInProgress = true;
- // Request a connection from the client to Location Services.
- mLocationClient.connect();
- // A request is already underway, so disconnect the client and retry the request.
- } else {
- mLocationClient.disconnect();
- mLocationClient.connect();
- }
- }
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
- mInProgress = false;
// If the error has a resolution, start a Google Play services activity to resolve it.
if (connectionResult.hasResolution()) {
try {
@@ -168,15 +143,8 @@
}
}
- /**
- * Called by Location Services if the location client disconnects.
- */
@Override
public void onDisconnected() {
- // Turn off the request flag.
- mInProgress = false;
- // Destroy the current location client.
- mLocationClient = null;
}
/**
@@ -184,32 +152,22 @@
*/
@Override
public void onConnected(Bundle connectionHint) {
- // Use mRequestType to determine what action to take. Only ADD is used in this sample.
- if (REQUEST_TYPE.ADD == mRequestType) {
- // Get the PendingIntent for the geofence monitoring request.
- mGeofenceRequestIntent = getGeofenceTransitionPendingIntent();
- // Send a request to add the current geofences.
- mLocationClient.addGeofences(mGeofenceList, mGeofenceRequestIntent, this);
+ // Get the PendingIntent for the geofence monitoring request.
+ // Send a request to add the current geofences.
+ mGeofenceRequestIntent = getGeofenceTransitionPendingIntent();
+ LocationServices.GeofencingApi.addGeofences(mApiClient, mGeofenceList,
+ mGeofenceRequestIntent);
+ Toast.makeText(this, getString(R.string.start_geofence_service), Toast.LENGTH_SHORT).show();
+ finish();
+ }
+
+ @Override
+ public void onConnectionSuspended(int i) {
+ if (null != mGeofenceRequestIntent) {
+ LocationServices.GeofencingApi.removeGeofences(mApiClient, mGeofenceRequestIntent);
}
}
- /**
- * Called when request to add geofences is complete, with a result status code.
- */
- @Override
- public void onAddGeofencesResult(int statusCode, String[] geofenceRequestIds) {
- // Log if adding the geofences was successful.
- if (LocationStatusCodes.SUCCESS == statusCode) {
- if (Log.isLoggable(TAG, Log.DEBUG)) {
- Log.d(TAG, "Added geofences successfully.");
- }
- } else {
- Log.e(TAG, "Failed to add geofences. Status code: " + statusCode);
- }
- // Turn off the in progress flag and disconnect the client.
- mInProgress = false;
- mLocationClient.disconnect();
- }
/**
* Checks if Google Play services is available.
diff --git a/wearable/wear/Geofencing/Application/src/main/res/values/strings.xml b/wearable/wear/Geofencing/Application/src/main/res/values/strings.xml
index ad9717b..610661e 100644
--- a/wearable/wear/Geofencing/Application/src/main/res/values/strings.xml
+++ b/wearable/wear/Geofencing/Application/src/main/res/values/strings.xml
@@ -16,4 +16,6 @@
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="start_geofence_service">Starting geofence transition service</string>
+ <string name="entering_geofence">Entering the GeoFence zone</string>
+ <string name="exiting_geofence">Exiting the GeoFence zone</string>
</resources>