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