• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.tv.settings.system;
18 
19 import android.app.AlertDialog;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.speech.tts.TextToSpeech;
23 import android.support.v7.preference.Preference;
24 import android.support.v7.preference.PreferenceViewHolder;
25 import android.util.Log;
26 import android.widget.Checkable;
27 import android.widget.RadioButton;
28 
29 import com.android.tv.settings.R;
30 
31 public class TtsEnginePreference extends Preference {
32 
33     private static final String TAG = "TtsEnginePreference";
34 
35     /**
36      * The engine information for the engine this preference represents.
37      * Contains it's name, label etc. which are used for display.
38      */
39     private final TextToSpeech.EngineInfo mEngineInfo;
40 
41     /**
42      * The shared radio button state, which button is checked etc.
43      */
44     private final RadioButtonGroupState mSharedState;
45 
46     private RadioButton mRadioButton;
47 
TtsEnginePreference(Context context, TextToSpeech.EngineInfo info, RadioButtonGroupState state)48     public TtsEnginePreference(Context context, TextToSpeech.EngineInfo info,
49             RadioButtonGroupState state) {
50         super(context);
51         setWidgetLayoutResource(R.layout.radio_preference_widget);
52 
53         mSharedState = state;
54         mEngineInfo = info;
55 
56         setKey(mEngineInfo.name);
57         setTitle(mEngineInfo.label);
58     }
59 
60     @Override
onBindViewHolder(PreferenceViewHolder viewHolder)61     public void onBindViewHolder(PreferenceViewHolder viewHolder) {
62         super.onBindViewHolder(viewHolder);
63 
64         final RadioButton rb = (RadioButton) viewHolder.findViewById(android.R.id.checkbox);
65 
66         boolean isChecked = getKey().equals(mSharedState.getCurrentKey());
67         if (isChecked) {
68             mSharedState.setCurrentChecked(rb);
69         }
70 
71         rb.setChecked(isChecked);
72 
73         mRadioButton = rb;
74     }
75 
76     @Override
onClick()77     protected void onClick() {
78         super.onClick();
79         onRadioButtonClicked(mRadioButton, !mRadioButton.isChecked());
80     }
81 
shouldDisplayDataAlert()82     private boolean shouldDisplayDataAlert() {
83         return !mEngineInfo.system;
84     }
85 
displayDataAlert( DialogInterface.OnClickListener positiveOnClickListener, DialogInterface.OnClickListener negativeOnClickListener)86     private void displayDataAlert(
87             DialogInterface.OnClickListener positiveOnClickListener,
88             DialogInterface.OnClickListener negativeOnClickListener) {
89         // TODO: don't use phone UI
90         Log.i(TAG, "Displaying data alert for :" + mEngineInfo.name);
91 
92         AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
93         builder.setTitle(android.R.string.dialog_alert_title)
94                 .setMessage(getContext().getString(
95                         R.string.tts_engine_security_warning, mEngineInfo.label))
96                 .setCancelable(true)
97                 .setPositiveButton(android.R.string.ok, positiveOnClickListener)
98                 .setNegativeButton(android.R.string.cancel, negativeOnClickListener);
99 
100         AlertDialog dialog = builder.create();
101         dialog.show();
102     }
103 
onRadioButtonClicked(final Checkable buttonView, boolean isChecked)104     private void onRadioButtonClicked(final Checkable buttonView,
105             boolean isChecked) {
106         if (mSharedState.getCurrentChecked() == buttonView) {
107             return;
108         }
109 
110         if (isChecked) {
111             // Should we alert user? if that's true, delay making engine current one.
112             if (shouldDisplayDataAlert()) {
113                 displayDataAlert(
114                         new DialogInterface.OnClickListener() {
115                             @Override
116                             public void onClick(DialogInterface dialog, int which) {
117                                 makeCurrentEngine(buttonView);
118                             }
119                         },
120                         new DialogInterface.OnClickListener() {
121                             @Override
122                             public void onClick(DialogInterface dialog, int which) {
123                                 // Undo the click.
124                                 buttonView.setChecked(false);
125                             }
126                         });
127             } else {
128                 // Privileged engine, set it current
129                 makeCurrentEngine(buttonView);
130             }
131         }
132     }
133 
makeCurrentEngine(Checkable current)134     private void makeCurrentEngine(Checkable current) {
135         if (mSharedState.getCurrentChecked() != null) {
136             mSharedState.getCurrentChecked().setChecked(false);
137         }
138         mSharedState.setCurrentChecked(current);
139         mSharedState.setCurrentKey(getKey());
140         callChangeListener(mSharedState.getCurrentKey());
141     }
142 
143 
144     /**
145      * Holds all state that is common to this group of radio buttons, such
146      * as the currently selected key and the currently checked compound button.
147      * (which corresponds to this key).
148      */
149     public interface RadioButtonGroupState {
getCurrentKey()150         String getCurrentKey();
getCurrentChecked()151         Checkable getCurrentChecked();
152 
setCurrentKey(String key)153         void setCurrentKey(String key);
setCurrentChecked(Checkable current)154         void setCurrentChecked(Checkable current);
155     }
156 
157 }
158