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 17package com.android.settings.voice; 18 19import android.app.AlertDialog; 20import android.content.ComponentName; 21import android.content.Context; 22import android.content.DialogInterface; 23import android.content.Intent; 24import android.preference.Preference; 25import android.util.Log; 26import android.view.View; 27import android.view.ViewGroup; 28import android.widget.Checkable; 29import android.widget.CompoundButton; 30import android.widget.RadioButton; 31 32 33import com.android.settings.R; 34import com.android.settings.Utils; 35 36public final class VoiceInputPreference extends Preference { 37 38 private static final String TAG = "VoiceInputPreference"; 39 40 private final CharSequence mLabel; 41 42 private final CharSequence mAppLabel; 43 44 private final CharSequence mAlertText; 45 46 private final ComponentName mSettingsComponent; 47 48 /** 49 * The shared radio button state, which button is checked etc. 50 */ 51 private final RadioButtonGroupState mSharedState; 52 53 /** 54 * When true, the change callbacks on the radio button will not 55 * fire. 56 */ 57 private volatile boolean mPreventRadioButtonCallbacks; 58 59 private View mSettingsIcon; 60 private RadioButton mRadioButton; 61 62 private final CompoundButton.OnCheckedChangeListener mRadioChangeListener = 63 new CompoundButton.OnCheckedChangeListener() { 64 @Override 65 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 66 onRadioButtonClicked(buttonView, isChecked); 67 } 68 }; 69 70 public VoiceInputPreference(Context context, VoiceInputHelper.BaseInfo info, 71 CharSequence summary, CharSequence alertText, RadioButtonGroupState state) { 72 super(context); 73 setLayoutResource(R.layout.preference_tts_engine); 74 75 mSharedState = state; 76 mLabel = info.label; 77 mAppLabel = info.appLabel; 78 mAlertText = alertText; 79 mSettingsComponent = info.settings; 80 mPreventRadioButtonCallbacks = false; 81 82 setKey(info.key); 83 setTitle(info.label); 84 setSummary(summary); 85 } 86 87 @Override 88 public View getView(View convertView, ViewGroup parent) { 89 if (mSharedState == null) { 90 throw new IllegalStateException("Call to getView() before a call to" + 91 "setSharedState()"); 92 } 93 94 View view = super.getView(convertView, parent); 95 final RadioButton rb = (RadioButton) view.findViewById(R.id.tts_engine_radiobutton); 96 rb.setOnCheckedChangeListener(mRadioChangeListener); 97 98 boolean isChecked = getKey().equals(mSharedState.getCurrentKey()); 99 if (isChecked) { 100 mSharedState.setCurrentChecked(rb); 101 } 102 103 mPreventRadioButtonCallbacks = true; 104 rb.setChecked(isChecked); 105 mPreventRadioButtonCallbacks = false; 106 107 mRadioButton = rb; 108 109 View textLayout = view.findViewById(R.id.tts_engine_pref_text); 110 textLayout.setOnClickListener(new View.OnClickListener() { 111 @Override 112 public void onClick(View v) { 113 onRadioButtonClicked(rb, !rb.isChecked()); 114 } 115 }); 116 117 mSettingsIcon = view.findViewById(R.id.tts_engine_settings); 118 mSettingsIcon.setOnClickListener(new View.OnClickListener() { 119 @Override 120 public void onClick(View v) { 121 Intent intent = new Intent(Intent.ACTION_MAIN); 122 intent.setComponent(mSettingsComponent); 123 getContext().startActivity(new Intent(intent)); 124 } 125 }); 126 updateCheckedState(isChecked); 127 128 return view; 129 } 130 131 private boolean shouldDisplayAlert() { 132 return mAlertText != null; 133 } 134 135 private void displayAlert( 136 final DialogInterface.OnClickListener positiveOnClickListener, 137 final DialogInterface.OnClickListener negativeOnClickListener) { 138 Log.i(TAG, "Displaying data alert for :" + getKey()); 139 140 AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); 141 String msg = String.format(getContext().getResources().getConfiguration().locale, 142 mAlertText.toString(), mAppLabel); 143 builder.setTitle(android.R.string.dialog_alert_title) 144 .setMessage(msg) 145 .setCancelable(true) 146 .setPositiveButton(android.R.string.ok, positiveOnClickListener) 147 .setNegativeButton(android.R.string.cancel, negativeOnClickListener) 148 .setOnCancelListener(new DialogInterface.OnCancelListener() { 149 @Override public void onCancel(DialogInterface dialog) { 150 negativeOnClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE); 151 } 152 }); 153 154 AlertDialog dialog = builder.create(); 155 dialog.show(); 156 } 157 158 public void doClick() { 159 mRadioButton.performClick(); 160 } 161 162 void updateCheckedState(boolean isChecked) { 163 if (mSettingsComponent != null) { 164 mSettingsIcon.setVisibility(View.VISIBLE); 165 if (isChecked) { 166 mSettingsIcon.setEnabled(true); 167 mSettingsIcon.setAlpha(1); 168 } else { 169 mSettingsIcon.setEnabled(false); 170 mSettingsIcon.setAlpha(Utils.DISABLED_ALPHA); 171 } 172 } else { 173 mSettingsIcon.setVisibility(View.GONE); 174 } 175 } 176 177 void onRadioButtonClicked(final CompoundButton buttonView, boolean isChecked) { 178 if (mPreventRadioButtonCallbacks) { 179 return; 180 } 181 if (mSharedState.getCurrentChecked() == buttonView) { 182 updateCheckedState(isChecked); 183 return; 184 } 185 186 if (isChecked) { 187 // Should we alert user? if that's true, delay making engine current one. 188 if (shouldDisplayAlert()) { 189 displayAlert(new DialogInterface.OnClickListener() { 190 @Override 191 public void onClick(DialogInterface dialog, int which) { 192 makeCurrentChecked(buttonView); 193 } 194 }, new DialogInterface.OnClickListener() { 195 @Override 196 public void onClick(DialogInterface dialog, int which) { 197 // Undo the click. 198 buttonView.setChecked(false); 199 } 200 } 201 ); 202 } else { 203 // Privileged engine, set it current 204 makeCurrentChecked(buttonView); 205 } 206 } else { 207 updateCheckedState(isChecked); 208 } 209 } 210 211 void makeCurrentChecked(Checkable current) { 212 if (mSharedState.getCurrentChecked() != null) { 213 mSharedState.getCurrentChecked().setChecked(false); 214 } 215 mSharedState.setCurrentChecked(current); 216 mSharedState.setCurrentKey(getKey()); 217 updateCheckedState(true); 218 callChangeListener(mSharedState.getCurrentKey()); 219 } 220 221 /** 222 * Holds all state that is common to this group of radio buttons, such 223 * as the currently selected key and the currently checked compound button. 224 * (which corresponds to this key). 225 */ 226 public interface RadioButtonGroupState { 227 String getCurrentKey(); 228 Checkable getCurrentChecked(); 229 230 void setCurrentKey(String key); 231 void setCurrentChecked(Checkable current); 232 } 233} 234