blob: 9daf7c4187ec06601e4a9fcae8803c174455d07f [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;
36import android.media.RingtoneManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070037import android.net.sip.SipManager;
38import android.os.AsyncResult;
39import android.os.Bundle;
40import android.os.Handler;
41import android.os.Message;
42import android.os.UserHandle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.preference.CheckBoxPreference;
44import android.preference.ListPreference;
45import android.preference.Preference;
46import android.preference.PreferenceActivity;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.preference.PreferenceManager;
48import android.preference.PreferenceScreen;
49import android.provider.ContactsContract.CommonDataKinds;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070051import android.telecom.PhoneAccountHandle;
52import android.telecom.TelecomManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import android.telephony.PhoneNumberUtils;
54import android.text.TextUtils;
55import android.util.Log;
56import android.view.MenuItem;
57import android.view.WindowManager;
58import android.widget.ListAdapter;
59
60import com.android.internal.telephony.CallForwardInfo;
61import com.android.internal.telephony.CommandsInterface;
62import com.android.internal.telephony.Phone;
63import com.android.internal.telephony.PhoneConstants;
Andrew Lee2170a972014-08-13 18:13:01 -070064import com.android.phone.common.util.SettingsUtil;
Andrew Leedb2fe562014-09-03 15:40:43 -070065import com.android.phone.settings.AccountSelectionPreference;
Sailesh Nepal788959e2014-07-08 23:36:40 -070066import com.android.services.telephony.sip.SipSharedPreferences;
67import 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 *
80 * This preference screen is the root of the "Call settings" hierarchy
81 * available from the Phone app; the settings here let you control various
82 * features related to phone calls (including voicemail settings, SIP
83 * settings, the "Respond via SMS" feature, and others.) It's used only
84 * on voice-capable phone devices.
85 *
86 * Note that this activity is part of the package com.android.phone, even
87 * though you reach it from the "Phone" app (i.e. DialtactsActivity) which
88 * is from the package com.android.contacts.
89 *
90 * For the "Mobile network settings" screen under the main Settings app,
91 * See {@link MobileNetworkSettings}.
92 *
Andrew Lee2170a972014-08-13 18:13:01 -070093 * TODO: Settings should be split into PreferenceFragments where possible (ie. voicemail, SIP).
94 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095 * @see com.android.phone.MobileNetworkSettings
96 */
97public class CallFeaturesSetting extends PreferenceActivity
98 implements DialogInterface.OnClickListener,
Andrew Lee2170a972014-08-13 18:13:01 -070099 Preference.OnPreferenceChangeListener,
100 Preference.OnPreferenceClickListener,
101 EditPhoneNumberPreference.OnDialogClosedListener,
Andrew Leedb2fe562014-09-03 15:40:43 -0700102 EditPhoneNumberPreference.GetDefaultNumberListener,
103 AccountSelectionPreference.AccountSelectionListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700104 private static final String LOG_TAG = "CallFeaturesSetting";
105 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
106
107 /**
108 * Intent action to bring up Voicemail Provider settings.
109 *
110 * @see #IGNORE_PROVIDER_EXTRA
111 */
112 public static final String ACTION_ADD_VOICEMAIL =
113 "com.android.phone.CallFeaturesSetting.ADD_VOICEMAIL";
114 // intent action sent by this activity to a voice mail provider
115 // to trigger its configuration UI
116 public static final String ACTION_CONFIGURE_VOICEMAIL =
117 "com.android.phone.CallFeaturesSetting.CONFIGURE_VOICEMAIL";
118 // Extra put in the return from VM provider config containing voicemail number to set
119 public static final String VM_NUMBER_EXTRA = "com.android.phone.VoicemailNumber";
120 // Extra put in the return from VM provider config containing call forwarding number to set
121 public static final String FWD_NUMBER_EXTRA = "com.android.phone.ForwardingNumber";
122 // Extra put in the return from VM provider config containing call forwarding number to set
123 public static final String FWD_NUMBER_TIME_EXTRA = "com.android.phone.ForwardingNumberTime";
124 // If the VM provider returns non null value in this extra we will force the user to
125 // choose another VM provider
126 public static final String SIGNOUT_EXTRA = "com.android.phone.Signout";
127 //Information about logical "up" Activity
128 private static final String UP_ACTIVITY_PACKAGE = "com.android.dialer";
129 private static final String UP_ACTIVITY_CLASS =
130 "com.android.dialer.DialtactsActivity";
131
132 // Used to tell the saving logic to leave forwarding number as is
133 public static final CallForwardInfo[] FWD_SETTINGS_DONT_TOUCH = null;
134 // Suffix appended to provider key for storing vm number
135 public static final String VM_NUMBER_TAG = "#VMNumber";
136 // Suffix appended to provider key for storing forwarding settings
137 public static final String FWD_SETTINGS_TAG = "#FWDSettings";
138 // Suffix appended to forward settings key for storing length of settings array
139 public static final String FWD_SETTINGS_LENGTH_TAG = "#Length";
140 // Suffix appended to forward settings key for storing an individual setting
141 public static final String FWD_SETTING_TAG = "#Setting";
142 // Suffixes appended to forward setting key for storing an individual setting properties
143 public static final String FWD_SETTING_STATUS = "#Status";
144 public static final String FWD_SETTING_REASON = "#Reason";
145 public static final String FWD_SETTING_NUMBER = "#Number";
146 public static final String FWD_SETTING_TIME = "#Time";
147
148 // Key identifying the default vocie mail provider
149 public static final String DEFAULT_VM_PROVIDER_KEY = "";
150
151 /**
152 * String Extra put into ACTION_ADD_VOICEMAIL call to indicate which provider should be hidden
153 * in the list of providers presented to the user. This allows a provider which is being
154 * disabled (e.g. GV user logging out) to force the user to pick some other provider.
155 */
156 public static final String IGNORE_PROVIDER_EXTRA = "com.android.phone.ProviderToIgnore";
157
158 // string constants
159 private static final String NUM_PROJECTION[] = {CommonDataKinds.Phone.NUMBER};
160
161 // String keys for preference lookup
162 // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
163 private static final String BUTTON_VOICEMAIL_KEY = "button_voicemail_key";
164 private static final String BUTTON_VOICEMAIL_PROVIDER_KEY = "button_voicemail_provider_key";
165 private static final String BUTTON_VOICEMAIL_SETTING_KEY = "button_voicemail_setting_key";
166 // New preference key for voicemail notification vibration
167 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY =
168 "button_voicemail_notification_vibrate_key";
169 // Old preference key for voicemail notification vibration. Used for migration to the new
170 // preference key only.
171 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY =
172 "button_voicemail_notification_vibrate_when_key";
173 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_RINGTONE_KEY =
174 "button_voicemail_notification_ringtone_key";
175 private static final String BUTTON_FDN_KEY = "button_fdn_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700176
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700177 private static final String BUTTON_DTMF_KEY = "button_dtmf_settings";
178 private static final String BUTTON_RETRY_KEY = "button_auto_retry_key";
179 private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
180 private static final String BUTTON_HAC_KEY = "button_hac_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700181
182 private static final String BUTTON_GSM_UMTS_OPTIONS = "button_gsm_more_expand_key";
183 private static final String BUTTON_CDMA_OPTIONS = "button_cdma_more_expand_key";
184
185 private static final String VM_NUMBERS_SHARED_PREFERENCES_NAME = "vm_numbers";
186
Andrew Lee2170a972014-08-13 18:13:01 -0700187 private static final String BUTTON_SIP_CALL_OPTIONS = "sip_call_options_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188 private static final String BUTTON_SIP_CALL_OPTIONS_WIFI_ONLY =
189 "sip_call_options_wifi_only_key";
Andrew Lee2170a972014-08-13 18:13:01 -0700190 private static final String SIP_SETTINGS_PREFERENCE_SCREEN_KEY =
191 "sip_settings_preference_screen_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700192
Andrew Leedb2fe562014-09-03 15:40:43 -0700193 private static final String DEFAULT_OUTGOING_ACCOUNT_KEY = "default_outgoing_account";
194 private static final String WIFI_CALL_MANAGER_ACCOUNT_KEY = "wifi_call_manager_account";
195
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700196 private Intent mContactListIntent;
197
198 /** Event for Async voicemail change call */
199 private static final int EVENT_VOICEMAIL_CHANGED = 500;
200 private static final int EVENT_FORWARDING_CHANGED = 501;
201 private static final int EVENT_FORWARDING_GET_COMPLETED = 502;
202
Andrew Lee2170a972014-08-13 18:13:01 -0700203 private static final int MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY = 1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700204
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700205 public static final String HAC_KEY = "HACSetting";
206 public static final String HAC_VAL_ON = "ON";
207 public static final String HAC_VAL_OFF = "OFF";
208
209 /** Handle to voicemail pref */
210 private static final int VOICEMAIL_PREF_ID = 1;
211 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
212
213 private Phone mPhone;
214
215 private AudioManager mAudioManager;
216 private SipManager mSipManager;
217
218 private static final int VM_NOCHANGE_ERROR = 400;
219 private static final int VM_RESPONSE_ERROR = 500;
220 private static final int FW_SET_RESPONSE_ERROR = 501;
221 private static final int FW_GET_RESPONSE_ERROR = 502;
222
223
224 // dialog identifiers for voicemail
225 private static final int VOICEMAIL_DIALOG_CONFIRM = 600;
226 private static final int VOICEMAIL_FWD_SAVING_DIALOG = 601;
227 private static final int VOICEMAIL_FWD_READING_DIALOG = 602;
228 private static final int VOICEMAIL_REVERTING_DIALOG = 603;
229
230 // status message sent back from handlers
231 private static final int MSG_OK = 100;
232
233 // special statuses for voicemail controls.
234 private static final int MSG_VM_EXCEPTION = 400;
235 private static final int MSG_FW_SET_EXCEPTION = 401;
236 private static final int MSG_FW_GET_EXCEPTION = 402;
237 private static final int MSG_VM_OK = 600;
238 private static final int MSG_VM_NOCHANGE = 700;
239
240 // voicemail notification vibration string constants
241 private static final String VOICEMAIL_VIBRATION_ALWAYS = "always";
242 private static final String VOICEMAIL_VIBRATION_NEVER = "never";
243
244 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
245
Andrew Lee2170a972014-08-13 18:13:01 -0700246 private Runnable mVoicemailRingtoneLookupRunnable;
247 private final Handler mVoicemailRingtoneLookupComplete = new Handler() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700248 @Override
249 public void handleMessage(Message msg) {
250 switch (msg.what) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700251 case MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY:
252 mVoicemailNotificationRingtone.setSummary((CharSequence) msg.obj);
253 break;
254 }
255 }
256 };
257
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700258 /** Whether dialpad plays DTMF tone or not. */
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700259 private CheckBoxPreference mButtonAutoRetry;
260 private CheckBoxPreference mButtonHAC;
261 private ListPreference mButtonDTMF;
262 private ListPreference mButtonTTY;
263 private ListPreference mButtonSipCallOptions;
Evan Charlton1c696832014-04-15 14:24:23 -0700264 private Preference mWifiCallOptionsPreference;
265 private Preference mWifiCallAccountPreference;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700266 private ListPreference mVoicemailProviders;
267 private PreferenceScreen mVoicemailSettings;
268 private Preference mVoicemailNotificationRingtone;
269 private CheckBoxPreference mVoicemailNotificationVibrate;
270 private SipSharedPreferences mSipSharedPreferences;
Andrew Leedb2fe562014-09-03 15:40:43 -0700271 private AccountSelectionPreference mDefaultOutgoingAccount;
272 private AccountSelectionPreference mSimCallManagerAccount;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700273
274 private class VoiceMailProvider {
275 public VoiceMailProvider(String name, Intent intent) {
276 this.name = name;
277 this.intent = intent;
278 }
279 public String name;
280 public Intent intent;
281 }
282
283 /**
284 * Forwarding settings we are going to save.
285 */
286 private static final int [] FORWARDING_SETTINGS_REASONS = new int[] {
287 CommandsInterface.CF_REASON_UNCONDITIONAL,
288 CommandsInterface.CF_REASON_BUSY,
289 CommandsInterface.CF_REASON_NO_REPLY,
290 CommandsInterface.CF_REASON_NOT_REACHABLE
291 };
292
293 private class VoiceMailProviderSettings {
294 /**
295 * Constructs settings object, setting all conditional forwarding to the specified number
296 */
297 public VoiceMailProviderSettings(String voicemailNumber, String forwardingNumber,
298 int timeSeconds) {
299 this.voicemailNumber = voicemailNumber;
300 if (forwardingNumber == null || forwardingNumber.length() == 0) {
301 this.forwardingSettings = FWD_SETTINGS_DONT_TOUCH;
302 } else {
303 this.forwardingSettings = new CallForwardInfo[FORWARDING_SETTINGS_REASONS.length];
304 for (int i = 0; i < this.forwardingSettings.length; i++) {
305 CallForwardInfo fi = new CallForwardInfo();
306 this.forwardingSettings[i] = fi;
307 fi.reason = FORWARDING_SETTINGS_REASONS[i];
308 fi.status = (fi.reason == CommandsInterface.CF_REASON_UNCONDITIONAL) ? 0 : 1;
309 fi.serviceClass = CommandsInterface.SERVICE_CLASS_VOICE;
310 fi.toa = PhoneNumberUtils.TOA_International;
311 fi.number = forwardingNumber;
312 fi.timeSeconds = timeSeconds;
313 }
314 }
315 }
316
317 public VoiceMailProviderSettings(String voicemailNumber, CallForwardInfo[] infos) {
318 this.voicemailNumber = voicemailNumber;
319 this.forwardingSettings = infos;
320 }
321
322 @Override
323 public boolean equals(Object o) {
324 if (o == null) return false;
325 if (!(o instanceof VoiceMailProviderSettings)) return false;
326 final VoiceMailProviderSettings v = (VoiceMailProviderSettings)o;
327
328 return ((this.voicemailNumber == null &&
329 v.voicemailNumber == null) ||
330 this.voicemailNumber != null &&
331 this.voicemailNumber.equals(v.voicemailNumber))
332 &&
333 forwardingSettingsEqual(this.forwardingSettings,
334 v.forwardingSettings);
335 }
336
337 private boolean forwardingSettingsEqual(CallForwardInfo[] infos1,
338 CallForwardInfo[] infos2) {
339 if (infos1 == infos2) return true;
340 if (infos1 == null || infos2 == null) return false;
341 if (infos1.length != infos2.length) return false;
342 for (int i = 0; i < infos1.length; i++) {
343 CallForwardInfo i1 = infos1[i];
344 CallForwardInfo i2 = infos2[i];
345 if (i1.status != i2.status ||
346 i1.reason != i2.reason ||
347 i1.serviceClass != i2.serviceClass ||
348 i1.toa != i2.toa ||
349 i1.number != i2.number ||
350 i1.timeSeconds != i2.timeSeconds) {
351 return false;
352 }
353 }
354 return true;
355 }
356
357 @Override
358 public String toString() {
359 return voicemailNumber + ((forwardingSettings != null ) ? (", " +
360 forwardingSettings.toString()) : "");
361 }
362
363 public String voicemailNumber;
364 public CallForwardInfo[] forwardingSettings;
365 }
366
367 private SharedPreferences mPerProviderSavedVMNumbers;
368
369 /**
370 * Results of reading forwarding settings
371 */
372 private CallForwardInfo[] mForwardingReadResults = null;
373
374 /**
375 * Result of forwarding number change.
376 * Keys are reasons (eg. unconditional forwarding).
377 */
378 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
379
380 /**
381 * Expected CF read result types.
382 * This set keeps track of the CF types for which we've issued change
383 * commands so we can tell when we've received all of the responses.
384 */
385 private Collection<Integer> mExpectedChangeResultReasons = null;
386
387 /**
388 * Result of vm number change
389 */
390 private AsyncResult mVoicemailChangeResult = null;
391
392 /**
393 * Previous VM provider setting so we can return to it in case of failure.
394 */
395 private String mPreviousVMProviderKey = null;
396
397 /**
398 * Id of the dialog being currently shown.
399 */
400 private int mCurrentDialogId = 0;
401
402 /**
403 * Flag indicating that we are invoking settings for the voicemail provider programmatically
404 * due to vm provider change.
405 */
406 private boolean mVMProviderSettingsForced = false;
407
408 /**
409 * Flag indicating that we are making changes to vm or fwd numbers
410 * due to vm provider change.
411 */
412 private boolean mChangingVMorFwdDueToProviderChange = false;
413
414 /**
415 * True if we are in the process of vm & fwd number change and vm has already been changed.
416 * This is used to decide what to do in case of rollback.
417 */
418 private boolean mVMChangeCompletedSuccessfully = false;
419
420 /**
421 * True if we had full or partial failure setting forwarding numbers and so need to roll them
422 * back.
423 */
424 private boolean mFwdChangesRequireRollback = false;
425
426 /**
427 * Id of error msg to display to user once we are done reverting the VM provider to the previous
428 * one.
429 */
430 private int mVMOrFwdSetError = 0;
431
432 /**
433 * Data about discovered voice mail settings providers.
434 * Is populated by querying which activities can handle ACTION_CONFIGURE_VOICEMAIL.
435 * They key in this map is package name + activity name.
436 * We always add an entry for the default provider with a key of empty
437 * string and intent value of null.
438 * @see #initVoiceMailProviders()
439 */
440 private final Map<String, VoiceMailProvider> mVMProvidersData =
441 new HashMap<String, VoiceMailProvider>();
442
443 /** string to hold old voicemail number as it is being updated. */
444 private String mOldVmNumber;
445
446 // New call forwarding settings and vm number we will be setting
447 // Need to save these since before we get to saving we need to asynchronously
448 // query the existing forwarding settings.
449 private CallForwardInfo[] mNewFwdSettings;
450 private String mNewVMNumber;
451
452 private boolean mForeground;
453
454 @Override
455 public void onPause() {
456 super.onPause();
457 mForeground = false;
458 }
459
460 /**
461 * We have to pull current settings from the network for all kinds of
462 * voicemail providers so we can tell whether we have to update them,
463 * so use this bit to keep track of whether we're reading settings for the
464 * default provider and should therefore save them out when done.
465 */
466 private boolean mReadingSettingsForDefaultProvider = false;
467
Tyler Gunnbaee2952014-09-10 16:01:02 -0700468 /**
469 * Used to indicate that the voicemail preference should be shown.
470 */
471 private boolean mShowVoicemailPreference = false;
472
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700473 /*
474 * Click Listeners, handle click based on objects attached to UI.
475 */
476
477 // Click listener for all toggle events
478 @Override
479 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
480 if (preference == mSubMenuVoicemailSettings) {
481 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700482 } else if (preference == mButtonDTMF) {
483 return true;
484 } else if (preference == mButtonTTY) {
485 return true;
486 } else if (preference == mButtonAutoRetry) {
487 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
488 android.provider.Settings.Global.CALL_AUTO_RETRY,
489 mButtonAutoRetry.isChecked() ? 1 : 0);
490 return true;
491 } else if (preference == mButtonHAC) {
492 int hac = mButtonHAC.isChecked() ? 1 : 0;
493 // Update HAC value in Settings database
494 Settings.System.putInt(mPhone.getContext().getContentResolver(),
495 Settings.System.HEARING_AID, hac);
496
497 // Update HAC Value in AudioManager
498 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
499 return true;
500 } else if (preference == mVoicemailSettings) {
501 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
502 if (preference.getIntent() != null) {
503 if (DBG) {
504 log("onPreferenceTreeClick: Invoking cfg intent "
505 + preference.getIntent().getPackage());
506 }
507
508 // onActivityResult() will be responsible for resetting some of variables.
509 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
510 return true;
511 } else {
512 if (DBG) {
513 log("onPreferenceTreeClick:"
514 + " No Intent is available. Use default behavior defined in xml.");
515 }
516
517 // There's no onActivityResult(), so we need to take care of some of variables
518 // which should be reset here.
519 mPreviousVMProviderKey = DEFAULT_VM_PROVIDER_KEY;
520 mVMProviderSettingsForced = false;
521
522 // This should let the preference use default behavior in the xml.
523 return false;
524 }
525 }
526 return false;
527 }
528
529 /**
530 * Implemented to support onPreferenceChangeListener to look for preference
531 * changes.
532 *
533 * @param preference is the preference to be changed
534 * @param objValue should be the value of the selection, NOT its localized
535 * display value.
536 */
537 @Override
538 public boolean onPreferenceChange(Preference preference, Object objValue) {
539 if (DBG) {
540 log("onPreferenceChange(). preferenece: \"" + preference + "\""
541 + ", value: \"" + objValue + "\"");
542 }
Andrew Lee2170a972014-08-13 18:13:01 -0700543
544 if (preference == mButtonDTMF) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700545 int index = mButtonDTMF.findIndexOfValue((String) objValue);
546 Settings.System.putInt(mPhone.getContext().getContentResolver(),
547 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
548 } else if (preference == mButtonTTY) {
549 handleTTYChange(preference, objValue);
550 } else if (preference == mVoicemailProviders) {
551 final String newProviderKey = (String) objValue;
552 if (DBG) {
553 log("Voicemail Provider changes from \"" + mPreviousVMProviderKey
554 + "\" to \"" + newProviderKey + "\".");
555 }
556 // If previous provider key and the new one is same, we don't need to handle it.
557 if (mPreviousVMProviderKey.equals(newProviderKey)) {
558 if (DBG) log("No change is made toward VM provider setting.");
559 return true;
560 }
561 updateVMPreferenceWidgets(newProviderKey);
562
563 final VoiceMailProviderSettings newProviderSettings =
564 loadSettingsForVoiceMailProvider(newProviderKey);
565
566 // If the user switches to a voice mail provider and we have a
567 // numbers stored for it we will automatically change the
568 // phone's
569 // voice mail and forwarding number to the stored ones.
570 // Otherwise we will bring up provider's configuration UI.
571
572 if (newProviderSettings == null) {
573 // Force the user into a configuration of the chosen provider
574 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
575 mVMProviderSettingsForced = true;
576 simulatePreferenceClick(mVoicemailSettings);
577 } else {
578 if (DBG) log("Saved preferences found - switching to them");
579 // Set this flag so if we get a failure we revert to previous provider
580 mChangingVMorFwdDueToProviderChange = true;
581 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
582 }
583 } else if (preference == mButtonSipCallOptions) {
584 handleSipCallOptionsChange(objValue);
585 }
586 // always let the preference setting proceed.
587 return true;
588 }
589
590 @Override
Evan Charlton1c696832014-04-15 14:24:23 -0700591 public boolean onPreferenceClick(Preference preference) {
592 if (preference == mWifiCallOptionsPreference || preference == mWifiCallAccountPreference) {
593 handleWifiCallSettingsClick(preference);
594 }
595 return true;
596 }
597
598 @Override
Andrew Leedb2fe562014-09-03 15:40:43 -0700599 public boolean onAccountSelected(AccountSelectionPreference pref, PhoneAccountHandle account) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -0700600 TelecomManager telecomManager = TelecomManager.from(this);
Andrew Leedb2fe562014-09-03 15:40:43 -0700601 if (pref == mDefaultOutgoingAccount) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -0700602 telecomManager.setUserSelectedOutgoingPhoneAccount(account);
Andrew Leedb2fe562014-09-03 15:40:43 -0700603 return true;
604 } else if (pref == mSimCallManagerAccount) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -0700605 telecomManager.setSimCallManager(account);
Andrew Leedb2fe562014-09-03 15:40:43 -0700606 return true;
607 }
608 return false;
609 }
610
Tyler Gunnbaee2952014-09-10 16:01:02 -0700611 /**
612 * Called just prior to showing an AccountSelection dialog to re-populate the model of the
Nancy Chen90f2ffd2014-09-15 17:58:14 -0700613 * AccountSelection dialog.
Tyler Gunnbaee2952014-09-10 16:01:02 -0700614 *
615 * @param pref The account selection preference dialog being shown.
616 */
617 @Override
618 public void onAccountSelectionDialogShow(AccountSelectionPreference pref) {
619 if (pref == mDefaultOutgoingAccount) {
620 populateDefaultOutgoingAccountsModel();
621 } else if (pref == mSimCallManagerAccount) {
622 populateSimCallManagerAccountsModel();
623 }
624 }
625
Andrew Leedb2fe562014-09-03 15:40:43 -0700626 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700627 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
628 if (DBG) log("onPreferenceClick: request preference click on dialog close: " +
629 buttonClicked);
630 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
631 return;
632 }
633
634 if (preference == mSubMenuVoicemailSettings) {
635 handleVMBtnClickRequest();
636 }
637 }
638
639 /**
640 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
641 * This method set the default values for the various
642 * EditPhoneNumberPreference dialogs.
643 */
644 @Override
645 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
646 if (preference == mSubMenuVoicemailSettings) {
647 // update the voicemail number field, which takes care of the
648 // mSubMenuVoicemailSettings itself, so we should return null.
649 if (DBG) log("updating default for voicemail dialog");
650 updateVoiceNumberField();
651 return null;
652 }
653
654 String vmDisplay = mPhone.getVoiceMailNumber();
655 if (TextUtils.isEmpty(vmDisplay)) {
656 // if there is no voicemail number, we just return null to
657 // indicate no contribution.
658 return null;
659 }
660
661 // Return the voicemail number prepended with "VM: "
662 if (DBG) log("updating default for call forwarding dialogs");
663 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
664 }
665
666
667 // override the startsubactivity call to make changes in state consistent.
668 @Override
669 public void startActivityForResult(Intent intent, int requestCode) {
670 if (requestCode == -1) {
671 // this is an intent requested from the preference framework.
672 super.startActivityForResult(intent, requestCode);
673 return;
674 }
675
676 if (DBG) log("startSubActivity: starting requested subactivity");
677 super.startActivityForResult(intent, requestCode);
678 }
679
680 private void switchToPreviousVoicemailProvider() {
681 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
682 if (mPreviousVMProviderKey != null) {
683 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
684 // we have to revert with carrier
685 if (DBG) {
686 log("Needs to rollback."
687 + " mVMChangeCompletedSuccessfully=" + mVMChangeCompletedSuccessfully
688 + ", mFwdChangesRequireRollback=" + mFwdChangesRequireRollback);
689 }
690
691 showDialogIfForeground(VOICEMAIL_REVERTING_DIALOG);
692 final VoiceMailProviderSettings prevSettings =
693 loadSettingsForVoiceMailProvider(mPreviousVMProviderKey);
694 if (prevSettings == null) {
695 // prevSettings never becomes null since it should be already loaded!
696 Log.e(LOG_TAG, "VoiceMailProviderSettings for the key \""
697 + mPreviousVMProviderKey + "\" becomes null, which is unexpected.");
698 if (DBG) {
699 Log.e(LOG_TAG,
700 "mVMChangeCompletedSuccessfully: " + mVMChangeCompletedSuccessfully
701 + ", mFwdChangesRequireRollback: " + mFwdChangesRequireRollback);
702 }
703 }
704 if (mVMChangeCompletedSuccessfully) {
705 mNewVMNumber = prevSettings.voicemailNumber;
706 Log.i(LOG_TAG, "VM change is already completed successfully."
707 + "Have to revert VM back to " + mNewVMNumber + " again.");
708 mPhone.setVoiceMailNumber(
709 mPhone.getVoiceMailAlphaTag().toString(),
710 mNewVMNumber,
711 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
712 }
713 if (mFwdChangesRequireRollback) {
714 Log.i(LOG_TAG, "Requested to rollback Fwd changes.");
715 final CallForwardInfo[] prevFwdSettings =
716 prevSettings.forwardingSettings;
717 if (prevFwdSettings != null) {
718 Map<Integer, AsyncResult> results =
719 mForwardingChangeResults;
720 resetForwardingChangeState();
721 for (int i = 0; i < prevFwdSettings.length; i++) {
722 CallForwardInfo fi = prevFwdSettings[i];
723 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
724 // Only revert the settings for which the update
725 // succeeded
726 AsyncResult result = results.get(fi.reason);
727 if (result != null && result.exception == null) {
728 mExpectedChangeResultReasons.add(fi.reason);
729 mPhone.setCallForwardingOption(
730 (fi.status == 1 ?
731 CommandsInterface.CF_ACTION_REGISTRATION :
732 CommandsInterface.CF_ACTION_DISABLE),
733 fi.reason,
734 fi.number,
735 fi.timeSeconds,
736 mRevertOptionComplete.obtainMessage(
737 EVENT_FORWARDING_CHANGED, i, 0));
738 }
739 }
740 }
741 }
742 } else {
743 if (DBG) log("No need to revert");
744 onRevertDone();
745 }
746 }
747 }
748
749 private void onRevertDone() {
750 if (DBG) log("Flipping provider key back to " + mPreviousVMProviderKey);
751 mVoicemailProviders.setValue(mPreviousVMProviderKey);
752 updateVMPreferenceWidgets(mPreviousVMProviderKey);
753 updateVoiceNumberField();
754 if (mVMOrFwdSetError != 0) {
755 showVMDialog(mVMOrFwdSetError);
756 mVMOrFwdSetError = 0;
757 }
758 }
759
760 @Override
761 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
762 if (DBG) {
763 log("onActivityResult: requestCode: " + requestCode
764 + ", resultCode: " + resultCode
765 + ", data: " + data);
766 }
767 // there are cases where the contact picker may end up sending us more than one
768 // request. We want to ignore the request if we're not in the correct state.
769 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
770 boolean failure = false;
771
772 // No matter how the processing of result goes lets clear the flag
773 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
774 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
775 mVMProviderSettingsForced = false;
776
777 String vmNum = null;
778 if (resultCode != RESULT_OK) {
779 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
780 failure = true;
781 } else {
782 if (data == null) {
783 if (DBG) log("onActivityResult: vm provider cfg result has no data");
784 failure = true;
785 } else {
786 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
787 if (DBG) log("Provider requested signout");
788 if (isVMProviderSettingsForced) {
789 if (DBG) log("Going back to previous provider on signout");
790 switchToPreviousVoicemailProvider();
791 } else {
792 final String victim = getCurrentVoicemailProviderKey();
793 if (DBG) log("Relaunching activity and ignoring " + victim);
794 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
795 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
796 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
797 this.startActivity(i);
798 }
799 return;
800 }
801 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
802 if (vmNum == null || vmNum.length() == 0) {
803 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
804 failure = true;
805 }
806 }
807 }
808 if (failure) {
809 if (DBG) log("Failure in return from voicemail provider");
810 if (isVMProviderSettingsForced) {
811 switchToPreviousVoicemailProvider();
812 } else {
813 if (DBG) log("Not switching back the provider since this is not forced config");
814 }
815 return;
816 }
817 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
818 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
819
Santos Cordonda120f42014-08-06 04:44:34 -0700820 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700821 // send it to the provider when it's config is invoked so it can use this as default
822 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
823
824 if (DBG) log("onActivityResult: vm provider cfg result " +
825 (fwdNum != null ? "has" : " does not have") + " forwarding number");
826 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(),
827 new VoiceMailProviderSettings(vmNum, fwdNum, fwdNumTime));
828 return;
829 }
830
831 if (requestCode == VOICEMAIL_PREF_ID) {
832 if (resultCode != RESULT_OK) {
833 if (DBG) log("onActivityResult: contact picker result not OK.");
834 return;
835 }
836
837 Cursor cursor = null;
838 try {
839 cursor = getContentResolver().query(data.getData(),
840 NUM_PROJECTION, null, null, null);
841 if ((cursor == null) || (!cursor.moveToFirst())) {
842 if (DBG) log("onActivityResult: bad contact data, no results found.");
843 return;
844 }
845 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
846 return;
847 } finally {
848 if (cursor != null) {
849 cursor.close();
850 }
851 }
852 }
853
854 super.onActivityResult(requestCode, resultCode, data);
855 }
856
857 // Voicemail button logic
858 private void handleVMBtnClickRequest() {
859 // normally called on the dialog close.
860
861 // Since we're stripping the formatting out on the getPhoneNumber()
862 // call now, we won't need to do so here anymore.
863
864 saveVoiceMailAndForwardingNumber(
865 getCurrentVoicemailProviderKey(),
866 new VoiceMailProviderSettings(mSubMenuVoicemailSettings.getPhoneNumber(),
Tyler Gunn4d45d1c2014-09-12 22:17:53 -0700867 FWD_SETTINGS_DONT_TOUCH)
868 );
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700869 }
870
871
872 /**
873 * Wrapper around showDialog() that will silently do nothing if we're
874 * not in the foreground.
875 *
876 * This is useful here because most of the dialogs we display from
877 * this class are triggered by asynchronous events (like
878 * success/failure messages from the telephony layer) and it's
879 * possible for those events to come in even after the user has gone
880 * to a different screen.
881 */
882 // TODO: this is too brittle: it's still easy to accidentally add new
883 // code here that calls showDialog() directly (which will result in a
884 // WindowManager$BadTokenException if called after the activity has
885 // been stopped.)
886 //
887 // It would be cleaner to do the "if (mForeground)" check in one
888 // central place, maybe by using a single Handler for all asynchronous
889 // events (and have *that* discard events if we're not in the
890 // foreground.)
891 //
892 // Unfortunately it's not that simple, since we sometimes need to do
893 // actual work to handle these events whether or not we're in the
894 // foreground (see the Handler code in mSetOptionComplete for
895 // example.)
896 private void showDialogIfForeground(int id) {
897 if (mForeground) {
898 showDialog(id);
899 }
900 }
901
902 private void dismissDialogSafely(int id) {
903 try {
904 dismissDialog(id);
905 } catch (IllegalArgumentException e) {
906 // This is expected in the case where we were in the background
907 // at the time we would normally have shown the dialog, so we didn't
908 // show it.
909 }
910 }
911
912 private void saveVoiceMailAndForwardingNumber(String key,
913 VoiceMailProviderSettings newSettings) {
914 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
915 mNewVMNumber = newSettings.voicemailNumber;
916 // empty vm number == clearing the vm number ?
917 if (mNewVMNumber == null) {
918 mNewVMNumber = "";
919 }
920
921 mNewFwdSettings = newSettings.forwardingSettings;
922 if (DBG) log("newFwdNumber " +
923 String.valueOf((mNewFwdSettings != null ? mNewFwdSettings.length : 0))
924 + " settings");
925
926 // No fwd settings on CDMA
927 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
928 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
929 mNewFwdSettings = FWD_SETTINGS_DONT_TOUCH;
930 }
931
932 //throw a warning if the vm is the same and we do not touch forwarding.
933 if (mNewVMNumber.equals(mOldVmNumber) && mNewFwdSettings == FWD_SETTINGS_DONT_TOUCH) {
934 showVMDialog(MSG_VM_NOCHANGE);
935 return;
936 }
937
938 maybeSaveSettingsForVoicemailProvider(key, newSettings);
939 mVMChangeCompletedSuccessfully = false;
940 mFwdChangesRequireRollback = false;
941 mVMOrFwdSetError = 0;
942 if (!key.equals(mPreviousVMProviderKey)) {
943 mReadingSettingsForDefaultProvider =
944 mPreviousVMProviderKey.equals(DEFAULT_VM_PROVIDER_KEY);
945 if (DBG) log("Reading current forwarding settings");
946 mForwardingReadResults = new CallForwardInfo[FORWARDING_SETTINGS_REASONS.length];
947 for (int i = 0; i < FORWARDING_SETTINGS_REASONS.length; i++) {
948 mForwardingReadResults[i] = null;
949 mPhone.getCallForwardingOption(FORWARDING_SETTINGS_REASONS[i],
950 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
951 }
952 showDialogIfForeground(VOICEMAIL_FWD_READING_DIALOG);
953 } else {
954 saveVoiceMailAndForwardingNumberStage2();
955 }
956 }
957
958 private final Handler mGetOptionComplete = new Handler() {
959 @Override
960 public void handleMessage(Message msg) {
961 AsyncResult result = (AsyncResult) msg.obj;
962 switch (msg.what) {
963 case EVENT_FORWARDING_GET_COMPLETED:
964 handleForwardingSettingsReadResult(result, msg.arg1);
965 break;
966 }
967 }
968 };
969
970 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
971 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
972 Throwable error = null;
973 if (ar.exception != null) {
974 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" +
975 ar.exception.getMessage());
976 error = ar.exception;
977 }
978 if (ar.userObj instanceof Throwable) {
979 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" +
980 ((Throwable)ar.userObj).getMessage());
981 error = (Throwable)ar.userObj;
982 }
983
984 // We may have already gotten an error and decided to ignore the other results.
985 if (mForwardingReadResults == null) {
986 if (DBG) Log.d(LOG_TAG, "ignoring fwd reading result: " + idx);
987 return;
988 }
989
990 // In case of error ignore other results, show an error dialog
991 if (error != null) {
992 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
993 mForwardingReadResults = null;
994 dismissDialogSafely(VOICEMAIL_FWD_READING_DIALOG);
995 showVMDialog(MSG_FW_GET_EXCEPTION);
996 return;
997 }
998
999 // Get the forwarding info
1000 final CallForwardInfo cfInfoArray[] = (CallForwardInfo[]) ar.result;
1001 CallForwardInfo fi = null;
1002 for (int i = 0 ; i < cfInfoArray.length; i++) {
1003 if ((cfInfoArray[i].serviceClass & CommandsInterface.SERVICE_CLASS_VOICE) != 0) {
1004 fi = cfInfoArray[i];
1005 break;
1006 }
1007 }
1008 if (fi == null) {
1009
1010 // In case we go nothing it means we need this reason disabled
1011 // so create a CallForwardInfo for capturing this
1012 if (DBG) Log.d(LOG_TAG, "Creating default info for " + idx);
1013 fi = new CallForwardInfo();
1014 fi.status = 0;
1015 fi.reason = FORWARDING_SETTINGS_REASONS[idx];
1016 fi.serviceClass = CommandsInterface.SERVICE_CLASS_VOICE;
1017 } else {
1018 // if there is not a forwarding number, ensure the entry is set to "not active."
1019 if (fi.number == null || fi.number.length() == 0) {
1020 fi.status = 0;
1021 }
1022
1023 if (DBG) Log.d(LOG_TAG, "Got " + fi.toString() + " for " + idx);
1024 }
1025 mForwardingReadResults[idx] = fi;
1026
1027 // Check if we got all the results already
1028 boolean done = true;
1029 for (int i = 0; i < mForwardingReadResults.length; i++) {
1030 if (mForwardingReadResults[i] == null) {
1031 done = false;
1032 break;
1033 }
1034 }
1035 if (done) {
1036 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
1037 dismissDialogSafely(VOICEMAIL_FWD_READING_DIALOG);
1038 if (mReadingSettingsForDefaultProvider) {
1039 maybeSaveSettingsForVoicemailProvider(DEFAULT_VM_PROVIDER_KEY,
1040 new VoiceMailProviderSettings(this.mOldVmNumber,
1041 mForwardingReadResults));
1042 mReadingSettingsForDefaultProvider = false;
1043 }
1044 saveVoiceMailAndForwardingNumberStage2();
1045 } else {
1046 if (DBG) Log.d(LOG_TAG, "Not done receiving fwd info");
1047 }
1048 }
1049
1050 private CallForwardInfo infoForReason(CallForwardInfo[] infos, int reason) {
1051 CallForwardInfo result = null;
1052 if (null != infos) {
1053 for (CallForwardInfo info : infos) {
1054 if (info.reason == reason) {
1055 result = info;
1056 break;
1057 }
1058 }
1059 }
1060 return result;
1061 }
1062
1063 private boolean isUpdateRequired(CallForwardInfo oldInfo,
1064 CallForwardInfo newInfo) {
1065 boolean result = true;
1066 if (0 == newInfo.status) {
1067 // If we're disabling a type of forwarding, and it's already
1068 // disabled for the account, don't make any change
1069 if (oldInfo != null && oldInfo.status == 0) {
1070 result = false;
1071 }
1072 }
1073 return result;
1074 }
1075
1076 private void resetForwardingChangeState() {
1077 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
1078 mExpectedChangeResultReasons = new HashSet<Integer>();
1079 }
1080
1081 // Called after we are done saving the previous forwarding settings if
1082 // we needed.
1083 private void saveVoiceMailAndForwardingNumberStage2() {
1084 mForwardingChangeResults = null;
1085 mVoicemailChangeResult = null;
1086 if (mNewFwdSettings != FWD_SETTINGS_DONT_TOUCH) {
1087 resetForwardingChangeState();
1088 for (int i = 0; i < mNewFwdSettings.length; i++) {
1089 CallForwardInfo fi = mNewFwdSettings[i];
1090
1091 final boolean doUpdate = isUpdateRequired(infoForReason(
1092 mForwardingReadResults, fi.reason), fi);
1093
1094 if (doUpdate) {
1095 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
1096 mExpectedChangeResultReasons.add(i);
1097
1098 mPhone.setCallForwardingOption(
1099 fi.status == 1 ?
1100 CommandsInterface.CF_ACTION_REGISTRATION :
1101 CommandsInterface.CF_ACTION_DISABLE,
1102 fi.reason,
1103 fi.number,
1104 fi.timeSeconds,
1105 mSetOptionComplete.obtainMessage(
1106 EVENT_FORWARDING_CHANGED, fi.reason, 0));
1107 }
1108 }
1109 showDialogIfForeground(VOICEMAIL_FWD_SAVING_DIALOG);
1110 } else {
1111 if (DBG) log("Not touching fwd #");
1112 setVMNumberWithCarrier();
1113 }
1114 }
1115
1116 private void setVMNumberWithCarrier() {
1117 if (DBG) log("save voicemail #: " + mNewVMNumber);
1118 mPhone.setVoiceMailNumber(
1119 mPhone.getVoiceMailAlphaTag().toString(),
1120 mNewVMNumber,
1121 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
1122 }
1123
1124 /**
1125 * Callback to handle option update completions
1126 */
1127 private final Handler mSetOptionComplete = new Handler() {
1128 @Override
1129 public void handleMessage(Message msg) {
1130 AsyncResult result = (AsyncResult) msg.obj;
1131 boolean done = false;
1132 switch (msg.what) {
1133 case EVENT_VOICEMAIL_CHANGED:
1134 mVoicemailChangeResult = result;
1135 mVMChangeCompletedSuccessfully = checkVMChangeSuccess() == null;
1136 if (DBG) log("VM change complete msg, VM change done = " +
1137 String.valueOf(mVMChangeCompletedSuccessfully));
1138 done = true;
1139 break;
1140 case EVENT_FORWARDING_CHANGED:
1141 mForwardingChangeResults.put(msg.arg1, result);
1142 if (result.exception != null) {
1143 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
1144 result.exception.getMessage());
1145 } else {
1146 if (DBG) log("Success in setting fwd# " + msg.arg1);
1147 }
1148 final boolean completed = checkForwardingCompleted();
1149 if (completed) {
1150 if (checkFwdChangeSuccess() == null) {
1151 if (DBG) log("Overall fwd changes completed ok, starting vm change");
1152 setVMNumberWithCarrier();
1153 } else {
1154 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
1155 "Check if we need to try rollback for some settings.");
1156 mFwdChangesRequireRollback = false;
1157 Iterator<Map.Entry<Integer,AsyncResult>> it =
1158 mForwardingChangeResults.entrySet().iterator();
1159 while (it.hasNext()) {
1160 Map.Entry<Integer,AsyncResult> entry = it.next();
1161 if (entry.getValue().exception == null) {
1162 // If at least one succeeded we have to revert
1163 Log.i(LOG_TAG, "Rollback will be required");
1164 mFwdChangesRequireRollback = true;
1165 break;
1166 }
1167 }
1168 if (!mFwdChangesRequireRollback) {
1169 Log.i(LOG_TAG, "No rollback needed.");
1170 }
1171 done = true;
1172 }
1173 }
1174 break;
1175 default:
1176 // TODO: should never reach this, may want to throw exception
1177 }
1178 if (done) {
1179 if (DBG) log("All VM provider related changes done");
1180 if (mForwardingChangeResults != null) {
1181 dismissDialogSafely(VOICEMAIL_FWD_SAVING_DIALOG);
1182 }
1183 handleSetVMOrFwdMessage();
1184 }
1185 }
1186 };
1187
1188 /**
1189 * Callback to handle option revert completions
1190 */
1191 private final Handler mRevertOptionComplete = new Handler() {
1192 @Override
1193 public void handleMessage(Message msg) {
1194 AsyncResult result = (AsyncResult) msg.obj;
1195 switch (msg.what) {
1196 case EVENT_VOICEMAIL_CHANGED:
1197 mVoicemailChangeResult = result;
1198 if (DBG) log("VM revert complete msg");
1199 break;
1200 case EVENT_FORWARDING_CHANGED:
1201 mForwardingChangeResults.put(msg.arg1, result);
1202 if (result.exception != null) {
1203 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
1204 result.exception.getMessage());
1205 } else {
1206 if (DBG) log("Success in reverting fwd# " + msg.arg1);
1207 }
1208 if (DBG) log("FWD revert complete msg ");
1209 break;
1210 default:
1211 // TODO: should never reach this, may want to throw exception
1212 }
1213 final boolean done =
1214 (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null) &&
1215 (!mFwdChangesRequireRollback || checkForwardingCompleted());
1216 if (done) {
1217 if (DBG) log("All VM reverts done");
1218 dismissDialogSafely(VOICEMAIL_REVERTING_DIALOG);
1219 onRevertDone();
1220 }
1221 }
1222 };
1223
1224 /**
1225 * @return true if forwarding change has completed
1226 */
1227 private boolean checkForwardingCompleted() {
1228 boolean result;
1229 if (mForwardingChangeResults == null) {
1230 result = true;
1231 } else {
1232 // return true iff there is a change result for every reason for
1233 // which we expected a result
1234 result = true;
1235 for (Integer reason : mExpectedChangeResultReasons) {
1236 if (mForwardingChangeResults.get(reason) == null) {
1237 result = false;
1238 break;
1239 }
1240 }
1241 }
1242 return result;
1243 }
1244 /**
1245 * @return error string or null if successful
1246 */
1247 private String checkFwdChangeSuccess() {
1248 String result = null;
1249 Iterator<Map.Entry<Integer,AsyncResult>> it =
1250 mForwardingChangeResults.entrySet().iterator();
1251 while (it.hasNext()) {
1252 Map.Entry<Integer,AsyncResult> entry = it.next();
1253 Throwable exception = entry.getValue().exception;
1254 if (exception != null) {
1255 result = exception.getMessage();
1256 if (result == null) {
1257 result = "";
1258 }
1259 break;
1260 }
1261 }
1262 return result;
1263 }
1264
1265 /**
1266 * @return error string or null if successful
1267 */
1268 private String checkVMChangeSuccess() {
1269 if (mVoicemailChangeResult.exception != null) {
1270 final String msg = mVoicemailChangeResult.exception.getMessage();
1271 if (msg == null) {
1272 return "";
1273 }
1274 return msg;
1275 }
1276 return null;
1277 }
1278
1279 private void handleSetVMOrFwdMessage() {
1280 if (DBG) {
1281 log("handleSetVMMessage: set VM request complete");
1282 }
1283 boolean success = true;
1284 boolean fwdFailure = false;
1285 String exceptionMessage = "";
1286 if (mForwardingChangeResults != null) {
1287 exceptionMessage = checkFwdChangeSuccess();
1288 if (exceptionMessage != null) {
1289 success = false;
1290 fwdFailure = true;
1291 }
1292 }
1293 if (success) {
1294 exceptionMessage = checkVMChangeSuccess();
1295 if (exceptionMessage != null) {
1296 success = false;
1297 }
1298 }
1299 if (success) {
1300 if (DBG) log("change VM success!");
1301 handleVMAndFwdSetSuccess(MSG_VM_OK);
1302 } else {
1303 if (fwdFailure) {
1304 Log.w(LOG_TAG, "Failed to change fowarding setting. Reason: " + exceptionMessage);
1305 handleVMOrFwdSetError(MSG_FW_SET_EXCEPTION);
1306 } else {
1307 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + exceptionMessage);
1308 handleVMOrFwdSetError(MSG_VM_EXCEPTION);
1309 }
1310 }
1311 }
1312
1313 /**
1314 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
1315 * changes to those settings and show "failure" dialog.
1316 *
1317 * @param msgId Message ID used for the specific error case. {@link #MSG_FW_SET_EXCEPTION} or
1318 * {@link #MSG_VM_EXCEPTION}
1319 */
1320 private void handleVMOrFwdSetError(int msgId) {
1321 if (mChangingVMorFwdDueToProviderChange) {
1322 mVMOrFwdSetError = msgId;
1323 mChangingVMorFwdDueToProviderChange = false;
1324 switchToPreviousVoicemailProvider();
1325 return;
1326 }
1327 mChangingVMorFwdDueToProviderChange = false;
1328 showVMDialog(msgId);
1329 updateVoiceNumberField();
1330 }
1331
1332 /**
1333 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1334 * This updates a bunch of variables and show "success" dialog.
1335 */
1336 private void handleVMAndFwdSetSuccess(int msg) {
1337 if (DBG) {
1338 log("handleVMAndFwdSetSuccess(). current voicemail provider key: "
1339 + getCurrentVoicemailProviderKey());
1340 }
1341 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1342 mChangingVMorFwdDueToProviderChange = false;
1343 showVMDialog(msg);
1344 updateVoiceNumberField();
1345 }
1346
1347 /**
1348 * Update the voicemail number from what we've recorded on the sim.
1349 */
1350 private void updateVoiceNumberField() {
1351 if (DBG) {
1352 log("updateVoiceNumberField(). mSubMenuVoicemailSettings=" + mSubMenuVoicemailSettings);
1353 }
1354 if (mSubMenuVoicemailSettings == null) {
1355 return;
1356 }
1357
1358 mOldVmNumber = mPhone.getVoiceMailNumber();
1359 if (mOldVmNumber == null) {
1360 mOldVmNumber = "";
1361 }
1362 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1363 final String summary = (mOldVmNumber.length() > 0) ? mOldVmNumber :
1364 getString(R.string.voicemail_number_not_set);
1365 mSubMenuVoicemailSettings.setSummary(summary);
1366 }
1367
1368 /*
1369 * Helper Methods for Activity class.
1370 * The initial query commands are split into two pieces now
1371 * for individual expansion. This combined with the ability
1372 * to cancel queries allows for a much better user experience,
1373 * and also ensures that the user only waits to update the
1374 * data that is relevant.
1375 */
1376
1377 @Override
1378 protected void onPrepareDialog(int id, Dialog dialog) {
1379 super.onPrepareDialog(id, dialog);
1380 mCurrentDialogId = id;
1381 }
1382
1383 // dialog creation method, called by showDialog()
1384 @Override
1385 protected Dialog onCreateDialog(int id) {
1386 if ((id == VM_RESPONSE_ERROR) || (id == VM_NOCHANGE_ERROR) ||
1387 (id == FW_SET_RESPONSE_ERROR) || (id == FW_GET_RESPONSE_ERROR) ||
1388 (id == VOICEMAIL_DIALOG_CONFIRM)) {
1389
1390 AlertDialog.Builder b = new AlertDialog.Builder(this);
1391
1392 int msgId;
1393 int titleId = R.string.error_updating_title;
1394 switch (id) {
1395 case VOICEMAIL_DIALOG_CONFIRM:
1396 msgId = R.string.vm_changed;
1397 titleId = R.string.voicemail;
1398 // Set Button 2
1399 b.setNegativeButton(R.string.close_dialog, this);
1400 break;
1401 case VM_NOCHANGE_ERROR:
1402 // even though this is technically an error,
1403 // keep the title friendly.
1404 msgId = R.string.no_change;
1405 titleId = R.string.voicemail;
1406 // Set Button 2
1407 b.setNegativeButton(R.string.close_dialog, this);
1408 break;
1409 case VM_RESPONSE_ERROR:
1410 msgId = R.string.vm_change_failed;
1411 // Set Button 1
1412 b.setPositiveButton(R.string.close_dialog, this);
1413 break;
1414 case FW_SET_RESPONSE_ERROR:
1415 msgId = R.string.fw_change_failed;
1416 // Set Button 1
1417 b.setPositiveButton(R.string.close_dialog, this);
1418 break;
1419 case FW_GET_RESPONSE_ERROR:
1420 msgId = R.string.fw_get_in_vm_failed;
1421 b.setPositiveButton(R.string.alert_dialog_yes, this);
1422 b.setNegativeButton(R.string.alert_dialog_no, this);
1423 break;
1424 default:
1425 msgId = R.string.exception_error;
1426 // Set Button 3, tells the activity that the error is
1427 // not recoverable on dialog exit.
1428 b.setNeutralButton(R.string.close_dialog, this);
1429 break;
1430 }
1431
1432 b.setTitle(getText(titleId));
1433 String message = getText(msgId).toString();
1434 b.setMessage(message);
1435 b.setCancelable(false);
1436 AlertDialog dialog = b.create();
1437
1438 // make the dialog more obvious by bluring the background.
1439 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
1440
1441 return dialog;
1442 } else if (id == VOICEMAIL_FWD_SAVING_DIALOG || id == VOICEMAIL_FWD_READING_DIALOG ||
1443 id == VOICEMAIL_REVERTING_DIALOG) {
1444 ProgressDialog dialog = new ProgressDialog(this);
1445 dialog.setTitle(getText(R.string.updating_title));
1446 dialog.setIndeterminate(true);
1447 dialog.setCancelable(false);
1448 dialog.setMessage(getText(
1449 id == VOICEMAIL_FWD_SAVING_DIALOG ? R.string.updating_settings :
1450 (id == VOICEMAIL_REVERTING_DIALOG ? R.string.reverting_settings :
1451 R.string.reading_settings)));
1452 return dialog;
1453 }
1454
1455
1456 return null;
1457 }
1458
1459 // This is a method implemented for DialogInterface.OnClickListener.
1460 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1461 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1462 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1463 public void onClick(DialogInterface dialog, int which) {
1464 dialog.dismiss();
1465 switch (which){
1466 case DialogInterface.BUTTON_NEUTRAL:
1467 if (DBG) log("Neutral button");
1468 break;
1469 case DialogInterface.BUTTON_NEGATIVE:
1470 if (DBG) log("Negative button");
1471 if (mCurrentDialogId == FW_GET_RESPONSE_ERROR) {
1472 // We failed to get current forwarding settings and the user
1473 // does not wish to continue.
1474 switchToPreviousVoicemailProvider();
1475 }
1476 break;
1477 case DialogInterface.BUTTON_POSITIVE:
1478 if (DBG) log("Positive button");
1479 if (mCurrentDialogId == FW_GET_RESPONSE_ERROR) {
1480 // We failed to get current forwarding settings but the user
1481 // wishes to continue changing settings to the new vm provider
1482 saveVoiceMailAndForwardingNumberStage2();
1483 } else {
1484 finish();
1485 }
1486 return;
1487 default:
1488 // just let the dialog close and go back to the input
1489 }
1490 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1491 // with settings UI. If we were called to explicitly configure voice mail then
1492 // we finish the settings activity here to come back to whatever the user was doing.
1493 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1494 finish();
1495 }
1496 }
1497
1498 // set the app state with optional status.
1499 private void showVMDialog(int msgStatus) {
1500 switch (msgStatus) {
1501 // It's a bit worrisome to punt in the error cases here when we're
1502 // not in the foreground; maybe toast instead?
1503 case MSG_VM_EXCEPTION:
1504 showDialogIfForeground(VM_RESPONSE_ERROR);
1505 break;
1506 case MSG_FW_SET_EXCEPTION:
1507 showDialogIfForeground(FW_SET_RESPONSE_ERROR);
1508 break;
1509 case MSG_FW_GET_EXCEPTION:
1510 showDialogIfForeground(FW_GET_RESPONSE_ERROR);
1511 break;
1512 case MSG_VM_NOCHANGE:
1513 showDialogIfForeground(VM_NOCHANGE_ERROR);
1514 break;
1515 case MSG_VM_OK:
1516 showDialogIfForeground(VOICEMAIL_DIALOG_CONFIRM);
1517 break;
1518 case MSG_OK:
1519 default:
1520 // This should never happen.
1521 }
1522 }
1523
1524 /*
1525 * Activity class methods
1526 */
1527
1528 @Override
1529 protected void onCreate(Bundle icicle) {
1530 super.onCreate(icicle);
1531 if (DBG) log("onCreate(). Intent: " + getIntent());
1532 mPhone = PhoneGlobals.getPhone();
Tyler Gunnbaee2952014-09-10 16:01:02 -07001533 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
1534
1535 // create intent to bring up contact list
1536 mContactListIntent = new Intent(Intent.ACTION_GET_CONTENT);
1537 mContactListIntent.setType(android.provider.Contacts.Phones.CONTENT_ITEM_TYPE);
1538
1539 mVoicemailRingtoneLookupRunnable = new Runnable() {
1540 @Override
1541 public void run() {
1542 if (mVoicemailNotificationRingtone != null) {
1543 SettingsUtil.updateRingtoneName(
1544 mPhone.getContext(),
1545 mVoicemailRingtoneLookupComplete,
1546 RingtoneManager.TYPE_NOTIFICATION,
1547 mVoicemailNotificationRingtone,
1548 MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY);
1549 }
1550 }
1551 };
1552
1553 ActionBar actionBar = getActionBar();
1554 if (actionBar != null) {
1555 // android.R.id.home will be triggered in onOptionsItemSelected()
1556 actionBar.setDisplayShowHomeEnabled(true);
1557 actionBar.setDisplayHomeAsUpEnabled(true);
1558 actionBar.setDisplayShowTitleEnabled(true);
1559 }
1560
1561 // Show the voicemail preference in onResume if the calling intent specifies the
1562 // ACTION_ADD_VOICEMAIL action.
1563 mShowVoicemailPreference = (icicle == null) &&
1564 getIntent().getAction().equals(ACTION_ADD_VOICEMAIL);
1565 }
1566
1567 private void initPhoneAccountPreferences() {
1568 mDefaultOutgoingAccount = (AccountSelectionPreference)
1569 findPreference(DEFAULT_OUTGOING_ACCOUNT_KEY);
1570 mSimCallManagerAccount = (AccountSelectionPreference)
1571 findPreference(WIFI_CALL_MANAGER_ACCOUNT_KEY);
1572
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001573 TelecomManager telecomManager = TelecomManager.from(this);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001574
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001575 int allPhoneAccountsCount = telecomManager.getAllPhoneAccountsCount();
Nancy Chen90f2ffd2014-09-15 17:58:14 -07001576 // Show the phone accounts preference if there are is more than one phone account
Tyler Gunnbaee2952014-09-10 16:01:02 -07001577 if (allPhoneAccountsCount > 1) {
1578 populateDefaultOutgoingAccountsModel();
1579
1580 mDefaultOutgoingAccount.setListener(this);
1581 } else {
1582 getPreferenceScreen().removePreference(mDefaultOutgoingAccount);
1583 }
1584
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001585 List<PhoneAccountHandle> simCallManagers = telecomManager.getSimCallManagers();
Tyler Gunnbaee2952014-09-10 16:01:02 -07001586 if (!simCallManagers.isEmpty()) {
1587 populateSimCallManagerAccountsModel();
1588 mSimCallManagerAccount.setListener(this);
1589 } else {
1590 getPreferenceScreen().removePreference(mSimCallManagerAccount);
1591 }
1592 }
1593
1594 private void createSipCallSettings() {
1595 // Add Internet call settings.
1596 if (SipUtil.isVoipSupported(this)) {
1597 mSipManager = SipManager.newInstance(this);
1598 mSipSharedPreferences = new SipSharedPreferences(this);
1599 addPreferencesFromResource(
1600 com.android.services.telephony.sip.R.xml.sip_settings_category);
1601 mButtonSipCallOptions = getSipCallOptionPreference();
1602 mButtonSipCallOptions.setOnPreferenceChangeListener(this);
1603 mButtonSipCallOptions.setValueIndex(
1604 mButtonSipCallOptions.findIndexOfValue(
1605 mSipSharedPreferences.getSipCallOption()));
1606 mButtonSipCallOptions.setSummary(mButtonSipCallOptions.getEntry());
1607 }
1608 }
1609
1610 private boolean canLaunchIntent(Intent intent) {
1611 PackageManager pm = getPackageManager();
1612 return pm.resolveActivity(intent, PackageManager.GET_ACTIVITIES) != null;
1613 }
1614
1615 // Gets the call options for SIP depending on whether SIP is allowed only
1616 // on Wi-Fi only; also make the other options preference invisible.
1617 private ListPreference getSipCallOptionPreference() {
1618 ListPreference wifiAnd3G = (ListPreference)
1619 findPreference(BUTTON_SIP_CALL_OPTIONS);
1620 ListPreference wifiOnly = (ListPreference)
1621 findPreference(BUTTON_SIP_CALL_OPTIONS_WIFI_ONLY);
1622 PreferenceScreen sipSettings = (PreferenceScreen)
1623 getPreferenceScreen().findPreference(SIP_SETTINGS_PREFERENCE_SCREEN_KEY);
1624 if (SipManager.isSipWifiOnly(this)) {
1625 sipSettings.removePreference(wifiAnd3G);
1626 return wifiOnly;
1627 } else {
1628 sipSettings.removePreference(wifiOnly);
1629 return wifiAnd3G;
1630 }
1631 }
1632
1633 @Override
1634 protected void onResume() {
1635 super.onResume();
1636 mForeground = true;
1637
1638 PreferenceScreen preferenceScreen = getPreferenceScreen();
1639 if (preferenceScreen != null) {
1640 preferenceScreen.removeAll();
1641 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001642
1643 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Leedb2fe562014-09-03 15:40:43 -07001644 initPhoneAccountPreferences();
1645
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001646 // get buttons
1647 PreferenceScreen prefSet = getPreferenceScreen();
1648 mSubMenuVoicemailSettings = (EditPhoneNumberPreference)findPreference(BUTTON_VOICEMAIL_KEY);
1649 if (mSubMenuVoicemailSettings != null) {
1650 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1651 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1652 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
1653 }
1654
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001655 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1656 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1657 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
1658 mButtonTTY = (ListPreference) findPreference(BUTTON_TTY_KEY);
1659 mVoicemailProviders = (ListPreference) findPreference(BUTTON_VOICEMAIL_PROVIDER_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001660
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001661 if (mVoicemailProviders != null) {
1662 mVoicemailProviders.setOnPreferenceChangeListener(this);
1663 mVoicemailSettings = (PreferenceScreen)findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
1664 mVoicemailNotificationRingtone =
1665 findPreference(BUTTON_VOICEMAIL_NOTIFICATION_RINGTONE_KEY);
1666 mVoicemailNotificationVibrate =
1667 (CheckBoxPreference) findPreference(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY);
1668 initVoiceMailProviders();
1669 }
1670
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001671
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001672 if (mButtonDTMF != null) {
1673 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1674 mButtonDTMF.setOnPreferenceChangeListener(this);
1675 } else {
1676 prefSet.removePreference(mButtonDTMF);
1677 mButtonDTMF = null;
1678 }
1679 }
1680
1681 if (mButtonAutoRetry != null) {
1682 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1683 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1684 } else {
1685 prefSet.removePreference(mButtonAutoRetry);
1686 mButtonAutoRetry = null;
1687 }
1688 }
1689
1690 if (mButtonHAC != null) {
1691 if (getResources().getBoolean(R.bool.hac_enabled)) {
1692
1693 mButtonHAC.setOnPreferenceChangeListener(this);
1694 } else {
1695 prefSet.removePreference(mButtonHAC);
1696 mButtonHAC = null;
1697 }
1698 }
1699
1700 if (mButtonTTY != null) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001701 TelecomManager telecomManager = TelecomManager.from(this);
1702 if (telecomManager != null && telecomManager.isTtySupported()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001703 mButtonTTY.setOnPreferenceChangeListener(this);
1704 } else {
1705 prefSet.removePreference(mButtonTTY);
1706 mButtonTTY = null;
1707 }
1708 }
1709
1710 if (!getResources().getBoolean(R.bool.world_phone)) {
1711 Preference options = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
Andrew Lee2170a972014-08-13 18:13:01 -07001712 if (options != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001713 prefSet.removePreference(options);
Andrew Lee2170a972014-08-13 18:13:01 -07001714 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001715 options = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
Andrew Lee2170a972014-08-13 18:13:01 -07001716 if (options != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001717 prefSet.removePreference(options);
Andrew Lee2170a972014-08-13 18:13:01 -07001718 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001719
1720 int phoneType = mPhone.getPhoneType();
1721 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1722 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001723 if (fdnButton != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001724 prefSet.removePreference(fdnButton);
Andrew Lee2170a972014-08-13 18:13:01 -07001725 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001726 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1727 addPreferencesFromResource(R.xml.cdma_call_privacy);
1728 }
1729 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
Andrew Lee2170a972014-08-13 18:13:01 -07001730 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
Etan Cohen0ca1c802014-07-07 15:35:48 -07001731 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1732 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001733 } else {
1734 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1735 }
1736 }
1737
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001738 // check the intent that started this activity and pop up the voicemail
1739 // dialog if we've been asked to.
1740 // If we have at least one non default VM provider registered then bring up
1741 // the selection for the VM provider, otherwise bring up a VM number dialog.
1742 // We only bring up the dialog the first time we are called (not after orientation change)
Tyler Gunnbaee2952014-09-10 16:01:02 -07001743 if (mShowVoicemailPreference && mVoicemailProviders != null) {
1744 if (DBG) {
1745 log("ACTION_ADD_VOICEMAIL Intent is thrown. current VM data size: "
1746 + mVMProvidersData.size());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001747 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001748 if (mVMProvidersData.size() > 1) {
1749 simulatePreferenceClick(mVoicemailProviders);
1750 } else {
1751 onPreferenceChange(mVoicemailProviders, DEFAULT_VM_PROVIDER_KEY);
1752 mVoicemailProviders.setValue(DEFAULT_VM_PROVIDER_KEY);
1753 }
1754 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001755 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001756
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001757 updateVoiceNumberField();
1758 mVMProviderSettingsForced = false;
1759 createSipCallSettings();
1760
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001761 if (isAirplaneModeOn()) {
Andrew Lee2170a972014-08-13 18:13:01 -07001762 Preference sipSettings = findPreference(SIP_SETTINGS_PREFERENCE_SCREEN_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001763 PreferenceScreen screen = getPreferenceScreen();
1764 int count = screen.getPreferenceCount();
1765 for (int i = 0 ; i < count ; ++i) {
1766 Preference pref = screen.getPreference(i);
1767 if (pref != sipSettings) pref.setEnabled(false);
1768 }
1769 return;
1770 }
1771
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001772 if (mButtonDTMF != null) {
1773 int dtmf = Settings.System.getInt(getContentResolver(),
1774 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1775 mButtonDTMF.setValueIndex(dtmf);
1776 }
1777
1778 if (mButtonAutoRetry != null) {
1779 int autoretry = Settings.Global.getInt(getContentResolver(),
1780 Settings.Global.CALL_AUTO_RETRY, 0);
1781 mButtonAutoRetry.setChecked(autoretry != 0);
1782 }
1783
1784 if (mButtonHAC != null) {
1785 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1786 mButtonHAC.setChecked(hac != 0);
1787 }
1788
1789 if (mButtonTTY != null) {
1790 int settingsTtyMode = Settings.Secure.getInt(getContentResolver(),
1791 Settings.Secure.PREFERRED_TTY_MODE,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001792 TelecomManager.TTY_MODE_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001793 mButtonTTY.setValue(Integer.toString(settingsTtyMode));
1794 updatePreferredTtyModeSummary(settingsTtyMode);
1795 }
1796
1797 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
1798 mPhone.getContext());
1799 if (migrateVoicemailVibrationSettingsIfNeeded(prefs)) {
1800 mVoicemailNotificationVibrate.setChecked(prefs.getBoolean(
1801 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, false));
1802 }
1803
Andrew Lee2170a972014-08-13 18:13:01 -07001804 // Look up the voicemail ringtone name asynchronously and update its preference.
1805 new Thread(mVoicemailRingtoneLookupRunnable).start();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001806 }
1807
1808 // Migrate settings from BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY to
1809 // BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, if the latter does not exist.
1810 // Returns true if migration was performed.
1811 public static boolean migrateVoicemailVibrationSettingsIfNeeded(SharedPreferences prefs) {
1812 if (!prefs.contains(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY)) {
1813 String vibrateWhen = prefs.getString(
1814 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY, VOICEMAIL_VIBRATION_NEVER);
1815 // If vibrateWhen is always, then voicemailVibrate should be True.
1816 // otherwise if vibrateWhen is "only in silent mode", or "never", then
1817 // voicemailVibrate = False.
1818 boolean voicemailVibrate = vibrateWhen.equals(VOICEMAIL_VIBRATION_ALWAYS);
1819 final SharedPreferences.Editor editor = prefs.edit();
1820 editor.putBoolean(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, voicemailVibrate);
1821 editor.commit();
1822 return true;
1823 }
1824 return false;
1825 }
1826
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001827 private boolean isAirplaneModeOn() {
1828 return Settings.System.getInt(getContentResolver(),
1829 Settings.System.AIRPLANE_MODE_ON, 0) != 0;
1830 }
1831
1832 private void handleTTYChange(Preference preference, Object objValue) {
1833 int buttonTtyMode;
1834 buttonTtyMode = Integer.valueOf((String) objValue).intValue();
1835 int settingsTtyMode = android.provider.Settings.Secure.getInt(
1836 getContentResolver(),
Sailesh Nepalbf900542014-07-15 16:18:32 -07001837 android.provider.Settings.Secure.PREFERRED_TTY_MODE,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001838 TelecomManager.TTY_MODE_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001839 if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
1840 Integer.toString(buttonTtyMode));
1841
1842 if (buttonTtyMode != settingsTtyMode) {
1843 switch(buttonTtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001844 case TelecomManager.TTY_MODE_OFF:
1845 case TelecomManager.TTY_MODE_FULL:
1846 case TelecomManager.TTY_MODE_HCO:
1847 case TelecomManager.TTY_MODE_VCO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001848 android.provider.Settings.Secure.putInt(getContentResolver(),
1849 android.provider.Settings.Secure.PREFERRED_TTY_MODE, buttonTtyMode);
1850 break;
1851 default:
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001852 buttonTtyMode = TelecomManager.TTY_MODE_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001853 }
1854
1855 mButtonTTY.setValue(Integer.toString(buttonTtyMode));
1856 updatePreferredTtyModeSummary(buttonTtyMode);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001857 Intent ttyModeChanged = new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
1858 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001859 sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
1860 }
1861 }
1862
1863 private void handleSipCallOptionsChange(Object objValue) {
1864 String option = objValue.toString();
1865 mSipSharedPreferences.setSipCallOption(option);
1866 mButtonSipCallOptions.setValueIndex(
1867 mButtonSipCallOptions.findIndexOfValue(option));
1868 mButtonSipCallOptions.setSummary(mButtonSipCallOptions.getEntry());
1869 }
1870
Evan Charlton1c696832014-04-15 14:24:23 -07001871 private void handleWifiCallSettingsClick(Preference preference) {
1872 Intent intent = preference.getIntent();
1873 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1874 // TODO - Restrict to a (the?) blessed Wi-Fi calling app.
1875
1876 Bundle noAnimations = ActivityOptions.makeCustomAnimation(this, 0, 0).toBundle();
1877 startActivity(intent, noAnimations);
Ihab Awadca8ee7d2013-12-05 12:13:16 -08001878 }
1879
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001880 private void updatePreferredTtyModeSummary(int TtyMode) {
1881 String [] txts = getResources().getStringArray(R.array.tty_mode_entries);
1882 switch(TtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001883 case TelecomManager.TTY_MODE_OFF:
1884 case TelecomManager.TTY_MODE_HCO:
1885 case TelecomManager.TTY_MODE_VCO:
1886 case TelecomManager.TTY_MODE_FULL:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001887 mButtonTTY.setSummary(txts[TtyMode]);
1888 break;
1889 default:
1890 mButtonTTY.setEnabled(false);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001891 mButtonTTY.setSummary(txts[TelecomManager.TTY_MODE_OFF]);
Sailesh Nepalbf900542014-07-15 16:18:32 -07001892 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001893 }
1894 }
1895
1896 private static void log(String msg) {
1897 Log.d(LOG_TAG, msg);
1898 }
1899
1900 /**
1901 * Updates the look of the VM preference widgets based on current VM provider settings.
1902 * Note that the provider name is loaded form the found activity via loadLabel in
1903 * {@link #initVoiceMailProviders()} in order for it to be localizable.
1904 */
1905 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1906 final String key = currentProviderSetting;
1907 final VoiceMailProvider provider = mVMProvidersData.get(key);
1908
1909 /* This is the case when we are coming up on a freshly wiped phone and there is no
1910 persisted value for the list preference mVoicemailProviders.
1911 In this case we want to show the UI asking the user to select a voicemail provider as
1912 opposed to silently falling back to default one. */
1913 if (provider == null) {
1914 if (DBG) {
1915 log("updateVMPreferenceWidget: provider for the key \"" + key + "\" is null.");
1916 }
1917 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1918 mVoicemailSettings.setEnabled(false);
1919 mVoicemailSettings.setIntent(null);
1920
1921 mVoicemailNotificationVibrate.setEnabled(false);
1922 } else {
1923 if (DBG) {
1924 log("updateVMPreferenceWidget: provider for the key \"" + key + "\".."
1925 + "name: " + provider.name
1926 + ", intent: " + provider.intent);
1927 }
1928 final String providerName = provider.name;
1929 mVoicemailProviders.setSummary(providerName);
1930 mVoicemailSettings.setEnabled(true);
1931 mVoicemailSettings.setIntent(provider.intent);
1932
1933 mVoicemailNotificationVibrate.setEnabled(true);
1934 }
1935 }
1936
1937 /**
1938 * Enumerates existing VM providers and puts their data into the list and populates
1939 * the preference list objects with their names.
1940 * In case we are called with ACTION_ADD_VOICEMAIL intent the intent may have
1941 * an extra string called IGNORE_PROVIDER_EXTRA with "package.activityName" of the provider
1942 * which should be hidden when we bring up the list of possible VM providers to choose.
1943 */
1944 private void initVoiceMailProviders() {
1945 if (DBG) log("initVoiceMailProviders()");
1946 mPerProviderSavedVMNumbers =
1947 this.getApplicationContext().getSharedPreferences(
1948 VM_NUMBERS_SHARED_PREFERENCES_NAME, MODE_PRIVATE);
1949
1950 String providerToIgnore = null;
1951 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1952 if (getIntent().hasExtra(IGNORE_PROVIDER_EXTRA)) {
1953 providerToIgnore = getIntent().getStringExtra(IGNORE_PROVIDER_EXTRA);
1954 }
1955 if (DBG) log("Found ACTION_ADD_VOICEMAIL. providerToIgnore=" + providerToIgnore);
1956 if (providerToIgnore != null) {
1957 // IGNORE_PROVIDER_EXTRA implies we want to remove the choice from the list.
1958 deleteSettingsForVoicemailProvider(providerToIgnore);
1959 }
1960 }
1961
1962 mVMProvidersData.clear();
1963
1964 // Stick the default element which is always there
1965 final String myCarrier = getString(R.string.voicemail_default);
1966 mVMProvidersData.put(DEFAULT_VM_PROVIDER_KEY, new VoiceMailProvider(myCarrier, null));
1967
1968 // Enumerate providers
1969 PackageManager pm = getPackageManager();
1970 Intent intent = new Intent();
1971 intent.setAction(ACTION_CONFIGURE_VOICEMAIL);
1972 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
1973 int len = resolveInfos.size() + 1; // +1 for the default choice we will insert.
1974
1975 // Go through the list of discovered providers populating the data map
1976 // skip the provider we were instructed to ignore if there was one
1977 for (int i = 0; i < resolveInfos.size(); i++) {
1978 final ResolveInfo ri= resolveInfos.get(i);
1979 final ActivityInfo currentActivityInfo = ri.activityInfo;
1980 final String key = makeKeyForActivity(currentActivityInfo);
1981 if (key.equals(providerToIgnore)) {
1982 if (DBG) log("Ignoring key: " + key);
1983 len--;
1984 continue;
1985 }
1986 if (DBG) log("Loading key: " + key);
1987 final String nameForDisplay = ri.loadLabel(pm).toString();
1988 Intent providerIntent = new Intent();
1989 providerIntent.setAction(ACTION_CONFIGURE_VOICEMAIL);
1990 providerIntent.setClassName(currentActivityInfo.packageName,
1991 currentActivityInfo.name);
1992 if (DBG) {
1993 log("Store loaded VoiceMailProvider. key: " + key
1994 + " -> name: " + nameForDisplay + ", intent: " + providerIntent);
1995 }
1996 mVMProvidersData.put(
1997 key,
1998 new VoiceMailProvider(nameForDisplay, providerIntent));
1999
2000 }
2001
2002 // Now we know which providers to display - create entries and values array for
2003 // the list preference
2004 String [] entries = new String [len];
2005 String [] values = new String [len];
2006 entries[0] = myCarrier;
2007 values[0] = DEFAULT_VM_PROVIDER_KEY;
2008 int entryIdx = 1;
2009 for (int i = 0; i < resolveInfos.size(); i++) {
2010 final String key = makeKeyForActivity(resolveInfos.get(i).activityInfo);
2011 if (!mVMProvidersData.containsKey(key)) {
2012 continue;
2013 }
2014 entries[entryIdx] = mVMProvidersData.get(key).name;
2015 values[entryIdx] = key;
2016 entryIdx++;
2017 }
2018
2019 // ListPreference is now updated.
2020 mVoicemailProviders.setEntries(entries);
2021 mVoicemailProviders.setEntryValues(values);
2022
2023 // Remember the current Voicemail Provider key as a "previous" key. This will be used
2024 // when we fail to update Voicemail Provider, which requires rollback.
2025 // We will update this when the VM Provider setting is successfully updated.
2026 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
2027 if (DBG) log("Set up the first mPreviousVMProviderKey: " + mPreviousVMProviderKey);
2028
2029 // Finally update the preference texts.
2030 updateVMPreferenceWidgets(mPreviousVMProviderKey);
2031 }
2032
2033 private String makeKeyForActivity(ActivityInfo ai) {
2034 return ai.name;
2035 }
2036
2037 /**
2038 * Simulates user clicking on a passed preference.
2039 * Usually needed when the preference is a dialog preference and we want to invoke
2040 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07002041 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002042 */
2043 private void simulatePreferenceClick(Preference preference) {
2044 // Go through settings until we find our setting
2045 // and then simulate a click on it to bring up the dialog
2046 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
2047 for (int idx = 0; idx < adapter.getCount(); idx++) {
2048 if (adapter.getItem(idx) == preference) {
2049 getPreferenceScreen().onItemClick(this.getListView(),
2050 null, idx, adapter.getItemId(idx));
2051 break;
2052 }
2053 }
2054 }
2055
2056 /**
2057 * Saves new VM provider settings associating them with the currently selected
2058 * provider if settings are different than the ones already stored for this
2059 * provider.
2060 * Later on these will be used when the user switches a provider.
2061 */
2062 private void maybeSaveSettingsForVoicemailProvider(String key,
2063 VoiceMailProviderSettings newSettings) {
2064 if (mVoicemailProviders == null) {
2065 return;
2066 }
2067 final VoiceMailProviderSettings curSettings = loadSettingsForVoiceMailProvider(key);
2068 if (newSettings.equals(curSettings)) {
2069 if (DBG) {
2070 log("maybeSaveSettingsForVoicemailProvider:"
2071 + " Not saving setting for " + key + " since they have not changed");
2072 }
2073 return;
2074 }
2075 if (DBG) log("Saving settings for " + key + ": " + newSettings.toString());
2076 Editor editor = mPerProviderSavedVMNumbers.edit();
2077 editor.putString(key + VM_NUMBER_TAG, newSettings.voicemailNumber);
2078 String fwdKey = key + FWD_SETTINGS_TAG;
2079 CallForwardInfo[] s = newSettings.forwardingSettings;
2080 if (s != FWD_SETTINGS_DONT_TOUCH) {
2081 editor.putInt(fwdKey + FWD_SETTINGS_LENGTH_TAG, s.length);
2082 for (int i = 0; i < s.length; i++) {
2083 final String settingKey = fwdKey + FWD_SETTING_TAG + String.valueOf(i);
2084 final CallForwardInfo fi = s[i];
2085 editor.putInt(settingKey + FWD_SETTING_STATUS, fi.status);
2086 editor.putInt(settingKey + FWD_SETTING_REASON, fi.reason);
2087 editor.putString(settingKey + FWD_SETTING_NUMBER, fi.number);
2088 editor.putInt(settingKey + FWD_SETTING_TIME, fi.timeSeconds);
2089 }
2090 } else {
2091 editor.putInt(fwdKey + FWD_SETTINGS_LENGTH_TAG, 0);
2092 }
2093 editor.apply();
2094 }
2095
2096 /**
2097 * Returns settings previously stored for the currently selected
2098 * voice mail provider. If none is stored returns null.
2099 * If the user switches to a voice mail provider and we have settings
2100 * stored for it we will automatically change the phone's voice mail number
2101 * and forwarding number to the stored one. Otherwise we will bring up provider's configuration
2102 * UI.
2103 */
2104 private VoiceMailProviderSettings loadSettingsForVoiceMailProvider(String key) {
2105 final String vmNumberSetting = mPerProviderSavedVMNumbers.getString(key + VM_NUMBER_TAG,
2106 null);
2107 if (vmNumberSetting == null) {
2108 Log.w(LOG_TAG, "VoiceMailProvider settings for the key \"" + key + "\""
2109 + " was not found. Returning null.");
2110 return null;
2111 }
2112
2113 CallForwardInfo[] cfi = FWD_SETTINGS_DONT_TOUCH;
2114 String fwdKey = key + FWD_SETTINGS_TAG;
2115 final int fwdLen = mPerProviderSavedVMNumbers.getInt(fwdKey + FWD_SETTINGS_LENGTH_TAG, 0);
2116 if (fwdLen > 0) {
2117 cfi = new CallForwardInfo[fwdLen];
2118 for (int i = 0; i < cfi.length; i++) {
2119 final String settingKey = fwdKey + FWD_SETTING_TAG + String.valueOf(i);
2120 cfi[i] = new CallForwardInfo();
2121 cfi[i].status = mPerProviderSavedVMNumbers.getInt(
2122 settingKey + FWD_SETTING_STATUS, 0);
2123 cfi[i].reason = mPerProviderSavedVMNumbers.getInt(
2124 settingKey + FWD_SETTING_REASON,
2125 CommandsInterface.CF_REASON_ALL_CONDITIONAL);
2126 cfi[i].serviceClass = CommandsInterface.SERVICE_CLASS_VOICE;
2127 cfi[i].toa = PhoneNumberUtils.TOA_International;
2128 cfi[i].number = mPerProviderSavedVMNumbers.getString(
2129 settingKey + FWD_SETTING_NUMBER, "");
2130 cfi[i].timeSeconds = mPerProviderSavedVMNumbers.getInt(
2131 settingKey + FWD_SETTING_TIME, 20);
2132 }
2133 }
2134
2135 VoiceMailProviderSettings settings = new VoiceMailProviderSettings(vmNumberSetting, cfi);
2136 if (DBG) log("Loaded settings for " + key + ": " + settings.toString());
2137 return settings;
2138 }
2139
2140 /**
Tyler Gunnbaee2952014-09-10 16:01:02 -07002141 * Populates the phone accounts which could potentially be selected as the default.
2142 */
2143 private void populateDefaultOutgoingAccountsModel() {
2144 if (mDefaultOutgoingAccount == null ) {
2145 return;
2146 }
2147
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07002148 TelecomManager telecomManager = TelecomManager.from(this);
Nancy Chen90f2ffd2014-09-15 17:58:14 -07002149 List<PhoneAccountHandle> callCapablePhoneAccounts =
2150 telecomManager.getCallCapablePhoneAccounts();
Tyler Gunnbaee2952014-09-10 16:01:02 -07002151 mDefaultOutgoingAccount.setModel(
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07002152 telecomManager,
Nancy Chen90f2ffd2014-09-15 17:58:14 -07002153 callCapablePhoneAccounts,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07002154 telecomManager.getUserSelectedOutgoingPhoneAccount(),
Tyler Gunnbaee2952014-09-10 16:01:02 -07002155 getString(R.string.phone_accounts_ask_every_time));
2156 }
2157
2158 /**
2159 * Populates the phone accounts which could potentially be selected as the default sim call
2160 * manager.
2161 */
2162 private void populateSimCallManagerAccountsModel() {
2163 if (mSimCallManagerAccount == null) {
2164 return;
2165 }
2166
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07002167 TelecomManager telecomManager = TelecomManager.from(this);
2168 List<PhoneAccountHandle> simCallManagers = telecomManager.getSimCallManagers();
Tyler Gunnbaee2952014-09-10 16:01:02 -07002169 mSimCallManagerAccount.setModel(
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07002170 telecomManager,
Tyler Gunnbaee2952014-09-10 16:01:02 -07002171 simCallManagers,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07002172 telecomManager.getSimCallManager(),
Tyler Gunnbaee2952014-09-10 16:01:02 -07002173 getString(R.string.wifi_calling_do_not_use));
2174 }
2175
2176 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002177 * Deletes settings for the specified provider.
2178 */
2179 private void deleteSettingsForVoicemailProvider(String key) {
2180 if (DBG) log("Deleting settings for" + key);
2181 if (mVoicemailProviders == null) {
2182 return;
2183 }
2184 mPerProviderSavedVMNumbers.edit()
2185 .putString(key + VM_NUMBER_TAG, null)
2186 .putInt(key + FWD_SETTINGS_TAG + FWD_SETTINGS_LENGTH_TAG, 0)
2187 .commit();
2188 }
2189
2190 private String getCurrentVoicemailProviderKey() {
2191 final String key = mVoicemailProviders.getValue();
2192 return (key != null) ? key : DEFAULT_VM_PROVIDER_KEY;
2193 }
2194
2195 @Override
2196 public boolean onOptionsItemSelected(MenuItem item) {
2197 final int itemId = item.getItemId();
2198 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07002199 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002200 return true;
2201 }
2202 return super.onOptionsItemSelected(item);
2203 }
2204
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002205 /**
2206 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
2207 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
2208 */
2209 public static void goUpToTopLevelSetting(Activity activity) {
2210 Intent intent = new Intent(activity, CallFeaturesSetting.class);
2211 intent.setAction(Intent.ACTION_MAIN);
2212 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
2213 activity.startActivity(intent);
2214 activity.finish();
2215 }
2216}