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;
23a3eccfee788c3ac3c831a443b085b141b39bb63dBrad Ebingerimport android.telecom.Log;
24ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awadimport android.os.Bundle;
25ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awadimport android.preference.EditTextPreference;
26ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awadimport android.preference.Preference;
27ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awadimport android.preference.PreferenceActivity;
28477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Leeimport android.preference.PreferenceScreen;
29ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awadimport android.view.Menu;
30ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awadimport android.view.MenuItem;
31ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad
32af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee// TODO: This class is newly copied into Telecom (com.android.server.telecom) from it previous
33af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee// location in Telephony (com.android.phone). User's preferences stored in the old location
34af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee// will be lost. We need code here to migrate KLP -> LMP settings values.
3591d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn
36ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad/**
37af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee * Settings activity to manage the responses available for the "Respond via SMS Message" feature to
38af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee * respond to incoming calls.
39ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad */
40af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Leepublic class RespondViaSmsSettings extends PreferenceActivity
41af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        implements Preference.OnPreferenceChangeListener {
42477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee
43477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee    private SharedPreferences mPrefs;
44477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee
45af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee    @Override
46af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee    protected void onCreate(Bundle icicle) {
47af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        super.onCreate(icicle);
48af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        Log.d(this, "Settings: onCreate()...");
49af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee
50af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        // This function guarantees that QuickResponses will be in our
51af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        // SharedPreferences with the proper values considering there may be
52af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        // old QuickResponses in Telephony pre L.
53af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        QuickResponseUtils.maybeMigrateLegacyQuickResponses(this);
54af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee
55477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee        getPreferenceManager().setSharedPreferencesName(QuickResponseUtils.SHARED_PREFERENCES_NAME);
56477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee        mPrefs = getPreferenceManager().getSharedPreferences();
57477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee    }
58477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee
59477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee    @Override
60477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee    public void onResume() {
61477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee        super.onResume();
62477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee
63477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee        PreferenceScreen preferenceScreen = getPreferenceScreen();
64477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee        if (preferenceScreen != null) {
65477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee            preferenceScreen.removeAll();
66477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee        }
67af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee
68af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        // This preference screen is ultra-simple; it's just 4 plain
69af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        // <EditTextPreference>s, one for each of the 4 "canned responses".
70af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        //
71af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        // The only nontrivial thing we do here is copy the text value of
72af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        // each of those EditTextPreferences and use it as the preference's
73af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        // "title" as well, so that the user will immediately see all 4
74af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        // strings when they arrive here.
75af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        //
76af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        // Also, listen for change events (since we'll need to update the
77af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        // title any time the user edits one of the strings.)
78af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee
79af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        addPreferencesFromResource(R.xml.respond_via_sms_settings);
80477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee        initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_1));
81477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee        initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_2));
82477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee        initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_3));
83477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee        initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_4));
84af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee
85af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        ActionBar actionBar = getActionBar();
86af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        if (actionBar != null) {
87af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee            // android.R.id.home will be triggered in onOptionsItemSelected()
88af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee            actionBar.setDisplayHomeAsUpEnabled(true);
89ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad        }
90af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee    }
91ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad
92af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee    // Preference.OnPreferenceChangeListener implementation
93af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee    @Override
94af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee    public boolean onPreferenceChange(Preference preference, Object newValue) {
95af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        Log.d(this, "onPreferenceChange: key = %s", preference.getKey());
96af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        Log.d(this, "  preference = '%s'", preference);
97af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        Log.d(this, "  newValue = '%s'", newValue);
98ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad
99af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        EditTextPreference pref = (EditTextPreference) preference;
100ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad
101af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        // Copy the new text over to the title, just like in onCreate().
102af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        // (Watch out: onPreferenceChange() is called *before* the
103af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        // Preference itself gets updated, so we need to use newValue here
104af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        // rather than pref.getText().)
105af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        pref.setTitle((String) newValue);
106ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad
107477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee        // Save the new preference value.
108477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee        SharedPreferences.Editor editor = mPrefs.edit();
109477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee        editor.putString(pref.getKey(), (String) newValue).commit();
110477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee
111af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        return true;  // means it's OK to update the state of the Preference with the new value
112af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee    }
113ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad
114af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee    @Override
115af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee    public boolean onOptionsItemSelected(MenuItem item) {
116af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        final int itemId = item.getItemId();
117af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        switch (itemId) {
118af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee            case android.R.id.home:
119af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee                goUpToTopLevelSetting(this);
120af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee                return true;
121af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee            default:
122ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad        }
123af22dd1f9ab859c2139ffd623c52ab90c7895e04Andrew Lee        return super.onOptionsItemSelected(item);
124ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad    }
125ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad
126ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad    /**
127ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad     * Finish current Activity and go up to the top level Settings.
128ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad     */
129ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad    public static void goUpToTopLevelSetting(Activity activity) {
130ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad        activity.finish();
131477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee    }
132477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee
133477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee    /**
134477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee     * Initialize the preference to the persisted preference value or default text.
135477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee     */
136477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee    private void initPref(Preference preference) {
137477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee        EditTextPreference pref = (EditTextPreference) preference;
138477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee        pref.setText(mPrefs.getString(pref.getKey(), pref.getText()));
139477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee        pref.setTitle(pref.getText());
140477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee        pref.setOnPreferenceChangeListener(this);
141477ac8cdc4a5fb877f6d6a79e60142831f6deb8aAndrew Lee    }
142ff7493a8f620509d41dd8a5106c1d0dcd27cd274Ihab Awad}
143