blob: eb1d7d87e5ac15c5d50663078c216fec4432985f [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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import java.util.Collection;
72import java.util.HashMap;
73import java.util.HashSet;
74import java.util.Iterator;
75import java.util.List;
76import java.util.Map;
77
78/**
79 * Top level "Call settings" UI; see res/xml/call_feature_setting.xml
80 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070081 * This preference screen is the root of the "Call settings" hierarchy available from the Phone
82 * app; the settings here let you control various features related to phone calls (including
83 * voicemail settings, the "Respond via SMS" feature, and others.) It's used only on
84 * voice-capable phone devices.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070085 *
86 * Note that this activity is part of the package com.android.phone, even
87 * though you reach it from the "Phone" app (i.e. DialtactsActivity) which
88 * is from the package com.android.contacts.
89 *
90 * For the "Mobile network settings" screen under the main Settings app,
91 * See {@link MobileNetworkSettings}.
92 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070093 * TODO: Settings should be split into PreferenceFragments where possible (ie. voicemail).
Andrew Lee2170a972014-08-13 18:13:01 -070094 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095 * @see com.android.phone.MobileNetworkSettings
96 */
97public class CallFeaturesSetting extends PreferenceActivity
98 implements DialogInterface.OnClickListener,
Andrew Lee2170a972014-08-13 18:13:01 -070099 Preference.OnPreferenceChangeListener,
Andrew Lee2170a972014-08-13 18:13:01 -0700100 EditPhoneNumberPreference.OnDialogClosedListener,
Andrew Leece8ae2a2014-09-10 10:41:48 -0700101 EditPhoneNumberPreference.GetDefaultNumberListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102 private static final String LOG_TAG = "CallFeaturesSetting";
103 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
Andrew Lee77527ac2014-10-21 16:57:39 -0700104 // STOPSHIP if true. Flag to override behavior default behavior to hide VT setting.
105 private static final boolean ENABLE_VT_FLAG = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700106
107 /**
108 * Intent action to bring up Voicemail Provider settings.
109 *
110 * @see #IGNORE_PROVIDER_EXTRA
111 */
112 public static final String ACTION_ADD_VOICEMAIL =
113 "com.android.phone.CallFeaturesSetting.ADD_VOICEMAIL";
114 // intent action sent by this activity to a voice mail provider
115 // to trigger its configuration UI
116 public static final String ACTION_CONFIGURE_VOICEMAIL =
117 "com.android.phone.CallFeaturesSetting.CONFIGURE_VOICEMAIL";
118 // Extra put in the return from VM provider config containing voicemail number to set
119 public static final String VM_NUMBER_EXTRA = "com.android.phone.VoicemailNumber";
120 // Extra put in the return from VM provider config containing call forwarding number to set
121 public static final String FWD_NUMBER_EXTRA = "com.android.phone.ForwardingNumber";
122 // Extra put in the return from VM provider config containing call forwarding number to set
123 public static final String FWD_NUMBER_TIME_EXTRA = "com.android.phone.ForwardingNumberTime";
124 // If the VM provider returns non null value in this extra we will force the user to
125 // choose another VM provider
126 public static final String SIGNOUT_EXTRA = "com.android.phone.Signout";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128 // Key identifying the default vocie mail provider
129 public static final String DEFAULT_VM_PROVIDER_KEY = "";
130
131 /**
132 * String Extra put into ACTION_ADD_VOICEMAIL call to indicate which provider should be hidden
133 * in the list of providers presented to the user. This allows a provider which is being
134 * disabled (e.g. GV user logging out) to force the user to pick some other provider.
135 */
136 public static final String IGNORE_PROVIDER_EXTRA = "com.android.phone.ProviderToIgnore";
137
138 // string constants
139 private static final String NUM_PROJECTION[] = {CommonDataKinds.Phone.NUMBER};
140
141 // String keys for preference lookup
142 // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
Andrew Lee97708a42014-09-25 12:39:07 -0700143 private static final String VOICEMAIL_SETTING_SCREEN_PREF_KEY = "button_voicemail_category_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700144 private static final String BUTTON_VOICEMAIL_KEY = "button_voicemail_key";
145 private static final String BUTTON_VOICEMAIL_PROVIDER_KEY = "button_voicemail_provider_key";
146 private static final String BUTTON_VOICEMAIL_SETTING_KEY = "button_voicemail_setting_key";
147 // New preference key for voicemail notification vibration
148 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY =
149 "button_voicemail_notification_vibrate_key";
150 // Old preference key for voicemail notification vibration. Used for migration to the new
151 // preference key only.
152 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY =
153 "button_voicemail_notification_vibrate_when_key";
154 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_RINGTONE_KEY =
155 "button_voicemail_notification_ringtone_key";
156 private static final String BUTTON_FDN_KEY = "button_fdn_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700157
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700158 private static final String BUTTON_DTMF_KEY = "button_dtmf_settings";
159 private static final String BUTTON_RETRY_KEY = "button_auto_retry_key";
160 private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
161 private static final String BUTTON_HAC_KEY = "button_hac_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700162
163 private static final String BUTTON_GSM_UMTS_OPTIONS = "button_gsm_more_expand_key";
164 private static final String BUTTON_CDMA_OPTIONS = "button_cdma_more_expand_key";
165
Andrew Leedb2fe562014-09-03 15:40:43 -0700166 private static final String DEFAULT_OUTGOING_ACCOUNT_KEY = "default_outgoing_account";
Andrew Leece8ae2a2014-09-10 10:41:48 -0700167 private static final String PHONE_ACCOUNT_SETTINGS_KEY =
168 "phone_account_settings_preference_screen";
Andrew Leedb2fe562014-09-03 15:40:43 -0700169
Andrew Leedf14ead2014-10-17 14:22:52 -0700170 private static final String ENABLE_VIDEO_CALLING_KEY = "button_enable_video_calling";
171
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700172 /** Event for Async voicemail change call */
173 private static final int EVENT_VOICEMAIL_CHANGED = 500;
174 private static final int EVENT_FORWARDING_CHANGED = 501;
175 private static final int EVENT_FORWARDING_GET_COMPLETED = 502;
176
Andrew Lee2170a972014-08-13 18:13:01 -0700177 private static final int MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY = 1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700179 public static final String HAC_KEY = "HACSetting";
180 public static final String HAC_VAL_ON = "ON";
181 public static final String HAC_VAL_OFF = "OFF";
182
183 /** Handle to voicemail pref */
184 private static final int VOICEMAIL_PREF_ID = 1;
185 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
186
187 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188 private AudioManager mAudioManager;
Andrew Lee88b51e22014-10-29 15:48:51 -0700189 private VoicemailProviderSettingsUtil mVmProviderSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700190
191 private static final int VM_NOCHANGE_ERROR = 400;
192 private static final int VM_RESPONSE_ERROR = 500;
193 private static final int FW_SET_RESPONSE_ERROR = 501;
194 private static final int FW_GET_RESPONSE_ERROR = 502;
195
196
197 // dialog identifiers for voicemail
198 private static final int VOICEMAIL_DIALOG_CONFIRM = 600;
199 private static final int VOICEMAIL_FWD_SAVING_DIALOG = 601;
200 private static final int VOICEMAIL_FWD_READING_DIALOG = 602;
201 private static final int VOICEMAIL_REVERTING_DIALOG = 603;
202
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700203 // voicemail notification vibration string constants
204 private static final String VOICEMAIL_VIBRATION_ALWAYS = "always";
205 private static final String VOICEMAIL_VIBRATION_NEVER = "never";
206
Andrew Lee5ed870c2014-10-29 11:47:49 -0700207 private SubscriptionInfoHelper mSubscriptionInfoHelper;
208
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700209 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
210
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700211 /** Whether dialpad plays DTMF tone or not. */
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700212 private CheckBoxPreference mButtonAutoRetry;
213 private CheckBoxPreference mButtonHAC;
214 private ListPreference mButtonDTMF;
215 private ListPreference mButtonTTY;
Andrew Leece8ae2a2014-09-10 10:41:48 -0700216 private Preference mPhoneAccountSettingsPreference;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700217 private ListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700218 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700219 private PreferenceScreen mVoicemailSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700220 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700221 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700222
223 private class VoiceMailProvider {
224 public VoiceMailProvider(String name, Intent intent) {
225 this.name = name;
226 this.intent = intent;
227 }
228 public String name;
229 public Intent intent;
230 }
231
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700232 /**
233 * Results of reading forwarding settings
234 */
235 private CallForwardInfo[] mForwardingReadResults = null;
236
237 /**
238 * Result of forwarding number change.
239 * Keys are reasons (eg. unconditional forwarding).
240 */
241 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
242
243 /**
244 * Expected CF read result types.
245 * This set keeps track of the CF types for which we've issued change
246 * commands so we can tell when we've received all of the responses.
247 */
248 private Collection<Integer> mExpectedChangeResultReasons = null;
249
250 /**
251 * Result of vm number change
252 */
253 private AsyncResult mVoicemailChangeResult = null;
254
255 /**
256 * Previous VM provider setting so we can return to it in case of failure.
257 */
258 private String mPreviousVMProviderKey = null;
259
260 /**
261 * Id of the dialog being currently shown.
262 */
263 private int mCurrentDialogId = 0;
264
265 /**
266 * Flag indicating that we are invoking settings for the voicemail provider programmatically
267 * due to vm provider change.
268 */
269 private boolean mVMProviderSettingsForced = false;
270
271 /**
272 * Flag indicating that we are making changes to vm or fwd numbers
273 * due to vm provider change.
274 */
275 private boolean mChangingVMorFwdDueToProviderChange = false;
276
277 /**
278 * True if we are in the process of vm & fwd number change and vm has already been changed.
279 * This is used to decide what to do in case of rollback.
280 */
281 private boolean mVMChangeCompletedSuccessfully = false;
282
283 /**
284 * True if we had full or partial failure setting forwarding numbers and so need to roll them
285 * back.
286 */
287 private boolean mFwdChangesRequireRollback = false;
288
289 /**
290 * Id of error msg to display to user once we are done reverting the VM provider to the previous
291 * one.
292 */
293 private int mVMOrFwdSetError = 0;
294
295 /**
296 * Data about discovered voice mail settings providers.
297 * Is populated by querying which activities can handle ACTION_CONFIGURE_VOICEMAIL.
298 * They key in this map is package name + activity name.
299 * We always add an entry for the default provider with a key of empty
300 * string and intent value of null.
301 * @see #initVoiceMailProviders()
302 */
303 private final Map<String, VoiceMailProvider> mVMProvidersData =
304 new HashMap<String, VoiceMailProvider>();
305
306 /** string to hold old voicemail number as it is being updated. */
307 private String mOldVmNumber;
308
309 // New call forwarding settings and vm number we will be setting
310 // Need to save these since before we get to saving we need to asynchronously
311 // query the existing forwarding settings.
312 private CallForwardInfo[] mNewFwdSettings;
313 private String mNewVMNumber;
314
315 private boolean mForeground;
316
317 @Override
318 public void onPause() {
319 super.onPause();
320 mForeground = false;
321 }
322
323 /**
324 * We have to pull current settings from the network for all kinds of
325 * voicemail providers so we can tell whether we have to update them,
326 * so use this bit to keep track of whether we're reading settings for the
327 * default provider and should therefore save them out when done.
328 */
329 private boolean mReadingSettingsForDefaultProvider = false;
330
Tyler Gunnbaee2952014-09-10 16:01:02 -0700331 /**
332 * Used to indicate that the voicemail preference should be shown.
333 */
334 private boolean mShowVoicemailPreference = false;
335
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700336 /*
337 * Click Listeners, handle click based on objects attached to UI.
338 */
339
340 // Click listener for all toggle events
341 @Override
342 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
343 if (preference == mSubMenuVoicemailSettings) {
344 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700345 } else if (preference == mButtonDTMF) {
346 return true;
347 } else if (preference == mButtonTTY) {
348 return true;
349 } else if (preference == mButtonAutoRetry) {
350 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
351 android.provider.Settings.Global.CALL_AUTO_RETRY,
352 mButtonAutoRetry.isChecked() ? 1 : 0);
353 return true;
354 } else if (preference == mButtonHAC) {
355 int hac = mButtonHAC.isChecked() ? 1 : 0;
356 // Update HAC value in Settings database
357 Settings.System.putInt(mPhone.getContext().getContentResolver(),
358 Settings.System.HEARING_AID, hac);
359
360 // Update HAC Value in AudioManager
361 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
362 return true;
363 } else if (preference == mVoicemailSettings) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700364 final Dialog dialog = mVoicemailSettings.getDialog();
365 if (dialog != null) {
366 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
367 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700368 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
369 if (preference.getIntent() != null) {
370 if (DBG) {
371 log("onPreferenceTreeClick: Invoking cfg intent "
372 + preference.getIntent().getPackage());
373 }
374
375 // onActivityResult() will be responsible for resetting some of variables.
376 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
377 return true;
378 } else {
379 if (DBG) {
380 log("onPreferenceTreeClick:"
381 + " No Intent is available. Use default behavior defined in xml.");
382 }
383
384 // There's no onActivityResult(), so we need to take care of some of variables
385 // which should be reset here.
386 mPreviousVMProviderKey = DEFAULT_VM_PROVIDER_KEY;
387 mVMProviderSettingsForced = false;
388
389 // This should let the preference use default behavior in the xml.
390 return false;
391 }
Andrew Lee97708a42014-09-25 12:39:07 -0700392 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700393 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
394 if (dialog != null) {
395 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
396 }
Andrew Lee97708a42014-09-25 12:39:07 -0700397 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700398 }
399 return false;
400 }
401
402 /**
403 * Implemented to support onPreferenceChangeListener to look for preference
404 * changes.
405 *
406 * @param preference is the preference to be changed
407 * @param objValue should be the value of the selection, NOT its localized
408 * display value.
409 */
410 @Override
411 public boolean onPreferenceChange(Preference preference, Object objValue) {
412 if (DBG) {
Andrew Leedf14ead2014-10-17 14:22:52 -0700413 log("onPreferenceChange(). preference: \"" + preference + "\""
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700414 + ", value: \"" + objValue + "\"");
415 }
Andrew Lee2170a972014-08-13 18:13:01 -0700416
417 if (preference == mButtonDTMF) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700418 int index = mButtonDTMF.findIndexOfValue((String) objValue);
419 Settings.System.putInt(mPhone.getContext().getContentResolver(),
420 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
421 } else if (preference == mButtonTTY) {
422 handleTTYChange(preference, objValue);
423 } else if (preference == mVoicemailProviders) {
424 final String newProviderKey = (String) objValue;
425 if (DBG) {
426 log("Voicemail Provider changes from \"" + mPreviousVMProviderKey
427 + "\" to \"" + newProviderKey + "\".");
428 }
429 // If previous provider key and the new one is same, we don't need to handle it.
430 if (mPreviousVMProviderKey.equals(newProviderKey)) {
431 if (DBG) log("No change is made toward VM provider setting.");
432 return true;
433 }
434 updateVMPreferenceWidgets(newProviderKey);
435
Andrew Leeb490d732014-10-27 15:00:41 -0700436 final VoicemailProviderSettings newProviderSettings =
Andrew Lee88b51e22014-10-29 15:48:51 -0700437 mVmProviderSettingsUtil.load(newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700438
Andrew Lee88b51e22014-10-29 15:48:51 -0700439 // If the user switches to a voice mail provider and we have numbers stored for it we
440 // will automatically change the phone's voice mail and forwarding number to the stored
441 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700442 if (newProviderSettings == null) {
443 // Force the user into a configuration of the chosen provider
444 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
445 mVMProviderSettingsForced = true;
446 simulatePreferenceClick(mVoicemailSettings);
447 } else {
448 if (DBG) log("Saved preferences found - switching to them");
449 // Set this flag so if we get a failure we revert to previous provider
450 mChangingVMorFwdDueToProviderChange = true;
451 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
452 }
Andrew Leedf14ead2014-10-17 14:22:52 -0700453 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700454 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
455 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
456 } else {
457 AlertDialog.Builder builder = new AlertDialog.Builder(this);
458 DialogInterface.OnClickListener networkSettingsClickListener =
459 new Dialog.OnClickListener() {
460 @Override
461 public void onClick(DialogInterface dialog, int which) {
462 startActivity(new Intent(mPhone.getContext(),
463 com.android.phone.MobileNetworkSettings.class));
464 }
465 };
466 builder.setMessage(getResources().getString(
467 R.string.enable_video_calling_dialog_msg))
468 .setNeutralButton(getResources().getString(
469 R.string.enable_video_calling_dialog_settings),
470 networkSettingsClickListener)
471 .setPositiveButton(android.R.string.ok, null)
472 .show();
473 return false;
474 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700475 }
476 // always let the preference setting proceed.
477 return true;
478 }
479
480 @Override
481 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
482 if (DBG) log("onPreferenceClick: request preference click on dialog close: " +
483 buttonClicked);
484 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
485 return;
486 }
487
488 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700489 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
490 mSubMenuVoicemailSettings.getPhoneNumber(),
491 VoicemailProviderSettings.NO_FORWARDING);
492 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700493 }
494 }
495
496 /**
497 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
498 * This method set the default values for the various
499 * EditPhoneNumberPreference dialogs.
500 */
501 @Override
502 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
503 if (preference == mSubMenuVoicemailSettings) {
504 // update the voicemail number field, which takes care of the
505 // mSubMenuVoicemailSettings itself, so we should return null.
506 if (DBG) log("updating default for voicemail dialog");
507 updateVoiceNumberField();
508 return null;
509 }
510
511 String vmDisplay = mPhone.getVoiceMailNumber();
512 if (TextUtils.isEmpty(vmDisplay)) {
513 // if there is no voicemail number, we just return null to
514 // indicate no contribution.
515 return null;
516 }
517
518 // Return the voicemail number prepended with "VM: "
519 if (DBG) log("updating default for call forwarding dialogs");
520 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
521 }
522
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700523 private void switchToPreviousVoicemailProvider() {
524 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
525 if (mPreviousVMProviderKey != null) {
526 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
527 // we have to revert with carrier
528 if (DBG) {
529 log("Needs to rollback."
530 + " mVMChangeCompletedSuccessfully=" + mVMChangeCompletedSuccessfully
531 + ", mFwdChangesRequireRollback=" + mFwdChangesRequireRollback);
532 }
533
534 showDialogIfForeground(VOICEMAIL_REVERTING_DIALOG);
Andrew Leeb490d732014-10-27 15:00:41 -0700535 final VoicemailProviderSettings prevSettings =
Andrew Lee88b51e22014-10-29 15:48:51 -0700536 mVmProviderSettingsUtil.load(mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700537 if (prevSettings == null) {
538 // prevSettings never becomes null since it should be already loaded!
Andrew Leeb490d732014-10-27 15:00:41 -0700539 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700540 + mPreviousVMProviderKey + "\" becomes null, which is unexpected.");
541 if (DBG) {
542 Log.e(LOG_TAG,
543 "mVMChangeCompletedSuccessfully: " + mVMChangeCompletedSuccessfully
544 + ", mFwdChangesRequireRollback: " + mFwdChangesRequireRollback);
545 }
546 }
547 if (mVMChangeCompletedSuccessfully) {
Andrew Leeb490d732014-10-27 15:00:41 -0700548 mNewVMNumber = prevSettings.getVoicemailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700549 Log.i(LOG_TAG, "VM change is already completed successfully."
550 + "Have to revert VM back to " + mNewVMNumber + " again.");
551 mPhone.setVoiceMailNumber(
552 mPhone.getVoiceMailAlphaTag().toString(),
553 mNewVMNumber,
554 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
555 }
556 if (mFwdChangesRequireRollback) {
557 Log.i(LOG_TAG, "Requested to rollback Fwd changes.");
Andrew Leeb490d732014-10-27 15:00:41 -0700558 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700559 if (prevFwdSettings != null) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800560 Map<Integer, AsyncResult> results = mForwardingChangeResults;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700561 resetForwardingChangeState();
562 for (int i = 0; i < prevFwdSettings.length; i++) {
563 CallForwardInfo fi = prevFwdSettings[i];
564 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
Andrew Lee1af6cf72014-11-04 17:35:26 -0800565 // Only revert the settings for which the update succeeded.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700566 AsyncResult result = results.get(fi.reason);
567 if (result != null && result.exception == null) {
568 mExpectedChangeResultReasons.add(fi.reason);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800569 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700570 mRevertOptionComplete.obtainMessage(
571 EVENT_FORWARDING_CHANGED, i, 0));
572 }
573 }
574 }
575 }
576 } else {
577 if (DBG) log("No need to revert");
578 onRevertDone();
579 }
580 }
581 }
582
583 private void onRevertDone() {
584 if (DBG) log("Flipping provider key back to " + mPreviousVMProviderKey);
585 mVoicemailProviders.setValue(mPreviousVMProviderKey);
586 updateVMPreferenceWidgets(mPreviousVMProviderKey);
587 updateVoiceNumberField();
588 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800589 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700590 mVMOrFwdSetError = 0;
591 }
592 }
593
594 @Override
595 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
596 if (DBG) {
597 log("onActivityResult: requestCode: " + requestCode
598 + ", resultCode: " + resultCode
599 + ", data: " + data);
600 }
601 // there are cases where the contact picker may end up sending us more than one
602 // request. We want to ignore the request if we're not in the correct state.
603 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
604 boolean failure = false;
605
606 // No matter how the processing of result goes lets clear the flag
607 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
608 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
609 mVMProviderSettingsForced = false;
610
611 String vmNum = null;
612 if (resultCode != RESULT_OK) {
613 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
614 failure = true;
615 } else {
616 if (data == null) {
617 if (DBG) log("onActivityResult: vm provider cfg result has no data");
618 failure = true;
619 } else {
620 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
621 if (DBG) log("Provider requested signout");
622 if (isVMProviderSettingsForced) {
623 if (DBG) log("Going back to previous provider on signout");
624 switchToPreviousVoicemailProvider();
625 } else {
626 final String victim = getCurrentVoicemailProviderKey();
627 if (DBG) log("Relaunching activity and ignoring " + victim);
628 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
629 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
630 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
631 this.startActivity(i);
632 }
633 return;
634 }
635 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
636 if (vmNum == null || vmNum.length() == 0) {
637 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
638 failure = true;
639 }
640 }
641 }
642 if (failure) {
643 if (DBG) log("Failure in return from voicemail provider");
644 if (isVMProviderSettingsForced) {
645 switchToPreviousVoicemailProvider();
646 } else {
647 if (DBG) log("Not switching back the provider since this is not forced config");
648 }
649 return;
650 }
651 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
652 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
653
Santos Cordonda120f42014-08-06 04:44:34 -0700654 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700655 // send it to the provider when it's config is invoked so it can use this as default
656 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
657
658 if (DBG) log("onActivityResult: vm provider cfg result " +
659 (fwdNum != null ? "has" : " does not have") + " forwarding number");
660 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700661 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700662 return;
663 }
664
665 if (requestCode == VOICEMAIL_PREF_ID) {
666 if (resultCode != RESULT_OK) {
667 if (DBG) log("onActivityResult: contact picker result not OK.");
668 return;
669 }
670
671 Cursor cursor = null;
672 try {
673 cursor = getContentResolver().query(data.getData(),
674 NUM_PROJECTION, null, null, null);
675 if ((cursor == null) || (!cursor.moveToFirst())) {
676 if (DBG) log("onActivityResult: bad contact data, no results found.");
677 return;
678 }
679 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
680 return;
681 } finally {
682 if (cursor != null) {
683 cursor.close();
684 }
685 }
686 }
687
688 super.onActivityResult(requestCode, resultCode, data);
689 }
690
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700691 /**
692 * Wrapper around showDialog() that will silently do nothing if we're
693 * not in the foreground.
694 *
695 * This is useful here because most of the dialogs we display from
696 * this class are triggered by asynchronous events (like
697 * success/failure messages from the telephony layer) and it's
698 * possible for those events to come in even after the user has gone
699 * to a different screen.
700 */
701 // TODO: this is too brittle: it's still easy to accidentally add new
702 // code here that calls showDialog() directly (which will result in a
703 // WindowManager$BadTokenException if called after the activity has
704 // been stopped.)
705 //
706 // It would be cleaner to do the "if (mForeground)" check in one
707 // central place, maybe by using a single Handler for all asynchronous
708 // events (and have *that* discard events if we're not in the
709 // foreground.)
710 //
711 // Unfortunately it's not that simple, since we sometimes need to do
712 // actual work to handle these events whether or not we're in the
713 // foreground (see the Handler code in mSetOptionComplete for
714 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800715 //
716 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
717 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700718 private void showDialogIfForeground(int id) {
719 if (mForeground) {
720 showDialog(id);
721 }
722 }
723
724 private void dismissDialogSafely(int id) {
725 try {
726 dismissDialog(id);
727 } catch (IllegalArgumentException e) {
728 // This is expected in the case where we were in the background
729 // at the time we would normally have shown the dialog, so we didn't
730 // show it.
731 }
732 }
733
Andrew Leeb490d732014-10-27 15:00:41 -0700734 private void saveVoiceMailAndForwardingNumber(
735 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700736 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700737 mNewVMNumber = newSettings.getVoicemailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700738 // empty vm number == clearing the vm number ?
739 if (mNewVMNumber == null) {
740 mNewVMNumber = "";
741 }
742
Andrew Leeb490d732014-10-27 15:00:41 -0700743 mNewFwdSettings = newSettings.getForwardingSettings();
744 if (DBG) log("newFwdNumber "
745 + String.valueOf((mNewFwdSettings != null ? mNewFwdSettings.length : 0))
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700746 + " settings");
747
748 // No fwd settings on CDMA
749 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
750 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700751 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700752 }
753
Andrew Leee3c15212014-10-28 13:12:55 -0700754 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700755 if (mNewVMNumber.equals(mOldVmNumber)
756 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Leeab082272014-11-04 15:50:42 -0800757 showDialogIfForeground(VM_NOCHANGE_ERROR);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700758 return;
759 }
760
Andrew Lee88b51e22014-10-29 15:48:51 -0700761 mVmProviderSettingsUtil.save(key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700762 mVMChangeCompletedSuccessfully = false;
763 mFwdChangesRequireRollback = false;
764 mVMOrFwdSetError = 0;
765 if (!key.equals(mPreviousVMProviderKey)) {
766 mReadingSettingsForDefaultProvider =
767 mPreviousVMProviderKey.equals(DEFAULT_VM_PROVIDER_KEY);
768 if (DBG) log("Reading current forwarding settings");
Andrew Leeb490d732014-10-27 15:00:41 -0700769 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
770 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
771 for (int i = 0; i < mForwardingReadResults.length; i++) {
772 mPhone.getCallForwardingOption(
773 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700774 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
775 }
776 showDialogIfForeground(VOICEMAIL_FWD_READING_DIALOG);
777 } else {
778 saveVoiceMailAndForwardingNumberStage2();
779 }
780 }
781
782 private final Handler mGetOptionComplete = new Handler() {
783 @Override
784 public void handleMessage(Message msg) {
785 AsyncResult result = (AsyncResult) msg.obj;
786 switch (msg.what) {
787 case EVENT_FORWARDING_GET_COMPLETED:
788 handleForwardingSettingsReadResult(result, msg.arg1);
789 break;
790 }
791 }
792 };
793
794 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
795 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
796 Throwable error = null;
797 if (ar.exception != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700798 error = ar.exception;
Andrew Lee1af6cf72014-11-04 17:35:26 -0800799 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700800 }
801 if (ar.userObj instanceof Throwable) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800802 error = (Throwable) ar.userObj;
803 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700804 }
805
806 // We may have already gotten an error and decided to ignore the other results.
807 if (mForwardingReadResults == null) {
808 if (DBG) Log.d(LOG_TAG, "ignoring fwd reading result: " + idx);
809 return;
810 }
811
812 // In case of error ignore other results, show an error dialog
813 if (error != null) {
814 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
815 mForwardingReadResults = null;
816 dismissDialogSafely(VOICEMAIL_FWD_READING_DIALOG);
Andrew Leeab082272014-11-04 15:50:42 -0800817 showDialogIfForeground(FW_GET_RESPONSE_ERROR);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700818 return;
819 }
820
Andrew Lee1af6cf72014-11-04 17:35:26 -0800821 // Get the forwarding info.
822 mForwardingReadResults[idx] = CallForwardInfoUtil.getCallForwardInfo(
823 (CallForwardInfo[]) ar.result,
824 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx]);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700825
826 // Check if we got all the results already
827 boolean done = true;
828 for (int i = 0; i < mForwardingReadResults.length; i++) {
829 if (mForwardingReadResults[i] == null) {
830 done = false;
831 break;
832 }
833 }
Andrew Lee1af6cf72014-11-04 17:35:26 -0800834
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700835 if (done) {
836 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
837 dismissDialogSafely(VOICEMAIL_FWD_READING_DIALOG);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800838
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700839 if (mReadingSettingsForDefaultProvider) {
Andrew Lee88b51e22014-10-29 15:48:51 -0700840 mVmProviderSettingsUtil.save(DEFAULT_VM_PROVIDER_KEY,
841 new VoicemailProviderSettings(this.mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700842 mReadingSettingsForDefaultProvider = false;
843 }
844 saveVoiceMailAndForwardingNumberStage2();
845 } else {
846 if (DBG) Log.d(LOG_TAG, "Not done receiving fwd info");
847 }
848 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700849 private void resetForwardingChangeState() {
850 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
851 mExpectedChangeResultReasons = new HashSet<Integer>();
852 }
853
854 // Called after we are done saving the previous forwarding settings if
855 // we needed.
856 private void saveVoiceMailAndForwardingNumberStage2() {
857 mForwardingChangeResults = null;
858 mVoicemailChangeResult = null;
Andrew Leeb490d732014-10-27 15:00:41 -0700859 if (mNewFwdSettings != VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700860 resetForwardingChangeState();
861 for (int i = 0; i < mNewFwdSettings.length; i++) {
862 CallForwardInfo fi = mNewFwdSettings[i];
Andrew Lee1af6cf72014-11-04 17:35:26 -0800863 CallForwardInfo fiForReason =
864 CallForwardInfoUtil.infoForReason(mForwardingReadResults, fi.reason);
865 final boolean doUpdate = CallForwardInfoUtil.isUpdateRequired(fiForReason, fi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700866
867 if (doUpdate) {
868 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
869 mExpectedChangeResultReasons.add(i);
870
Andrew Lee1af6cf72014-11-04 17:35:26 -0800871 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700872 mSetOptionComplete.obtainMessage(
873 EVENT_FORWARDING_CHANGED, fi.reason, 0));
874 }
875 }
876 showDialogIfForeground(VOICEMAIL_FWD_SAVING_DIALOG);
877 } else {
878 if (DBG) log("Not touching fwd #");
879 setVMNumberWithCarrier();
880 }
881 }
882
883 private void setVMNumberWithCarrier() {
884 if (DBG) log("save voicemail #: " + mNewVMNumber);
885 mPhone.setVoiceMailNumber(
886 mPhone.getVoiceMailAlphaTag().toString(),
887 mNewVMNumber,
888 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
889 }
890
891 /**
892 * Callback to handle option update completions
893 */
894 private final Handler mSetOptionComplete = new Handler() {
895 @Override
896 public void handleMessage(Message msg) {
897 AsyncResult result = (AsyncResult) msg.obj;
898 boolean done = false;
899 switch (msg.what) {
900 case EVENT_VOICEMAIL_CHANGED:
901 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700902 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700903 done = true;
904 break;
905 case EVENT_FORWARDING_CHANGED:
906 mForwardingChangeResults.put(msg.arg1, result);
907 if (result.exception != null) {
908 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
909 result.exception.getMessage());
910 } else {
911 if (DBG) log("Success in setting fwd# " + msg.arg1);
912 }
Andrew Leee438b312014-10-29 16:59:15 -0700913 if (isForwardingCompleted()) {
914 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700915 if (DBG) log("Overall fwd changes completed ok, starting vm change");
916 setVMNumberWithCarrier();
917 } else {
918 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
919 "Check if we need to try rollback for some settings.");
920 mFwdChangesRequireRollback = false;
921 Iterator<Map.Entry<Integer,AsyncResult>> it =
922 mForwardingChangeResults.entrySet().iterator();
923 while (it.hasNext()) {
924 Map.Entry<Integer,AsyncResult> entry = it.next();
925 if (entry.getValue().exception == null) {
926 // If at least one succeeded we have to revert
927 Log.i(LOG_TAG, "Rollback will be required");
928 mFwdChangesRequireRollback = true;
929 break;
930 }
931 }
932 if (!mFwdChangesRequireRollback) {
933 Log.i(LOG_TAG, "No rollback needed.");
934 }
935 done = true;
936 }
937 }
938 break;
939 default:
940 // TODO: should never reach this, may want to throw exception
941 }
942 if (done) {
943 if (DBG) log("All VM provider related changes done");
944 if (mForwardingChangeResults != null) {
945 dismissDialogSafely(VOICEMAIL_FWD_SAVING_DIALOG);
946 }
Andrew Leee438b312014-10-29 16:59:15 -0700947 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700948 }
949 }
950 };
951
952 /**
953 * Callback to handle option revert completions
954 */
955 private final Handler mRevertOptionComplete = new Handler() {
956 @Override
957 public void handleMessage(Message msg) {
958 AsyncResult result = (AsyncResult) msg.obj;
959 switch (msg.what) {
960 case EVENT_VOICEMAIL_CHANGED:
961 mVoicemailChangeResult = result;
962 if (DBG) log("VM revert complete msg");
963 break;
964 case EVENT_FORWARDING_CHANGED:
965 mForwardingChangeResults.put(msg.arg1, result);
966 if (result.exception != null) {
967 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
968 result.exception.getMessage());
969 } else {
970 if (DBG) log("Success in reverting fwd# " + msg.arg1);
971 }
972 if (DBG) log("FWD revert complete msg ");
973 break;
974 default:
975 // TODO: should never reach this, may want to throw exception
976 }
977 final boolean done =
978 (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null) &&
Andrew Leee438b312014-10-29 16:59:15 -0700979 (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700980 if (done) {
981 if (DBG) log("All VM reverts done");
982 dismissDialogSafely(VOICEMAIL_REVERTING_DIALOG);
983 onRevertDone();
984 }
985 }
986 };
987
988 /**
Andrew Leee438b312014-10-29 16:59:15 -0700989 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700990 */
Andrew Leee438b312014-10-29 16:59:15 -0700991 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700992 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -0700993 return true;
994 }
995
996 for (Integer reason : mExpectedChangeResultReasons) {
997 if (mForwardingChangeResults.get(reason) == null) {
998 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700999 }
1000 }
Andrew Leee438b312014-10-29 16:59:15 -07001001
1002 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001003 }
Andrew Leee438b312014-10-29 16:59:15 -07001004
1005 private boolean isFwdChangeSuccess() {
1006 if (mForwardingChangeResults == null) {
1007 return true;
1008 }
1009
1010 for (AsyncResult result : mForwardingChangeResults.values()) {
1011 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001012 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -07001013 String msg = exception.getMessage();
1014 msg = (msg != null) ? msg : "";
1015 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
1016 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001017 }
1018 }
Andrew Leee438b312014-10-29 16:59:15 -07001019 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001020 }
1021
Andrew Leee438b312014-10-29 16:59:15 -07001022 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001023 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -07001024 String msg = mVoicemailChangeResult.exception.getMessage();
1025 msg = (msg != null) ? msg : "";
1026 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
1027 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001028 }
Andrew Leee438b312014-10-29 16:59:15 -07001029
1030 if (DBG) log("VM change completed successfully.");
1031 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001032 }
1033
Andrew Leee438b312014-10-29 16:59:15 -07001034 private void handleSetVmOrFwdMessage() {
1035 if (DBG) log("handleSetVMMessage: set VM request complete");
1036
1037 if (!isFwdChangeSuccess()) {
Andrew Leeab082272014-11-04 15:50:42 -08001038 handleVmOrFwdSetError(FW_SET_RESPONSE_ERROR);
Andrew Leee438b312014-10-29 16:59:15 -07001039 } else if (!isVmChangeSuccess()) {
Andrew Leeab082272014-11-04 15:50:42 -08001040 handleVmOrFwdSetError(VM_RESPONSE_ERROR);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001041 } else {
Andrew Leee438b312014-10-29 16:59:15 -07001042 if (DBG) log("change VM success!");
Andrew Leeab082272014-11-04 15:50:42 -08001043 handleVmAndFwdSetSuccess(VOICEMAIL_DIALOG_CONFIRM);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001044 }
1045 }
1046
1047 /**
1048 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
1049 * changes to those settings and show "failure" dialog.
1050 *
Andrew Leeab082272014-11-04 15:50:42 -08001051 * @param dialogId ID of the dialog to show for the specific error case. Either
1052 * {@link #FW_SET_RESPONSE_ERROR} or {@link #VM_RESPONSE_ERROR}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001053 */
Andrew Leeab082272014-11-04 15:50:42 -08001054 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001055 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -08001056 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001057 mChangingVMorFwdDueToProviderChange = false;
1058 switchToPreviousVoicemailProvider();
1059 return;
1060 }
1061 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001062 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001063 updateVoiceNumberField();
1064 }
1065
1066 /**
1067 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1068 * This updates a bunch of variables and show "success" dialog.
1069 */
Andrew Leeab082272014-11-04 15:50:42 -08001070 private void handleVmAndFwdSetSuccess(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001071 if (DBG) {
Andrew Leee438b312014-10-29 16:59:15 -07001072 log("handleVmAndFwdSetSuccess(). current voicemail provider key: "
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001073 + getCurrentVoicemailProviderKey());
1074 }
1075 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1076 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001077 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001078 updateVoiceNumberField();
1079 }
1080
1081 /**
1082 * Update the voicemail number from what we've recorded on the sim.
1083 */
1084 private void updateVoiceNumberField() {
1085 if (DBG) {
1086 log("updateVoiceNumberField(). mSubMenuVoicemailSettings=" + mSubMenuVoicemailSettings);
1087 }
1088 if (mSubMenuVoicemailSettings == null) {
1089 return;
1090 }
1091
1092 mOldVmNumber = mPhone.getVoiceMailNumber();
1093 if (mOldVmNumber == null) {
1094 mOldVmNumber = "";
1095 }
1096 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1097 final String summary = (mOldVmNumber.length() > 0) ? mOldVmNumber :
1098 getString(R.string.voicemail_number_not_set);
1099 mSubMenuVoicemailSettings.setSummary(summary);
1100 }
1101
1102 /*
1103 * Helper Methods for Activity class.
1104 * The initial query commands are split into two pieces now
1105 * for individual expansion. This combined with the ability
1106 * to cancel queries allows for a much better user experience,
1107 * and also ensures that the user only waits to update the
1108 * data that is relevant.
1109 */
1110
1111 @Override
1112 protected void onPrepareDialog(int id, Dialog dialog) {
1113 super.onPrepareDialog(id, dialog);
1114 mCurrentDialogId = id;
1115 }
1116
1117 // dialog creation method, called by showDialog()
1118 @Override
1119 protected Dialog onCreateDialog(int id) {
1120 if ((id == VM_RESPONSE_ERROR) || (id == VM_NOCHANGE_ERROR) ||
1121 (id == FW_SET_RESPONSE_ERROR) || (id == FW_GET_RESPONSE_ERROR) ||
1122 (id == VOICEMAIL_DIALOG_CONFIRM)) {
1123
1124 AlertDialog.Builder b = new AlertDialog.Builder(this);
1125
1126 int msgId;
1127 int titleId = R.string.error_updating_title;
1128 switch (id) {
1129 case VOICEMAIL_DIALOG_CONFIRM:
1130 msgId = R.string.vm_changed;
1131 titleId = R.string.voicemail;
1132 // Set Button 2
1133 b.setNegativeButton(R.string.close_dialog, this);
1134 break;
1135 case VM_NOCHANGE_ERROR:
1136 // even though this is technically an error,
1137 // keep the title friendly.
1138 msgId = R.string.no_change;
1139 titleId = R.string.voicemail;
1140 // Set Button 2
1141 b.setNegativeButton(R.string.close_dialog, this);
1142 break;
1143 case VM_RESPONSE_ERROR:
1144 msgId = R.string.vm_change_failed;
1145 // Set Button 1
1146 b.setPositiveButton(R.string.close_dialog, this);
1147 break;
1148 case FW_SET_RESPONSE_ERROR:
1149 msgId = R.string.fw_change_failed;
1150 // Set Button 1
1151 b.setPositiveButton(R.string.close_dialog, this);
1152 break;
1153 case FW_GET_RESPONSE_ERROR:
1154 msgId = R.string.fw_get_in_vm_failed;
1155 b.setPositiveButton(R.string.alert_dialog_yes, this);
1156 b.setNegativeButton(R.string.alert_dialog_no, this);
1157 break;
1158 default:
1159 msgId = R.string.exception_error;
1160 // Set Button 3, tells the activity that the error is
1161 // not recoverable on dialog exit.
1162 b.setNeutralButton(R.string.close_dialog, this);
1163 break;
1164 }
1165
1166 b.setTitle(getText(titleId));
1167 String message = getText(msgId).toString();
1168 b.setMessage(message);
1169 b.setCancelable(false);
1170 AlertDialog dialog = b.create();
1171
1172 // make the dialog more obvious by bluring the background.
1173 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
1174
1175 return dialog;
1176 } else if (id == VOICEMAIL_FWD_SAVING_DIALOG || id == VOICEMAIL_FWD_READING_DIALOG ||
1177 id == VOICEMAIL_REVERTING_DIALOG) {
1178 ProgressDialog dialog = new ProgressDialog(this);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001179 dialog.setTitle(getText(R.string.call_settings));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001180 dialog.setIndeterminate(true);
1181 dialog.setCancelable(false);
1182 dialog.setMessage(getText(
1183 id == VOICEMAIL_FWD_SAVING_DIALOG ? R.string.updating_settings :
1184 (id == VOICEMAIL_REVERTING_DIALOG ? R.string.reverting_settings :
1185 R.string.reading_settings)));
1186 return dialog;
1187 }
1188
1189
1190 return null;
1191 }
1192
1193 // This is a method implemented for DialogInterface.OnClickListener.
1194 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1195 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1196 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1197 public void onClick(DialogInterface dialog, int which) {
1198 dialog.dismiss();
1199 switch (which){
1200 case DialogInterface.BUTTON_NEUTRAL:
1201 if (DBG) log("Neutral button");
1202 break;
1203 case DialogInterface.BUTTON_NEGATIVE:
1204 if (DBG) log("Negative button");
1205 if (mCurrentDialogId == FW_GET_RESPONSE_ERROR) {
1206 // We failed to get current forwarding settings and the user
1207 // does not wish to continue.
1208 switchToPreviousVoicemailProvider();
1209 }
1210 break;
1211 case DialogInterface.BUTTON_POSITIVE:
1212 if (DBG) log("Positive button");
1213 if (mCurrentDialogId == FW_GET_RESPONSE_ERROR) {
1214 // We failed to get current forwarding settings but the user
1215 // wishes to continue changing settings to the new vm provider
1216 saveVoiceMailAndForwardingNumberStage2();
1217 } else {
1218 finish();
1219 }
1220 return;
1221 default:
1222 // just let the dialog close and go back to the input
1223 }
1224 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1225 // with settings UI. If we were called to explicitly configure voice mail then
1226 // we finish the settings activity here to come back to whatever the user was doing.
1227 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1228 finish();
1229 }
1230 }
1231
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001232 /*
1233 * Activity class methods
1234 */
1235
1236 @Override
1237 protected void onCreate(Bundle icicle) {
1238 super.onCreate(icicle);
1239 if (DBG) log("onCreate(). Intent: " + getIntent());
1240 mPhone = PhoneGlobals.getPhone();
Tyler Gunnbaee2952014-09-10 16:01:02 -07001241 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andrew Lee88b51e22014-10-29 15:48:51 -07001242 mVmProviderSettingsUtil = new VoicemailProviderSettingsUtil(getApplicationContext());
Tyler Gunnbaee2952014-09-10 16:01:02 -07001243
Tyler Gunnbaee2952014-09-10 16:01:02 -07001244 // Show the voicemail preference in onResume if the calling intent specifies the
1245 // ACTION_ADD_VOICEMAIL action.
1246 mShowVoicemailPreference = (icicle == null) &&
1247 getIntent().getAction().equals(ACTION_ADD_VOICEMAIL);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001248
1249 mSubscriptionInfoHelper = new SubscriptionInfoHelper(getIntent());
1250 mSubscriptionInfoHelper.setActionBarTitle(
1251 getActionBar(), getResources(), R.string.call_settings_with_label);
1252 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001253
1254 private void initPhoneAccountPreferences() {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001255 mPhoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001256
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001257 TelecomManager telecomManager = TelecomManager.from(this);
Andrew Lee93c345f2014-10-27 15:25:07 -07001258 TelephonyManager telephonyManager =
1259 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001260
Andrew Lee93c345f2014-10-27 15:25:07 -07001261 if ((telecomManager.getSimCallManagers().isEmpty() && !SipUtil.isVoipSupported(this))
1262 || telephonyManager.getPhoneCount() > 1) {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001263 getPreferenceScreen().removePreference(mPhoneAccountSettingsPreference);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001264 }
1265 }
1266
1267 private boolean canLaunchIntent(Intent intent) {
1268 PackageManager pm = getPackageManager();
1269 return pm.resolveActivity(intent, PackageManager.GET_ACTIVITIES) != null;
1270 }
1271
Tyler Gunnbaee2952014-09-10 16:01:02 -07001272 @Override
1273 protected void onResume() {
1274 super.onResume();
1275 mForeground = true;
1276
1277 PreferenceScreen preferenceScreen = getPreferenceScreen();
1278 if (preferenceScreen != null) {
1279 preferenceScreen.removeAll();
1280 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001281
1282 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001283
Andrew Leedb2fe562014-09-03 15:40:43 -07001284 initPhoneAccountPreferences();
1285
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001286 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001287 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1288 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1289 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1290 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001291
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001292 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1293 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1294 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
1295 mButtonTTY = (ListPreference) findPreference(BUTTON_TTY_KEY);
1296 mVoicemailProviders = (ListPreference) findPreference(BUTTON_VOICEMAIL_PROVIDER_KEY);
Andrew Lee312e8172014-10-23 17:01:36 -07001297 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001298
Andrew Lee2c027892014-10-29 11:29:54 -07001299 mVoicemailProviders.setOnPreferenceChangeListener(this);
1300 mVoicemailSettingsScreen =
1301 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
1302 mVoicemailSettings = (PreferenceScreen)findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
1303 mVoicemailNotificationVibrate =
1304 (CheckBoxPreference) findPreference(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY);
1305 initVoiceMailProviders();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001306
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001307
Andrew Lee64a7d792014-10-15 17:38:38 -07001308 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1309 mButtonDTMF.setOnPreferenceChangeListener(this);
1310 int dtmf = Settings.System.getInt(getContentResolver(),
1311 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1312 mButtonDTMF.setValueIndex(dtmf);
1313 } else {
1314 prefSet.removePreference(mButtonDTMF);
1315 mButtonDTMF = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001316 }
1317
Andrew Lee64a7d792014-10-15 17:38:38 -07001318 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1319 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1320 int autoretry = Settings.Global.getInt(
1321 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1322 mButtonAutoRetry.setChecked(autoretry != 0);
1323 } else {
1324 prefSet.removePreference(mButtonAutoRetry);
1325 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001326 }
1327
Andrew Lee64a7d792014-10-15 17:38:38 -07001328 if (getResources().getBoolean(R.bool.hac_enabled)) {
1329 mButtonHAC.setOnPreferenceChangeListener(this);
1330 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1331 mButtonHAC.setChecked(hac != 0);
1332 } else {
1333 prefSet.removePreference(mButtonHAC);
1334 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001335 }
1336
Andrew Lee64a7d792014-10-15 17:38:38 -07001337 TelecomManager telecomManager = TelecomManager.from(this);
1338 if (telecomManager != null && telecomManager.isTtySupported()) {
1339 mButtonTTY.setOnPreferenceChangeListener(this);
1340 int settingsTtyMode = Settings.Secure.getInt(getContentResolver(),
1341 Settings.Secure.PREFERRED_TTY_MODE,
1342 TelecomManager.TTY_MODE_OFF);
1343 mButtonTTY.setValue(Integer.toString(settingsTtyMode));
1344 updatePreferredTtyModeSummary(settingsTtyMode);
1345 } else {
1346 prefSet.removePreference(mButtonTTY);
1347 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001348 }
1349
1350 if (!getResources().getBoolean(R.bool.world_phone)) {
1351 Preference options = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
Andrew Lee2170a972014-08-13 18:13:01 -07001352 if (options != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001353 prefSet.removePreference(options);
Andrew Lee2170a972014-08-13 18:13:01 -07001354 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001355 options = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
Andrew Lee2170a972014-08-13 18:13:01 -07001356 if (options != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001357 prefSet.removePreference(options);
Andrew Lee2170a972014-08-13 18:13:01 -07001358 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001359
1360 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001361 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001362 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001363 prefSet.removePreference(fdnButton);
1364
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001365 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1366 addPreferencesFromResource(R.xml.cdma_call_privacy);
1367 }
1368 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001369 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(this, FdnSetting.class));
1370
Andrew Lee2170a972014-08-13 18:13:01 -07001371 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
Etan Cohen0ca1c802014-07-07 15:35:48 -07001372 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1373 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001374 } else {
1375 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1376 }
1377 }
1378
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001379 // check the intent that started this activity and pop up the voicemail
1380 // dialog if we've been asked to.
1381 // If we have at least one non default VM provider registered then bring up
1382 // the selection for the VM provider, otherwise bring up a VM number dialog.
1383 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001384 if (mShowVoicemailPreference) {
Tyler Gunnbaee2952014-09-10 16:01:02 -07001385 if (DBG) {
1386 log("ACTION_ADD_VOICEMAIL Intent is thrown. current VM data size: "
1387 + mVMProvidersData.size());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001388 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001389 if (mVMProvidersData.size() > 1) {
1390 simulatePreferenceClick(mVoicemailProviders);
1391 } else {
1392 onPreferenceChange(mVoicemailProviders, DEFAULT_VM_PROVIDER_KEY);
1393 mVoicemailProviders.setValue(DEFAULT_VM_PROVIDER_KEY);
1394 }
1395 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001396 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001397
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001398 updateVoiceNumberField();
1399 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001400
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001401 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
1402 mPhone.getContext());
1403 if (migrateVoicemailVibrationSettingsIfNeeded(prefs)) {
1404 mVoicemailNotificationVibrate.setChecked(prefs.getBoolean(
1405 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, false));
1406 }
1407
Andrew Lee312e8172014-10-23 17:01:36 -07001408 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1409 boolean currentValue =
1410 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1411 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1412 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001413 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1414 } else {
1415 prefSet.removePreference(mEnableVideoCalling);
1416 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001417 }
1418
1419 // Migrate settings from BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY to
1420 // BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, if the latter does not exist.
1421 // Returns true if migration was performed.
1422 public static boolean migrateVoicemailVibrationSettingsIfNeeded(SharedPreferences prefs) {
1423 if (!prefs.contains(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY)) {
1424 String vibrateWhen = prefs.getString(
1425 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY, VOICEMAIL_VIBRATION_NEVER);
1426 // If vibrateWhen is always, then voicemailVibrate should be True.
1427 // otherwise if vibrateWhen is "only in silent mode", or "never", then
1428 // voicemailVibrate = False.
1429 boolean voicemailVibrate = vibrateWhen.equals(VOICEMAIL_VIBRATION_ALWAYS);
1430 final SharedPreferences.Editor editor = prefs.edit();
1431 editor.putBoolean(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, voicemailVibrate);
1432 editor.commit();
1433 return true;
1434 }
1435 return false;
1436 }
1437
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001438 private void handleTTYChange(Preference preference, Object objValue) {
1439 int buttonTtyMode;
1440 buttonTtyMode = Integer.valueOf((String) objValue).intValue();
1441 int settingsTtyMode = android.provider.Settings.Secure.getInt(
1442 getContentResolver(),
Sailesh Nepalbf900542014-07-15 16:18:32 -07001443 android.provider.Settings.Secure.PREFERRED_TTY_MODE,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001444 TelecomManager.TTY_MODE_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001445 if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
1446 Integer.toString(buttonTtyMode));
1447
1448 if (buttonTtyMode != settingsTtyMode) {
1449 switch(buttonTtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001450 case TelecomManager.TTY_MODE_OFF:
1451 case TelecomManager.TTY_MODE_FULL:
1452 case TelecomManager.TTY_MODE_HCO:
1453 case TelecomManager.TTY_MODE_VCO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001454 android.provider.Settings.Secure.putInt(getContentResolver(),
1455 android.provider.Settings.Secure.PREFERRED_TTY_MODE, buttonTtyMode);
1456 break;
1457 default:
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001458 buttonTtyMode = TelecomManager.TTY_MODE_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001459 }
1460
1461 mButtonTTY.setValue(Integer.toString(buttonTtyMode));
1462 updatePreferredTtyModeSummary(buttonTtyMode);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001463 Intent ttyModeChanged = new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
1464 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001465 sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
1466 }
1467 }
1468
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001469 private void updatePreferredTtyModeSummary(int TtyMode) {
1470 String [] txts = getResources().getStringArray(R.array.tty_mode_entries);
1471 switch(TtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001472 case TelecomManager.TTY_MODE_OFF:
1473 case TelecomManager.TTY_MODE_HCO:
1474 case TelecomManager.TTY_MODE_VCO:
1475 case TelecomManager.TTY_MODE_FULL:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001476 mButtonTTY.setSummary(txts[TtyMode]);
1477 break;
1478 default:
1479 mButtonTTY.setEnabled(false);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001480 mButtonTTY.setSummary(txts[TelecomManager.TTY_MODE_OFF]);
Sailesh Nepalbf900542014-07-15 16:18:32 -07001481 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001482 }
1483 }
1484
1485 private static void log(String msg) {
1486 Log.d(LOG_TAG, msg);
1487 }
1488
1489 /**
1490 * Updates the look of the VM preference widgets based on current VM provider settings.
1491 * Note that the provider name is loaded form the found activity via loadLabel in
1492 * {@link #initVoiceMailProviders()} in order for it to be localizable.
1493 */
1494 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1495 final String key = currentProviderSetting;
1496 final VoiceMailProvider provider = mVMProvidersData.get(key);
1497
1498 /* This is the case when we are coming up on a freshly wiped phone and there is no
1499 persisted value for the list preference mVoicemailProviders.
1500 In this case we want to show the UI asking the user to select a voicemail provider as
1501 opposed to silently falling back to default one. */
1502 if (provider == null) {
1503 if (DBG) {
1504 log("updateVMPreferenceWidget: provider for the key \"" + key + "\" is null.");
1505 }
1506 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1507 mVoicemailSettings.setEnabled(false);
1508 mVoicemailSettings.setIntent(null);
1509
1510 mVoicemailNotificationVibrate.setEnabled(false);
1511 } else {
1512 if (DBG) {
1513 log("updateVMPreferenceWidget: provider for the key \"" + key + "\".."
1514 + "name: " + provider.name
1515 + ", intent: " + provider.intent);
1516 }
1517 final String providerName = provider.name;
1518 mVoicemailProviders.setSummary(providerName);
1519 mVoicemailSettings.setEnabled(true);
1520 mVoicemailSettings.setIntent(provider.intent);
1521
1522 mVoicemailNotificationVibrate.setEnabled(true);
1523 }
1524 }
1525
1526 /**
1527 * Enumerates existing VM providers and puts their data into the list and populates
1528 * the preference list objects with their names.
1529 * In case we are called with ACTION_ADD_VOICEMAIL intent the intent may have
1530 * an extra string called IGNORE_PROVIDER_EXTRA with "package.activityName" of the provider
1531 * which should be hidden when we bring up the list of possible VM providers to choose.
1532 */
1533 private void initVoiceMailProviders() {
1534 if (DBG) log("initVoiceMailProviders()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001535
1536 String providerToIgnore = null;
1537 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1538 if (getIntent().hasExtra(IGNORE_PROVIDER_EXTRA)) {
1539 providerToIgnore = getIntent().getStringExtra(IGNORE_PROVIDER_EXTRA);
1540 }
1541 if (DBG) log("Found ACTION_ADD_VOICEMAIL. providerToIgnore=" + providerToIgnore);
1542 if (providerToIgnore != null) {
1543 // IGNORE_PROVIDER_EXTRA implies we want to remove the choice from the list.
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
1550 // Stick the default element which is always there
1551 final String myCarrier = getString(R.string.voicemail_default);
1552 mVMProvidersData.put(DEFAULT_VM_PROVIDER_KEY, new VoiceMailProvider(myCarrier, null));
1553
1554 // Enumerate providers
1555 PackageManager pm = getPackageManager();
1556 Intent intent = new Intent();
1557 intent.setAction(ACTION_CONFIGURE_VOICEMAIL);
1558 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
1559 int len = resolveInfos.size() + 1; // +1 for the default choice we will insert.
1560
1561 // Go through the list of discovered providers populating the data map
1562 // skip the provider we were instructed to ignore if there was one
1563 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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001567 if (key.equals(providerToIgnore)) {
1568 if (DBG) log("Ignoring key: " + key);
1569 len--;
1570 continue;
1571 }
1572 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);
1576 providerIntent.setClassName(currentActivityInfo.packageName,
1577 currentActivityInfo.name);
1578 if (DBG) {
1579 log("Store loaded VoiceMailProvider. key: " + key
1580 + " -> name: " + nameForDisplay + ", intent: " + providerIntent);
1581 }
1582 mVMProvidersData.put(
1583 key,
1584 new VoiceMailProvider(nameForDisplay, providerIntent));
1585
1586 }
1587
1588 // Now we know which providers to display - create entries and values array for
1589 // the list preference
1590 String [] entries = new String [len];
1591 String [] values = new String [len];
1592 entries[0] = myCarrier;
1593 values[0] = DEFAULT_VM_PROVIDER_KEY;
1594 int entryIdx = 1;
1595 for (int i = 0; i < resolveInfos.size(); i++) {
Andrew Lee6214e2b2014-11-04 13:57:38 -08001596 final String key = resolveInfos.get(i).activityInfo.name;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001597 if (!mVMProvidersData.containsKey(key)) {
1598 continue;
1599 }
1600 entries[entryIdx] = mVMProvidersData.get(key).name;
1601 values[entryIdx] = key;
1602 entryIdx++;
1603 }
1604
1605 // ListPreference is now updated.
1606 mVoicemailProviders.setEntries(entries);
1607 mVoicemailProviders.setEntryValues(values);
1608
1609 // Remember the current Voicemail Provider key as a "previous" key. This will be used
1610 // when we fail to update Voicemail Provider, which requires rollback.
1611 // We will update this when the VM Provider setting is successfully updated.
1612 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1613 if (DBG) log("Set up the first mPreviousVMProviderKey: " + mPreviousVMProviderKey);
1614
1615 // Finally update the preference texts.
1616 updateVMPreferenceWidgets(mPreviousVMProviderKey);
1617 }
1618
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001619 /**
1620 * Simulates user clicking on a passed preference.
1621 * Usually needed when the preference is a dialog preference and we want to invoke
1622 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001623 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001624 */
1625 private void simulatePreferenceClick(Preference preference) {
1626 // Go through settings until we find our setting
1627 // and then simulate a click on it to bring up the dialog
1628 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1629 for (int idx = 0; idx < adapter.getCount(); idx++) {
1630 if (adapter.getItem(idx) == preference) {
1631 getPreferenceScreen().onItemClick(this.getListView(),
1632 null, idx, adapter.getItemId(idx));
1633 break;
1634 }
1635 }
1636 }
1637
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001638 private String getCurrentVoicemailProviderKey() {
1639 final String key = mVoicemailProviders.getValue();
1640 return (key != null) ? key : DEFAULT_VM_PROVIDER_KEY;
1641 }
1642
1643 @Override
1644 public boolean onOptionsItemSelected(MenuItem item) {
1645 final int itemId = item.getItemId();
1646 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001647 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001648 return true;
1649 }
1650 return super.onOptionsItemSelected(item);
1651 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001652 /**
1653 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1654 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1655 */
1656 public static void goUpToTopLevelSetting(Activity activity) {
1657 Intent intent = new Intent(activity, CallFeaturesSetting.class);
1658 intent.setAction(Intent.ACTION_MAIN);
1659 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1660 activity.startActivity(intent);
1661 activity.finish();
1662 }
1663}