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