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.CHOSEN_ANSWER_CORRECT;
     20 import static com.example.android.wearable.quiz.Constants.QUESTION_INDEX;
     21 import static com.example.android.wearable.quiz.Constants.QUESTION_WAS_ANSWERED;
     22 
     23 import android.app.IntentService;
     24 import android.app.NotificationManager;
     25 import android.content.Intent;
     26 import android.net.Uri;
     27 import android.os.Bundle;
     28 import android.util.Log;
     29 
     30 import com.google.android.gms.common.ConnectionResult;
     31 import com.google.android.gms.common.api.GoogleApiClient;
     32 import com.google.android.gms.wearable.DataApi;
     33 import com.google.android.gms.wearable.DataMap;
     34 import com.google.android.gms.wearable.DataMapItem;
     35 import com.google.android.gms.wearable.PutDataMapRequest;
     36 import com.google.android.gms.wearable.PutDataRequest;
     37 import com.google.android.gms.wearable.Wearable;
     38 
     39 import java.util.concurrent.TimeUnit;
     40 
     41 /**
     42  * Updates quiz status on the phone when user selects an answer to a question on the watch.
     43  */
     44 public class UpdateQuestionService extends IntentService
     45         implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
     46 
     47     public static final String EXTRA_QUESTION_CORRECT = "extra_question_correct";
     48     public static final String EXTRA_QUESTION_INDEX = "extra_question_index";
     49 
     50     private static final long TIME_OUT_MS = 100;
     51     private static final String TAG = "UpdateQuestionService";
     52 
     53     private GoogleApiClient mGoogleApiClient;
     54 
     55     public UpdateQuestionService() {
     56         super(UpdateQuestionService.class.getSimpleName());
     57     }
     58 
     59     @Override
     60     public void onCreate() {
     61         super.onCreate();
     62         mGoogleApiClient = new GoogleApiClient.Builder(this)
     63                 .addApi(Wearable.API)
     64                 .addConnectionCallbacks(this)
     65                 .addOnConnectionFailedListener(this)
     66                 .build();
     67     }
     68 
     69     @Override
     70     protected void onHandleIntent(Intent intent) {
     71         mGoogleApiClient.blockingConnect(TIME_OUT_MS, TimeUnit.MILLISECONDS);
     72         Uri dataItemUri = intent.getData();
     73         if (!mGoogleApiClient.isConnected()) {
     74             Log.e(TAG, "Failed to update data item " + dataItemUri
     75                     + " because client is disconnected from Google Play Services");
     76             return;
     77         }
     78         DataApi.DataItemResult dataItemResult = Wearable.DataApi.getDataItem(
     79                 mGoogleApiClient, dataItemUri).await();
     80         PutDataMapRequest putDataMapRequest = PutDataMapRequest
     81                 .createFromDataMapItem(DataMapItem.fromDataItem(dataItemResult.getDataItem()));
     82         DataMap dataMap = putDataMapRequest.getDataMap();
     83 
     84         // Update quiz status variables, which will be reflected on the phone.
     85         int questionIndex = intent.getIntExtra(EXTRA_QUESTION_INDEX, -1);
     86         boolean chosenAnswerCorrect = intent.getBooleanExtra(EXTRA_QUESTION_CORRECT, false);
     87         dataMap.putInt(QUESTION_INDEX, questionIndex);
     88         dataMap.putBoolean(CHOSEN_ANSWER_CORRECT, chosenAnswerCorrect);
     89         dataMap.putBoolean(QUESTION_WAS_ANSWERED, true);
     90         PutDataRequest request = putDataMapRequest.asPutDataRequest();
     91         request.setUrgent();
     92         Wearable.DataApi.putDataItem(mGoogleApiClient, request).await();
     93 
     94         // Remove this question notification.
     95         ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancel(questionIndex);
     96         mGoogleApiClient.disconnect();
     97     }
     98 
     99     @Override
    100     public void onConnected(Bundle connectionHint) {
    101     }
    102 
    103     @Override
    104     public void onConnectionSuspended(int cause) {
    105     }
    106 
    107     @Override
    108     public void onConnectionFailed(ConnectionResult result) {
    109     }
    110 }
    111