1ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad/* 2ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad * Copyright (C) 2011 The Android Open Source Project 3ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad * 4ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad * Licensed under the Apache License, Version 2.0 (the "License"); 5ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad * you may not use this file except in compliance with the License. 6ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad * You may obtain a copy of the License at 7ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad * 8ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad * http://www.apache.org/licenses/LICENSE-2.0 9ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad * 10ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad * Unless required by applicable law or agreed to in writing, software 11ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad * distributed under the License is distributed on an "AS IS" BASIS, 12ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad * See the License for the specific language governing permissions and 14ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad * limitations under the License. 15ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad */ 16ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad 177cc70b4f0ad1064a4a0dce6056ad82b205887160Tyler Gunnpackage com.android.server.telecom; 18ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad 19ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awadimport android.app.ActionBar; 20ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awadimport android.app.Activity; 21ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awadimport android.content.Context; 22ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awadimport android.content.SharedPreferences; 23ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awadimport android.os.Bundle; 24ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awadimport android.preference.EditTextPreference; 25ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awadimport android.preference.Preference; 26ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awadimport android.preference.PreferenceActivity; 27477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Leeimport android.preference.PreferenceScreen; 28ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awadimport android.view.Menu; 29ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awadimport android.view.MenuItem; 30ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad 31af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee// TODO: This class is newly copied into Telecom (com.android.server.telecom) from it previous 32af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee// location in Telephony (com.android.phone). User's preferences stored in the old location 33af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee// will be lost. We need code here to migrate KLP -> LMP settings values. 3491d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn 35ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad/** 36af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee * Settings activity to manage the responses available for the "Respond via SMS Message" feature to 37af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee * respond to incoming calls. 38ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad */ 39af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Leepublic class RespondViaSmsSettings extends PreferenceActivity 40af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee implements Preference.OnPreferenceChangeListener { 41477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee 42477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee private SharedPreferences mPrefs; 43477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee 44af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee @Override 45af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee protected void onCreate(Bundle icicle) { 46af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee super.onCreate(icicle); 47af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee Log.d(this, "Settings: onCreate()..."); 48af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee 49af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // This function guarantees that QuickResponses will be in our 50af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // SharedPreferences with the proper values considering there may be 51af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // old QuickResponses in Telephony pre L. 52af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee QuickResponseUtils.maybeMigrateLegacyQuickResponses(this); 53af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee 54477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee getPreferenceManager().setSharedPreferencesName(QuickResponseUtils.SHARED_PREFERENCES_NAME); 55477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee mPrefs = getPreferenceManager().getSharedPreferences(); 56477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee } 57477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee 58477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee @Override 59477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee public void onResume() { 60477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee super.onResume(); 61477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee 62477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee PreferenceScreen preferenceScreen = getPreferenceScreen(); 63477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee if (preferenceScreen != null) { 64477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee preferenceScreen.removeAll(); 65477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee } 66af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee 67af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // This preference screen is ultra-simple; it's just 4 plain 68af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // <EditTextPreference>s, one for each of the 4 "canned responses". 69af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // 70af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // The only nontrivial thing we do here is copy the text value of 71af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // each of those EditTextPreferences and use it as the preference's 72af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // "title" as well, so that the user will immediately see all 4 73af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // strings when they arrive here. 74af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // 75af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // Also, listen for change events (since we'll need to update the 76af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // title any time the user edits one of the strings.) 77af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee 78af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee addPreferencesFromResource(R.xml.respond_via_sms_settings); 79477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_1)); 80477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_2)); 81477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_3)); 82477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_4)); 83af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee 84af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee ActionBar actionBar = getActionBar(); 85af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee if (actionBar != null) { 86af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // android.R.id.home will be triggered in onOptionsItemSelected() 87af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee actionBar.setDisplayHomeAsUpEnabled(true); 88ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad } 89af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee } 90ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad 91af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // Preference.OnPreferenceChangeListener implementation 92af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee @Override 93af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee public boolean onPreferenceChange(Preference preference, Object newValue) { 94af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee Log.d(this, "onPreferenceChange: key = %s", preference.getKey()); 95af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee Log.d(this, " preference = '%s'", preference); 96af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee Log.d(this, " newValue = '%s'", newValue); 97ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad 98af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee EditTextPreference pref = (EditTextPreference) preference; 99ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad 100af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // Copy the new text over to the title, just like in onCreate(). 101af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // (Watch out: onPreferenceChange() is called *before* the 102af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // Preference itself gets updated, so we need to use newValue here 103af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee // rather than pref.getText().) 104af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee pref.setTitle((String) newValue); 105ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad 106477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee // Save the new preference value. 107477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee SharedPreferences.Editor editor = mPrefs.edit(); 108477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee editor.putString(pref.getKey(), (String) newValue).commit(); 109477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee 110af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee return true; // means it's OK to update the state of the Preference with the new value 111af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee } 112ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad 113af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee @Override 114af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee public boolean onOptionsItemSelected(MenuItem item) { 115af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee final int itemId = item.getItemId(); 116af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee switch (itemId) { 117af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee case android.R.id.home: 118af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee goUpToTopLevelSetting(this); 119af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee return true; 120af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee default: 121ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad } 122af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee return super.onOptionsItemSelected(item); 123ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad } 124ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad 125ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad /** 126ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad * Finish current Activity and go up to the top level Settings. 127ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad */ 128ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad public static void goUpToTopLevelSetting(Activity activity) { 129ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad activity.finish(); 130477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee } 131477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee 132477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee /** 133477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee * Initialize the preference to the persisted preference value or default text. 134477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee */ 135477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee private void initPref(Preference preference) { 136477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee EditTextPreference pref = (EditTextPreference) preference; 137477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee pref.setText(mPrefs.getString(pref.getKey(), pref.getText())); 138477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee pref.setTitle(pref.getText()); 139477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee pref.setOnPreferenceChangeListener(this); 140477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee } 141ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad} 142