Home | History | Annotate | Download | only in geofencing
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.wearable.geofencing;
     18 
     19 import static com.example.android.wearable.geofencing.Constants.CONNECTION_TIME_OUT_MS;
     20 import static com.example.android.wearable.geofencing.Constants.GEOFENCE_DATA_ITEM_PATH;
     21 import static com.example.android.wearable.geofencing.Constants.GEOFENCE_DATA_ITEM_URI;
     22 import static com.example.android.wearable.geofencing.Constants.KEY_GEOFENCE_ID;
     23 import static com.example.android.wearable.geofencing.Constants.TAG;
     24 
     25 import android.app.IntentService;
     26 import android.content.Intent;
     27 import android.os.Bundle;
     28 import android.util.Log;
     29 import android.widget.Toast;
     30 
     31 import com.google.android.gms.common.ConnectionResult;
     32 import com.google.android.gms.common.api.GoogleApiClient;
     33 import com.google.android.gms.location.Geofence;
     34 import com.google.android.gms.location.GeofencingEvent;
     35 import com.google.android.gms.wearable.PutDataMapRequest;
     36 import com.google.android.gms.wearable.Wearable;
     37 
     38 import java.util.concurrent.TimeUnit;
     39 
     40 /**
     41  * Listens for geofence transition changes.
     42  */
     43 public class GeofenceTransitionsIntentService extends IntentService
     44         implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
     45 
     46     private GoogleApiClient mGoogleApiClient;
     47 
     48     public GeofenceTransitionsIntentService() {
     49         super(GeofenceTransitionsIntentService.class.getSimpleName());
     50     }
     51 
     52     @Override
     53     public void onCreate() {
     54         super.onCreate();
     55         mGoogleApiClient = new GoogleApiClient.Builder(this)
     56                 .addApi(Wearable.API)
     57                 .addConnectionCallbacks(this)
     58                 .addOnConnectionFailedListener(this)
     59                 .build();
     60     }
     61 
     62     /**
     63      * Handles incoming intents.
     64      * @param intent The Intent sent by Location Services. This Intent is provided to Location
     65      * Services (inside a PendingIntent) when addGeofences() is called.
     66      */
     67     @Override
     68     protected void onHandleIntent(Intent intent) {
     69         GeofencingEvent geoFenceEvent = GeofencingEvent.fromIntent(intent);
     70         if (geoFenceEvent.hasError()) {
     71             int errorCode = geoFenceEvent.getErrorCode();
     72             Log.e(TAG, "Location Services error: " + errorCode);
     73         } else {
     74 
     75             int transitionType = geoFenceEvent.getGeofenceTransition();
     76             if (Geofence.GEOFENCE_TRANSITION_ENTER == transitionType) {
     77                 // Connect to the Google Api service in preparation for sending a DataItem.
     78                 mGoogleApiClient.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
     79                 // Get the geofence id triggered. Note that only one geofence can be triggered at a
     80                 // time in this example, but in some cases you might want to consider the full list
     81                 // of geofences triggered.
     82                 String triggeredGeoFenceId = geoFenceEvent.getTriggeringGeofences().get(0)
     83                         .getRequestId();
     84                 // Create a DataItem with this geofence's id. The wearable can use this to create
     85                 // a notification.
     86                 final PutDataMapRequest putDataMapRequest =
     87                         PutDataMapRequest.create(GEOFENCE_DATA_ITEM_PATH);
     88                 putDataMapRequest.getDataMap().putString(KEY_GEOFENCE_ID, triggeredGeoFenceId);
     89                 if (mGoogleApiClient.isConnected()) {
     90                     Wearable.DataApi.putDataItem(
     91                             mGoogleApiClient, putDataMapRequest.asPutDataRequest()).await();
     92                 } else {
     93                     Log.e(TAG, "Failed to send data item: " + putDataMapRequest
     94                             + " - Client disconnected from Google Play Services");
     95                 }
     96                 Toast.makeText(this, getString(R.string.entering_geofence),
     97                         Toast.LENGTH_SHORT).show();
     98                 mGoogleApiClient.disconnect();
     99             } else if (Geofence.GEOFENCE_TRANSITION_EXIT == transitionType) {
    100                 // Delete the data item when leaving a geofence region.
    101                 mGoogleApiClient.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
    102                 Wearable.DataApi.deleteDataItems(mGoogleApiClient, GEOFENCE_DATA_ITEM_URI).await();
    103                 Toast.makeText(this, getString(R.string.exiting_geofence),
    104                         Toast.LENGTH_SHORT).show();
    105                 mGoogleApiClient.disconnect();
    106             }
    107         }
    108     }
    109 
    110     @Override
    111     public void onConnected(Bundle connectionHint) {
    112     }
    113 
    114     @Override
    115     public void onConnectionSuspended(int cause) {
    116     }
    117 
    118     @Override
    119     public void onConnectionFailed(ConnectionResult result) {
    120     }
    121 
    122 }
    123