blob: 5e65a4ab70b258be5ca4a5ed407777f37a9ba5b5 [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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import com.android.internal.telephony.Phone;
61import com.android.internal.telephony.PhoneConstants;
Andrew Lee2170a972014-08-13 18:13:01 -070062import com.android.phone.common.util.SettingsUtil;
Andrew Leedb2fe562014-09-03 15:40:43 -070063import com.android.phone.settings.AccountSelectionPreference;
Andrew Lee1af6cf72014-11-04 17:35:26 -080064import com.android.phone.settings.CallForwardInfoUtil;
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;
Andrew Lee5ed870c2014-10-29 11:47:49 -070067import com.android.phone.settings.fdn.FdnSetting;
Sailesh Nepal788959e2014-07-08 23:36:40 -070068import com.android.services.telephony.sip.SipUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069
Andrew Lee2170a972014-08-13 18:13:01 -070070import java.lang.String;
Andrew Leef1776d82014-11-04 14:45:02 -080071import java.util.ArrayList;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072import java.util.Collection;
73import java.util.HashMap;
74import java.util.HashSet;
75import java.util.Iterator;
76import java.util.List;
77import java.util.Map;
78
79/**
80 * Top level "Call settings" UI; see res/xml/call_feature_setting.xml
81 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070082 * This preference screen is the root of the "Call settings" hierarchy available from the Phone
83 * app; the settings here let you control various features related to phone calls (including
84 * voicemail settings, the "Respond via SMS" feature, and others.) It's used only on
85 * voice-capable phone devices.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070086 *
87 * Note that this activity is part of the package com.android.phone, even
88 * though you reach it from the "Phone" app (i.e. DialtactsActivity) which
89 * is from the package com.android.contacts.
90 *
91 * For the "Mobile network settings" screen under the main Settings app,
92 * See {@link MobileNetworkSettings}.
93 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070094 * TODO: Settings should be split into PreferenceFragments where possible (ie. voicemail).
Andrew Lee2170a972014-08-13 18:13:01 -070095 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -070096 * @see com.android.phone.MobileNetworkSettings
97 */
98public class CallFeaturesSetting extends PreferenceActivity
99 implements DialogInterface.OnClickListener,
Andrew Lee2170a972014-08-13 18:13:01 -0700100 Preference.OnPreferenceChangeListener,
Andrew Lee2170a972014-08-13 18:13:01 -0700101 EditPhoneNumberPreference.OnDialogClosedListener,
Andrew Leece8ae2a2014-09-10 10:41:48 -0700102 EditPhoneNumberPreference.GetDefaultNumberListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700103 private static final String LOG_TAG = "CallFeaturesSetting";
104 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
Andrew Lee77527ac2014-10-21 16:57:39 -0700105 // STOPSHIP if true. Flag to override behavior default behavior to hide VT setting.
106 private static final boolean ENABLE_VT_FLAG = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107
108 /**
109 * Intent action to bring up Voicemail Provider settings.
110 *
111 * @see #IGNORE_PROVIDER_EXTRA
112 */
113 public static final String ACTION_ADD_VOICEMAIL =
114 "com.android.phone.CallFeaturesSetting.ADD_VOICEMAIL";
115 // intent action sent by this activity to a voice mail provider
116 // to trigger its configuration UI
117 public static final String ACTION_CONFIGURE_VOICEMAIL =
118 "com.android.phone.CallFeaturesSetting.CONFIGURE_VOICEMAIL";
119 // Extra put in the return from VM provider config containing voicemail number to set
120 public static final String VM_NUMBER_EXTRA = "com.android.phone.VoicemailNumber";
121 // Extra put in the return from VM provider config containing call forwarding number to set
122 public static final String FWD_NUMBER_EXTRA = "com.android.phone.ForwardingNumber";
123 // Extra put in the return from VM provider config containing call forwarding number to set
124 public static final String FWD_NUMBER_TIME_EXTRA = "com.android.phone.ForwardingNumberTime";
125 // If the VM provider returns non null value in this extra we will force the user to
126 // choose another VM provider
127 public static final String SIGNOUT_EXTRA = "com.android.phone.Signout";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700129 // Key identifying the default vocie mail provider
130 public static final String DEFAULT_VM_PROVIDER_KEY = "";
131
132 /**
133 * String Extra put into ACTION_ADD_VOICEMAIL call to indicate which provider should be hidden
134 * in the list of providers presented to the user. This allows a provider which is being
135 * disabled (e.g. GV user logging out) to force the user to pick some other provider.
136 */
137 public static final String IGNORE_PROVIDER_EXTRA = "com.android.phone.ProviderToIgnore";
138
139 // string constants
140 private static final String NUM_PROJECTION[] = {CommonDataKinds.Phone.NUMBER};
141
142 // String keys for preference lookup
143 // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
Andrew Lee97708a42014-09-25 12:39:07 -0700144 private static final String VOICEMAIL_SETTING_SCREEN_PREF_KEY = "button_voicemail_category_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700145 private static final String BUTTON_VOICEMAIL_KEY = "button_voicemail_key";
146 private static final String BUTTON_VOICEMAIL_PROVIDER_KEY = "button_voicemail_provider_key";
147 private static final String BUTTON_VOICEMAIL_SETTING_KEY = "button_voicemail_setting_key";
148 // New preference key for voicemail notification vibration
149 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY =
150 "button_voicemail_notification_vibrate_key";
151 // Old preference key for voicemail notification vibration. Used for migration to the new
152 // preference key only.
153 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY =
154 "button_voicemail_notification_vibrate_when_key";
155 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_RINGTONE_KEY =
156 "button_voicemail_notification_ringtone_key";
157 private static final String BUTTON_FDN_KEY = "button_fdn_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700158
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700159 private static final String BUTTON_DTMF_KEY = "button_dtmf_settings";
160 private static final String BUTTON_RETRY_KEY = "button_auto_retry_key";
161 private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
162 private static final String BUTTON_HAC_KEY = "button_hac_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700163
164 private static final String BUTTON_GSM_UMTS_OPTIONS = "button_gsm_more_expand_key";
165 private static final String BUTTON_CDMA_OPTIONS = "button_cdma_more_expand_key";
166
Andrew Leedb2fe562014-09-03 15:40:43 -0700167 private static final String DEFAULT_OUTGOING_ACCOUNT_KEY = "default_outgoing_account";
Andrew Leece8ae2a2014-09-10 10:41:48 -0700168 private static final String PHONE_ACCOUNT_SETTINGS_KEY =
169 "phone_account_settings_preference_screen";
Andrew Leedb2fe562014-09-03 15:40:43 -0700170
Andrew Leedf14ead2014-10-17 14:22:52 -0700171 private static final String ENABLE_VIDEO_CALLING_KEY = "button_enable_video_calling";
172
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700173 /** Event for Async voicemail change call */
174 private static final int EVENT_VOICEMAIL_CHANGED = 500;
175 private static final int EVENT_FORWARDING_CHANGED = 501;
176 private static final int EVENT_FORWARDING_GET_COMPLETED = 502;
177
Andrew Lee2170a972014-08-13 18:13:01 -0700178 private static final int MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY = 1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700179
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700180 public static final String HAC_KEY = "HACSetting";
181 public static final String HAC_VAL_ON = "ON";
182 public static final String HAC_VAL_OFF = "OFF";
183
184 /** Handle to voicemail pref */
185 private static final int VOICEMAIL_PREF_ID = 1;
186 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
187
188 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189 private AudioManager mAudioManager;
Andrew Lee88b51e22014-10-29 15:48:51 -0700190 private VoicemailProviderSettingsUtil mVmProviderSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700191
192 private static final int VM_NOCHANGE_ERROR = 400;
193 private static final int VM_RESPONSE_ERROR = 500;
194 private static final int FW_SET_RESPONSE_ERROR = 501;
195 private static final int FW_GET_RESPONSE_ERROR = 502;
196
197
198 // dialog identifiers for voicemail
199 private static final int VOICEMAIL_DIALOG_CONFIRM = 600;
200 private static final int VOICEMAIL_FWD_SAVING_DIALOG = 601;
201 private static final int VOICEMAIL_FWD_READING_DIALOG = 602;
202 private static final int VOICEMAIL_REVERTING_DIALOG = 603;
203
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700204 // voicemail notification vibration string constants
205 private static final String VOICEMAIL_VIBRATION_ALWAYS = "always";
206 private static final String VOICEMAIL_VIBRATION_NEVER = "never";
207
Andrew Lee5ed870c2014-10-29 11:47:49 -0700208 private SubscriptionInfoHelper mSubscriptionInfoHelper;
209
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700210 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
211
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700212 /** Whether dialpad plays DTMF tone or not. */
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700213 private CheckBoxPreference mButtonAutoRetry;
214 private CheckBoxPreference mButtonHAC;
215 private ListPreference mButtonDTMF;
216 private ListPreference mButtonTTY;
Andrew Leece8ae2a2014-09-10 10:41:48 -0700217 private Preference mPhoneAccountSettingsPreference;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700218 private ListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700219 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700220 private PreferenceScreen mVoicemailSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700221 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700222 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700223
224 private class VoiceMailProvider {
Andrew Leef1776d82014-11-04 14:45:02 -0800225 public String name;
226 public Intent intent;
227
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700228 public VoiceMailProvider(String name, Intent intent) {
229 this.name = name;
230 this.intent = intent;
231 }
Andrew Leef1776d82014-11-04 14:45:02 -0800232
233 public String toString() {
234 return "[ Name: " + name + ", Intent: " + intent + " ]";
235 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700236 }
237
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700238 /**
239 * Results of reading forwarding settings
240 */
241 private CallForwardInfo[] mForwardingReadResults = null;
242
243 /**
244 * Result of forwarding number change.
245 * Keys are reasons (eg. unconditional forwarding).
246 */
247 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
248
249 /**
250 * Expected CF read result types.
251 * This set keeps track of the CF types for which we've issued change
252 * commands so we can tell when we've received all of the responses.
253 */
254 private Collection<Integer> mExpectedChangeResultReasons = null;
255
256 /**
257 * Result of vm number change
258 */
259 private AsyncResult mVoicemailChangeResult = null;
260
261 /**
262 * Previous VM provider setting so we can return to it in case of failure.
263 */
264 private String mPreviousVMProviderKey = null;
265
266 /**
267 * Id of the dialog being currently shown.
268 */
269 private int mCurrentDialogId = 0;
270
271 /**
272 * Flag indicating that we are invoking settings for the voicemail provider programmatically
273 * due to vm provider change.
274 */
275 private boolean mVMProviderSettingsForced = false;
276
277 /**
278 * Flag indicating that we are making changes to vm or fwd numbers
279 * due to vm provider change.
280 */
281 private boolean mChangingVMorFwdDueToProviderChange = false;
282
283 /**
284 * True if we are in the process of vm & fwd number change and vm has already been changed.
285 * This is used to decide what to do in case of rollback.
286 */
287 private boolean mVMChangeCompletedSuccessfully = false;
288
289 /**
290 * True if we had full or partial failure setting forwarding numbers and so need to roll them
291 * back.
292 */
293 private boolean mFwdChangesRequireRollback = false;
294
295 /**
296 * Id of error msg to display to user once we are done reverting the VM provider to the previous
297 * one.
298 */
299 private int mVMOrFwdSetError = 0;
300
301 /**
302 * Data about discovered voice mail settings providers.
303 * Is populated by querying which activities can handle ACTION_CONFIGURE_VOICEMAIL.
304 * They key in this map is package name + activity name.
305 * We always add an entry for the default provider with a key of empty
306 * string and intent value of null.
307 * @see #initVoiceMailProviders()
308 */
309 private final Map<String, VoiceMailProvider> mVMProvidersData =
310 new HashMap<String, VoiceMailProvider>();
311
312 /** string to hold old voicemail number as it is being updated. */
313 private String mOldVmNumber;
314
315 // New call forwarding settings and vm number we will be setting
316 // Need to save these since before we get to saving we need to asynchronously
317 // query the existing forwarding settings.
318 private CallForwardInfo[] mNewFwdSettings;
319 private String mNewVMNumber;
320
321 private boolean mForeground;
322
323 @Override
324 public void onPause() {
325 super.onPause();
326 mForeground = false;
327 }
328
329 /**
330 * We have to pull current settings from the network for all kinds of
331 * voicemail providers so we can tell whether we have to update them,
332 * so use this bit to keep track of whether we're reading settings for the
333 * default provider and should therefore save them out when done.
334 */
335 private boolean mReadingSettingsForDefaultProvider = false;
336
Tyler Gunnbaee2952014-09-10 16:01:02 -0700337 /**
338 * Used to indicate that the voicemail preference should be shown.
339 */
340 private boolean mShowVoicemailPreference = false;
341
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700342 /*
343 * Click Listeners, handle click based on objects attached to UI.
344 */
345
346 // Click listener for all toggle events
347 @Override
348 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
349 if (preference == mSubMenuVoicemailSettings) {
350 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700351 } else if (preference == mButtonDTMF) {
352 return true;
353 } else if (preference == mButtonTTY) {
354 return true;
355 } else if (preference == mButtonAutoRetry) {
356 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
357 android.provider.Settings.Global.CALL_AUTO_RETRY,
358 mButtonAutoRetry.isChecked() ? 1 : 0);
359 return true;
360 } else if (preference == mButtonHAC) {
361 int hac = mButtonHAC.isChecked() ? 1 : 0;
362 // Update HAC value in Settings database
363 Settings.System.putInt(mPhone.getContext().getContentResolver(),
364 Settings.System.HEARING_AID, hac);
365
366 // Update HAC Value in AudioManager
367 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
368 return true;
369 } else if (preference == mVoicemailSettings) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700370 final Dialog dialog = mVoicemailSettings.getDialog();
371 if (dialog != null) {
372 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
373 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700374 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
375 if (preference.getIntent() != null) {
376 if (DBG) {
377 log("onPreferenceTreeClick: Invoking cfg intent "
378 + preference.getIntent().getPackage());
379 }
380
381 // onActivityResult() will be responsible for resetting some of variables.
382 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
383 return true;
384 } else {
385 if (DBG) {
386 log("onPreferenceTreeClick:"
387 + " No Intent is available. Use default behavior defined in xml.");
388 }
389
390 // There's no onActivityResult(), so we need to take care of some of variables
391 // which should be reset here.
392 mPreviousVMProviderKey = DEFAULT_VM_PROVIDER_KEY;
393 mVMProviderSettingsForced = false;
394
395 // This should let the preference use default behavior in the xml.
396 return false;
397 }
Andrew Lee97708a42014-09-25 12:39:07 -0700398 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700399 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
400 if (dialog != null) {
401 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
402 }
Andrew Lee97708a42014-09-25 12:39:07 -0700403 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700404 }
405 return false;
406 }
407
408 /**
409 * Implemented to support onPreferenceChangeListener to look for preference
410 * changes.
411 *
412 * @param preference is the preference to be changed
413 * @param objValue should be the value of the selection, NOT its localized
414 * display value.
415 */
416 @Override
417 public boolean onPreferenceChange(Preference preference, Object objValue) {
418 if (DBG) {
Andrew Leedf14ead2014-10-17 14:22:52 -0700419 log("onPreferenceChange(). preference: \"" + preference + "\""
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700420 + ", value: \"" + objValue + "\"");
421 }
Andrew Lee2170a972014-08-13 18:13:01 -0700422
423 if (preference == mButtonDTMF) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700424 int index = mButtonDTMF.findIndexOfValue((String) objValue);
425 Settings.System.putInt(mPhone.getContext().getContentResolver(),
426 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
427 } else if (preference == mButtonTTY) {
428 handleTTYChange(preference, objValue);
429 } else if (preference == mVoicemailProviders) {
430 final String newProviderKey = (String) objValue;
431 if (DBG) {
432 log("Voicemail Provider changes from \"" + mPreviousVMProviderKey
433 + "\" to \"" + newProviderKey + "\".");
434 }
435 // If previous provider key and the new one is same, we don't need to handle it.
436 if (mPreviousVMProviderKey.equals(newProviderKey)) {
437 if (DBG) log("No change is made toward VM provider setting.");
438 return true;
439 }
440 updateVMPreferenceWidgets(newProviderKey);
441
Andrew Leeb490d732014-10-27 15:00:41 -0700442 final VoicemailProviderSettings newProviderSettings =
Andrew Lee88b51e22014-10-29 15:48:51 -0700443 mVmProviderSettingsUtil.load(newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700444
Andrew Lee88b51e22014-10-29 15:48:51 -0700445 // If the user switches to a voice mail provider and we have numbers stored for it we
446 // will automatically change the phone's voice mail and forwarding number to the stored
447 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700448 if (newProviderSettings == null) {
449 // Force the user into a configuration of the chosen provider
450 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
451 mVMProviderSettingsForced = true;
452 simulatePreferenceClick(mVoicemailSettings);
453 } else {
454 if (DBG) log("Saved preferences found - switching to them");
455 // Set this flag so if we get a failure we revert to previous provider
456 mChangingVMorFwdDueToProviderChange = true;
457 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
458 }
Andrew Leedf14ead2014-10-17 14:22:52 -0700459 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700460 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
461 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
462 } else {
463 AlertDialog.Builder builder = new AlertDialog.Builder(this);
464 DialogInterface.OnClickListener networkSettingsClickListener =
465 new Dialog.OnClickListener() {
466 @Override
467 public void onClick(DialogInterface dialog, int which) {
468 startActivity(new Intent(mPhone.getContext(),
469 com.android.phone.MobileNetworkSettings.class));
470 }
471 };
472 builder.setMessage(getResources().getString(
473 R.string.enable_video_calling_dialog_msg))
474 .setNeutralButton(getResources().getString(
475 R.string.enable_video_calling_dialog_settings),
476 networkSettingsClickListener)
477 .setPositiveButton(android.R.string.ok, null)
478 .show();
479 return false;
480 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700481 }
482 // always let the preference setting proceed.
483 return true;
484 }
485
486 @Override
487 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
488 if (DBG) log("onPreferenceClick: request preference click on dialog close: " +
489 buttonClicked);
490 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
491 return;
492 }
493
494 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700495 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
496 mSubMenuVoicemailSettings.getPhoneNumber(),
497 VoicemailProviderSettings.NO_FORWARDING);
498 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700499 }
500 }
501
502 /**
503 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
504 * This method set the default values for the various
505 * EditPhoneNumberPreference dialogs.
506 */
507 @Override
508 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
509 if (preference == mSubMenuVoicemailSettings) {
510 // update the voicemail number field, which takes care of the
511 // mSubMenuVoicemailSettings itself, so we should return null.
512 if (DBG) log("updating default for voicemail dialog");
513 updateVoiceNumberField();
514 return null;
515 }
516
517 String vmDisplay = mPhone.getVoiceMailNumber();
518 if (TextUtils.isEmpty(vmDisplay)) {
519 // if there is no voicemail number, we just return null to
520 // indicate no contribution.
521 return null;
522 }
523
524 // Return the voicemail number prepended with "VM: "
525 if (DBG) log("updating default for call forwarding dialogs");
526 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
527 }
528
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700529 private void switchToPreviousVoicemailProvider() {
530 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
531 if (mPreviousVMProviderKey != null) {
532 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
533 // we have to revert with carrier
534 if (DBG) {
535 log("Needs to rollback."
536 + " mVMChangeCompletedSuccessfully=" + mVMChangeCompletedSuccessfully
537 + ", mFwdChangesRequireRollback=" + mFwdChangesRequireRollback);
538 }
539
540 showDialogIfForeground(VOICEMAIL_REVERTING_DIALOG);
Andrew Leeb490d732014-10-27 15:00:41 -0700541 final VoicemailProviderSettings prevSettings =
Andrew Lee88b51e22014-10-29 15:48:51 -0700542 mVmProviderSettingsUtil.load(mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700543 if (prevSettings == null) {
544 // prevSettings never becomes null since it should be already loaded!
Andrew Leeb490d732014-10-27 15:00:41 -0700545 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700546 + mPreviousVMProviderKey + "\" becomes null, which is unexpected.");
547 if (DBG) {
548 Log.e(LOG_TAG,
549 "mVMChangeCompletedSuccessfully: " + mVMChangeCompletedSuccessfully
550 + ", mFwdChangesRequireRollback: " + mFwdChangesRequireRollback);
551 }
552 }
553 if (mVMChangeCompletedSuccessfully) {
Andrew Leeb490d732014-10-27 15:00:41 -0700554 mNewVMNumber = prevSettings.getVoicemailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700555 Log.i(LOG_TAG, "VM change is already completed successfully."
556 + "Have to revert VM back to " + mNewVMNumber + " again.");
557 mPhone.setVoiceMailNumber(
558 mPhone.getVoiceMailAlphaTag().toString(),
559 mNewVMNumber,
560 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
561 }
562 if (mFwdChangesRequireRollback) {
563 Log.i(LOG_TAG, "Requested to rollback Fwd changes.");
Andrew Leeb490d732014-10-27 15:00:41 -0700564 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700565 if (prevFwdSettings != null) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800566 Map<Integer, AsyncResult> results = mForwardingChangeResults;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700567 resetForwardingChangeState();
568 for (int i = 0; i < prevFwdSettings.length; i++) {
569 CallForwardInfo fi = prevFwdSettings[i];
570 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
Andrew Lee1af6cf72014-11-04 17:35:26 -0800571 // Only revert the settings for which the update succeeded.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700572 AsyncResult result = results.get(fi.reason);
573 if (result != null && result.exception == null) {
574 mExpectedChangeResultReasons.add(fi.reason);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800575 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700576 mRevertOptionComplete.obtainMessage(
577 EVENT_FORWARDING_CHANGED, i, 0));
578 }
579 }
580 }
581 }
582 } else {
583 if (DBG) log("No need to revert");
584 onRevertDone();
585 }
586 }
587 }
588
589 private void onRevertDone() {
590 if (DBG) log("Flipping provider key back to " + mPreviousVMProviderKey);
591 mVoicemailProviders.setValue(mPreviousVMProviderKey);
592 updateVMPreferenceWidgets(mPreviousVMProviderKey);
593 updateVoiceNumberField();
594 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800595 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700596 mVMOrFwdSetError = 0;
597 }
598 }
599
600 @Override
601 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
602 if (DBG) {
603 log("onActivityResult: requestCode: " + requestCode
604 + ", resultCode: " + resultCode
605 + ", data: " + data);
606 }
607 // there are cases where the contact picker may end up sending us more than one
608 // request. We want to ignore the request if we're not in the correct state.
609 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
610 boolean failure = false;
611
612 // No matter how the processing of result goes lets clear the flag
613 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
614 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
615 mVMProviderSettingsForced = false;
616
617 String vmNum = null;
618 if (resultCode != RESULT_OK) {
619 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
620 failure = true;
621 } else {
622 if (data == null) {
623 if (DBG) log("onActivityResult: vm provider cfg result has no data");
624 failure = true;
625 } else {
626 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
627 if (DBG) log("Provider requested signout");
628 if (isVMProviderSettingsForced) {
629 if (DBG) log("Going back to previous provider on signout");
630 switchToPreviousVoicemailProvider();
631 } else {
632 final String victim = getCurrentVoicemailProviderKey();
633 if (DBG) log("Relaunching activity and ignoring " + victim);
634 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
635 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
636 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
637 this.startActivity(i);
638 }
639 return;
640 }
641 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
642 if (vmNum == null || vmNum.length() == 0) {
643 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
644 failure = true;
645 }
646 }
647 }
648 if (failure) {
649 if (DBG) log("Failure in return from voicemail provider");
650 if (isVMProviderSettingsForced) {
651 switchToPreviousVoicemailProvider();
652 } else {
653 if (DBG) log("Not switching back the provider since this is not forced config");
654 }
655 return;
656 }
657 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
658 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
659
Santos Cordonda120f42014-08-06 04:44:34 -0700660 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700661 // send it to the provider when it's config is invoked so it can use this as default
662 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
663
664 if (DBG) log("onActivityResult: vm provider cfg result " +
665 (fwdNum != null ? "has" : " does not have") + " forwarding number");
666 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700667 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700668 return;
669 }
670
671 if (requestCode == VOICEMAIL_PREF_ID) {
672 if (resultCode != RESULT_OK) {
673 if (DBG) log("onActivityResult: contact picker result not OK.");
674 return;
675 }
676
677 Cursor cursor = null;
678 try {
679 cursor = getContentResolver().query(data.getData(),
680 NUM_PROJECTION, null, null, null);
681 if ((cursor == null) || (!cursor.moveToFirst())) {
682 if (DBG) log("onActivityResult: bad contact data, no results found.");
683 return;
684 }
685 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
686 return;
687 } finally {
688 if (cursor != null) {
689 cursor.close();
690 }
691 }
692 }
693
694 super.onActivityResult(requestCode, resultCode, data);
695 }
696
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700697 /**
698 * Wrapper around showDialog() that will silently do nothing if we're
699 * not in the foreground.
700 *
701 * This is useful here because most of the dialogs we display from
702 * this class are triggered by asynchronous events (like
703 * success/failure messages from the telephony layer) and it's
704 * possible for those events to come in even after the user has gone
705 * to a different screen.
706 */
707 // TODO: this is too brittle: it's still easy to accidentally add new
708 // code here that calls showDialog() directly (which will result in a
709 // WindowManager$BadTokenException if called after the activity has
710 // been stopped.)
711 //
712 // It would be cleaner to do the "if (mForeground)" check in one
713 // central place, maybe by using a single Handler for all asynchronous
714 // events (and have *that* discard events if we're not in the
715 // foreground.)
716 //
717 // Unfortunately it's not that simple, since we sometimes need to do
718 // actual work to handle these events whether or not we're in the
719 // foreground (see the Handler code in mSetOptionComplete for
720 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800721 //
722 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
723 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700724 private void showDialogIfForeground(int id) {
725 if (mForeground) {
726 showDialog(id);
727 }
728 }
729
730 private void dismissDialogSafely(int id) {
731 try {
732 dismissDialog(id);
733 } catch (IllegalArgumentException e) {
734 // This is expected in the case where we were in the background
735 // at the time we would normally have shown the dialog, so we didn't
736 // show it.
737 }
738 }
739
Andrew Leeb490d732014-10-27 15:00:41 -0700740 private void saveVoiceMailAndForwardingNumber(
741 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700742 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700743 mNewVMNumber = newSettings.getVoicemailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700744 // empty vm number == clearing the vm number ?
745 if (mNewVMNumber == null) {
746 mNewVMNumber = "";
747 }
748
Andrew Leeb490d732014-10-27 15:00:41 -0700749 mNewFwdSettings = newSettings.getForwardingSettings();
750 if (DBG) log("newFwdNumber "
751 + String.valueOf((mNewFwdSettings != null ? mNewFwdSettings.length : 0))
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700752 + " settings");
753
754 // No fwd settings on CDMA
755 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
756 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700757 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700758 }
759
Andrew Leee3c15212014-10-28 13:12:55 -0700760 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700761 if (mNewVMNumber.equals(mOldVmNumber)
762 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Leeab082272014-11-04 15:50:42 -0800763 showDialogIfForeground(VM_NOCHANGE_ERROR);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700764 return;
765 }
766
Andrew Lee88b51e22014-10-29 15:48:51 -0700767 mVmProviderSettingsUtil.save(key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700768 mVMChangeCompletedSuccessfully = false;
769 mFwdChangesRequireRollback = false;
770 mVMOrFwdSetError = 0;
771 if (!key.equals(mPreviousVMProviderKey)) {
772 mReadingSettingsForDefaultProvider =
773 mPreviousVMProviderKey.equals(DEFAULT_VM_PROVIDER_KEY);
774 if (DBG) log("Reading current forwarding settings");
Andrew Leeb490d732014-10-27 15:00:41 -0700775 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
776 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
777 for (int i = 0; i < mForwardingReadResults.length; i++) {
778 mPhone.getCallForwardingOption(
779 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700780 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
781 }
782 showDialogIfForeground(VOICEMAIL_FWD_READING_DIALOG);
783 } else {
784 saveVoiceMailAndForwardingNumberStage2();
785 }
786 }
787
788 private final Handler mGetOptionComplete = new Handler() {
789 @Override
790 public void handleMessage(Message msg) {
791 AsyncResult result = (AsyncResult) msg.obj;
792 switch (msg.what) {
793 case EVENT_FORWARDING_GET_COMPLETED:
794 handleForwardingSettingsReadResult(result, msg.arg1);
795 break;
796 }
797 }
798 };
799
800 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
801 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
802 Throwable error = null;
803 if (ar.exception != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700804 error = ar.exception;
Andrew Lee1af6cf72014-11-04 17:35:26 -0800805 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700806 }
807 if (ar.userObj instanceof Throwable) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800808 error = (Throwable) ar.userObj;
809 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700810 }
811
812 // We may have already gotten an error and decided to ignore the other results.
813 if (mForwardingReadResults == null) {
814 if (DBG) Log.d(LOG_TAG, "ignoring fwd reading result: " + idx);
815 return;
816 }
817
818 // In case of error ignore other results, show an error dialog
819 if (error != null) {
820 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
821 mForwardingReadResults = null;
822 dismissDialogSafely(VOICEMAIL_FWD_READING_DIALOG);
Andrew Leeab082272014-11-04 15:50:42 -0800823 showDialogIfForeground(FW_GET_RESPONSE_ERROR);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700824 return;
825 }
826
Andrew Lee1af6cf72014-11-04 17:35:26 -0800827 // Get the forwarding info.
828 mForwardingReadResults[idx] = CallForwardInfoUtil.getCallForwardInfo(
829 (CallForwardInfo[]) ar.result,
830 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx]);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700831
832 // Check if we got all the results already
833 boolean done = true;
834 for (int i = 0; i < mForwardingReadResults.length; i++) {
835 if (mForwardingReadResults[i] == null) {
836 done = false;
837 break;
838 }
839 }
Andrew Lee1af6cf72014-11-04 17:35:26 -0800840
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700841 if (done) {
842 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
843 dismissDialogSafely(VOICEMAIL_FWD_READING_DIALOG);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800844
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700845 if (mReadingSettingsForDefaultProvider) {
Andrew Lee88b51e22014-10-29 15:48:51 -0700846 mVmProviderSettingsUtil.save(DEFAULT_VM_PROVIDER_KEY,
847 new VoicemailProviderSettings(this.mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700848 mReadingSettingsForDefaultProvider = false;
849 }
850 saveVoiceMailAndForwardingNumberStage2();
851 } else {
852 if (DBG) Log.d(LOG_TAG, "Not done receiving fwd info");
853 }
854 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700855 private void resetForwardingChangeState() {
856 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
857 mExpectedChangeResultReasons = new HashSet<Integer>();
858 }
859
860 // Called after we are done saving the previous forwarding settings if
861 // we needed.
862 private void saveVoiceMailAndForwardingNumberStage2() {
863 mForwardingChangeResults = null;
864 mVoicemailChangeResult = null;
Andrew Leeb490d732014-10-27 15:00:41 -0700865 if (mNewFwdSettings != VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700866 resetForwardingChangeState();
867 for (int i = 0; i < mNewFwdSettings.length; i++) {
868 CallForwardInfo fi = mNewFwdSettings[i];
Andrew Lee1af6cf72014-11-04 17:35:26 -0800869 CallForwardInfo fiForReason =
870 CallForwardInfoUtil.infoForReason(mForwardingReadResults, fi.reason);
871 final boolean doUpdate = CallForwardInfoUtil.isUpdateRequired(fiForReason, fi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700872
873 if (doUpdate) {
874 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
875 mExpectedChangeResultReasons.add(i);
876
Andrew Lee1af6cf72014-11-04 17:35:26 -0800877 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700878 mSetOptionComplete.obtainMessage(
879 EVENT_FORWARDING_CHANGED, fi.reason, 0));
880 }
881 }
882 showDialogIfForeground(VOICEMAIL_FWD_SAVING_DIALOG);
883 } else {
884 if (DBG) log("Not touching fwd #");
885 setVMNumberWithCarrier();
886 }
887 }
888
889 private void setVMNumberWithCarrier() {
890 if (DBG) log("save voicemail #: " + mNewVMNumber);
891 mPhone.setVoiceMailNumber(
892 mPhone.getVoiceMailAlphaTag().toString(),
893 mNewVMNumber,
894 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
895 }
896
897 /**
898 * Callback to handle option update completions
899 */
900 private final Handler mSetOptionComplete = new Handler() {
901 @Override
902 public void handleMessage(Message msg) {
903 AsyncResult result = (AsyncResult) msg.obj;
904 boolean done = false;
905 switch (msg.what) {
906 case EVENT_VOICEMAIL_CHANGED:
907 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700908 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700909 done = true;
910 break;
911 case EVENT_FORWARDING_CHANGED:
912 mForwardingChangeResults.put(msg.arg1, result);
913 if (result.exception != null) {
914 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
915 result.exception.getMessage());
916 } else {
917 if (DBG) log("Success in setting fwd# " + msg.arg1);
918 }
Andrew Leee438b312014-10-29 16:59:15 -0700919 if (isForwardingCompleted()) {
920 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700921 if (DBG) log("Overall fwd changes completed ok, starting vm change");
922 setVMNumberWithCarrier();
923 } else {
924 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
925 "Check if we need to try rollback for some settings.");
926 mFwdChangesRequireRollback = false;
927 Iterator<Map.Entry<Integer,AsyncResult>> it =
928 mForwardingChangeResults.entrySet().iterator();
929 while (it.hasNext()) {
930 Map.Entry<Integer,AsyncResult> entry = it.next();
931 if (entry.getValue().exception == null) {
932 // If at least one succeeded we have to revert
933 Log.i(LOG_TAG, "Rollback will be required");
934 mFwdChangesRequireRollback = true;
935 break;
936 }
937 }
938 if (!mFwdChangesRequireRollback) {
939 Log.i(LOG_TAG, "No rollback needed.");
940 }
941 done = true;
942 }
943 }
944 break;
945 default:
946 // TODO: should never reach this, may want to throw exception
947 }
948 if (done) {
949 if (DBG) log("All VM provider related changes done");
950 if (mForwardingChangeResults != null) {
951 dismissDialogSafely(VOICEMAIL_FWD_SAVING_DIALOG);
952 }
Andrew Leee438b312014-10-29 16:59:15 -0700953 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700954 }
955 }
956 };
957
958 /**
959 * Callback to handle option revert completions
960 */
961 private final Handler mRevertOptionComplete = new Handler() {
962 @Override
963 public void handleMessage(Message msg) {
964 AsyncResult result = (AsyncResult) msg.obj;
965 switch (msg.what) {
966 case EVENT_VOICEMAIL_CHANGED:
967 mVoicemailChangeResult = result;
968 if (DBG) log("VM revert complete msg");
969 break;
970 case EVENT_FORWARDING_CHANGED:
971 mForwardingChangeResults.put(msg.arg1, result);
972 if (result.exception != null) {
973 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
974 result.exception.getMessage());
975 } else {
976 if (DBG) log("Success in reverting fwd# " + msg.arg1);
977 }
978 if (DBG) log("FWD revert complete msg ");
979 break;
980 default:
981 // TODO: should never reach this, may want to throw exception
982 }
983 final boolean done =
984 (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null) &&
Andrew Leee438b312014-10-29 16:59:15 -0700985 (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700986 if (done) {
987 if (DBG) log("All VM reverts done");
988 dismissDialogSafely(VOICEMAIL_REVERTING_DIALOG);
989 onRevertDone();
990 }
991 }
992 };
993
994 /**
Andrew Leee438b312014-10-29 16:59:15 -0700995 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700996 */
Andrew Leee438b312014-10-29 16:59:15 -0700997 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700998 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -0700999 return true;
1000 }
1001
1002 for (Integer reason : mExpectedChangeResultReasons) {
1003 if (mForwardingChangeResults.get(reason) == null) {
1004 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001005 }
1006 }
Andrew Leee438b312014-10-29 16:59:15 -07001007
1008 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001009 }
Andrew Leee438b312014-10-29 16:59:15 -07001010
1011 private boolean isFwdChangeSuccess() {
1012 if (mForwardingChangeResults == null) {
1013 return true;
1014 }
1015
1016 for (AsyncResult result : mForwardingChangeResults.values()) {
1017 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001018 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -07001019 String msg = exception.getMessage();
1020 msg = (msg != null) ? msg : "";
1021 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
1022 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001023 }
1024 }
Andrew Leee438b312014-10-29 16:59:15 -07001025 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001026 }
1027
Andrew Leee438b312014-10-29 16:59:15 -07001028 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001029 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -07001030 String msg = mVoicemailChangeResult.exception.getMessage();
1031 msg = (msg != null) ? msg : "";
1032 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
1033 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001034 }
Andrew Leee438b312014-10-29 16:59:15 -07001035
1036 if (DBG) log("VM change completed successfully.");
1037 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001038 }
1039
Andrew Leee438b312014-10-29 16:59:15 -07001040 private void handleSetVmOrFwdMessage() {
1041 if (DBG) log("handleSetVMMessage: set VM request complete");
1042
1043 if (!isFwdChangeSuccess()) {
Andrew Leeab082272014-11-04 15:50:42 -08001044 handleVmOrFwdSetError(FW_SET_RESPONSE_ERROR);
Andrew Leee438b312014-10-29 16:59:15 -07001045 } else if (!isVmChangeSuccess()) {
Andrew Leeab082272014-11-04 15:50:42 -08001046 handleVmOrFwdSetError(VM_RESPONSE_ERROR);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001047 } else {
Andrew Leee438b312014-10-29 16:59:15 -07001048 if (DBG) log("change VM success!");
Andrew Leeab082272014-11-04 15:50:42 -08001049 handleVmAndFwdSetSuccess(VOICEMAIL_DIALOG_CONFIRM);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001050 }
1051 }
1052
1053 /**
1054 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
1055 * changes to those settings and show "failure" dialog.
1056 *
Andrew Leeab082272014-11-04 15:50:42 -08001057 * @param dialogId ID of the dialog to show for the specific error case. Either
1058 * {@link #FW_SET_RESPONSE_ERROR} or {@link #VM_RESPONSE_ERROR}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001059 */
Andrew Leeab082272014-11-04 15:50:42 -08001060 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001061 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -08001062 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001063 mChangingVMorFwdDueToProviderChange = false;
1064 switchToPreviousVoicemailProvider();
1065 return;
1066 }
1067 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001068 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001069 updateVoiceNumberField();
1070 }
1071
1072 /**
1073 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1074 * This updates a bunch of variables and show "success" dialog.
1075 */
Andrew Leeab082272014-11-04 15:50:42 -08001076 private void handleVmAndFwdSetSuccess(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001077 if (DBG) {
Andrew Leee438b312014-10-29 16:59:15 -07001078 log("handleVmAndFwdSetSuccess(). current voicemail provider key: "
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001079 + getCurrentVoicemailProviderKey());
1080 }
1081 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1082 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001083 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001084 updateVoiceNumberField();
1085 }
1086
1087 /**
1088 * Update the voicemail number from what we've recorded on the sim.
1089 */
1090 private void updateVoiceNumberField() {
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001091 if (DBG) log("updateVoiceNumberField()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001092
1093 mOldVmNumber = mPhone.getVoiceMailNumber();
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001094 if (TextUtils.isEmpty(mOldVmNumber)) {
1095 mSubMenuVoicemailSettings.setPhoneNumber("");
1096 mSubMenuVoicemailSettings.setSummary(getString(R.string.voicemail_number_not_set));
1097 } else {
1098 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1099 mSubMenuVoicemailSettings.setSummary(mOldVmNumber);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001100 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001101 }
1102
1103 /*
1104 * Helper Methods for Activity class.
1105 * The initial query commands are split into two pieces now
1106 * for individual expansion. This combined with the ability
1107 * to cancel queries allows for a much better user experience,
1108 * and also ensures that the user only waits to update the
1109 * data that is relevant.
1110 */
1111
1112 @Override
1113 protected void onPrepareDialog(int id, Dialog dialog) {
1114 super.onPrepareDialog(id, dialog);
1115 mCurrentDialogId = id;
1116 }
1117
1118 // dialog creation method, called by showDialog()
1119 @Override
1120 protected Dialog onCreateDialog(int id) {
1121 if ((id == VM_RESPONSE_ERROR) || (id == VM_NOCHANGE_ERROR) ||
1122 (id == FW_SET_RESPONSE_ERROR) || (id == FW_GET_RESPONSE_ERROR) ||
1123 (id == VOICEMAIL_DIALOG_CONFIRM)) {
1124
1125 AlertDialog.Builder b = new AlertDialog.Builder(this);
1126
1127 int msgId;
1128 int titleId = R.string.error_updating_title;
1129 switch (id) {
1130 case VOICEMAIL_DIALOG_CONFIRM:
1131 msgId = R.string.vm_changed;
1132 titleId = R.string.voicemail;
1133 // Set Button 2
1134 b.setNegativeButton(R.string.close_dialog, this);
1135 break;
1136 case VM_NOCHANGE_ERROR:
1137 // even though this is technically an error,
1138 // keep the title friendly.
1139 msgId = R.string.no_change;
1140 titleId = R.string.voicemail;
1141 // Set Button 2
1142 b.setNegativeButton(R.string.close_dialog, this);
1143 break;
1144 case VM_RESPONSE_ERROR:
1145 msgId = R.string.vm_change_failed;
1146 // Set Button 1
1147 b.setPositiveButton(R.string.close_dialog, this);
1148 break;
1149 case FW_SET_RESPONSE_ERROR:
1150 msgId = R.string.fw_change_failed;
1151 // Set Button 1
1152 b.setPositiveButton(R.string.close_dialog, this);
1153 break;
1154 case FW_GET_RESPONSE_ERROR:
1155 msgId = R.string.fw_get_in_vm_failed;
1156 b.setPositiveButton(R.string.alert_dialog_yes, this);
1157 b.setNegativeButton(R.string.alert_dialog_no, this);
1158 break;
1159 default:
1160 msgId = R.string.exception_error;
1161 // Set Button 3, tells the activity that the error is
1162 // not recoverable on dialog exit.
1163 b.setNeutralButton(R.string.close_dialog, this);
1164 break;
1165 }
1166
1167 b.setTitle(getText(titleId));
1168 String message = getText(msgId).toString();
1169 b.setMessage(message);
1170 b.setCancelable(false);
1171 AlertDialog dialog = b.create();
1172
1173 // make the dialog more obvious by bluring the background.
1174 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
1175
1176 return dialog;
1177 } else if (id == VOICEMAIL_FWD_SAVING_DIALOG || id == VOICEMAIL_FWD_READING_DIALOG ||
1178 id == VOICEMAIL_REVERTING_DIALOG) {
1179 ProgressDialog dialog = new ProgressDialog(this);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001180 dialog.setTitle(getText(R.string.call_settings));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001181 dialog.setIndeterminate(true);
1182 dialog.setCancelable(false);
1183 dialog.setMessage(getText(
1184 id == VOICEMAIL_FWD_SAVING_DIALOG ? R.string.updating_settings :
1185 (id == VOICEMAIL_REVERTING_DIALOG ? R.string.reverting_settings :
1186 R.string.reading_settings)));
1187 return dialog;
1188 }
1189
1190
1191 return null;
1192 }
1193
1194 // This is a method implemented for DialogInterface.OnClickListener.
1195 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1196 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1197 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1198 public void onClick(DialogInterface dialog, int which) {
1199 dialog.dismiss();
1200 switch (which){
1201 case DialogInterface.BUTTON_NEUTRAL:
1202 if (DBG) log("Neutral button");
1203 break;
1204 case DialogInterface.BUTTON_NEGATIVE:
1205 if (DBG) log("Negative button");
1206 if (mCurrentDialogId == FW_GET_RESPONSE_ERROR) {
1207 // We failed to get current forwarding settings and the user
1208 // does not wish to continue.
1209 switchToPreviousVoicemailProvider();
1210 }
1211 break;
1212 case DialogInterface.BUTTON_POSITIVE:
1213 if (DBG) log("Positive button");
1214 if (mCurrentDialogId == FW_GET_RESPONSE_ERROR) {
1215 // We failed to get current forwarding settings but the user
1216 // wishes to continue changing settings to the new vm provider
1217 saveVoiceMailAndForwardingNumberStage2();
1218 } else {
1219 finish();
1220 }
1221 return;
1222 default:
1223 // just let the dialog close and go back to the input
1224 }
1225 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1226 // with settings UI. If we were called to explicitly configure voice mail then
1227 // we finish the settings activity here to come back to whatever the user was doing.
1228 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1229 finish();
1230 }
1231 }
1232
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001233 /*
1234 * Activity class methods
1235 */
1236
1237 @Override
1238 protected void onCreate(Bundle icicle) {
1239 super.onCreate(icicle);
1240 if (DBG) log("onCreate(). Intent: " + getIntent());
1241 mPhone = PhoneGlobals.getPhone();
Tyler Gunnbaee2952014-09-10 16:01:02 -07001242 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andrew Lee88b51e22014-10-29 15:48:51 -07001243 mVmProviderSettingsUtil = new VoicemailProviderSettingsUtil(getApplicationContext());
Tyler Gunnbaee2952014-09-10 16:01:02 -07001244
Tyler Gunnbaee2952014-09-10 16:01:02 -07001245 // Show the voicemail preference in onResume if the calling intent specifies the
1246 // ACTION_ADD_VOICEMAIL action.
1247 mShowVoicemailPreference = (icicle == null) &&
1248 getIntent().getAction().equals(ACTION_ADD_VOICEMAIL);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001249
1250 mSubscriptionInfoHelper = new SubscriptionInfoHelper(getIntent());
1251 mSubscriptionInfoHelper.setActionBarTitle(
1252 getActionBar(), getResources(), R.string.call_settings_with_label);
1253 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001254
1255 private void initPhoneAccountPreferences() {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001256 mPhoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001257
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001258 TelecomManager telecomManager = TelecomManager.from(this);
Andrew Lee93c345f2014-10-27 15:25:07 -07001259 TelephonyManager telephonyManager =
1260 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001261
Andrew Lee93c345f2014-10-27 15:25:07 -07001262 if ((telecomManager.getSimCallManagers().isEmpty() && !SipUtil.isVoipSupported(this))
1263 || telephonyManager.getPhoneCount() > 1) {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001264 getPreferenceScreen().removePreference(mPhoneAccountSettingsPreference);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001265 }
1266 }
1267
1268 private boolean canLaunchIntent(Intent intent) {
1269 PackageManager pm = getPackageManager();
1270 return pm.resolveActivity(intent, PackageManager.GET_ACTIVITIES) != null;
1271 }
1272
Tyler Gunnbaee2952014-09-10 16:01:02 -07001273 @Override
1274 protected void onResume() {
1275 super.onResume();
1276 mForeground = true;
1277
1278 PreferenceScreen preferenceScreen = getPreferenceScreen();
1279 if (preferenceScreen != null) {
1280 preferenceScreen.removeAll();
1281 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001282
1283 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001284
Andrew Leedb2fe562014-09-03 15:40:43 -07001285 initPhoneAccountPreferences();
1286
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001287 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001288 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1289 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1290 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1291 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001292
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001293 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1294 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1295 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
1296 mButtonTTY = (ListPreference) findPreference(BUTTON_TTY_KEY);
1297 mVoicemailProviders = (ListPreference) findPreference(BUTTON_VOICEMAIL_PROVIDER_KEY);
Andrew Lee312e8172014-10-23 17:01:36 -07001298 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001299
Andrew Lee2c027892014-10-29 11:29:54 -07001300 mVoicemailProviders.setOnPreferenceChangeListener(this);
1301 mVoicemailSettingsScreen =
1302 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
1303 mVoicemailSettings = (PreferenceScreen)findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
1304 mVoicemailNotificationVibrate =
1305 (CheckBoxPreference) findPreference(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY);
1306 initVoiceMailProviders();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001307
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001308
Andrew Lee64a7d792014-10-15 17:38:38 -07001309 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1310 mButtonDTMF.setOnPreferenceChangeListener(this);
1311 int dtmf = Settings.System.getInt(getContentResolver(),
1312 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1313 mButtonDTMF.setValueIndex(dtmf);
1314 } else {
1315 prefSet.removePreference(mButtonDTMF);
1316 mButtonDTMF = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001317 }
1318
Andrew Lee64a7d792014-10-15 17:38:38 -07001319 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1320 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1321 int autoretry = Settings.Global.getInt(
1322 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1323 mButtonAutoRetry.setChecked(autoretry != 0);
1324 } else {
1325 prefSet.removePreference(mButtonAutoRetry);
1326 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001327 }
1328
Andrew Lee64a7d792014-10-15 17:38:38 -07001329 if (getResources().getBoolean(R.bool.hac_enabled)) {
1330 mButtonHAC.setOnPreferenceChangeListener(this);
1331 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1332 mButtonHAC.setChecked(hac != 0);
1333 } else {
1334 prefSet.removePreference(mButtonHAC);
1335 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001336 }
1337
Andrew Lee64a7d792014-10-15 17:38:38 -07001338 TelecomManager telecomManager = TelecomManager.from(this);
1339 if (telecomManager != null && telecomManager.isTtySupported()) {
1340 mButtonTTY.setOnPreferenceChangeListener(this);
1341 int settingsTtyMode = Settings.Secure.getInt(getContentResolver(),
1342 Settings.Secure.PREFERRED_TTY_MODE,
1343 TelecomManager.TTY_MODE_OFF);
1344 mButtonTTY.setValue(Integer.toString(settingsTtyMode));
1345 updatePreferredTtyModeSummary(settingsTtyMode);
1346 } else {
1347 prefSet.removePreference(mButtonTTY);
1348 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001349 }
1350
1351 if (!getResources().getBoolean(R.bool.world_phone)) {
1352 Preference options = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
Andrew Lee2170a972014-08-13 18:13:01 -07001353 if (options != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001354 prefSet.removePreference(options);
Andrew Lee2170a972014-08-13 18:13:01 -07001355 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001356 options = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
Andrew Lee2170a972014-08-13 18:13:01 -07001357 if (options != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001358 prefSet.removePreference(options);
Andrew Lee2170a972014-08-13 18:13:01 -07001359 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001360
1361 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001362 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001363 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001364 prefSet.removePreference(fdnButton);
1365
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001366 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1367 addPreferencesFromResource(R.xml.cdma_call_privacy);
1368 }
1369 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001370 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(this, FdnSetting.class));
1371
Andrew Lee2170a972014-08-13 18:13:01 -07001372 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
Etan Cohen0ca1c802014-07-07 15:35:48 -07001373 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1374 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001375 } else {
1376 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1377 }
1378 }
1379
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001380 // check the intent that started this activity and pop up the voicemail
1381 // dialog if we've been asked to.
1382 // If we have at least one non default VM provider registered then bring up
1383 // the selection for the VM provider, otherwise bring up a VM number dialog.
1384 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001385 if (mShowVoicemailPreference) {
Tyler Gunnbaee2952014-09-10 16:01:02 -07001386 if (DBG) {
1387 log("ACTION_ADD_VOICEMAIL Intent is thrown. current VM data size: "
1388 + mVMProvidersData.size());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001389 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001390 if (mVMProvidersData.size() > 1) {
1391 simulatePreferenceClick(mVoicemailProviders);
1392 } else {
1393 onPreferenceChange(mVoicemailProviders, DEFAULT_VM_PROVIDER_KEY);
1394 mVoicemailProviders.setValue(DEFAULT_VM_PROVIDER_KEY);
1395 }
1396 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001397 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001398
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001399 updateVoiceNumberField();
1400 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001401
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001402 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
1403 mPhone.getContext());
1404 if (migrateVoicemailVibrationSettingsIfNeeded(prefs)) {
1405 mVoicemailNotificationVibrate.setChecked(prefs.getBoolean(
1406 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, false));
1407 }
1408
Andrew Lee312e8172014-10-23 17:01:36 -07001409 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1410 boolean currentValue =
1411 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1412 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1413 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001414 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1415 } else {
1416 prefSet.removePreference(mEnableVideoCalling);
1417 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001418 }
1419
1420 // Migrate settings from BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY to
1421 // BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, if the latter does not exist.
1422 // Returns true if migration was performed.
1423 public static boolean migrateVoicemailVibrationSettingsIfNeeded(SharedPreferences prefs) {
1424 if (!prefs.contains(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY)) {
1425 String vibrateWhen = prefs.getString(
1426 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY, VOICEMAIL_VIBRATION_NEVER);
1427 // If vibrateWhen is always, then voicemailVibrate should be True.
1428 // otherwise if vibrateWhen is "only in silent mode", or "never", then
1429 // voicemailVibrate = False.
1430 boolean voicemailVibrate = vibrateWhen.equals(VOICEMAIL_VIBRATION_ALWAYS);
1431 final SharedPreferences.Editor editor = prefs.edit();
1432 editor.putBoolean(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, voicemailVibrate);
1433 editor.commit();
1434 return true;
1435 }
1436 return false;
1437 }
1438
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001439 private void handleTTYChange(Preference preference, Object objValue) {
1440 int buttonTtyMode;
1441 buttonTtyMode = Integer.valueOf((String) objValue).intValue();
1442 int settingsTtyMode = android.provider.Settings.Secure.getInt(
1443 getContentResolver(),
Sailesh Nepalbf900542014-07-15 16:18:32 -07001444 android.provider.Settings.Secure.PREFERRED_TTY_MODE,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001445 TelecomManager.TTY_MODE_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001446 if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
1447 Integer.toString(buttonTtyMode));
1448
1449 if (buttonTtyMode != settingsTtyMode) {
1450 switch(buttonTtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001451 case TelecomManager.TTY_MODE_OFF:
1452 case TelecomManager.TTY_MODE_FULL:
1453 case TelecomManager.TTY_MODE_HCO:
1454 case TelecomManager.TTY_MODE_VCO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001455 android.provider.Settings.Secure.putInt(getContentResolver(),
1456 android.provider.Settings.Secure.PREFERRED_TTY_MODE, buttonTtyMode);
1457 break;
1458 default:
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001459 buttonTtyMode = TelecomManager.TTY_MODE_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001460 }
1461
1462 mButtonTTY.setValue(Integer.toString(buttonTtyMode));
1463 updatePreferredTtyModeSummary(buttonTtyMode);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001464 Intent ttyModeChanged = new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
1465 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001466 sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
1467 }
1468 }
1469
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001470 private void updatePreferredTtyModeSummary(int TtyMode) {
1471 String [] txts = getResources().getStringArray(R.array.tty_mode_entries);
1472 switch(TtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001473 case TelecomManager.TTY_MODE_OFF:
1474 case TelecomManager.TTY_MODE_HCO:
1475 case TelecomManager.TTY_MODE_VCO:
1476 case TelecomManager.TTY_MODE_FULL:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001477 mButtonTTY.setSummary(txts[TtyMode]);
1478 break;
1479 default:
1480 mButtonTTY.setEnabled(false);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001481 mButtonTTY.setSummary(txts[TelecomManager.TTY_MODE_OFF]);
Sailesh Nepalbf900542014-07-15 16:18:32 -07001482 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001483 }
1484 }
1485
1486 private static void log(String msg) {
1487 Log.d(LOG_TAG, msg);
1488 }
1489
1490 /**
1491 * Updates the look of the VM preference widgets based on current VM provider settings.
1492 * Note that the provider name is loaded form the found activity via loadLabel in
1493 * {@link #initVoiceMailProviders()} in order for it to be localizable.
1494 */
1495 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1496 final String key = currentProviderSetting;
1497 final VoiceMailProvider provider = mVMProvidersData.get(key);
1498
1499 /* This is the case when we are coming up on a freshly wiped phone and there is no
1500 persisted value for the list preference mVoicemailProviders.
1501 In this case we want to show the UI asking the user to select a voicemail provider as
1502 opposed to silently falling back to default one. */
1503 if (provider == null) {
1504 if (DBG) {
1505 log("updateVMPreferenceWidget: provider for the key \"" + key + "\" is null.");
1506 }
1507 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1508 mVoicemailSettings.setEnabled(false);
1509 mVoicemailSettings.setIntent(null);
1510
1511 mVoicemailNotificationVibrate.setEnabled(false);
1512 } else {
1513 if (DBG) {
1514 log("updateVMPreferenceWidget: provider for the key \"" + key + "\".."
1515 + "name: " + provider.name
1516 + ", intent: " + provider.intent);
1517 }
1518 final String providerName = provider.name;
1519 mVoicemailProviders.setSummary(providerName);
1520 mVoicemailSettings.setEnabled(true);
1521 mVoicemailSettings.setIntent(provider.intent);
1522
1523 mVoicemailNotificationVibrate.setEnabled(true);
1524 }
1525 }
1526
1527 /**
1528 * Enumerates existing VM providers and puts their data into the list and populates
1529 * the preference list objects with their names.
1530 * In case we are called with ACTION_ADD_VOICEMAIL intent the intent may have
1531 * an extra string called IGNORE_PROVIDER_EXTRA with "package.activityName" of the provider
1532 * which should be hidden when we bring up the list of possible VM providers to choose.
1533 */
1534 private void initVoiceMailProviders() {
1535 if (DBG) log("initVoiceMailProviders()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001536
1537 String providerToIgnore = null;
Andrew Leef1776d82014-11-04 14:45:02 -08001538 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)
1539 && getIntent().hasExtra(IGNORE_PROVIDER_EXTRA)) {
1540 providerToIgnore = getIntent().getStringExtra(IGNORE_PROVIDER_EXTRA);
1541 // Remove this provider from the list.
1542 if (!TextUtils.isEmpty(providerToIgnore)) {
1543 if (DBG) log("Found ACTION_ADD_VOICEMAIL. providerToIgnore= " + providerToIgnore);
Andrew Lee88b51e22014-10-29 15:48:51 -07001544 mVmProviderSettingsUtil.delete(providerToIgnore);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001545 }
1546 }
1547
1548 mVMProvidersData.clear();
1549
Andrew Leef1776d82014-11-04 14:45:02 -08001550 List<String> entries = new ArrayList<String>();
1551 List<String> values = new ArrayList<String>();
1552
1553 // Add default voicemail provider.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001554 final String myCarrier = getString(R.string.voicemail_default);
1555 mVMProvidersData.put(DEFAULT_VM_PROVIDER_KEY, new VoiceMailProvider(myCarrier, null));
Andrew Leef1776d82014-11-04 14:45:02 -08001556 entries.add(myCarrier);
1557 values.add(DEFAULT_VM_PROVIDER_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001558
Andrew Leef1776d82014-11-04 14:45:02 -08001559 // Add other voicemail providers.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001560 PackageManager pm = getPackageManager();
Andrew Leef1776d82014-11-04 14:45:02 -08001561 Intent intent = new Intent(ACTION_CONFIGURE_VOICEMAIL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001562 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001563 for (int i = 0; i < resolveInfos.size(); i++) {
1564 final ResolveInfo ri= resolveInfos.get(i);
1565 final ActivityInfo currentActivityInfo = ri.activityInfo;
Andrew Lee6214e2b2014-11-04 13:57:38 -08001566 final String key = currentActivityInfo.name;
Andrew Leef1776d82014-11-04 14:45:02 -08001567
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001568 if (key.equals(providerToIgnore)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001569 continue;
1570 }
Andrew Leef1776d82014-11-04 14:45:02 -08001571
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001572 if (DBG) log("Loading key: " + key);
1573 final String nameForDisplay = ri.loadLabel(pm).toString();
1574 Intent providerIntent = new Intent();
1575 providerIntent.setAction(ACTION_CONFIGURE_VOICEMAIL);
Andrew Leef1776d82014-11-04 14:45:02 -08001576 providerIntent.setClassName(currentActivityInfo.packageName, currentActivityInfo.name);
1577 VoiceMailProvider vmProvider = new VoiceMailProvider(nameForDisplay, providerIntent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001578
Andrew Leef1776d82014-11-04 14:45:02 -08001579 if (DBG) log("Store VoiceMailProvider. Key: " + key + " -> " + vmProvider.toString());
1580 mVMProvidersData.put(key, vmProvider);
1581 entries.add(vmProvider.name);
1582 values.add(key);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001583 }
1584
Andrew Leef1776d82014-11-04 14:45:02 -08001585 mVoicemailProviders.setEntries(entries.toArray(new String[0]));
1586 mVoicemailProviders.setEntryValues(values.toArray(new String[0]));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001587
Andrew Leef1776d82014-11-04 14:45:02 -08001588 // Remember the current Voicemail Provider key as a "previous" key. This will be used when
1589 // we fail to update Voicemail Provider, which requires rollback. We will update this when
1590 // the VM Provider setting is successfully updated.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001591 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1592 if (DBG) log("Set up the first mPreviousVMProviderKey: " + mPreviousVMProviderKey);
1593
1594 // Finally update the preference texts.
1595 updateVMPreferenceWidgets(mPreviousVMProviderKey);
1596 }
1597
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001598 /**
1599 * Simulates user clicking on a passed preference.
1600 * Usually needed when the preference is a dialog preference and we want to invoke
1601 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001602 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001603 */
1604 private void simulatePreferenceClick(Preference preference) {
1605 // Go through settings until we find our setting
1606 // and then simulate a click on it to bring up the dialog
1607 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1608 for (int idx = 0; idx < adapter.getCount(); idx++) {
1609 if (adapter.getItem(idx) == preference) {
1610 getPreferenceScreen().onItemClick(this.getListView(),
1611 null, idx, adapter.getItemId(idx));
1612 break;
1613 }
1614 }
1615 }
1616
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001617 private String getCurrentVoicemailProviderKey() {
1618 final String key = mVoicemailProviders.getValue();
1619 return (key != null) ? key : DEFAULT_VM_PROVIDER_KEY;
1620 }
1621
1622 @Override
1623 public boolean onOptionsItemSelected(MenuItem item) {
1624 final int itemId = item.getItemId();
1625 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001626 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001627 return true;
1628 }
1629 return super.onOptionsItemSelected(item);
1630 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001631 /**
1632 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1633 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1634 */
1635 public static void goUpToTopLevelSetting(Activity activity) {
1636 Intent intent = new Intent(activity, CallFeaturesSetting.class);
1637 intent.setAction(Intent.ACTION_MAIN);
1638 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1639 activity.startActivity(intent);
1640 activity.finish();
1641 }
1642}