blob: f99692298d126c1c76184ae1407cabbf481e919f [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() {
1091 if (DBG) {
1092 log("updateVoiceNumberField(). mSubMenuVoicemailSettings=" + mSubMenuVoicemailSettings);
1093 }
1094 if (mSubMenuVoicemailSettings == null) {
1095 return;
1096 }
1097
1098 mOldVmNumber = mPhone.getVoiceMailNumber();
1099 if (mOldVmNumber == null) {
1100 mOldVmNumber = "";
1101 }
1102 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1103 final String summary = (mOldVmNumber.length() > 0) ? mOldVmNumber :
1104 getString(R.string.voicemail_number_not_set);
1105 mSubMenuVoicemailSettings.setSummary(summary);
1106 }
1107
1108 /*
1109 * Helper Methods for Activity class.
1110 * The initial query commands are split into two pieces now
1111 * for individual expansion. This combined with the ability
1112 * to cancel queries allows for a much better user experience,
1113 * and also ensures that the user only waits to update the
1114 * data that is relevant.
1115 */
1116
1117 @Override
1118 protected void onPrepareDialog(int id, Dialog dialog) {
1119 super.onPrepareDialog(id, dialog);
1120 mCurrentDialogId = id;
1121 }
1122
1123 // dialog creation method, called by showDialog()
1124 @Override
1125 protected Dialog onCreateDialog(int id) {
1126 if ((id == VM_RESPONSE_ERROR) || (id == VM_NOCHANGE_ERROR) ||
1127 (id == FW_SET_RESPONSE_ERROR) || (id == FW_GET_RESPONSE_ERROR) ||
1128 (id == VOICEMAIL_DIALOG_CONFIRM)) {
1129
1130 AlertDialog.Builder b = new AlertDialog.Builder(this);
1131
1132 int msgId;
1133 int titleId = R.string.error_updating_title;
1134 switch (id) {
1135 case VOICEMAIL_DIALOG_CONFIRM:
1136 msgId = R.string.vm_changed;
1137 titleId = R.string.voicemail;
1138 // Set Button 2
1139 b.setNegativeButton(R.string.close_dialog, this);
1140 break;
1141 case VM_NOCHANGE_ERROR:
1142 // even though this is technically an error,
1143 // keep the title friendly.
1144 msgId = R.string.no_change;
1145 titleId = R.string.voicemail;
1146 // Set Button 2
1147 b.setNegativeButton(R.string.close_dialog, this);
1148 break;
1149 case VM_RESPONSE_ERROR:
1150 msgId = R.string.vm_change_failed;
1151 // Set Button 1
1152 b.setPositiveButton(R.string.close_dialog, this);
1153 break;
1154 case FW_SET_RESPONSE_ERROR:
1155 msgId = R.string.fw_change_failed;
1156 // Set Button 1
1157 b.setPositiveButton(R.string.close_dialog, this);
1158 break;
1159 case FW_GET_RESPONSE_ERROR:
1160 msgId = R.string.fw_get_in_vm_failed;
1161 b.setPositiveButton(R.string.alert_dialog_yes, this);
1162 b.setNegativeButton(R.string.alert_dialog_no, this);
1163 break;
1164 default:
1165 msgId = R.string.exception_error;
1166 // Set Button 3, tells the activity that the error is
1167 // not recoverable on dialog exit.
1168 b.setNeutralButton(R.string.close_dialog, this);
1169 break;
1170 }
1171
1172 b.setTitle(getText(titleId));
1173 String message = getText(msgId).toString();
1174 b.setMessage(message);
1175 b.setCancelable(false);
1176 AlertDialog dialog = b.create();
1177
1178 // make the dialog more obvious by bluring the background.
1179 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
1180
1181 return dialog;
1182 } else if (id == VOICEMAIL_FWD_SAVING_DIALOG || id == VOICEMAIL_FWD_READING_DIALOG ||
1183 id == VOICEMAIL_REVERTING_DIALOG) {
1184 ProgressDialog dialog = new ProgressDialog(this);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001185 dialog.setTitle(getText(R.string.call_settings));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001186 dialog.setIndeterminate(true);
1187 dialog.setCancelable(false);
1188 dialog.setMessage(getText(
1189 id == VOICEMAIL_FWD_SAVING_DIALOG ? R.string.updating_settings :
1190 (id == VOICEMAIL_REVERTING_DIALOG ? R.string.reverting_settings :
1191 R.string.reading_settings)));
1192 return dialog;
1193 }
1194
1195
1196 return null;
1197 }
1198
1199 // This is a method implemented for DialogInterface.OnClickListener.
1200 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1201 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1202 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1203 public void onClick(DialogInterface dialog, int which) {
1204 dialog.dismiss();
1205 switch (which){
1206 case DialogInterface.BUTTON_NEUTRAL:
1207 if (DBG) log("Neutral button");
1208 break;
1209 case DialogInterface.BUTTON_NEGATIVE:
1210 if (DBG) log("Negative button");
1211 if (mCurrentDialogId == FW_GET_RESPONSE_ERROR) {
1212 // We failed to get current forwarding settings and the user
1213 // does not wish to continue.
1214 switchToPreviousVoicemailProvider();
1215 }
1216 break;
1217 case DialogInterface.BUTTON_POSITIVE:
1218 if (DBG) log("Positive button");
1219 if (mCurrentDialogId == FW_GET_RESPONSE_ERROR) {
1220 // We failed to get current forwarding settings but the user
1221 // wishes to continue changing settings to the new vm provider
1222 saveVoiceMailAndForwardingNumberStage2();
1223 } else {
1224 finish();
1225 }
1226 return;
1227 default:
1228 // just let the dialog close and go back to the input
1229 }
1230 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1231 // with settings UI. If we were called to explicitly configure voice mail then
1232 // we finish the settings activity here to come back to whatever the user was doing.
1233 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1234 finish();
1235 }
1236 }
1237
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001238 /*
1239 * Activity class methods
1240 */
1241
1242 @Override
1243 protected void onCreate(Bundle icicle) {
1244 super.onCreate(icicle);
1245 if (DBG) log("onCreate(). Intent: " + getIntent());
1246 mPhone = PhoneGlobals.getPhone();
Tyler Gunnbaee2952014-09-10 16:01:02 -07001247 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andrew Lee88b51e22014-10-29 15:48:51 -07001248 mVmProviderSettingsUtil = new VoicemailProviderSettingsUtil(getApplicationContext());
Tyler Gunnbaee2952014-09-10 16:01:02 -07001249
Tyler Gunnbaee2952014-09-10 16:01:02 -07001250 // Show the voicemail preference in onResume if the calling intent specifies the
1251 // ACTION_ADD_VOICEMAIL action.
1252 mShowVoicemailPreference = (icicle == null) &&
1253 getIntent().getAction().equals(ACTION_ADD_VOICEMAIL);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001254
1255 mSubscriptionInfoHelper = new SubscriptionInfoHelper(getIntent());
1256 mSubscriptionInfoHelper.setActionBarTitle(
1257 getActionBar(), getResources(), R.string.call_settings_with_label);
1258 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001259
1260 private void initPhoneAccountPreferences() {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001261 mPhoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001262
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001263 TelecomManager telecomManager = TelecomManager.from(this);
Andrew Lee93c345f2014-10-27 15:25:07 -07001264 TelephonyManager telephonyManager =
1265 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001266
Andrew Lee93c345f2014-10-27 15:25:07 -07001267 if ((telecomManager.getSimCallManagers().isEmpty() && !SipUtil.isVoipSupported(this))
1268 || telephonyManager.getPhoneCount() > 1) {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001269 getPreferenceScreen().removePreference(mPhoneAccountSettingsPreference);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001270 }
1271 }
1272
1273 private boolean canLaunchIntent(Intent intent) {
1274 PackageManager pm = getPackageManager();
1275 return pm.resolveActivity(intent, PackageManager.GET_ACTIVITIES) != null;
1276 }
1277
Tyler Gunnbaee2952014-09-10 16:01:02 -07001278 @Override
1279 protected void onResume() {
1280 super.onResume();
1281 mForeground = true;
1282
1283 PreferenceScreen preferenceScreen = getPreferenceScreen();
1284 if (preferenceScreen != null) {
1285 preferenceScreen.removeAll();
1286 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001287
1288 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001289
Andrew Leedb2fe562014-09-03 15:40:43 -07001290 initPhoneAccountPreferences();
1291
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001292 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001293 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1294 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1295 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1296 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001297
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001298 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1299 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1300 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
1301 mButtonTTY = (ListPreference) findPreference(BUTTON_TTY_KEY);
1302 mVoicemailProviders = (ListPreference) findPreference(BUTTON_VOICEMAIL_PROVIDER_KEY);
Andrew Lee312e8172014-10-23 17:01:36 -07001303 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001304
Andrew Lee2c027892014-10-29 11:29:54 -07001305 mVoicemailProviders.setOnPreferenceChangeListener(this);
1306 mVoicemailSettingsScreen =
1307 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
1308 mVoicemailSettings = (PreferenceScreen)findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
1309 mVoicemailNotificationVibrate =
1310 (CheckBoxPreference) findPreference(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY);
1311 initVoiceMailProviders();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001312
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001313
Andrew Lee64a7d792014-10-15 17:38:38 -07001314 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1315 mButtonDTMF.setOnPreferenceChangeListener(this);
1316 int dtmf = Settings.System.getInt(getContentResolver(),
1317 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1318 mButtonDTMF.setValueIndex(dtmf);
1319 } else {
1320 prefSet.removePreference(mButtonDTMF);
1321 mButtonDTMF = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001322 }
1323
Andrew Lee64a7d792014-10-15 17:38:38 -07001324 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1325 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1326 int autoretry = Settings.Global.getInt(
1327 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1328 mButtonAutoRetry.setChecked(autoretry != 0);
1329 } else {
1330 prefSet.removePreference(mButtonAutoRetry);
1331 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001332 }
1333
Andrew Lee64a7d792014-10-15 17:38:38 -07001334 if (getResources().getBoolean(R.bool.hac_enabled)) {
1335 mButtonHAC.setOnPreferenceChangeListener(this);
1336 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1337 mButtonHAC.setChecked(hac != 0);
1338 } else {
1339 prefSet.removePreference(mButtonHAC);
1340 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001341 }
1342
Andrew Lee64a7d792014-10-15 17:38:38 -07001343 TelecomManager telecomManager = TelecomManager.from(this);
1344 if (telecomManager != null && telecomManager.isTtySupported()) {
1345 mButtonTTY.setOnPreferenceChangeListener(this);
1346 int settingsTtyMode = Settings.Secure.getInt(getContentResolver(),
1347 Settings.Secure.PREFERRED_TTY_MODE,
1348 TelecomManager.TTY_MODE_OFF);
1349 mButtonTTY.setValue(Integer.toString(settingsTtyMode));
1350 updatePreferredTtyModeSummary(settingsTtyMode);
1351 } else {
1352 prefSet.removePreference(mButtonTTY);
1353 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001354 }
1355
1356 if (!getResources().getBoolean(R.bool.world_phone)) {
1357 Preference options = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
Andrew Lee2170a972014-08-13 18:13:01 -07001358 if (options != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001359 prefSet.removePreference(options);
Andrew Lee2170a972014-08-13 18:13:01 -07001360 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001361 options = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
Andrew Lee2170a972014-08-13 18:13:01 -07001362 if (options != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001363 prefSet.removePreference(options);
Andrew Lee2170a972014-08-13 18:13:01 -07001364 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001365
1366 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001367 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001368 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001369 prefSet.removePreference(fdnButton);
1370
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001371 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1372 addPreferencesFromResource(R.xml.cdma_call_privacy);
1373 }
1374 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001375 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(this, FdnSetting.class));
1376
Andrew Lee2170a972014-08-13 18:13:01 -07001377 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
Etan Cohen0ca1c802014-07-07 15:35:48 -07001378 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1379 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001380 } else {
1381 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1382 }
1383 }
1384
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001385 // check the intent that started this activity and pop up the voicemail
1386 // dialog if we've been asked to.
1387 // If we have at least one non default VM provider registered then bring up
1388 // the selection for the VM provider, otherwise bring up a VM number dialog.
1389 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001390 if (mShowVoicemailPreference) {
Tyler Gunnbaee2952014-09-10 16:01:02 -07001391 if (DBG) {
1392 log("ACTION_ADD_VOICEMAIL Intent is thrown. current VM data size: "
1393 + mVMProvidersData.size());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001394 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001395 if (mVMProvidersData.size() > 1) {
1396 simulatePreferenceClick(mVoicemailProviders);
1397 } else {
1398 onPreferenceChange(mVoicemailProviders, DEFAULT_VM_PROVIDER_KEY);
1399 mVoicemailProviders.setValue(DEFAULT_VM_PROVIDER_KEY);
1400 }
1401 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001402 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001403
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001404 updateVoiceNumberField();
1405 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001406
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001407 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
1408 mPhone.getContext());
1409 if (migrateVoicemailVibrationSettingsIfNeeded(prefs)) {
1410 mVoicemailNotificationVibrate.setChecked(prefs.getBoolean(
1411 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, false));
1412 }
1413
Andrew Lee312e8172014-10-23 17:01:36 -07001414 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1415 boolean currentValue =
1416 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1417 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1418 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001419 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1420 } else {
1421 prefSet.removePreference(mEnableVideoCalling);
1422 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001423 }
1424
1425 // Migrate settings from BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY to
1426 // BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, if the latter does not exist.
1427 // Returns true if migration was performed.
1428 public static boolean migrateVoicemailVibrationSettingsIfNeeded(SharedPreferences prefs) {
1429 if (!prefs.contains(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY)) {
1430 String vibrateWhen = prefs.getString(
1431 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY, VOICEMAIL_VIBRATION_NEVER);
1432 // If vibrateWhen is always, then voicemailVibrate should be True.
1433 // otherwise if vibrateWhen is "only in silent mode", or "never", then
1434 // voicemailVibrate = False.
1435 boolean voicemailVibrate = vibrateWhen.equals(VOICEMAIL_VIBRATION_ALWAYS);
1436 final SharedPreferences.Editor editor = prefs.edit();
1437 editor.putBoolean(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, voicemailVibrate);
1438 editor.commit();
1439 return true;
1440 }
1441 return false;
1442 }
1443
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001444 private void handleTTYChange(Preference preference, Object objValue) {
1445 int buttonTtyMode;
1446 buttonTtyMode = Integer.valueOf((String) objValue).intValue();
1447 int settingsTtyMode = android.provider.Settings.Secure.getInt(
1448 getContentResolver(),
Sailesh Nepalbf900542014-07-15 16:18:32 -07001449 android.provider.Settings.Secure.PREFERRED_TTY_MODE,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001450 TelecomManager.TTY_MODE_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001451 if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
1452 Integer.toString(buttonTtyMode));
1453
1454 if (buttonTtyMode != settingsTtyMode) {
1455 switch(buttonTtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001456 case TelecomManager.TTY_MODE_OFF:
1457 case TelecomManager.TTY_MODE_FULL:
1458 case TelecomManager.TTY_MODE_HCO:
1459 case TelecomManager.TTY_MODE_VCO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001460 android.provider.Settings.Secure.putInt(getContentResolver(),
1461 android.provider.Settings.Secure.PREFERRED_TTY_MODE, buttonTtyMode);
1462 break;
1463 default:
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001464 buttonTtyMode = TelecomManager.TTY_MODE_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001465 }
1466
1467 mButtonTTY.setValue(Integer.toString(buttonTtyMode));
1468 updatePreferredTtyModeSummary(buttonTtyMode);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001469 Intent ttyModeChanged = new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
1470 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001471 sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
1472 }
1473 }
1474
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001475 private void updatePreferredTtyModeSummary(int TtyMode) {
1476 String [] txts = getResources().getStringArray(R.array.tty_mode_entries);
1477 switch(TtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001478 case TelecomManager.TTY_MODE_OFF:
1479 case TelecomManager.TTY_MODE_HCO:
1480 case TelecomManager.TTY_MODE_VCO:
1481 case TelecomManager.TTY_MODE_FULL:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001482 mButtonTTY.setSummary(txts[TtyMode]);
1483 break;
1484 default:
1485 mButtonTTY.setEnabled(false);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001486 mButtonTTY.setSummary(txts[TelecomManager.TTY_MODE_OFF]);
Sailesh Nepalbf900542014-07-15 16:18:32 -07001487 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001488 }
1489 }
1490
1491 private static void log(String msg) {
1492 Log.d(LOG_TAG, msg);
1493 }
1494
1495 /**
1496 * Updates the look of the VM preference widgets based on current VM provider settings.
1497 * Note that the provider name is loaded form the found activity via loadLabel in
1498 * {@link #initVoiceMailProviders()} in order for it to be localizable.
1499 */
1500 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1501 final String key = currentProviderSetting;
1502 final VoiceMailProvider provider = mVMProvidersData.get(key);
1503
1504 /* This is the case when we are coming up on a freshly wiped phone and there is no
1505 persisted value for the list preference mVoicemailProviders.
1506 In this case we want to show the UI asking the user to select a voicemail provider as
1507 opposed to silently falling back to default one. */
1508 if (provider == null) {
1509 if (DBG) {
1510 log("updateVMPreferenceWidget: provider for the key \"" + key + "\" is null.");
1511 }
1512 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1513 mVoicemailSettings.setEnabled(false);
1514 mVoicemailSettings.setIntent(null);
1515
1516 mVoicemailNotificationVibrate.setEnabled(false);
1517 } else {
1518 if (DBG) {
1519 log("updateVMPreferenceWidget: provider for the key \"" + key + "\".."
1520 + "name: " + provider.name
1521 + ", intent: " + provider.intent);
1522 }
1523 final String providerName = provider.name;
1524 mVoicemailProviders.setSummary(providerName);
1525 mVoicemailSettings.setEnabled(true);
1526 mVoicemailSettings.setIntent(provider.intent);
1527
1528 mVoicemailNotificationVibrate.setEnabled(true);
1529 }
1530 }
1531
1532 /**
1533 * Enumerates existing VM providers and puts their data into the list and populates
1534 * the preference list objects with their names.
1535 * In case we are called with ACTION_ADD_VOICEMAIL intent the intent may have
1536 * an extra string called IGNORE_PROVIDER_EXTRA with "package.activityName" of the provider
1537 * which should be hidden when we bring up the list of possible VM providers to choose.
1538 */
1539 private void initVoiceMailProviders() {
1540 if (DBG) log("initVoiceMailProviders()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001541
1542 String providerToIgnore = null;
Andrew Leef1776d82014-11-04 14:45:02 -08001543 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)
1544 && getIntent().hasExtra(IGNORE_PROVIDER_EXTRA)) {
1545 providerToIgnore = getIntent().getStringExtra(IGNORE_PROVIDER_EXTRA);
1546 // Remove this provider from the list.
1547 if (!TextUtils.isEmpty(providerToIgnore)) {
1548 if (DBG) log("Found ACTION_ADD_VOICEMAIL. providerToIgnore= " + providerToIgnore);
Andrew Lee88b51e22014-10-29 15:48:51 -07001549 mVmProviderSettingsUtil.delete(providerToIgnore);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001550 }
1551 }
1552
1553 mVMProvidersData.clear();
1554
Andrew Leef1776d82014-11-04 14:45:02 -08001555 List<String> entries = new ArrayList<String>();
1556 List<String> values = new ArrayList<String>();
1557
1558 // Add default voicemail provider.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001559 final String myCarrier = getString(R.string.voicemail_default);
1560 mVMProvidersData.put(DEFAULT_VM_PROVIDER_KEY, new VoiceMailProvider(myCarrier, null));
Andrew Leef1776d82014-11-04 14:45:02 -08001561 entries.add(myCarrier);
1562 values.add(DEFAULT_VM_PROVIDER_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001563
Andrew Leef1776d82014-11-04 14:45:02 -08001564 // Add other voicemail providers.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001565 PackageManager pm = getPackageManager();
Andrew Leef1776d82014-11-04 14:45:02 -08001566 Intent intent = new Intent(ACTION_CONFIGURE_VOICEMAIL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001567 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001568 for (int i = 0; i < resolveInfos.size(); i++) {
1569 final ResolveInfo ri= resolveInfos.get(i);
1570 final ActivityInfo currentActivityInfo = ri.activityInfo;
Andrew Lee6214e2b2014-11-04 13:57:38 -08001571 final String key = currentActivityInfo.name;
Andrew Leef1776d82014-11-04 14:45:02 -08001572
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001573 if (key.equals(providerToIgnore)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001574 continue;
1575 }
Andrew Leef1776d82014-11-04 14:45:02 -08001576
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001577 if (DBG) log("Loading key: " + key);
1578 final String nameForDisplay = ri.loadLabel(pm).toString();
1579 Intent providerIntent = new Intent();
1580 providerIntent.setAction(ACTION_CONFIGURE_VOICEMAIL);
Andrew Leef1776d82014-11-04 14:45:02 -08001581 providerIntent.setClassName(currentActivityInfo.packageName, currentActivityInfo.name);
1582 VoiceMailProvider vmProvider = new VoiceMailProvider(nameForDisplay, providerIntent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001583
Andrew Leef1776d82014-11-04 14:45:02 -08001584 if (DBG) log("Store VoiceMailProvider. Key: " + key + " -> " + vmProvider.toString());
1585 mVMProvidersData.put(key, vmProvider);
1586 entries.add(vmProvider.name);
1587 values.add(key);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001588 }
1589
Andrew Leef1776d82014-11-04 14:45:02 -08001590 mVoicemailProviders.setEntries(entries.toArray(new String[0]));
1591 mVoicemailProviders.setEntryValues(values.toArray(new String[0]));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001592
Andrew Leef1776d82014-11-04 14:45:02 -08001593 // Remember the current Voicemail Provider key as a "previous" key. This will be used when
1594 // we fail to update Voicemail Provider, which requires rollback. We will update this when
1595 // the VM Provider setting is successfully updated.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001596 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1597 if (DBG) log("Set up the first mPreviousVMProviderKey: " + mPreviousVMProviderKey);
1598
1599 // Finally update the preference texts.
1600 updateVMPreferenceWidgets(mPreviousVMProviderKey);
1601 }
1602
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001603 /**
1604 * Simulates user clicking on a passed preference.
1605 * Usually needed when the preference is a dialog preference and we want to invoke
1606 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001607 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001608 */
1609 private void simulatePreferenceClick(Preference preference) {
1610 // Go through settings until we find our setting
1611 // and then simulate a click on it to bring up the dialog
1612 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1613 for (int idx = 0; idx < adapter.getCount(); idx++) {
1614 if (adapter.getItem(idx) == preference) {
1615 getPreferenceScreen().onItemClick(this.getListView(),
1616 null, idx, adapter.getItemId(idx));
1617 break;
1618 }
1619 }
1620 }
1621
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001622 private String getCurrentVoicemailProviderKey() {
1623 final String key = mVoicemailProviders.getValue();
1624 return (key != null) ? key : DEFAULT_VM_PROVIDER_KEY;
1625 }
1626
1627 @Override
1628 public boolean onOptionsItemSelected(MenuItem item) {
1629 final int itemId = item.getItemId();
1630 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001631 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001632 return true;
1633 }
1634 return super.onOptionsItemSelected(item);
1635 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001636 /**
1637 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1638 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1639 */
1640 public static void goUpToTopLevelSetting(Activity activity) {
1641 Intent intent = new Intent(activity, CallFeaturesSetting.class);
1642 intent.setAction(Intent.ACTION_MAIN);
1643 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1644 activity.startActivity(intent);
1645 activity.finish();
1646 }
1647}