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