blob: a842f349cbd3abfc43a551492a97a2d4dae0e72a [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
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.phone;
18
19import android.app.ActivityManager;
20import android.app.ActionBar;
21import android.app.AlertDialog;
22import android.app.Dialog;
23import android.content.ComponentName;
24import android.content.Context;
25import android.content.DialogInterface;
26import android.content.Intent;
27import android.content.SharedPreferences;
28import android.content.pm.ApplicationInfo;
29import android.content.pm.PackageInfo;
30import android.content.pm.PackageManager;
31import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
33import android.content.pm.ServiceInfo;
34import android.content.res.Resources;
35import android.graphics.drawable.Drawable;
36import android.net.Uri;
37import android.os.Bundle;
38import android.os.SystemProperties;
39import android.preference.EditTextPreference;
40import android.preference.Preference;
41import android.preference.PreferenceActivity;
42import android.telephony.PhoneNumberUtils;
43import android.telephony.TelephonyManager;
44import android.text.TextUtils;
45import android.util.Log;
46import android.view.LayoutInflater;
47import android.view.Menu;
48import android.view.MenuItem;
49import android.view.View;
50import android.view.ViewGroup;
51import android.widget.AdapterView;
52import android.widget.ArrayAdapter;
53import android.widget.BaseAdapter;
54import android.widget.CheckBox;
55import android.widget.CompoundButton;
56import android.widget.ImageView;
57import android.widget.ListView;
58import android.widget.TextView;
59import android.widget.Toast;
60
61import com.android.internal.telephony.Call;
62import com.android.internal.telephony.Connection;
63import com.android.internal.telephony.PhoneConstants;
Yorke Lee814da302013-08-30 16:01:07 -070064
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import com.google.android.collect.Lists;
66
67import java.util.ArrayList;
68import java.util.Arrays;
69import java.util.List;
70
71/**
72 * Helper class to manage the "Respond via Message" feature for incoming calls.
73 *
74 * @see InCallScreen.internalRespondViaSms()
75 */
76public 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 Cordon7d4ddf62013-07-10 11:58:08 -070083 /** 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 Cordon7d4ddf62013-07-10 11:58:08 -070099 * 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 Cordon7d4ddf62013-07-10 11:58:08 -0700195 private static void log(String msg) {
Jay Shrauner6fe8fd62013-09-16 19:39:30 -0700196 Log.e(TAG, msg);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700197 }
198}