blob: d254f8f4240fb2ec78e9627d8457857e9569a3aa [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;
24import android.app.ProgressDialog;
25import android.content.ContentResolver;
26import android.content.Context;
27import android.content.DialogInterface;
28import android.content.Intent;
29import android.content.SharedPreferences;
30import android.content.SharedPreferences.Editor;
31import android.content.pm.ActivityInfo;
32import android.content.pm.PackageManager;
33import android.content.pm.ResolveInfo;
34import android.database.Cursor;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.media.AudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.os.AsyncResult;
37import android.os.Bundle;
38import android.os.Handler;
39import android.os.Message;
40import android.os.UserHandle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.preference.CheckBoxPreference;
42import android.preference.ListPreference;
43import android.preference.Preference;
44import android.preference.PreferenceActivity;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.preference.PreferenceManager;
46import android.preference.PreferenceScreen;
47import android.provider.ContactsContract.CommonDataKinds;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070049import android.telecom.PhoneAccountHandle;
50import android.telecom.TelecomManager;
Andrew Lee93c345f2014-10-27 15:25:07 -070051import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.text.TextUtils;
53import android.util.Log;
54import android.view.MenuItem;
55import android.view.WindowManager;
56import android.widget.ListAdapter;
57
Andrew Lee312e8172014-10-23 17:01:36 -070058import com.android.ims.ImsManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import com.android.internal.telephony.CallForwardInfo;
60import com.android.internal.telephony.CommandsInterface;
61import com.android.internal.telephony.Phone;
62import com.android.internal.telephony.PhoneConstants;
Andrew Lee2170a972014-08-13 18:13:01 -070063import com.android.phone.common.util.SettingsUtil;
Andrew Leedb2fe562014-09-03 15:40:43 -070064import com.android.phone.settings.AccountSelectionPreference;
Andrew Leeb490d732014-10-27 15:00:41 -070065import com.android.phone.settings.VoicemailProviderSettings;
Andrew Lee88b51e22014-10-29 15:48:51 -070066import com.android.phone.settings.VoicemailProviderSettingsUtil;
Sailesh Nepal788959e2014-07-08 23:36:40 -070067import com.android.services.telephony.sip.SipUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068
Andrew Lee2170a972014-08-13 18:13:01 -070069import java.lang.String;
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";
Andrew Leee3c15212014-10-28 13:12:55 -0700117 // Extra on intent to Call Settings containing the id of the subscription to modify.
118 public static final String SUB_ID_EXTRA =
119 "com.android.phone.CallFeaturesSetting.SubscriptionId";
120 // Extra on intent to Call Settings containing the label of the subscription to modify.
121 public static final String SUB_LABEL_EXTRA =
122 "com.android.phone.CallFeaturesSetting.SubscriptionLabel";
123
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124 // Extra put in the return from VM provider config containing voicemail number to set
125 public static final String VM_NUMBER_EXTRA = "com.android.phone.VoicemailNumber";
126 // Extra put in the return from VM provider config containing call forwarding number to set
127 public static final String FWD_NUMBER_EXTRA = "com.android.phone.ForwardingNumber";
128 // Extra put in the return from VM provider config containing call forwarding number to set
129 public static final String FWD_NUMBER_TIME_EXTRA = "com.android.phone.ForwardingNumberTime";
130 // If the VM provider returns non null value in this extra we will force the user to
131 // choose another VM provider
132 public static final String SIGNOUT_EXTRA = "com.android.phone.Signout";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700133
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700134 // Key identifying the default vocie mail provider
135 public static final String DEFAULT_VM_PROVIDER_KEY = "";
136
137 /**
138 * String Extra put into ACTION_ADD_VOICEMAIL call to indicate which provider should be hidden
139 * in the list of providers presented to the user. This allows a provider which is being
140 * disabled (e.g. GV user logging out) to force the user to pick some other provider.
141 */
142 public static final String IGNORE_PROVIDER_EXTRA = "com.android.phone.ProviderToIgnore";
143
144 // string constants
145 private static final String NUM_PROJECTION[] = {CommonDataKinds.Phone.NUMBER};
146
147 // String keys for preference lookup
148 // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
Andrew Lee97708a42014-09-25 12:39:07 -0700149 private static final String VOICEMAIL_SETTING_SCREEN_PREF_KEY = "button_voicemail_category_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150 private static final String BUTTON_VOICEMAIL_KEY = "button_voicemail_key";
151 private static final String BUTTON_VOICEMAIL_PROVIDER_KEY = "button_voicemail_provider_key";
152 private static final String BUTTON_VOICEMAIL_SETTING_KEY = "button_voicemail_setting_key";
153 // New preference key for voicemail notification vibration
154 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY =
155 "button_voicemail_notification_vibrate_key";
156 // Old preference key for voicemail notification vibration. Used for migration to the new
157 // preference key only.
158 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY =
159 "button_voicemail_notification_vibrate_when_key";
160 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_RINGTONE_KEY =
161 "button_voicemail_notification_ringtone_key";
162 private static final String BUTTON_FDN_KEY = "button_fdn_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700163
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700164 private static final String BUTTON_DTMF_KEY = "button_dtmf_settings";
165 private static final String BUTTON_RETRY_KEY = "button_auto_retry_key";
166 private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
167 private static final String BUTTON_HAC_KEY = "button_hac_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168
169 private static final String BUTTON_GSM_UMTS_OPTIONS = "button_gsm_more_expand_key";
170 private static final String BUTTON_CDMA_OPTIONS = "button_cdma_more_expand_key";
171
Andrew Leedb2fe562014-09-03 15:40:43 -0700172 private static final String DEFAULT_OUTGOING_ACCOUNT_KEY = "default_outgoing_account";
Andrew Leece8ae2a2014-09-10 10:41:48 -0700173 private static final String PHONE_ACCOUNT_SETTINGS_KEY =
174 "phone_account_settings_preference_screen";
Andrew Leedb2fe562014-09-03 15:40:43 -0700175
Andrew Leedf14ead2014-10-17 14:22:52 -0700176 private static final String ENABLE_VIDEO_CALLING_KEY = "button_enable_video_calling";
177
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178 /** Event for Async voicemail change call */
179 private static final int EVENT_VOICEMAIL_CHANGED = 500;
180 private static final int EVENT_FORWARDING_CHANGED = 501;
181 private static final int EVENT_FORWARDING_GET_COMPLETED = 502;
182
Andrew Lee2170a972014-08-13 18:13:01 -0700183 private static final int MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY = 1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700184
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185 public static final String HAC_KEY = "HACSetting";
186 public static final String HAC_VAL_ON = "ON";
187 public static final String HAC_VAL_OFF = "OFF";
188
189 /** Handle to voicemail pref */
190 private static final int VOICEMAIL_PREF_ID = 1;
191 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
192
193 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700194 private AudioManager mAudioManager;
Andrew Lee88b51e22014-10-29 15:48:51 -0700195 private VoicemailProviderSettingsUtil mVmProviderSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700196
197 private static final int VM_NOCHANGE_ERROR = 400;
198 private static final int VM_RESPONSE_ERROR = 500;
199 private static final int FW_SET_RESPONSE_ERROR = 501;
200 private static final int FW_GET_RESPONSE_ERROR = 502;
201
202
203 // dialog identifiers for voicemail
204 private static final int VOICEMAIL_DIALOG_CONFIRM = 600;
205 private static final int VOICEMAIL_FWD_SAVING_DIALOG = 601;
206 private static final int VOICEMAIL_FWD_READING_DIALOG = 602;
207 private static final int VOICEMAIL_REVERTING_DIALOG = 603;
208
Andrew Leee438b312014-10-29 16:59:15 -0700209 /**
210 * @see CallForwardInfo#status
211 */
212 private static final int CALL_FORWARD_INFO_INACTIVE_STATUS = 0;
213
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700214 // voicemail notification vibration string constants
215 private static final String VOICEMAIL_VIBRATION_ALWAYS = "always";
216 private static final String VOICEMAIL_VIBRATION_NEVER = "never";
217
218 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
219
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700220 /** Whether dialpad plays DTMF tone or not. */
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700221 private CheckBoxPreference mButtonAutoRetry;
222 private CheckBoxPreference mButtonHAC;
223 private ListPreference mButtonDTMF;
224 private ListPreference mButtonTTY;
Andrew Leece8ae2a2014-09-10 10:41:48 -0700225 private Preference mPhoneAccountSettingsPreference;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700226 private ListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700227 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700228 private PreferenceScreen mVoicemailSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700229 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700230 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700231
232 private class VoiceMailProvider {
233 public VoiceMailProvider(String name, Intent intent) {
234 this.name = name;
235 this.intent = intent;
236 }
237 public String name;
238 public Intent intent;
239 }
240
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700241 /**
242 * Results of reading forwarding settings
243 */
244 private CallForwardInfo[] mForwardingReadResults = null;
245
246 /**
247 * Result of forwarding number change.
248 * Keys are reasons (eg. unconditional forwarding).
249 */
250 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
251
252 /**
253 * Expected CF read result types.
254 * This set keeps track of the CF types for which we've issued change
255 * commands so we can tell when we've received all of the responses.
256 */
257 private Collection<Integer> mExpectedChangeResultReasons = null;
258
259 /**
260 * Result of vm number change
261 */
262 private AsyncResult mVoicemailChangeResult = null;
263
264 /**
265 * Previous VM provider setting so we can return to it in case of failure.
266 */
267 private String mPreviousVMProviderKey = null;
268
269 /**
270 * Id of the dialog being currently shown.
271 */
272 private int mCurrentDialogId = 0;
273
274 /**
275 * Flag indicating that we are invoking settings for the voicemail provider programmatically
276 * due to vm provider change.
277 */
278 private boolean mVMProviderSettingsForced = false;
279
280 /**
281 * Flag indicating that we are making changes to vm or fwd numbers
282 * due to vm provider change.
283 */
284 private boolean mChangingVMorFwdDueToProviderChange = false;
285
286 /**
287 * True if we are in the process of vm & fwd number change and vm has already been changed.
288 * This is used to decide what to do in case of rollback.
289 */
290 private boolean mVMChangeCompletedSuccessfully = false;
291
292 /**
293 * True if we had full or partial failure setting forwarding numbers and so need to roll them
294 * back.
295 */
296 private boolean mFwdChangesRequireRollback = false;
297
298 /**
299 * Id of error msg to display to user once we are done reverting the VM provider to the previous
300 * one.
301 */
302 private int mVMOrFwdSetError = 0;
303
304 /**
305 * Data about discovered voice mail settings providers.
306 * Is populated by querying which activities can handle ACTION_CONFIGURE_VOICEMAIL.
307 * They key in this map is package name + activity name.
308 * We always add an entry for the default provider with a key of empty
309 * string and intent value of null.
310 * @see #initVoiceMailProviders()
311 */
312 private final Map<String, VoiceMailProvider> mVMProvidersData =
313 new HashMap<String, VoiceMailProvider>();
314
315 /** string to hold old voicemail number as it is being updated. */
316 private String mOldVmNumber;
317
318 // New call forwarding settings and vm number we will be setting
319 // Need to save these since before we get to saving we need to asynchronously
320 // query the existing forwarding settings.
321 private CallForwardInfo[] mNewFwdSettings;
322 private String mNewVMNumber;
323
324 private boolean mForeground;
325
326 @Override
327 public void onPause() {
328 super.onPause();
329 mForeground = false;
330 }
331
332 /**
333 * We have to pull current settings from the network for all kinds of
334 * voicemail providers so we can tell whether we have to update them,
335 * so use this bit to keep track of whether we're reading settings for the
336 * default provider and should therefore save them out when done.
337 */
338 private boolean mReadingSettingsForDefaultProvider = false;
339
Tyler Gunnbaee2952014-09-10 16:01:02 -0700340 /**
341 * Used to indicate that the voicemail preference should be shown.
342 */
343 private boolean mShowVoicemailPreference = false;
344
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700345 /*
346 * Click Listeners, handle click based on objects attached to UI.
347 */
348
349 // Click listener for all toggle events
350 @Override
351 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
352 if (preference == mSubMenuVoicemailSettings) {
353 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700354 } else if (preference == mButtonDTMF) {
355 return true;
356 } else if (preference == mButtonTTY) {
357 return true;
358 } else if (preference == mButtonAutoRetry) {
359 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
360 android.provider.Settings.Global.CALL_AUTO_RETRY,
361 mButtonAutoRetry.isChecked() ? 1 : 0);
362 return true;
363 } else if (preference == mButtonHAC) {
364 int hac = mButtonHAC.isChecked() ? 1 : 0;
365 // Update HAC value in Settings database
366 Settings.System.putInt(mPhone.getContext().getContentResolver(),
367 Settings.System.HEARING_AID, hac);
368
369 // Update HAC Value in AudioManager
370 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
371 return true;
372 } else if (preference == mVoicemailSettings) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700373 final Dialog dialog = mVoicemailSettings.getDialog();
374 if (dialog != null) {
375 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
376 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700377 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
378 if (preference.getIntent() != null) {
379 if (DBG) {
380 log("onPreferenceTreeClick: Invoking cfg intent "
381 + preference.getIntent().getPackage());
382 }
383
384 // onActivityResult() will be responsible for resetting some of variables.
385 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
386 return true;
387 } else {
388 if (DBG) {
389 log("onPreferenceTreeClick:"
390 + " No Intent is available. Use default behavior defined in xml.");
391 }
392
393 // There's no onActivityResult(), so we need to take care of some of variables
394 // which should be reset here.
395 mPreviousVMProviderKey = DEFAULT_VM_PROVIDER_KEY;
396 mVMProviderSettingsForced = false;
397
398 // This should let the preference use default behavior in the xml.
399 return false;
400 }
Andrew Lee97708a42014-09-25 12:39:07 -0700401 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700402 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
403 if (dialog != null) {
404 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
405 }
Andrew Lee97708a42014-09-25 12:39:07 -0700406 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700407 }
408 return false;
409 }
410
411 /**
412 * Implemented to support onPreferenceChangeListener to look for preference
413 * changes.
414 *
415 * @param preference is the preference to be changed
416 * @param objValue should be the value of the selection, NOT its localized
417 * display value.
418 */
419 @Override
420 public boolean onPreferenceChange(Preference preference, Object objValue) {
421 if (DBG) {
Andrew Leedf14ead2014-10-17 14:22:52 -0700422 log("onPreferenceChange(). preference: \"" + preference + "\""
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700423 + ", value: \"" + objValue + "\"");
424 }
Andrew Lee2170a972014-08-13 18:13:01 -0700425
426 if (preference == mButtonDTMF) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700427 int index = mButtonDTMF.findIndexOfValue((String) objValue);
428 Settings.System.putInt(mPhone.getContext().getContentResolver(),
429 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
430 } else if (preference == mButtonTTY) {
431 handleTTYChange(preference, objValue);
432 } else if (preference == mVoicemailProviders) {
433 final String newProviderKey = (String) objValue;
434 if (DBG) {
435 log("Voicemail Provider changes from \"" + mPreviousVMProviderKey
436 + "\" to \"" + newProviderKey + "\".");
437 }
438 // If previous provider key and the new one is same, we don't need to handle it.
439 if (mPreviousVMProviderKey.equals(newProviderKey)) {
440 if (DBG) log("No change is made toward VM provider setting.");
441 return true;
442 }
443 updateVMPreferenceWidgets(newProviderKey);
444
Andrew Leeb490d732014-10-27 15:00:41 -0700445 final VoicemailProviderSettings newProviderSettings =
Andrew Lee88b51e22014-10-29 15:48:51 -0700446 mVmProviderSettingsUtil.load(newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700447
Andrew Lee88b51e22014-10-29 15:48:51 -0700448 // If the user switches to a voice mail provider and we have numbers stored for it we
449 // will automatically change the phone's voice mail and forwarding number to the stored
450 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700451 if (newProviderSettings == null) {
452 // Force the user into a configuration of the chosen provider
453 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
454 mVMProviderSettingsForced = true;
455 simulatePreferenceClick(mVoicemailSettings);
456 } else {
457 if (DBG) log("Saved preferences found - switching to them");
458 // Set this flag so if we get a failure we revert to previous provider
459 mChangingVMorFwdDueToProviderChange = true;
460 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
461 }
Andrew Leedf14ead2014-10-17 14:22:52 -0700462 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700463 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
464 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
465 } else {
466 AlertDialog.Builder builder = new AlertDialog.Builder(this);
467 DialogInterface.OnClickListener networkSettingsClickListener =
468 new Dialog.OnClickListener() {
469 @Override
470 public void onClick(DialogInterface dialog, int which) {
471 startActivity(new Intent(mPhone.getContext(),
472 com.android.phone.MobileNetworkSettings.class));
473 }
474 };
475 builder.setMessage(getResources().getString(
476 R.string.enable_video_calling_dialog_msg))
477 .setNeutralButton(getResources().getString(
478 R.string.enable_video_calling_dialog_settings),
479 networkSettingsClickListener)
480 .setPositiveButton(android.R.string.ok, null)
481 .show();
482 return false;
483 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700484 }
485 // always let the preference setting proceed.
486 return true;
487 }
488
489 @Override
490 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
491 if (DBG) log("onPreferenceClick: request preference click on dialog close: " +
492 buttonClicked);
493 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
494 return;
495 }
496
497 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700498 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
499 mSubMenuVoicemailSettings.getPhoneNumber(),
500 VoicemailProviderSettings.NO_FORWARDING);
501 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700502 }
503 }
504
505 /**
506 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
507 * This method set the default values for the various
508 * EditPhoneNumberPreference dialogs.
509 */
510 @Override
511 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
512 if (preference == mSubMenuVoicemailSettings) {
513 // update the voicemail number field, which takes care of the
514 // mSubMenuVoicemailSettings itself, so we should return null.
515 if (DBG) log("updating default for voicemail dialog");
516 updateVoiceNumberField();
517 return null;
518 }
519
520 String vmDisplay = mPhone.getVoiceMailNumber();
521 if (TextUtils.isEmpty(vmDisplay)) {
522 // if there is no voicemail number, we just return null to
523 // indicate no contribution.
524 return null;
525 }
526
527 // Return the voicemail number prepended with "VM: "
528 if (DBG) log("updating default for call forwarding dialogs");
529 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
530 }
531
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700532 private void switchToPreviousVoicemailProvider() {
533 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
534 if (mPreviousVMProviderKey != null) {
535 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
536 // we have to revert with carrier
537 if (DBG) {
538 log("Needs to rollback."
539 + " mVMChangeCompletedSuccessfully=" + mVMChangeCompletedSuccessfully
540 + ", mFwdChangesRequireRollback=" + mFwdChangesRequireRollback);
541 }
542
543 showDialogIfForeground(VOICEMAIL_REVERTING_DIALOG);
Andrew Leeb490d732014-10-27 15:00:41 -0700544 final VoicemailProviderSettings prevSettings =
Andrew Lee88b51e22014-10-29 15:48:51 -0700545 mVmProviderSettingsUtil.load(mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700546 if (prevSettings == null) {
547 // prevSettings never becomes null since it should be already loaded!
Andrew Leeb490d732014-10-27 15:00:41 -0700548 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700549 + mPreviousVMProviderKey + "\" becomes null, which is unexpected.");
550 if (DBG) {
551 Log.e(LOG_TAG,
552 "mVMChangeCompletedSuccessfully: " + mVMChangeCompletedSuccessfully
553 + ", mFwdChangesRequireRollback: " + mFwdChangesRequireRollback);
554 }
555 }
556 if (mVMChangeCompletedSuccessfully) {
Andrew Leeb490d732014-10-27 15:00:41 -0700557 mNewVMNumber = prevSettings.getVoicemailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700558 Log.i(LOG_TAG, "VM change is already completed successfully."
559 + "Have to revert VM back to " + mNewVMNumber + " again.");
560 mPhone.setVoiceMailNumber(
561 mPhone.getVoiceMailAlphaTag().toString(),
562 mNewVMNumber,
563 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
564 }
565 if (mFwdChangesRequireRollback) {
566 Log.i(LOG_TAG, "Requested to rollback Fwd changes.");
Andrew Leeb490d732014-10-27 15:00:41 -0700567 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700568 if (prevFwdSettings != null) {
569 Map<Integer, AsyncResult> results =
570 mForwardingChangeResults;
571 resetForwardingChangeState();
572 for (int i = 0; i < prevFwdSettings.length; i++) {
573 CallForwardInfo fi = prevFwdSettings[i];
574 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
575 // Only revert the settings for which the update
576 // succeeded
577 AsyncResult result = results.get(fi.reason);
578 if (result != null && result.exception == null) {
579 mExpectedChangeResultReasons.add(fi.reason);
580 mPhone.setCallForwardingOption(
581 (fi.status == 1 ?
582 CommandsInterface.CF_ACTION_REGISTRATION :
583 CommandsInterface.CF_ACTION_DISABLE),
584 fi.reason,
585 fi.number,
586 fi.timeSeconds,
587 mRevertOptionComplete.obtainMessage(
588 EVENT_FORWARDING_CHANGED, i, 0));
589 }
590 }
591 }
592 }
593 } else {
594 if (DBG) log("No need to revert");
595 onRevertDone();
596 }
597 }
598 }
599
600 private void onRevertDone() {
601 if (DBG) log("Flipping provider key back to " + mPreviousVMProviderKey);
602 mVoicemailProviders.setValue(mPreviousVMProviderKey);
603 updateVMPreferenceWidgets(mPreviousVMProviderKey);
604 updateVoiceNumberField();
605 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800606 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700607 mVMOrFwdSetError = 0;
608 }
609 }
610
611 @Override
612 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
613 if (DBG) {
614 log("onActivityResult: requestCode: " + requestCode
615 + ", resultCode: " + resultCode
616 + ", data: " + data);
617 }
618 // there are cases where the contact picker may end up sending us more than one
619 // request. We want to ignore the request if we're not in the correct state.
620 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
621 boolean failure = false;
622
623 // No matter how the processing of result goes lets clear the flag
624 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
625 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
626 mVMProviderSettingsForced = false;
627
628 String vmNum = null;
629 if (resultCode != RESULT_OK) {
630 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
631 failure = true;
632 } else {
633 if (data == null) {
634 if (DBG) log("onActivityResult: vm provider cfg result has no data");
635 failure = true;
636 } else {
637 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
638 if (DBG) log("Provider requested signout");
639 if (isVMProviderSettingsForced) {
640 if (DBG) log("Going back to previous provider on signout");
641 switchToPreviousVoicemailProvider();
642 } else {
643 final String victim = getCurrentVoicemailProviderKey();
644 if (DBG) log("Relaunching activity and ignoring " + victim);
645 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
646 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
647 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
648 this.startActivity(i);
649 }
650 return;
651 }
652 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
653 if (vmNum == null || vmNum.length() == 0) {
654 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
655 failure = true;
656 }
657 }
658 }
659 if (failure) {
660 if (DBG) log("Failure in return from voicemail provider");
661 if (isVMProviderSettingsForced) {
662 switchToPreviousVoicemailProvider();
663 } else {
664 if (DBG) log("Not switching back the provider since this is not forced config");
665 }
666 return;
667 }
668 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
669 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
670
Santos Cordonda120f42014-08-06 04:44:34 -0700671 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700672 // send it to the provider when it's config is invoked so it can use this as default
673 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
674
675 if (DBG) log("onActivityResult: vm provider cfg result " +
676 (fwdNum != null ? "has" : " does not have") + " forwarding number");
677 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700678 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700679 return;
680 }
681
682 if (requestCode == VOICEMAIL_PREF_ID) {
683 if (resultCode != RESULT_OK) {
684 if (DBG) log("onActivityResult: contact picker result not OK.");
685 return;
686 }
687
688 Cursor cursor = null;
689 try {
690 cursor = getContentResolver().query(data.getData(),
691 NUM_PROJECTION, null, null, null);
692 if ((cursor == null) || (!cursor.moveToFirst())) {
693 if (DBG) log("onActivityResult: bad contact data, no results found.");
694 return;
695 }
696 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
697 return;
698 } finally {
699 if (cursor != null) {
700 cursor.close();
701 }
702 }
703 }
704
705 super.onActivityResult(requestCode, resultCode, data);
706 }
707
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700708 /**
709 * Wrapper around showDialog() that will silently do nothing if we're
710 * not in the foreground.
711 *
712 * This is useful here because most of the dialogs we display from
713 * this class are triggered by asynchronous events (like
714 * success/failure messages from the telephony layer) and it's
715 * possible for those events to come in even after the user has gone
716 * to a different screen.
717 */
718 // TODO: this is too brittle: it's still easy to accidentally add new
719 // code here that calls showDialog() directly (which will result in a
720 // WindowManager$BadTokenException if called after the activity has
721 // been stopped.)
722 //
723 // It would be cleaner to do the "if (mForeground)" check in one
724 // central place, maybe by using a single Handler for all asynchronous
725 // events (and have *that* discard events if we're not in the
726 // foreground.)
727 //
728 // Unfortunately it's not that simple, since we sometimes need to do
729 // actual work to handle these events whether or not we're in the
730 // foreground (see the Handler code in mSetOptionComplete for
731 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800732 //
733 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
734 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700735 private void showDialogIfForeground(int id) {
736 if (mForeground) {
737 showDialog(id);
738 }
739 }
740
741 private void dismissDialogSafely(int id) {
742 try {
743 dismissDialog(id);
744 } catch (IllegalArgumentException e) {
745 // This is expected in the case where we were in the background
746 // at the time we would normally have shown the dialog, so we didn't
747 // show it.
748 }
749 }
750
Andrew Leeb490d732014-10-27 15:00:41 -0700751 private void saveVoiceMailAndForwardingNumber(
752 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700753 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700754 mNewVMNumber = newSettings.getVoicemailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700755 // empty vm number == clearing the vm number ?
756 if (mNewVMNumber == null) {
757 mNewVMNumber = "";
758 }
759
Andrew Leeb490d732014-10-27 15:00:41 -0700760 mNewFwdSettings = newSettings.getForwardingSettings();
761 if (DBG) log("newFwdNumber "
762 + String.valueOf((mNewFwdSettings != null ? mNewFwdSettings.length : 0))
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700763 + " settings");
764
765 // No fwd settings on CDMA
766 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
767 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700768 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700769 }
770
Andrew Leee3c15212014-10-28 13:12:55 -0700771 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700772 if (mNewVMNumber.equals(mOldVmNumber)
773 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Leeab082272014-11-04 15:50:42 -0800774 showDialogIfForeground(VM_NOCHANGE_ERROR);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700775 return;
776 }
777
Andrew Lee88b51e22014-10-29 15:48:51 -0700778 mVmProviderSettingsUtil.save(key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700779 mVMChangeCompletedSuccessfully = false;
780 mFwdChangesRequireRollback = false;
781 mVMOrFwdSetError = 0;
782 if (!key.equals(mPreviousVMProviderKey)) {
783 mReadingSettingsForDefaultProvider =
784 mPreviousVMProviderKey.equals(DEFAULT_VM_PROVIDER_KEY);
785 if (DBG) log("Reading current forwarding settings");
Andrew Leeb490d732014-10-27 15:00:41 -0700786 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
787 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
788 for (int i = 0; i < mForwardingReadResults.length; i++) {
789 mPhone.getCallForwardingOption(
790 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700791 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
792 }
793 showDialogIfForeground(VOICEMAIL_FWD_READING_DIALOG);
794 } else {
795 saveVoiceMailAndForwardingNumberStage2();
796 }
797 }
798
799 private final Handler mGetOptionComplete = new Handler() {
800 @Override
801 public void handleMessage(Message msg) {
802 AsyncResult result = (AsyncResult) msg.obj;
803 switch (msg.what) {
804 case EVENT_FORWARDING_GET_COMPLETED:
805 handleForwardingSettingsReadResult(result, msg.arg1);
806 break;
807 }
808 }
809 };
810
811 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
812 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
813 Throwable error = null;
814 if (ar.exception != null) {
815 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" +
816 ar.exception.getMessage());
817 error = ar.exception;
818 }
819 if (ar.userObj instanceof Throwable) {
820 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" +
821 ((Throwable)ar.userObj).getMessage());
822 error = (Throwable)ar.userObj;
823 }
824
825 // We may have already gotten an error and decided to ignore the other results.
826 if (mForwardingReadResults == null) {
827 if (DBG) Log.d(LOG_TAG, "ignoring fwd reading result: " + idx);
828 return;
829 }
830
831 // In case of error ignore other results, show an error dialog
832 if (error != null) {
833 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
834 mForwardingReadResults = null;
835 dismissDialogSafely(VOICEMAIL_FWD_READING_DIALOG);
Andrew Leeab082272014-11-04 15:50:42 -0800836 showDialogIfForeground(FW_GET_RESPONSE_ERROR);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700837 return;
838 }
839
840 // Get the forwarding info
841 final CallForwardInfo cfInfoArray[] = (CallForwardInfo[]) ar.result;
842 CallForwardInfo fi = null;
843 for (int i = 0 ; i < cfInfoArray.length; i++) {
844 if ((cfInfoArray[i].serviceClass & CommandsInterface.SERVICE_CLASS_VOICE) != 0) {
845 fi = cfInfoArray[i];
846 break;
847 }
848 }
849 if (fi == null) {
850
851 // In case we go nothing it means we need this reason disabled
852 // so create a CallForwardInfo for capturing this
853 if (DBG) Log.d(LOG_TAG, "Creating default info for " + idx);
854 fi = new CallForwardInfo();
855 fi.status = 0;
Andrew Leeb490d732014-10-27 15:00:41 -0700856 fi.reason = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx];
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700857 fi.serviceClass = CommandsInterface.SERVICE_CLASS_VOICE;
858 } else {
859 // if there is not a forwarding number, ensure the entry is set to "not active."
860 if (fi.number == null || fi.number.length() == 0) {
861 fi.status = 0;
862 }
863
864 if (DBG) Log.d(LOG_TAG, "Got " + fi.toString() + " for " + idx);
865 }
866 mForwardingReadResults[idx] = fi;
867
868 // Check if we got all the results already
869 boolean done = true;
870 for (int i = 0; i < mForwardingReadResults.length; i++) {
871 if (mForwardingReadResults[i] == null) {
872 done = false;
873 break;
874 }
875 }
876 if (done) {
877 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
878 dismissDialogSafely(VOICEMAIL_FWD_READING_DIALOG);
879 if (mReadingSettingsForDefaultProvider) {
Andrew Lee88b51e22014-10-29 15:48:51 -0700880 mVmProviderSettingsUtil.save(DEFAULT_VM_PROVIDER_KEY,
881 new VoicemailProviderSettings(this.mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700882 mReadingSettingsForDefaultProvider = false;
883 }
884 saveVoiceMailAndForwardingNumberStage2();
885 } else {
886 if (DBG) Log.d(LOG_TAG, "Not done receiving fwd info");
887 }
888 }
889
890 private CallForwardInfo infoForReason(CallForwardInfo[] infos, int reason) {
891 CallForwardInfo result = null;
892 if (null != infos) {
893 for (CallForwardInfo info : infos) {
894 if (info.reason == reason) {
895 result = info;
896 break;
897 }
898 }
899 }
900 return result;
901 }
902
Andrew Leee438b312014-10-29 16:59:15 -0700903 private boolean isUpdateRequired(CallForwardInfo oldInfo, CallForwardInfo newInfo) {
904 if (oldInfo == null) {
905 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700906 }
Andrew Leee438b312014-10-29 16:59:15 -0700907
908 // If we're disabling a type of forwarding, don't make any change if it's already disabled.
909 if (newInfo.status == CALL_FORWARD_INFO_INACTIVE_STATUS
910 && oldInfo.status == CALL_FORWARD_INFO_INACTIVE_STATUS) {
911 return false;
912 }
913
914 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700915 }
916
917 private void resetForwardingChangeState() {
918 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
919 mExpectedChangeResultReasons = new HashSet<Integer>();
920 }
921
922 // Called after we are done saving the previous forwarding settings if
923 // we needed.
924 private void saveVoiceMailAndForwardingNumberStage2() {
925 mForwardingChangeResults = null;
926 mVoicemailChangeResult = null;
Andrew Leeb490d732014-10-27 15:00:41 -0700927 if (mNewFwdSettings != VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700928 resetForwardingChangeState();
929 for (int i = 0; i < mNewFwdSettings.length; i++) {
930 CallForwardInfo fi = mNewFwdSettings[i];
931
932 final boolean doUpdate = isUpdateRequired(infoForReason(
933 mForwardingReadResults, fi.reason), fi);
934
935 if (doUpdate) {
936 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
937 mExpectedChangeResultReasons.add(i);
938
939 mPhone.setCallForwardingOption(
940 fi.status == 1 ?
941 CommandsInterface.CF_ACTION_REGISTRATION :
942 CommandsInterface.CF_ACTION_DISABLE,
943 fi.reason,
944 fi.number,
945 fi.timeSeconds,
946 mSetOptionComplete.obtainMessage(
947 EVENT_FORWARDING_CHANGED, fi.reason, 0));
948 }
949 }
950 showDialogIfForeground(VOICEMAIL_FWD_SAVING_DIALOG);
951 } else {
952 if (DBG) log("Not touching fwd #");
953 setVMNumberWithCarrier();
954 }
955 }
956
957 private void setVMNumberWithCarrier() {
958 if (DBG) log("save voicemail #: " + mNewVMNumber);
959 mPhone.setVoiceMailNumber(
960 mPhone.getVoiceMailAlphaTag().toString(),
961 mNewVMNumber,
962 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
963 }
964
965 /**
966 * Callback to handle option update completions
967 */
968 private final Handler mSetOptionComplete = new Handler() {
969 @Override
970 public void handleMessage(Message msg) {
971 AsyncResult result = (AsyncResult) msg.obj;
972 boolean done = false;
973 switch (msg.what) {
974 case EVENT_VOICEMAIL_CHANGED:
975 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700976 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700977 done = true;
978 break;
979 case EVENT_FORWARDING_CHANGED:
980 mForwardingChangeResults.put(msg.arg1, result);
981 if (result.exception != null) {
982 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
983 result.exception.getMessage());
984 } else {
985 if (DBG) log("Success in setting fwd# " + msg.arg1);
986 }
Andrew Leee438b312014-10-29 16:59:15 -0700987 if (isForwardingCompleted()) {
988 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700989 if (DBG) log("Overall fwd changes completed ok, starting vm change");
990 setVMNumberWithCarrier();
991 } else {
992 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
993 "Check if we need to try rollback for some settings.");
994 mFwdChangesRequireRollback = false;
995 Iterator<Map.Entry<Integer,AsyncResult>> it =
996 mForwardingChangeResults.entrySet().iterator();
997 while (it.hasNext()) {
998 Map.Entry<Integer,AsyncResult> entry = it.next();
999 if (entry.getValue().exception == null) {
1000 // If at least one succeeded we have to revert
1001 Log.i(LOG_TAG, "Rollback will be required");
1002 mFwdChangesRequireRollback = true;
1003 break;
1004 }
1005 }
1006 if (!mFwdChangesRequireRollback) {
1007 Log.i(LOG_TAG, "No rollback needed.");
1008 }
1009 done = true;
1010 }
1011 }
1012 break;
1013 default:
1014 // TODO: should never reach this, may want to throw exception
1015 }
1016 if (done) {
1017 if (DBG) log("All VM provider related changes done");
1018 if (mForwardingChangeResults != null) {
1019 dismissDialogSafely(VOICEMAIL_FWD_SAVING_DIALOG);
1020 }
Andrew Leee438b312014-10-29 16:59:15 -07001021 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001022 }
1023 }
1024 };
1025
1026 /**
1027 * Callback to handle option revert completions
1028 */
1029 private final Handler mRevertOptionComplete = new Handler() {
1030 @Override
1031 public void handleMessage(Message msg) {
1032 AsyncResult result = (AsyncResult) msg.obj;
1033 switch (msg.what) {
1034 case EVENT_VOICEMAIL_CHANGED:
1035 mVoicemailChangeResult = result;
1036 if (DBG) log("VM revert complete msg");
1037 break;
1038 case EVENT_FORWARDING_CHANGED:
1039 mForwardingChangeResults.put(msg.arg1, result);
1040 if (result.exception != null) {
1041 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
1042 result.exception.getMessage());
1043 } else {
1044 if (DBG) log("Success in reverting fwd# " + msg.arg1);
1045 }
1046 if (DBG) log("FWD revert complete msg ");
1047 break;
1048 default:
1049 // TODO: should never reach this, may want to throw exception
1050 }
1051 final boolean done =
1052 (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null) &&
Andrew Leee438b312014-10-29 16:59:15 -07001053 (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001054 if (done) {
1055 if (DBG) log("All VM reverts done");
1056 dismissDialogSafely(VOICEMAIL_REVERTING_DIALOG);
1057 onRevertDone();
1058 }
1059 }
1060 };
1061
1062 /**
Andrew Leee438b312014-10-29 16:59:15 -07001063 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001064 */
Andrew Leee438b312014-10-29 16:59:15 -07001065 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001066 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -07001067 return true;
1068 }
1069
1070 for (Integer reason : mExpectedChangeResultReasons) {
1071 if (mForwardingChangeResults.get(reason) == null) {
1072 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001073 }
1074 }
Andrew Leee438b312014-10-29 16:59:15 -07001075
1076 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001077 }
Andrew Leee438b312014-10-29 16:59:15 -07001078
1079 private boolean isFwdChangeSuccess() {
1080 if (mForwardingChangeResults == null) {
1081 return true;
1082 }
1083
1084 for (AsyncResult result : mForwardingChangeResults.values()) {
1085 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001086 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -07001087 String msg = exception.getMessage();
1088 msg = (msg != null) ? msg : "";
1089 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
1090 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001091 }
1092 }
Andrew Leee438b312014-10-29 16:59:15 -07001093 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001094 }
1095
Andrew Leee438b312014-10-29 16:59:15 -07001096 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001097 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -07001098 String msg = mVoicemailChangeResult.exception.getMessage();
1099 msg = (msg != null) ? msg : "";
1100 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
1101 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001102 }
Andrew Leee438b312014-10-29 16:59:15 -07001103
1104 if (DBG) log("VM change completed successfully.");
1105 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001106 }
1107
Andrew Leee438b312014-10-29 16:59:15 -07001108 private void handleSetVmOrFwdMessage() {
1109 if (DBG) log("handleSetVMMessage: set VM request complete");
1110
1111 if (!isFwdChangeSuccess()) {
Andrew Leeab082272014-11-04 15:50:42 -08001112 handleVmOrFwdSetError(FW_SET_RESPONSE_ERROR);
Andrew Leee438b312014-10-29 16:59:15 -07001113 } else if (!isVmChangeSuccess()) {
Andrew Leeab082272014-11-04 15:50:42 -08001114 handleVmOrFwdSetError(VM_RESPONSE_ERROR);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001115 } else {
Andrew Leee438b312014-10-29 16:59:15 -07001116 if (DBG) log("change VM success!");
Andrew Leeab082272014-11-04 15:50:42 -08001117 handleVmAndFwdSetSuccess(VOICEMAIL_DIALOG_CONFIRM);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001118 }
1119 }
1120
1121 /**
1122 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
1123 * changes to those settings and show "failure" dialog.
1124 *
Andrew Leeab082272014-11-04 15:50:42 -08001125 * @param dialogId ID of the dialog to show for the specific error case. Either
1126 * {@link #FW_SET_RESPONSE_ERROR} or {@link #VM_RESPONSE_ERROR}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001127 */
Andrew Leeab082272014-11-04 15:50:42 -08001128 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001129 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -08001130 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001131 mChangingVMorFwdDueToProviderChange = false;
1132 switchToPreviousVoicemailProvider();
1133 return;
1134 }
1135 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001136 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001137 updateVoiceNumberField();
1138 }
1139
1140 /**
1141 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1142 * This updates a bunch of variables and show "success" dialog.
1143 */
Andrew Leeab082272014-11-04 15:50:42 -08001144 private void handleVmAndFwdSetSuccess(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001145 if (DBG) {
Andrew Leee438b312014-10-29 16:59:15 -07001146 log("handleVmAndFwdSetSuccess(). current voicemail provider key: "
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001147 + getCurrentVoicemailProviderKey());
1148 }
1149 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1150 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001151 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001152 updateVoiceNumberField();
1153 }
1154
1155 /**
1156 * Update the voicemail number from what we've recorded on the sim.
1157 */
1158 private void updateVoiceNumberField() {
1159 if (DBG) {
1160 log("updateVoiceNumberField(). mSubMenuVoicemailSettings=" + mSubMenuVoicemailSettings);
1161 }
1162 if (mSubMenuVoicemailSettings == null) {
1163 return;
1164 }
1165
1166 mOldVmNumber = mPhone.getVoiceMailNumber();
1167 if (mOldVmNumber == null) {
1168 mOldVmNumber = "";
1169 }
1170 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1171 final String summary = (mOldVmNumber.length() > 0) ? mOldVmNumber :
1172 getString(R.string.voicemail_number_not_set);
1173 mSubMenuVoicemailSettings.setSummary(summary);
1174 }
1175
1176 /*
1177 * Helper Methods for Activity class.
1178 * The initial query commands are split into two pieces now
1179 * for individual expansion. This combined with the ability
1180 * to cancel queries allows for a much better user experience,
1181 * and also ensures that the user only waits to update the
1182 * data that is relevant.
1183 */
1184
1185 @Override
1186 protected void onPrepareDialog(int id, Dialog dialog) {
1187 super.onPrepareDialog(id, dialog);
1188 mCurrentDialogId = id;
1189 }
1190
1191 // dialog creation method, called by showDialog()
1192 @Override
1193 protected Dialog onCreateDialog(int id) {
1194 if ((id == VM_RESPONSE_ERROR) || (id == VM_NOCHANGE_ERROR) ||
1195 (id == FW_SET_RESPONSE_ERROR) || (id == FW_GET_RESPONSE_ERROR) ||
1196 (id == VOICEMAIL_DIALOG_CONFIRM)) {
1197
1198 AlertDialog.Builder b = new AlertDialog.Builder(this);
1199
1200 int msgId;
1201 int titleId = R.string.error_updating_title;
1202 switch (id) {
1203 case VOICEMAIL_DIALOG_CONFIRM:
1204 msgId = R.string.vm_changed;
1205 titleId = R.string.voicemail;
1206 // Set Button 2
1207 b.setNegativeButton(R.string.close_dialog, this);
1208 break;
1209 case VM_NOCHANGE_ERROR:
1210 // even though this is technically an error,
1211 // keep the title friendly.
1212 msgId = R.string.no_change;
1213 titleId = R.string.voicemail;
1214 // Set Button 2
1215 b.setNegativeButton(R.string.close_dialog, this);
1216 break;
1217 case VM_RESPONSE_ERROR:
1218 msgId = R.string.vm_change_failed;
1219 // Set Button 1
1220 b.setPositiveButton(R.string.close_dialog, this);
1221 break;
1222 case FW_SET_RESPONSE_ERROR:
1223 msgId = R.string.fw_change_failed;
1224 // Set Button 1
1225 b.setPositiveButton(R.string.close_dialog, this);
1226 break;
1227 case FW_GET_RESPONSE_ERROR:
1228 msgId = R.string.fw_get_in_vm_failed;
1229 b.setPositiveButton(R.string.alert_dialog_yes, this);
1230 b.setNegativeButton(R.string.alert_dialog_no, this);
1231 break;
1232 default:
1233 msgId = R.string.exception_error;
1234 // Set Button 3, tells the activity that the error is
1235 // not recoverable on dialog exit.
1236 b.setNeutralButton(R.string.close_dialog, this);
1237 break;
1238 }
1239
1240 b.setTitle(getText(titleId));
1241 String message = getText(msgId).toString();
1242 b.setMessage(message);
1243 b.setCancelable(false);
1244 AlertDialog dialog = b.create();
1245
1246 // make the dialog more obvious by bluring the background.
1247 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
1248
1249 return dialog;
1250 } else if (id == VOICEMAIL_FWD_SAVING_DIALOG || id == VOICEMAIL_FWD_READING_DIALOG ||
1251 id == VOICEMAIL_REVERTING_DIALOG) {
1252 ProgressDialog dialog = new ProgressDialog(this);
1253 dialog.setTitle(getText(R.string.updating_title));
1254 dialog.setIndeterminate(true);
1255 dialog.setCancelable(false);
1256 dialog.setMessage(getText(
1257 id == VOICEMAIL_FWD_SAVING_DIALOG ? R.string.updating_settings :
1258 (id == VOICEMAIL_REVERTING_DIALOG ? R.string.reverting_settings :
1259 R.string.reading_settings)));
1260 return dialog;
1261 }
1262
1263
1264 return null;
1265 }
1266
1267 // This is a method implemented for DialogInterface.OnClickListener.
1268 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1269 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1270 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1271 public void onClick(DialogInterface dialog, int which) {
1272 dialog.dismiss();
1273 switch (which){
1274 case DialogInterface.BUTTON_NEUTRAL:
1275 if (DBG) log("Neutral button");
1276 break;
1277 case DialogInterface.BUTTON_NEGATIVE:
1278 if (DBG) log("Negative button");
1279 if (mCurrentDialogId == FW_GET_RESPONSE_ERROR) {
1280 // We failed to get current forwarding settings and the user
1281 // does not wish to continue.
1282 switchToPreviousVoicemailProvider();
1283 }
1284 break;
1285 case DialogInterface.BUTTON_POSITIVE:
1286 if (DBG) log("Positive button");
1287 if (mCurrentDialogId == FW_GET_RESPONSE_ERROR) {
1288 // We failed to get current forwarding settings but the user
1289 // wishes to continue changing settings to the new vm provider
1290 saveVoiceMailAndForwardingNumberStage2();
1291 } else {
1292 finish();
1293 }
1294 return;
1295 default:
1296 // just let the dialog close and go back to the input
1297 }
1298 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1299 // with settings UI. If we were called to explicitly configure voice mail then
1300 // we finish the settings activity here to come back to whatever the user was doing.
1301 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1302 finish();
1303 }
1304 }
1305
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001306 /*
1307 * Activity class methods
1308 */
1309
1310 @Override
1311 protected void onCreate(Bundle icicle) {
1312 super.onCreate(icicle);
1313 if (DBG) log("onCreate(). Intent: " + getIntent());
1314 mPhone = PhoneGlobals.getPhone();
Tyler Gunnbaee2952014-09-10 16:01:02 -07001315 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andrew Lee88b51e22014-10-29 15:48:51 -07001316 mVmProviderSettingsUtil = new VoicemailProviderSettingsUtil(getApplicationContext());
Tyler Gunnbaee2952014-09-10 16:01:02 -07001317
Tyler Gunnbaee2952014-09-10 16:01:02 -07001318 // Show the voicemail preference in onResume if the calling intent specifies the
1319 // ACTION_ADD_VOICEMAIL action.
1320 mShowVoicemailPreference = (icicle == null) &&
1321 getIntent().getAction().equals(ACTION_ADD_VOICEMAIL);
1322 }
1323
1324 private void initPhoneAccountPreferences() {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001325 mPhoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001326
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001327 TelecomManager telecomManager = TelecomManager.from(this);
Andrew Lee93c345f2014-10-27 15:25:07 -07001328 TelephonyManager telephonyManager =
1329 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001330
Andrew Lee93c345f2014-10-27 15:25:07 -07001331 if ((telecomManager.getSimCallManagers().isEmpty() && !SipUtil.isVoipSupported(this))
1332 || telephonyManager.getPhoneCount() > 1) {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001333 getPreferenceScreen().removePreference(mPhoneAccountSettingsPreference);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001334 }
1335 }
1336
1337 private boolean canLaunchIntent(Intent intent) {
1338 PackageManager pm = getPackageManager();
1339 return pm.resolveActivity(intent, PackageManager.GET_ACTIVITIES) != null;
1340 }
1341
Tyler Gunnbaee2952014-09-10 16:01:02 -07001342 @Override
1343 protected void onResume() {
1344 super.onResume();
1345 mForeground = true;
1346
1347 PreferenceScreen preferenceScreen = getPreferenceScreen();
1348 if (preferenceScreen != null) {
1349 preferenceScreen.removeAll();
1350 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001351
1352 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Leedb2fe562014-09-03 15:40:43 -07001353 initPhoneAccountPreferences();
1354
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001355 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001356 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1357 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1358 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1359 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001360
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001361 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1362 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1363 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
1364 mButtonTTY = (ListPreference) findPreference(BUTTON_TTY_KEY);
1365 mVoicemailProviders = (ListPreference) findPreference(BUTTON_VOICEMAIL_PROVIDER_KEY);
Andrew Lee312e8172014-10-23 17:01:36 -07001366 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001367
Andrew Lee2c027892014-10-29 11:29:54 -07001368 mVoicemailProviders.setOnPreferenceChangeListener(this);
1369 mVoicemailSettingsScreen =
1370 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
1371 mVoicemailSettings = (PreferenceScreen)findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
1372 mVoicemailNotificationVibrate =
1373 (CheckBoxPreference) findPreference(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY);
1374 initVoiceMailProviders();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001375
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001376
Andrew Lee64a7d792014-10-15 17:38:38 -07001377 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1378 mButtonDTMF.setOnPreferenceChangeListener(this);
1379 int dtmf = Settings.System.getInt(getContentResolver(),
1380 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1381 mButtonDTMF.setValueIndex(dtmf);
1382 } else {
1383 prefSet.removePreference(mButtonDTMF);
1384 mButtonDTMF = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001385 }
1386
Andrew Lee64a7d792014-10-15 17:38:38 -07001387 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1388 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1389 int autoretry = Settings.Global.getInt(
1390 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1391 mButtonAutoRetry.setChecked(autoretry != 0);
1392 } else {
1393 prefSet.removePreference(mButtonAutoRetry);
1394 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001395 }
1396
Andrew Lee64a7d792014-10-15 17:38:38 -07001397 if (getResources().getBoolean(R.bool.hac_enabled)) {
1398 mButtonHAC.setOnPreferenceChangeListener(this);
1399 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1400 mButtonHAC.setChecked(hac != 0);
1401 } else {
1402 prefSet.removePreference(mButtonHAC);
1403 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001404 }
1405
Andrew Lee64a7d792014-10-15 17:38:38 -07001406 TelecomManager telecomManager = TelecomManager.from(this);
1407 if (telecomManager != null && telecomManager.isTtySupported()) {
1408 mButtonTTY.setOnPreferenceChangeListener(this);
1409 int settingsTtyMode = Settings.Secure.getInt(getContentResolver(),
1410 Settings.Secure.PREFERRED_TTY_MODE,
1411 TelecomManager.TTY_MODE_OFF);
1412 mButtonTTY.setValue(Integer.toString(settingsTtyMode));
1413 updatePreferredTtyModeSummary(settingsTtyMode);
1414 } else {
1415 prefSet.removePreference(mButtonTTY);
1416 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001417 }
1418
1419 if (!getResources().getBoolean(R.bool.world_phone)) {
1420 Preference options = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
Andrew Lee2170a972014-08-13 18:13:01 -07001421 if (options != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001422 prefSet.removePreference(options);
Andrew Lee2170a972014-08-13 18:13:01 -07001423 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001424 options = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
Andrew Lee2170a972014-08-13 18:13:01 -07001425 if (options != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001426 prefSet.removePreference(options);
Andrew Lee2170a972014-08-13 18:13:01 -07001427 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001428
1429 int phoneType = mPhone.getPhoneType();
1430 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1431 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001432 if (fdnButton != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001433 prefSet.removePreference(fdnButton);
Andrew Lee2170a972014-08-13 18:13:01 -07001434 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001435 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1436 addPreferencesFromResource(R.xml.cdma_call_privacy);
1437 }
1438 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
Andrew Lee2170a972014-08-13 18:13:01 -07001439 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
Etan Cohen0ca1c802014-07-07 15:35:48 -07001440 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1441 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001442 } else {
1443 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1444 }
1445 }
1446
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001447 // check the intent that started this activity and pop up the voicemail
1448 // dialog if we've been asked to.
1449 // If we have at least one non default VM provider registered then bring up
1450 // the selection for the VM provider, otherwise bring up a VM number dialog.
1451 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001452 if (mShowVoicemailPreference) {
Tyler Gunnbaee2952014-09-10 16:01:02 -07001453 if (DBG) {
1454 log("ACTION_ADD_VOICEMAIL Intent is thrown. current VM data size: "
1455 + mVMProvidersData.size());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001456 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001457 if (mVMProvidersData.size() > 1) {
1458 simulatePreferenceClick(mVoicemailProviders);
1459 } else {
1460 onPreferenceChange(mVoicemailProviders, DEFAULT_VM_PROVIDER_KEY);
1461 mVoicemailProviders.setValue(DEFAULT_VM_PROVIDER_KEY);
1462 }
1463 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001464 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001465
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001466 updateVoiceNumberField();
1467 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001468
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001469 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
1470 mPhone.getContext());
1471 if (migrateVoicemailVibrationSettingsIfNeeded(prefs)) {
1472 mVoicemailNotificationVibrate.setChecked(prefs.getBoolean(
1473 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, false));
1474 }
1475
Andrew Lee312e8172014-10-23 17:01:36 -07001476 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1477 boolean currentValue =
1478 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1479 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1480 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001481 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1482 } else {
1483 prefSet.removePreference(mEnableVideoCalling);
1484 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001485 }
1486
1487 // Migrate settings from BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY to
1488 // BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, if the latter does not exist.
1489 // Returns true if migration was performed.
1490 public static boolean migrateVoicemailVibrationSettingsIfNeeded(SharedPreferences prefs) {
1491 if (!prefs.contains(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY)) {
1492 String vibrateWhen = prefs.getString(
1493 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY, VOICEMAIL_VIBRATION_NEVER);
1494 // If vibrateWhen is always, then voicemailVibrate should be True.
1495 // otherwise if vibrateWhen is "only in silent mode", or "never", then
1496 // voicemailVibrate = False.
1497 boolean voicemailVibrate = vibrateWhen.equals(VOICEMAIL_VIBRATION_ALWAYS);
1498 final SharedPreferences.Editor editor = prefs.edit();
1499 editor.putBoolean(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, voicemailVibrate);
1500 editor.commit();
1501 return true;
1502 }
1503 return false;
1504 }
1505
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001506 private void handleTTYChange(Preference preference, Object objValue) {
1507 int buttonTtyMode;
1508 buttonTtyMode = Integer.valueOf((String) objValue).intValue();
1509 int settingsTtyMode = android.provider.Settings.Secure.getInt(
1510 getContentResolver(),
Sailesh Nepalbf900542014-07-15 16:18:32 -07001511 android.provider.Settings.Secure.PREFERRED_TTY_MODE,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001512 TelecomManager.TTY_MODE_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001513 if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
1514 Integer.toString(buttonTtyMode));
1515
1516 if (buttonTtyMode != settingsTtyMode) {
1517 switch(buttonTtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001518 case TelecomManager.TTY_MODE_OFF:
1519 case TelecomManager.TTY_MODE_FULL:
1520 case TelecomManager.TTY_MODE_HCO:
1521 case TelecomManager.TTY_MODE_VCO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001522 android.provider.Settings.Secure.putInt(getContentResolver(),
1523 android.provider.Settings.Secure.PREFERRED_TTY_MODE, buttonTtyMode);
1524 break;
1525 default:
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001526 buttonTtyMode = TelecomManager.TTY_MODE_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001527 }
1528
1529 mButtonTTY.setValue(Integer.toString(buttonTtyMode));
1530 updatePreferredTtyModeSummary(buttonTtyMode);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001531 Intent ttyModeChanged = new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
1532 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001533 sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
1534 }
1535 }
1536
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001537 private void updatePreferredTtyModeSummary(int TtyMode) {
1538 String [] txts = getResources().getStringArray(R.array.tty_mode_entries);
1539 switch(TtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001540 case TelecomManager.TTY_MODE_OFF:
1541 case TelecomManager.TTY_MODE_HCO:
1542 case TelecomManager.TTY_MODE_VCO:
1543 case TelecomManager.TTY_MODE_FULL:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001544 mButtonTTY.setSummary(txts[TtyMode]);
1545 break;
1546 default:
1547 mButtonTTY.setEnabled(false);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001548 mButtonTTY.setSummary(txts[TelecomManager.TTY_MODE_OFF]);
Sailesh Nepalbf900542014-07-15 16:18:32 -07001549 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001550 }
1551 }
1552
1553 private static void log(String msg) {
1554 Log.d(LOG_TAG, msg);
1555 }
1556
1557 /**
1558 * Updates the look of the VM preference widgets based on current VM provider settings.
1559 * Note that the provider name is loaded form the found activity via loadLabel in
1560 * {@link #initVoiceMailProviders()} in order for it to be localizable.
1561 */
1562 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1563 final String key = currentProviderSetting;
1564 final VoiceMailProvider provider = mVMProvidersData.get(key);
1565
1566 /* This is the case when we are coming up on a freshly wiped phone and there is no
1567 persisted value for the list preference mVoicemailProviders.
1568 In this case we want to show the UI asking the user to select a voicemail provider as
1569 opposed to silently falling back to default one. */
1570 if (provider == null) {
1571 if (DBG) {
1572 log("updateVMPreferenceWidget: provider for the key \"" + key + "\" is null.");
1573 }
1574 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1575 mVoicemailSettings.setEnabled(false);
1576 mVoicemailSettings.setIntent(null);
1577
1578 mVoicemailNotificationVibrate.setEnabled(false);
1579 } else {
1580 if (DBG) {
1581 log("updateVMPreferenceWidget: provider for the key \"" + key + "\".."
1582 + "name: " + provider.name
1583 + ", intent: " + provider.intent);
1584 }
1585 final String providerName = provider.name;
1586 mVoicemailProviders.setSummary(providerName);
1587 mVoicemailSettings.setEnabled(true);
1588 mVoicemailSettings.setIntent(provider.intent);
1589
1590 mVoicemailNotificationVibrate.setEnabled(true);
1591 }
1592 }
1593
1594 /**
1595 * Enumerates existing VM providers and puts their data into the list and populates
1596 * the preference list objects with their names.
1597 * In case we are called with ACTION_ADD_VOICEMAIL intent the intent may have
1598 * an extra string called IGNORE_PROVIDER_EXTRA with "package.activityName" of the provider
1599 * which should be hidden when we bring up the list of possible VM providers to choose.
1600 */
1601 private void initVoiceMailProviders() {
1602 if (DBG) log("initVoiceMailProviders()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001603
1604 String providerToIgnore = null;
1605 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1606 if (getIntent().hasExtra(IGNORE_PROVIDER_EXTRA)) {
1607 providerToIgnore = getIntent().getStringExtra(IGNORE_PROVIDER_EXTRA);
1608 }
1609 if (DBG) log("Found ACTION_ADD_VOICEMAIL. providerToIgnore=" + providerToIgnore);
1610 if (providerToIgnore != null) {
1611 // IGNORE_PROVIDER_EXTRA implies we want to remove the choice from the list.
Andrew Lee88b51e22014-10-29 15:48:51 -07001612 mVmProviderSettingsUtil.delete(providerToIgnore);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001613 }
1614 }
1615
1616 mVMProvidersData.clear();
1617
1618 // Stick the default element which is always there
1619 final String myCarrier = getString(R.string.voicemail_default);
1620 mVMProvidersData.put(DEFAULT_VM_PROVIDER_KEY, new VoiceMailProvider(myCarrier, null));
1621
1622 // Enumerate providers
1623 PackageManager pm = getPackageManager();
1624 Intent intent = new Intent();
1625 intent.setAction(ACTION_CONFIGURE_VOICEMAIL);
1626 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
1627 int len = resolveInfos.size() + 1; // +1 for the default choice we will insert.
1628
1629 // Go through the list of discovered providers populating the data map
1630 // skip the provider we were instructed to ignore if there was one
1631 for (int i = 0; i < resolveInfos.size(); i++) {
1632 final ResolveInfo ri= resolveInfos.get(i);
1633 final ActivityInfo currentActivityInfo = ri.activityInfo;
Andrew Lee6214e2b2014-11-04 13:57:38 -08001634 final String key = currentActivityInfo.name;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001635 if (key.equals(providerToIgnore)) {
1636 if (DBG) log("Ignoring key: " + key);
1637 len--;
1638 continue;
1639 }
1640 if (DBG) log("Loading key: " + key);
1641 final String nameForDisplay = ri.loadLabel(pm).toString();
1642 Intent providerIntent = new Intent();
1643 providerIntent.setAction(ACTION_CONFIGURE_VOICEMAIL);
1644 providerIntent.setClassName(currentActivityInfo.packageName,
1645 currentActivityInfo.name);
1646 if (DBG) {
1647 log("Store loaded VoiceMailProvider. key: " + key
1648 + " -> name: " + nameForDisplay + ", intent: " + providerIntent);
1649 }
1650 mVMProvidersData.put(
1651 key,
1652 new VoiceMailProvider(nameForDisplay, providerIntent));
1653
1654 }
1655
1656 // Now we know which providers to display - create entries and values array for
1657 // the list preference
1658 String [] entries = new String [len];
1659 String [] values = new String [len];
1660 entries[0] = myCarrier;
1661 values[0] = DEFAULT_VM_PROVIDER_KEY;
1662 int entryIdx = 1;
1663 for (int i = 0; i < resolveInfos.size(); i++) {
Andrew Lee6214e2b2014-11-04 13:57:38 -08001664 final String key = resolveInfos.get(i).activityInfo.name;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001665 if (!mVMProvidersData.containsKey(key)) {
1666 continue;
1667 }
1668 entries[entryIdx] = mVMProvidersData.get(key).name;
1669 values[entryIdx] = key;
1670 entryIdx++;
1671 }
1672
1673 // ListPreference is now updated.
1674 mVoicemailProviders.setEntries(entries);
1675 mVoicemailProviders.setEntryValues(values);
1676
1677 // Remember the current Voicemail Provider key as a "previous" key. This will be used
1678 // when we fail to update Voicemail Provider, which requires rollback.
1679 // We will update this when the VM Provider setting is successfully updated.
1680 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1681 if (DBG) log("Set up the first mPreviousVMProviderKey: " + mPreviousVMProviderKey);
1682
1683 // Finally update the preference texts.
1684 updateVMPreferenceWidgets(mPreviousVMProviderKey);
1685 }
1686
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001687 /**
1688 * Simulates user clicking on a passed preference.
1689 * Usually needed when the preference is a dialog preference and we want to invoke
1690 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001691 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001692 */
1693 private void simulatePreferenceClick(Preference preference) {
1694 // Go through settings until we find our setting
1695 // and then simulate a click on it to bring up the dialog
1696 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1697 for (int idx = 0; idx < adapter.getCount(); idx++) {
1698 if (adapter.getItem(idx) == preference) {
1699 getPreferenceScreen().onItemClick(this.getListView(),
1700 null, idx, adapter.getItemId(idx));
1701 break;
1702 }
1703 }
1704 }
1705
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001706 private String getCurrentVoicemailProviderKey() {
1707 final String key = mVoicemailProviders.getValue();
1708 return (key != null) ? key : DEFAULT_VM_PROVIDER_KEY;
1709 }
1710
1711 @Override
1712 public boolean onOptionsItemSelected(MenuItem item) {
1713 final int itemId = item.getItemId();
1714 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001715 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001716 return true;
1717 }
1718 return super.onOptionsItemSelected(item);
1719 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001720 /**
1721 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1722 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1723 */
1724 public static void goUpToTopLevelSetting(Activity activity) {
1725 Intent intent = new Intent(activity, CallFeaturesSetting.class);
1726 intent.setAction(Intent.ACTION_MAIN);
1727 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1728 activity.startActivity(intent);
1729 activity.finish();
1730 }
1731}