Home | History | Annotate | Download | only in com.example.android.wearable.quiz
      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.quiz;
     18 
     19 import android.app.IntentService;
     20 import android.content.Intent;
     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.Node;
     26 import com.google.android.gms.wearable.NodeApi;
     27 import com.google.android.gms.wearable.Wearable;
     28 
     29 import java.util.concurrent.TimeUnit;
     30 
     31 import static com.example.android.wearable.quiz.Constants.CONNECT_TIMEOUT_MS;
     32 import static com.example.android.wearable.quiz.Constants.RESET_QUIZ_PATH;
     33 
     34 /**
     35  * Service to reset the quiz (by sending a message to the phone) when the Reset Quiz
     36  * action on the Quiz Report is selected.
     37  */
     38 public class QuizReportActionService extends IntentService {
     39     public static final String ACTION_RESET_QUIZ = "com.example.android.wearable.quiz.RESET_QUIZ";
     40 
     41     private static final String TAG = "QuizReportActionReceiver";
     42 
     43     public QuizReportActionService() {
     44         super(QuizReportActionService.class.getSimpleName());
     45     }
     46 
     47     @Override
     48     public void onHandleIntent(Intent intent) {
     49         if (intent.getAction().equals(ACTION_RESET_QUIZ)) {
     50             GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
     51                     .addApi(Wearable.API)
     52                     .build();
     53             ConnectionResult result = googleApiClient.blockingConnect(CONNECT_TIMEOUT_MS,
     54                     TimeUnit.MILLISECONDS);
     55             if (!result.isSuccess()) {
     56                 Log.e(TAG, "QuizListenerService failed to connect to GoogleApiClient.");
     57                 return;
     58             }
     59             NodeApi.GetConnectedNodesResult nodes =
     60                     Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
     61             for (Node node : nodes.getNodes()) {
     62                 Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), RESET_QUIZ_PATH,
     63                         new byte[0]);
     64             }
     65         }
     66     }
     67 }
     68