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