• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.google.android.gms.common.ConnectionResult;
20 import com.google.android.gms.common.api.GoogleApiClient;
21 import com.google.android.gms.wearable.DataApi;
22 import com.google.android.gms.wearable.Wearable;
23 
24 import android.app.IntentService;
25 import android.app.NotificationManager;
26 import android.content.Intent;
27 import android.net.Uri;
28 import android.os.Bundle;
29 import android.support.wearable.activity.ConfirmationActivity;
30 import android.util.Log;
31 
32 import java.util.concurrent.TimeUnit;
33 
34 import static com.example.android.wearable.geofencing.Constants.ACTION_CHECK_IN;
35 import static com.example.android.wearable.geofencing.Constants.ACTION_DELETE_DATA_ITEM;
36 import static com.example.android.wearable.geofencing.Constants.CONNECTION_TIME_OUT_MS;
37 import static com.example.android.wearable.geofencing.Constants.NOTIFICATION_ID;
38 import static com.example.android.wearable.geofencing.Constants.TAG;
39 
40 /**
41  * Handles "Check In" action on the location-based notification. Also deletes orphan DataItems
42  * when a notification is dismissed from the wearable.
43  */
44 public class CheckInAndDeleteDataItemsService extends IntentService
45         implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
46 
47     private GoogleApiClient mGoogleApiClient;
48 
CheckInAndDeleteDataItemsService()49     public CheckInAndDeleteDataItemsService() {
50         super(CheckInAndDeleteDataItemsService.class.getSimpleName());
51     }
52 
53     @Override
onCreate()54     public void onCreate() {
55         super.onCreate();
56         mGoogleApiClient = new GoogleApiClient.Builder(this)
57                 .addApi(Wearable.API)
58                 .addConnectionCallbacks(this)
59                 .addOnConnectionFailedListener(this)
60                 .build();
61     }
62 
63     @Override
onHandleIntent(Intent intent)64     protected void onHandleIntent(Intent intent) {
65         if (ACTION_CHECK_IN.equals(intent.getAction())) {
66             // In a real app, code for checking in would go here. For this sample, we will simply
67             // display a success animation.
68             startConfirmationActivity(ConfirmationActivity.SUCCESS_ANIMATION,
69                     getString(R.string.check_in_success));
70             // Dismiss the check-in notification.
71             ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancel(NOTIFICATION_ID);
72         } else if (!ACTION_DELETE_DATA_ITEM.equals(intent.getAction())) {
73             // The only possible actions should be checking in or dismissing the notification
74             // (which causes an intent with ACTION_DELETE_DATA_ITEM).
75             Log.e(TAG, "Unrecognized action: " + intent.getAction());
76             return;
77         }
78         // Regardless of the action, delete the DataItem (we are only be handling intents
79         // if the notification is dismissed or if the user has chosen to check in, either of which
80         // would be completed at this point).
81         mGoogleApiClient.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
82         Uri dataItemUri = intent.getData();
83         if (mGoogleApiClient.isConnected()) {
84             DataApi.DeleteDataItemsResult result = Wearable.DataApi
85                     .deleteDataItems(mGoogleApiClient, dataItemUri).await();
86             if (!result.getStatus().isSuccess()) {
87                 Log.e(TAG, "CheckInAndDeleteDataItemsService.onHandleIntent: "
88                          + "Failed to delete dataItem: " + dataItemUri);
89             } else if (Log.isLoggable(TAG, Log.DEBUG)) {
90                 Log.d(TAG, "Successfully deleted data item: " + dataItemUri);
91             }
92         } else {
93             Log.e(TAG, "Failed to delete data item: " + dataItemUri
94                      + " - Client disconnected from Google Play Services");
95         }
96         mGoogleApiClient.disconnect();
97     }
98 
99     /**
100      * Helper method to create confirmation animations on the wearable.
101      * @param animationType Defined by constants in ConfirmationActivity.
102      * @param message The message to display with the animation.
103      */
startConfirmationActivity(int animationType, String message)104     private void startConfirmationActivity(int animationType, String message) {
105         Intent confirmationActivity = new Intent(this, ConfirmationActivity.class)
106                 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION)
107                 .putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, animationType)
108                 .putExtra(ConfirmationActivity.EXTRA_MESSAGE, message);
109         startActivity(confirmationActivity);
110     }
111 
112     @Override
onConnected(Bundle connectionHint)113     public void onConnected(Bundle connectionHint) {
114     }
115 
116     @Override
onConnectionSuspended(int cause)117     public void onConnectionSuspended(int cause) {
118     }
119 
120     @Override
onConnectionFailed(ConnectionResult result)121     public void onConnectionFailed(ConnectionResult result) {
122     }
123 
124 }
125