Home | History | Annotate | Download | only in utils
      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.android.settings.utils;
     18 
     19 import android.app.Activity;
     20 import android.app.Fragment;
     21 import android.app.VoiceInteractor;
     22 import android.app.VoiceInteractor.AbortVoiceRequest;
     23 import android.app.VoiceInteractor.CompleteVoiceRequest;
     24 import android.content.Intent;
     25 import android.os.Bundle;
     26 import android.view.View;
     27 import android.widget.TextView;
     28 import android.util.Log;
     29 
     30 /**
     31  * Activity for modifying a setting using the Voice Interaction API. This activity
     32  * will only allow modifying the setting if the intent was sent using
     33  * {@link android.service.voice.VoiceInteractionSession#startVoiceActivity startVoiceActivity}
     34  * by the current Voice Interaction Service.
     35  */
     36 abstract public class VoiceSettingsActivity extends Activity {
     37 
     38     private static final String TAG = "VoiceSettingsActivity";
     39 
     40     @Override
     41     public void onCreate(Bundle savedInstanceState) {
     42         super.onCreate(savedInstanceState);
     43 
     44         if (isVoiceInteractionRoot()) {
     45             // Only permit if this is a voice interaction.
     46             if (onVoiceSettingInteraction(getIntent())) {
     47                 // If it's complete, finish.
     48                 finish();
     49             }
     50         } else {
     51             Log.v(TAG, "Cannot modify settings without voice interaction");
     52             finish();
     53         }
     54     }
     55 
     56     /**
     57      * Modify the setting as a voice interaction. Should return true if the
     58      * voice interaction is complete or false if more interaction is required.
     59      */
     60     abstract protected boolean onVoiceSettingInteraction(Intent intent);
     61 
     62     /**
     63      * Send a notification that the interaction was successful. If {@param prompt} is
     64      * not null, then it will be read to the user.
     65      */
     66     protected void notifySuccess(CharSequence prompt) {
     67         if (getVoiceInteractor() != null) {
     68             getVoiceInteractor().submitRequest(new CompleteVoiceRequest(prompt, null) {
     69                 @Override
     70                 public void onCompleteResult(Bundle options) {
     71                     finish();
     72                 }
     73             });
     74         }
     75     }
     76 
     77     /**
     78      * Indicates when the setting could not be changed.
     79      */
     80     protected void notifyFailure(CharSequence prompt) {
     81         if (getVoiceInteractor() != null) {
     82             getVoiceInteractor().submitRequest(new AbortVoiceRequest(prompt, null));
     83         }
     84     }
     85 }
     86