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