Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 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 | |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 17 | package com.android.server.telecom; |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 18 | |
| 19 | import android.app.ActionBar; |
| 20 | import android.app.Activity; |
| 21 | import android.content.Context; |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 22 | import android.content.SharedPreferences; |
Brad Ebinger | a3eccfe | 2016-10-05 15:45:22 -0700 | [diff] [blame] | 23 | import android.telecom.Log; |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 24 | import android.os.Bundle; |
| 25 | import android.preference.EditTextPreference; |
| 26 | import android.preference.Preference; |
| 27 | import android.preference.PreferenceActivity; |
Andrew Lee | 477ac8c | 2015-06-11 10:40:49 -0700 | [diff] [blame] | 28 | import android.preference.PreferenceScreen; |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 29 | import android.view.Menu; |
| 30 | import android.view.MenuItem; |
| 31 | |
Andrew Lee | af22dd1 | 2015-02-25 17:40:11 -0800 | [diff] [blame] | 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. |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 35 | |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 36 | /** |
Andrew Lee | af22dd1 | 2015-02-25 17:40:11 -0800 | [diff] [blame] | 37 | * Settings activity to manage the responses available for the "Respond via SMS Message" feature to |
| 38 | * respond to incoming calls. |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 39 | */ |
Andrew Lee | af22dd1 | 2015-02-25 17:40:11 -0800 | [diff] [blame] | 40 | public class RespondViaSmsSettings extends PreferenceActivity |
| 41 | implements Preference.OnPreferenceChangeListener { |
Andrew Lee | 477ac8c | 2015-06-11 10:40:49 -0700 | [diff] [blame] | 42 | |
| 43 | private SharedPreferences mPrefs; |
| 44 | |
Andrew Lee | af22dd1 | 2015-02-25 17:40:11 -0800 | [diff] [blame] | 45 | @Override |
| 46 | protected void onCreate(Bundle icicle) { |
| 47 | super.onCreate(icicle); |
| 48 | Log.d(this, "Settings: onCreate()..."); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 49 | |
Andrew Lee | af22dd1 | 2015-02-25 17:40:11 -0800 | [diff] [blame] | 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); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 54 | |
Andrew Lee | 477ac8c | 2015-06-11 10:40:49 -0700 | [diff] [blame] | 55 | getPreferenceManager().setSharedPreferencesName(QuickResponseUtils.SHARED_PREFERENCES_NAME); |
| 56 | mPrefs = getPreferenceManager().getSharedPreferences(); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void onResume() { |
| 61 | super.onResume(); |
| 62 | |
| 63 | PreferenceScreen preferenceScreen = getPreferenceScreen(); |
| 64 | if (preferenceScreen != null) { |
| 65 | preferenceScreen.removeAll(); |
| 66 | } |
Anthony Lee | c67cd75 | 2014-09-02 23:43:44 -0700 | [diff] [blame] | 67 | |
Andrew Lee | af22dd1 | 2015-02-25 17:40:11 -0800 | [diff] [blame] | 68 | // This preference screen is ultra-simple; it's just 4 plain |
| 69 | // <EditTextPreference>s, one for each of the 4 "canned responses". |
| 70 | // |
| 71 | // The only nontrivial thing we do here is copy the text value of |
| 72 | // each of those EditTextPreferences and use it as the preference's |
| 73 | // "title" as well, so that the user will immediately see all 4 |
| 74 | // strings when they arrive here. |
| 75 | // |
| 76 | // Also, listen for change events (since we'll need to update the |
| 77 | // title any time the user edits one of the strings.) |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 78 | |
Andrew Lee | af22dd1 | 2015-02-25 17:40:11 -0800 | [diff] [blame] | 79 | addPreferencesFromResource(R.xml.respond_via_sms_settings); |
Andrew Lee | 477ac8c | 2015-06-11 10:40:49 -0700 | [diff] [blame] | 80 | initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_1)); |
| 81 | initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_2)); |
| 82 | initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_3)); |
| 83 | initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_4)); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 84 | |
Andrew Lee | af22dd1 | 2015-02-25 17:40:11 -0800 | [diff] [blame] | 85 | ActionBar actionBar = getActionBar(); |
| 86 | if (actionBar != null) { |
| 87 | // android.R.id.home will be triggered in onOptionsItemSelected() |
| 88 | actionBar.setDisplayHomeAsUpEnabled(true); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 89 | } |
Andrew Lee | af22dd1 | 2015-02-25 17:40:11 -0800 | [diff] [blame] | 90 | } |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 91 | |
Andrew Lee | af22dd1 | 2015-02-25 17:40:11 -0800 | [diff] [blame] | 92 | // Preference.OnPreferenceChangeListener implementation |
| 93 | @Override |
| 94 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 95 | Log.d(this, "onPreferenceChange: key = %s", preference.getKey()); |
| 96 | Log.d(this, " preference = '%s'", preference); |
| 97 | Log.d(this, " newValue = '%s'", newValue); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 98 | |
Andrew Lee | af22dd1 | 2015-02-25 17:40:11 -0800 | [diff] [blame] | 99 | EditTextPreference pref = (EditTextPreference) preference; |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 100 | |
Andrew Lee | af22dd1 | 2015-02-25 17:40:11 -0800 | [diff] [blame] | 101 | // Copy the new text over to the title, just like in onCreate(). |
| 102 | // (Watch out: onPreferenceChange() is called *before* the |
| 103 | // Preference itself gets updated, so we need to use newValue here |
| 104 | // rather than pref.getText().) |
| 105 | pref.setTitle((String) newValue); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 106 | |
Andrew Lee | 477ac8c | 2015-06-11 10:40:49 -0700 | [diff] [blame] | 107 | // Save the new preference value. |
| 108 | SharedPreferences.Editor editor = mPrefs.edit(); |
| 109 | editor.putString(pref.getKey(), (String) newValue).commit(); |
| 110 | |
Andrew Lee | af22dd1 | 2015-02-25 17:40:11 -0800 | [diff] [blame] | 111 | return true; // means it's OK to update the state of the Preference with the new value |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public boolean onOptionsItemSelected(MenuItem item) { |
| 116 | final int itemId = item.getItemId(); |
| 117 | switch (itemId) { |
| 118 | case android.R.id.home: |
| 119 | goUpToTopLevelSetting(this); |
| 120 | return true; |
| 121 | default: |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 122 | } |
Andrew Lee | af22dd1 | 2015-02-25 17:40:11 -0800 | [diff] [blame] | 123 | return super.onOptionsItemSelected(item); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Finish current Activity and go up to the top level Settings. |
| 128 | */ |
| 129 | public static void goUpToTopLevelSetting(Activity activity) { |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 130 | activity.finish(); |
Andrew Lee | 477ac8c | 2015-06-11 10:40:49 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Initialize the preference to the persisted preference value or default text. |
| 135 | */ |
| 136 | private void initPref(Preference preference) { |
| 137 | EditTextPreference pref = (EditTextPreference) preference; |
| 138 | pref.setText(mPrefs.getString(pref.getKey(), pref.getText())); |
| 139 | pref.setTitle(pref.getText()); |
| 140 | pref.setOnPreferenceChangeListener(this); |
| 141 | } |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 142 | } |