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 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 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 } 93 } 94 95 // Preference.OnPreferenceChangeListener implementation 96 @Override 97 public boolean onPreferenceChange(Preference preference, Object newValue) { 98 Log.d(this, "onPreferenceChange: key = %s", preference.getKey()); 99 Log.d(this, " preference = '%s'", preference); 100 Log.d(this, " newValue = '%s'", newValue); 101 102 EditTextPreference pref = (EditTextPreference) preference; 103 104 // Copy the new text over to the title, just like in onCreate(). 105 // (Watch out: onPreferenceChange() is called *before* the 106 // Preference itself gets updated, so we need to use newValue here 107 // rather than pref.getText().) 108 // If the newValue is an empty string, skip this to avoid setting an empty response. 109 // TODO: Show a popup to inform user that response didn't set because it's empty. 110 if (((String) newValue).length() != 0) { 111 pref.setTitle((String) newValue); 112 113 // Save the new preference value. 114 SharedPreferences.Editor editor = mPrefs.edit(); 115 editor.putString(pref.getKey(), (String) newValue).commit(); 116 } 117 118 // If the user just reset the quick response to its original text, clear the pref. 119 QuickResponseUtils.maybeResetQuickResponses(this, mPrefs); 120 121 return true; // means it's OK to update the state of the Preference with the new value 122 } 123 124 @Override 125 public boolean onOptionsItemSelected(MenuItem item) { 126 final int itemId = item.getItemId(); 127 switch (itemId) { 128 case android.R.id.home: 129 goUpToTopLevelSetting(this); 130 return true; 131 default: 132 } 133 return super.onOptionsItemSelected(item); 134 } 135 136 /** 137 * Finish current Activity and go up to the top level Settings. 138 */ 139 public static void goUpToTopLevelSetting(Activity activity) { 140 activity.finish(); 141 } 142 143 /** 144 * Initialize the preference to the persisted preference value or default text. 145 */ 146 private void initPref(Preference preference) { 147 EditTextPreference pref = (EditTextPreference) preference; 148 pref.setText(mPrefs.getString(pref.getKey(), pref.getText())); 149 pref.setTitle(pref.getText()); 150 pref.getEditText().addTextChangedListener(new TextWatcher() { 151 152 @Override 153 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 154 155 } 156 157 @Override 158 public void onTextChanged(CharSequence s, int start, int before, int count) { 159 160 } 161 162 @Override 163 public void afterTextChanged(Editable s) { 164 try { 165 Button button = ((AlertDialog) pref.getDialog()) 166 .getButton(AlertDialog.BUTTON_POSITIVE); 167 if (s.toString().length() == 0) { 168 button.setEnabled(false); 169 } else { 170 button.setEnabled(true); 171 } 172 } catch (NullPointerException e) { 173 Log.d(this, e.toString()); 174 } 175 } 176 }); 177 pref.setOnPreferenceChangeListener(this); 178 } 179 } 180