Home | History | Annotate | Download | only in delayedconfirmation
      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.app.Notification;
     21 import android.app.NotificationManager;
     22 import android.os.Bundle;
     23 import android.support.wearable.view.DelayedConfirmationView;
     24 import android.util.Log;
     25 import android.view.View;
     26 
     27 import com.google.android.gms.common.ConnectionResult;
     28 import com.google.android.gms.common.api.GoogleApiClient;
     29 import com.google.android.gms.common.api.ResultCallback;
     30 import com.google.android.gms.wearable.MessageApi;
     31 import com.google.android.gms.wearable.Node;
     32 import com.google.android.gms.wearable.NodeApi;
     33 import com.google.android.gms.wearable.Wearable;
     34 
     35 
     36 public class MainActivity extends Activity implements
     37         DelayedConfirmationView.DelayedConfirmationListener,
     38         GoogleApiClient.OnConnectionFailedListener {
     39 
     40     private static final String TAG = "DelayedConfirmation";
     41     private static final int NUM_SECONDS = 5;
     42 
     43     private static final String TIMER_SELECTED_PATH = "/timer_selected";
     44     private static final String TIMER_FINISHED_PATH = "/timer_finished";
     45 
     46     private DelayedConfirmationView delayedConfirmationView;
     47     private GoogleApiClient mGoogleApiClient;
     48 
     49     @Override
     50     public void onCreate(Bundle b) {
     51         super.onCreate(b);
     52         setContentView(R.layout.main_activity);
     53         delayedConfirmationView = (DelayedConfirmationView) findViewById(R.id.delayed_confirmation);
     54         delayedConfirmationView.setTotalTimeMs(NUM_SECONDS * 1000);
     55         mGoogleApiClient = new GoogleApiClient.Builder(this)
     56                 .addApi(Wearable.API)
     57                 .addOnConnectionFailedListener(this)
     58                 .build();
     59     }
     60 
     61     @Override
     62     protected void onResume() {
     63         super.onResume();
     64         if (!mGoogleApiClient.isConnected()) {
     65             mGoogleApiClient.connect();
     66         }
     67     }
     68 
     69     @Override
     70     protected void onDestroy() {
     71         if (mGoogleApiClient.isConnected()) {
     72             mGoogleApiClient.disconnect();
     73         }
     74         super.onDestroy();
     75     }
     76 
     77     /**
     78      * Starts the DelayedConfirmationView when user presses "Start Timer" button.
     79      */
     80     public void onStartTimer(View view) {
     81         delayedConfirmationView.start();
     82         delayedConfirmationView.setListener(this);
     83     }
     84 
     85     @Override
     86     public void onTimerSelected(View v) {
     87         v.setPressed(true);
     88         Notification notification = new Notification.Builder(this)
     89                 .setSmallIcon(R.drawable.ic_launcher)
     90                 .setContentTitle(getString(R.string.notification_title))
     91                 .setContentText(getString(R.string.notification_timer_selected))
     92                 .build();
     93         ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(0, notification);
     94         sendMessageToCompanion(TIMER_SELECTED_PATH);
     95         // Prevent onTimerFinished from being heard.
     96         ((DelayedConfirmationView) v).setListener(null);
     97         finish();
     98     }
     99 
    100     @Override
    101     public void onTimerFinished(View v) {
    102         Notification notification = new Notification.Builder(this)
    103                 .setSmallIcon(R.drawable.ic_launcher)
    104                 .setContentTitle(getString(R.string.notification_title))
    105                 .setContentText(getString(R.string.notification_timer_finished))
    106                 .build();
    107         ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(0, notification);
    108         sendMessageToCompanion(TIMER_FINISHED_PATH);
    109         finish();
    110     }
    111 
    112     @Override
    113     public void onConnectionFailed(ConnectionResult connectionResult) {
    114         Log.e(TAG, "Failed to connect to Google Api Client");
    115     }
    116 
    117     private void sendMessageToCompanion(final String path) {
    118         Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(
    119                 new ResultCallback<NodeApi.GetConnectedNodesResult>() {
    120                     @Override
    121                     public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
    122                         for (final Node node : getConnectedNodesResult.getNodes()) {
    123                             Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), path,
    124                                     new byte[0]).setResultCallback(getSendMessageResultCallback());
    125                         }
    126                     }
    127                 }
    128         );
    129 
    130     }
    131 
    132     private ResultCallback<MessageApi.SendMessageResult> getSendMessageResultCallback() {
    133         return new ResultCallback<MessageApi.SendMessageResult>() {
    134             @Override
    135             public void onResult(MessageApi.SendMessageResult sendMessageResult) {
    136                 if (!sendMessageResult.getStatus().isSuccess()) {
    137                     Log.e(TAG, "Failed to connect to Google Api Client with status "
    138                             + sendMessageResult.getStatus());
    139                 }
    140             }
    141         };
    142     }
    143 }
    144