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 static com.example.android.wearable.quiz.Constants.CONNECT_TIMEOUT_MS;
     20 import static com.example.android.wearable.quiz.Constants.GET_CAPABILITIES_TIMEOUT_MS;
     21 import static com.example.android.wearable.quiz.Constants.RESET_QUIZ_PATH;
     22 
     23 import android.app.IntentService;
     24 import android.content.Intent;
     25 import android.util.Log;
     26 
     27 import com.google.android.gms.common.ConnectionResult;
     28 import com.google.android.gms.common.api.GoogleApiClient;
     29 import com.google.android.gms.wearable.CapabilityApi;
     30 import com.google.android.gms.wearable.CapabilityInfo;
     31 import com.google.android.gms.wearable.Node;
     32 import com.google.android.gms.wearable.Wearable;
     33 
     34 import java.util.Set;
     35 import java.util.concurrent.TimeUnit;
     36 
     37 /**
     38  * Service to reset the quiz (by sending a message to the phone) when the Reset Quiz
     39  * action on the Quiz Report is selected.
     40  */
     41 public class QuizReportActionService extends IntentService {
     42 
     43     public static final String ACTION_RESET_QUIZ = "com.example.android.wearable.quiz.RESET_QUIZ";
     44 
     45     private static final String TAG = "QuizReportActionService";
     46     private static final String RESET_QUIZ_CAPABILITY_NAME = "reset_quiz";
     47 
     48     public QuizReportActionService() {
     49         super(QuizReportActionService.class.getSimpleName());
     50     }
     51 
     52     @Override
     53     public void onHandleIntent(Intent intent) {
     54         if (intent.getAction().equals(ACTION_RESET_QUIZ)) {
     55             final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
     56                     .addApi(Wearable.API)
     57                     .build();
     58             ConnectionResult result = googleApiClient.blockingConnect(CONNECT_TIMEOUT_MS,
     59                     TimeUnit.MILLISECONDS);
     60             if (!result.isSuccess()) {
     61                 Log.e(TAG, "QuizReportActionService failed to connect to GoogleApiClient.");
     62                 return;
     63             }
     64 
     65             CapabilityApi.GetCapabilityResult capabilityResult = Wearable.CapabilityApi
     66                     .getCapability(googleApiClient, RESET_QUIZ_CAPABILITY_NAME,
     67                             CapabilityApi.FILTER_REACHABLE)
     68                     .await(GET_CAPABILITIES_TIMEOUT_MS, TimeUnit.MILLISECONDS);
     69             if (capabilityResult.getStatus().isSuccess()) {
     70                 sendResetMessage(googleApiClient, capabilityResult.getCapability());
     71             } else {
     72                 Log.e(TAG, "Failed to get capabilities, status: "
     73                         + capabilityResult.getStatus().getStatusMessage());
     74             }
     75         }
     76     }
     77 
     78     private void sendResetMessage(GoogleApiClient googleApiClient, CapabilityInfo capabilityInfo) {
     79         Set<Node> connectedNodes = capabilityInfo.getNodes();
     80         if (connectedNodes.isEmpty()) {
     81             Log.w(TAG, "No node capable of resetting quiz was found");
     82         } else {
     83             for (Node node : connectedNodes) {
     84                 Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), RESET_QUIZ_PATH,
     85                         new byte[0]);
     86             }
     87         }
     88     }
     89 }
     90