blob: c99e432facda4c76256a9d179a44be11b4d52446 [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";
Andrew Lee2b36ba22014-11-05 17:08:49 -0800166 private static final String CALL_FORWARDING_KEY = "call_forwarding_key";
167 private static final String ADDITIONAL_GSM_SETTINGS_KEY = "additional_gsm_call_settings_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168
Andrew Leedb2fe562014-09-03 15:40:43 -0700169 private static final String DEFAULT_OUTGOING_ACCOUNT_KEY = "default_outgoing_account";
Andrew Leece8ae2a2014-09-10 10:41:48 -0700170 private static final String PHONE_ACCOUNT_SETTINGS_KEY =
171 "phone_account_settings_preference_screen";
Andrew Leedb2fe562014-09-03 15:40:43 -0700172
Andrew Leedf14ead2014-10-17 14:22:52 -0700173 private static final String ENABLE_VIDEO_CALLING_KEY = "button_enable_video_calling";
174
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700175 /** Event for Async voicemail change call */
176 private static final int EVENT_VOICEMAIL_CHANGED = 500;
177 private static final int EVENT_FORWARDING_CHANGED = 501;
178 private static final int EVENT_FORWARDING_GET_COMPLETED = 502;
179
Andrew Lee2170a972014-08-13 18:13:01 -0700180 private static final int MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY = 1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700181
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700182 public static final String HAC_KEY = "HACSetting";
183 public static final String HAC_VAL_ON = "ON";
184 public static final String HAC_VAL_OFF = "OFF";
185
186 /** Handle to voicemail pref */
187 private static final int VOICEMAIL_PREF_ID = 1;
188 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
189
190 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700191 private AudioManager mAudioManager;
Andrew Lee88b51e22014-10-29 15:48:51 -0700192 private VoicemailProviderSettingsUtil mVmProviderSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700193
194 private static final int VM_NOCHANGE_ERROR = 400;
195 private static final int VM_RESPONSE_ERROR = 500;
196 private static final int FW_SET_RESPONSE_ERROR = 501;
197 private static final int FW_GET_RESPONSE_ERROR = 502;
198
199
200 // dialog identifiers for voicemail
201 private static final int VOICEMAIL_DIALOG_CONFIRM = 600;
202 private static final int VOICEMAIL_FWD_SAVING_DIALOG = 601;
203 private static final int VOICEMAIL_FWD_READING_DIALOG = 602;
204 private static final int VOICEMAIL_REVERTING_DIALOG = 603;
205
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700206 // voicemail notification vibration string constants
207 private static final String VOICEMAIL_VIBRATION_ALWAYS = "always";
208 private static final String VOICEMAIL_VIBRATION_NEVER = "never";
209
Andrew Lee5ed870c2014-10-29 11:47:49 -0700210 private SubscriptionInfoHelper mSubscriptionInfoHelper;
211
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700212 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
213
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700214 /** Whether dialpad plays DTMF tone or not. */
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700215 private CheckBoxPreference mButtonAutoRetry;
216 private CheckBoxPreference mButtonHAC;
217 private ListPreference mButtonDTMF;
218 private ListPreference mButtonTTY;
Andrew Leece8ae2a2014-09-10 10:41:48 -0700219 private Preference mPhoneAccountSettingsPreference;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700220 private ListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700221 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700222 private PreferenceScreen mVoicemailSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700223 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700224 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700225
226 private class VoiceMailProvider {
Andrew Leef1776d82014-11-04 14:45:02 -0800227 public String name;
228 public Intent intent;
229
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700230 public VoiceMailProvider(String name, Intent intent) {
231 this.name = name;
232 this.intent = intent;
233 }
Andrew Leef1776d82014-11-04 14:45:02 -0800234
235 public String toString() {
236 return "[ Name: " + name + ", Intent: " + intent + " ]";
237 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700238 }
239
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700240 /**
241 * Results of reading forwarding settings
242 */
243 private CallForwardInfo[] mForwardingReadResults = null;
244
245 /**
246 * Result of forwarding number change.
247 * Keys are reasons (eg. unconditional forwarding).
248 */
249 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
250
251 /**
252 * Expected CF read result types.
253 * This set keeps track of the CF types for which we've issued change
254 * commands so we can tell when we've received all of the responses.
255 */
256 private Collection<Integer> mExpectedChangeResultReasons = null;
257
258 /**
259 * Result of vm number change
260 */
261 private AsyncResult mVoicemailChangeResult = null;
262
263 /**
264 * Previous VM provider setting so we can return to it in case of failure.
265 */
266 private String mPreviousVMProviderKey = null;
267
268 /**
269 * Id of the dialog being currently shown.
270 */
271 private int mCurrentDialogId = 0;
272
273 /**
274 * Flag indicating that we are invoking settings for the voicemail provider programmatically
275 * due to vm provider change.
276 */
277 private boolean mVMProviderSettingsForced = false;
278
279 /**
280 * Flag indicating that we are making changes to vm or fwd numbers
281 * due to vm provider change.
282 */
283 private boolean mChangingVMorFwdDueToProviderChange = false;
284
285 /**
286 * True if we are in the process of vm & fwd number change and vm has already been changed.
287 * This is used to decide what to do in case of rollback.
288 */
289 private boolean mVMChangeCompletedSuccessfully = false;
290
291 /**
292 * True if we had full or partial failure setting forwarding numbers and so need to roll them
293 * back.
294 */
295 private boolean mFwdChangesRequireRollback = false;
296
297 /**
298 * Id of error msg to display to user once we are done reverting the VM provider to the previous
299 * one.
300 */
301 private int mVMOrFwdSetError = 0;
302
303 /**
304 * Data about discovered voice mail settings providers.
305 * Is populated by querying which activities can handle ACTION_CONFIGURE_VOICEMAIL.
306 * They key in this map is package name + activity name.
307 * We always add an entry for the default provider with a key of empty
308 * string and intent value of null.
309 * @see #initVoiceMailProviders()
310 */
311 private final Map<String, VoiceMailProvider> mVMProvidersData =
312 new HashMap<String, VoiceMailProvider>();
313
314 /** string to hold old voicemail number as it is being updated. */
315 private String mOldVmNumber;
316
317 // New call forwarding settings and vm number we will be setting
318 // Need to save these since before we get to saving we need to asynchronously
319 // query the existing forwarding settings.
320 private CallForwardInfo[] mNewFwdSettings;
321 private String mNewVMNumber;
322
323 private boolean mForeground;
324
325 @Override
326 public void onPause() {
327 super.onPause();
328 mForeground = false;
329 }
330
331 /**
332 * We have to pull current settings from the network for all kinds of
333 * voicemail providers so we can tell whether we have to update them,
334 * so use this bit to keep track of whether we're reading settings for the
335 * default provider and should therefore save them out when done.
336 */
337 private boolean mReadingSettingsForDefaultProvider = false;
338
Tyler Gunnbaee2952014-09-10 16:01:02 -0700339 /**
340 * Used to indicate that the voicemail preference should be shown.
341 */
342 private boolean mShowVoicemailPreference = false;
343
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344 /*
345 * Click Listeners, handle click based on objects attached to UI.
346 */
347
348 // Click listener for all toggle events
349 @Override
350 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
351 if (preference == mSubMenuVoicemailSettings) {
352 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700353 } else if (preference == mButtonDTMF) {
354 return true;
355 } else if (preference == mButtonTTY) {
356 return true;
357 } else if (preference == mButtonAutoRetry) {
358 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
359 android.provider.Settings.Global.CALL_AUTO_RETRY,
360 mButtonAutoRetry.isChecked() ? 1 : 0);
361 return true;
362 } else if (preference == mButtonHAC) {
363 int hac = mButtonHAC.isChecked() ? 1 : 0;
364 // Update HAC value in Settings database
365 Settings.System.putInt(mPhone.getContext().getContentResolver(),
366 Settings.System.HEARING_AID, hac);
367
368 // Update HAC Value in AudioManager
369 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
370 return true;
371 } else if (preference == mVoicemailSettings) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800372 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
373
Yorke Leea0f63bf2014-10-09 18:27:20 -0700374 final Dialog dialog = mVoicemailSettings.getDialog();
375 if (dialog != null) {
376 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
377 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800378
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700379 if (preference.getIntent() != null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800380 if (DBG) log("Invoking cfg intent " + preference.getIntent().getPackage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700381
382 // onActivityResult() will be responsible for resetting some of variables.
383 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
384 return true;
385 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800386 if (DBG) log("onPreferenceTreeClick(). No intent; use default behavior in xml.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700387
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800388 // onActivityResult() will not be called, so reset variables here.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700389 mPreviousVMProviderKey = DEFAULT_VM_PROVIDER_KEY;
390 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700391 return false;
392 }
Andrew Lee97708a42014-09-25 12:39:07 -0700393 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700394 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
395 if (dialog != null) {
396 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
397 }
Andrew Lee97708a42014-09-25 12:39:07 -0700398 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700399 }
400 return false;
401 }
402
403 /**
404 * Implemented to support onPreferenceChangeListener to look for preference
405 * changes.
406 *
407 * @param preference is the preference to be changed
408 * @param objValue should be the value of the selection, NOT its localized
409 * display value.
410 */
411 @Override
412 public boolean onPreferenceChange(Preference preference, Object objValue) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800413 if (DBG) log("onPreferenceChange: \"" + preference + "\" changed to \"" + objValue + "\"");
Andrew Lee2170a972014-08-13 18:13:01 -0700414
415 if (preference == mButtonDTMF) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700416 int index = mButtonDTMF.findIndexOfValue((String) objValue);
417 Settings.System.putInt(mPhone.getContext().getContentResolver(),
418 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
419 } else if (preference == mButtonTTY) {
420 handleTTYChange(preference, objValue);
421 } else if (preference == mVoicemailProviders) {
422 final String newProviderKey = (String) objValue;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800423
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700424 // If previous provider key and the new one is same, we don't need to handle it.
425 if (mPreviousVMProviderKey.equals(newProviderKey)) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800426 if (DBG) log("No change is made to the VM provider setting.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700427 return true;
428 }
429 updateVMPreferenceWidgets(newProviderKey);
430
Andrew Leeb490d732014-10-27 15:00:41 -0700431 final VoicemailProviderSettings newProviderSettings =
Andrew Lee88b51e22014-10-29 15:48:51 -0700432 mVmProviderSettingsUtil.load(newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700433
Andrew Lee88b51e22014-10-29 15:48:51 -0700434 // If the user switches to a voice mail provider and we have numbers stored for it we
435 // will automatically change the phone's voice mail and forwarding number to the stored
436 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700437 if (newProviderSettings == null) {
438 // Force the user into a configuration of the chosen provider
439 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
440 mVMProviderSettingsForced = true;
441 simulatePreferenceClick(mVoicemailSettings);
442 } else {
443 if (DBG) log("Saved preferences found - switching to them");
444 // Set this flag so if we get a failure we revert to previous provider
445 mChangingVMorFwdDueToProviderChange = true;
446 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
447 }
Andrew Leedf14ead2014-10-17 14:22:52 -0700448 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700449 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
450 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
451 } else {
452 AlertDialog.Builder builder = new AlertDialog.Builder(this);
453 DialogInterface.OnClickListener networkSettingsClickListener =
454 new Dialog.OnClickListener() {
455 @Override
456 public void onClick(DialogInterface dialog, int which) {
457 startActivity(new Intent(mPhone.getContext(),
458 com.android.phone.MobileNetworkSettings.class));
459 }
460 };
461 builder.setMessage(getResources().getString(
462 R.string.enable_video_calling_dialog_msg))
463 .setNeutralButton(getResources().getString(
464 R.string.enable_video_calling_dialog_settings),
465 networkSettingsClickListener)
466 .setPositiveButton(android.R.string.ok, null)
467 .show();
468 return false;
469 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700470 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800471
472 // Always let the preference setting proceed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700473 return true;
474 }
475
476 @Override
477 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800478 if (DBG) log("onDialogClosed: Button clicked is " + buttonClicked);
479
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700480 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
481 return;
482 }
483
484 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700485 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
486 mSubMenuVoicemailSettings.getPhoneNumber(),
487 VoicemailProviderSettings.NO_FORWARDING);
488 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700489 }
490 }
491
492 /**
493 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
494 * This method set the default values for the various
495 * EditPhoneNumberPreference dialogs.
496 */
497 @Override
498 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
499 if (preference == mSubMenuVoicemailSettings) {
500 // update the voicemail number field, which takes care of the
501 // mSubMenuVoicemailSettings itself, so we should return null.
502 if (DBG) log("updating default for voicemail dialog");
503 updateVoiceNumberField();
504 return null;
505 }
506
507 String vmDisplay = mPhone.getVoiceMailNumber();
508 if (TextUtils.isEmpty(vmDisplay)) {
509 // if there is no voicemail number, we just return null to
510 // indicate no contribution.
511 return null;
512 }
513
514 // Return the voicemail number prepended with "VM: "
515 if (DBG) log("updating default for call forwarding dialogs");
516 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
517 }
518
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700519 private void switchToPreviousVoicemailProvider() {
520 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700521
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800522 if (mPreviousVMProviderKey == null) {
523 return;
524 }
525
526 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
527 showDialogIfForeground(VOICEMAIL_REVERTING_DIALOG);
528 final VoicemailProviderSettings prevSettings =
529 mVmProviderSettingsUtil.load(mPreviousVMProviderKey);
530 if (prevSettings == null) {
531 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
532 + mPreviousVMProviderKey + "\" is null but should be loaded.");
533 }
534
535 if (mVMChangeCompletedSuccessfully) {
536 mNewVMNumber = prevSettings.getVoicemailNumber();
537 Log.i(LOG_TAG, "VM change is already completed successfully."
538 + "Have to revert VM back to " + mNewVMNumber + " again.");
539 mPhone.setVoiceMailNumber(
540 mPhone.getVoiceMailAlphaTag().toString(),
541 mNewVMNumber,
542 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
543 }
544
545 if (mFwdChangesRequireRollback) {
546 Log.i(LOG_TAG, "Requested to rollback forwarding changes.");
547
548 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
549 if (prevFwdSettings != null) {
550 Map<Integer, AsyncResult> results = mForwardingChangeResults;
551 resetForwardingChangeState();
552 for (int i = 0; i < prevFwdSettings.length; i++) {
553 CallForwardInfo fi = prevFwdSettings[i];
554 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
555 // Only revert the settings for which the update succeeded.
556 AsyncResult result = results.get(fi.reason);
557 if (result != null && result.exception == null) {
558 mExpectedChangeResultReasons.add(fi.reason);
559 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
560 mRevertOptionComplete.obtainMessage(
561 EVENT_FORWARDING_CHANGED, i, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700562 }
563 }
564 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700565 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800566 } else {
567 if (DBG) log("No need to revert");
568 onRevertDone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700569 }
570 }
571
572 private void onRevertDone() {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800573 if (DBG) log("onRevertDone: Changing provider key back to " + mPreviousVMProviderKey);
574
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700575 updateVMPreferenceWidgets(mPreviousVMProviderKey);
576 updateVoiceNumberField();
577 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800578 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700579 mVMOrFwdSetError = 0;
580 }
581 }
582
583 @Override
584 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
585 if (DBG) {
586 log("onActivityResult: requestCode: " + requestCode
587 + ", resultCode: " + resultCode
588 + ", data: " + data);
589 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800590
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700591 // there are cases where the contact picker may end up sending us more than one
592 // request. We want to ignore the request if we're not in the correct state.
593 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
594 boolean failure = false;
595
596 // No matter how the processing of result goes lets clear the flag
597 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
598 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
599 mVMProviderSettingsForced = false;
600
601 String vmNum = null;
602 if (resultCode != RESULT_OK) {
603 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
604 failure = true;
605 } else {
606 if (data == null) {
607 if (DBG) log("onActivityResult: vm provider cfg result has no data");
608 failure = true;
609 } else {
610 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
611 if (DBG) log("Provider requested signout");
612 if (isVMProviderSettingsForced) {
613 if (DBG) log("Going back to previous provider on signout");
614 switchToPreviousVoicemailProvider();
615 } else {
616 final String victim = getCurrentVoicemailProviderKey();
617 if (DBG) log("Relaunching activity and ignoring " + victim);
618 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
619 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
620 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
621 this.startActivity(i);
622 }
623 return;
624 }
625 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
626 if (vmNum == null || vmNum.length() == 0) {
627 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
628 failure = true;
629 }
630 }
631 }
632 if (failure) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800633 if (DBG) log("Failure in return from voicemail provider.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700634 if (isVMProviderSettingsForced) {
635 switchToPreviousVoicemailProvider();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700636 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800637
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700638 return;
639 }
640 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
641 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
642
Santos Cordonda120f42014-08-06 04:44:34 -0700643 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700644 // send it to the provider when it's config is invoked so it can use this as default
645 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
646
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800647 if (DBG) log("onActivityResult: cfg result has forwarding number " + fwdNum);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700648 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700649 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700650 return;
651 }
652
653 if (requestCode == VOICEMAIL_PREF_ID) {
654 if (resultCode != RESULT_OK) {
655 if (DBG) log("onActivityResult: contact picker result not OK.");
656 return;
657 }
658
659 Cursor cursor = null;
660 try {
661 cursor = getContentResolver().query(data.getData(),
662 NUM_PROJECTION, null, null, null);
663 if ((cursor == null) || (!cursor.moveToFirst())) {
664 if (DBG) log("onActivityResult: bad contact data, no results found.");
665 return;
666 }
667 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
668 return;
669 } finally {
670 if (cursor != null) {
671 cursor.close();
672 }
673 }
674 }
675
676 super.onActivityResult(requestCode, resultCode, data);
677 }
678
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700679 /**
680 * Wrapper around showDialog() that will silently do nothing if we're
681 * not in the foreground.
682 *
683 * This is useful here because most of the dialogs we display from
684 * this class are triggered by asynchronous events (like
685 * success/failure messages from the telephony layer) and it's
686 * possible for those events to come in even after the user has gone
687 * to a different screen.
688 */
689 // TODO: this is too brittle: it's still easy to accidentally add new
690 // code here that calls showDialog() directly (which will result in a
691 // WindowManager$BadTokenException if called after the activity has
692 // been stopped.)
693 //
694 // It would be cleaner to do the "if (mForeground)" check in one
695 // central place, maybe by using a single Handler for all asynchronous
696 // events (and have *that* discard events if we're not in the
697 // foreground.)
698 //
699 // Unfortunately it's not that simple, since we sometimes need to do
700 // actual work to handle these events whether or not we're in the
701 // foreground (see the Handler code in mSetOptionComplete for
702 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800703 //
704 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
705 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700706 private void showDialogIfForeground(int id) {
707 if (mForeground) {
708 showDialog(id);
709 }
710 }
711
712 private void dismissDialogSafely(int id) {
713 try {
714 dismissDialog(id);
715 } catch (IllegalArgumentException e) {
716 // This is expected in the case where we were in the background
717 // at the time we would normally have shown the dialog, so we didn't
718 // show it.
719 }
720 }
721
Andrew Leeb490d732014-10-27 15:00:41 -0700722 private void saveVoiceMailAndForwardingNumber(
723 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700724 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700725 mNewVMNumber = newSettings.getVoicemailNumber();
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800726 mNewVMNumber = (mNewVMNumber == null) ? "" : mNewVMNumber;
Andrew Leeb490d732014-10-27 15:00:41 -0700727 mNewFwdSettings = newSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700728
729 // No fwd settings on CDMA
730 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
731 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700732 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700733 }
734
Andrew Leee3c15212014-10-28 13:12:55 -0700735 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700736 if (mNewVMNumber.equals(mOldVmNumber)
737 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Leeab082272014-11-04 15:50:42 -0800738 showDialogIfForeground(VM_NOCHANGE_ERROR);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700739 return;
740 }
741
Andrew Lee88b51e22014-10-29 15:48:51 -0700742 mVmProviderSettingsUtil.save(key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700743 mVMChangeCompletedSuccessfully = false;
744 mFwdChangesRequireRollback = false;
745 mVMOrFwdSetError = 0;
746 if (!key.equals(mPreviousVMProviderKey)) {
747 mReadingSettingsForDefaultProvider =
748 mPreviousVMProviderKey.equals(DEFAULT_VM_PROVIDER_KEY);
749 if (DBG) log("Reading current forwarding settings");
Andrew Leeb490d732014-10-27 15:00:41 -0700750 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
751 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
752 for (int i = 0; i < mForwardingReadResults.length; i++) {
753 mPhone.getCallForwardingOption(
754 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700755 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
756 }
757 showDialogIfForeground(VOICEMAIL_FWD_READING_DIALOG);
758 } else {
759 saveVoiceMailAndForwardingNumberStage2();
760 }
761 }
762
763 private final Handler mGetOptionComplete = new Handler() {
764 @Override
765 public void handleMessage(Message msg) {
766 AsyncResult result = (AsyncResult) msg.obj;
767 switch (msg.what) {
768 case EVENT_FORWARDING_GET_COMPLETED:
769 handleForwardingSettingsReadResult(result, msg.arg1);
770 break;
771 }
772 }
773 };
774
775 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
776 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800777
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700778 Throwable error = null;
779 if (ar.exception != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700780 error = ar.exception;
Andrew Lee1af6cf72014-11-04 17:35:26 -0800781 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700782 }
783 if (ar.userObj instanceof Throwable) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800784 error = (Throwable) ar.userObj;
785 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700786 }
787
788 // We may have already gotten an error and decided to ignore the other results.
789 if (mForwardingReadResults == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800790 if (DBG) Log.d(LOG_TAG, "Ignoring fwd reading result: " + idx);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700791 return;
792 }
793
794 // In case of error ignore other results, show an error dialog
795 if (error != null) {
796 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
797 mForwardingReadResults = null;
798 dismissDialogSafely(VOICEMAIL_FWD_READING_DIALOG);
Andrew Leeab082272014-11-04 15:50:42 -0800799 showDialogIfForeground(FW_GET_RESPONSE_ERROR);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700800 return;
801 }
802
Andrew Lee1af6cf72014-11-04 17:35:26 -0800803 // Get the forwarding info.
804 mForwardingReadResults[idx] = CallForwardInfoUtil.getCallForwardInfo(
805 (CallForwardInfo[]) ar.result,
806 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx]);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700807
808 // Check if we got all the results already
809 boolean done = true;
810 for (int i = 0; i < mForwardingReadResults.length; i++) {
811 if (mForwardingReadResults[i] == null) {
812 done = false;
813 break;
814 }
815 }
Andrew Lee1af6cf72014-11-04 17:35:26 -0800816
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700817 if (done) {
818 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
819 dismissDialogSafely(VOICEMAIL_FWD_READING_DIALOG);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800820
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700821 if (mReadingSettingsForDefaultProvider) {
Andrew Lee88b51e22014-10-29 15:48:51 -0700822 mVmProviderSettingsUtil.save(DEFAULT_VM_PROVIDER_KEY,
823 new VoicemailProviderSettings(this.mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700824 mReadingSettingsForDefaultProvider = false;
825 }
826 saveVoiceMailAndForwardingNumberStage2();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700827 }
828 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800829
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700830 private void resetForwardingChangeState() {
831 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
832 mExpectedChangeResultReasons = new HashSet<Integer>();
833 }
834
835 // Called after we are done saving the previous forwarding settings if
836 // we needed.
837 private void saveVoiceMailAndForwardingNumberStage2() {
838 mForwardingChangeResults = null;
839 mVoicemailChangeResult = null;
Andrew Leeb490d732014-10-27 15:00:41 -0700840 if (mNewFwdSettings != VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700841 resetForwardingChangeState();
842 for (int i = 0; i < mNewFwdSettings.length; i++) {
843 CallForwardInfo fi = mNewFwdSettings[i];
Andrew Lee1af6cf72014-11-04 17:35:26 -0800844 CallForwardInfo fiForReason =
845 CallForwardInfoUtil.infoForReason(mForwardingReadResults, fi.reason);
846 final boolean doUpdate = CallForwardInfoUtil.isUpdateRequired(fiForReason, fi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700847
848 if (doUpdate) {
849 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
850 mExpectedChangeResultReasons.add(i);
851
Andrew Lee1af6cf72014-11-04 17:35:26 -0800852 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700853 mSetOptionComplete.obtainMessage(
854 EVENT_FORWARDING_CHANGED, fi.reason, 0));
855 }
856 }
857 showDialogIfForeground(VOICEMAIL_FWD_SAVING_DIALOG);
858 } else {
859 if (DBG) log("Not touching fwd #");
860 setVMNumberWithCarrier();
861 }
862 }
863
864 private void setVMNumberWithCarrier() {
865 if (DBG) log("save voicemail #: " + mNewVMNumber);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800866
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700867 mPhone.setVoiceMailNumber(
868 mPhone.getVoiceMailAlphaTag().toString(),
869 mNewVMNumber,
870 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
871 }
872
873 /**
874 * Callback to handle option update completions
875 */
876 private final Handler mSetOptionComplete = new Handler() {
877 @Override
878 public void handleMessage(Message msg) {
879 AsyncResult result = (AsyncResult) msg.obj;
880 boolean done = false;
881 switch (msg.what) {
882 case EVENT_VOICEMAIL_CHANGED:
883 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700884 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700885 done = true;
886 break;
887 case EVENT_FORWARDING_CHANGED:
888 mForwardingChangeResults.put(msg.arg1, result);
889 if (result.exception != null) {
890 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
891 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700892 }
Andrew Leee438b312014-10-29 16:59:15 -0700893 if (isForwardingCompleted()) {
894 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700895 if (DBG) log("Overall fwd changes completed ok, starting vm change");
896 setVMNumberWithCarrier();
897 } else {
898 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
899 "Check if we need to try rollback for some settings.");
900 mFwdChangesRequireRollback = false;
901 Iterator<Map.Entry<Integer,AsyncResult>> it =
902 mForwardingChangeResults.entrySet().iterator();
903 while (it.hasNext()) {
904 Map.Entry<Integer,AsyncResult> entry = it.next();
905 if (entry.getValue().exception == null) {
906 // If at least one succeeded we have to revert
907 Log.i(LOG_TAG, "Rollback will be required");
908 mFwdChangesRequireRollback = true;
909 break;
910 }
911 }
912 if (!mFwdChangesRequireRollback) {
913 Log.i(LOG_TAG, "No rollback needed.");
914 }
915 done = true;
916 }
917 }
918 break;
919 default:
920 // TODO: should never reach this, may want to throw exception
921 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800922
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700923 if (done) {
924 if (DBG) log("All VM provider related changes done");
925 if (mForwardingChangeResults != null) {
926 dismissDialogSafely(VOICEMAIL_FWD_SAVING_DIALOG);
927 }
Andrew Leee438b312014-10-29 16:59:15 -0700928 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700929 }
930 }
931 };
932
933 /**
934 * Callback to handle option revert completions
935 */
936 private final Handler mRevertOptionComplete = new Handler() {
937 @Override
938 public void handleMessage(Message msg) {
939 AsyncResult result = (AsyncResult) msg.obj;
940 switch (msg.what) {
941 case EVENT_VOICEMAIL_CHANGED:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700942 if (DBG) log("VM revert complete msg");
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800943 mVoicemailChangeResult = result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700944 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800945
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700946 case EVENT_FORWARDING_CHANGED:
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800947 if (DBG) log("FWD revert complete msg ");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700948 mForwardingChangeResults.put(msg.arg1, result);
949 if (result.exception != null) {
950 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
951 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700952 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700953 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800954
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700955 default:
956 // TODO: should never reach this, may want to throw exception
957 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800958
959 final boolean done = (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null)
960 && (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700961 if (done) {
962 if (DBG) log("All VM reverts done");
963 dismissDialogSafely(VOICEMAIL_REVERTING_DIALOG);
964 onRevertDone();
965 }
966 }
967 };
968
969 /**
Andrew Leee438b312014-10-29 16:59:15 -0700970 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700971 */
Andrew Leee438b312014-10-29 16:59:15 -0700972 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700973 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -0700974 return true;
975 }
976
977 for (Integer reason : mExpectedChangeResultReasons) {
978 if (mForwardingChangeResults.get(reason) == null) {
979 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700980 }
981 }
Andrew Leee438b312014-10-29 16:59:15 -0700982
983 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700984 }
Andrew Leee438b312014-10-29 16:59:15 -0700985
986 private boolean isFwdChangeSuccess() {
987 if (mForwardingChangeResults == null) {
988 return true;
989 }
990
991 for (AsyncResult result : mForwardingChangeResults.values()) {
992 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700993 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700994 String msg = exception.getMessage();
995 msg = (msg != null) ? msg : "";
996 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
997 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700998 }
999 }
Andrew Leee438b312014-10-29 16:59:15 -07001000 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001001 }
1002
Andrew Leee438b312014-10-29 16:59:15 -07001003 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001004 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -07001005 String msg = mVoicemailChangeResult.exception.getMessage();
1006 msg = (msg != null) ? msg : "";
1007 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
1008 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001009 }
Andrew Leee438b312014-10-29 16:59:15 -07001010 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001011 }
1012
Andrew Leee438b312014-10-29 16:59:15 -07001013 private void handleSetVmOrFwdMessage() {
1014 if (DBG) log("handleSetVMMessage: set VM request complete");
1015
1016 if (!isFwdChangeSuccess()) {
Andrew Leeab082272014-11-04 15:50:42 -08001017 handleVmOrFwdSetError(FW_SET_RESPONSE_ERROR);
Andrew Leee438b312014-10-29 16:59:15 -07001018 } else if (!isVmChangeSuccess()) {
Andrew Leeab082272014-11-04 15:50:42 -08001019 handleVmOrFwdSetError(VM_RESPONSE_ERROR);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001020 } else {
Andrew Leeab082272014-11-04 15:50:42 -08001021 handleVmAndFwdSetSuccess(VOICEMAIL_DIALOG_CONFIRM);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001022 }
1023 }
1024
1025 /**
1026 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
1027 * changes to those settings and show "failure" dialog.
1028 *
Andrew Leeab082272014-11-04 15:50:42 -08001029 * @param dialogId ID of the dialog to show for the specific error case. Either
1030 * {@link #FW_SET_RESPONSE_ERROR} or {@link #VM_RESPONSE_ERROR}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001031 */
Andrew Leeab082272014-11-04 15:50:42 -08001032 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001033 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -08001034 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001035 mChangingVMorFwdDueToProviderChange = false;
1036 switchToPreviousVoicemailProvider();
1037 return;
1038 }
1039 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001040 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001041 updateVoiceNumberField();
1042 }
1043
1044 /**
1045 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1046 * This updates a bunch of variables and show "success" dialog.
1047 */
Andrew Leeab082272014-11-04 15:50:42 -08001048 private void handleVmAndFwdSetSuccess(int dialogId) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001049 if (DBG) log("handleVmAndFwdSetSuccess: key is " + getCurrentVoicemailProviderKey());
1050
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001051 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1052 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001053 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001054 updateVoiceNumberField();
1055 }
1056
1057 /**
1058 * Update the voicemail number from what we've recorded on the sim.
1059 */
1060 private void updateVoiceNumberField() {
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001061 if (DBG) log("updateVoiceNumberField()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001062
1063 mOldVmNumber = mPhone.getVoiceMailNumber();
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001064 if (TextUtils.isEmpty(mOldVmNumber)) {
1065 mSubMenuVoicemailSettings.setPhoneNumber("");
1066 mSubMenuVoicemailSettings.setSummary(getString(R.string.voicemail_number_not_set));
1067 } else {
1068 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1069 mSubMenuVoicemailSettings.setSummary(mOldVmNumber);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001070 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001071 }
1072
1073 /*
1074 * Helper Methods for Activity class.
1075 * The initial query commands are split into two pieces now
1076 * for individual expansion. This combined with the ability
1077 * to cancel queries allows for a much better user experience,
1078 * and also ensures that the user only waits to update the
1079 * data that is relevant.
1080 */
1081
1082 @Override
1083 protected void onPrepareDialog(int id, Dialog dialog) {
1084 super.onPrepareDialog(id, dialog);
1085 mCurrentDialogId = id;
1086 }
1087
1088 // dialog creation method, called by showDialog()
1089 @Override
1090 protected Dialog onCreateDialog(int id) {
1091 if ((id == VM_RESPONSE_ERROR) || (id == VM_NOCHANGE_ERROR) ||
1092 (id == FW_SET_RESPONSE_ERROR) || (id == FW_GET_RESPONSE_ERROR) ||
1093 (id == VOICEMAIL_DIALOG_CONFIRM)) {
1094
1095 AlertDialog.Builder b = new AlertDialog.Builder(this);
1096
1097 int msgId;
1098 int titleId = R.string.error_updating_title;
1099 switch (id) {
1100 case VOICEMAIL_DIALOG_CONFIRM:
1101 msgId = R.string.vm_changed;
1102 titleId = R.string.voicemail;
1103 // Set Button 2
1104 b.setNegativeButton(R.string.close_dialog, this);
1105 break;
1106 case VM_NOCHANGE_ERROR:
1107 // even though this is technically an error,
1108 // keep the title friendly.
1109 msgId = R.string.no_change;
1110 titleId = R.string.voicemail;
1111 // Set Button 2
1112 b.setNegativeButton(R.string.close_dialog, this);
1113 break;
1114 case VM_RESPONSE_ERROR:
1115 msgId = R.string.vm_change_failed;
1116 // Set Button 1
1117 b.setPositiveButton(R.string.close_dialog, this);
1118 break;
1119 case FW_SET_RESPONSE_ERROR:
1120 msgId = R.string.fw_change_failed;
1121 // Set Button 1
1122 b.setPositiveButton(R.string.close_dialog, this);
1123 break;
1124 case FW_GET_RESPONSE_ERROR:
1125 msgId = R.string.fw_get_in_vm_failed;
1126 b.setPositiveButton(R.string.alert_dialog_yes, this);
1127 b.setNegativeButton(R.string.alert_dialog_no, this);
1128 break;
1129 default:
1130 msgId = R.string.exception_error;
1131 // Set Button 3, tells the activity that the error is
1132 // not recoverable on dialog exit.
1133 b.setNeutralButton(R.string.close_dialog, this);
1134 break;
1135 }
1136
1137 b.setTitle(getText(titleId));
1138 String message = getText(msgId).toString();
1139 b.setMessage(message);
1140 b.setCancelable(false);
1141 AlertDialog dialog = b.create();
1142
1143 // make the dialog more obvious by bluring the background.
1144 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
1145
1146 return dialog;
1147 } else if (id == VOICEMAIL_FWD_SAVING_DIALOG || id == VOICEMAIL_FWD_READING_DIALOG ||
1148 id == VOICEMAIL_REVERTING_DIALOG) {
1149 ProgressDialog dialog = new ProgressDialog(this);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001150 dialog.setTitle(getText(R.string.call_settings));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001151 dialog.setIndeterminate(true);
1152 dialog.setCancelable(false);
1153 dialog.setMessage(getText(
1154 id == VOICEMAIL_FWD_SAVING_DIALOG ? R.string.updating_settings :
1155 (id == VOICEMAIL_REVERTING_DIALOG ? R.string.reverting_settings :
1156 R.string.reading_settings)));
1157 return dialog;
1158 }
1159
1160
1161 return null;
1162 }
1163
1164 // This is a method implemented for DialogInterface.OnClickListener.
1165 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1166 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1167 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1168 public void onClick(DialogInterface dialog, int which) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001169 if (DBG) log("onClick: button clicked is " + which);
1170
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001171 dialog.dismiss();
1172 switch (which){
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001173 case DialogInterface.BUTTON_NEGATIVE:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001174 if (mCurrentDialogId == FW_GET_RESPONSE_ERROR) {
1175 // We failed to get current forwarding settings and the user
1176 // does not wish to continue.
1177 switchToPreviousVoicemailProvider();
1178 }
1179 break;
1180 case DialogInterface.BUTTON_POSITIVE:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001181 if (mCurrentDialogId == FW_GET_RESPONSE_ERROR) {
1182 // We failed to get current forwarding settings but the user
1183 // wishes to continue changing settings to the new vm provider
1184 saveVoiceMailAndForwardingNumberStage2();
1185 } else {
1186 finish();
1187 }
1188 return;
1189 default:
1190 // just let the dialog close and go back to the input
1191 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001192
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001193 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1194 // with settings UI. If we were called to explicitly configure voice mail then
1195 // we finish the settings activity here to come back to whatever the user was doing.
1196 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1197 finish();
1198 }
1199 }
1200
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001201 /*
1202 * Activity class methods
1203 */
1204
1205 @Override
1206 protected void onCreate(Bundle icicle) {
1207 super.onCreate(icicle);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001208 if (DBG) log("onCreate: Intent is " + getIntent());
1209
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001210 mPhone = PhoneGlobals.getPhone();
Tyler Gunnbaee2952014-09-10 16:01:02 -07001211 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andrew Lee88b51e22014-10-29 15:48:51 -07001212 mVmProviderSettingsUtil = new VoicemailProviderSettingsUtil(getApplicationContext());
Tyler Gunnbaee2952014-09-10 16:01:02 -07001213
Tyler Gunnbaee2952014-09-10 16:01:02 -07001214 // Show the voicemail preference in onResume if the calling intent specifies the
1215 // ACTION_ADD_VOICEMAIL action.
1216 mShowVoicemailPreference = (icicle == null) &&
1217 getIntent().getAction().equals(ACTION_ADD_VOICEMAIL);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001218
1219 mSubscriptionInfoHelper = new SubscriptionInfoHelper(getIntent());
1220 mSubscriptionInfoHelper.setActionBarTitle(
1221 getActionBar(), getResources(), R.string.call_settings_with_label);
1222 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001223
1224 private void initPhoneAccountPreferences() {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001225 mPhoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001226
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001227 TelecomManager telecomManager = TelecomManager.from(this);
Andrew Lee93c345f2014-10-27 15:25:07 -07001228 TelephonyManager telephonyManager =
1229 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001230
Andrew Lee93c345f2014-10-27 15:25:07 -07001231 if ((telecomManager.getSimCallManagers().isEmpty() && !SipUtil.isVoipSupported(this))
1232 || telephonyManager.getPhoneCount() > 1) {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001233 getPreferenceScreen().removePreference(mPhoneAccountSettingsPreference);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001234 }
1235 }
1236
1237 private boolean canLaunchIntent(Intent intent) {
1238 PackageManager pm = getPackageManager();
1239 return pm.resolveActivity(intent, PackageManager.GET_ACTIVITIES) != null;
1240 }
1241
Tyler Gunnbaee2952014-09-10 16:01:02 -07001242 @Override
1243 protected void onResume() {
1244 super.onResume();
1245 mForeground = true;
1246
1247 PreferenceScreen preferenceScreen = getPreferenceScreen();
1248 if (preferenceScreen != null) {
1249 preferenceScreen.removeAll();
1250 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001251
1252 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001253
Andrew Leedb2fe562014-09-03 15:40:43 -07001254 initPhoneAccountPreferences();
1255
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001256 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001257 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1258 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1259 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1260 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001261
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001262 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1263 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1264 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
1265 mButtonTTY = (ListPreference) findPreference(BUTTON_TTY_KEY);
1266 mVoicemailProviders = (ListPreference) findPreference(BUTTON_VOICEMAIL_PROVIDER_KEY);
Andrew Lee312e8172014-10-23 17:01:36 -07001267 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001268
Andrew Lee2c027892014-10-29 11:29:54 -07001269 mVoicemailProviders.setOnPreferenceChangeListener(this);
1270 mVoicemailSettingsScreen =
1271 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
1272 mVoicemailSettings = (PreferenceScreen)findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
1273 mVoicemailNotificationVibrate =
1274 (CheckBoxPreference) findPreference(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY);
1275 initVoiceMailProviders();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001276
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001277
Andrew Lee64a7d792014-10-15 17:38:38 -07001278 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1279 mButtonDTMF.setOnPreferenceChangeListener(this);
1280 int dtmf = Settings.System.getInt(getContentResolver(),
1281 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1282 mButtonDTMF.setValueIndex(dtmf);
1283 } else {
1284 prefSet.removePreference(mButtonDTMF);
1285 mButtonDTMF = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001286 }
1287
Andrew Lee64a7d792014-10-15 17:38:38 -07001288 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1289 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1290 int autoretry = Settings.Global.getInt(
1291 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1292 mButtonAutoRetry.setChecked(autoretry != 0);
1293 } else {
1294 prefSet.removePreference(mButtonAutoRetry);
1295 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001296 }
1297
Andrew Lee64a7d792014-10-15 17:38:38 -07001298 if (getResources().getBoolean(R.bool.hac_enabled)) {
1299 mButtonHAC.setOnPreferenceChangeListener(this);
1300 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1301 mButtonHAC.setChecked(hac != 0);
1302 } else {
1303 prefSet.removePreference(mButtonHAC);
1304 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001305 }
1306
Andrew Lee64a7d792014-10-15 17:38:38 -07001307 TelecomManager telecomManager = TelecomManager.from(this);
1308 if (telecomManager != null && telecomManager.isTtySupported()) {
1309 mButtonTTY.setOnPreferenceChangeListener(this);
1310 int settingsTtyMode = Settings.Secure.getInt(getContentResolver(),
1311 Settings.Secure.PREFERRED_TTY_MODE,
1312 TelecomManager.TTY_MODE_OFF);
1313 mButtonTTY.setValue(Integer.toString(settingsTtyMode));
1314 updatePreferredTtyModeSummary(settingsTtyMode);
1315 } else {
1316 prefSet.removePreference(mButtonTTY);
1317 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001318 }
1319
1320 if (!getResources().getBoolean(R.bool.world_phone)) {
Andrew Lee2b36ba22014-11-05 17:08:49 -08001321 Preference cdmaOptions = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
1322 prefSet.removePreference(cdmaOptions);
1323
1324 // TODO: Support MSIM for this preference option.
1325 Preference gsmOptions = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
1326 prefSet.removePreference(gsmOptions);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001327
1328 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001329 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001330 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001331 prefSet.removePreference(fdnButton);
1332
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001333 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1334 addPreferencesFromResource(R.xml.cdma_call_privacy);
1335 }
1336 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001337 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(this, FdnSetting.class));
1338
Andrew Lee2170a972014-08-13 18:13:01 -07001339 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
Etan Cohen0ca1c802014-07-07 15:35:48 -07001340 addPreferencesFromResource(R.xml.gsm_umts_call_options);
Andrew Lee2b36ba22014-11-05 17:08:49 -08001341
1342 Preference callForwardingPref = prefSet.findPreference(CALL_FORWARDING_KEY);
1343 callForwardingPref.setIntent(mSubscriptionInfoHelper.getIntent(
1344 this, GsmUmtsCallForwardOptions.class));
1345
1346 Preference additionalGsmSettingsPref =
1347 prefSet.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
1348 additionalGsmSettingsPref.setIntent(mSubscriptionInfoHelper.getIntent(
1349 this, GsmUmtsAdditionalCallOptions.class));
Etan Cohen0ca1c802014-07-07 15:35:48 -07001350 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001351 } else {
1352 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1353 }
1354 }
1355
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001356 // check the intent that started this activity and pop up the voicemail
1357 // dialog if we've been asked to.
1358 // If we have at least one non default VM provider registered then bring up
1359 // the selection for the VM provider, otherwise bring up a VM number dialog.
1360 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001361 if (mShowVoicemailPreference) {
Tyler Gunnbaee2952014-09-10 16:01:02 -07001362 if (DBG) {
1363 log("ACTION_ADD_VOICEMAIL Intent is thrown. current VM data size: "
1364 + mVMProvidersData.size());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001365 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001366 if (mVMProvidersData.size() > 1) {
1367 simulatePreferenceClick(mVoicemailProviders);
1368 } else {
1369 onPreferenceChange(mVoicemailProviders, DEFAULT_VM_PROVIDER_KEY);
1370 mVoicemailProviders.setValue(DEFAULT_VM_PROVIDER_KEY);
1371 }
1372 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001373 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001374
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001375 updateVoiceNumberField();
1376 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001377
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001378 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
1379 mPhone.getContext());
1380 if (migrateVoicemailVibrationSettingsIfNeeded(prefs)) {
1381 mVoicemailNotificationVibrate.setChecked(prefs.getBoolean(
1382 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, false));
1383 }
1384
Andrew Lee312e8172014-10-23 17:01:36 -07001385 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1386 boolean currentValue =
1387 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1388 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1389 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001390 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1391 } else {
1392 prefSet.removePreference(mEnableVideoCalling);
1393 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001394 }
1395
1396 // Migrate settings from BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY to
1397 // BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, if the latter does not exist.
1398 // Returns true if migration was performed.
1399 public static boolean migrateVoicemailVibrationSettingsIfNeeded(SharedPreferences prefs) {
1400 if (!prefs.contains(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY)) {
1401 String vibrateWhen = prefs.getString(
1402 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY, VOICEMAIL_VIBRATION_NEVER);
1403 // If vibrateWhen is always, then voicemailVibrate should be True.
1404 // otherwise if vibrateWhen is "only in silent mode", or "never", then
1405 // voicemailVibrate = False.
1406 boolean voicemailVibrate = vibrateWhen.equals(VOICEMAIL_VIBRATION_ALWAYS);
1407 final SharedPreferences.Editor editor = prefs.edit();
1408 editor.putBoolean(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, voicemailVibrate);
1409 editor.commit();
1410 return true;
1411 }
1412 return false;
1413 }
1414
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001415 private void handleTTYChange(Preference preference, Object objValue) {
1416 int buttonTtyMode;
1417 buttonTtyMode = Integer.valueOf((String) objValue).intValue();
1418 int settingsTtyMode = android.provider.Settings.Secure.getInt(
1419 getContentResolver(),
Sailesh Nepalbf900542014-07-15 16:18:32 -07001420 android.provider.Settings.Secure.PREFERRED_TTY_MODE,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001421 TelecomManager.TTY_MODE_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001422 if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
1423 Integer.toString(buttonTtyMode));
1424
1425 if (buttonTtyMode != settingsTtyMode) {
1426 switch(buttonTtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001427 case TelecomManager.TTY_MODE_OFF:
1428 case TelecomManager.TTY_MODE_FULL:
1429 case TelecomManager.TTY_MODE_HCO:
1430 case TelecomManager.TTY_MODE_VCO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001431 android.provider.Settings.Secure.putInt(getContentResolver(),
1432 android.provider.Settings.Secure.PREFERRED_TTY_MODE, buttonTtyMode);
1433 break;
1434 default:
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001435 buttonTtyMode = TelecomManager.TTY_MODE_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001436 }
1437
1438 mButtonTTY.setValue(Integer.toString(buttonTtyMode));
1439 updatePreferredTtyModeSummary(buttonTtyMode);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001440 Intent ttyModeChanged = new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
1441 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001442 sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
1443 }
1444 }
1445
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001446 private void updatePreferredTtyModeSummary(int TtyMode) {
1447 String [] txts = getResources().getStringArray(R.array.tty_mode_entries);
1448 switch(TtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001449 case TelecomManager.TTY_MODE_OFF:
1450 case TelecomManager.TTY_MODE_HCO:
1451 case TelecomManager.TTY_MODE_VCO:
1452 case TelecomManager.TTY_MODE_FULL:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001453 mButtonTTY.setSummary(txts[TtyMode]);
1454 break;
1455 default:
1456 mButtonTTY.setEnabled(false);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001457 mButtonTTY.setSummary(txts[TelecomManager.TTY_MODE_OFF]);
Sailesh Nepalbf900542014-07-15 16:18:32 -07001458 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001459 }
1460 }
1461
1462 private static void log(String msg) {
1463 Log.d(LOG_TAG, msg);
1464 }
1465
1466 /**
1467 * Updates the look of the VM preference widgets based on current VM provider settings.
1468 * Note that the provider name is loaded form the found activity via loadLabel in
1469 * {@link #initVoiceMailProviders()} in order for it to be localizable.
1470 */
1471 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1472 final String key = currentProviderSetting;
1473 final VoiceMailProvider provider = mVMProvidersData.get(key);
1474
1475 /* This is the case when we are coming up on a freshly wiped phone and there is no
1476 persisted value for the list preference mVoicemailProviders.
1477 In this case we want to show the UI asking the user to select a voicemail provider as
1478 opposed to silently falling back to default one. */
1479 if (provider == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001480 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> null.");
1481
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001482 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1483 mVoicemailSettings.setEnabled(false);
1484 mVoicemailSettings.setIntent(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001485 mVoicemailNotificationVibrate.setEnabled(false);
1486 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001487 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> " + provider.toString());
1488
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001489 final String providerName = provider.name;
1490 mVoicemailProviders.setSummary(providerName);
1491 mVoicemailSettings.setEnabled(true);
1492 mVoicemailSettings.setIntent(provider.intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001493 mVoicemailNotificationVibrate.setEnabled(true);
1494 }
1495 }
1496
1497 /**
1498 * Enumerates existing VM providers and puts their data into the list and populates
1499 * the preference list objects with their names.
1500 * In case we are called with ACTION_ADD_VOICEMAIL intent the intent may have
1501 * an extra string called IGNORE_PROVIDER_EXTRA with "package.activityName" of the provider
1502 * which should be hidden when we bring up the list of possible VM providers to choose.
1503 */
1504 private void initVoiceMailProviders() {
1505 if (DBG) log("initVoiceMailProviders()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001506
1507 String providerToIgnore = null;
Andrew Leef1776d82014-11-04 14:45:02 -08001508 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)
1509 && getIntent().hasExtra(IGNORE_PROVIDER_EXTRA)) {
1510 providerToIgnore = getIntent().getStringExtra(IGNORE_PROVIDER_EXTRA);
1511 // Remove this provider from the list.
1512 if (!TextUtils.isEmpty(providerToIgnore)) {
1513 if (DBG) log("Found ACTION_ADD_VOICEMAIL. providerToIgnore= " + providerToIgnore);
Andrew Lee88b51e22014-10-29 15:48:51 -07001514 mVmProviderSettingsUtil.delete(providerToIgnore);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001515 }
1516 }
1517
1518 mVMProvidersData.clear();
1519
Andrew Leef1776d82014-11-04 14:45:02 -08001520 List<String> entries = new ArrayList<String>();
1521 List<String> values = new ArrayList<String>();
1522
1523 // Add default voicemail provider.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001524 final String myCarrier = getString(R.string.voicemail_default);
1525 mVMProvidersData.put(DEFAULT_VM_PROVIDER_KEY, new VoiceMailProvider(myCarrier, null));
Andrew Leef1776d82014-11-04 14:45:02 -08001526 entries.add(myCarrier);
1527 values.add(DEFAULT_VM_PROVIDER_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001528
Andrew Leef1776d82014-11-04 14:45:02 -08001529 // Add other voicemail providers.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001530 PackageManager pm = getPackageManager();
Andrew Leef1776d82014-11-04 14:45:02 -08001531 Intent intent = new Intent(ACTION_CONFIGURE_VOICEMAIL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001532 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001533 for (int i = 0; i < resolveInfos.size(); i++) {
1534 final ResolveInfo ri= resolveInfos.get(i);
1535 final ActivityInfo currentActivityInfo = ri.activityInfo;
Andrew Lee6214e2b2014-11-04 13:57:38 -08001536 final String key = currentActivityInfo.name;
Andrew Leef1776d82014-11-04 14:45:02 -08001537
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001538 if (key.equals(providerToIgnore)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001539 continue;
1540 }
Andrew Leef1776d82014-11-04 14:45:02 -08001541
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001542 if (DBG) log("Loading key: " + key);
1543 final String nameForDisplay = ri.loadLabel(pm).toString();
1544 Intent providerIntent = new Intent();
1545 providerIntent.setAction(ACTION_CONFIGURE_VOICEMAIL);
Andrew Leef1776d82014-11-04 14:45:02 -08001546 providerIntent.setClassName(currentActivityInfo.packageName, currentActivityInfo.name);
1547 VoiceMailProvider vmProvider = new VoiceMailProvider(nameForDisplay, providerIntent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001548
Andrew Leef1776d82014-11-04 14:45:02 -08001549 if (DBG) log("Store VoiceMailProvider. Key: " + key + " -> " + vmProvider.toString());
1550 mVMProvidersData.put(key, vmProvider);
1551 entries.add(vmProvider.name);
1552 values.add(key);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001553 }
1554
Andrew Leef1776d82014-11-04 14:45:02 -08001555 mVoicemailProviders.setEntries(entries.toArray(new String[0]));
1556 mVoicemailProviders.setEntryValues(values.toArray(new String[0]));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001557
Andrew Leef1776d82014-11-04 14:45:02 -08001558 // Remember the current Voicemail Provider key as a "previous" key. This will be used when
1559 // we fail to update Voicemail Provider, which requires rollback. We will update this when
1560 // the VM Provider setting is successfully updated.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001561 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1562 if (DBG) log("Set up the first mPreviousVMProviderKey: " + mPreviousVMProviderKey);
1563
1564 // Finally update the preference texts.
1565 updateVMPreferenceWidgets(mPreviousVMProviderKey);
1566 }
1567
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001568 /**
1569 * Simulates user clicking on a passed preference.
1570 * Usually needed when the preference is a dialog preference and we want to invoke
1571 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001572 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001573 */
1574 private void simulatePreferenceClick(Preference preference) {
1575 // Go through settings until we find our setting
1576 // and then simulate a click on it to bring up the dialog
1577 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1578 for (int idx = 0; idx < adapter.getCount(); idx++) {
1579 if (adapter.getItem(idx) == preference) {
1580 getPreferenceScreen().onItemClick(this.getListView(),
1581 null, idx, adapter.getItemId(idx));
1582 break;
1583 }
1584 }
1585 }
1586
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001587 private String getCurrentVoicemailProviderKey() {
1588 final String key = mVoicemailProviders.getValue();
1589 return (key != null) ? key : DEFAULT_VM_PROVIDER_KEY;
1590 }
1591
1592 @Override
1593 public boolean onOptionsItemSelected(MenuItem item) {
1594 final int itemId = item.getItemId();
1595 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001596 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001597 return true;
1598 }
1599 return super.onOptionsItemSelected(item);
1600 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001601 /**
1602 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1603 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1604 */
1605 public static void goUpToTopLevelSetting(Activity activity) {
1606 Intent intent = new Intent(activity, CallFeaturesSetting.class);
1607 intent.setAction(Intent.ACTION_MAIN);
1608 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1609 activity.startActivity(intent);
1610 activity.finish();
1611 }
1612}