blob: 6947727df981b1358fb3e7c13f8cb7067cdb6350 [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
613 * AccountSelection dialog. Important to ensure changes to the enabled state of
614 * {@code PhoneAccount}s are reflected in the dialog.
615 *
616 * @param pref The account selection preference dialog being shown.
617 */
618 @Override
619 public void onAccountSelectionDialogShow(AccountSelectionPreference pref) {
620 if (pref == mDefaultOutgoingAccount) {
621 populateDefaultOutgoingAccountsModel();
622 } else if (pref == mSimCallManagerAccount) {
623 populateSimCallManagerAccountsModel();
624 }
625 }
626
Andrew Leedb2fe562014-09-03 15:40:43 -0700627 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700628 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
629 if (DBG) log("onPreferenceClick: request preference click on dialog close: " +
630 buttonClicked);
631 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
632 return;
633 }
634
635 if (preference == mSubMenuVoicemailSettings) {
636 handleVMBtnClickRequest();
637 }
638 }
639
640 /**
641 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
642 * This method set the default values for the various
643 * EditPhoneNumberPreference dialogs.
644 */
645 @Override
646 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
647 if (preference == mSubMenuVoicemailSettings) {
648 // update the voicemail number field, which takes care of the
649 // mSubMenuVoicemailSettings itself, so we should return null.
650 if (DBG) log("updating default for voicemail dialog");
651 updateVoiceNumberField();
652 return null;
653 }
654
655 String vmDisplay = mPhone.getVoiceMailNumber();
656 if (TextUtils.isEmpty(vmDisplay)) {
657 // if there is no voicemail number, we just return null to
658 // indicate no contribution.
659 return null;
660 }
661
662 // Return the voicemail number prepended with "VM: "
663 if (DBG) log("updating default for call forwarding dialogs");
664 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
665 }
666
667
668 // override the startsubactivity call to make changes in state consistent.
669 @Override
670 public void startActivityForResult(Intent intent, int requestCode) {
671 if (requestCode == -1) {
672 // this is an intent requested from the preference framework.
673 super.startActivityForResult(intent, requestCode);
674 return;
675 }
676
677 if (DBG) log("startSubActivity: starting requested subactivity");
678 super.startActivityForResult(intent, requestCode);
679 }
680
681 private void switchToPreviousVoicemailProvider() {
682 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
683 if (mPreviousVMProviderKey != null) {
684 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
685 // we have to revert with carrier
686 if (DBG) {
687 log("Needs to rollback."
688 + " mVMChangeCompletedSuccessfully=" + mVMChangeCompletedSuccessfully
689 + ", mFwdChangesRequireRollback=" + mFwdChangesRequireRollback);
690 }
691
692 showDialogIfForeground(VOICEMAIL_REVERTING_DIALOG);
693 final VoiceMailProviderSettings prevSettings =
694 loadSettingsForVoiceMailProvider(mPreviousVMProviderKey);
695 if (prevSettings == null) {
696 // prevSettings never becomes null since it should be already loaded!
697 Log.e(LOG_TAG, "VoiceMailProviderSettings for the key \""
698 + mPreviousVMProviderKey + "\" becomes null, which is unexpected.");
699 if (DBG) {
700 Log.e(LOG_TAG,
701 "mVMChangeCompletedSuccessfully: " + mVMChangeCompletedSuccessfully
702 + ", mFwdChangesRequireRollback: " + mFwdChangesRequireRollback);
703 }
704 }
705 if (mVMChangeCompletedSuccessfully) {
706 mNewVMNumber = prevSettings.voicemailNumber;
707 Log.i(LOG_TAG, "VM change is already completed successfully."
708 + "Have to revert VM back to " + mNewVMNumber + " again.");
709 mPhone.setVoiceMailNumber(
710 mPhone.getVoiceMailAlphaTag().toString(),
711 mNewVMNumber,
712 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
713 }
714 if (mFwdChangesRequireRollback) {
715 Log.i(LOG_TAG, "Requested to rollback Fwd changes.");
716 final CallForwardInfo[] prevFwdSettings =
717 prevSettings.forwardingSettings;
718 if (prevFwdSettings != null) {
719 Map<Integer, AsyncResult> results =
720 mForwardingChangeResults;
721 resetForwardingChangeState();
722 for (int i = 0; i < prevFwdSettings.length; i++) {
723 CallForwardInfo fi = prevFwdSettings[i];
724 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
725 // Only revert the settings for which the update
726 // succeeded
727 AsyncResult result = results.get(fi.reason);
728 if (result != null && result.exception == null) {
729 mExpectedChangeResultReasons.add(fi.reason);
730 mPhone.setCallForwardingOption(
731 (fi.status == 1 ?
732 CommandsInterface.CF_ACTION_REGISTRATION :
733 CommandsInterface.CF_ACTION_DISABLE),
734 fi.reason,
735 fi.number,
736 fi.timeSeconds,
737 mRevertOptionComplete.obtainMessage(
738 EVENT_FORWARDING_CHANGED, i, 0));
739 }
740 }
741 }
742 }
743 } else {
744 if (DBG) log("No need to revert");
745 onRevertDone();
746 }
747 }
748 }
749
750 private void onRevertDone() {
751 if (DBG) log("Flipping provider key back to " + mPreviousVMProviderKey);
752 mVoicemailProviders.setValue(mPreviousVMProviderKey);
753 updateVMPreferenceWidgets(mPreviousVMProviderKey);
754 updateVoiceNumberField();
755 if (mVMOrFwdSetError != 0) {
756 showVMDialog(mVMOrFwdSetError);
757 mVMOrFwdSetError = 0;
758 }
759 }
760
761 @Override
762 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
763 if (DBG) {
764 log("onActivityResult: requestCode: " + requestCode
765 + ", resultCode: " + resultCode
766 + ", data: " + data);
767 }
768 // there are cases where the contact picker may end up sending us more than one
769 // request. We want to ignore the request if we're not in the correct state.
770 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
771 boolean failure = false;
772
773 // No matter how the processing of result goes lets clear the flag
774 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
775 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
776 mVMProviderSettingsForced = false;
777
778 String vmNum = null;
779 if (resultCode != RESULT_OK) {
780 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
781 failure = true;
782 } else {
783 if (data == null) {
784 if (DBG) log("onActivityResult: vm provider cfg result has no data");
785 failure = true;
786 } else {
787 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
788 if (DBG) log("Provider requested signout");
789 if (isVMProviderSettingsForced) {
790 if (DBG) log("Going back to previous provider on signout");
791 switchToPreviousVoicemailProvider();
792 } else {
793 final String victim = getCurrentVoicemailProviderKey();
794 if (DBG) log("Relaunching activity and ignoring " + victim);
795 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
796 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
797 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
798 this.startActivity(i);
799 }
800 return;
801 }
802 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
803 if (vmNum == null || vmNum.length() == 0) {
804 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
805 failure = true;
806 }
807 }
808 }
809 if (failure) {
810 if (DBG) log("Failure in return from voicemail provider");
811 if (isVMProviderSettingsForced) {
812 switchToPreviousVoicemailProvider();
813 } else {
814 if (DBG) log("Not switching back the provider since this is not forced config");
815 }
816 return;
817 }
818 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
819 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
820
Santos Cordonda120f42014-08-06 04:44:34 -0700821 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700822 // send it to the provider when it's config is invoked so it can use this as default
823 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
824
825 if (DBG) log("onActivityResult: vm provider cfg result " +
826 (fwdNum != null ? "has" : " does not have") + " forwarding number");
827 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(),
828 new VoiceMailProviderSettings(vmNum, fwdNum, fwdNumTime));
829 return;
830 }
831
832 if (requestCode == VOICEMAIL_PREF_ID) {
833 if (resultCode != RESULT_OK) {
834 if (DBG) log("onActivityResult: contact picker result not OK.");
835 return;
836 }
837
838 Cursor cursor = null;
839 try {
840 cursor = getContentResolver().query(data.getData(),
841 NUM_PROJECTION, null, null, null);
842 if ((cursor == null) || (!cursor.moveToFirst())) {
843 if (DBG) log("onActivityResult: bad contact data, no results found.");
844 return;
845 }
846 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
847 return;
848 } finally {
849 if (cursor != null) {
850 cursor.close();
851 }
852 }
853 }
854
855 super.onActivityResult(requestCode, resultCode, data);
856 }
857
858 // Voicemail button logic
859 private void handleVMBtnClickRequest() {
860 // normally called on the dialog close.
861
862 // Since we're stripping the formatting out on the getPhoneNumber()
863 // call now, we won't need to do so here anymore.
864
865 saveVoiceMailAndForwardingNumber(
866 getCurrentVoicemailProviderKey(),
867 new VoiceMailProviderSettings(mSubMenuVoicemailSettings.getPhoneNumber(),
Tyler Gunn4d45d1c2014-09-12 22:17:53 -0700868 FWD_SETTINGS_DONT_TOUCH)
869 );
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700870 }
871
872
873 /**
874 * Wrapper around showDialog() that will silently do nothing if we're
875 * not in the foreground.
876 *
877 * This is useful here because most of the dialogs we display from
878 * this class are triggered by asynchronous events (like
879 * success/failure messages from the telephony layer) and it's
880 * possible for those events to come in even after the user has gone
881 * to a different screen.
882 */
883 // TODO: this is too brittle: it's still easy to accidentally add new
884 // code here that calls showDialog() directly (which will result in a
885 // WindowManager$BadTokenException if called after the activity has
886 // been stopped.)
887 //
888 // It would be cleaner to do the "if (mForeground)" check in one
889 // central place, maybe by using a single Handler for all asynchronous
890 // events (and have *that* discard events if we're not in the
891 // foreground.)
892 //
893 // Unfortunately it's not that simple, since we sometimes need to do
894 // actual work to handle these events whether or not we're in the
895 // foreground (see the Handler code in mSetOptionComplete for
896 // example.)
897 private void showDialogIfForeground(int id) {
898 if (mForeground) {
899 showDialog(id);
900 }
901 }
902
903 private void dismissDialogSafely(int id) {
904 try {
905 dismissDialog(id);
906 } catch (IllegalArgumentException e) {
907 // This is expected in the case where we were in the background
908 // at the time we would normally have shown the dialog, so we didn't
909 // show it.
910 }
911 }
912
913 private void saveVoiceMailAndForwardingNumber(String key,
914 VoiceMailProviderSettings newSettings) {
915 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
916 mNewVMNumber = newSettings.voicemailNumber;
917 // empty vm number == clearing the vm number ?
918 if (mNewVMNumber == null) {
919 mNewVMNumber = "";
920 }
921
922 mNewFwdSettings = newSettings.forwardingSettings;
923 if (DBG) log("newFwdNumber " +
924 String.valueOf((mNewFwdSettings != null ? mNewFwdSettings.length : 0))
925 + " settings");
926
927 // No fwd settings on CDMA
928 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
929 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
930 mNewFwdSettings = FWD_SETTINGS_DONT_TOUCH;
931 }
932
933 //throw a warning if the vm is the same and we do not touch forwarding.
934 if (mNewVMNumber.equals(mOldVmNumber) && mNewFwdSettings == FWD_SETTINGS_DONT_TOUCH) {
935 showVMDialog(MSG_VM_NOCHANGE);
936 return;
937 }
938
939 maybeSaveSettingsForVoicemailProvider(key, newSettings);
940 mVMChangeCompletedSuccessfully = false;
941 mFwdChangesRequireRollback = false;
942 mVMOrFwdSetError = 0;
943 if (!key.equals(mPreviousVMProviderKey)) {
944 mReadingSettingsForDefaultProvider =
945 mPreviousVMProviderKey.equals(DEFAULT_VM_PROVIDER_KEY);
946 if (DBG) log("Reading current forwarding settings");
947 mForwardingReadResults = new CallForwardInfo[FORWARDING_SETTINGS_REASONS.length];
948 for (int i = 0; i < FORWARDING_SETTINGS_REASONS.length; i++) {
949 mForwardingReadResults[i] = null;
950 mPhone.getCallForwardingOption(FORWARDING_SETTINGS_REASONS[i],
951 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
952 }
953 showDialogIfForeground(VOICEMAIL_FWD_READING_DIALOG);
954 } else {
955 saveVoiceMailAndForwardingNumberStage2();
956 }
957 }
958
959 private final Handler mGetOptionComplete = new Handler() {
960 @Override
961 public void handleMessage(Message msg) {
962 AsyncResult result = (AsyncResult) msg.obj;
963 switch (msg.what) {
964 case EVENT_FORWARDING_GET_COMPLETED:
965 handleForwardingSettingsReadResult(result, msg.arg1);
966 break;
967 }
968 }
969 };
970
971 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
972 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
973 Throwable error = null;
974 if (ar.exception != null) {
975 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" +
976 ar.exception.getMessage());
977 error = ar.exception;
978 }
979 if (ar.userObj instanceof Throwable) {
980 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" +
981 ((Throwable)ar.userObj).getMessage());
982 error = (Throwable)ar.userObj;
983 }
984
985 // We may have already gotten an error and decided to ignore the other results.
986 if (mForwardingReadResults == null) {
987 if (DBG) Log.d(LOG_TAG, "ignoring fwd reading result: " + idx);
988 return;
989 }
990
991 // In case of error ignore other results, show an error dialog
992 if (error != null) {
993 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
994 mForwardingReadResults = null;
995 dismissDialogSafely(VOICEMAIL_FWD_READING_DIALOG);
996 showVMDialog(MSG_FW_GET_EXCEPTION);
997 return;
998 }
999
1000 // Get the forwarding info
1001 final CallForwardInfo cfInfoArray[] = (CallForwardInfo[]) ar.result;
1002 CallForwardInfo fi = null;
1003 for (int i = 0 ; i < cfInfoArray.length; i++) {
1004 if ((cfInfoArray[i].serviceClass & CommandsInterface.SERVICE_CLASS_VOICE) != 0) {
1005 fi = cfInfoArray[i];
1006 break;
1007 }
1008 }
1009 if (fi == null) {
1010
1011 // In case we go nothing it means we need this reason disabled
1012 // so create a CallForwardInfo for capturing this
1013 if (DBG) Log.d(LOG_TAG, "Creating default info for " + idx);
1014 fi = new CallForwardInfo();
1015 fi.status = 0;
1016 fi.reason = FORWARDING_SETTINGS_REASONS[idx];
1017 fi.serviceClass = CommandsInterface.SERVICE_CLASS_VOICE;
1018 } else {
1019 // if there is not a forwarding number, ensure the entry is set to "not active."
1020 if (fi.number == null || fi.number.length() == 0) {
1021 fi.status = 0;
1022 }
1023
1024 if (DBG) Log.d(LOG_TAG, "Got " + fi.toString() + " for " + idx);
1025 }
1026 mForwardingReadResults[idx] = fi;
1027
1028 // Check if we got all the results already
1029 boolean done = true;
1030 for (int i = 0; i < mForwardingReadResults.length; i++) {
1031 if (mForwardingReadResults[i] == null) {
1032 done = false;
1033 break;
1034 }
1035 }
1036 if (done) {
1037 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
1038 dismissDialogSafely(VOICEMAIL_FWD_READING_DIALOG);
1039 if (mReadingSettingsForDefaultProvider) {
1040 maybeSaveSettingsForVoicemailProvider(DEFAULT_VM_PROVIDER_KEY,
1041 new VoiceMailProviderSettings(this.mOldVmNumber,
1042 mForwardingReadResults));
1043 mReadingSettingsForDefaultProvider = false;
1044 }
1045 saveVoiceMailAndForwardingNumberStage2();
1046 } else {
1047 if (DBG) Log.d(LOG_TAG, "Not done receiving fwd info");
1048 }
1049 }
1050
1051 private CallForwardInfo infoForReason(CallForwardInfo[] infos, int reason) {
1052 CallForwardInfo result = null;
1053 if (null != infos) {
1054 for (CallForwardInfo info : infos) {
1055 if (info.reason == reason) {
1056 result = info;
1057 break;
1058 }
1059 }
1060 }
1061 return result;
1062 }
1063
1064 private boolean isUpdateRequired(CallForwardInfo oldInfo,
1065 CallForwardInfo newInfo) {
1066 boolean result = true;
1067 if (0 == newInfo.status) {
1068 // If we're disabling a type of forwarding, and it's already
1069 // disabled for the account, don't make any change
1070 if (oldInfo != null && oldInfo.status == 0) {
1071 result = false;
1072 }
1073 }
1074 return result;
1075 }
1076
1077 private void resetForwardingChangeState() {
1078 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
1079 mExpectedChangeResultReasons = new HashSet<Integer>();
1080 }
1081
1082 // Called after we are done saving the previous forwarding settings if
1083 // we needed.
1084 private void saveVoiceMailAndForwardingNumberStage2() {
1085 mForwardingChangeResults = null;
1086 mVoicemailChangeResult = null;
1087 if (mNewFwdSettings != FWD_SETTINGS_DONT_TOUCH) {
1088 resetForwardingChangeState();
1089 for (int i = 0; i < mNewFwdSettings.length; i++) {
1090 CallForwardInfo fi = mNewFwdSettings[i];
1091
1092 final boolean doUpdate = isUpdateRequired(infoForReason(
1093 mForwardingReadResults, fi.reason), fi);
1094
1095 if (doUpdate) {
1096 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
1097 mExpectedChangeResultReasons.add(i);
1098
1099 mPhone.setCallForwardingOption(
1100 fi.status == 1 ?
1101 CommandsInterface.CF_ACTION_REGISTRATION :
1102 CommandsInterface.CF_ACTION_DISABLE,
1103 fi.reason,
1104 fi.number,
1105 fi.timeSeconds,
1106 mSetOptionComplete.obtainMessage(
1107 EVENT_FORWARDING_CHANGED, fi.reason, 0));
1108 }
1109 }
1110 showDialogIfForeground(VOICEMAIL_FWD_SAVING_DIALOG);
1111 } else {
1112 if (DBG) log("Not touching fwd #");
1113 setVMNumberWithCarrier();
1114 }
1115 }
1116
1117 private void setVMNumberWithCarrier() {
1118 if (DBG) log("save voicemail #: " + mNewVMNumber);
1119 mPhone.setVoiceMailNumber(
1120 mPhone.getVoiceMailAlphaTag().toString(),
1121 mNewVMNumber,
1122 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
1123 }
1124
1125 /**
1126 * Callback to handle option update completions
1127 */
1128 private final Handler mSetOptionComplete = new Handler() {
1129 @Override
1130 public void handleMessage(Message msg) {
1131 AsyncResult result = (AsyncResult) msg.obj;
1132 boolean done = false;
1133 switch (msg.what) {
1134 case EVENT_VOICEMAIL_CHANGED:
1135 mVoicemailChangeResult = result;
1136 mVMChangeCompletedSuccessfully = checkVMChangeSuccess() == null;
1137 if (DBG) log("VM change complete msg, VM change done = " +
1138 String.valueOf(mVMChangeCompletedSuccessfully));
1139 done = true;
1140 break;
1141 case EVENT_FORWARDING_CHANGED:
1142 mForwardingChangeResults.put(msg.arg1, result);
1143 if (result.exception != null) {
1144 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
1145 result.exception.getMessage());
1146 } else {
1147 if (DBG) log("Success in setting fwd# " + msg.arg1);
1148 }
1149 final boolean completed = checkForwardingCompleted();
1150 if (completed) {
1151 if (checkFwdChangeSuccess() == null) {
1152 if (DBG) log("Overall fwd changes completed ok, starting vm change");
1153 setVMNumberWithCarrier();
1154 } else {
1155 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
1156 "Check if we need to try rollback for some settings.");
1157 mFwdChangesRequireRollback = false;
1158 Iterator<Map.Entry<Integer,AsyncResult>> it =
1159 mForwardingChangeResults.entrySet().iterator();
1160 while (it.hasNext()) {
1161 Map.Entry<Integer,AsyncResult> entry = it.next();
1162 if (entry.getValue().exception == null) {
1163 // If at least one succeeded we have to revert
1164 Log.i(LOG_TAG, "Rollback will be required");
1165 mFwdChangesRequireRollback = true;
1166 break;
1167 }
1168 }
1169 if (!mFwdChangesRequireRollback) {
1170 Log.i(LOG_TAG, "No rollback needed.");
1171 }
1172 done = true;
1173 }
1174 }
1175 break;
1176 default:
1177 // TODO: should never reach this, may want to throw exception
1178 }
1179 if (done) {
1180 if (DBG) log("All VM provider related changes done");
1181 if (mForwardingChangeResults != null) {
1182 dismissDialogSafely(VOICEMAIL_FWD_SAVING_DIALOG);
1183 }
1184 handleSetVMOrFwdMessage();
1185 }
1186 }
1187 };
1188
1189 /**
1190 * Callback to handle option revert completions
1191 */
1192 private final Handler mRevertOptionComplete = new Handler() {
1193 @Override
1194 public void handleMessage(Message msg) {
1195 AsyncResult result = (AsyncResult) msg.obj;
1196 switch (msg.what) {
1197 case EVENT_VOICEMAIL_CHANGED:
1198 mVoicemailChangeResult = result;
1199 if (DBG) log("VM revert complete msg");
1200 break;
1201 case EVENT_FORWARDING_CHANGED:
1202 mForwardingChangeResults.put(msg.arg1, result);
1203 if (result.exception != null) {
1204 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
1205 result.exception.getMessage());
1206 } else {
1207 if (DBG) log("Success in reverting fwd# " + msg.arg1);
1208 }
1209 if (DBG) log("FWD revert complete msg ");
1210 break;
1211 default:
1212 // TODO: should never reach this, may want to throw exception
1213 }
1214 final boolean done =
1215 (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null) &&
1216 (!mFwdChangesRequireRollback || checkForwardingCompleted());
1217 if (done) {
1218 if (DBG) log("All VM reverts done");
1219 dismissDialogSafely(VOICEMAIL_REVERTING_DIALOG);
1220 onRevertDone();
1221 }
1222 }
1223 };
1224
1225 /**
1226 * @return true if forwarding change has completed
1227 */
1228 private boolean checkForwardingCompleted() {
1229 boolean result;
1230 if (mForwardingChangeResults == null) {
1231 result = true;
1232 } else {
1233 // return true iff there is a change result for every reason for
1234 // which we expected a result
1235 result = true;
1236 for (Integer reason : mExpectedChangeResultReasons) {
1237 if (mForwardingChangeResults.get(reason) == null) {
1238 result = false;
1239 break;
1240 }
1241 }
1242 }
1243 return result;
1244 }
1245 /**
1246 * @return error string or null if successful
1247 */
1248 private String checkFwdChangeSuccess() {
1249 String result = null;
1250 Iterator<Map.Entry<Integer,AsyncResult>> it =
1251 mForwardingChangeResults.entrySet().iterator();
1252 while (it.hasNext()) {
1253 Map.Entry<Integer,AsyncResult> entry = it.next();
1254 Throwable exception = entry.getValue().exception;
1255 if (exception != null) {
1256 result = exception.getMessage();
1257 if (result == null) {
1258 result = "";
1259 }
1260 break;
1261 }
1262 }
1263 return result;
1264 }
1265
1266 /**
1267 * @return error string or null if successful
1268 */
1269 private String checkVMChangeSuccess() {
1270 if (mVoicemailChangeResult.exception != null) {
1271 final String msg = mVoicemailChangeResult.exception.getMessage();
1272 if (msg == null) {
1273 return "";
1274 }
1275 return msg;
1276 }
1277 return null;
1278 }
1279
1280 private void handleSetVMOrFwdMessage() {
1281 if (DBG) {
1282 log("handleSetVMMessage: set VM request complete");
1283 }
1284 boolean success = true;
1285 boolean fwdFailure = false;
1286 String exceptionMessage = "";
1287 if (mForwardingChangeResults != null) {
1288 exceptionMessage = checkFwdChangeSuccess();
1289 if (exceptionMessage != null) {
1290 success = false;
1291 fwdFailure = true;
1292 }
1293 }
1294 if (success) {
1295 exceptionMessage = checkVMChangeSuccess();
1296 if (exceptionMessage != null) {
1297 success = false;
1298 }
1299 }
1300 if (success) {
1301 if (DBG) log("change VM success!");
1302 handleVMAndFwdSetSuccess(MSG_VM_OK);
1303 } else {
1304 if (fwdFailure) {
1305 Log.w(LOG_TAG, "Failed to change fowarding setting. Reason: " + exceptionMessage);
1306 handleVMOrFwdSetError(MSG_FW_SET_EXCEPTION);
1307 } else {
1308 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + exceptionMessage);
1309 handleVMOrFwdSetError(MSG_VM_EXCEPTION);
1310 }
1311 }
1312 }
1313
1314 /**
1315 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
1316 * changes to those settings and show "failure" dialog.
1317 *
1318 * @param msgId Message ID used for the specific error case. {@link #MSG_FW_SET_EXCEPTION} or
1319 * {@link #MSG_VM_EXCEPTION}
1320 */
1321 private void handleVMOrFwdSetError(int msgId) {
1322 if (mChangingVMorFwdDueToProviderChange) {
1323 mVMOrFwdSetError = msgId;
1324 mChangingVMorFwdDueToProviderChange = false;
1325 switchToPreviousVoicemailProvider();
1326 return;
1327 }
1328 mChangingVMorFwdDueToProviderChange = false;
1329 showVMDialog(msgId);
1330 updateVoiceNumberField();
1331 }
1332
1333 /**
1334 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1335 * This updates a bunch of variables and show "success" dialog.
1336 */
1337 private void handleVMAndFwdSetSuccess(int msg) {
1338 if (DBG) {
1339 log("handleVMAndFwdSetSuccess(). current voicemail provider key: "
1340 + getCurrentVoicemailProviderKey());
1341 }
1342 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1343 mChangingVMorFwdDueToProviderChange = false;
1344 showVMDialog(msg);
1345 updateVoiceNumberField();
1346 }
1347
1348 /**
1349 * Update the voicemail number from what we've recorded on the sim.
1350 */
1351 private void updateVoiceNumberField() {
1352 if (DBG) {
1353 log("updateVoiceNumberField(). mSubMenuVoicemailSettings=" + mSubMenuVoicemailSettings);
1354 }
1355 if (mSubMenuVoicemailSettings == null) {
1356 return;
1357 }
1358
1359 mOldVmNumber = mPhone.getVoiceMailNumber();
1360 if (mOldVmNumber == null) {
1361 mOldVmNumber = "";
1362 }
1363 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1364 final String summary = (mOldVmNumber.length() > 0) ? mOldVmNumber :
1365 getString(R.string.voicemail_number_not_set);
1366 mSubMenuVoicemailSettings.setSummary(summary);
1367 }
1368
1369 /*
1370 * Helper Methods for Activity class.
1371 * The initial query commands are split into two pieces now
1372 * for individual expansion. This combined with the ability
1373 * to cancel queries allows for a much better user experience,
1374 * and also ensures that the user only waits to update the
1375 * data that is relevant.
1376 */
1377
1378 @Override
1379 protected void onPrepareDialog(int id, Dialog dialog) {
1380 super.onPrepareDialog(id, dialog);
1381 mCurrentDialogId = id;
1382 }
1383
1384 // dialog creation method, called by showDialog()
1385 @Override
1386 protected Dialog onCreateDialog(int id) {
1387 if ((id == VM_RESPONSE_ERROR) || (id == VM_NOCHANGE_ERROR) ||
1388 (id == FW_SET_RESPONSE_ERROR) || (id == FW_GET_RESPONSE_ERROR) ||
1389 (id == VOICEMAIL_DIALOG_CONFIRM)) {
1390
1391 AlertDialog.Builder b = new AlertDialog.Builder(this);
1392
1393 int msgId;
1394 int titleId = R.string.error_updating_title;
1395 switch (id) {
1396 case VOICEMAIL_DIALOG_CONFIRM:
1397 msgId = R.string.vm_changed;
1398 titleId = R.string.voicemail;
1399 // Set Button 2
1400 b.setNegativeButton(R.string.close_dialog, this);
1401 break;
1402 case VM_NOCHANGE_ERROR:
1403 // even though this is technically an error,
1404 // keep the title friendly.
1405 msgId = R.string.no_change;
1406 titleId = R.string.voicemail;
1407 // Set Button 2
1408 b.setNegativeButton(R.string.close_dialog, this);
1409 break;
1410 case VM_RESPONSE_ERROR:
1411 msgId = R.string.vm_change_failed;
1412 // Set Button 1
1413 b.setPositiveButton(R.string.close_dialog, this);
1414 break;
1415 case FW_SET_RESPONSE_ERROR:
1416 msgId = R.string.fw_change_failed;
1417 // Set Button 1
1418 b.setPositiveButton(R.string.close_dialog, this);
1419 break;
1420 case FW_GET_RESPONSE_ERROR:
1421 msgId = R.string.fw_get_in_vm_failed;
1422 b.setPositiveButton(R.string.alert_dialog_yes, this);
1423 b.setNegativeButton(R.string.alert_dialog_no, this);
1424 break;
1425 default:
1426 msgId = R.string.exception_error;
1427 // Set Button 3, tells the activity that the error is
1428 // not recoverable on dialog exit.
1429 b.setNeutralButton(R.string.close_dialog, this);
1430 break;
1431 }
1432
1433 b.setTitle(getText(titleId));
1434 String message = getText(msgId).toString();
1435 b.setMessage(message);
1436 b.setCancelable(false);
1437 AlertDialog dialog = b.create();
1438
1439 // make the dialog more obvious by bluring the background.
1440 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
1441
1442 return dialog;
1443 } else if (id == VOICEMAIL_FWD_SAVING_DIALOG || id == VOICEMAIL_FWD_READING_DIALOG ||
1444 id == VOICEMAIL_REVERTING_DIALOG) {
1445 ProgressDialog dialog = new ProgressDialog(this);
1446 dialog.setTitle(getText(R.string.updating_title));
1447 dialog.setIndeterminate(true);
1448 dialog.setCancelable(false);
1449 dialog.setMessage(getText(
1450 id == VOICEMAIL_FWD_SAVING_DIALOG ? R.string.updating_settings :
1451 (id == VOICEMAIL_REVERTING_DIALOG ? R.string.reverting_settings :
1452 R.string.reading_settings)));
1453 return dialog;
1454 }
1455
1456
1457 return null;
1458 }
1459
1460 // This is a method implemented for DialogInterface.OnClickListener.
1461 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1462 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1463 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1464 public void onClick(DialogInterface dialog, int which) {
1465 dialog.dismiss();
1466 switch (which){
1467 case DialogInterface.BUTTON_NEUTRAL:
1468 if (DBG) log("Neutral button");
1469 break;
1470 case DialogInterface.BUTTON_NEGATIVE:
1471 if (DBG) log("Negative button");
1472 if (mCurrentDialogId == FW_GET_RESPONSE_ERROR) {
1473 // We failed to get current forwarding settings and the user
1474 // does not wish to continue.
1475 switchToPreviousVoicemailProvider();
1476 }
1477 break;
1478 case DialogInterface.BUTTON_POSITIVE:
1479 if (DBG) log("Positive button");
1480 if (mCurrentDialogId == FW_GET_RESPONSE_ERROR) {
1481 // We failed to get current forwarding settings but the user
1482 // wishes to continue changing settings to the new vm provider
1483 saveVoiceMailAndForwardingNumberStage2();
1484 } else {
1485 finish();
1486 }
1487 return;
1488 default:
1489 // just let the dialog close and go back to the input
1490 }
1491 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1492 // with settings UI. If we were called to explicitly configure voice mail then
1493 // we finish the settings activity here to come back to whatever the user was doing.
1494 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1495 finish();
1496 }
1497 }
1498
1499 // set the app state with optional status.
1500 private void showVMDialog(int msgStatus) {
1501 switch (msgStatus) {
1502 // It's a bit worrisome to punt in the error cases here when we're
1503 // not in the foreground; maybe toast instead?
1504 case MSG_VM_EXCEPTION:
1505 showDialogIfForeground(VM_RESPONSE_ERROR);
1506 break;
1507 case MSG_FW_SET_EXCEPTION:
1508 showDialogIfForeground(FW_SET_RESPONSE_ERROR);
1509 break;
1510 case MSG_FW_GET_EXCEPTION:
1511 showDialogIfForeground(FW_GET_RESPONSE_ERROR);
1512 break;
1513 case MSG_VM_NOCHANGE:
1514 showDialogIfForeground(VM_NOCHANGE_ERROR);
1515 break;
1516 case MSG_VM_OK:
1517 showDialogIfForeground(VOICEMAIL_DIALOG_CONFIRM);
1518 break;
1519 case MSG_OK:
1520 default:
1521 // This should never happen.
1522 }
1523 }
1524
1525 /*
1526 * Activity class methods
1527 */
1528
1529 @Override
1530 protected void onCreate(Bundle icicle) {
1531 super.onCreate(icicle);
1532 if (DBG) log("onCreate(). Intent: " + getIntent());
1533 mPhone = PhoneGlobals.getPhone();
Tyler Gunnbaee2952014-09-10 16:01:02 -07001534 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
1535
1536 // create intent to bring up contact list
1537 mContactListIntent = new Intent(Intent.ACTION_GET_CONTENT);
1538 mContactListIntent.setType(android.provider.Contacts.Phones.CONTENT_ITEM_TYPE);
1539
1540 mVoicemailRingtoneLookupRunnable = new Runnable() {
1541 @Override
1542 public void run() {
1543 if (mVoicemailNotificationRingtone != null) {
1544 SettingsUtil.updateRingtoneName(
1545 mPhone.getContext(),
1546 mVoicemailRingtoneLookupComplete,
1547 RingtoneManager.TYPE_NOTIFICATION,
1548 mVoicemailNotificationRingtone,
1549 MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY);
1550 }
1551 }
1552 };
1553
1554 ActionBar actionBar = getActionBar();
1555 if (actionBar != null) {
1556 // android.R.id.home will be triggered in onOptionsItemSelected()
1557 actionBar.setDisplayShowHomeEnabled(true);
1558 actionBar.setDisplayHomeAsUpEnabled(true);
1559 actionBar.setDisplayShowTitleEnabled(true);
1560 }
1561
1562 // Show the voicemail preference in onResume if the calling intent specifies the
1563 // ACTION_ADD_VOICEMAIL action.
1564 mShowVoicemailPreference = (icicle == null) &&
1565 getIntent().getAction().equals(ACTION_ADD_VOICEMAIL);
1566 }
1567
1568 private void initPhoneAccountPreferences() {
1569 mDefaultOutgoingAccount = (AccountSelectionPreference)
1570 findPreference(DEFAULT_OUTGOING_ACCOUNT_KEY);
1571 mSimCallManagerAccount = (AccountSelectionPreference)
1572 findPreference(WIFI_CALL_MANAGER_ACCOUNT_KEY);
1573
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001574 TelecomManager telecomManager = TelecomManager.from(this);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001575
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001576 int allPhoneAccountsCount = telecomManager.getAllPhoneAccountsCount();
Tyler Gunnbaee2952014-09-10 16:01:02 -07001577 // Show the phone accounts preference if there are is more than one phone account (this
1578 // includes disabled phone accounts). The default selection, however, only includes those
1579 // PhoneAccounts which are enabled.
1580 if (allPhoneAccountsCount > 1) {
1581 populateDefaultOutgoingAccountsModel();
1582
1583 mDefaultOutgoingAccount.setListener(this);
1584 } else {
1585 getPreferenceScreen().removePreference(mDefaultOutgoingAccount);
1586 }
1587
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001588 List<PhoneAccountHandle> simCallManagers = telecomManager.getSimCallManagers();
Tyler Gunnbaee2952014-09-10 16:01:02 -07001589 if (!simCallManagers.isEmpty()) {
1590 populateSimCallManagerAccountsModel();
1591 mSimCallManagerAccount.setListener(this);
1592 } else {
1593 getPreferenceScreen().removePreference(mSimCallManagerAccount);
1594 }
1595 }
1596
1597 private void createSipCallSettings() {
1598 // Add Internet call settings.
1599 if (SipUtil.isVoipSupported(this)) {
1600 mSipManager = SipManager.newInstance(this);
1601 mSipSharedPreferences = new SipSharedPreferences(this);
1602 addPreferencesFromResource(
1603 com.android.services.telephony.sip.R.xml.sip_settings_category);
1604 mButtonSipCallOptions = getSipCallOptionPreference();
1605 mButtonSipCallOptions.setOnPreferenceChangeListener(this);
1606 mButtonSipCallOptions.setValueIndex(
1607 mButtonSipCallOptions.findIndexOfValue(
1608 mSipSharedPreferences.getSipCallOption()));
1609 mButtonSipCallOptions.setSummary(mButtonSipCallOptions.getEntry());
1610 }
1611 }
1612
1613 private boolean canLaunchIntent(Intent intent) {
1614 PackageManager pm = getPackageManager();
1615 return pm.resolveActivity(intent, PackageManager.GET_ACTIVITIES) != null;
1616 }
1617
1618 // Gets the call options for SIP depending on whether SIP is allowed only
1619 // on Wi-Fi only; also make the other options preference invisible.
1620 private ListPreference getSipCallOptionPreference() {
1621 ListPreference wifiAnd3G = (ListPreference)
1622 findPreference(BUTTON_SIP_CALL_OPTIONS);
1623 ListPreference wifiOnly = (ListPreference)
1624 findPreference(BUTTON_SIP_CALL_OPTIONS_WIFI_ONLY);
1625 PreferenceScreen sipSettings = (PreferenceScreen)
1626 getPreferenceScreen().findPreference(SIP_SETTINGS_PREFERENCE_SCREEN_KEY);
1627 if (SipManager.isSipWifiOnly(this)) {
1628 sipSettings.removePreference(wifiAnd3G);
1629 return wifiOnly;
1630 } else {
1631 sipSettings.removePreference(wifiOnly);
1632 return wifiAnd3G;
1633 }
1634 }
1635
1636 @Override
1637 protected void onResume() {
1638 super.onResume();
1639 mForeground = true;
1640
1641 PreferenceScreen preferenceScreen = getPreferenceScreen();
1642 if (preferenceScreen != null) {
1643 preferenceScreen.removeAll();
1644 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001645
1646 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Leedb2fe562014-09-03 15:40:43 -07001647 initPhoneAccountPreferences();
1648
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001649 // get buttons
1650 PreferenceScreen prefSet = getPreferenceScreen();
1651 mSubMenuVoicemailSettings = (EditPhoneNumberPreference)findPreference(BUTTON_VOICEMAIL_KEY);
1652 if (mSubMenuVoicemailSettings != null) {
1653 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1654 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1655 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
1656 }
1657
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001658 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1659 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1660 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
1661 mButtonTTY = (ListPreference) findPreference(BUTTON_TTY_KEY);
1662 mVoicemailProviders = (ListPreference) findPreference(BUTTON_VOICEMAIL_PROVIDER_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001663
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001664 if (mVoicemailProviders != null) {
1665 mVoicemailProviders.setOnPreferenceChangeListener(this);
1666 mVoicemailSettings = (PreferenceScreen)findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
1667 mVoicemailNotificationRingtone =
1668 findPreference(BUTTON_VOICEMAIL_NOTIFICATION_RINGTONE_KEY);
1669 mVoicemailNotificationVibrate =
1670 (CheckBoxPreference) findPreference(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY);
1671 initVoiceMailProviders();
1672 }
1673
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001674
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001675 if (mButtonDTMF != null) {
1676 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1677 mButtonDTMF.setOnPreferenceChangeListener(this);
1678 } else {
1679 prefSet.removePreference(mButtonDTMF);
1680 mButtonDTMF = null;
1681 }
1682 }
1683
1684 if (mButtonAutoRetry != null) {
1685 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1686 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1687 } else {
1688 prefSet.removePreference(mButtonAutoRetry);
1689 mButtonAutoRetry = null;
1690 }
1691 }
1692
1693 if (mButtonHAC != null) {
1694 if (getResources().getBoolean(R.bool.hac_enabled)) {
1695
1696 mButtonHAC.setOnPreferenceChangeListener(this);
1697 } else {
1698 prefSet.removePreference(mButtonHAC);
1699 mButtonHAC = null;
1700 }
1701 }
1702
1703 if (mButtonTTY != null) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001704 TelecomManager telecomManager = TelecomManager.from(this);
1705 if (telecomManager != null && telecomManager.isTtySupported()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001706 mButtonTTY.setOnPreferenceChangeListener(this);
1707 } else {
1708 prefSet.removePreference(mButtonTTY);
1709 mButtonTTY = null;
1710 }
1711 }
1712
1713 if (!getResources().getBoolean(R.bool.world_phone)) {
1714 Preference options = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
Andrew Lee2170a972014-08-13 18:13:01 -07001715 if (options != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001716 prefSet.removePreference(options);
Andrew Lee2170a972014-08-13 18:13:01 -07001717 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001718 options = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
Andrew Lee2170a972014-08-13 18:13:01 -07001719 if (options != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001720 prefSet.removePreference(options);
Andrew Lee2170a972014-08-13 18:13:01 -07001721 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001722
1723 int phoneType = mPhone.getPhoneType();
1724 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1725 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001726 if (fdnButton != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001727 prefSet.removePreference(fdnButton);
Andrew Lee2170a972014-08-13 18:13:01 -07001728 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001729 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1730 addPreferencesFromResource(R.xml.cdma_call_privacy);
1731 }
1732 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
Andrew Lee2170a972014-08-13 18:13:01 -07001733 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
Etan Cohen0ca1c802014-07-07 15:35:48 -07001734 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1735 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001736 } else {
1737 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1738 }
1739 }
1740
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001741 // check the intent that started this activity and pop up the voicemail
1742 // dialog if we've been asked to.
1743 // If we have at least one non default VM provider registered then bring up
1744 // the selection for the VM provider, otherwise bring up a VM number dialog.
1745 // We only bring up the dialog the first time we are called (not after orientation change)
Tyler Gunnbaee2952014-09-10 16:01:02 -07001746 if (mShowVoicemailPreference && mVoicemailProviders != null) {
1747 if (DBG) {
1748 log("ACTION_ADD_VOICEMAIL Intent is thrown. current VM data size: "
1749 + mVMProvidersData.size());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001750 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001751 if (mVMProvidersData.size() > 1) {
1752 simulatePreferenceClick(mVoicemailProviders);
1753 } else {
1754 onPreferenceChange(mVoicemailProviders, DEFAULT_VM_PROVIDER_KEY);
1755 mVoicemailProviders.setValue(DEFAULT_VM_PROVIDER_KEY);
1756 }
1757 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001758 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001759
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001760 updateVoiceNumberField();
1761 mVMProviderSettingsForced = false;
1762 createSipCallSettings();
1763
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001764 if (isAirplaneModeOn()) {
Andrew Lee2170a972014-08-13 18:13:01 -07001765 Preference sipSettings = findPreference(SIP_SETTINGS_PREFERENCE_SCREEN_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001766 PreferenceScreen screen = getPreferenceScreen();
1767 int count = screen.getPreferenceCount();
1768 for (int i = 0 ; i < count ; ++i) {
1769 Preference pref = screen.getPreference(i);
1770 if (pref != sipSettings) pref.setEnabled(false);
1771 }
1772 return;
1773 }
1774
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001775 if (mButtonDTMF != null) {
1776 int dtmf = Settings.System.getInt(getContentResolver(),
1777 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1778 mButtonDTMF.setValueIndex(dtmf);
1779 }
1780
1781 if (mButtonAutoRetry != null) {
1782 int autoretry = Settings.Global.getInt(getContentResolver(),
1783 Settings.Global.CALL_AUTO_RETRY, 0);
1784 mButtonAutoRetry.setChecked(autoretry != 0);
1785 }
1786
1787 if (mButtonHAC != null) {
1788 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1789 mButtonHAC.setChecked(hac != 0);
1790 }
1791
1792 if (mButtonTTY != null) {
1793 int settingsTtyMode = Settings.Secure.getInt(getContentResolver(),
1794 Settings.Secure.PREFERRED_TTY_MODE,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001795 TelecomManager.TTY_MODE_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001796 mButtonTTY.setValue(Integer.toString(settingsTtyMode));
1797 updatePreferredTtyModeSummary(settingsTtyMode);
1798 }
1799
1800 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
1801 mPhone.getContext());
1802 if (migrateVoicemailVibrationSettingsIfNeeded(prefs)) {
1803 mVoicemailNotificationVibrate.setChecked(prefs.getBoolean(
1804 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, false));
1805 }
1806
Andrew Lee2170a972014-08-13 18:13:01 -07001807 // Look up the voicemail ringtone name asynchronously and update its preference.
1808 new Thread(mVoicemailRingtoneLookupRunnable).start();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001809 }
1810
1811 // Migrate settings from BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY to
1812 // BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, if the latter does not exist.
1813 // Returns true if migration was performed.
1814 public static boolean migrateVoicemailVibrationSettingsIfNeeded(SharedPreferences prefs) {
1815 if (!prefs.contains(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY)) {
1816 String vibrateWhen = prefs.getString(
1817 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY, VOICEMAIL_VIBRATION_NEVER);
1818 // If vibrateWhen is always, then voicemailVibrate should be True.
1819 // otherwise if vibrateWhen is "only in silent mode", or "never", then
1820 // voicemailVibrate = False.
1821 boolean voicemailVibrate = vibrateWhen.equals(VOICEMAIL_VIBRATION_ALWAYS);
1822 final SharedPreferences.Editor editor = prefs.edit();
1823 editor.putBoolean(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, voicemailVibrate);
1824 editor.commit();
1825 return true;
1826 }
1827 return false;
1828 }
1829
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001830 private boolean isAirplaneModeOn() {
1831 return Settings.System.getInt(getContentResolver(),
1832 Settings.System.AIRPLANE_MODE_ON, 0) != 0;
1833 }
1834
1835 private void handleTTYChange(Preference preference, Object objValue) {
1836 int buttonTtyMode;
1837 buttonTtyMode = Integer.valueOf((String) objValue).intValue();
1838 int settingsTtyMode = android.provider.Settings.Secure.getInt(
1839 getContentResolver(),
Sailesh Nepalbf900542014-07-15 16:18:32 -07001840 android.provider.Settings.Secure.PREFERRED_TTY_MODE,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001841 TelecomManager.TTY_MODE_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001842 if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
1843 Integer.toString(buttonTtyMode));
1844
1845 if (buttonTtyMode != settingsTtyMode) {
1846 switch(buttonTtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001847 case TelecomManager.TTY_MODE_OFF:
1848 case TelecomManager.TTY_MODE_FULL:
1849 case TelecomManager.TTY_MODE_HCO:
1850 case TelecomManager.TTY_MODE_VCO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001851 android.provider.Settings.Secure.putInt(getContentResolver(),
1852 android.provider.Settings.Secure.PREFERRED_TTY_MODE, buttonTtyMode);
1853 break;
1854 default:
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001855 buttonTtyMode = TelecomManager.TTY_MODE_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001856 }
1857
1858 mButtonTTY.setValue(Integer.toString(buttonTtyMode));
1859 updatePreferredTtyModeSummary(buttonTtyMode);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001860 Intent ttyModeChanged = new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
1861 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001862 sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
1863 }
1864 }
1865
1866 private void handleSipCallOptionsChange(Object objValue) {
1867 String option = objValue.toString();
1868 mSipSharedPreferences.setSipCallOption(option);
1869 mButtonSipCallOptions.setValueIndex(
1870 mButtonSipCallOptions.findIndexOfValue(option));
1871 mButtonSipCallOptions.setSummary(mButtonSipCallOptions.getEntry());
1872 }
1873
Evan Charlton1c696832014-04-15 14:24:23 -07001874 private void handleWifiCallSettingsClick(Preference preference) {
1875 Intent intent = preference.getIntent();
1876 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1877 // TODO - Restrict to a (the?) blessed Wi-Fi calling app.
1878
1879 Bundle noAnimations = ActivityOptions.makeCustomAnimation(this, 0, 0).toBundle();
1880 startActivity(intent, noAnimations);
Ihab Awadca8ee7d2013-12-05 12:13:16 -08001881 }
1882
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001883 private void updatePreferredTtyModeSummary(int TtyMode) {
1884 String [] txts = getResources().getStringArray(R.array.tty_mode_entries);
1885 switch(TtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001886 case TelecomManager.TTY_MODE_OFF:
1887 case TelecomManager.TTY_MODE_HCO:
1888 case TelecomManager.TTY_MODE_VCO:
1889 case TelecomManager.TTY_MODE_FULL:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001890 mButtonTTY.setSummary(txts[TtyMode]);
1891 break;
1892 default:
1893 mButtonTTY.setEnabled(false);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001894 mButtonTTY.setSummary(txts[TelecomManager.TTY_MODE_OFF]);
Sailesh Nepalbf900542014-07-15 16:18:32 -07001895 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001896 }
1897 }
1898
1899 private static void log(String msg) {
1900 Log.d(LOG_TAG, msg);
1901 }
1902
1903 /**
1904 * Updates the look of the VM preference widgets based on current VM provider settings.
1905 * Note that the provider name is loaded form the found activity via loadLabel in
1906 * {@link #initVoiceMailProviders()} in order for it to be localizable.
1907 */
1908 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1909 final String key = currentProviderSetting;
1910 final VoiceMailProvider provider = mVMProvidersData.get(key);
1911
1912 /* This is the case when we are coming up on a freshly wiped phone and there is no
1913 persisted value for the list preference mVoicemailProviders.
1914 In this case we want to show the UI asking the user to select a voicemail provider as
1915 opposed to silently falling back to default one. */
1916 if (provider == null) {
1917 if (DBG) {
1918 log("updateVMPreferenceWidget: provider for the key \"" + key + "\" is null.");
1919 }
1920 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1921 mVoicemailSettings.setEnabled(false);
1922 mVoicemailSettings.setIntent(null);
1923
1924 mVoicemailNotificationVibrate.setEnabled(false);
1925 } else {
1926 if (DBG) {
1927 log("updateVMPreferenceWidget: provider for the key \"" + key + "\".."
1928 + "name: " + provider.name
1929 + ", intent: " + provider.intent);
1930 }
1931 final String providerName = provider.name;
1932 mVoicemailProviders.setSummary(providerName);
1933 mVoicemailSettings.setEnabled(true);
1934 mVoicemailSettings.setIntent(provider.intent);
1935
1936 mVoicemailNotificationVibrate.setEnabled(true);
1937 }
1938 }
1939
1940 /**
1941 * Enumerates existing VM providers and puts their data into the list and populates
1942 * the preference list objects with their names.
1943 * In case we are called with ACTION_ADD_VOICEMAIL intent the intent may have
1944 * an extra string called IGNORE_PROVIDER_EXTRA with "package.activityName" of the provider
1945 * which should be hidden when we bring up the list of possible VM providers to choose.
1946 */
1947 private void initVoiceMailProviders() {
1948 if (DBG) log("initVoiceMailProviders()");
1949 mPerProviderSavedVMNumbers =
1950 this.getApplicationContext().getSharedPreferences(
1951 VM_NUMBERS_SHARED_PREFERENCES_NAME, MODE_PRIVATE);
1952
1953 String providerToIgnore = null;
1954 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1955 if (getIntent().hasExtra(IGNORE_PROVIDER_EXTRA)) {
1956 providerToIgnore = getIntent().getStringExtra(IGNORE_PROVIDER_EXTRA);
1957 }
1958 if (DBG) log("Found ACTION_ADD_VOICEMAIL. providerToIgnore=" + providerToIgnore);
1959 if (providerToIgnore != null) {
1960 // IGNORE_PROVIDER_EXTRA implies we want to remove the choice from the list.
1961 deleteSettingsForVoicemailProvider(providerToIgnore);
1962 }
1963 }
1964
1965 mVMProvidersData.clear();
1966
1967 // Stick the default element which is always there
1968 final String myCarrier = getString(R.string.voicemail_default);
1969 mVMProvidersData.put(DEFAULT_VM_PROVIDER_KEY, new VoiceMailProvider(myCarrier, null));
1970
1971 // Enumerate providers
1972 PackageManager pm = getPackageManager();
1973 Intent intent = new Intent();
1974 intent.setAction(ACTION_CONFIGURE_VOICEMAIL);
1975 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
1976 int len = resolveInfos.size() + 1; // +1 for the default choice we will insert.
1977
1978 // Go through the list of discovered providers populating the data map
1979 // skip the provider we were instructed to ignore if there was one
1980 for (int i = 0; i < resolveInfos.size(); i++) {
1981 final ResolveInfo ri= resolveInfos.get(i);
1982 final ActivityInfo currentActivityInfo = ri.activityInfo;
1983 final String key = makeKeyForActivity(currentActivityInfo);
1984 if (key.equals(providerToIgnore)) {
1985 if (DBG) log("Ignoring key: " + key);
1986 len--;
1987 continue;
1988 }
1989 if (DBG) log("Loading key: " + key);
1990 final String nameForDisplay = ri.loadLabel(pm).toString();
1991 Intent providerIntent = new Intent();
1992 providerIntent.setAction(ACTION_CONFIGURE_VOICEMAIL);
1993 providerIntent.setClassName(currentActivityInfo.packageName,
1994 currentActivityInfo.name);
1995 if (DBG) {
1996 log("Store loaded VoiceMailProvider. key: " + key
1997 + " -> name: " + nameForDisplay + ", intent: " + providerIntent);
1998 }
1999 mVMProvidersData.put(
2000 key,
2001 new VoiceMailProvider(nameForDisplay, providerIntent));
2002
2003 }
2004
2005 // Now we know which providers to display - create entries and values array for
2006 // the list preference
2007 String [] entries = new String [len];
2008 String [] values = new String [len];
2009 entries[0] = myCarrier;
2010 values[0] = DEFAULT_VM_PROVIDER_KEY;
2011 int entryIdx = 1;
2012 for (int i = 0; i < resolveInfos.size(); i++) {
2013 final String key = makeKeyForActivity(resolveInfos.get(i).activityInfo);
2014 if (!mVMProvidersData.containsKey(key)) {
2015 continue;
2016 }
2017 entries[entryIdx] = mVMProvidersData.get(key).name;
2018 values[entryIdx] = key;
2019 entryIdx++;
2020 }
2021
2022 // ListPreference is now updated.
2023 mVoicemailProviders.setEntries(entries);
2024 mVoicemailProviders.setEntryValues(values);
2025
2026 // Remember the current Voicemail Provider key as a "previous" key. This will be used
2027 // when we fail to update Voicemail Provider, which requires rollback.
2028 // We will update this when the VM Provider setting is successfully updated.
2029 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
2030 if (DBG) log("Set up the first mPreviousVMProviderKey: " + mPreviousVMProviderKey);
2031
2032 // Finally update the preference texts.
2033 updateVMPreferenceWidgets(mPreviousVMProviderKey);
2034 }
2035
2036 private String makeKeyForActivity(ActivityInfo ai) {
2037 return ai.name;
2038 }
2039
2040 /**
2041 * Simulates user clicking on a passed preference.
2042 * Usually needed when the preference is a dialog preference and we want to invoke
2043 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07002044 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002045 */
2046 private void simulatePreferenceClick(Preference preference) {
2047 // Go through settings until we find our setting
2048 // and then simulate a click on it to bring up the dialog
2049 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
2050 for (int idx = 0; idx < adapter.getCount(); idx++) {
2051 if (adapter.getItem(idx) == preference) {
2052 getPreferenceScreen().onItemClick(this.getListView(),
2053 null, idx, adapter.getItemId(idx));
2054 break;
2055 }
2056 }
2057 }
2058
2059 /**
2060 * Saves new VM provider settings associating them with the currently selected
2061 * provider if settings are different than the ones already stored for this
2062 * provider.
2063 * Later on these will be used when the user switches a provider.
2064 */
2065 private void maybeSaveSettingsForVoicemailProvider(String key,
2066 VoiceMailProviderSettings newSettings) {
2067 if (mVoicemailProviders == null) {
2068 return;
2069 }
2070 final VoiceMailProviderSettings curSettings = loadSettingsForVoiceMailProvider(key);
2071 if (newSettings.equals(curSettings)) {
2072 if (DBG) {
2073 log("maybeSaveSettingsForVoicemailProvider:"
2074 + " Not saving setting for " + key + " since they have not changed");
2075 }
2076 return;
2077 }
2078 if (DBG) log("Saving settings for " + key + ": " + newSettings.toString());
2079 Editor editor = mPerProviderSavedVMNumbers.edit();
2080 editor.putString(key + VM_NUMBER_TAG, newSettings.voicemailNumber);
2081 String fwdKey = key + FWD_SETTINGS_TAG;
2082 CallForwardInfo[] s = newSettings.forwardingSettings;
2083 if (s != FWD_SETTINGS_DONT_TOUCH) {
2084 editor.putInt(fwdKey + FWD_SETTINGS_LENGTH_TAG, s.length);
2085 for (int i = 0; i < s.length; i++) {
2086 final String settingKey = fwdKey + FWD_SETTING_TAG + String.valueOf(i);
2087 final CallForwardInfo fi = s[i];
2088 editor.putInt(settingKey + FWD_SETTING_STATUS, fi.status);
2089 editor.putInt(settingKey + FWD_SETTING_REASON, fi.reason);
2090 editor.putString(settingKey + FWD_SETTING_NUMBER, fi.number);
2091 editor.putInt(settingKey + FWD_SETTING_TIME, fi.timeSeconds);
2092 }
2093 } else {
2094 editor.putInt(fwdKey + FWD_SETTINGS_LENGTH_TAG, 0);
2095 }
2096 editor.apply();
2097 }
2098
2099 /**
2100 * Returns settings previously stored for the currently selected
2101 * voice mail provider. If none is stored returns null.
2102 * If the user switches to a voice mail provider and we have settings
2103 * stored for it we will automatically change the phone's voice mail number
2104 * and forwarding number to the stored one. Otherwise we will bring up provider's configuration
2105 * UI.
2106 */
2107 private VoiceMailProviderSettings loadSettingsForVoiceMailProvider(String key) {
2108 final String vmNumberSetting = mPerProviderSavedVMNumbers.getString(key + VM_NUMBER_TAG,
2109 null);
2110 if (vmNumberSetting == null) {
2111 Log.w(LOG_TAG, "VoiceMailProvider settings for the key \"" + key + "\""
2112 + " was not found. Returning null.");
2113 return null;
2114 }
2115
2116 CallForwardInfo[] cfi = FWD_SETTINGS_DONT_TOUCH;
2117 String fwdKey = key + FWD_SETTINGS_TAG;
2118 final int fwdLen = mPerProviderSavedVMNumbers.getInt(fwdKey + FWD_SETTINGS_LENGTH_TAG, 0);
2119 if (fwdLen > 0) {
2120 cfi = new CallForwardInfo[fwdLen];
2121 for (int i = 0; i < cfi.length; i++) {
2122 final String settingKey = fwdKey + FWD_SETTING_TAG + String.valueOf(i);
2123 cfi[i] = new CallForwardInfo();
2124 cfi[i].status = mPerProviderSavedVMNumbers.getInt(
2125 settingKey + FWD_SETTING_STATUS, 0);
2126 cfi[i].reason = mPerProviderSavedVMNumbers.getInt(
2127 settingKey + FWD_SETTING_REASON,
2128 CommandsInterface.CF_REASON_ALL_CONDITIONAL);
2129 cfi[i].serviceClass = CommandsInterface.SERVICE_CLASS_VOICE;
2130 cfi[i].toa = PhoneNumberUtils.TOA_International;
2131 cfi[i].number = mPerProviderSavedVMNumbers.getString(
2132 settingKey + FWD_SETTING_NUMBER, "");
2133 cfi[i].timeSeconds = mPerProviderSavedVMNumbers.getInt(
2134 settingKey + FWD_SETTING_TIME, 20);
2135 }
2136 }
2137
2138 VoiceMailProviderSettings settings = new VoiceMailProviderSettings(vmNumberSetting, cfi);
2139 if (DBG) log("Loaded settings for " + key + ": " + settings.toString());
2140 return settings;
2141 }
2142
2143 /**
Tyler Gunnbaee2952014-09-10 16:01:02 -07002144 * Populates the phone accounts which could potentially be selected as the default.
2145 */
2146 private void populateDefaultOutgoingAccountsModel() {
2147 if (mDefaultOutgoingAccount == null ) {
2148 return;
2149 }
2150
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07002151 TelecomManager telecomManager = TelecomManager.from(this);
2152 List<PhoneAccountHandle> enabledPhoneAccounts = telecomManager.getEnabledPhoneAccounts();
Tyler Gunnbaee2952014-09-10 16:01:02 -07002153 mDefaultOutgoingAccount.setModel(
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07002154 telecomManager,
Tyler Gunnbaee2952014-09-10 16:01:02 -07002155 enabledPhoneAccounts,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07002156 telecomManager.getUserSelectedOutgoingPhoneAccount(),
Tyler Gunnbaee2952014-09-10 16:01:02 -07002157 getString(R.string.phone_accounts_ask_every_time));
2158 }
2159
2160 /**
2161 * Populates the phone accounts which could potentially be selected as the default sim call
2162 * manager.
2163 */
2164 private void populateSimCallManagerAccountsModel() {
2165 if (mSimCallManagerAccount == null) {
2166 return;
2167 }
2168
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07002169 TelecomManager telecomManager = TelecomManager.from(this);
2170 List<PhoneAccountHandle> simCallManagers = telecomManager.getSimCallManagers();
Tyler Gunnbaee2952014-09-10 16:01:02 -07002171 mSimCallManagerAccount.setModel(
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07002172 telecomManager,
Tyler Gunnbaee2952014-09-10 16:01:02 -07002173 simCallManagers,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07002174 telecomManager.getSimCallManager(),
Tyler Gunnbaee2952014-09-10 16:01:02 -07002175 getString(R.string.wifi_calling_do_not_use));
2176 }
2177
2178 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002179 * Deletes settings for the specified provider.
2180 */
2181 private void deleteSettingsForVoicemailProvider(String key) {
2182 if (DBG) log("Deleting settings for" + key);
2183 if (mVoicemailProviders == null) {
2184 return;
2185 }
2186 mPerProviderSavedVMNumbers.edit()
2187 .putString(key + VM_NUMBER_TAG, null)
2188 .putInt(key + FWD_SETTINGS_TAG + FWD_SETTINGS_LENGTH_TAG, 0)
2189 .commit();
2190 }
2191
2192 private String getCurrentVoicemailProviderKey() {
2193 final String key = mVoicemailProviders.getValue();
2194 return (key != null) ? key : DEFAULT_VM_PROVIDER_KEY;
2195 }
2196
2197 @Override
2198 public boolean onOptionsItemSelected(MenuItem item) {
2199 final int itemId = item.getItemId();
2200 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07002201 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002202 return true;
2203 }
2204 return super.onOptionsItemSelected(item);
2205 }
2206
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002207 /**
2208 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
2209 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
2210 */
2211 public static void goUpToTopLevelSetting(Activity activity) {
2212 Intent intent = new Intent(activity, CallFeaturesSetting.class);
2213 intent.setAction(Intent.ACTION_MAIN);
2214 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
2215 activity.startActivity(intent);
2216 activity.finish();
2217 }
2218}