blob: 2ea204ae516b7e6905ddc93554106560ae122858 [file] [log] [blame]
Ihab Awadff7493a2014-06-10 13:47:44 -07001/*
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 Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Ihab Awadff7493a2014-06-10 13:47:44 -070018
19import android.app.ActionBar;
20import android.app.Activity;
21import android.content.Context;
Ihab Awadff7493a2014-06-10 13:47:44 -070022import android.content.SharedPreferences;
Brad Ebingera3eccfe2016-10-05 15:45:22 -070023import android.telecom.Log;
Ihab Awadff7493a2014-06-10 13:47:44 -070024import android.os.Bundle;
25import android.preference.EditTextPreference;
26import android.preference.Preference;
27import android.preference.PreferenceActivity;
Andrew Lee477ac8c2015-06-11 10:40:49 -070028import android.preference.PreferenceScreen;
Ihab Awadff7493a2014-06-10 13:47:44 -070029import android.view.Menu;
30import android.view.MenuItem;
31
Andrew Leeaf22dd12015-02-25 17:40:11 -080032// 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 Gunn91d43cf2014-09-17 12:19:39 -070035
Ihab Awadff7493a2014-06-10 13:47:44 -070036/**
Andrew Leeaf22dd12015-02-25 17:40:11 -080037 * Settings activity to manage the responses available for the "Respond via SMS Message" feature to
38 * respond to incoming calls.
Ihab Awadff7493a2014-06-10 13:47:44 -070039 */
Andrew Leeaf22dd12015-02-25 17:40:11 -080040public class RespondViaSmsSettings extends PreferenceActivity
41 implements Preference.OnPreferenceChangeListener {
Andrew Lee477ac8c2015-06-11 10:40:49 -070042
43 private SharedPreferences mPrefs;
44
Andrew Leeaf22dd12015-02-25 17:40:11 -080045 @Override
46 protected void onCreate(Bundle icicle) {
47 super.onCreate(icicle);
48 Log.d(this, "Settings: onCreate()...");
Ihab Awadff7493a2014-06-10 13:47:44 -070049
Andrew Leeaf22dd12015-02-25 17:40:11 -080050 // 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 Awadff7493a2014-06-10 13:47:44 -070054
Andrew Lee477ac8c2015-06-11 10:40:49 -070055 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 Leec67cd752014-09-02 23:43:44 -070067
Andrew Leeaf22dd12015-02-25 17:40:11 -080068 // 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 Awadff7493a2014-06-10 13:47:44 -070078
Andrew Leeaf22dd12015-02-25 17:40:11 -080079 addPreferencesFromResource(R.xml.respond_via_sms_settings);
Andrew Lee477ac8c2015-06-11 10:40:49 -070080 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 Awadff7493a2014-06-10 13:47:44 -070084
Andrew Leeaf22dd12015-02-25 17:40:11 -080085 ActionBar actionBar = getActionBar();
86 if (actionBar != null) {
87 // android.R.id.home will be triggered in onOptionsItemSelected()
88 actionBar.setDisplayHomeAsUpEnabled(true);
Ihab Awadff7493a2014-06-10 13:47:44 -070089 }
Andrew Leeaf22dd12015-02-25 17:40:11 -080090 }
Ihab Awadff7493a2014-06-10 13:47:44 -070091
Andrew Leeaf22dd12015-02-25 17:40:11 -080092 // 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 Awadff7493a2014-06-10 13:47:44 -070098
Andrew Leeaf22dd12015-02-25 17:40:11 -080099 EditTextPreference pref = (EditTextPreference) preference;
Ihab Awadff7493a2014-06-10 13:47:44 -0700100
Andrew Leeaf22dd12015-02-25 17:40:11 -0800101 // 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 Awadff7493a2014-06-10 13:47:44 -0700106
Andrew Lee477ac8c2015-06-11 10:40:49 -0700107 // Save the new preference value.
108 SharedPreferences.Editor editor = mPrefs.edit();
109 editor.putString(pref.getKey(), (String) newValue).commit();
110
Andrew Leeaf22dd12015-02-25 17:40:11 -0800111 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 Awadff7493a2014-06-10 13:47:44 -0700122 }
Andrew Leeaf22dd12015-02-25 17:40:11 -0800123 return super.onOptionsItemSelected(item);
Ihab Awadff7493a2014-06-10 13:47:44 -0700124 }
125
126 /**
127 * Finish current Activity and go up to the top level Settings.
128 */
129 public static void goUpToTopLevelSetting(Activity activity) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700130 activity.finish();
Andrew Lee477ac8c2015-06-11 10:40:49 -0700131 }
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 Awadff7493a2014-06-10 13:47:44 -0700142}