blob: 956bdaf384bc5d61c96c5f82d76d2d1a5cce49c5 [file] [log] [blame]
Trevor Johnsf420ff02014-10-22 20:10:29 -07001/*
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
Trevor Johns527a4f32014-11-12 11:39:30 -080017package com.example.android.wearable.quiz;
Trevor Johnsf420ff02014-10-22 20:10:29 -070018
19import android.app.IntentService;
20import android.content.Intent;
21import android.util.Log;
22
23import com.google.android.gms.common.ConnectionResult;
24import com.google.android.gms.common.api.GoogleApiClient;
25import com.google.android.gms.wearable.Node;
26import com.google.android.gms.wearable.NodeApi;
27import com.google.android.gms.wearable.Wearable;
28
29import java.util.concurrent.TimeUnit;
30
Trevor Johns527a4f32014-11-12 11:39:30 -080031import static com.example.android.wearable.quiz.Constants.CONNECT_TIMEOUT_MS;
32import static com.example.android.wearable.quiz.Constants.RESET_QUIZ_PATH;
Trevor Johnsf420ff02014-10-22 20:10:29 -070033
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 */
38public class QuizReportActionService extends IntentService {
Trevor Johns527a4f32014-11-12 11:39:30 -080039 public static final String ACTION_RESET_QUIZ = "com.example.android.wearable.quiz.RESET_QUIZ";
Trevor Johnsf420ff02014-10-22 20:10:29 -070040
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}