Home | History | Annotate | Download | only in com.example.android.wearable.watchface
      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.watchface;
     18 
     19 import android.os.Bundle;
     20 import android.util.Log;
     21 
     22 import com.google.android.gms.common.ConnectionResult;
     23 import com.google.android.gms.common.api.GoogleApiClient;
     24 import com.google.android.gms.wearable.DataMap;
     25 import com.google.android.gms.wearable.MessageEvent;
     26 import com.google.android.gms.wearable.Wearable;
     27 import com.google.android.gms.wearable.WearableListenerService;
     28 
     29 import java.util.concurrent.TimeUnit;
     30 
     31 /**
     32  * A {@link WearableListenerService} listening for {@link DigitalWatchFaceService} config messages
     33  * and updating the config {@link com.google.android.gms.wearable.DataItem} accordingly.
     34  */
     35 public class DigitalWatchFaceConfigListenerService extends WearableListenerService
     36         implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
     37     private static final String TAG = "DigitalListenerService";
     38 
     39     private GoogleApiClient mGoogleApiClient;
     40 
     41     @Override // WearableListenerService
     42     public void onMessageReceived(MessageEvent messageEvent) {
     43         if (!messageEvent.getPath().equals(DigitalWatchFaceUtil.PATH_WITH_FEATURE)) {
     44             return;
     45         }
     46         byte[] rawData = messageEvent.getData();
     47         // It's allowed that the message carries only some of the keys used in the config DataItem
     48         // and skips the ones that we don't want to change.
     49         DataMap configKeysToOverwrite = DataMap.fromByteArray(rawData);
     50         if (Log.isLoggable(TAG, Log.DEBUG)) {
     51             Log.d(TAG, "Received watch face config message: " + configKeysToOverwrite);
     52         }
     53 
     54         if (mGoogleApiClient == null) {
     55             mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this)
     56                     .addOnConnectionFailedListener(this).addApi(Wearable.API).build();
     57         }
     58         if (!mGoogleApiClient.isConnected()) {
     59             ConnectionResult connectionResult =
     60                     mGoogleApiClient.blockingConnect(30, TimeUnit.SECONDS);
     61 
     62             if (!connectionResult.isSuccess()) {
     63                 Log.e(TAG, "Failed to connect to GoogleApiClient.");
     64                 return;
     65             }
     66         }
     67 
     68         DigitalWatchFaceUtil.overwriteKeysInConfigDataMap(mGoogleApiClient, configKeysToOverwrite);
     69     }
     70 
     71     @Override // GoogleApiClient.ConnectionCallbacks
     72     public void onConnected(Bundle connectionHint) {
     73         if (Log.isLoggable(TAG, Log.DEBUG)) {
     74             Log.d(TAG, "onConnected: " + connectionHint);
     75         }
     76     }
     77 
     78     @Override  // GoogleApiClient.ConnectionCallbacks
     79     public void onConnectionSuspended(int cause) {
     80         if (Log.isLoggable(TAG, Log.DEBUG)) {
     81             Log.d(TAG, "onConnectionSuspended: " + cause);
     82         }
     83     }
     84 
     85     @Override  // GoogleApiClient.OnConnectionFailedListener
     86     public void onConnectionFailed(ConnectionResult result) {
     87         if (Log.isLoggable(TAG, Log.DEBUG)) {
     88             Log.d(TAG, "onConnectionFailed: " + result);
     89         }
     90     }
     91 }
     92