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