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.content.Context; 22 import android.content.SharedPreferences; 23 import android.telecom.Log; 24 import android.os.Bundle; 25 import android.preference.EditTextPreference; 26 import android.preference.Preference; 27 import android.preference.PreferenceActivity; 28 import android.preference.PreferenceScreen; 29 import android.view.Menu; 30 import android.view.MenuItem; 31 32 // TODO: This class is newly copied into Telecom (com.android.server.telecom) from it previous 33 // location in Telephony (com.android.phone). User's preferences stored in the old location 34 // will be lost. We need code here to migrate KLP -> LMP settings values. 35 36 /** 37 * Settings activity to manage the responses available for the "Respond via SMS Message" feature to 38 * respond to incoming calls. 39 */ 40 public class RespondViaSmsSettings extends PreferenceActivity 41 implements Preference.OnPreferenceChangeListener { 42 43 private SharedPreferences mPrefs; 44 45 @Override onCreate(Bundle icicle)46 protected void onCreate(Bundle icicle) { 47 super.onCreate(icicle); 48 Log.d(this, "Settings: onCreate()..."); 49 50 // This function guarantees that QuickResponses will be in our 51 // SharedPreferences with the proper values considering there may be 52 // old QuickResponses in Telephony pre L. 53 QuickResponseUtils.maybeMigrateLegacyQuickResponses(this); 54 55 getPreferenceManager().setSharedPreferencesName(QuickResponseUtils.SHARED_PREFERENCES_NAME); 56 mPrefs = getPreferenceManager().getSharedPreferences(); 57 QuickResponseUtils.maybeResetQuickResponses(this, mPrefs); 58 } 59 60 @Override onResume()61 public void onResume() { 62 super.onResume(); 63 64 PreferenceScreen preferenceScreen = getPreferenceScreen(); 65 if (preferenceScreen != null) { 66 preferenceScreen.removeAll(); 67 } 68 69 // This preference screen is ultra-simple; it's just 4 plain 70 // <EditTextPreference>s, one for each of the 4 "canned responses". 71 // 72 // The only nontrivial thing we do here is copy the text value of 73 // each of those EditTextPreferences and use it as the preference's 74 // "title" as well, so that the user will immediately see all 4 75 // strings when they arrive here. 76 // 77 // Also, listen for change events (since we'll need to update the 78 // title any time the user edits one of the strings.) 79 80 addPreferencesFromResource(R.xml.respond_via_sms_settings); 81 initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_1)); 82 initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_2)); 83 initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_3)); 84 initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_4)); 85 86 ActionBar actionBar = getActionBar(); 87 if (actionBar != null) { 88 // android.R.id.home will be triggered in onOptionsItemSelected() 89 actionBar.setDisplayHomeAsUpEnabled(true); 90 } 91 } 92 93 // Preference.OnPreferenceChangeListener implementation 94 @Override onPreferenceChange(Preference preference, Object newValue)95 public boolean onPreferenceChange(Preference preference, Object newValue) { 96 Log.d(this, "onPreferenceChange: key = %s", preference.getKey()); 97 Log.d(this, " preference = '%s'", preference); 98 Log.d(this, " newValue = '%s'", newValue); 99 100 EditTextPreference pref = (EditTextPreference) preference; 101 102 // Copy the new text over to the title, just like in onCreate(). 103 // (Watch out: onPreferenceChange() is called *before* the 104 // Preference itself gets updated, so we need to use newValue here 105 // rather than pref.getText().) 106 pref.setTitle((String) newValue); 107 108 // Save the new preference value. 109 SharedPreferences.Editor editor = mPrefs.edit(); 110 editor.putString(pref.getKey(), (String) newValue).commit(); 111 112 // If the user just reset the quick response to its original text, clear the pref. 113 QuickResponseUtils.maybeResetQuickResponses(this, mPrefs); 114 115 return true; // means it's OK to update the state of the Preference with the new value 116 } 117 118 @Override onOptionsItemSelected(MenuItem item)119 public boolean onOptionsItemSelected(MenuItem item) { 120 final int itemId = item.getItemId(); 121 switch (itemId) { 122 case android.R.id.home: 123 goUpToTopLevelSetting(this); 124 return true; 125 default: 126 } 127 return super.onOptionsItemSelected(item); 128 } 129 130 /** 131 * Finish current Activity and go up to the top level Settings. 132 */ goUpToTopLevelSetting(Activity activity)133 public static void goUpToTopLevelSetting(Activity activity) { 134 activity.finish(); 135 } 136 137 /** 138 * Initialize the preference to the persisted preference value or default text. 139 */ initPref(Preference preference)140 private void initPref(Preference preference) { 141 EditTextPreference pref = (EditTextPreference) preference; 142 pref.setText(mPrefs.getString(pref.getKey(), pref.getText())); 143 pref.setTitle(pref.getText()); 144 pref.setOnPreferenceChangeListener(this); 145 } 146 } 147