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