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.quiz; 18 19 import android.app.IntentService; 20 import android.content.Intent; 21 import android.net.Uri; 22 import android.os.Bundle; 23 import android.util.Log; 24 25 import com.google.android.gms.common.ConnectionResult; 26 import com.google.android.gms.common.api.GoogleApiClient; 27 import com.google.android.gms.wearable.DataApi; 28 import com.google.android.gms.wearable.DataMap; 29 import com.google.android.gms.wearable.DataMapItem; 30 import com.google.android.gms.wearable.PutDataMapRequest; 31 import com.google.android.gms.wearable.PutDataRequest; 32 import com.google.android.gms.wearable.Wearable; 33 34 import java.util.concurrent.TimeUnit; 35 36 import static com.example.android.wearable.quiz.Constants.CONNECT_TIMEOUT_MS; 37 import static com.example.android.wearable.quiz.Constants.QUESTION_WAS_DELETED; 38 39 /** 40 * Used to update quiz status on the phone when user dismisses a question on the watch. 41 */ 42 public class DeleteQuestionService extends IntentService 43 implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 44 45 private static final String TAG = "DeleteQuestionReceiver"; 46 47 private GoogleApiClient mGoogleApiClient; 48 49 public DeleteQuestionService() { 50 super(DeleteQuestionService.class.getSimpleName()); 51 } 52 53 @Override 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 64 public void onHandleIntent(Intent intent) { 65 mGoogleApiClient.blockingConnect(CONNECT_TIMEOUT_MS, TimeUnit.MILLISECONDS); 66 Uri dataItemUri = intent.getData(); 67 if (!mGoogleApiClient.isConnected()) { 68 Log.e(TAG, "Failed to update data item " + dataItemUri 69 + " because client is disconnected from Google Play Services"); 70 return; 71 } 72 DataApi.DataItemResult dataItemResult = Wearable.DataApi.getDataItem( 73 mGoogleApiClient, dataItemUri).await(); 74 PutDataMapRequest putDataMapRequest = PutDataMapRequest 75 .createFromDataMapItem(DataMapItem.fromDataItem(dataItemResult.getDataItem())); 76 DataMap dataMap = putDataMapRequest.getDataMap(); 77 dataMap.putBoolean(QUESTION_WAS_DELETED, true); 78 PutDataRequest request = putDataMapRequest.asPutDataRequest(); 79 Wearable.DataApi.putDataItem(mGoogleApiClient, request).await(); 80 mGoogleApiClient.disconnect(); 81 } 82 83 @Override 84 public void onConnected(Bundle bundle) { 85 } 86 87 @Override 88 public void onConnectionSuspended(int i) { 89 } 90 91 @Override 92 public void onConnectionFailed(ConnectionResult connectionResult) { 93 } 94 } 95