blob: d56a274b8111c951a2d3dbab53be91094aa4fc83 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
19import android.app.ActionBar;
20import android.app.Activity;
Evan Charlton1c696832014-04-15 14:24:23 -070021import android.app.ActivityOptions;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.AlertDialog;
23import android.app.Dialog;
24import android.app.ProgressDialog;
25import android.content.ContentResolver;
26import android.content.Context;
27import android.content.DialogInterface;
28import android.content.Intent;
29import android.content.SharedPreferences;
30import android.content.SharedPreferences.Editor;
31import android.content.pm.ActivityInfo;
32import android.content.pm.PackageManager;
33import android.content.pm.ResolveInfo;
34import android.database.Cursor;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.media.AudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.os.AsyncResult;
37import android.os.Bundle;
38import android.os.Handler;
39import android.os.Message;
40import android.os.UserHandle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.preference.CheckBoxPreference;
42import android.preference.ListPreference;
43import android.preference.Preference;
44import android.preference.PreferenceActivity;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.preference.PreferenceManager;
46import android.preference.PreferenceScreen;
47import android.provider.ContactsContract.CommonDataKinds;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070049import android.telecom.PhoneAccountHandle;
50import android.telecom.TelecomManager;
Andrew Lee93c345f2014-10-27 15:25:07 -070051import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.text.TextUtils;
53import android.util.Log;
54import android.view.MenuItem;
55import android.view.WindowManager;
56import android.widget.ListAdapter;
57
Andrew Lee312e8172014-10-23 17:01:36 -070058import com.android.ims.ImsManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import com.android.internal.telephony.CallForwardInfo;
60import com.android.internal.telephony.CommandsInterface;
61import com.android.internal.telephony.Phone;
62import com.android.internal.telephony.PhoneConstants;
Andrew Lee2170a972014-08-13 18:13:01 -070063import com.android.phone.common.util.SettingsUtil;
Andrew Leedb2fe562014-09-03 15:40:43 -070064import com.android.phone.settings.AccountSelectionPreference;
Andrew Leeb490d732014-10-27 15:00:41 -070065import com.android.phone.settings.VoicemailProviderSettings;
Andrew Lee88b51e22014-10-29 15:48:51 -070066import com.android.phone.settings.VoicemailProviderSettingsUtil;
Sailesh Nepal788959e2014-07-08 23:36:40 -070067import com.android.services.telephony.sip.SipUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068
Andrew Lee2170a972014-08-13 18:13:01 -070069import java.lang.String;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import java.util.Collection;
71import java.util.HashMap;
72import java.util.HashSet;
73import java.util.Iterator;
74import java.util.List;
75import java.util.Map;
76
77/**
78 * Top level "Call settings" UI; see res/xml/call_feature_setting.xml
79 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070080 * This preference screen is the root of the "Call settings" hierarchy available from the Phone
81 * app; the settings here let you control various features related to phone calls (including
82 * voicemail settings, the "Respond via SMS" feature, and others.) It's used only on
83 * voice-capable phone devices.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084 *
85 * Note that this activity is part of the package com.android.phone, even
86 * though you reach it from the "Phone" app (i.e. DialtactsActivity) which
87 * is from the package com.android.contacts.
88 *
89 * For the "Mobile network settings" screen under the main Settings app,
90 * See {@link MobileNetworkSettings}.
91 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070092 * TODO: Settings should be split into PreferenceFragments where possible (ie. voicemail).
Andrew Lee2170a972014-08-13 18:13:01 -070093 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094 * @see com.android.phone.MobileNetworkSettings
95 */
96public class CallFeaturesSetting extends PreferenceActivity
97 implements DialogInterface.OnClickListener,
Andrew Lee2170a972014-08-13 18:13:01 -070098 Preference.OnPreferenceChangeListener,
Andrew Lee2170a972014-08-13 18:13:01 -070099 EditPhoneNumberPreference.OnDialogClosedListener,
Andrew Leece8ae2a2014-09-10 10:41:48 -0700100 EditPhoneNumberPreference.GetDefaultNumberListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700101 private static final String LOG_TAG = "CallFeaturesSetting";
102 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
Andrew Lee77527ac2014-10-21 16:57:39 -0700103 // STOPSHIP if true. Flag to override behavior default behavior to hide VT setting.
104 private static final boolean ENABLE_VT_FLAG = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700105
106 /**
107 * Intent action to bring up Voicemail Provider settings.
108 *
109 * @see #IGNORE_PROVIDER_EXTRA
110 */
111 public static final String ACTION_ADD_VOICEMAIL =
112 "com.android.phone.CallFeaturesSetting.ADD_VOICEMAIL";
113 // intent action sent by this activity to a voice mail provider
114 // to trigger its configuration UI
115 public static final String ACTION_CONFIGURE_VOICEMAIL =
116 "com.android.phone.CallFeaturesSetting.CONFIGURE_VOICEMAIL";
Andrew Leee3c15212014-10-28 13:12:55 -0700117 // Extra on intent to Call Settings containing the id of the subscription to modify.
118 public static final String SUB_ID_EXTRA =
119 "com.android.phone.CallFeaturesSetting.SubscriptionId";
120 // Extra on intent to Call Settings containing the label of the subscription to modify.
121 public static final String SUB_LABEL_EXTRA =
122 "com.android.phone.CallFeaturesSetting.SubscriptionLabel";
123
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124 // Extra put in the return from VM provider config containing voicemail number to set
125 public static final String VM_NUMBER_EXTRA = "com.android.phone.VoicemailNumber";
126 // Extra put in the return from VM provider config containing call forwarding number to set
127 public static final String FWD_NUMBER_EXTRA = "com.android.phone.ForwardingNumber";
128 // Extra put in the return from VM provider config containing call forwarding number to set
129 public static final String FWD_NUMBER_TIME_EXTRA = "com.android.phone.ForwardingNumberTime";
130 // If the VM provider returns non null value in this extra we will force the user to
131 // choose another VM provider
132 public static final String SIGNOUT_EXTRA = "com.android.phone.Signout";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700133
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700134 // Key identifying the default vocie mail provider
135 public static final String DEFAULT_VM_PROVIDER_KEY = "";
136
137 /**
138 * String Extra put into ACTION_ADD_VOICEMAIL call to indicate which provider should be hidden
139 * in the list of providers presented to the user. This allows a provider which is being
140 * disabled (e.g. GV user logging out) to force the user to pick some other provider.
141 */
142 public static final String IGNORE_PROVIDER_EXTRA = "com.android.phone.ProviderToIgnore";
143
144 // string constants
145 private static final String NUM_PROJECTION[] = {CommonDataKinds.Phone.NUMBER};
146
147 // String keys for preference lookup
148 // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
Andrew Lee97708a42014-09-25 12:39:07 -0700149 private static final String VOICEMAIL_SETTING_SCREEN_PREF_KEY = "button_voicemail_category_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150 private static final String BUTTON_VOICEMAIL_KEY = "button_voicemail_key";
151 private static final String BUTTON_VOICEMAIL_PROVIDER_KEY = "button_voicemail_provider_key";
152 private static final String BUTTON_VOICEMAIL_SETTING_KEY = "button_voicemail_setting_key";
153 // New preference key for voicemail notification vibration
154 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY =
155 "button_voicemail_notification_vibrate_key";
156 // Old preference key for voicemail notification vibration. Used for migration to the new
157 // preference key only.
158 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY =
159 "button_voicemail_notification_vibrate_when_key";
160 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_RINGTONE_KEY =
161 "button_voicemail_notification_ringtone_key";
162 private static final String BUTTON_FDN_KEY = "button_fdn_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700163
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700164 private static final String BUTTON_DTMF_KEY = "button_dtmf_settings";
165 private static final String BUTTON_RETRY_KEY = "button_auto_retry_key";
166 private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
167 private static final String BUTTON_HAC_KEY = "button_hac_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168
169 private static final String BUTTON_GSM_UMTS_OPTIONS = "button_gsm_more_expand_key";
170 private static final String BUTTON_CDMA_OPTIONS = "button_cdma_more_expand_key";
171
Andrew Leedb2fe562014-09-03 15:40:43 -0700172 private static final String DEFAULT_OUTGOING_ACCOUNT_KEY = "default_outgoing_account";
Andrew Leece8ae2a2014-09-10 10:41:48 -0700173 private static final String PHONE_ACCOUNT_SETTINGS_KEY =
174 "phone_account_settings_preference_screen";
Andrew Leedb2fe562014-09-03 15:40:43 -0700175
Andrew Leedf14ead2014-10-17 14:22:52 -0700176 private static final String ENABLE_VIDEO_CALLING_KEY = "button_enable_video_calling";
177
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178 /** Event for Async voicemail change call */
179 private static final int EVENT_VOICEMAIL_CHANGED = 500;
180 private static final int EVENT_FORWARDING_CHANGED = 501;
181 private static final int EVENT_FORWARDING_GET_COMPLETED = 502;
182
Andrew Lee2170a972014-08-13 18:13:01 -0700183 private static final int MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY = 1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700184
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185 public static final String HAC_KEY = "HACSetting";
186 public static final String HAC_VAL_ON = "ON";
187 public static final String HAC_VAL_OFF = "OFF";
188
189 /** Handle to voicemail pref */
190 private static final int VOICEMAIL_PREF_ID = 1;
191 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
192
193 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700194 private AudioManager mAudioManager;
Andrew Lee88b51e22014-10-29 15:48:51 -0700195 private VoicemailProviderSettingsUtil mVmProviderSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700196
197 private static final int VM_NOCHANGE_ERROR = 400;
198 private static final int VM_RESPONSE_ERROR = 500;
199 private static final int FW_SET_RESPONSE_ERROR = 501;
200 private static final int FW_GET_RESPONSE_ERROR = 502;
201
202
203 // dialog identifiers for voicemail
204 private static final int VOICEMAIL_DIALOG_CONFIRM = 600;
205 private static final int VOICEMAIL_FWD_SAVING_DIALOG = 601;
206 private static final int VOICEMAIL_FWD_READING_DIALOG = 602;
207 private static final int VOICEMAIL_REVERTING_DIALOG = 603;
208
209 // status message sent back from handlers
210 private static final int MSG_OK = 100;
211
212 // special statuses for voicemail controls.
213 private static final int MSG_VM_EXCEPTION = 400;
214 private static final int MSG_FW_SET_EXCEPTION = 401;
215 private static final int MSG_FW_GET_EXCEPTION = 402;
216 private static final int MSG_VM_OK = 600;
217 private static final int MSG_VM_NOCHANGE = 700;
218
Andrew Leee438b312014-10-29 16:59:15 -0700219 /**
220 * @see CallForwardInfo#status
221 */
222 private static final int CALL_FORWARD_INFO_INACTIVE_STATUS = 0;
223
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700224 // voicemail notification vibration string constants
225 private static final String VOICEMAIL_VIBRATION_ALWAYS = "always";
226 private static final String VOICEMAIL_VIBRATION_NEVER = "never";
227
228 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
229
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700230 /** Whether dialpad plays DTMF tone or not. */
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700231 private CheckBoxPreference mButtonAutoRetry;
232 private CheckBoxPreference mButtonHAC;
233 private ListPreference mButtonDTMF;
234 private ListPreference mButtonTTY;
Andrew Leece8ae2a2014-09-10 10:41:48 -0700235 private Preference mPhoneAccountSettingsPreference;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700236 private ListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700237 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700238 private PreferenceScreen mVoicemailSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700239 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700240 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700241
242 private class VoiceMailProvider {
243 public VoiceMailProvider(String name, Intent intent) {
244 this.name = name;
245 this.intent = intent;
246 }
247 public String name;
248 public Intent intent;
249 }
250
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700251 /**
252 * Results of reading forwarding settings
253 */
254 private CallForwardInfo[] mForwardingReadResults = null;
255
256 /**
257 * Result of forwarding number change.
258 * Keys are reasons (eg. unconditional forwarding).
259 */
260 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
261
262 /**
263 * Expected CF read result types.
264 * This set keeps track of the CF types for which we've issued change
265 * commands so we can tell when we've received all of the responses.
266 */
267 private Collection<Integer> mExpectedChangeResultReasons = null;
268
269 /**
270 * Result of vm number change
271 */
272 private AsyncResult mVoicemailChangeResult = null;
273
274 /**
275 * Previous VM provider setting so we can return to it in case of failure.
276 */
277 private String mPreviousVMProviderKey = null;
278
279 /**
280 * Id of the dialog being currently shown.
281 */
282 private int mCurrentDialogId = 0;
283
284 /**
285 * Flag indicating that we are invoking settings for the voicemail provider programmatically
286 * due to vm provider change.
287 */
288 private boolean mVMProviderSettingsForced = false;
289
290 /**
291 * Flag indicating that we are making changes to vm or fwd numbers
292 * due to vm provider change.
293 */
294 private boolean mChangingVMorFwdDueToProviderChange = false;
295
296 /**
297 * True if we are in the process of vm & fwd number change and vm has already been changed.
298 * This is used to decide what to do in case of rollback.
299 */
300 private boolean mVMChangeCompletedSuccessfully = false;
301
302 /**
303 * True if we had full or partial failure setting forwarding numbers and so need to roll them
304 * back.
305 */
306 private boolean mFwdChangesRequireRollback = false;
307
308 /**
309 * Id of error msg to display to user once we are done reverting the VM provider to the previous
310 * one.
311 */
312 private int mVMOrFwdSetError = 0;
313
314 /**
315 * Data about discovered voice mail settings providers.
316 * Is populated by querying which activities can handle ACTION_CONFIGURE_VOICEMAIL.
317 * They key in this map is package name + activity name.
318 * We always add an entry for the default provider with a key of empty
319 * string and intent value of null.
320 * @see #initVoiceMailProviders()
321 */
322 private final Map<String, VoiceMailProvider> mVMProvidersData =
323 new HashMap<String, VoiceMailProvider>();
324
325 /** string to hold old voicemail number as it is being updated. */
326 private String mOldVmNumber;
327
328 // New call forwarding settings and vm number we will be setting
329 // Need to save these since before we get to saving we need to asynchronously
330 // query the existing forwarding settings.
331 private CallForwardInfo[] mNewFwdSettings;
332 private String mNewVMNumber;
333
334 private boolean mForeground;
335
336 @Override
337 public void onPause() {
338 super.onPause();
339 mForeground = false;
340 }
341
342 /**
343 * We have to pull current settings from the network for all kinds of
344 * voicemail providers so we can tell whether we have to update them,
345 * so use this bit to keep track of whether we're reading settings for the
346 * default provider and should therefore save them out when done.
347 */
348 private boolean mReadingSettingsForDefaultProvider = false;
349
Tyler Gunnbaee2952014-09-10 16:01:02 -0700350 /**
351 * Used to indicate that the voicemail preference should be shown.
352 */
353 private boolean mShowVoicemailPreference = false;
354
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700355 /*
356 * Click Listeners, handle click based on objects attached to UI.
357 */
358
359 // Click listener for all toggle events
360 @Override
361 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
362 if (preference == mSubMenuVoicemailSettings) {
363 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700364 } else if (preference == mButtonDTMF) {
365 return true;
366 } else if (preference == mButtonTTY) {
367 return true;
368 } else if (preference == mButtonAutoRetry) {
369 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
370 android.provider.Settings.Global.CALL_AUTO_RETRY,
371 mButtonAutoRetry.isChecked() ? 1 : 0);
372 return true;
373 } else if (preference == mButtonHAC) {
374 int hac = mButtonHAC.isChecked() ? 1 : 0;
375 // Update HAC value in Settings database
376 Settings.System.putInt(mPhone.getContext().getContentResolver(),
377 Settings.System.HEARING_AID, hac);
378
379 // Update HAC Value in AudioManager
380 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
381 return true;
382 } else if (preference == mVoicemailSettings) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700383 final Dialog dialog = mVoicemailSettings.getDialog();
384 if (dialog != null) {
385 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
386 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700387 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
388 if (preference.getIntent() != null) {
389 if (DBG) {
390 log("onPreferenceTreeClick: Invoking cfg intent "
391 + preference.getIntent().getPackage());
392 }
393
394 // onActivityResult() will be responsible for resetting some of variables.
395 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
396 return true;
397 } else {
398 if (DBG) {
399 log("onPreferenceTreeClick:"
400 + " No Intent is available. Use default behavior defined in xml.");
401 }
402
403 // There's no onActivityResult(), so we need to take care of some of variables
404 // which should be reset here.
405 mPreviousVMProviderKey = DEFAULT_VM_PROVIDER_KEY;
406 mVMProviderSettingsForced = false;
407
408 // This should let the preference use default behavior in the xml.
409 return false;
410 }
Andrew Lee97708a42014-09-25 12:39:07 -0700411 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700412 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
413 if (dialog != null) {
414 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
415 }
Andrew Lee97708a42014-09-25 12:39:07 -0700416 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700417 }
418 return false;
419 }
420
421 /**
422 * Implemented to support onPreferenceChangeListener to look for preference
423 * changes.
424 *
425 * @param preference is the preference to be changed
426 * @param objValue should be the value of the selection, NOT its localized
427 * display value.
428 */
429 @Override
430 public boolean onPreferenceChange(Preference preference, Object objValue) {
431 if (DBG) {
Andrew Leedf14ead2014-10-17 14:22:52 -0700432 log("onPreferenceChange(). preference: \"" + preference + "\""
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700433 + ", value: \"" + objValue + "\"");
434 }
Andrew Lee2170a972014-08-13 18:13:01 -0700435
436 if (preference == mButtonDTMF) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700437 int index = mButtonDTMF.findIndexOfValue((String) objValue);
438 Settings.System.putInt(mPhone.getContext().getContentResolver(),
439 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
440 } else if (preference == mButtonTTY) {
441 handleTTYChange(preference, objValue);
442 } else if (preference == mVoicemailProviders) {
443 final String newProviderKey = (String) objValue;
444 if (DBG) {
445 log("Voicemail Provider changes from \"" + mPreviousVMProviderKey
446 + "\" to \"" + newProviderKey + "\".");
447 }
448 // If previous provider key and the new one is same, we don't need to handle it.
449 if (mPreviousVMProviderKey.equals(newProviderKey)) {
450 if (DBG) log("No change is made toward VM provider setting.");
451 return true;
452 }
453 updateVMPreferenceWidgets(newProviderKey);
454
Andrew Leeb490d732014-10-27 15:00:41 -0700455 final VoicemailProviderSettings newProviderSettings =
Andrew Lee88b51e22014-10-29 15:48:51 -0700456 mVmProviderSettingsUtil.load(newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700457
Andrew Lee88b51e22014-10-29 15:48:51 -0700458 // If the user switches to a voice mail provider and we have numbers stored for it we
459 // will automatically change the phone's voice mail and forwarding number to the stored
460 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700461 if (newProviderSettings == null) {
462 // Force the user into a configuration of the chosen provider
463 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
464 mVMProviderSettingsForced = true;
465 simulatePreferenceClick(mVoicemailSettings);
466 } else {
467 if (DBG) log("Saved preferences found - switching to them");
468 // Set this flag so if we get a failure we revert to previous provider
469 mChangingVMorFwdDueToProviderChange = true;
470 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
471 }
Andrew Leedf14ead2014-10-17 14:22:52 -0700472 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700473 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
474 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
475 } else {
476 AlertDialog.Builder builder = new AlertDialog.Builder(this);
477 DialogInterface.OnClickListener networkSettingsClickListener =
478 new Dialog.OnClickListener() {
479 @Override
480 public void onClick(DialogInterface dialog, int which) {
481 startActivity(new Intent(mPhone.getContext(),
482 com.android.phone.MobileNetworkSettings.class));
483 }
484 };
485 builder.setMessage(getResources().getString(
486 R.string.enable_video_calling_dialog_msg))
487 .setNeutralButton(getResources().getString(
488 R.string.enable_video_calling_dialog_settings),
489 networkSettingsClickListener)
490 .setPositiveButton(android.R.string.ok, null)
491 .show();
492 return false;
493 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700494 }
495 // always let the preference setting proceed.
496 return true;
497 }
498
499 @Override
500 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
501 if (DBG) log("onPreferenceClick: request preference click on dialog close: " +
502 buttonClicked);
503 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
504 return;
505 }
506
507 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700508 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
509 mSubMenuVoicemailSettings.getPhoneNumber(),
510 VoicemailProviderSettings.NO_FORWARDING);
511 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700512 }
513 }
514
515 /**
516 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
517 * This method set the default values for the various
518 * EditPhoneNumberPreference dialogs.
519 */
520 @Override
521 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
522 if (preference == mSubMenuVoicemailSettings) {
523 // update the voicemail number field, which takes care of the
524 // mSubMenuVoicemailSettings itself, so we should return null.
525 if (DBG) log("updating default for voicemail dialog");
526 updateVoiceNumberField();
527 return null;
528 }
529
530 String vmDisplay = mPhone.getVoiceMailNumber();
531 if (TextUtils.isEmpty(vmDisplay)) {
532 // if there is no voicemail number, we just return null to
533 // indicate no contribution.
534 return null;
535 }
536
537 // Return the voicemail number prepended with "VM: "
538 if (DBG) log("updating default for call forwarding dialogs");
539 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
540 }
541
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700542 private void switchToPreviousVoicemailProvider() {
543 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
544 if (mPreviousVMProviderKey != null) {
545 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
546 // we have to revert with carrier
547 if (DBG) {
548 log("Needs to rollback."
549 + " mVMChangeCompletedSuccessfully=" + mVMChangeCompletedSuccessfully
550 + ", mFwdChangesRequireRollback=" + mFwdChangesRequireRollback);
551 }
552
553 showDialogIfForeground(VOICEMAIL_REVERTING_DIALOG);
Andrew Leeb490d732014-10-27 15:00:41 -0700554 final VoicemailProviderSettings prevSettings =
Andrew Lee88b51e22014-10-29 15:48:51 -0700555 mVmProviderSettingsUtil.load(mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700556 if (prevSettings == null) {
557 // prevSettings never becomes null since it should be already loaded!
Andrew Leeb490d732014-10-27 15:00:41 -0700558 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700559 + mPreviousVMProviderKey + "\" becomes null, which is unexpected.");
560 if (DBG) {
561 Log.e(LOG_TAG,
562 "mVMChangeCompletedSuccessfully: " + mVMChangeCompletedSuccessfully
563 + ", mFwdChangesRequireRollback: " + mFwdChangesRequireRollback);
564 }
565 }
566 if (mVMChangeCompletedSuccessfully) {
Andrew Leeb490d732014-10-27 15:00:41 -0700567 mNewVMNumber = prevSettings.getVoicemailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700568 Log.i(LOG_TAG, "VM change is already completed successfully."
569 + "Have to revert VM back to " + mNewVMNumber + " again.");
570 mPhone.setVoiceMailNumber(
571 mPhone.getVoiceMailAlphaTag().toString(),
572 mNewVMNumber,
573 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
574 }
575 if (mFwdChangesRequireRollback) {
576 Log.i(LOG_TAG, "Requested to rollback Fwd changes.");
Andrew Leeb490d732014-10-27 15:00:41 -0700577 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700578 if (prevFwdSettings != null) {
579 Map<Integer, AsyncResult> results =
580 mForwardingChangeResults;
581 resetForwardingChangeState();
582 for (int i = 0; i < prevFwdSettings.length; i++) {
583 CallForwardInfo fi = prevFwdSettings[i];
584 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
585 // Only revert the settings for which the update
586 // succeeded
587 AsyncResult result = results.get(fi.reason);
588 if (result != null && result.exception == null) {
589 mExpectedChangeResultReasons.add(fi.reason);
590 mPhone.setCallForwardingOption(
591 (fi.status == 1 ?
592 CommandsInterface.CF_ACTION_REGISTRATION :
593 CommandsInterface.CF_ACTION_DISABLE),
594 fi.reason,
595 fi.number,
596 fi.timeSeconds,
597 mRevertOptionComplete.obtainMessage(
598 EVENT_FORWARDING_CHANGED, i, 0));
599 }
600 }
601 }
602 }
603 } else {
604 if (DBG) log("No need to revert");
605 onRevertDone();
606 }
607 }
608 }
609
610 private void onRevertDone() {
611 if (DBG) log("Flipping provider key back to " + mPreviousVMProviderKey);
612 mVoicemailProviders.setValue(mPreviousVMProviderKey);
613 updateVMPreferenceWidgets(mPreviousVMProviderKey);
614 updateVoiceNumberField();
615 if (mVMOrFwdSetError != 0) {
616 showVMDialog(mVMOrFwdSetError);
617 mVMOrFwdSetError = 0;
618 }
619 }
620
621 @Override
622 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
623 if (DBG) {
624 log("onActivityResult: requestCode: " + requestCode
625 + ", resultCode: " + resultCode
626 + ", data: " + data);
627 }
628 // there are cases where the contact picker may end up sending us more than one
629 // request. We want to ignore the request if we're not in the correct state.
630 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
631 boolean failure = false;
632
633 // No matter how the processing of result goes lets clear the flag
634 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
635 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
636 mVMProviderSettingsForced = false;
637
638 String vmNum = null;
639 if (resultCode != RESULT_OK) {
640 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
641 failure = true;
642 } else {
643 if (data == null) {
644 if (DBG) log("onActivityResult: vm provider cfg result has no data");
645 failure = true;
646 } else {
647 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
648 if (DBG) log("Provider requested signout");
649 if (isVMProviderSettingsForced) {
650 if (DBG) log("Going back to previous provider on signout");
651 switchToPreviousVoicemailProvider();
652 } else {
653 final String victim = getCurrentVoicemailProviderKey();
654 if (DBG) log("Relaunching activity and ignoring " + victim);
655 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
656 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
657 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
658 this.startActivity(i);
659 }
660 return;
661 }
662 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
663 if (vmNum == null || vmNum.length() == 0) {
664 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
665 failure = true;
666 }
667 }
668 }
669 if (failure) {
670 if (DBG) log("Failure in return from voicemail provider");
671 if (isVMProviderSettingsForced) {
672 switchToPreviousVoicemailProvider();
673 } else {
674 if (DBG) log("Not switching back the provider since this is not forced config");
675 }
676 return;
677 }
678 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
679 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
680
Santos Cordonda120f42014-08-06 04:44:34 -0700681 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700682 // send it to the provider when it's config is invoked so it can use this as default
683 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
684
685 if (DBG) log("onActivityResult: vm provider cfg result " +
686 (fwdNum != null ? "has" : " does not have") + " forwarding number");
687 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700688 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700689 return;
690 }
691
692 if (requestCode == VOICEMAIL_PREF_ID) {
693 if (resultCode != RESULT_OK) {
694 if (DBG) log("onActivityResult: contact picker result not OK.");
695 return;
696 }
697
698 Cursor cursor = null;
699 try {
700 cursor = getContentResolver().query(data.getData(),
701 NUM_PROJECTION, null, null, null);
702 if ((cursor == null) || (!cursor.moveToFirst())) {
703 if (DBG) log("onActivityResult: bad contact data, no results found.");
704 return;
705 }
706 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
707 return;
708 } finally {
709 if (cursor != null) {
710 cursor.close();
711 }
712 }
713 }
714
715 super.onActivityResult(requestCode, resultCode, data);
716 }
717
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700718 /**
719 * Wrapper around showDialog() that will silently do nothing if we're
720 * not in the foreground.
721 *
722 * This is useful here because most of the dialogs we display from
723 * this class are triggered by asynchronous events (like
724 * success/failure messages from the telephony layer) and it's
725 * possible for those events to come in even after the user has gone
726 * to a different screen.
727 */
728 // TODO: this is too brittle: it's still easy to accidentally add new
729 // code here that calls showDialog() directly (which will result in a
730 // WindowManager$BadTokenException if called after the activity has
731 // been stopped.)
732 //
733 // It would be cleaner to do the "if (mForeground)" check in one
734 // central place, maybe by using a single Handler for all asynchronous
735 // events (and have *that* discard events if we're not in the
736 // foreground.)
737 //
738 // Unfortunately it's not that simple, since we sometimes need to do
739 // actual work to handle these events whether or not we're in the
740 // foreground (see the Handler code in mSetOptionComplete for
741 // example.)
742 private void showDialogIfForeground(int id) {
743 if (mForeground) {
744 showDialog(id);
745 }
746 }
747
748 private void dismissDialogSafely(int id) {
749 try {
750 dismissDialog(id);
751 } catch (IllegalArgumentException e) {
752 // This is expected in the case where we were in the background
753 // at the time we would normally have shown the dialog, so we didn't
754 // show it.
755 }
756 }
757
Andrew Leeb490d732014-10-27 15:00:41 -0700758 private void saveVoiceMailAndForwardingNumber(
759 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700760 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700761 mNewVMNumber = newSettings.getVoicemailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700762 // empty vm number == clearing the vm number ?
763 if (mNewVMNumber == null) {
764 mNewVMNumber = "";
765 }
766
Andrew Leeb490d732014-10-27 15:00:41 -0700767 mNewFwdSettings = newSettings.getForwardingSettings();
768 if (DBG) log("newFwdNumber "
769 + String.valueOf((mNewFwdSettings != null ? mNewFwdSettings.length : 0))
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700770 + " settings");
771
772 // No fwd settings on CDMA
773 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
774 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700775 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700776 }
777
Andrew Leee3c15212014-10-28 13:12:55 -0700778 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700779 if (mNewVMNumber.equals(mOldVmNumber)
780 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700781 showVMDialog(MSG_VM_NOCHANGE);
782 return;
783 }
784
Andrew Lee88b51e22014-10-29 15:48:51 -0700785 mVmProviderSettingsUtil.save(key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700786 mVMChangeCompletedSuccessfully = false;
787 mFwdChangesRequireRollback = false;
788 mVMOrFwdSetError = 0;
789 if (!key.equals(mPreviousVMProviderKey)) {
790 mReadingSettingsForDefaultProvider =
791 mPreviousVMProviderKey.equals(DEFAULT_VM_PROVIDER_KEY);
792 if (DBG) log("Reading current forwarding settings");
Andrew Leeb490d732014-10-27 15:00:41 -0700793 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
794 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
795 for (int i = 0; i < mForwardingReadResults.length; i++) {
796 mPhone.getCallForwardingOption(
797 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700798 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
799 }
800 showDialogIfForeground(VOICEMAIL_FWD_READING_DIALOG);
801 } else {
802 saveVoiceMailAndForwardingNumberStage2();
803 }
804 }
805
806 private final Handler mGetOptionComplete = new Handler() {
807 @Override
808 public void handleMessage(Message msg) {
809 AsyncResult result = (AsyncResult) msg.obj;
810 switch (msg.what) {
811 case EVENT_FORWARDING_GET_COMPLETED:
812 handleForwardingSettingsReadResult(result, msg.arg1);
813 break;
814 }
815 }
816 };
817
818 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
819 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
820 Throwable error = null;
821 if (ar.exception != null) {
822 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" +
823 ar.exception.getMessage());
824 error = ar.exception;
825 }
826 if (ar.userObj instanceof Throwable) {
827 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" +
828 ((Throwable)ar.userObj).getMessage());
829 error = (Throwable)ar.userObj;
830 }
831
832 // We may have already gotten an error and decided to ignore the other results.
833 if (mForwardingReadResults == null) {
834 if (DBG) Log.d(LOG_TAG, "ignoring fwd reading result: " + idx);
835 return;
836 }
837
838 // In case of error ignore other results, show an error dialog
839 if (error != null) {
840 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
841 mForwardingReadResults = null;
842 dismissDialogSafely(VOICEMAIL_FWD_READING_DIALOG);
843 showVMDialog(MSG_FW_GET_EXCEPTION);
844 return;
845 }
846
847 // Get the forwarding info
848 final CallForwardInfo cfInfoArray[] = (CallForwardInfo[]) ar.result;
849 CallForwardInfo fi = null;
850 for (int i = 0 ; i < cfInfoArray.length; i++) {
851 if ((cfInfoArray[i].serviceClass & CommandsInterface.SERVICE_CLASS_VOICE) != 0) {
852 fi = cfInfoArray[i];
853 break;
854 }
855 }
856 if (fi == null) {
857
858 // In case we go nothing it means we need this reason disabled
859 // so create a CallForwardInfo for capturing this
860 if (DBG) Log.d(LOG_TAG, "Creating default info for " + idx);
861 fi = new CallForwardInfo();
862 fi.status = 0;
Andrew Leeb490d732014-10-27 15:00:41 -0700863 fi.reason = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx];
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700864 fi.serviceClass = CommandsInterface.SERVICE_CLASS_VOICE;
865 } else {
866 // if there is not a forwarding number, ensure the entry is set to "not active."
867 if (fi.number == null || fi.number.length() == 0) {
868 fi.status = 0;
869 }
870
871 if (DBG) Log.d(LOG_TAG, "Got " + fi.toString() + " for " + idx);
872 }
873 mForwardingReadResults[idx] = fi;
874
875 // Check if we got all the results already
876 boolean done = true;
877 for (int i = 0; i < mForwardingReadResults.length; i++) {
878 if (mForwardingReadResults[i] == null) {
879 done = false;
880 break;
881 }
882 }
883 if (done) {
884 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
885 dismissDialogSafely(VOICEMAIL_FWD_READING_DIALOG);
886 if (mReadingSettingsForDefaultProvider) {
Andrew Lee88b51e22014-10-29 15:48:51 -0700887 mVmProviderSettingsUtil.save(DEFAULT_VM_PROVIDER_KEY,
888 new VoicemailProviderSettings(this.mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700889 mReadingSettingsForDefaultProvider = false;
890 }
891 saveVoiceMailAndForwardingNumberStage2();
892 } else {
893 if (DBG) Log.d(LOG_TAG, "Not done receiving fwd info");
894 }
895 }
896
897 private CallForwardInfo infoForReason(CallForwardInfo[] infos, int reason) {
898 CallForwardInfo result = null;
899 if (null != infos) {
900 for (CallForwardInfo info : infos) {
901 if (info.reason == reason) {
902 result = info;
903 break;
904 }
905 }
906 }
907 return result;
908 }
909
Andrew Leee438b312014-10-29 16:59:15 -0700910 private boolean isUpdateRequired(CallForwardInfo oldInfo, CallForwardInfo newInfo) {
911 if (oldInfo == null) {
912 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700913 }
Andrew Leee438b312014-10-29 16:59:15 -0700914
915 // If we're disabling a type of forwarding, don't make any change if it's already disabled.
916 if (newInfo.status == CALL_FORWARD_INFO_INACTIVE_STATUS
917 && oldInfo.status == CALL_FORWARD_INFO_INACTIVE_STATUS) {
918 return false;
919 }
920
921 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700922 }
923
924 private void resetForwardingChangeState() {
925 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
926 mExpectedChangeResultReasons = new HashSet<Integer>();
927 }
928
929 // Called after we are done saving the previous forwarding settings if
930 // we needed.
931 private void saveVoiceMailAndForwardingNumberStage2() {
932 mForwardingChangeResults = null;
933 mVoicemailChangeResult = null;
Andrew Leeb490d732014-10-27 15:00:41 -0700934 if (mNewFwdSettings != VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700935 resetForwardingChangeState();
936 for (int i = 0; i < mNewFwdSettings.length; i++) {
937 CallForwardInfo fi = mNewFwdSettings[i];
938
939 final boolean doUpdate = isUpdateRequired(infoForReason(
940 mForwardingReadResults, fi.reason), fi);
941
942 if (doUpdate) {
943 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
944 mExpectedChangeResultReasons.add(i);
945
946 mPhone.setCallForwardingOption(
947 fi.status == 1 ?
948 CommandsInterface.CF_ACTION_REGISTRATION :
949 CommandsInterface.CF_ACTION_DISABLE,
950 fi.reason,
951 fi.number,
952 fi.timeSeconds,
953 mSetOptionComplete.obtainMessage(
954 EVENT_FORWARDING_CHANGED, fi.reason, 0));
955 }
956 }
957 showDialogIfForeground(VOICEMAIL_FWD_SAVING_DIALOG);
958 } else {
959 if (DBG) log("Not touching fwd #");
960 setVMNumberWithCarrier();
961 }
962 }
963
964 private void setVMNumberWithCarrier() {
965 if (DBG) log("save voicemail #: " + mNewVMNumber);
966 mPhone.setVoiceMailNumber(
967 mPhone.getVoiceMailAlphaTag().toString(),
968 mNewVMNumber,
969 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
970 }
971
972 /**
973 * Callback to handle option update completions
974 */
975 private final Handler mSetOptionComplete = new Handler() {
976 @Override
977 public void handleMessage(Message msg) {
978 AsyncResult result = (AsyncResult) msg.obj;
979 boolean done = false;
980 switch (msg.what) {
981 case EVENT_VOICEMAIL_CHANGED:
982 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700983 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700984 done = true;
985 break;
986 case EVENT_FORWARDING_CHANGED:
987 mForwardingChangeResults.put(msg.arg1, result);
988 if (result.exception != null) {
989 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
990 result.exception.getMessage());
991 } else {
992 if (DBG) log("Success in setting fwd# " + msg.arg1);
993 }
Andrew Leee438b312014-10-29 16:59:15 -0700994 if (isForwardingCompleted()) {
995 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700996 if (DBG) log("Overall fwd changes completed ok, starting vm change");
997 setVMNumberWithCarrier();
998 } else {
999 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
1000 "Check if we need to try rollback for some settings.");
1001 mFwdChangesRequireRollback = false;
1002 Iterator<Map.Entry<Integer,AsyncResult>> it =
1003 mForwardingChangeResults.entrySet().iterator();
1004 while (it.hasNext()) {
1005 Map.Entry<Integer,AsyncResult> entry = it.next();
1006 if (entry.getValue().exception == null) {
1007 // If at least one succeeded we have to revert
1008 Log.i(LOG_TAG, "Rollback will be required");
1009 mFwdChangesRequireRollback = true;
1010 break;
1011 }
1012 }
1013 if (!mFwdChangesRequireRollback) {
1014 Log.i(LOG_TAG, "No rollback needed.");
1015 }
1016 done = true;
1017 }
1018 }
1019 break;
1020 default:
1021 // TODO: should never reach this, may want to throw exception
1022 }
1023 if (done) {
1024 if (DBG) log("All VM provider related changes done");
1025 if (mForwardingChangeResults != null) {
1026 dismissDialogSafely(VOICEMAIL_FWD_SAVING_DIALOG);
1027 }
Andrew Leee438b312014-10-29 16:59:15 -07001028 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001029 }
1030 }
1031 };
1032
1033 /**
1034 * Callback to handle option revert completions
1035 */
1036 private final Handler mRevertOptionComplete = new Handler() {
1037 @Override
1038 public void handleMessage(Message msg) {
1039 AsyncResult result = (AsyncResult) msg.obj;
1040 switch (msg.what) {
1041 case EVENT_VOICEMAIL_CHANGED:
1042 mVoicemailChangeResult = result;
1043 if (DBG) log("VM revert complete msg");
1044 break;
1045 case EVENT_FORWARDING_CHANGED:
1046 mForwardingChangeResults.put(msg.arg1, result);
1047 if (result.exception != null) {
1048 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
1049 result.exception.getMessage());
1050 } else {
1051 if (DBG) log("Success in reverting fwd# " + msg.arg1);
1052 }
1053 if (DBG) log("FWD revert complete msg ");
1054 break;
1055 default:
1056 // TODO: should never reach this, may want to throw exception
1057 }
1058 final boolean done =
1059 (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null) &&
Andrew Leee438b312014-10-29 16:59:15 -07001060 (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001061 if (done) {
1062 if (DBG) log("All VM reverts done");
1063 dismissDialogSafely(VOICEMAIL_REVERTING_DIALOG);
1064 onRevertDone();
1065 }
1066 }
1067 };
1068
1069 /**
Andrew Leee438b312014-10-29 16:59:15 -07001070 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001071 */
Andrew Leee438b312014-10-29 16:59:15 -07001072 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001073 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -07001074 return true;
1075 }
1076
1077 for (Integer reason : mExpectedChangeResultReasons) {
1078 if (mForwardingChangeResults.get(reason) == null) {
1079 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001080 }
1081 }
Andrew Leee438b312014-10-29 16:59:15 -07001082
1083 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001084 }
Andrew Leee438b312014-10-29 16:59:15 -07001085
1086 private boolean isFwdChangeSuccess() {
1087 if (mForwardingChangeResults == null) {
1088 return true;
1089 }
1090
1091 for (AsyncResult result : mForwardingChangeResults.values()) {
1092 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001093 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -07001094 String msg = exception.getMessage();
1095 msg = (msg != null) ? msg : "";
1096 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
1097 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001098 }
1099 }
Andrew Leee438b312014-10-29 16:59:15 -07001100 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001101 }
1102
Andrew Leee438b312014-10-29 16:59:15 -07001103 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001104 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -07001105 String msg = mVoicemailChangeResult.exception.getMessage();
1106 msg = (msg != null) ? msg : "";
1107 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
1108 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001109 }
Andrew Leee438b312014-10-29 16:59:15 -07001110
1111 if (DBG) log("VM change completed successfully.");
1112 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001113 }
1114
Andrew Leee438b312014-10-29 16:59:15 -07001115 private void handleSetVmOrFwdMessage() {
1116 if (DBG) log("handleSetVMMessage: set VM request complete");
1117
1118 if (!isFwdChangeSuccess()) {
1119 handleVmOrFwdSetError(MSG_FW_SET_EXCEPTION);
1120 } else if (!isVmChangeSuccess()) {
1121 handleVmOrFwdSetError(MSG_VM_EXCEPTION);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001122 } else {
Andrew Leee438b312014-10-29 16:59:15 -07001123 if (DBG) log("change VM success!");
1124 handleVmAndFwdSetSuccess(MSG_VM_OK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001125 }
1126 }
1127
1128 /**
1129 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
1130 * changes to those settings and show "failure" dialog.
1131 *
1132 * @param msgId Message ID used for the specific error case. {@link #MSG_FW_SET_EXCEPTION} or
1133 * {@link #MSG_VM_EXCEPTION}
1134 */
Andrew Leee438b312014-10-29 16:59:15 -07001135 private void handleVmOrFwdSetError(int msgId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001136 if (mChangingVMorFwdDueToProviderChange) {
1137 mVMOrFwdSetError = msgId;
1138 mChangingVMorFwdDueToProviderChange = false;
1139 switchToPreviousVoicemailProvider();
1140 return;
1141 }
1142 mChangingVMorFwdDueToProviderChange = false;
1143 showVMDialog(msgId);
1144 updateVoiceNumberField();
1145 }
1146
1147 /**
1148 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1149 * This updates a bunch of variables and show "success" dialog.
1150 */
Andrew Leee438b312014-10-29 16:59:15 -07001151 private void handleVmAndFwdSetSuccess(int msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001152 if (DBG) {
Andrew Leee438b312014-10-29 16:59:15 -07001153 log("handleVmAndFwdSetSuccess(). current voicemail provider key: "
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001154 + getCurrentVoicemailProviderKey());
1155 }
1156 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1157 mChangingVMorFwdDueToProviderChange = false;
1158 showVMDialog(msg);
1159 updateVoiceNumberField();
1160 }
1161
1162 /**
1163 * Update the voicemail number from what we've recorded on the sim.
1164 */
1165 private void updateVoiceNumberField() {
1166 if (DBG) {
1167 log("updateVoiceNumberField(). mSubMenuVoicemailSettings=" + mSubMenuVoicemailSettings);
1168 }
1169 if (mSubMenuVoicemailSettings == null) {
1170 return;
1171 }
1172
1173 mOldVmNumber = mPhone.getVoiceMailNumber();
1174 if (mOldVmNumber == null) {
1175 mOldVmNumber = "";
1176 }
1177 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1178 final String summary = (mOldVmNumber.length() > 0) ? mOldVmNumber :
1179 getString(R.string.voicemail_number_not_set);
1180 mSubMenuVoicemailSettings.setSummary(summary);
1181 }
1182
1183 /*
1184 * Helper Methods for Activity class.
1185 * The initial query commands are split into two pieces now
1186 * for individual expansion. This combined with the ability
1187 * to cancel queries allows for a much better user experience,
1188 * and also ensures that the user only waits to update the
1189 * data that is relevant.
1190 */
1191
1192 @Override
1193 protected void onPrepareDialog(int id, Dialog dialog) {
1194 super.onPrepareDialog(id, dialog);
1195 mCurrentDialogId = id;
1196 }
1197
1198 // dialog creation method, called by showDialog()
1199 @Override
1200 protected Dialog onCreateDialog(int id) {
1201 if ((id == VM_RESPONSE_ERROR) || (id == VM_NOCHANGE_ERROR) ||
1202 (id == FW_SET_RESPONSE_ERROR) || (id == FW_GET_RESPONSE_ERROR) ||
1203 (id == VOICEMAIL_DIALOG_CONFIRM)) {
1204
1205 AlertDialog.Builder b = new AlertDialog.Builder(this);
1206
1207 int msgId;
1208 int titleId = R.string.error_updating_title;
1209 switch (id) {
1210 case VOICEMAIL_DIALOG_CONFIRM:
1211 msgId = R.string.vm_changed;
1212 titleId = R.string.voicemail;
1213 // Set Button 2
1214 b.setNegativeButton(R.string.close_dialog, this);
1215 break;
1216 case VM_NOCHANGE_ERROR:
1217 // even though this is technically an error,
1218 // keep the title friendly.
1219 msgId = R.string.no_change;
1220 titleId = R.string.voicemail;
1221 // Set Button 2
1222 b.setNegativeButton(R.string.close_dialog, this);
1223 break;
1224 case VM_RESPONSE_ERROR:
1225 msgId = R.string.vm_change_failed;
1226 // Set Button 1
1227 b.setPositiveButton(R.string.close_dialog, this);
1228 break;
1229 case FW_SET_RESPONSE_ERROR:
1230 msgId = R.string.fw_change_failed;
1231 // Set Button 1
1232 b.setPositiveButton(R.string.close_dialog, this);
1233 break;
1234 case FW_GET_RESPONSE_ERROR:
1235 msgId = R.string.fw_get_in_vm_failed;
1236 b.setPositiveButton(R.string.alert_dialog_yes, this);
1237 b.setNegativeButton(R.string.alert_dialog_no, this);
1238 break;
1239 default:
1240 msgId = R.string.exception_error;
1241 // Set Button 3, tells the activity that the error is
1242 // not recoverable on dialog exit.
1243 b.setNeutralButton(R.string.close_dialog, this);
1244 break;
1245 }
1246
1247 b.setTitle(getText(titleId));
1248 String message = getText(msgId).toString();
1249 b.setMessage(message);
1250 b.setCancelable(false);
1251 AlertDialog dialog = b.create();
1252
1253 // make the dialog more obvious by bluring the background.
1254 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
1255
1256 return dialog;
1257 } else if (id == VOICEMAIL_FWD_SAVING_DIALOG || id == VOICEMAIL_FWD_READING_DIALOG ||
1258 id == VOICEMAIL_REVERTING_DIALOG) {
1259 ProgressDialog dialog = new ProgressDialog(this);
1260 dialog.setTitle(getText(R.string.updating_title));
1261 dialog.setIndeterminate(true);
1262 dialog.setCancelable(false);
1263 dialog.setMessage(getText(
1264 id == VOICEMAIL_FWD_SAVING_DIALOG ? R.string.updating_settings :
1265 (id == VOICEMAIL_REVERTING_DIALOG ? R.string.reverting_settings :
1266 R.string.reading_settings)));
1267 return dialog;
1268 }
1269
1270
1271 return null;
1272 }
1273
1274 // This is a method implemented for DialogInterface.OnClickListener.
1275 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1276 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1277 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1278 public void onClick(DialogInterface dialog, int which) {
1279 dialog.dismiss();
1280 switch (which){
1281 case DialogInterface.BUTTON_NEUTRAL:
1282 if (DBG) log("Neutral button");
1283 break;
1284 case DialogInterface.BUTTON_NEGATIVE:
1285 if (DBG) log("Negative button");
1286 if (mCurrentDialogId == FW_GET_RESPONSE_ERROR) {
1287 // We failed to get current forwarding settings and the user
1288 // does not wish to continue.
1289 switchToPreviousVoicemailProvider();
1290 }
1291 break;
1292 case DialogInterface.BUTTON_POSITIVE:
1293 if (DBG) log("Positive button");
1294 if (mCurrentDialogId == FW_GET_RESPONSE_ERROR) {
1295 // We failed to get current forwarding settings but the user
1296 // wishes to continue changing settings to the new vm provider
1297 saveVoiceMailAndForwardingNumberStage2();
1298 } else {
1299 finish();
1300 }
1301 return;
1302 default:
1303 // just let the dialog close and go back to the input
1304 }
1305 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1306 // with settings UI. If we were called to explicitly configure voice mail then
1307 // we finish the settings activity here to come back to whatever the user was doing.
1308 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1309 finish();
1310 }
1311 }
1312
1313 // set the app state with optional status.
1314 private void showVMDialog(int msgStatus) {
1315 switch (msgStatus) {
1316 // It's a bit worrisome to punt in the error cases here when we're
1317 // not in the foreground; maybe toast instead?
1318 case MSG_VM_EXCEPTION:
1319 showDialogIfForeground(VM_RESPONSE_ERROR);
1320 break;
1321 case MSG_FW_SET_EXCEPTION:
1322 showDialogIfForeground(FW_SET_RESPONSE_ERROR);
1323 break;
1324 case MSG_FW_GET_EXCEPTION:
1325 showDialogIfForeground(FW_GET_RESPONSE_ERROR);
1326 break;
1327 case MSG_VM_NOCHANGE:
1328 showDialogIfForeground(VM_NOCHANGE_ERROR);
1329 break;
1330 case MSG_VM_OK:
1331 showDialogIfForeground(VOICEMAIL_DIALOG_CONFIRM);
1332 break;
1333 case MSG_OK:
1334 default:
1335 // This should never happen.
1336 }
1337 }
1338
1339 /*
1340 * Activity class methods
1341 */
1342
1343 @Override
1344 protected void onCreate(Bundle icicle) {
1345 super.onCreate(icicle);
1346 if (DBG) log("onCreate(). Intent: " + getIntent());
1347 mPhone = PhoneGlobals.getPhone();
Tyler Gunnbaee2952014-09-10 16:01:02 -07001348 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andrew Lee88b51e22014-10-29 15:48:51 -07001349 mVmProviderSettingsUtil = new VoicemailProviderSettingsUtil(getApplicationContext());
Tyler Gunnbaee2952014-09-10 16:01:02 -07001350
Tyler Gunnbaee2952014-09-10 16:01:02 -07001351 // Show the voicemail preference in onResume if the calling intent specifies the
1352 // ACTION_ADD_VOICEMAIL action.
1353 mShowVoicemailPreference = (icicle == null) &&
1354 getIntent().getAction().equals(ACTION_ADD_VOICEMAIL);
1355 }
1356
1357 private void initPhoneAccountPreferences() {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001358 mPhoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001359
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001360 TelecomManager telecomManager = TelecomManager.from(this);
Andrew Lee93c345f2014-10-27 15:25:07 -07001361 TelephonyManager telephonyManager =
1362 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001363
Andrew Lee93c345f2014-10-27 15:25:07 -07001364 if ((telecomManager.getSimCallManagers().isEmpty() && !SipUtil.isVoipSupported(this))
1365 || telephonyManager.getPhoneCount() > 1) {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001366 getPreferenceScreen().removePreference(mPhoneAccountSettingsPreference);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001367 }
1368 }
1369
1370 private boolean canLaunchIntent(Intent intent) {
1371 PackageManager pm = getPackageManager();
1372 return pm.resolveActivity(intent, PackageManager.GET_ACTIVITIES) != null;
1373 }
1374
Tyler Gunnbaee2952014-09-10 16:01:02 -07001375 @Override
1376 protected void onResume() {
1377 super.onResume();
1378 mForeground = true;
1379
1380 PreferenceScreen preferenceScreen = getPreferenceScreen();
1381 if (preferenceScreen != null) {
1382 preferenceScreen.removeAll();
1383 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001384
1385 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Leedb2fe562014-09-03 15:40:43 -07001386 initPhoneAccountPreferences();
1387
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001388 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001389 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1390 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1391 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1392 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001393
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001394 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1395 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1396 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
1397 mButtonTTY = (ListPreference) findPreference(BUTTON_TTY_KEY);
1398 mVoicemailProviders = (ListPreference) findPreference(BUTTON_VOICEMAIL_PROVIDER_KEY);
Andrew Lee312e8172014-10-23 17:01:36 -07001399 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001400
Andrew Lee2c027892014-10-29 11:29:54 -07001401 mVoicemailProviders.setOnPreferenceChangeListener(this);
1402 mVoicemailSettingsScreen =
1403 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
1404 mVoicemailSettings = (PreferenceScreen)findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
1405 mVoicemailNotificationVibrate =
1406 (CheckBoxPreference) findPreference(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY);
1407 initVoiceMailProviders();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001408
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001409
Andrew Lee64a7d792014-10-15 17:38:38 -07001410 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1411 mButtonDTMF.setOnPreferenceChangeListener(this);
1412 int dtmf = Settings.System.getInt(getContentResolver(),
1413 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1414 mButtonDTMF.setValueIndex(dtmf);
1415 } else {
1416 prefSet.removePreference(mButtonDTMF);
1417 mButtonDTMF = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001418 }
1419
Andrew Lee64a7d792014-10-15 17:38:38 -07001420 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1421 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1422 int autoretry = Settings.Global.getInt(
1423 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1424 mButtonAutoRetry.setChecked(autoretry != 0);
1425 } else {
1426 prefSet.removePreference(mButtonAutoRetry);
1427 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001428 }
1429
Andrew Lee64a7d792014-10-15 17:38:38 -07001430 if (getResources().getBoolean(R.bool.hac_enabled)) {
1431 mButtonHAC.setOnPreferenceChangeListener(this);
1432 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1433 mButtonHAC.setChecked(hac != 0);
1434 } else {
1435 prefSet.removePreference(mButtonHAC);
1436 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001437 }
1438
Andrew Lee64a7d792014-10-15 17:38:38 -07001439 TelecomManager telecomManager = TelecomManager.from(this);
1440 if (telecomManager != null && telecomManager.isTtySupported()) {
1441 mButtonTTY.setOnPreferenceChangeListener(this);
1442 int settingsTtyMode = Settings.Secure.getInt(getContentResolver(),
1443 Settings.Secure.PREFERRED_TTY_MODE,
1444 TelecomManager.TTY_MODE_OFF);
1445 mButtonTTY.setValue(Integer.toString(settingsTtyMode));
1446 updatePreferredTtyModeSummary(settingsTtyMode);
1447 } else {
1448 prefSet.removePreference(mButtonTTY);
1449 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001450 }
1451
1452 if (!getResources().getBoolean(R.bool.world_phone)) {
1453 Preference options = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
Andrew Lee2170a972014-08-13 18:13:01 -07001454 if (options != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001455 prefSet.removePreference(options);
Andrew Lee2170a972014-08-13 18:13:01 -07001456 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001457 options = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
Andrew Lee2170a972014-08-13 18:13:01 -07001458 if (options != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001459 prefSet.removePreference(options);
Andrew Lee2170a972014-08-13 18:13:01 -07001460 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001461
1462 int phoneType = mPhone.getPhoneType();
1463 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1464 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001465 if (fdnButton != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001466 prefSet.removePreference(fdnButton);
Andrew Lee2170a972014-08-13 18:13:01 -07001467 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001468 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1469 addPreferencesFromResource(R.xml.cdma_call_privacy);
1470 }
1471 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
Andrew Lee2170a972014-08-13 18:13:01 -07001472 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
Etan Cohen0ca1c802014-07-07 15:35:48 -07001473 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1474 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001475 } else {
1476 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1477 }
1478 }
1479
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001480 // check the intent that started this activity and pop up the voicemail
1481 // dialog if we've been asked to.
1482 // If we have at least one non default VM provider registered then bring up
1483 // the selection for the VM provider, otherwise bring up a VM number dialog.
1484 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001485 if (mShowVoicemailPreference) {
Tyler Gunnbaee2952014-09-10 16:01:02 -07001486 if (DBG) {
1487 log("ACTION_ADD_VOICEMAIL Intent is thrown. current VM data size: "
1488 + mVMProvidersData.size());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001489 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001490 if (mVMProvidersData.size() > 1) {
1491 simulatePreferenceClick(mVoicemailProviders);
1492 } else {
1493 onPreferenceChange(mVoicemailProviders, DEFAULT_VM_PROVIDER_KEY);
1494 mVoicemailProviders.setValue(DEFAULT_VM_PROVIDER_KEY);
1495 }
1496 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001497 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001498
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001499 updateVoiceNumberField();
1500 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001501
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001502 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
1503 mPhone.getContext());
1504 if (migrateVoicemailVibrationSettingsIfNeeded(prefs)) {
1505 mVoicemailNotificationVibrate.setChecked(prefs.getBoolean(
1506 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, false));
1507 }
1508
Andrew Lee312e8172014-10-23 17:01:36 -07001509 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1510 boolean currentValue =
1511 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1512 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1513 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001514 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1515 } else {
1516 prefSet.removePreference(mEnableVideoCalling);
1517 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001518 }
1519
1520 // Migrate settings from BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY to
1521 // BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, if the latter does not exist.
1522 // Returns true if migration was performed.
1523 public static boolean migrateVoicemailVibrationSettingsIfNeeded(SharedPreferences prefs) {
1524 if (!prefs.contains(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY)) {
1525 String vibrateWhen = prefs.getString(
1526 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY, VOICEMAIL_VIBRATION_NEVER);
1527 // If vibrateWhen is always, then voicemailVibrate should be True.
1528 // otherwise if vibrateWhen is "only in silent mode", or "never", then
1529 // voicemailVibrate = False.
1530 boolean voicemailVibrate = vibrateWhen.equals(VOICEMAIL_VIBRATION_ALWAYS);
1531 final SharedPreferences.Editor editor = prefs.edit();
1532 editor.putBoolean(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, voicemailVibrate);
1533 editor.commit();
1534 return true;
1535 }
1536 return false;
1537 }
1538
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001539 private void handleTTYChange(Preference preference, Object objValue) {
1540 int buttonTtyMode;
1541 buttonTtyMode = Integer.valueOf((String) objValue).intValue();
1542 int settingsTtyMode = android.provider.Settings.Secure.getInt(
1543 getContentResolver(),
Sailesh Nepalbf900542014-07-15 16:18:32 -07001544 android.provider.Settings.Secure.PREFERRED_TTY_MODE,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001545 TelecomManager.TTY_MODE_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001546 if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
1547 Integer.toString(buttonTtyMode));
1548
1549 if (buttonTtyMode != settingsTtyMode) {
1550 switch(buttonTtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001551 case TelecomManager.TTY_MODE_OFF:
1552 case TelecomManager.TTY_MODE_FULL:
1553 case TelecomManager.TTY_MODE_HCO:
1554 case TelecomManager.TTY_MODE_VCO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001555 android.provider.Settings.Secure.putInt(getContentResolver(),
1556 android.provider.Settings.Secure.PREFERRED_TTY_MODE, buttonTtyMode);
1557 break;
1558 default:
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001559 buttonTtyMode = TelecomManager.TTY_MODE_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001560 }
1561
1562 mButtonTTY.setValue(Integer.toString(buttonTtyMode));
1563 updatePreferredTtyModeSummary(buttonTtyMode);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001564 Intent ttyModeChanged = new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
1565 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001566 sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
1567 }
1568 }
1569
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001570 private void updatePreferredTtyModeSummary(int TtyMode) {
1571 String [] txts = getResources().getStringArray(R.array.tty_mode_entries);
1572 switch(TtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001573 case TelecomManager.TTY_MODE_OFF:
1574 case TelecomManager.TTY_MODE_HCO:
1575 case TelecomManager.TTY_MODE_VCO:
1576 case TelecomManager.TTY_MODE_FULL:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001577 mButtonTTY.setSummary(txts[TtyMode]);
1578 break;
1579 default:
1580 mButtonTTY.setEnabled(false);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001581 mButtonTTY.setSummary(txts[TelecomManager.TTY_MODE_OFF]);
Sailesh Nepalbf900542014-07-15 16:18:32 -07001582 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001583 }
1584 }
1585
1586 private static void log(String msg) {
1587 Log.d(LOG_TAG, msg);
1588 }
1589
1590 /**
1591 * Updates the look of the VM preference widgets based on current VM provider settings.
1592 * Note that the provider name is loaded form the found activity via loadLabel in
1593 * {@link #initVoiceMailProviders()} in order for it to be localizable.
1594 */
1595 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1596 final String key = currentProviderSetting;
1597 final VoiceMailProvider provider = mVMProvidersData.get(key);
1598
1599 /* This is the case when we are coming up on a freshly wiped phone and there is no
1600 persisted value for the list preference mVoicemailProviders.
1601 In this case we want to show the UI asking the user to select a voicemail provider as
1602 opposed to silently falling back to default one. */
1603 if (provider == null) {
1604 if (DBG) {
1605 log("updateVMPreferenceWidget: provider for the key \"" + key + "\" is null.");
1606 }
1607 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1608 mVoicemailSettings.setEnabled(false);
1609 mVoicemailSettings.setIntent(null);
1610
1611 mVoicemailNotificationVibrate.setEnabled(false);
1612 } else {
1613 if (DBG) {
1614 log("updateVMPreferenceWidget: provider for the key \"" + key + "\".."
1615 + "name: " + provider.name
1616 + ", intent: " + provider.intent);
1617 }
1618 final String providerName = provider.name;
1619 mVoicemailProviders.setSummary(providerName);
1620 mVoicemailSettings.setEnabled(true);
1621 mVoicemailSettings.setIntent(provider.intent);
1622
1623 mVoicemailNotificationVibrate.setEnabled(true);
1624 }
1625 }
1626
1627 /**
1628 * Enumerates existing VM providers and puts their data into the list and populates
1629 * the preference list objects with their names.
1630 * In case we are called with ACTION_ADD_VOICEMAIL intent the intent may have
1631 * an extra string called IGNORE_PROVIDER_EXTRA with "package.activityName" of the provider
1632 * which should be hidden when we bring up the list of possible VM providers to choose.
1633 */
1634 private void initVoiceMailProviders() {
1635 if (DBG) log("initVoiceMailProviders()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001636
1637 String providerToIgnore = null;
1638 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1639 if (getIntent().hasExtra(IGNORE_PROVIDER_EXTRA)) {
1640 providerToIgnore = getIntent().getStringExtra(IGNORE_PROVIDER_EXTRA);
1641 }
1642 if (DBG) log("Found ACTION_ADD_VOICEMAIL. providerToIgnore=" + providerToIgnore);
1643 if (providerToIgnore != null) {
1644 // IGNORE_PROVIDER_EXTRA implies we want to remove the choice from the list.
Andrew Lee88b51e22014-10-29 15:48:51 -07001645 mVmProviderSettingsUtil.delete(providerToIgnore);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001646 }
1647 }
1648
1649 mVMProvidersData.clear();
1650
1651 // Stick the default element which is always there
1652 final String myCarrier = getString(R.string.voicemail_default);
1653 mVMProvidersData.put(DEFAULT_VM_PROVIDER_KEY, new VoiceMailProvider(myCarrier, null));
1654
1655 // Enumerate providers
1656 PackageManager pm = getPackageManager();
1657 Intent intent = new Intent();
1658 intent.setAction(ACTION_CONFIGURE_VOICEMAIL);
1659 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
1660 int len = resolveInfos.size() + 1; // +1 for the default choice we will insert.
1661
1662 // Go through the list of discovered providers populating the data map
1663 // skip the provider we were instructed to ignore if there was one
1664 for (int i = 0; i < resolveInfos.size(); i++) {
1665 final ResolveInfo ri= resolveInfos.get(i);
1666 final ActivityInfo currentActivityInfo = ri.activityInfo;
Andrew Lee6214e2b2014-11-04 13:57:38 -08001667 final String key = currentActivityInfo.name;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001668 if (key.equals(providerToIgnore)) {
1669 if (DBG) log("Ignoring key: " + key);
1670 len--;
1671 continue;
1672 }
1673 if (DBG) log("Loading key: " + key);
1674 final String nameForDisplay = ri.loadLabel(pm).toString();
1675 Intent providerIntent = new Intent();
1676 providerIntent.setAction(ACTION_CONFIGURE_VOICEMAIL);
1677 providerIntent.setClassName(currentActivityInfo.packageName,
1678 currentActivityInfo.name);
1679 if (DBG) {
1680 log("Store loaded VoiceMailProvider. key: " + key
1681 + " -> name: " + nameForDisplay + ", intent: " + providerIntent);
1682 }
1683 mVMProvidersData.put(
1684 key,
1685 new VoiceMailProvider(nameForDisplay, providerIntent));
1686
1687 }
1688
1689 // Now we know which providers to display - create entries and values array for
1690 // the list preference
1691 String [] entries = new String [len];
1692 String [] values = new String [len];
1693 entries[0] = myCarrier;
1694 values[0] = DEFAULT_VM_PROVIDER_KEY;
1695 int entryIdx = 1;
1696 for (int i = 0; i < resolveInfos.size(); i++) {
Andrew Lee6214e2b2014-11-04 13:57:38 -08001697 final String key = resolveInfos.get(i).activityInfo.name;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001698 if (!mVMProvidersData.containsKey(key)) {
1699 continue;
1700 }
1701 entries[entryIdx] = mVMProvidersData.get(key).name;
1702 values[entryIdx] = key;
1703 entryIdx++;
1704 }
1705
1706 // ListPreference is now updated.
1707 mVoicemailProviders.setEntries(entries);
1708 mVoicemailProviders.setEntryValues(values);
1709
1710 // Remember the current Voicemail Provider key as a "previous" key. This will be used
1711 // when we fail to update Voicemail Provider, which requires rollback.
1712 // We will update this when the VM Provider setting is successfully updated.
1713 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1714 if (DBG) log("Set up the first mPreviousVMProviderKey: " + mPreviousVMProviderKey);
1715
1716 // Finally update the preference texts.
1717 updateVMPreferenceWidgets(mPreviousVMProviderKey);
1718 }
1719
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001720 /**
1721 * Simulates user clicking on a passed preference.
1722 * Usually needed when the preference is a dialog preference and we want to invoke
1723 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001724 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001725 */
1726 private void simulatePreferenceClick(Preference preference) {
1727 // Go through settings until we find our setting
1728 // and then simulate a click on it to bring up the dialog
1729 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1730 for (int idx = 0; idx < adapter.getCount(); idx++) {
1731 if (adapter.getItem(idx) == preference) {
1732 getPreferenceScreen().onItemClick(this.getListView(),
1733 null, idx, adapter.getItemId(idx));
1734 break;
1735 }
1736 }
1737 }
1738
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001739 private String getCurrentVoicemailProviderKey() {
1740 final String key = mVoicemailProviders.getValue();
1741 return (key != null) ? key : DEFAULT_VM_PROVIDER_KEY;
1742 }
1743
1744 @Override
1745 public boolean onOptionsItemSelected(MenuItem item) {
1746 final int itemId = item.getItemId();
1747 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001748 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001749 return true;
1750 }
1751 return super.onOptionsItemSelected(item);
1752 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001753 /**
1754 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1755 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1756 */
1757 public static void goUpToTopLevelSetting(Activity activity) {
1758 Intent intent = new Intent(activity, CallFeaturesSetting.class);
1759 intent.setAction(Intent.ACTION_MAIN);
1760 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1761 activity.startActivity(intent);
1762 activity.finish();
1763 }
1764}