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.delayedconfirmation; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.util.Log; 22 import android.view.View; 23 import android.widget.Toast; 24 25 import com.google.android.gms.common.ConnectionResult; 26 import com.google.android.gms.common.api.GoogleApiClient; 27 import com.google.android.gms.common.api.ResultCallback; 28 import com.google.android.gms.wearable.MessageApi; 29 import com.google.android.gms.wearable.MessageEvent; 30 import com.google.android.gms.wearable.Node; 31 import com.google.android.gms.wearable.NodeApi; 32 import com.google.android.gms.wearable.Wearable; 33 34 /** 35 * Has a single button, used to start the Wearable MainActivity. 36 */ 37 public class MainActivity extends Activity implements MessageApi.MessageListener, 38 GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 39 40 private static final String TAG = "DelayedConfirmation"; 41 private static final String START_ACTIVITY_PATH = "/start-activity"; 42 private static final String TIMER_SELECTED_PATH = "/timer_selected"; 43 private static final String TIMER_FINISHED_PATH = "/timer_finished"; 44 private GoogleApiClient mGoogleApiClient; 45 46 @Override onCreate(Bundle b)47 public void onCreate(Bundle b) { 48 super.onCreate(b); 49 setContentView(R.layout.main_activity); 50 mGoogleApiClient = new GoogleApiClient.Builder(this) 51 .addApi(Wearable.API) 52 .addConnectionCallbacks(this) 53 .addOnConnectionFailedListener(this) 54 .build(); 55 } 56 57 @Override onResume()58 protected void onResume() { 59 super.onResume(); 60 if (!mGoogleApiClient.isConnected()) { 61 mGoogleApiClient.connect(); 62 } 63 } 64 65 @Override onDestroy()66 protected void onDestroy() { 67 if (mGoogleApiClient.isConnected()) { 68 mGoogleApiClient.disconnect(); 69 } 70 super.onDestroy(); 71 } 72 73 @Override onConnected(Bundle bundle)74 public void onConnected(Bundle bundle) { 75 Wearable.MessageApi.addListener(mGoogleApiClient, this); 76 } 77 78 @Override onConnectionSuspended(int i)79 public void onConnectionSuspended(int i) { 80 Wearable.MessageApi.removeListener(mGoogleApiClient, this); 81 } 82 83 @Override onMessageReceived(final MessageEvent messageEvent)84 public void onMessageReceived(final MessageEvent messageEvent) { 85 runOnUiThread(new Runnable() { 86 @Override 87 public void run() { 88 if (messageEvent.getPath().equals(TIMER_SELECTED_PATH)) { 89 Toast.makeText(getApplicationContext(), R.string.toast_timer_selected, 90 Toast.LENGTH_SHORT).show(); 91 } else if (messageEvent.getPath().equals(TIMER_FINISHED_PATH)) { 92 Toast.makeText(getApplicationContext(), R.string.toast_timer_finished, 93 Toast.LENGTH_SHORT).show(); 94 } 95 } 96 }); 97 98 } 99 100 @Override onConnectionFailed(ConnectionResult connectionResult)101 public void onConnectionFailed(ConnectionResult connectionResult) { 102 Log.e(TAG, "Failed to connect to Google Api Client with error code " 103 + connectionResult.getErrorCode()); 104 } 105 106 /** 107 * Sends a message to Wearable MainActivity when button is pressed. 108 */ onStartWearableActivityClick(View view)109 public void onStartWearableActivityClick(View view) { 110 Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback( 111 new ResultCallback<NodeApi.GetConnectedNodesResult>() { 112 @Override 113 public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) { 114 for (final Node node : getConnectedNodesResult.getNodes()) { 115 Wearable.MessageApi.sendMessage( 116 mGoogleApiClient, node.getId(), START_ACTIVITY_PATH, new byte[0]) 117 .setResultCallback(getSendMessageResultCallback()); 118 } 119 } 120 }); 121 } 122 getSendMessageResultCallback()123 private ResultCallback<MessageApi.SendMessageResult> getSendMessageResultCallback() { 124 return new ResultCallback<MessageApi.SendMessageResult>() { 125 @Override 126 public void onResult(MessageApi.SendMessageResult sendMessageResult) { 127 if (!sendMessageResult.getStatus().isSuccess()) { 128 Log.e(TAG, "Failed to connect to Google Api Client with status " 129 + sendMessageResult.getStatus()); 130 } 131 } 132 }; 133 } 134 } 135