1 /*
2  * Copyright (C) 2014 Google Inc. All Rights Reserved.
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.speedtracker.db;
18 
19 import com.google.android.gms.common.ConnectionResult;
20 import com.google.android.gms.common.api.GoogleApiClient;
21 import com.google.android.gms.common.api.ResultCallback;
22 import com.google.android.gms.wearable.DataApi;
23 import com.google.android.gms.wearable.DataEvent;
24 import com.google.android.gms.wearable.DataEventBuffer;
25 import com.google.android.gms.wearable.DataMap;
26 import com.google.android.gms.wearable.DataMapItem;
27 import com.google.android.gms.wearable.Wearable;
28 import com.google.android.gms.wearable.WearableListenerService;
29 
30 import android.net.Uri;
31 import android.os.Bundle;
32 import android.util.Log;
33 
34 import com.example.android.wearable.speedtracker.LocationDataManager;
35 import com.example.android.wearable.speedtracker.PhoneApplication;
36 import com.example.android.wearable.speedtracker.common.Constants;
37 import com.example.android.wearable.speedtracker.common.LocationEntry;
38 
39 import java.util.Calendar;
40 import java.util.HashSet;
41 import java.util.Set;
42 
43 /**
44  * A {@link com.google.android.gms.wearable.WearableListenerService} that is responsible for
45  * reading location data that gets added to the Data Layer storage.
46  */
47 public class UpdateService extends WearableListenerService
48         implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
49         ResultCallback<DataApi.DeleteDataItemsResult> {
50 
51     private static final String TAG = "UpdateService";
52     private LocationDataManager mDataManager;
53     private GoogleApiClient mGoogleApiClient;
54     private final Set<Uri> mToBeDeletedUris = new HashSet<Uri>();
55     public static final String ACTION_NOTIFY = "com.example.android.wearable.speedtracker.Message";
56     public static final String EXTRA_ENTRY = "entry";
57     public static final String EXTRA_LOG = "log";
58 
59     @Override
onCreate()60     public void onCreate() {
61         super.onCreate();
62         mGoogleApiClient = new GoogleApiClient.Builder(this)
63                 .addApi(Wearable.API)
64                 .addConnectionCallbacks(this)
65                 .addOnConnectionFailedListener(this)
66                 .build();
67         mGoogleApiClient.connect();
68         mDataManager = ((PhoneApplication) getApplicationContext()).getDataManager();
69     }
70 
71     @Override
onDataChanged(DataEventBuffer dataEvents)72     public void onDataChanged(DataEventBuffer dataEvents) {
73         for (DataEvent dataEvent : dataEvents) {
74             if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
75                 Uri dataItemUri = dataEvent.getDataItem().getUri();
76                 if (Log.isLoggable(TAG, Log.DEBUG)) {
77                     Log.d(TAG, "Received a data item with uri: " + dataItemUri.getPath());
78                 }
79                 if (dataItemUri.getPath().startsWith(Constants.PATH)) {
80                     DataMap dataMap = DataMapItem.fromDataItem(dataEvent.getDataItem())
81                             .getDataMap();
82                     double longitude = dataMap.getDouble(Constants.KEY_LONGITUDE);
83                     double latitude = dataMap.getDouble(Constants.KEY_LATITUDE);
84                     long time = dataMap.getLong(Constants.KEY_TIME);
85                     Calendar calendar = Calendar.getInstance();
86                     calendar.setTimeInMillis(time);
87                     mDataManager.addPoint(
88                             new LocationEntry(calendar, latitude, longitude));
89                     if (mGoogleApiClient.isConnected()) {
90                         Wearable.DataApi.deleteDataItems(
91                                 mGoogleApiClient, dataItemUri).setResultCallback(this);
92                     } else {
93                         synchronized (mToBeDeletedUris) {
94                             mToBeDeletedUris.add(dataItemUri);
95                         }
96                     }
97                 }
98             }
99         }
100     }
101 
102     @Override // ConnectionCallbacks
onConnected(Bundle bundle)103     public void onConnected(Bundle bundle) {
104         if (Log.isLoggable(TAG, Log.DEBUG)) {
105             Log.d(TAG, "onConnected(): api client is connected now");
106         }
107         synchronized (mToBeDeletedUris) {
108             if (!mToBeDeletedUris.isEmpty()) {
109                 for (Uri dataItemUri : mToBeDeletedUris) {
110                     Wearable.DataApi.deleteDataItems(
111                             mGoogleApiClient, dataItemUri).setResultCallback(this);
112                 }
113             }
114         }
115     }
116 
117     @Override // ConnectionCallbacks
onConnectionSuspended(int i)118     public void onConnectionSuspended(int i) {
119     }
120 
121     @Override // OnConnectionFailedListener
onConnectionFailed(ConnectionResult connectionResult)122     public void onConnectionFailed(ConnectionResult connectionResult) {
123         Log.e(TAG, "Failed to connect to the Google API client");
124     }
125 
126     @Override // ResultCallback
onResult(DataApi.DeleteDataItemsResult deleteDataItemsResult)127     public void onResult(DataApi.DeleteDataItemsResult deleteDataItemsResult) {
128         if (!deleteDataItemsResult.getStatus().isSuccess()) {
129             Log.e(TAG,
130                     "Failed to delete a dataItem, status code: " + deleteDataItemsResult.getStatus()
131                             .getStatusCode() + deleteDataItemsResult.getStatus()
132                             .getStatusMessage());
133         }
134     }
135 }
136