Home | History | Annotate | Download | only in com.example.android.wearable.datalayer
      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.datalayer;
     18 
     19 import android.content.Intent;
     20 import android.net.Uri;
     21 import android.util.Log;
     22 
     23 import com.google.android.gms.common.ConnectionResult;
     24 import com.google.android.gms.common.api.GoogleApiClient;
     25 import com.google.android.gms.wearable.DataEvent;
     26 import com.google.android.gms.wearable.DataEventBuffer;
     27 import com.google.android.gms.wearable.MessageEvent;
     28 import com.google.android.gms.wearable.Node;
     29 import com.google.android.gms.wearable.Wearable;
     30 import com.google.android.gms.wearable.WearableListenerService;
     31 
     32 import java.util.List;
     33 import java.util.concurrent.TimeUnit;
     34 
     35 /**
     36  * Listens to DataItems and Messages from the local node.
     37  */
     38 public class DataLayerListenerService extends WearableListenerService {
     39 
     40     private static final String TAG = "DataLayerListenerServic";
     41 
     42     private static final String START_ACTIVITY_PATH = "/start-activity";
     43     private static final String DATA_ITEM_RECEIVED_PATH = "/data-item-received";
     44     public static final String COUNT_PATH = "/count";
     45     public static final String IMAGE_PATH = "/image";
     46     public static final String IMAGE_KEY = "photo";
     47     GoogleApiClient mGoogleApiClient;
     48 
     49     @Override
     50     public void onCreate() {
     51         super.onCreate();
     52         mGoogleApiClient = new GoogleApiClient.Builder(this)
     53                 .addApi(Wearable.API)
     54                 .build();
     55         mGoogleApiClient.connect();
     56     }
     57 
     58     @Override
     59     public void onDataChanged(DataEventBuffer dataEvents) {
     60         LOGD(TAG, "onDataChanged: " + dataEvents);
     61         if (!mGoogleApiClient.isConnected() || !mGoogleApiClient.isConnecting()) {
     62             ConnectionResult connectionResult = mGoogleApiClient
     63                     .blockingConnect(30, TimeUnit.SECONDS);
     64             if (!connectionResult.isSuccess()) {
     65                 Log.e(TAG, "DataLayerListenerService failed to connect to GoogleApiClient, "
     66                         + "error code: " + connectionResult.getErrorCode());
     67                 return;
     68             }
     69         }
     70 
     71         // Loop through the events and send a message back to the node that created the data item.
     72         for (DataEvent event : dataEvents) {
     73             Uri uri = event.getDataItem().getUri();
     74             String path = uri.getPath();
     75             if (COUNT_PATH.equals(path)) {
     76                 // Get the node id of the node that created the data item from the host portion of
     77                 // the uri.
     78                 String nodeId = uri.getHost();
     79                 // Set the data of the message to be the bytes of the Uri.
     80                 byte[] payload = uri.toString().getBytes();
     81 
     82                 // Send the rpc
     83                 Wearable.MessageApi.sendMessage(mGoogleApiClient, nodeId, DATA_ITEM_RECEIVED_PATH,
     84                         payload);
     85             }
     86         }
     87     }
     88 
     89     @Override
     90     public void onMessageReceived(MessageEvent messageEvent) {
     91         LOGD(TAG, "onMessageReceived: " + messageEvent);
     92 
     93         // Check to see if the message is to start an activity
     94         if (messageEvent.getPath().equals(START_ACTIVITY_PATH)) {
     95             Intent startIntent = new Intent(this, MainActivity.class);
     96             startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     97             startActivity(startIntent);
     98         }
     99     }
    100 
    101     @Override
    102     public void onPeerConnected(Node peer) {
    103         LOGD(TAG, "onPeerConnected: " + peer);
    104     }
    105 
    106     @Override
    107     public void onPeerDisconnected(Node peer) {
    108         LOGD(TAG, "onPeerDisconnected: " + peer);
    109     }
    110 
    111     public static void LOGD(final String tag, String message) {
    112         if (Log.isLoggable(tag, Log.DEBUG)) {
    113             Log.d(tag, message);
    114         }
    115     }
    116 }
    117