Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -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.phone; |
| 18 | |
| 19 | import android.app.ActivityManager; |
| 20 | import android.app.ActionBar; |
| 21 | import android.app.AlertDialog; |
| 22 | import android.app.Dialog; |
| 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.DialogInterface; |
| 26 | import android.content.Intent; |
| 27 | import android.content.SharedPreferences; |
| 28 | import android.content.pm.ApplicationInfo; |
| 29 | import android.content.pm.PackageInfo; |
| 30 | import android.content.pm.PackageManager; |
| 31 | import android.content.pm.PackageManager; |
| 32 | import android.content.pm.ResolveInfo; |
| 33 | import android.content.pm.ServiceInfo; |
| 34 | import android.content.res.Resources; |
| 35 | import android.graphics.drawable.Drawable; |
| 36 | import android.net.Uri; |
| 37 | import android.os.Bundle; |
| 38 | import android.os.SystemProperties; |
| 39 | import android.preference.EditTextPreference; |
| 40 | import android.preference.Preference; |
| 41 | import android.preference.PreferenceActivity; |
| 42 | import android.telephony.PhoneNumberUtils; |
| 43 | import android.telephony.TelephonyManager; |
| 44 | import android.text.TextUtils; |
| 45 | import android.util.Log; |
| 46 | import android.view.LayoutInflater; |
| 47 | import android.view.Menu; |
| 48 | import android.view.MenuItem; |
| 49 | import android.view.View; |
| 50 | import android.view.ViewGroup; |
| 51 | import android.widget.AdapterView; |
| 52 | import android.widget.ArrayAdapter; |
| 53 | import android.widget.BaseAdapter; |
| 54 | import android.widget.CheckBox; |
| 55 | import android.widget.CompoundButton; |
| 56 | import android.widget.ImageView; |
| 57 | import android.widget.ListView; |
| 58 | import android.widget.TextView; |
| 59 | import android.widget.Toast; |
| 60 | |
| 61 | import com.android.internal.telephony.Call; |
| 62 | import com.android.internal.telephony.Connection; |
| 63 | import com.android.internal.telephony.PhoneConstants; |
Yorke Lee | 814da30 | 2013-08-30 16:01:07 -0700 | [diff] [blame] | 64 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 65 | import com.google.android.collect.Lists; |
| 66 | |
| 67 | import java.util.ArrayList; |
| 68 | import java.util.Arrays; |
| 69 | import java.util.List; |
| 70 | |
| 71 | /** |
| 72 | * Helper class to manage the "Respond via Message" feature for incoming calls. |
| 73 | * |
| 74 | * @see InCallScreen.internalRespondViaSms() |
| 75 | */ |
| 76 | public class RespondViaSmsManager { |
| 77 | private static final String TAG = "RespondViaSmsManager"; |
| 78 | private static final boolean DBG = |
| 79 | (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1); |
| 80 | // Do not check in with VDBG = true, since that may write PII to the system log. |
| 81 | private static final boolean VDBG = false; |
| 82 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 83 | /** SharedPreferences file name for our persistent settings. */ |
| 84 | private static final String SHARED_PREFERENCES_NAME = "respond_via_sms_prefs"; |
| 85 | |
| 86 | // Preference keys for the 4 "canned responses"; see RespondViaSmsManager$Settings. |
| 87 | // Since (for now at least) the number of messages is fixed at 4, and since |
| 88 | // SharedPreferences can't deal with arrays anyway, just store the messages |
| 89 | // as 4 separate strings. |
| 90 | private static final int NUM_CANNED_RESPONSES = 4; |
| 91 | private static final String KEY_CANNED_RESPONSE_PREF_1 = "canned_response_pref_1"; |
| 92 | private static final String KEY_CANNED_RESPONSE_PREF_2 = "canned_response_pref_2"; |
| 93 | private static final String KEY_CANNED_RESPONSE_PREF_3 = "canned_response_pref_3"; |
| 94 | private static final String KEY_CANNED_RESPONSE_PREF_4 = "canned_response_pref_4"; |
| 95 | private static final String KEY_PREFERRED_PACKAGE = "preferred_package_pref"; |
| 96 | private static final String KEY_INSTANT_TEXT_DEFAULT_COMPONENT = "instant_text_def_component"; |
| 97 | |
| 98 | /** |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 99 | * Settings activity under "Call settings" to let you manage the |
| 100 | * canned responses; see respond_via_sms_settings.xml |
| 101 | */ |
| 102 | public static class Settings extends PreferenceActivity |
| 103 | implements Preference.OnPreferenceChangeListener { |
| 104 | @Override |
| 105 | protected void onCreate(Bundle icicle) { |
| 106 | super.onCreate(icicle); |
| 107 | if (DBG) log("Settings: onCreate()..."); |
| 108 | |
| 109 | getPreferenceManager().setSharedPreferencesName(SHARED_PREFERENCES_NAME); |
| 110 | |
| 111 | // This preference screen is ultra-simple; it's just 4 plain |
| 112 | // <EditTextPreference>s, one for each of the 4 "canned responses". |
| 113 | // |
| 114 | // The only nontrivial thing we do here is copy the text value of |
| 115 | // each of those EditTextPreferences and use it as the preference's |
| 116 | // "title" as well, so that the user will immediately see all 4 |
| 117 | // strings when they arrive here. |
| 118 | // |
| 119 | // Also, listen for change events (since we'll need to update the |
| 120 | // title any time the user edits one of the strings.) |
| 121 | |
| 122 | addPreferencesFromResource(R.xml.respond_via_sms_settings); |
| 123 | |
| 124 | EditTextPreference pref; |
| 125 | pref = (EditTextPreference) findPreference(KEY_CANNED_RESPONSE_PREF_1); |
| 126 | pref.setTitle(pref.getText()); |
| 127 | pref.setOnPreferenceChangeListener(this); |
| 128 | |
| 129 | pref = (EditTextPreference) findPreference(KEY_CANNED_RESPONSE_PREF_2); |
| 130 | pref.setTitle(pref.getText()); |
| 131 | pref.setOnPreferenceChangeListener(this); |
| 132 | |
| 133 | pref = (EditTextPreference) findPreference(KEY_CANNED_RESPONSE_PREF_3); |
| 134 | pref.setTitle(pref.getText()); |
| 135 | pref.setOnPreferenceChangeListener(this); |
| 136 | |
| 137 | pref = (EditTextPreference) findPreference(KEY_CANNED_RESPONSE_PREF_4); |
| 138 | pref.setTitle(pref.getText()); |
| 139 | pref.setOnPreferenceChangeListener(this); |
| 140 | |
| 141 | ActionBar actionBar = getActionBar(); |
| 142 | if (actionBar != null) { |
| 143 | // android.R.id.home will be triggered in onOptionsItemSelected() |
| 144 | actionBar.setDisplayHomeAsUpEnabled(true); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // Preference.OnPreferenceChangeListener implementation |
| 149 | @Override |
| 150 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 151 | if (DBG) log("onPreferenceChange: key = " + preference.getKey()); |
| 152 | if (VDBG) log(" preference = '" + preference + "'"); |
| 153 | if (VDBG) log(" newValue = '" + newValue + "'"); |
| 154 | |
| 155 | EditTextPreference pref = (EditTextPreference) preference; |
| 156 | |
| 157 | // Copy the new text over to the title, just like in onCreate(). |
| 158 | // (Watch out: onPreferenceChange() is called *before* the |
| 159 | // Preference itself gets updated, so we need to use newValue here |
| 160 | // rather than pref.getText().) |
| 161 | pref.setTitle((String) newValue); |
| 162 | |
| 163 | return true; // means it's OK to update the state of the Preference with the new value |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public boolean onOptionsItemSelected(MenuItem item) { |
| 168 | final int itemId = item.getItemId(); |
| 169 | switch (itemId) { |
| 170 | case android.R.id.home: |
| 171 | // See ActionBar#setDisplayHomeAsUpEnabled() |
| 172 | CallFeaturesSetting.goUpToTopLevelSetting(this); |
| 173 | return true; |
| 174 | case R.id.respond_via_message_reset: |
| 175 | // Reset the preferences settings |
| 176 | SharedPreferences prefs = getSharedPreferences( |
| 177 | SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); |
| 178 | SharedPreferences.Editor editor = prefs.edit(); |
| 179 | editor.remove(KEY_INSTANT_TEXT_DEFAULT_COMPONENT); |
| 180 | editor.apply(); |
| 181 | |
| 182 | return true; |
| 183 | default: |
| 184 | } |
| 185 | return super.onOptionsItemSelected(item); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public boolean onCreateOptionsMenu(Menu menu) { |
| 190 | getMenuInflater().inflate(R.menu.respond_via_message_settings_menu, menu); |
| 191 | return super.onCreateOptionsMenu(menu); |
| 192 | } |
| 193 | } |
| 194 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 195 | private static void log(String msg) { |
Jay Shrauner | 6fe8fd6 | 2013-09-16 19:39:30 -0700 | [diff] [blame] | 196 | Log.e(TAG, msg); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 197 | } |
| 198 | } |