1 /* 2 * Copyright (C) 2011 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.server.telecom; 18 19 import android.app.ActionBar; 20 import android.app.Activity; 21 import android.app.AlertDialog; 22 import android.content.SharedPreferences; 23 import android.os.Bundle; 24 import android.preference.EditTextPreference; 25 import android.preference.Preference; 26 import android.preference.PreferenceActivity; 27 import android.preference.PreferenceScreen; 28 import android.telecom.Log; 29 import android.text.Editable; 30 import android.text.TextWatcher; 31 import android.view.MenuItem; 32 import android.widget.Button; 33 34 // TODO: This class is newly copied into Telecom (com.android.server.telecom) from it previous 35 // location in Telephony (com.android.phone). User's preferences stored in the old location 36 // will be lost. We need code here to migrate KLP -> LMP settings values. 37 38 /** 39 * Settings activity to manage the responses available for the "Respond via SMS Message" feature to 40 * respond to incoming calls. 41 */ 42 public class RespondViaSmsSettings extends PreferenceActivity 43 implements Preference.OnPreferenceChangeListener { 44 45 private SharedPreferences mPrefs; 46 47 @Override onCreate(Bundle icicle)48 protected void onCreate(Bundle icicle) { 49 super.onCreate(icicle); 50 Log.d(this, "Settings: onCreate()..."); 51 52 // This function guarantees that QuickResponses will be in our 53 // SharedPreferences with the proper values considering there may be 54 // old QuickResponses in Telephony pre L. 55 QuickResponseUtils.maybeMigrateLegacyQuickResponses(this); 56 57 getPreferenceManager().setSharedPreferencesName(QuickResponseUtils.SHARED_PREFERENCES_NAME); 58 mPrefs = getPreferenceManager().getSharedPreferences(); 59 QuickResponseUtils.maybeResetQuickResponses(this, mPrefs); 60 } 61 62 @Override onResume()63 public void onResume() { 64 super.onResume(); 65 66 PreferenceScreen preferenceScreen = getPreferenceScreen(); 67 if (preferenceScreen != null) { 68 preferenceScreen.removeAll(); 69 } 70 71 // This preference screen is ultra-simple; it's just 4 plain 72 // <EditTextPreference>s, one for each of the 4 "canned responses". 73 // 74 // The only nontrivial thing we do here is copy the text value of 75 // each of those EditTextPreferences and use it as the preference's 76 // "title" as well, so that the user will immediately see all 4 77 // strings when they arrive here. 78 // 79 // Also, listen for change events (since we'll need to update the 80 // title any time the user edits one of the strings.) 81 82 addPreferencesFromResource(R.xml.respond_via_sms_settings); 83 initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_1)); 84 initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_2)); 85 initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_3)); 86 initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_4)); 87 88 ActionBar actionBar = getActionBar(); 89 if (actionBar != null) { 90 // android.R.id.home will be triggered in onOptionsItemSelected() 91 actionBar.setDisplayHomeAsUpEnabled(true); 92 // set the talkback voice prompt to "Back" instead of "Navigate Up" 93 actionBar.setHomeActionContentDescription(R.string.back); 94 } 95 } 96 97 // Preference.OnPreferenceChangeListener implementation 98 @Override onPreferenceChange(Preference preference, Object newValue)99 public boolean onPreferenceChange(Preference preference, Object newValue) { 100 Log.d(this, "onPreferenceChange: key = %s", preference.getKey()); 101 Log.d(this, " preference = '%s'", preference); 102 Log.d(this, " newValue = '%s'", newValue); 103 104 EditTextPreference pref = (EditTextPreference)findPreference(preference.getKey()); 105 106 // Copy the new text over to the title, just like in onCreate(). 107 // (Watch out: onPreferenceChange() is called *before* the 108 // Preference itself gets updated, so we need to use newValue here 109 // rather than pref.getText().) 110 // If the newValue is an empty string, skip this to avoid setting an empty response. 111 // TODO: Show a popup to inform user that response didn't set because it's empty. 112 if (((String) newValue).length() != 0) { 113 pref.setTitle((String) newValue); 114 115 // Save the new preference value. 116 SharedPreferences.Editor editor = mPrefs.edit(); 117 editor.putString(pref.getKey(), (String) newValue).commit(); 118 } 119 120 // If the user just reset the quick response to its original text, clear the pref. 121 QuickResponseUtils.maybeResetQuickResponses(this, mPrefs); 122 123 return true; // means it's OK to update the state of the Preference with the new value 124 } 125 126 @Override onOptionsItemSelected(MenuItem item)127 public boolean onOptionsItemSelected(MenuItem item) { 128 final int itemId = item.getItemId(); 129 switch (itemId) { 130 case android.R.id.home: 131 goUpToTopLevelSetting(this); 132 return true; 133 default: 134 } 135 return super.onOptionsItemSelected(item); 136 } 137 138 /** 139 * Finish current Activity and go up to the top level Settings. 140 */ goUpToTopLevelSetting(Activity activity)141 public static void goUpToTopLevelSetting(Activity activity) { 142 activity.finish(); 143 } 144 145 /** 146 * Initialize the preference to the persisted preference value or default text. 147 */ initPref(Preference preference)148 private void initPref(Preference preference) { 149 EditTextPreference pref = (EditTextPreference) preference; 150 pref.setText(mPrefs.getString(pref.getKey(), pref.getText())); 151 pref.setTitle(pref.getText()); 152 pref.getEditText().addTextChangedListener(new TextWatcher() { 153 154 @Override 155 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 156 157 } 158 159 @Override 160 public void onTextChanged(CharSequence s, int start, int before, int count) { 161 162 } 163 164 @Override 165 public void afterTextChanged(Editable s) { 166 try { 167 Button button = ((AlertDialog) pref.getDialog()) 168 .getButton(AlertDialog.BUTTON_POSITIVE); 169 if (s.toString().length() == 0) { 170 button.setEnabled(false); 171 } else { 172 button.setEnabled(true); 173 } 174 } catch (NullPointerException e) { 175 Log.d(this, e.toString()); 176 } 177 } 178 }); 179 pref.setOnPreferenceChangeListener(this); 180 } 181 } 182