Home | History | Annotate | Download | only in agendadata
      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.agendadata;
     18 
     19 import android.app.IntentService;
     20 import android.content.Intent;
     21 import android.net.Uri;
     22 import android.os.Bundle;
     23 import android.support.wearable.activity.ConfirmationActivity;
     24 import android.util.Log;
     25 
     26 import static com.example.android.wearable.agendadata.Constants.TAG;
     27 import static com.example.android.wearable.agendadata.Constants.EXTRA_SILENT;
     28 
     29 import com.google.android.gms.common.ConnectionResult;
     30 import com.google.android.gms.common.api.GoogleApiClient;
     31 import com.google.android.gms.wearable.DataApi;
     32 import com.google.android.gms.wearable.Wearable;
     33 
     34 import java.util.concurrent.TimeUnit;
     35 
     36 /**
     37  * Handles "Delete" button on calendar event card.
     38  */
     39 public class DeleteService extends IntentService implements GoogleApiClient.ConnectionCallbacks,
     40         GoogleApiClient.OnConnectionFailedListener {
     41 
     42     /* Timeout for making a connection to GoogleApiClient (in milliseconds) */
     43     private static final long TIME_OUT = 100;
     44 
     45     private GoogleApiClient mGoogleApiClient;
     46 
     47     public DeleteService() {
     48         super(DeleteService.class.getSimpleName());
     49     }
     50 
     51     @Override
     52     public void onCreate() {
     53         super.onCreate();
     54         mGoogleApiClient = new GoogleApiClient.Builder(this)
     55                 .addApi(Wearable.API)
     56                 .addConnectionCallbacks(this)
     57                 .addOnConnectionFailedListener(this)
     58                 .build();
     59     }
     60 
     61     @Override
     62     protected void onHandleIntent(Intent intent) {
     63         mGoogleApiClient.blockingConnect(TIME_OUT, TimeUnit.MILLISECONDS);
     64         Uri dataItemUri = intent.getData();
     65         if (Log.isLoggable(TAG, Log.VERBOSE)) {
     66             Log.v(TAG, "DeleteService.onHandleIntent=" + dataItemUri);
     67         }
     68         if (mGoogleApiClient.isConnected()) {
     69             DataApi.DeleteDataItemsResult result = Wearable.DataApi
     70                     .deleteDataItems(mGoogleApiClient, dataItemUri).await();
     71             if (result.getStatus().isSuccess() && !intent.getBooleanExtra(EXTRA_SILENT, false)) {
     72                 // Show the success animation on the watch unless Silent extra is true.
     73                 startConfirmationActivity(ConfirmationActivity.SUCCESS_ANIMATION,
     74                                           getString(R.string.delete_successful));
     75             } else {
     76                 if (Log.isLoggable(TAG, Log.VERBOSE)) {
     77                     Log.v(TAG, "DeleteService.onHandleIntent: Failed to delete dataItem:"
     78                             + dataItemUri);
     79                 }
     80                 // Show the failure animation on the watch unless Silent extra is true.
     81                 if (!intent.getBooleanExtra(EXTRA_SILENT, false)) {
     82                     startConfirmationActivity(ConfirmationActivity.FAILURE_ANIMATION,
     83                                               getString(R.string.delete_unsuccessful));
     84                 }
     85             }
     86         } else {
     87             Log.e(TAG, "Failed to delete data item: " + dataItemUri
     88                     + " - Client disconnected from Google Play Services");
     89             // Show the failure animation on the watch unless Silent extra is true.
     90             if (!intent.getBooleanExtra(EXTRA_SILENT, false)) {
     91                 startConfirmationActivity(ConfirmationActivity.FAILURE_ANIMATION,
     92                         getString(R.string.delete_unsuccessful));
     93             }
     94         }
     95         mGoogleApiClient.disconnect();
     96     }
     97 
     98     private void startConfirmationActivity(int animationType, String message) {
     99         Intent confirmationActivity = new Intent(this, ConfirmationActivity.class)
    100                 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION)
    101                 .putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, animationType)
    102                 .putExtra(ConfirmationActivity.EXTRA_MESSAGE, message);
    103         startActivity(confirmationActivity);
    104     }
    105 
    106     @Override
    107     public void onConnected(Bundle connectionHint) {
    108         if (Log.isLoggable(TAG, Log.DEBUG)) {
    109             Log.d(TAG, "onConnected: " + connectionHint);
    110         }
    111     }
    112 
    113     @Override
    114     public void onConnectionSuspended(int cause) {
    115         if (Log.isLoggable(TAG, Log.DEBUG)) {
    116             Log.d(TAG, "onConnectionSuspended: " + cause);
    117         }
    118     }
    119 
    120     @Override
    121     public void onConnectionFailed(ConnectionResult result) {
    122         if (Log.isLoggable(TAG, Log.DEBUG)) {
    123             Log.d(TAG, "onConnectionFailed: " + result);
    124         }
    125     }
    126 
    127 }
    128