blob: f44528dac4d13dea01a327e9f38fe9be779bcbcd [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 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.ActionBar;
20import android.app.Activity;
Evan Charlton1c696832014-04-15 14:24:23 -070021import android.app.ActivityOptions;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.AlertDialog;
23import android.app.Dialog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.ContentResolver;
25import android.content.Context;
26import android.content.DialogInterface;
27import android.content.Intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.content.pm.ActivityInfo;
29import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
31import android.database.Cursor;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070032import android.media.AudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070033import android.os.AsyncResult;
34import android.os.Bundle;
35import android.os.Handler;
36import android.os.Message;
37import android.os.UserHandle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.preference.CheckBoxPreference;
39import android.preference.ListPreference;
40import android.preference.Preference;
41import android.preference.PreferenceActivity;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.preference.PreferenceScreen;
43import android.provider.ContactsContract.CommonDataKinds;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070045import android.telecom.PhoneAccountHandle;
46import android.telecom.TelecomManager;
Andrew Lee93c345f2014-10-27 15:25:07 -070047import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.text.TextUtils;
49import android.util.Log;
50import android.view.MenuItem;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.widget.ListAdapter;
Jay Shraunerc2684732014-11-12 12:10:37 -080052import android.widget.Toast;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053
Andrew Lee312e8172014-10-23 17:01:36 -070054import com.android.ims.ImsManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import com.android.internal.telephony.CallForwardInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import com.android.internal.telephony.Phone;
57import com.android.internal.telephony.PhoneConstants;
Andrew Lee2170a972014-08-13 18:13:01 -070058import com.android.phone.common.util.SettingsUtil;
Andrew Leedb2fe562014-09-03 15:40:43 -070059import com.android.phone.settings.AccountSelectionPreference;
Andrew Lee1af6cf72014-11-04 17:35:26 -080060import com.android.phone.settings.CallForwardInfoUtil;
Andrew Lee84024342014-11-06 23:37:09 -080061import com.android.phone.settings.VoicemailDialogUtil;
Andrew Leeb490d732014-10-27 15:00:41 -070062import com.android.phone.settings.VoicemailProviderSettings;
Andrew Lee88b51e22014-10-29 15:48:51 -070063import com.android.phone.settings.VoicemailProviderSettingsUtil;
Andrew Lee8d66d812014-11-24 14:54:02 -080064import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Andrew Lee5ed870c2014-10-29 11:47:49 -070065import com.android.phone.settings.fdn.FdnSetting;
Sailesh Nepal788959e2014-07-08 23:36:40 -070066import com.android.services.telephony.sip.SipUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067
Andrew Lee2170a972014-08-13 18:13:01 -070068import java.lang.String;
Andrew Leef1776d82014-11-04 14:45:02 -080069import java.util.ArrayList;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import java.util.Collection;
71import java.util.HashMap;
72import java.util.HashSet;
73import java.util.Iterator;
74import java.util.List;
75import java.util.Map;
76
77/**
78 * Top level "Call settings" UI; see res/xml/call_feature_setting.xml
79 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070080 * This preference screen is the root of the "Call settings" hierarchy available from the Phone
81 * app; the settings here let you control various features related to phone calls (including
82 * voicemail settings, the "Respond via SMS" feature, and others.) It's used only on
83 * voice-capable phone devices.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084 *
85 * Note that this activity is part of the package com.android.phone, even
86 * though you reach it from the "Phone" app (i.e. DialtactsActivity) which
87 * is from the package com.android.contacts.
88 *
89 * For the "Mobile network settings" screen under the main Settings app,
90 * See {@link MobileNetworkSettings}.
91 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070092 * TODO: Settings should be split into PreferenceFragments where possible (ie. voicemail).
Andrew Lee2170a972014-08-13 18:13:01 -070093 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094 * @see com.android.phone.MobileNetworkSettings
95 */
96public class CallFeaturesSetting extends PreferenceActivity
97 implements DialogInterface.OnClickListener,
Andrew Lee2170a972014-08-13 18:13:01 -070098 Preference.OnPreferenceChangeListener,
Andrew Lee2170a972014-08-13 18:13:01 -070099 EditPhoneNumberPreference.OnDialogClosedListener,
Andrew Leece8ae2a2014-09-10 10:41:48 -0700100 EditPhoneNumberPreference.GetDefaultNumberListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700101 private static final String LOG_TAG = "CallFeaturesSetting";
102 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
Andrew Lee77527ac2014-10-21 16:57:39 -0700103 // STOPSHIP if true. Flag to override behavior default behavior to hide VT setting.
104 private static final boolean ENABLE_VT_FLAG = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700105
106 /**
107 * Intent action to bring up Voicemail Provider settings.
108 *
109 * @see #IGNORE_PROVIDER_EXTRA
110 */
111 public static final String ACTION_ADD_VOICEMAIL =
112 "com.android.phone.CallFeaturesSetting.ADD_VOICEMAIL";
113 // intent action sent by this activity to a voice mail provider
114 // to trigger its configuration UI
115 public static final String ACTION_CONFIGURE_VOICEMAIL =
116 "com.android.phone.CallFeaturesSetting.CONFIGURE_VOICEMAIL";
117 // Extra put in the return from VM provider config containing voicemail number to set
118 public static final String VM_NUMBER_EXTRA = "com.android.phone.VoicemailNumber";
119 // Extra put in the return from VM provider config containing call forwarding number to set
120 public static final String FWD_NUMBER_EXTRA = "com.android.phone.ForwardingNumber";
121 // Extra put in the return from VM provider config containing call forwarding number to set
122 public static final String FWD_NUMBER_TIME_EXTRA = "com.android.phone.ForwardingNumberTime";
123 // If the VM provider returns non null value in this extra we will force the user to
124 // choose another VM provider
125 public static final String SIGNOUT_EXTRA = "com.android.phone.Signout";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700126
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127 // Key identifying the default vocie mail provider
128 public static final String DEFAULT_VM_PROVIDER_KEY = "";
129
130 /**
131 * String Extra put into ACTION_ADD_VOICEMAIL call to indicate which provider should be hidden
132 * in the list of providers presented to the user. This allows a provider which is being
133 * disabled (e.g. GV user logging out) to force the user to pick some other provider.
134 */
135 public static final String IGNORE_PROVIDER_EXTRA = "com.android.phone.ProviderToIgnore";
136
137 // string constants
138 private static final String NUM_PROJECTION[] = {CommonDataKinds.Phone.NUMBER};
139
140 // String keys for preference lookup
141 // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
Andrew Lee8d66d812014-11-24 14:54:02 -0800142 // TODO: Consider moving these strings to strings.xml, so that they are not duplicated here and
143 // in the layout files. These strings need to be treated carefully; if the setting is
144 // persistent, they are used as the key to store shared preferences and the name should not be
145 // changed unless the settings are also migrated.
Andrew Lee97708a42014-09-25 12:39:07 -0700146 private static final String VOICEMAIL_SETTING_SCREEN_PREF_KEY = "button_voicemail_category_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700147 private static final String BUTTON_VOICEMAIL_KEY = "button_voicemail_key";
148 private static final String BUTTON_VOICEMAIL_PROVIDER_KEY = "button_voicemail_provider_key";
149 private static final String BUTTON_VOICEMAIL_SETTING_KEY = "button_voicemail_setting_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150 private static final String BUTTON_FDN_KEY = "button_fdn_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700151
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700152 private static final String BUTTON_DTMF_KEY = "button_dtmf_settings";
153 private static final String BUTTON_RETRY_KEY = "button_auto_retry_key";
154 private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
155 private static final String BUTTON_HAC_KEY = "button_hac_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700156
157 private static final String BUTTON_GSM_UMTS_OPTIONS = "button_gsm_more_expand_key";
158 private static final String BUTTON_CDMA_OPTIONS = "button_cdma_more_expand_key";
Andrew Lee2b36ba22014-11-05 17:08:49 -0800159 private static final String CALL_FORWARDING_KEY = "call_forwarding_key";
160 private static final String ADDITIONAL_GSM_SETTINGS_KEY = "additional_gsm_call_settings_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700161
Andrew Leece8ae2a2014-09-10 10:41:48 -0700162 private static final String PHONE_ACCOUNT_SETTINGS_KEY =
163 "phone_account_settings_preference_screen";
Andrew Leedb2fe562014-09-03 15:40:43 -0700164
Andrew Leedf14ead2014-10-17 14:22:52 -0700165 private static final String ENABLE_VIDEO_CALLING_KEY = "button_enable_video_calling";
166
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700167 /** Event for Async voicemail change call */
168 private static final int EVENT_VOICEMAIL_CHANGED = 500;
169 private static final int EVENT_FORWARDING_CHANGED = 501;
170 private static final int EVENT_FORWARDING_GET_COMPLETED = 502;
171
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700172 public static final String HAC_KEY = "HACSetting";
173 public static final String HAC_VAL_ON = "ON";
174 public static final String HAC_VAL_OFF = "OFF";
175
176 /** Handle to voicemail pref */
177 private static final int VOICEMAIL_PREF_ID = 1;
178 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
179
180 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700181 private AudioManager mAudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700182
Andrew Lee5ed870c2014-10-29 11:47:49 -0700183 private SubscriptionInfoHelper mSubscriptionInfoHelper;
184
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
186
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700187 /** Whether dialpad plays DTMF tone or not. */
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188 private CheckBoxPreference mButtonAutoRetry;
189 private CheckBoxPreference mButtonHAC;
190 private ListPreference mButtonDTMF;
191 private ListPreference mButtonTTY;
Andrew Leece8ae2a2014-09-10 10:41:48 -0700192 private Preference mPhoneAccountSettingsPreference;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700193 private ListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700194 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700195 private PreferenceScreen mVoicemailSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700196 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700197 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198
199 private class VoiceMailProvider {
Andrew Leef1776d82014-11-04 14:45:02 -0800200 public String name;
201 public Intent intent;
202
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700203 public VoiceMailProvider(String name, Intent intent) {
204 this.name = name;
205 this.intent = intent;
206 }
Andrew Leef1776d82014-11-04 14:45:02 -0800207
208 public String toString() {
209 return "[ Name: " + name + ", Intent: " + intent + " ]";
210 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700211 }
212
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700213 /**
214 * Results of reading forwarding settings
215 */
216 private CallForwardInfo[] mForwardingReadResults = null;
217
218 /**
219 * Result of forwarding number change.
220 * Keys are reasons (eg. unconditional forwarding).
221 */
222 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
223
224 /**
225 * Expected CF read result types.
226 * This set keeps track of the CF types for which we've issued change
227 * commands so we can tell when we've received all of the responses.
228 */
229 private Collection<Integer> mExpectedChangeResultReasons = null;
230
231 /**
232 * Result of vm number change
233 */
234 private AsyncResult mVoicemailChangeResult = null;
235
236 /**
237 * Previous VM provider setting so we can return to it in case of failure.
238 */
239 private String mPreviousVMProviderKey = null;
240
241 /**
242 * Id of the dialog being currently shown.
243 */
244 private int mCurrentDialogId = 0;
245
246 /**
247 * Flag indicating that we are invoking settings for the voicemail provider programmatically
248 * due to vm provider change.
249 */
250 private boolean mVMProviderSettingsForced = false;
251
252 /**
253 * Flag indicating that we are making changes to vm or fwd numbers
254 * due to vm provider change.
255 */
256 private boolean mChangingVMorFwdDueToProviderChange = false;
257
258 /**
259 * True if we are in the process of vm & fwd number change and vm has already been changed.
260 * This is used to decide what to do in case of rollback.
261 */
262 private boolean mVMChangeCompletedSuccessfully = false;
263
264 /**
265 * True if we had full or partial failure setting forwarding numbers and so need to roll them
266 * back.
267 */
268 private boolean mFwdChangesRequireRollback = false;
269
270 /**
271 * Id of error msg to display to user once we are done reverting the VM provider to the previous
272 * one.
273 */
274 private int mVMOrFwdSetError = 0;
275
276 /**
277 * Data about discovered voice mail settings providers.
278 * Is populated by querying which activities can handle ACTION_CONFIGURE_VOICEMAIL.
279 * They key in this map is package name + activity name.
280 * We always add an entry for the default provider with a key of empty
281 * string and intent value of null.
282 * @see #initVoiceMailProviders()
283 */
284 private final Map<String, VoiceMailProvider> mVMProvidersData =
285 new HashMap<String, VoiceMailProvider>();
286
287 /** string to hold old voicemail number as it is being updated. */
288 private String mOldVmNumber;
289
290 // New call forwarding settings and vm number we will be setting
291 // Need to save these since before we get to saving we need to asynchronously
292 // query the existing forwarding settings.
293 private CallForwardInfo[] mNewFwdSettings;
294 private String mNewVMNumber;
295
296 private boolean mForeground;
297
298 @Override
299 public void onPause() {
300 super.onPause();
301 mForeground = false;
302 }
303
304 /**
305 * We have to pull current settings from the network for all kinds of
306 * voicemail providers so we can tell whether we have to update them,
307 * so use this bit to keep track of whether we're reading settings for the
308 * default provider and should therefore save them out when done.
309 */
310 private boolean mReadingSettingsForDefaultProvider = false;
311
Tyler Gunnbaee2952014-09-10 16:01:02 -0700312 /**
313 * Used to indicate that the voicemail preference should be shown.
314 */
315 private boolean mShowVoicemailPreference = false;
316
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700317 /*
318 * Click Listeners, handle click based on objects attached to UI.
319 */
320
321 // Click listener for all toggle events
322 @Override
323 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
324 if (preference == mSubMenuVoicemailSettings) {
325 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700326 } else if (preference == mButtonDTMF) {
327 return true;
328 } else if (preference == mButtonTTY) {
329 return true;
330 } else if (preference == mButtonAutoRetry) {
331 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
332 android.provider.Settings.Global.CALL_AUTO_RETRY,
333 mButtonAutoRetry.isChecked() ? 1 : 0);
334 return true;
335 } else if (preference == mButtonHAC) {
336 int hac = mButtonHAC.isChecked() ? 1 : 0;
337 // Update HAC value in Settings database
338 Settings.System.putInt(mPhone.getContext().getContentResolver(),
339 Settings.System.HEARING_AID, hac);
340
341 // Update HAC Value in AudioManager
342 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
343 return true;
Andrew Lee696bff52014-12-03 17:58:18 -0800344 } else if (preference.getKey().equals(mVoicemailSettings.getKey())) {
345 // Check key instead of comparing reference because closing the voicemail notification
346 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
347 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800348 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
349
Andrew Lee696bff52014-12-03 17:58:18 -0800350 final Dialog dialog = ((PreferenceScreen) preference).getDialog();
Yorke Leea0f63bf2014-10-09 18:27:20 -0700351 if (dialog != null) {
352 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
353 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800354
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700355 if (preference.getIntent() != null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800356 if (DBG) log("Invoking cfg intent " + preference.getIntent().getPackage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700357
358 // onActivityResult() will be responsible for resetting some of variables.
359 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
360 return true;
361 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800362 if (DBG) log("onPreferenceTreeClick(). No intent; use default behavior in xml.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700363
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800364 // onActivityResult() will not be called, so reset variables here.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700365 mPreviousVMProviderKey = DEFAULT_VM_PROVIDER_KEY;
366 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700367 return false;
368 }
Andrew Lee97708a42014-09-25 12:39:07 -0700369 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700370 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
371 if (dialog != null) {
372 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
373 }
Andrew Lee97708a42014-09-25 12:39:07 -0700374 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700375 }
376 return false;
377 }
378
379 /**
380 * Implemented to support onPreferenceChangeListener to look for preference
381 * changes.
382 *
383 * @param preference is the preference to be changed
384 * @param objValue should be the value of the selection, NOT its localized
385 * display value.
386 */
387 @Override
388 public boolean onPreferenceChange(Preference preference, Object objValue) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800389 if (DBG) log("onPreferenceChange: \"" + preference + "\" changed to \"" + objValue + "\"");
Andrew Lee2170a972014-08-13 18:13:01 -0700390
391 if (preference == mButtonDTMF) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700392 int index = mButtonDTMF.findIndexOfValue((String) objValue);
393 Settings.System.putInt(mPhone.getContext().getContentResolver(),
394 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
395 } else if (preference == mButtonTTY) {
396 handleTTYChange(preference, objValue);
397 } else if (preference == mVoicemailProviders) {
398 final String newProviderKey = (String) objValue;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800399
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700400 // If previous provider key and the new one is same, we don't need to handle it.
401 if (mPreviousVMProviderKey.equals(newProviderKey)) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800402 if (DBG) log("No change is made to the VM provider setting.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700403 return true;
404 }
405 updateVMPreferenceWidgets(newProviderKey);
406
Andrew Leeb490d732014-10-27 15:00:41 -0700407 final VoicemailProviderSettings newProviderSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800408 VoicemailProviderSettingsUtil.load(this, newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700409
Andrew Lee88b51e22014-10-29 15:48:51 -0700410 // If the user switches to a voice mail provider and we have numbers stored for it we
411 // will automatically change the phone's voice mail and forwarding number to the stored
412 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700413 if (newProviderSettings == null) {
414 // Force the user into a configuration of the chosen provider
415 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
416 mVMProviderSettingsForced = true;
417 simulatePreferenceClick(mVoicemailSettings);
418 } else {
419 if (DBG) log("Saved preferences found - switching to them");
420 // Set this flag so if we get a failure we revert to previous provider
421 mChangingVMorFwdDueToProviderChange = true;
422 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
423 }
Andrew Lee8d66d812014-11-24 14:54:02 -0800424 } else if (preference.getKey().equals(mVoicemailNotificationVibrate.getKey())) {
Andrew Lee696bff52014-12-03 17:58:18 -0800425 // Check key instead of comparing reference because closing the voicemail notification
426 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
427 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee8d66d812014-11-24 14:54:02 -0800428 VoicemailNotificationSettingsUtil.setVibrationEnabled(
429 mPhone.getContext(), Boolean.TRUE.equals(objValue));
Andrew Leedf14ead2014-10-17 14:22:52 -0700430 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700431 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
432 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
433 } else {
434 AlertDialog.Builder builder = new AlertDialog.Builder(this);
435 DialogInterface.OnClickListener networkSettingsClickListener =
436 new Dialog.OnClickListener() {
437 @Override
438 public void onClick(DialogInterface dialog, int which) {
439 startActivity(new Intent(mPhone.getContext(),
440 com.android.phone.MobileNetworkSettings.class));
441 }
442 };
443 builder.setMessage(getResources().getString(
444 R.string.enable_video_calling_dialog_msg))
445 .setNeutralButton(getResources().getString(
446 R.string.enable_video_calling_dialog_settings),
447 networkSettingsClickListener)
448 .setPositiveButton(android.R.string.ok, null)
449 .show();
450 return false;
451 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700452 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800453
454 // Always let the preference setting proceed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700455 return true;
456 }
457
458 @Override
459 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800460 if (DBG) log("onDialogClosed: Button clicked is " + buttonClicked);
461
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700462 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
463 return;
464 }
465
466 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700467 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
468 mSubMenuVoicemailSettings.getPhoneNumber(),
469 VoicemailProviderSettings.NO_FORWARDING);
470 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700471 }
472 }
473
474 /**
475 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
476 * This method set the default values for the various
477 * EditPhoneNumberPreference dialogs.
478 */
479 @Override
480 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
481 if (preference == mSubMenuVoicemailSettings) {
482 // update the voicemail number field, which takes care of the
483 // mSubMenuVoicemailSettings itself, so we should return null.
484 if (DBG) log("updating default for voicemail dialog");
485 updateVoiceNumberField();
486 return null;
487 }
488
489 String vmDisplay = mPhone.getVoiceMailNumber();
490 if (TextUtils.isEmpty(vmDisplay)) {
491 // if there is no voicemail number, we just return null to
492 // indicate no contribution.
493 return null;
494 }
495
496 // Return the voicemail number prepended with "VM: "
497 if (DBG) log("updating default for call forwarding dialogs");
498 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
499 }
500
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700501 private void switchToPreviousVoicemailProvider() {
502 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700503
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800504 if (mPreviousVMProviderKey == null) {
505 return;
506 }
507
508 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
Andrew Lee84024342014-11-06 23:37:09 -0800509 showDialogIfForeground(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800510 final VoicemailProviderSettings prevSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800511 VoicemailProviderSettingsUtil.load(this, mPreviousVMProviderKey);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800512 if (prevSettings == null) {
513 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
514 + mPreviousVMProviderKey + "\" is null but should be loaded.");
515 }
516
517 if (mVMChangeCompletedSuccessfully) {
518 mNewVMNumber = prevSettings.getVoicemailNumber();
519 Log.i(LOG_TAG, "VM change is already completed successfully."
520 + "Have to revert VM back to " + mNewVMNumber + " again.");
521 mPhone.setVoiceMailNumber(
522 mPhone.getVoiceMailAlphaTag().toString(),
523 mNewVMNumber,
524 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
525 }
526
527 if (mFwdChangesRequireRollback) {
528 Log.i(LOG_TAG, "Requested to rollback forwarding changes.");
529
530 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
531 if (prevFwdSettings != null) {
532 Map<Integer, AsyncResult> results = mForwardingChangeResults;
533 resetForwardingChangeState();
534 for (int i = 0; i < prevFwdSettings.length; i++) {
535 CallForwardInfo fi = prevFwdSettings[i];
536 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
537 // Only revert the settings for which the update succeeded.
538 AsyncResult result = results.get(fi.reason);
539 if (result != null && result.exception == null) {
540 mExpectedChangeResultReasons.add(fi.reason);
541 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
542 mRevertOptionComplete.obtainMessage(
543 EVENT_FORWARDING_CHANGED, i, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700544 }
545 }
546 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700547 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800548 } else {
549 if (DBG) log("No need to revert");
550 onRevertDone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700551 }
552 }
553
554 private void onRevertDone() {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800555 if (DBG) log("onRevertDone: Changing provider key back to " + mPreviousVMProviderKey);
556
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700557 updateVMPreferenceWidgets(mPreviousVMProviderKey);
558 updateVoiceNumberField();
559 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800560 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700561 mVMOrFwdSetError = 0;
562 }
563 }
564
565 @Override
566 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
567 if (DBG) {
568 log("onActivityResult: requestCode: " + requestCode
569 + ", resultCode: " + resultCode
570 + ", data: " + data);
571 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800572
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700573 // there are cases where the contact picker may end up sending us more than one
574 // request. We want to ignore the request if we're not in the correct state.
575 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
576 boolean failure = false;
577
578 // No matter how the processing of result goes lets clear the flag
579 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
580 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
581 mVMProviderSettingsForced = false;
582
583 String vmNum = null;
584 if (resultCode != RESULT_OK) {
585 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
586 failure = true;
587 } else {
588 if (data == null) {
589 if (DBG) log("onActivityResult: vm provider cfg result has no data");
590 failure = true;
591 } else {
592 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
593 if (DBG) log("Provider requested signout");
594 if (isVMProviderSettingsForced) {
595 if (DBG) log("Going back to previous provider on signout");
596 switchToPreviousVoicemailProvider();
597 } else {
598 final String victim = getCurrentVoicemailProviderKey();
599 if (DBG) log("Relaunching activity and ignoring " + victim);
600 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
601 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
602 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
603 this.startActivity(i);
604 }
605 return;
606 }
607 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
608 if (vmNum == null || vmNum.length() == 0) {
609 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
610 failure = true;
611 }
612 }
613 }
614 if (failure) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800615 if (DBG) log("Failure in return from voicemail provider.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700616 if (isVMProviderSettingsForced) {
617 switchToPreviousVoicemailProvider();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700618 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800619
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700620 return;
621 }
622 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
623 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
624
Santos Cordonda120f42014-08-06 04:44:34 -0700625 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700626 // send it to the provider when it's config is invoked so it can use this as default
627 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
628
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800629 if (DBG) log("onActivityResult: cfg result has forwarding number " + fwdNum);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700630 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700631 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700632 return;
633 }
634
635 if (requestCode == VOICEMAIL_PREF_ID) {
636 if (resultCode != RESULT_OK) {
637 if (DBG) log("onActivityResult: contact picker result not OK.");
638 return;
639 }
640
641 Cursor cursor = null;
642 try {
643 cursor = getContentResolver().query(data.getData(),
644 NUM_PROJECTION, null, null, null);
645 if ((cursor == null) || (!cursor.moveToFirst())) {
646 if (DBG) log("onActivityResult: bad contact data, no results found.");
647 return;
648 }
649 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
650 return;
651 } finally {
652 if (cursor != null) {
653 cursor.close();
654 }
655 }
656 }
657
658 super.onActivityResult(requestCode, resultCode, data);
659 }
660
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700661 /**
662 * Wrapper around showDialog() that will silently do nothing if we're
663 * not in the foreground.
664 *
665 * This is useful here because most of the dialogs we display from
666 * this class are triggered by asynchronous events (like
667 * success/failure messages from the telephony layer) and it's
668 * possible for those events to come in even after the user has gone
669 * to a different screen.
670 */
671 // TODO: this is too brittle: it's still easy to accidentally add new
672 // code here that calls showDialog() directly (which will result in a
673 // WindowManager$BadTokenException if called after the activity has
674 // been stopped.)
675 //
676 // It would be cleaner to do the "if (mForeground)" check in one
677 // central place, maybe by using a single Handler for all asynchronous
678 // events (and have *that* discard events if we're not in the
679 // foreground.)
680 //
681 // Unfortunately it's not that simple, since we sometimes need to do
682 // actual work to handle these events whether or not we're in the
683 // foreground (see the Handler code in mSetOptionComplete for
684 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800685 //
686 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
687 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700688 private void showDialogIfForeground(int id) {
689 if (mForeground) {
690 showDialog(id);
691 }
692 }
693
694 private void dismissDialogSafely(int id) {
695 try {
696 dismissDialog(id);
697 } catch (IllegalArgumentException e) {
698 // This is expected in the case where we were in the background
699 // at the time we would normally have shown the dialog, so we didn't
700 // show it.
701 }
702 }
703
Andrew Leeb490d732014-10-27 15:00:41 -0700704 private void saveVoiceMailAndForwardingNumber(
705 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700706 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700707 mNewVMNumber = newSettings.getVoicemailNumber();
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800708 mNewVMNumber = (mNewVMNumber == null) ? "" : mNewVMNumber;
Andrew Leeb490d732014-10-27 15:00:41 -0700709 mNewFwdSettings = newSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700710
711 // No fwd settings on CDMA
712 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
713 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700714 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700715 }
716
Andrew Leee3c15212014-10-28 13:12:55 -0700717 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700718 if (mNewVMNumber.equals(mOldVmNumber)
719 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Lee84024342014-11-06 23:37:09 -0800720 showDialogIfForeground(VoicemailDialogUtil.VM_NOCHANGE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700721 return;
722 }
723
Andrew Lee6950c6c2014-11-26 12:05:42 -0800724 VoicemailProviderSettingsUtil.save(this, key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700725 mVMChangeCompletedSuccessfully = false;
726 mFwdChangesRequireRollback = false;
727 mVMOrFwdSetError = 0;
728 if (!key.equals(mPreviousVMProviderKey)) {
729 mReadingSettingsForDefaultProvider =
730 mPreviousVMProviderKey.equals(DEFAULT_VM_PROVIDER_KEY);
731 if (DBG) log("Reading current forwarding settings");
Andrew Leeb490d732014-10-27 15:00:41 -0700732 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
733 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
734 for (int i = 0; i < mForwardingReadResults.length; i++) {
735 mPhone.getCallForwardingOption(
736 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700737 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
738 }
Andrew Lee84024342014-11-06 23:37:09 -0800739 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700740 } else {
741 saveVoiceMailAndForwardingNumberStage2();
742 }
743 }
744
745 private final Handler mGetOptionComplete = new Handler() {
746 @Override
747 public void handleMessage(Message msg) {
748 AsyncResult result = (AsyncResult) msg.obj;
749 switch (msg.what) {
750 case EVENT_FORWARDING_GET_COMPLETED:
751 handleForwardingSettingsReadResult(result, msg.arg1);
752 break;
753 }
754 }
755 };
756
757 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
758 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800759
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700760 Throwable error = null;
761 if (ar.exception != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700762 error = ar.exception;
Andrew Lee1af6cf72014-11-04 17:35:26 -0800763 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700764 }
765 if (ar.userObj instanceof Throwable) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800766 error = (Throwable) ar.userObj;
767 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700768 }
769
770 // We may have already gotten an error and decided to ignore the other results.
771 if (mForwardingReadResults == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800772 if (DBG) Log.d(LOG_TAG, "Ignoring fwd reading result: " + idx);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700773 return;
774 }
775
776 // In case of error ignore other results, show an error dialog
777 if (error != null) {
778 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
779 mForwardingReadResults = null;
Andrew Lee84024342014-11-06 23:37:09 -0800780 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
781 showDialogIfForeground(VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700782 return;
783 }
784
Andrew Lee1af6cf72014-11-04 17:35:26 -0800785 // Get the forwarding info.
786 mForwardingReadResults[idx] = CallForwardInfoUtil.getCallForwardInfo(
787 (CallForwardInfo[]) ar.result,
788 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx]);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700789
790 // Check if we got all the results already
791 boolean done = true;
792 for (int i = 0; i < mForwardingReadResults.length; i++) {
793 if (mForwardingReadResults[i] == null) {
794 done = false;
795 break;
796 }
797 }
Andrew Lee1af6cf72014-11-04 17:35:26 -0800798
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700799 if (done) {
800 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
Andrew Lee84024342014-11-06 23:37:09 -0800801 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800802
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700803 if (mReadingSettingsForDefaultProvider) {
Andrew Lee6950c6c2014-11-26 12:05:42 -0800804 VoicemailProviderSettingsUtil.save(this, DEFAULT_VM_PROVIDER_KEY,
Andrew Lee88b51e22014-10-29 15:48:51 -0700805 new VoicemailProviderSettings(this.mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700806 mReadingSettingsForDefaultProvider = false;
807 }
808 saveVoiceMailAndForwardingNumberStage2();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700809 }
810 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800811
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700812 private void resetForwardingChangeState() {
813 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
814 mExpectedChangeResultReasons = new HashSet<Integer>();
815 }
816
817 // Called after we are done saving the previous forwarding settings if
818 // we needed.
819 private void saveVoiceMailAndForwardingNumberStage2() {
820 mForwardingChangeResults = null;
821 mVoicemailChangeResult = null;
Andrew Leeb490d732014-10-27 15:00:41 -0700822 if (mNewFwdSettings != VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700823 resetForwardingChangeState();
824 for (int i = 0; i < mNewFwdSettings.length; i++) {
825 CallForwardInfo fi = mNewFwdSettings[i];
Andrew Lee1af6cf72014-11-04 17:35:26 -0800826 CallForwardInfo fiForReason =
827 CallForwardInfoUtil.infoForReason(mForwardingReadResults, fi.reason);
828 final boolean doUpdate = CallForwardInfoUtil.isUpdateRequired(fiForReason, fi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700829
830 if (doUpdate) {
831 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
832 mExpectedChangeResultReasons.add(i);
833
Andrew Lee1af6cf72014-11-04 17:35:26 -0800834 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700835 mSetOptionComplete.obtainMessage(
836 EVENT_FORWARDING_CHANGED, fi.reason, 0));
837 }
838 }
Andrew Lee84024342014-11-06 23:37:09 -0800839 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700840 } else {
841 if (DBG) log("Not touching fwd #");
842 setVMNumberWithCarrier();
843 }
844 }
845
846 private void setVMNumberWithCarrier() {
847 if (DBG) log("save voicemail #: " + mNewVMNumber);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800848
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700849 mPhone.setVoiceMailNumber(
850 mPhone.getVoiceMailAlphaTag().toString(),
851 mNewVMNumber,
852 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
853 }
854
855 /**
856 * Callback to handle option update completions
857 */
858 private final Handler mSetOptionComplete = new Handler() {
859 @Override
860 public void handleMessage(Message msg) {
861 AsyncResult result = (AsyncResult) msg.obj;
862 boolean done = false;
863 switch (msg.what) {
864 case EVENT_VOICEMAIL_CHANGED:
865 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700866 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700867 done = true;
868 break;
869 case EVENT_FORWARDING_CHANGED:
870 mForwardingChangeResults.put(msg.arg1, result);
871 if (result.exception != null) {
872 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
873 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700874 }
Andrew Leee438b312014-10-29 16:59:15 -0700875 if (isForwardingCompleted()) {
876 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700877 if (DBG) log("Overall fwd changes completed ok, starting vm change");
878 setVMNumberWithCarrier();
879 } else {
880 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
881 "Check if we need to try rollback for some settings.");
882 mFwdChangesRequireRollback = false;
883 Iterator<Map.Entry<Integer,AsyncResult>> it =
884 mForwardingChangeResults.entrySet().iterator();
885 while (it.hasNext()) {
886 Map.Entry<Integer,AsyncResult> entry = it.next();
887 if (entry.getValue().exception == null) {
888 // If at least one succeeded we have to revert
889 Log.i(LOG_TAG, "Rollback will be required");
890 mFwdChangesRequireRollback = true;
891 break;
892 }
893 }
894 if (!mFwdChangesRequireRollback) {
895 Log.i(LOG_TAG, "No rollback needed.");
896 }
897 done = true;
898 }
899 }
900 break;
901 default:
902 // TODO: should never reach this, may want to throw exception
903 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800904
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700905 if (done) {
906 if (DBG) log("All VM provider related changes done");
907 if (mForwardingChangeResults != null) {
Andrew Lee84024342014-11-06 23:37:09 -0800908 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700909 }
Andrew Leee438b312014-10-29 16:59:15 -0700910 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700911 }
912 }
913 };
914
915 /**
916 * Callback to handle option revert completions
917 */
918 private final Handler mRevertOptionComplete = new Handler() {
919 @Override
920 public void handleMessage(Message msg) {
921 AsyncResult result = (AsyncResult) msg.obj;
922 switch (msg.what) {
923 case EVENT_VOICEMAIL_CHANGED:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700924 if (DBG) log("VM revert complete msg");
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800925 mVoicemailChangeResult = result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700926 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800927
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700928 case EVENT_FORWARDING_CHANGED:
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800929 if (DBG) log("FWD revert complete msg ");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700930 mForwardingChangeResults.put(msg.arg1, result);
931 if (result.exception != null) {
932 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
933 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700934 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700935 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800936
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700937 default:
938 // TODO: should never reach this, may want to throw exception
939 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800940
941 final boolean done = (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null)
942 && (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700943 if (done) {
944 if (DBG) log("All VM reverts done");
Andrew Lee84024342014-11-06 23:37:09 -0800945 dismissDialogSafely(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700946 onRevertDone();
947 }
948 }
949 };
950
951 /**
Andrew Leee438b312014-10-29 16:59:15 -0700952 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700953 */
Andrew Leee438b312014-10-29 16:59:15 -0700954 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700955 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -0700956 return true;
957 }
958
959 for (Integer reason : mExpectedChangeResultReasons) {
960 if (mForwardingChangeResults.get(reason) == null) {
961 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700962 }
963 }
Andrew Leee438b312014-10-29 16:59:15 -0700964
965 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700966 }
Andrew Leee438b312014-10-29 16:59:15 -0700967
968 private boolean isFwdChangeSuccess() {
969 if (mForwardingChangeResults == null) {
970 return true;
971 }
972
973 for (AsyncResult result : mForwardingChangeResults.values()) {
974 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700975 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700976 String msg = exception.getMessage();
977 msg = (msg != null) ? msg : "";
978 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
979 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700980 }
981 }
Andrew Leee438b312014-10-29 16:59:15 -0700982 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700983 }
984
Andrew Leee438b312014-10-29 16:59:15 -0700985 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700986 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700987 String msg = mVoicemailChangeResult.exception.getMessage();
988 msg = (msg != null) ? msg : "";
989 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
990 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700991 }
Andrew Leee438b312014-10-29 16:59:15 -0700992 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700993 }
994
Andrew Leee438b312014-10-29 16:59:15 -0700995 private void handleSetVmOrFwdMessage() {
996 if (DBG) log("handleSetVMMessage: set VM request complete");
997
998 if (!isFwdChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -0800999 handleVmOrFwdSetError(VoicemailDialogUtil.FWD_SET_RESPONSE_ERROR_DIALOG);
Andrew Leee438b312014-10-29 16:59:15 -07001000 } else if (!isVmChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -08001001 handleVmOrFwdSetError(VoicemailDialogUtil.VM_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001002 } else {
Andrew Lee84024342014-11-06 23:37:09 -08001003 handleVmAndFwdSetSuccess(VoicemailDialogUtil.VM_CONFIRM_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001004 }
1005 }
1006
1007 /**
1008 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
1009 * changes to those settings and show "failure" dialog.
1010 *
Andrew Leeab082272014-11-04 15:50:42 -08001011 * @param dialogId ID of the dialog to show for the specific error case. Either
Andrew Lee84024342014-11-06 23:37:09 -08001012 * {@link #FWD_SET_RESPONSE_ERROR_DIALOG} or {@link #VM_RESPONSE_ERROR_DIALOG}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001013 */
Andrew Leeab082272014-11-04 15:50:42 -08001014 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001015 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -08001016 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001017 mChangingVMorFwdDueToProviderChange = false;
1018 switchToPreviousVoicemailProvider();
1019 return;
1020 }
1021 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001022 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001023 updateVoiceNumberField();
1024 }
1025
1026 /**
1027 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1028 * This updates a bunch of variables and show "success" dialog.
1029 */
Andrew Leeab082272014-11-04 15:50:42 -08001030 private void handleVmAndFwdSetSuccess(int dialogId) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001031 if (DBG) log("handleVmAndFwdSetSuccess: key is " + getCurrentVoicemailProviderKey());
1032
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001033 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1034 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001035 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001036 updateVoiceNumberField();
1037 }
1038
1039 /**
1040 * Update the voicemail number from what we've recorded on the sim.
1041 */
1042 private void updateVoiceNumberField() {
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001043 if (DBG) log("updateVoiceNumberField()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001044
1045 mOldVmNumber = mPhone.getVoiceMailNumber();
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001046 if (TextUtils.isEmpty(mOldVmNumber)) {
1047 mSubMenuVoicemailSettings.setPhoneNumber("");
1048 mSubMenuVoicemailSettings.setSummary(getString(R.string.voicemail_number_not_set));
1049 } else {
1050 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1051 mSubMenuVoicemailSettings.setSummary(mOldVmNumber);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001052 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001053 }
1054
1055 /*
1056 * Helper Methods for Activity class.
1057 * The initial query commands are split into two pieces now
1058 * for individual expansion. This combined with the ability
1059 * to cancel queries allows for a much better user experience,
1060 * and also ensures that the user only waits to update the
1061 * data that is relevant.
1062 */
1063
1064 @Override
1065 protected void onPrepareDialog(int id, Dialog dialog) {
1066 super.onPrepareDialog(id, dialog);
1067 mCurrentDialogId = id;
1068 }
1069
1070 // dialog creation method, called by showDialog()
1071 @Override
Andrew Lee84024342014-11-06 23:37:09 -08001072 protected Dialog onCreateDialog(int dialogId) {
1073 return VoicemailDialogUtil.getDialog(this, dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001074 }
1075
1076 // This is a method implemented for DialogInterface.OnClickListener.
1077 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1078 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1079 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1080 public void onClick(DialogInterface dialog, int which) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001081 if (DBG) log("onClick: button clicked is " + which);
1082
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001083 dialog.dismiss();
1084 switch (which){
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001085 case DialogInterface.BUTTON_NEGATIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001086 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001087 // We failed to get current forwarding settings and the user
1088 // does not wish to continue.
1089 switchToPreviousVoicemailProvider();
1090 }
1091 break;
1092 case DialogInterface.BUTTON_POSITIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001093 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001094 // We failed to get current forwarding settings but the user
1095 // wishes to continue changing settings to the new vm provider
1096 saveVoiceMailAndForwardingNumberStage2();
1097 } else {
1098 finish();
1099 }
1100 return;
1101 default:
1102 // just let the dialog close and go back to the input
1103 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001104
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001105 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1106 // with settings UI. If we were called to explicitly configure voice mail then
1107 // we finish the settings activity here to come back to whatever the user was doing.
1108 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1109 finish();
1110 }
1111 }
1112
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001113 /*
1114 * Activity class methods
1115 */
1116
1117 @Override
1118 protected void onCreate(Bundle icicle) {
1119 super.onCreate(icicle);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001120 if (DBG) log("onCreate: Intent is " + getIntent());
1121
Jay Shraunerc2684732014-11-12 12:10:37 -08001122 // Make sure we are running as the primary user.
1123 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
1124 Toast.makeText(this, R.string.call_settings_primary_user_only,
1125 Toast.LENGTH_SHORT).show();
1126 finish();
1127 return;
1128 }
1129
Tyler Gunnbaee2952014-09-10 16:01:02 -07001130 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
1131
Tyler Gunnbaee2952014-09-10 16:01:02 -07001132 // Show the voicemail preference in onResume if the calling intent specifies the
1133 // ACTION_ADD_VOICEMAIL action.
1134 mShowVoicemailPreference = (icicle == null) &&
Jay Shraunerbe2fb262014-11-11 15:19:58 -08001135 TextUtils.equals(getIntent().getAction(), ACTION_ADD_VOICEMAIL);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001136
1137 mSubscriptionInfoHelper = new SubscriptionInfoHelper(getIntent());
1138 mSubscriptionInfoHelper.setActionBarTitle(
1139 getActionBar(), getResources(), R.string.call_settings_with_label);
Andrew Lee1479dd12014-11-06 23:06:32 -08001140 mPhone = mSubscriptionInfoHelper.getPhone();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001141 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001142
1143 private void initPhoneAccountPreferences() {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001144 mPhoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001145
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001146 TelecomManager telecomManager = TelecomManager.from(this);
Andrew Lee93c345f2014-10-27 15:25:07 -07001147 TelephonyManager telephonyManager =
1148 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001149
Andrew Lee93c345f2014-10-27 15:25:07 -07001150 if ((telecomManager.getSimCallManagers().isEmpty() && !SipUtil.isVoipSupported(this))
1151 || telephonyManager.getPhoneCount() > 1) {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001152 getPreferenceScreen().removePreference(mPhoneAccountSettingsPreference);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001153 }
1154 }
1155
Tyler Gunnbaee2952014-09-10 16:01:02 -07001156 @Override
1157 protected void onResume() {
1158 super.onResume();
1159 mForeground = true;
1160
1161 PreferenceScreen preferenceScreen = getPreferenceScreen();
1162 if (preferenceScreen != null) {
1163 preferenceScreen.removeAll();
1164 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001165
1166 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001167
Andrew Leedb2fe562014-09-03 15:40:43 -07001168 initPhoneAccountPreferences();
1169
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001170 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001171 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1172 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1173 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1174 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001175
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001176 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1177 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1178 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
1179 mButtonTTY = (ListPreference) findPreference(BUTTON_TTY_KEY);
Andrew Lee312e8172014-10-23 17:01:36 -07001180 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001181
Andrew Lee8d66d812014-11-24 14:54:02 -08001182 mVoicemailProviders = (ListPreference) findPreference(BUTTON_VOICEMAIL_PROVIDER_KEY);
Andrew Lee2c027892014-10-29 11:29:54 -07001183 mVoicemailProviders.setOnPreferenceChangeListener(this);
Andrew Lee8d66d812014-11-24 14:54:02 -08001184
Andrew Lee2c027892014-10-29 11:29:54 -07001185 mVoicemailSettingsScreen =
1186 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
1187 mVoicemailSettings = (PreferenceScreen)findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
Andrew Lee8d66d812014-11-24 14:54:02 -08001188
1189 mVoicemailNotificationVibrate = (CheckBoxPreference) findPreference(
1190 getResources().getString(R.string.voicemail_notification_vibrate_key));
1191 mVoicemailNotificationVibrate.setOnPreferenceChangeListener(this);
1192
Andrew Lee2c027892014-10-29 11:29:54 -07001193 initVoiceMailProviders();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001194
Andrew Lee64a7d792014-10-15 17:38:38 -07001195 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1196 mButtonDTMF.setOnPreferenceChangeListener(this);
1197 int dtmf = Settings.System.getInt(getContentResolver(),
1198 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1199 mButtonDTMF.setValueIndex(dtmf);
1200 } else {
1201 prefSet.removePreference(mButtonDTMF);
1202 mButtonDTMF = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001203 }
1204
Andrew Lee64a7d792014-10-15 17:38:38 -07001205 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1206 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1207 int autoretry = Settings.Global.getInt(
1208 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1209 mButtonAutoRetry.setChecked(autoretry != 0);
1210 } else {
1211 prefSet.removePreference(mButtonAutoRetry);
1212 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001213 }
1214
Andrew Lee64a7d792014-10-15 17:38:38 -07001215 if (getResources().getBoolean(R.bool.hac_enabled)) {
1216 mButtonHAC.setOnPreferenceChangeListener(this);
1217 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1218 mButtonHAC.setChecked(hac != 0);
1219 } else {
1220 prefSet.removePreference(mButtonHAC);
1221 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001222 }
1223
Andrew Lee64a7d792014-10-15 17:38:38 -07001224 TelecomManager telecomManager = TelecomManager.from(this);
1225 if (telecomManager != null && telecomManager.isTtySupported()) {
1226 mButtonTTY.setOnPreferenceChangeListener(this);
1227 int settingsTtyMode = Settings.Secure.getInt(getContentResolver(),
1228 Settings.Secure.PREFERRED_TTY_MODE,
1229 TelecomManager.TTY_MODE_OFF);
1230 mButtonTTY.setValue(Integer.toString(settingsTtyMode));
1231 updatePreferredTtyModeSummary(settingsTtyMode);
1232 } else {
1233 prefSet.removePreference(mButtonTTY);
1234 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001235 }
1236
1237 if (!getResources().getBoolean(R.bool.world_phone)) {
Andrew Lee2b36ba22014-11-05 17:08:49 -08001238 Preference cdmaOptions = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
1239 prefSet.removePreference(cdmaOptions);
1240
1241 // TODO: Support MSIM for this preference option.
1242 Preference gsmOptions = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
1243 prefSet.removePreference(gsmOptions);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001244
1245 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001246 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Junda Liua09d6962014-11-12 13:39:02 -08001247 boolean shouldHideCarrierSettings = Settings.Global.getInt(
1248 getContentResolver(), Settings.Global.HIDE_CARRIER_NETWORK_SETTINGS, 0) == 1;
1249 if (shouldHideCarrierSettings) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001250 prefSet.removePreference(fdnButton);
Junda Liua09d6962014-11-12 13:39:02 -08001251 if (mButtonDTMF != null) {
1252 prefSet.removePreference(mButtonDTMF);
Etan Cohen0ca1c802014-07-07 15:35:48 -07001253 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001254 } else {
Junda Liua09d6962014-11-12 13:39:02 -08001255 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1256 prefSet.removePreference(fdnButton);
1257
1258 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1259 addPreferencesFromResource(R.xml.cdma_call_privacy);
1260 }
1261 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
1262 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(this, FdnSetting.class));
1263
1264 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
1265 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1266
1267 Preference callForwardingPref = prefSet.findPreference(CALL_FORWARDING_KEY);
1268 callForwardingPref.setIntent(mSubscriptionInfoHelper.getIntent(
1269 this, GsmUmtsCallForwardOptions.class));
1270
1271 Preference additionalGsmSettingsPref =
1272 prefSet.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
1273 additionalGsmSettingsPref.setIntent(mSubscriptionInfoHelper.getIntent(
1274 this, GsmUmtsAdditionalCallOptions.class));
1275 }
1276 } else {
1277 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1278 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001279 }
1280 }
1281
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001282 // check the intent that started this activity and pop up the voicemail
1283 // dialog if we've been asked to.
1284 // If we have at least one non default VM provider registered then bring up
1285 // the selection for the VM provider, otherwise bring up a VM number dialog.
1286 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001287 if (mShowVoicemailPreference) {
Tyler Gunnbaee2952014-09-10 16:01:02 -07001288 if (DBG) {
1289 log("ACTION_ADD_VOICEMAIL Intent is thrown. current VM data size: "
1290 + mVMProvidersData.size());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001291 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001292 if (mVMProvidersData.size() > 1) {
1293 simulatePreferenceClick(mVoicemailProviders);
1294 } else {
1295 onPreferenceChange(mVoicemailProviders, DEFAULT_VM_PROVIDER_KEY);
1296 mVoicemailProviders.setValue(DEFAULT_VM_PROVIDER_KEY);
1297 }
1298 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001299 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001300
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001301 updateVoiceNumberField();
1302 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001303
Andrew Lee8d66d812014-11-24 14:54:02 -08001304 mVoicemailNotificationVibrate.setChecked(
1305 VoicemailNotificationSettingsUtil.isVibrationEnabled(mPhone.getContext()));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001306
Andrew Lee312e8172014-10-23 17:01:36 -07001307 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1308 boolean currentValue =
1309 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1310 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1311 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001312 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1313 } else {
1314 prefSet.removePreference(mEnableVideoCalling);
1315 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001316 }
1317
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001318 private void handleTTYChange(Preference preference, Object objValue) {
1319 int buttonTtyMode;
1320 buttonTtyMode = Integer.valueOf((String) objValue).intValue();
1321 int settingsTtyMode = android.provider.Settings.Secure.getInt(
1322 getContentResolver(),
Sailesh Nepalbf900542014-07-15 16:18:32 -07001323 android.provider.Settings.Secure.PREFERRED_TTY_MODE,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001324 TelecomManager.TTY_MODE_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001325 if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
1326 Integer.toString(buttonTtyMode));
1327
1328 if (buttonTtyMode != settingsTtyMode) {
1329 switch(buttonTtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001330 case TelecomManager.TTY_MODE_OFF:
1331 case TelecomManager.TTY_MODE_FULL:
1332 case TelecomManager.TTY_MODE_HCO:
1333 case TelecomManager.TTY_MODE_VCO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001334 android.provider.Settings.Secure.putInt(getContentResolver(),
1335 android.provider.Settings.Secure.PREFERRED_TTY_MODE, buttonTtyMode);
1336 break;
1337 default:
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001338 buttonTtyMode = TelecomManager.TTY_MODE_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001339 }
1340
1341 mButtonTTY.setValue(Integer.toString(buttonTtyMode));
1342 updatePreferredTtyModeSummary(buttonTtyMode);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001343 Intent ttyModeChanged = new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
1344 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001345 sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
1346 }
1347 }
1348
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001349 private void updatePreferredTtyModeSummary(int TtyMode) {
1350 String [] txts = getResources().getStringArray(R.array.tty_mode_entries);
1351 switch(TtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001352 case TelecomManager.TTY_MODE_OFF:
1353 case TelecomManager.TTY_MODE_HCO:
1354 case TelecomManager.TTY_MODE_VCO:
1355 case TelecomManager.TTY_MODE_FULL:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001356 mButtonTTY.setSummary(txts[TtyMode]);
1357 break;
1358 default:
1359 mButtonTTY.setEnabled(false);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001360 mButtonTTY.setSummary(txts[TelecomManager.TTY_MODE_OFF]);
Sailesh Nepalbf900542014-07-15 16:18:32 -07001361 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001362 }
1363 }
1364
1365 private static void log(String msg) {
1366 Log.d(LOG_TAG, msg);
1367 }
1368
1369 /**
1370 * Updates the look of the VM preference widgets based on current VM provider settings.
1371 * Note that the provider name is loaded form the found activity via loadLabel in
1372 * {@link #initVoiceMailProviders()} in order for it to be localizable.
1373 */
1374 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1375 final String key = currentProviderSetting;
1376 final VoiceMailProvider provider = mVMProvidersData.get(key);
1377
1378 /* This is the case when we are coming up on a freshly wiped phone and there is no
1379 persisted value for the list preference mVoicemailProviders.
1380 In this case we want to show the UI asking the user to select a voicemail provider as
1381 opposed to silently falling back to default one. */
1382 if (provider == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001383 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> null.");
1384
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001385 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1386 mVoicemailSettings.setEnabled(false);
1387 mVoicemailSettings.setIntent(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001388 mVoicemailNotificationVibrate.setEnabled(false);
1389 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001390 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> " + provider.toString());
1391
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001392 final String providerName = provider.name;
1393 mVoicemailProviders.setSummary(providerName);
1394 mVoicemailSettings.setEnabled(true);
1395 mVoicemailSettings.setIntent(provider.intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001396 mVoicemailNotificationVibrate.setEnabled(true);
1397 }
1398 }
1399
1400 /**
1401 * Enumerates existing VM providers and puts their data into the list and populates
1402 * the preference list objects with their names.
1403 * In case we are called with ACTION_ADD_VOICEMAIL intent the intent may have
1404 * an extra string called IGNORE_PROVIDER_EXTRA with "package.activityName" of the provider
1405 * which should be hidden when we bring up the list of possible VM providers to choose.
1406 */
1407 private void initVoiceMailProviders() {
1408 if (DBG) log("initVoiceMailProviders()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001409
1410 String providerToIgnore = null;
Andrew Leef1776d82014-11-04 14:45:02 -08001411 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)
1412 && getIntent().hasExtra(IGNORE_PROVIDER_EXTRA)) {
1413 providerToIgnore = getIntent().getStringExtra(IGNORE_PROVIDER_EXTRA);
1414 // Remove this provider from the list.
1415 if (!TextUtils.isEmpty(providerToIgnore)) {
1416 if (DBG) log("Found ACTION_ADD_VOICEMAIL. providerToIgnore= " + providerToIgnore);
Andrew Lee6950c6c2014-11-26 12:05:42 -08001417 VoicemailProviderSettingsUtil.delete(this, providerToIgnore);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001418 }
1419 }
1420
1421 mVMProvidersData.clear();
1422
Andrew Leef1776d82014-11-04 14:45:02 -08001423 List<String> entries = new ArrayList<String>();
1424 List<String> values = new ArrayList<String>();
1425
1426 // Add default voicemail provider.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001427 final String myCarrier = getString(R.string.voicemail_default);
1428 mVMProvidersData.put(DEFAULT_VM_PROVIDER_KEY, new VoiceMailProvider(myCarrier, null));
Andrew Leef1776d82014-11-04 14:45:02 -08001429 entries.add(myCarrier);
1430 values.add(DEFAULT_VM_PROVIDER_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001431
Andrew Leef1776d82014-11-04 14:45:02 -08001432 // Add other voicemail providers.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001433 PackageManager pm = getPackageManager();
Andrew Leef1776d82014-11-04 14:45:02 -08001434 Intent intent = new Intent(ACTION_CONFIGURE_VOICEMAIL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001435 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001436 for (int i = 0; i < resolveInfos.size(); i++) {
1437 final ResolveInfo ri= resolveInfos.get(i);
1438 final ActivityInfo currentActivityInfo = ri.activityInfo;
Andrew Lee6214e2b2014-11-04 13:57:38 -08001439 final String key = currentActivityInfo.name;
Andrew Leef1776d82014-11-04 14:45:02 -08001440
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001441 if (key.equals(providerToIgnore)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001442 continue;
1443 }
Andrew Leef1776d82014-11-04 14:45:02 -08001444
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001445 if (DBG) log("Loading key: " + key);
1446 final String nameForDisplay = ri.loadLabel(pm).toString();
1447 Intent providerIntent = new Intent();
1448 providerIntent.setAction(ACTION_CONFIGURE_VOICEMAIL);
Andrew Leef1776d82014-11-04 14:45:02 -08001449 providerIntent.setClassName(currentActivityInfo.packageName, currentActivityInfo.name);
1450 VoiceMailProvider vmProvider = new VoiceMailProvider(nameForDisplay, providerIntent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001451
Andrew Leef1776d82014-11-04 14:45:02 -08001452 if (DBG) log("Store VoiceMailProvider. Key: " + key + " -> " + vmProvider.toString());
1453 mVMProvidersData.put(key, vmProvider);
1454 entries.add(vmProvider.name);
1455 values.add(key);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001456 }
1457
Andrew Leef1776d82014-11-04 14:45:02 -08001458 mVoicemailProviders.setEntries(entries.toArray(new String[0]));
1459 mVoicemailProviders.setEntryValues(values.toArray(new String[0]));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001460
Andrew Leef1776d82014-11-04 14:45:02 -08001461 // Remember the current Voicemail Provider key as a "previous" key. This will be used when
1462 // we fail to update Voicemail Provider, which requires rollback. We will update this when
1463 // the VM Provider setting is successfully updated.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001464 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1465 if (DBG) log("Set up the first mPreviousVMProviderKey: " + mPreviousVMProviderKey);
1466
1467 // Finally update the preference texts.
1468 updateVMPreferenceWidgets(mPreviousVMProviderKey);
1469 }
1470
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001471 /**
1472 * Simulates user clicking on a passed preference.
1473 * Usually needed when the preference is a dialog preference and we want to invoke
1474 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001475 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001476 */
1477 private void simulatePreferenceClick(Preference preference) {
1478 // Go through settings until we find our setting
1479 // and then simulate a click on it to bring up the dialog
1480 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1481 for (int idx = 0; idx < adapter.getCount(); idx++) {
1482 if (adapter.getItem(idx) == preference) {
1483 getPreferenceScreen().onItemClick(this.getListView(),
1484 null, idx, adapter.getItemId(idx));
1485 break;
1486 }
1487 }
1488 }
1489
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001490 private String getCurrentVoicemailProviderKey() {
1491 final String key = mVoicemailProviders.getValue();
1492 return (key != null) ? key : DEFAULT_VM_PROVIDER_KEY;
1493 }
1494
1495 @Override
1496 public boolean onOptionsItemSelected(MenuItem item) {
1497 final int itemId = item.getItemId();
1498 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001499 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001500 return true;
1501 }
1502 return super.onOptionsItemSelected(item);
1503 }
Andrew Lee8d66d812014-11-24 14:54:02 -08001504
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001505 /**
1506 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1507 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1508 */
1509 public static void goUpToTopLevelSetting(Activity activity) {
1510 Intent intent = new Intent(activity, CallFeaturesSetting.class);
1511 intent.setAction(Intent.ACTION_MAIN);
1512 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1513 activity.startActivity(intent);
1514 activity.finish();
1515 }
1516}