blob: a97f5d012f4cf0ff62e32fb6f7009b74a06a98cd [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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.ContentResolver;
25import android.content.Context;
26import android.content.DialogInterface;
27import android.content.Intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.database.Cursor;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.media.AudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070030import android.os.AsyncResult;
31import android.os.Bundle;
32import android.os.Handler;
33import android.os.Message;
34import android.os.UserHandle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.preference.CheckBoxPreference;
36import android.preference.ListPreference;
37import android.preference.Preference;
38import android.preference.PreferenceActivity;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.preference.PreferenceScreen;
40import android.provider.ContactsContract.CommonDataKinds;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070042import android.telecom.PhoneAccountHandle;
43import android.telecom.TelecomManager;
Etan Cohena51d4ef2014-12-15 10:44:42 -080044import android.telephony.PhoneStateListener;
Andrew Lee93c345f2014-10-27 15:25:07 -070045import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import android.text.TextUtils;
47import android.util.Log;
48import android.view.MenuItem;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.widget.ListAdapter;
Jay Shraunerc2684732014-11-12 12:10:37 -080050import android.widget.Toast;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051
Andrew Lee312e8172014-10-23 17:01:36 -070052import com.android.ims.ImsManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import com.android.internal.telephony.CallForwardInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import com.android.internal.telephony.Phone;
55import com.android.internal.telephony.PhoneConstants;
Andrew Lee2170a972014-08-13 18:13:01 -070056import com.android.phone.common.util.SettingsUtil;
Andrew Leedb2fe562014-09-03 15:40:43 -070057import com.android.phone.settings.AccountSelectionPreference;
Andrew Lee1af6cf72014-11-04 17:35:26 -080058import com.android.phone.settings.CallForwardInfoUtil;
Andrew Lee6fee2cb2014-12-09 16:42:18 -080059import com.android.phone.settings.TtyModeListPreference;
Andrew Lee84024342014-11-06 23:37:09 -080060import com.android.phone.settings.VoicemailDialogUtil;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -080061import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Andrew Lee64e5c9d2014-11-26 12:00:10 -080062import com.android.phone.settings.VoicemailProviderListPreference;
63import com.android.phone.settings.VoicemailProviderListPreference.VoicemailProvider;
Andrew Leeb490d732014-10-27 15:00:41 -070064import com.android.phone.settings.VoicemailProviderSettings;
Andrew Lee88b51e22014-10-29 15:48:51 -070065import com.android.phone.settings.VoicemailProviderSettingsUtil;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -080066import com.android.phone.settings.VoicemailRingtonePreference;
Andrew Lee5ed870c2014-10-29 11:47:49 -070067import com.android.phone.settings.fdn.FdnSetting;
Sailesh Nepal788959e2014-07-08 23:36:40 -070068import com.android.services.telephony.sip.SipUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069
Andrew Lee2170a972014-08-13 18:13:01 -070070import java.lang.String;
Andrew Leef1776d82014-11-04 14:45:02 -080071import java.util.ArrayList;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072import java.util.Collection;
73import java.util.HashMap;
74import java.util.HashSet;
75import java.util.Iterator;
76import java.util.List;
77import java.util.Map;
78
79/**
80 * Top level "Call settings" UI; see res/xml/call_feature_setting.xml
81 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070082 * This preference screen is the root of the "Call settings" hierarchy available from the Phone
83 * app; the settings here let you control various features related to phone calls (including
84 * voicemail settings, the "Respond via SMS" feature, and others.) It's used only on
85 * voice-capable phone devices.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070086 *
87 * Note that this activity is part of the package com.android.phone, even
88 * though you reach it from the "Phone" app (i.e. DialtactsActivity) which
89 * is from the package com.android.contacts.
90 *
91 * For the "Mobile network settings" screen under the main Settings app,
92 * See {@link MobileNetworkSettings}.
93 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070094 * TODO: Settings should be split into PreferenceFragments where possible (ie. voicemail).
Andrew Lee2170a972014-08-13 18:13:01 -070095 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -070096 * @see com.android.phone.MobileNetworkSettings
97 */
98public class CallFeaturesSetting extends PreferenceActivity
99 implements DialogInterface.OnClickListener,
Andrew Lee2170a972014-08-13 18:13:01 -0700100 Preference.OnPreferenceChangeListener,
Andrew Lee2170a972014-08-13 18:13:01 -0700101 EditPhoneNumberPreference.OnDialogClosedListener,
Andrew Leece8ae2a2014-09-10 10:41:48 -0700102 EditPhoneNumberPreference.GetDefaultNumberListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700103 private static final String LOG_TAG = "CallFeaturesSetting";
104 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
Andrew Lee77527ac2014-10-21 16:57:39 -0700105 // STOPSHIP if true. Flag to override behavior default behavior to hide VT setting.
106 private static final boolean ENABLE_VT_FLAG = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107
108 /**
109 * Intent action to bring up Voicemail Provider settings.
110 *
111 * @see #IGNORE_PROVIDER_EXTRA
112 */
113 public static final String ACTION_ADD_VOICEMAIL =
114 "com.android.phone.CallFeaturesSetting.ADD_VOICEMAIL";
115 // intent action sent by this activity to a voice mail provider
116 // to trigger its configuration UI
117 public static final String ACTION_CONFIGURE_VOICEMAIL =
118 "com.android.phone.CallFeaturesSetting.CONFIGURE_VOICEMAIL";
119 // Extra put in the return from VM provider config containing voicemail number to set
120 public static final String VM_NUMBER_EXTRA = "com.android.phone.VoicemailNumber";
121 // Extra put in the return from VM provider config containing call forwarding number to set
122 public static final String FWD_NUMBER_EXTRA = "com.android.phone.ForwardingNumber";
123 // Extra put in the return from VM provider config containing call forwarding number to set
124 public static final String FWD_NUMBER_TIME_EXTRA = "com.android.phone.ForwardingNumberTime";
125 // If the VM provider returns non null value in this extra we will force the user to
126 // choose another VM provider
127 public static final String SIGNOUT_EXTRA = "com.android.phone.Signout";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700129 /**
130 * String Extra put into ACTION_ADD_VOICEMAIL call to indicate which provider should be hidden
131 * in the list of providers presented to the user. This allows a provider which is being
132 * disabled (e.g. GV user logging out) to force the user to pick some other provider.
133 */
134 public static final String IGNORE_PROVIDER_EXTRA = "com.android.phone.ProviderToIgnore";
135
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800136 /**
137 * String Extra put into ACTION_ADD_VOICEMAIL to indicate that the voicemail setup screen should
138 * be opened.
139 */
140 public static final String SETUP_VOICEMAIL_EXTRA = "com.android.phone.SetupVoicemail";
141
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700142 // string constants
143 private static final String NUM_PROJECTION[] = {CommonDataKinds.Phone.NUMBER};
144
145 // String keys for preference lookup
146 // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
Andrew Lee8d66d812014-11-24 14:54:02 -0800147 // TODO: Consider moving these strings to strings.xml, so that they are not duplicated here and
148 // in the layout files. These strings need to be treated carefully; if the setting is
149 // persistent, they are used as the key to store shared preferences and the name should not be
150 // changed unless the settings are also migrated.
Andrew Lee97708a42014-09-25 12:39:07 -0700151 private static final String VOICEMAIL_SETTING_SCREEN_PREF_KEY = "button_voicemail_category_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700152 private static final String BUTTON_VOICEMAIL_KEY = "button_voicemail_key";
153 private static final String BUTTON_VOICEMAIL_PROVIDER_KEY = "button_voicemail_provider_key";
154 private static final String BUTTON_VOICEMAIL_SETTING_KEY = "button_voicemail_setting_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700155 private static final String BUTTON_FDN_KEY = "button_fdn_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700156
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700157 private static final String BUTTON_DTMF_KEY = "button_dtmf_settings";
158 private static final String BUTTON_RETRY_KEY = "button_auto_retry_key";
159 private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
160 private static final String BUTTON_HAC_KEY = "button_hac_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700161
162 private static final String BUTTON_GSM_UMTS_OPTIONS = "button_gsm_more_expand_key";
163 private static final String BUTTON_CDMA_OPTIONS = "button_cdma_more_expand_key";
Andrew Lee2b36ba22014-11-05 17:08:49 -0800164 private static final String CALL_FORWARDING_KEY = "call_forwarding_key";
165 private static final String ADDITIONAL_GSM_SETTINGS_KEY = "additional_gsm_call_settings_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166
Andrew Leece8ae2a2014-09-10 10:41:48 -0700167 private static final String PHONE_ACCOUNT_SETTINGS_KEY =
168 "phone_account_settings_preference_screen";
Andrew Leedb2fe562014-09-03 15:40:43 -0700169
Andrew Leedf14ead2014-10-17 14:22:52 -0700170 private static final String ENABLE_VIDEO_CALLING_KEY = "button_enable_video_calling";
171
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700172 /** Event for Async voicemail change call */
173 private static final int EVENT_VOICEMAIL_CHANGED = 500;
174 private static final int EVENT_FORWARDING_CHANGED = 501;
175 private static final int EVENT_FORWARDING_GET_COMPLETED = 502;
176
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700177 public static final String HAC_KEY = "HACSetting";
178 public static final String HAC_VAL_ON = "ON";
179 public static final String HAC_VAL_OFF = "OFF";
180
181 /** Handle to voicemail pref */
182 private static final int VOICEMAIL_PREF_ID = 1;
183 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
184
185 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700186 private AudioManager mAudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700187
Andrew Lee5ed870c2014-10-29 11:47:49 -0700188 private SubscriptionInfoHelper mSubscriptionInfoHelper;
189
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700190 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
191
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700192 /** Whether dialpad plays DTMF tone or not. */
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700193 private CheckBoxPreference mButtonAutoRetry;
194 private CheckBoxPreference mButtonHAC;
195 private ListPreference mButtonDTMF;
Andrew Lee6fee2cb2014-12-09 16:42:18 -0800196 private TtyModeListPreference mButtonTTY;
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800197 private VoicemailProviderListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700198 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700199 private PreferenceScreen mVoicemailSettings;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800200 private VoicemailRingtonePreference mVoicemailNotificationRingtone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700201 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700202 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700203
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700204 /**
205 * Results of reading forwarding settings
206 */
207 private CallForwardInfo[] mForwardingReadResults = null;
208
209 /**
210 * Result of forwarding number change.
211 * Keys are reasons (eg. unconditional forwarding).
212 */
213 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
214
215 /**
216 * Expected CF read result types.
217 * This set keeps track of the CF types for which we've issued change
218 * commands so we can tell when we've received all of the responses.
219 */
220 private Collection<Integer> mExpectedChangeResultReasons = null;
221
222 /**
223 * Result of vm number change
224 */
225 private AsyncResult mVoicemailChangeResult = null;
226
227 /**
228 * Previous VM provider setting so we can return to it in case of failure.
229 */
230 private String mPreviousVMProviderKey = null;
231
232 /**
233 * Id of the dialog being currently shown.
234 */
235 private int mCurrentDialogId = 0;
236
237 /**
238 * Flag indicating that we are invoking settings for the voicemail provider programmatically
239 * due to vm provider change.
240 */
241 private boolean mVMProviderSettingsForced = false;
242
243 /**
244 * Flag indicating that we are making changes to vm or fwd numbers
245 * due to vm provider change.
246 */
247 private boolean mChangingVMorFwdDueToProviderChange = false;
248
249 /**
250 * True if we are in the process of vm & fwd number change and vm has already been changed.
251 * This is used to decide what to do in case of rollback.
252 */
253 private boolean mVMChangeCompletedSuccessfully = false;
254
255 /**
256 * True if we had full or partial failure setting forwarding numbers and so need to roll them
257 * back.
258 */
259 private boolean mFwdChangesRequireRollback = false;
260
261 /**
262 * Id of error msg to display to user once we are done reverting the VM provider to the previous
263 * one.
264 */
265 private int mVMOrFwdSetError = 0;
266
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700267 /** string to hold old voicemail number as it is being updated. */
268 private String mOldVmNumber;
269
270 // New call forwarding settings and vm number we will be setting
271 // Need to save these since before we get to saving we need to asynchronously
272 // query the existing forwarding settings.
273 private CallForwardInfo[] mNewFwdSettings;
274 private String mNewVMNumber;
275
276 private boolean mForeground;
277
278 @Override
279 public void onPause() {
280 super.onPause();
281 mForeground = false;
Etan Cohena51d4ef2014-12-15 10:44:42 -0800282
283 if (ImsManager.isVolteEnabledByPlatform(this) &&
284 !mPhone.getContext().getResources().getBoolean(
285 com.android.internal.R.bool.config_carrier_volte_tty_supported)) {
286 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
287 tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
288 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700289 }
290
291 /**
292 * We have to pull current settings from the network for all kinds of
293 * voicemail providers so we can tell whether we have to update them,
294 * so use this bit to keep track of whether we're reading settings for the
295 * default provider and should therefore save them out when done.
296 */
297 private boolean mReadingSettingsForDefaultProvider = false;
298
Tyler Gunnbaee2952014-09-10 16:01:02 -0700299 /**
300 * Used to indicate that the voicemail preference should be shown.
301 */
302 private boolean mShowVoicemailPreference = false;
303
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800304 /**
305 * Used to indicate that the voicemail setup screen should be shown.
306 */
307 private boolean mSetupVoicemail = false;
308
Etan Cohena51d4ef2014-12-15 10:44:42 -0800309 private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
310 /**
311 * Enable/disable the TTY setting when in/out of a call (and if carrier doesn't
312 * support VoLTE with TTY).
313 * @see android.telephony.PhoneStateListener#onCallStateChanged(int,
314 * java.lang.String)
315 */
316 @Override
317 public void onCallStateChanged(int state, String incomingNumber) {
318 if (DBG) log("PhoneStateListener.onCallStateChanged: state=" + state);
319 Preference pref = getPreferenceScreen().findPreference(BUTTON_TTY_KEY);
320 if (pref != null) {
321 pref.setEnabled(state == TelephonyManager.CALL_STATE_IDLE);
322 }
323 }
324 };
325
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700326 /*
327 * Click Listeners, handle click based on objects attached to UI.
328 */
329
330 // Click listener for all toggle events
331 @Override
332 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
333 if (preference == mSubMenuVoicemailSettings) {
334 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700335 } else if (preference == mButtonDTMF) {
336 return true;
337 } else if (preference == mButtonTTY) {
338 return true;
339 } else if (preference == mButtonAutoRetry) {
340 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
341 android.provider.Settings.Global.CALL_AUTO_RETRY,
342 mButtonAutoRetry.isChecked() ? 1 : 0);
343 return true;
344 } else if (preference == mButtonHAC) {
345 int hac = mButtonHAC.isChecked() ? 1 : 0;
346 // Update HAC value in Settings database
347 Settings.System.putInt(mPhone.getContext().getContentResolver(),
348 Settings.System.HEARING_AID, hac);
349
350 // Update HAC Value in AudioManager
351 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
352 return true;
Andrew Lee696bff52014-12-03 17:58:18 -0800353 } else if (preference.getKey().equals(mVoicemailSettings.getKey())) {
354 // Check key instead of comparing reference because closing the voicemail notification
355 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
356 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800357 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
358
Andrew Lee696bff52014-12-03 17:58:18 -0800359 final Dialog dialog = ((PreferenceScreen) preference).getDialog();
Yorke Leea0f63bf2014-10-09 18:27:20 -0700360 if (dialog != null) {
361 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
362 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800363
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700364 if (preference.getIntent() != null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800365 if (DBG) log("Invoking cfg intent " + preference.getIntent().getPackage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700366
367 // onActivityResult() will be responsible for resetting some of variables.
368 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
369 return true;
370 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800371 if (DBG) log("onPreferenceTreeClick(). No intent; use default behavior in xml.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700372
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800373 // onActivityResult() will not be called, so reset variables here.
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800374 mPreviousVMProviderKey = VoicemailProviderListPreference.DEFAULT_KEY;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700375 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700376 return false;
377 }
Andrew Lee97708a42014-09-25 12:39:07 -0700378 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700379 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
380 if (dialog != null) {
381 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
382 }
Andrew Lee97708a42014-09-25 12:39:07 -0700383 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700384 }
385 return false;
386 }
387
388 /**
389 * Implemented to support onPreferenceChangeListener to look for preference
390 * changes.
391 *
392 * @param preference is the preference to be changed
393 * @param objValue should be the value of the selection, NOT its localized
394 * display value.
395 */
396 @Override
397 public boolean onPreferenceChange(Preference preference, Object objValue) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800398 if (DBG) log("onPreferenceChange: \"" + preference + "\" changed to \"" + objValue + "\"");
Andrew Lee2170a972014-08-13 18:13:01 -0700399
400 if (preference == mButtonDTMF) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700401 int index = mButtonDTMF.findIndexOfValue((String) objValue);
402 Settings.System.putInt(mPhone.getContext().getContentResolver(),
403 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700404 } else if (preference == mVoicemailProviders) {
405 final String newProviderKey = (String) objValue;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800406
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700407 // If previous provider key and the new one is same, we don't need to handle it.
408 if (mPreviousVMProviderKey.equals(newProviderKey)) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800409 if (DBG) log("No change is made to the VM provider setting.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700410 return true;
411 }
412 updateVMPreferenceWidgets(newProviderKey);
413
Andrew Leeb490d732014-10-27 15:00:41 -0700414 final VoicemailProviderSettings newProviderSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800415 VoicemailProviderSettingsUtil.load(this, newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700416
Andrew Lee88b51e22014-10-29 15:48:51 -0700417 // If the user switches to a voice mail provider and we have numbers stored for it we
418 // will automatically change the phone's voice mail and forwarding number to the stored
419 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700420 if (newProviderSettings == null) {
421 // Force the user into a configuration of the chosen provider
422 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
423 mVMProviderSettingsForced = true;
424 simulatePreferenceClick(mVoicemailSettings);
425 } else {
426 if (DBG) log("Saved preferences found - switching to them");
427 // Set this flag so if we get a failure we revert to previous provider
428 mChangingVMorFwdDueToProviderChange = true;
429 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
430 }
Andrew Lee8d66d812014-11-24 14:54:02 -0800431 } else if (preference.getKey().equals(mVoicemailNotificationVibrate.getKey())) {
Andrew Lee696bff52014-12-03 17:58:18 -0800432 // Check key instead of comparing reference because closing the voicemail notification
433 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
434 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee8d66d812014-11-24 14:54:02 -0800435 VoicemailNotificationSettingsUtil.setVibrationEnabled(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800436 mPhone, Boolean.TRUE.equals(objValue));
Andrew Leedf14ead2014-10-17 14:22:52 -0700437 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700438 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
439 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
440 } else {
441 AlertDialog.Builder builder = new AlertDialog.Builder(this);
442 DialogInterface.OnClickListener networkSettingsClickListener =
443 new Dialog.OnClickListener() {
444 @Override
445 public void onClick(DialogInterface dialog, int which) {
446 startActivity(new Intent(mPhone.getContext(),
447 com.android.phone.MobileNetworkSettings.class));
448 }
449 };
450 builder.setMessage(getResources().getString(
451 R.string.enable_video_calling_dialog_msg))
452 .setNeutralButton(getResources().getString(
453 R.string.enable_video_calling_dialog_settings),
454 networkSettingsClickListener)
455 .setPositiveButton(android.R.string.ok, null)
456 .show();
457 return false;
458 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700459 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800460
461 // Always let the preference setting proceed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700462 return true;
463 }
464
465 @Override
466 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800467 if (DBG) log("onDialogClosed: Button clicked is " + buttonClicked);
468
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700469 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
470 return;
471 }
472
473 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700474 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
475 mSubMenuVoicemailSettings.getPhoneNumber(),
476 VoicemailProviderSettings.NO_FORWARDING);
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800477 saveVoiceMailAndForwardingNumber(mVoicemailProviders.getKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700478 }
479 }
480
481 /**
482 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
483 * This method set the default values for the various
484 * EditPhoneNumberPreference dialogs.
485 */
486 @Override
487 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
488 if (preference == mSubMenuVoicemailSettings) {
489 // update the voicemail number field, which takes care of the
490 // mSubMenuVoicemailSettings itself, so we should return null.
491 if (DBG) log("updating default for voicemail dialog");
492 updateVoiceNumberField();
493 return null;
494 }
495
496 String vmDisplay = mPhone.getVoiceMailNumber();
497 if (TextUtils.isEmpty(vmDisplay)) {
498 // if there is no voicemail number, we just return null to
499 // indicate no contribution.
500 return null;
501 }
502
503 // Return the voicemail number prepended with "VM: "
504 if (DBG) log("updating default for call forwarding dialogs");
505 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
506 }
507
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700508 private void switchToPreviousVoicemailProvider() {
509 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700510
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800511 if (mPreviousVMProviderKey == null) {
512 return;
513 }
514
515 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
Andrew Lee84024342014-11-06 23:37:09 -0800516 showDialogIfForeground(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800517 final VoicemailProviderSettings prevSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800518 VoicemailProviderSettingsUtil.load(this, mPreviousVMProviderKey);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800519 if (prevSettings == null) {
520 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
521 + mPreviousVMProviderKey + "\" is null but should be loaded.");
522 }
523
524 if (mVMChangeCompletedSuccessfully) {
525 mNewVMNumber = prevSettings.getVoicemailNumber();
526 Log.i(LOG_TAG, "VM change is already completed successfully."
527 + "Have to revert VM back to " + mNewVMNumber + " again.");
528 mPhone.setVoiceMailNumber(
529 mPhone.getVoiceMailAlphaTag().toString(),
530 mNewVMNumber,
531 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
532 }
533
534 if (mFwdChangesRequireRollback) {
535 Log.i(LOG_TAG, "Requested to rollback forwarding changes.");
536
537 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
538 if (prevFwdSettings != null) {
539 Map<Integer, AsyncResult> results = mForwardingChangeResults;
540 resetForwardingChangeState();
541 for (int i = 0; i < prevFwdSettings.length; i++) {
542 CallForwardInfo fi = prevFwdSettings[i];
543 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
544 // Only revert the settings for which the update succeeded.
545 AsyncResult result = results.get(fi.reason);
546 if (result != null && result.exception == null) {
547 mExpectedChangeResultReasons.add(fi.reason);
548 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
549 mRevertOptionComplete.obtainMessage(
550 EVENT_FORWARDING_CHANGED, i, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700551 }
552 }
553 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700554 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800555 } else {
556 if (DBG) log("No need to revert");
557 onRevertDone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700558 }
559 }
560
561 private void onRevertDone() {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800562 if (DBG) log("onRevertDone: Changing provider key back to " + mPreviousVMProviderKey);
563
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700564 updateVMPreferenceWidgets(mPreviousVMProviderKey);
565 updateVoiceNumberField();
566 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800567 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700568 mVMOrFwdSetError = 0;
569 }
570 }
571
572 @Override
573 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
574 if (DBG) {
575 log("onActivityResult: requestCode: " + requestCode
576 + ", resultCode: " + resultCode
577 + ", data: " + data);
578 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800579
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700580 // there are cases where the contact picker may end up sending us more than one
581 // request. We want to ignore the request if we're not in the correct state.
582 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
583 boolean failure = false;
584
585 // No matter how the processing of result goes lets clear the flag
586 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
587 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
588 mVMProviderSettingsForced = false;
589
590 String vmNum = null;
591 if (resultCode != RESULT_OK) {
592 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
593 failure = true;
594 } else {
595 if (data == null) {
596 if (DBG) log("onActivityResult: vm provider cfg result has no data");
597 failure = true;
598 } else {
599 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
600 if (DBG) log("Provider requested signout");
601 if (isVMProviderSettingsForced) {
602 if (DBG) log("Going back to previous provider on signout");
603 switchToPreviousVoicemailProvider();
604 } else {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800605 final String victim = mVoicemailProviders.getKey();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700606 if (DBG) log("Relaunching activity and ignoring " + victim);
607 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
608 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
609 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
610 this.startActivity(i);
611 }
612 return;
613 }
614 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
615 if (vmNum == null || vmNum.length() == 0) {
616 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
617 failure = true;
618 }
619 }
620 }
621 if (failure) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800622 if (DBG) log("Failure in return from voicemail provider.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700623 if (isVMProviderSettingsForced) {
624 switchToPreviousVoicemailProvider();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700625 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800626
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700627 return;
628 }
629 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
630 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
631
Santos Cordonda120f42014-08-06 04:44:34 -0700632 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700633 // send it to the provider when it's config is invoked so it can use this as default
634 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
635
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800636 if (DBG) log("onActivityResult: cfg result has forwarding number " + fwdNum);
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800637 saveVoiceMailAndForwardingNumber(mVoicemailProviders.getKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700638 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700639 return;
640 }
641
642 if (requestCode == VOICEMAIL_PREF_ID) {
643 if (resultCode != RESULT_OK) {
644 if (DBG) log("onActivityResult: contact picker result not OK.");
645 return;
646 }
647
648 Cursor cursor = null;
649 try {
650 cursor = getContentResolver().query(data.getData(),
651 NUM_PROJECTION, null, null, null);
652 if ((cursor == null) || (!cursor.moveToFirst())) {
653 if (DBG) log("onActivityResult: bad contact data, no results found.");
654 return;
655 }
656 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
657 return;
658 } finally {
659 if (cursor != null) {
660 cursor.close();
661 }
662 }
663 }
664
665 super.onActivityResult(requestCode, resultCode, data);
666 }
667
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700668 /**
669 * Wrapper around showDialog() that will silently do nothing if we're
670 * not in the foreground.
671 *
672 * This is useful here because most of the dialogs we display from
673 * this class are triggered by asynchronous events (like
674 * success/failure messages from the telephony layer) and it's
675 * possible for those events to come in even after the user has gone
676 * to a different screen.
677 */
678 // TODO: this is too brittle: it's still easy to accidentally add new
679 // code here that calls showDialog() directly (which will result in a
680 // WindowManager$BadTokenException if called after the activity has
681 // been stopped.)
682 //
683 // It would be cleaner to do the "if (mForeground)" check in one
684 // central place, maybe by using a single Handler for all asynchronous
685 // events (and have *that* discard events if we're not in the
686 // foreground.)
687 //
688 // Unfortunately it's not that simple, since we sometimes need to do
689 // actual work to handle these events whether or not we're in the
690 // foreground (see the Handler code in mSetOptionComplete for
691 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800692 //
693 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
694 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700695 private void showDialogIfForeground(int id) {
696 if (mForeground) {
697 showDialog(id);
698 }
699 }
700
701 private void dismissDialogSafely(int id) {
702 try {
703 dismissDialog(id);
704 } catch (IllegalArgumentException e) {
705 // This is expected in the case where we were in the background
706 // at the time we would normally have shown the dialog, so we didn't
707 // show it.
708 }
709 }
710
Andrew Leeb490d732014-10-27 15:00:41 -0700711 private void saveVoiceMailAndForwardingNumber(
712 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700713 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700714 mNewVMNumber = newSettings.getVoicemailNumber();
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800715 mNewVMNumber = (mNewVMNumber == null) ? "" : mNewVMNumber;
Andrew Leeb490d732014-10-27 15:00:41 -0700716 mNewFwdSettings = newSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700717
718 // No fwd settings on CDMA
719 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
720 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700721 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700722 }
723
Andrew Leee3c15212014-10-28 13:12:55 -0700724 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700725 if (mNewVMNumber.equals(mOldVmNumber)
726 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Lee84024342014-11-06 23:37:09 -0800727 showDialogIfForeground(VoicemailDialogUtil.VM_NOCHANGE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700728 return;
729 }
730
Andrew Lee6950c6c2014-11-26 12:05:42 -0800731 VoicemailProviderSettingsUtil.save(this, key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700732 mVMChangeCompletedSuccessfully = false;
733 mFwdChangesRequireRollback = false;
734 mVMOrFwdSetError = 0;
735 if (!key.equals(mPreviousVMProviderKey)) {
736 mReadingSettingsForDefaultProvider =
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800737 mPreviousVMProviderKey.equals(VoicemailProviderListPreference.DEFAULT_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700738 if (DBG) log("Reading current forwarding settings");
Andrew Leeb490d732014-10-27 15:00:41 -0700739 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
740 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
741 for (int i = 0; i < mForwardingReadResults.length; i++) {
742 mPhone.getCallForwardingOption(
743 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700744 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
745 }
Andrew Lee84024342014-11-06 23:37:09 -0800746 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700747 } else {
748 saveVoiceMailAndForwardingNumberStage2();
749 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800750
751 // Refresh the MWI indicator if it is already showing.
752 PhoneGlobals.getInstance().refreshMwiIndicator(mSubscriptionInfoHelper.getSubId());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700753 }
754
755 private final Handler mGetOptionComplete = new Handler() {
756 @Override
757 public void handleMessage(Message msg) {
758 AsyncResult result = (AsyncResult) msg.obj;
759 switch (msg.what) {
760 case EVENT_FORWARDING_GET_COMPLETED:
761 handleForwardingSettingsReadResult(result, msg.arg1);
762 break;
763 }
764 }
765 };
766
767 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
768 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800769
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700770 Throwable error = null;
771 if (ar.exception != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700772 error = ar.exception;
Andrew Lee1af6cf72014-11-04 17:35:26 -0800773 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700774 }
775 if (ar.userObj instanceof Throwable) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800776 error = (Throwable) ar.userObj;
777 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700778 }
779
780 // We may have already gotten an error and decided to ignore the other results.
781 if (mForwardingReadResults == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800782 if (DBG) Log.d(LOG_TAG, "Ignoring fwd reading result: " + idx);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700783 return;
784 }
785
786 // In case of error ignore other results, show an error dialog
787 if (error != null) {
788 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
789 mForwardingReadResults = null;
Andrew Lee84024342014-11-06 23:37:09 -0800790 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
791 showDialogIfForeground(VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700792 return;
793 }
794
Andrew Lee1af6cf72014-11-04 17:35:26 -0800795 // Get the forwarding info.
796 mForwardingReadResults[idx] = CallForwardInfoUtil.getCallForwardInfo(
797 (CallForwardInfo[]) ar.result,
798 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx]);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700799
800 // Check if we got all the results already
801 boolean done = true;
802 for (int i = 0; i < mForwardingReadResults.length; i++) {
803 if (mForwardingReadResults[i] == null) {
804 done = false;
805 break;
806 }
807 }
Andrew Lee1af6cf72014-11-04 17:35:26 -0800808
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700809 if (done) {
810 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
Andrew Lee84024342014-11-06 23:37:09 -0800811 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800812
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700813 if (mReadingSettingsForDefaultProvider) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800814 VoicemailProviderSettingsUtil.save(mPhone.getContext(),
815 VoicemailProviderListPreference.DEFAULT_KEY,
Andrew Lee88b51e22014-10-29 15:48:51 -0700816 new VoicemailProviderSettings(this.mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700817 mReadingSettingsForDefaultProvider = false;
818 }
819 saveVoiceMailAndForwardingNumberStage2();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700820 }
821 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800822
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700823 private void resetForwardingChangeState() {
824 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
825 mExpectedChangeResultReasons = new HashSet<Integer>();
826 }
827
828 // Called after we are done saving the previous forwarding settings if
829 // we needed.
830 private void saveVoiceMailAndForwardingNumberStage2() {
831 mForwardingChangeResults = null;
832 mVoicemailChangeResult = null;
Andrew Leeb490d732014-10-27 15:00:41 -0700833 if (mNewFwdSettings != VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700834 resetForwardingChangeState();
835 for (int i = 0; i < mNewFwdSettings.length; i++) {
836 CallForwardInfo fi = mNewFwdSettings[i];
Andrew Lee1af6cf72014-11-04 17:35:26 -0800837 CallForwardInfo fiForReason =
838 CallForwardInfoUtil.infoForReason(mForwardingReadResults, fi.reason);
839 final boolean doUpdate = CallForwardInfoUtil.isUpdateRequired(fiForReason, fi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700840
841 if (doUpdate) {
842 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
843 mExpectedChangeResultReasons.add(i);
844
Andrew Lee1af6cf72014-11-04 17:35:26 -0800845 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700846 mSetOptionComplete.obtainMessage(
847 EVENT_FORWARDING_CHANGED, fi.reason, 0));
848 }
849 }
Andrew Lee84024342014-11-06 23:37:09 -0800850 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700851 } else {
852 if (DBG) log("Not touching fwd #");
853 setVMNumberWithCarrier();
854 }
855 }
856
857 private void setVMNumberWithCarrier() {
858 if (DBG) log("save voicemail #: " + mNewVMNumber);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800859
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700860 mPhone.setVoiceMailNumber(
861 mPhone.getVoiceMailAlphaTag().toString(),
862 mNewVMNumber,
863 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
864 }
865
866 /**
867 * Callback to handle option update completions
868 */
869 private final Handler mSetOptionComplete = new Handler() {
870 @Override
871 public void handleMessage(Message msg) {
872 AsyncResult result = (AsyncResult) msg.obj;
873 boolean done = false;
874 switch (msg.what) {
875 case EVENT_VOICEMAIL_CHANGED:
876 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700877 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700878 done = true;
879 break;
880 case EVENT_FORWARDING_CHANGED:
881 mForwardingChangeResults.put(msg.arg1, result);
882 if (result.exception != null) {
883 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
884 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700885 }
Andrew Leee438b312014-10-29 16:59:15 -0700886 if (isForwardingCompleted()) {
887 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700888 if (DBG) log("Overall fwd changes completed ok, starting vm change");
889 setVMNumberWithCarrier();
890 } else {
891 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
892 "Check if we need to try rollback for some settings.");
893 mFwdChangesRequireRollback = false;
894 Iterator<Map.Entry<Integer,AsyncResult>> it =
895 mForwardingChangeResults.entrySet().iterator();
896 while (it.hasNext()) {
897 Map.Entry<Integer,AsyncResult> entry = it.next();
898 if (entry.getValue().exception == null) {
899 // If at least one succeeded we have to revert
900 Log.i(LOG_TAG, "Rollback will be required");
901 mFwdChangesRequireRollback = true;
902 break;
903 }
904 }
905 if (!mFwdChangesRequireRollback) {
906 Log.i(LOG_TAG, "No rollback needed.");
907 }
908 done = true;
909 }
910 }
911 break;
912 default:
913 // TODO: should never reach this, may want to throw exception
914 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800915
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700916 if (done) {
917 if (DBG) log("All VM provider related changes done");
918 if (mForwardingChangeResults != null) {
Andrew Lee84024342014-11-06 23:37:09 -0800919 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700920 }
Andrew Leee438b312014-10-29 16:59:15 -0700921 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700922 }
923 }
924 };
925
926 /**
927 * Callback to handle option revert completions
928 */
929 private final Handler mRevertOptionComplete = new Handler() {
930 @Override
931 public void handleMessage(Message msg) {
932 AsyncResult result = (AsyncResult) msg.obj;
933 switch (msg.what) {
934 case EVENT_VOICEMAIL_CHANGED:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700935 if (DBG) log("VM revert complete msg");
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800936 mVoicemailChangeResult = result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700937 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800938
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700939 case EVENT_FORWARDING_CHANGED:
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800940 if (DBG) log("FWD revert complete msg ");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700941 mForwardingChangeResults.put(msg.arg1, result);
942 if (result.exception != null) {
943 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
944 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700945 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700946 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800947
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700948 default:
949 // TODO: should never reach this, may want to throw exception
950 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800951
952 final boolean done = (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null)
953 && (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700954 if (done) {
955 if (DBG) log("All VM reverts done");
Andrew Lee84024342014-11-06 23:37:09 -0800956 dismissDialogSafely(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700957 onRevertDone();
958 }
959 }
960 };
961
962 /**
Andrew Leee438b312014-10-29 16:59:15 -0700963 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700964 */
Andrew Leee438b312014-10-29 16:59:15 -0700965 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700966 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -0700967 return true;
968 }
969
970 for (Integer reason : mExpectedChangeResultReasons) {
971 if (mForwardingChangeResults.get(reason) == null) {
972 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700973 }
974 }
Andrew Leee438b312014-10-29 16:59:15 -0700975
976 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700977 }
Andrew Leee438b312014-10-29 16:59:15 -0700978
979 private boolean isFwdChangeSuccess() {
980 if (mForwardingChangeResults == null) {
981 return true;
982 }
983
984 for (AsyncResult result : mForwardingChangeResults.values()) {
985 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700986 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700987 String msg = exception.getMessage();
988 msg = (msg != null) ? msg : "";
989 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
990 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700991 }
992 }
Andrew Leee438b312014-10-29 16:59:15 -0700993 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700994 }
995
Andrew Leee438b312014-10-29 16:59:15 -0700996 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700997 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700998 String msg = mVoicemailChangeResult.exception.getMessage();
999 msg = (msg != null) ? msg : "";
1000 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
1001 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001002 }
Andrew Leee438b312014-10-29 16:59:15 -07001003 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001004 }
1005
Andrew Leee438b312014-10-29 16:59:15 -07001006 private void handleSetVmOrFwdMessage() {
1007 if (DBG) log("handleSetVMMessage: set VM request complete");
1008
1009 if (!isFwdChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -08001010 handleVmOrFwdSetError(VoicemailDialogUtil.FWD_SET_RESPONSE_ERROR_DIALOG);
Andrew Leee438b312014-10-29 16:59:15 -07001011 } else if (!isVmChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -08001012 handleVmOrFwdSetError(VoicemailDialogUtil.VM_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001013 } else {
Andrew Lee84024342014-11-06 23:37:09 -08001014 handleVmAndFwdSetSuccess(VoicemailDialogUtil.VM_CONFIRM_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001015 }
1016 }
1017
1018 /**
1019 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
1020 * changes to those settings and show "failure" dialog.
1021 *
Andrew Leeab082272014-11-04 15:50:42 -08001022 * @param dialogId ID of the dialog to show for the specific error case. Either
Andrew Lee84024342014-11-06 23:37:09 -08001023 * {@link #FWD_SET_RESPONSE_ERROR_DIALOG} or {@link #VM_RESPONSE_ERROR_DIALOG}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001024 */
Andrew Leeab082272014-11-04 15:50:42 -08001025 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001026 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -08001027 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001028 mChangingVMorFwdDueToProviderChange = false;
1029 switchToPreviousVoicemailProvider();
1030 return;
1031 }
1032 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001033 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001034 updateVoiceNumberField();
1035 }
1036
1037 /**
1038 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1039 * This updates a bunch of variables and show "success" dialog.
1040 */
Andrew Leeab082272014-11-04 15:50:42 -08001041 private void handleVmAndFwdSetSuccess(int dialogId) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001042 if (DBG) log("handleVmAndFwdSetSuccess: key is " + mVoicemailProviders.getKey());
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001043
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001044 mPreviousVMProviderKey = mVoicemailProviders.getKey();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001045 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001046 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001047 updateVoiceNumberField();
1048 }
1049
1050 /**
1051 * Update the voicemail number from what we've recorded on the sim.
1052 */
1053 private void updateVoiceNumberField() {
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001054 if (DBG) log("updateVoiceNumberField()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001055
1056 mOldVmNumber = mPhone.getVoiceMailNumber();
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001057 if (TextUtils.isEmpty(mOldVmNumber)) {
1058 mSubMenuVoicemailSettings.setPhoneNumber("");
1059 mSubMenuVoicemailSettings.setSummary(getString(R.string.voicemail_number_not_set));
1060 } else {
1061 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1062 mSubMenuVoicemailSettings.setSummary(mOldVmNumber);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001063 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001064 }
1065
1066 /*
1067 * Helper Methods for Activity class.
1068 * The initial query commands are split into two pieces now
1069 * for individual expansion. This combined with the ability
1070 * to cancel queries allows for a much better user experience,
1071 * and also ensures that the user only waits to update the
1072 * data that is relevant.
1073 */
1074
1075 @Override
1076 protected void onPrepareDialog(int id, Dialog dialog) {
1077 super.onPrepareDialog(id, dialog);
1078 mCurrentDialogId = id;
1079 }
1080
1081 // dialog creation method, called by showDialog()
1082 @Override
Andrew Lee84024342014-11-06 23:37:09 -08001083 protected Dialog onCreateDialog(int dialogId) {
1084 return VoicemailDialogUtil.getDialog(this, dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001085 }
1086
1087 // This is a method implemented for DialogInterface.OnClickListener.
1088 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1089 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1090 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1091 public void onClick(DialogInterface dialog, int which) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001092 if (DBG) log("onClick: button clicked is " + which);
1093
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001094 dialog.dismiss();
1095 switch (which){
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001096 case DialogInterface.BUTTON_NEGATIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001097 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001098 // We failed to get current forwarding settings and the user
1099 // does not wish to continue.
1100 switchToPreviousVoicemailProvider();
1101 }
1102 break;
1103 case DialogInterface.BUTTON_POSITIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001104 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001105 // We failed to get current forwarding settings but the user
1106 // wishes to continue changing settings to the new vm provider
1107 saveVoiceMailAndForwardingNumberStage2();
1108 } else {
1109 finish();
1110 }
1111 return;
1112 default:
1113 // just let the dialog close and go back to the input
1114 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001115
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001116 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1117 // with settings UI. If we were called to explicitly configure voice mail then
1118 // we finish the settings activity here to come back to whatever the user was doing.
1119 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1120 finish();
1121 }
1122 }
1123
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001124 /*
1125 * Activity class methods
1126 */
1127
1128 @Override
1129 protected void onCreate(Bundle icicle) {
1130 super.onCreate(icicle);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001131 if (DBG) log("onCreate: Intent is " + getIntent());
1132
Jay Shraunerc2684732014-11-12 12:10:37 -08001133 // Make sure we are running as the primary user.
1134 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
1135 Toast.makeText(this, R.string.call_settings_primary_user_only,
1136 Toast.LENGTH_SHORT).show();
1137 finish();
1138 return;
1139 }
1140
Tyler Gunnbaee2952014-09-10 16:01:02 -07001141 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
1142
Tyler Gunnbaee2952014-09-10 16:01:02 -07001143 // Show the voicemail preference in onResume if the calling intent specifies the
1144 // ACTION_ADD_VOICEMAIL action.
1145 mShowVoicemailPreference = (icicle == null) &&
Jay Shraunerbe2fb262014-11-11 15:19:58 -08001146 TextUtils.equals(getIntent().getAction(), ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -08001147 mSetupVoicemail = mShowVoicemailPreference &&
1148 getIntent().getBooleanExtra(SETUP_VOICEMAIL_EXTRA, false);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001149
Andrew Leedd4f6df2014-12-09 19:13:51 -08001150 mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent());
Andrew Lee5ed870c2014-10-29 11:47:49 -07001151 mSubscriptionInfoHelper.setActionBarTitle(
1152 getActionBar(), getResources(), R.string.call_settings_with_label);
Andrew Lee1479dd12014-11-06 23:06:32 -08001153 mPhone = mSubscriptionInfoHelper.getPhone();
Tyler Gunnbaee2952014-09-10 16:01:02 -07001154 }
1155
Tyler Gunnbaee2952014-09-10 16:01:02 -07001156 @Override
1157 protected void onResume() {
1158 super.onResume();
1159 mForeground = true;
1160
1161 PreferenceScreen preferenceScreen = getPreferenceScreen();
1162 if (preferenceScreen != null) {
1163 preferenceScreen.removeAll();
1164 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001165
1166 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001167
Andrew Lee6fee2cb2014-12-09 16:42:18 -08001168 TelecomManager telecomManager = TelecomManager.from(this);
1169 TelephonyManager telephonyManager =
1170 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
1171
1172 Preference phoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
1173 if (telephonyManager.isMultiSimEnabled() || (telecomManager.getSimCallManagers().isEmpty()
1174 && !SipUtil.isVoipSupported(mPhone.getContext()))) {
1175 getPreferenceScreen().removePreference(phoneAccountSettingsPreference);
1176 }
Andrew Leedb2fe562014-09-03 15:40:43 -07001177
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001178 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001179 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1180 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1181 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1182 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001183
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001184 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1185 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1186 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
Andrew Lee6fee2cb2014-12-09 16:42:18 -08001187 mButtonTTY = (TtyModeListPreference) findPreference(
1188 getResources().getString(R.string.tty_mode_key));
Andrew Lee2170a972014-08-13 18:13:01 -07001189
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001190 mVoicemailProviders = (VoicemailProviderListPreference) findPreference(
1191 BUTTON_VOICEMAIL_PROVIDER_KEY);
1192 mVoicemailProviders.init(mPhone, getIntent());
Andrew Lee2c027892014-10-29 11:29:54 -07001193 mVoicemailProviders.setOnPreferenceChangeListener(this);
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001194 mPreviousVMProviderKey = mVoicemailProviders.getValue();
Andrew Lee8d66d812014-11-24 14:54:02 -08001195
Andrew Lee2c027892014-10-29 11:29:54 -07001196 mVoicemailSettingsScreen =
1197 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001198 mVoicemailSettings = (PreferenceScreen) findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
Andrew Lee8d66d812014-11-24 14:54:02 -08001199
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001200 mVoicemailNotificationRingtone = (VoicemailRingtonePreference) findPreference(
1201 getResources().getString(R.string.voicemail_notification_ringtone_key));
1202 mVoicemailNotificationRingtone.init(mPhone);
1203
Andrew Lee8d66d812014-11-24 14:54:02 -08001204 mVoicemailNotificationVibrate = (CheckBoxPreference) findPreference(
1205 getResources().getString(R.string.voicemail_notification_vibrate_key));
1206 mVoicemailNotificationVibrate.setOnPreferenceChangeListener(this);
1207
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001208 updateVMPreferenceWidgets(mVoicemailProviders.getValue());
1209
1210 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001211
Andrew Lee64a7d792014-10-15 17:38:38 -07001212 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1213 mButtonDTMF.setOnPreferenceChangeListener(this);
1214 int dtmf = Settings.System.getInt(getContentResolver(),
1215 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1216 mButtonDTMF.setValueIndex(dtmf);
1217 } else {
1218 prefSet.removePreference(mButtonDTMF);
1219 mButtonDTMF = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001220 }
1221
Andrew Lee64a7d792014-10-15 17:38:38 -07001222 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1223 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1224 int autoretry = Settings.Global.getInt(
1225 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1226 mButtonAutoRetry.setChecked(autoretry != 0);
1227 } else {
1228 prefSet.removePreference(mButtonAutoRetry);
1229 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001230 }
1231
Andrew Lee64a7d792014-10-15 17:38:38 -07001232 if (getResources().getBoolean(R.bool.hac_enabled)) {
1233 mButtonHAC.setOnPreferenceChangeListener(this);
1234 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1235 mButtonHAC.setChecked(hac != 0);
1236 } else {
1237 prefSet.removePreference(mButtonHAC);
1238 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001239 }
1240
Andrew Lee6fee2cb2014-12-09 16:42:18 -08001241 if (!telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported()) {
1242 mButtonTTY.init();
Andrew Lee64a7d792014-10-15 17:38:38 -07001243 } else {
1244 prefSet.removePreference(mButtonTTY);
1245 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001246 }
1247
1248 if (!getResources().getBoolean(R.bool.world_phone)) {
Andrew Lee2b36ba22014-11-05 17:08:49 -08001249 Preference cdmaOptions = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
1250 prefSet.removePreference(cdmaOptions);
1251
1252 // TODO: Support MSIM for this preference option.
1253 Preference gsmOptions = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
1254 prefSet.removePreference(gsmOptions);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001255
1256 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001257 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Junda Liua09d6962014-11-12 13:39:02 -08001258 boolean shouldHideCarrierSettings = Settings.Global.getInt(
1259 getContentResolver(), Settings.Global.HIDE_CARRIER_NETWORK_SETTINGS, 0) == 1;
1260 if (shouldHideCarrierSettings) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001261 prefSet.removePreference(fdnButton);
Junda Liua09d6962014-11-12 13:39:02 -08001262 if (mButtonDTMF != null) {
1263 prefSet.removePreference(mButtonDTMF);
Etan Cohen0ca1c802014-07-07 15:35:48 -07001264 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001265 } else {
Junda Liua09d6962014-11-12 13:39:02 -08001266 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1267 prefSet.removePreference(fdnButton);
1268
1269 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1270 addPreferencesFromResource(R.xml.cdma_call_privacy);
1271 }
1272 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
Andrew Leedd4f6df2014-12-09 19:13:51 -08001273 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(FdnSetting.class));
Junda Liua09d6962014-11-12 13:39:02 -08001274
1275 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
1276 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1277
1278 Preference callForwardingPref = prefSet.findPreference(CALL_FORWARDING_KEY);
1279 callForwardingPref.setIntent(mSubscriptionInfoHelper.getIntent(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001280 GsmUmtsCallForwardOptions.class));
Junda Liua09d6962014-11-12 13:39:02 -08001281
1282 Preference additionalGsmSettingsPref =
1283 prefSet.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
1284 additionalGsmSettingsPref.setIntent(mSubscriptionInfoHelper.getIntent(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001285 GsmUmtsAdditionalCallOptions.class));
Junda Liua09d6962014-11-12 13:39:02 -08001286 }
1287 } else {
1288 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1289 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001290 }
1291 }
1292
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001293 // check the intent that started this activity and pop up the voicemail
1294 // dialog if we've been asked to.
1295 // If we have at least one non default VM provider registered then bring up
1296 // the selection for the VM provider, otherwise bring up a VM number dialog.
1297 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001298 if (mShowVoicemailPreference) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001299 if (DBG) log("ACTION_ADD_VOICEMAIL Intent is thrown");
Tyler Gunn9c1071f2014-12-09 10:07:54 -08001300 if (mSetupVoicemail) {
1301 simulatePreferenceClick(mVoicemailSettingsScreen);
1302 mSetupVoicemail = false;
1303 } else if (mVoicemailProviders.hasMoreThanOneVoicemailProvider()) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001304 if (DBG) log("Voicemail data has more than one provider.");
Tyler Gunnbaee2952014-09-10 16:01:02 -07001305 simulatePreferenceClick(mVoicemailProviders);
1306 } else {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001307 onPreferenceChange(mVoicemailProviders, VoicemailProviderListPreference.DEFAULT_KEY);
1308 mVoicemailProviders.setValue(VoicemailProviderListPreference.DEFAULT_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001309 }
1310 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001311 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001312
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001313 updateVoiceNumberField();
1314 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001315
Andrew Lee8d66d812014-11-24 14:54:02 -08001316 mVoicemailNotificationVibrate.setChecked(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001317 VoicemailNotificationSettingsUtil.isVibrationEnabled(mPhone));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001318
Andrew Lee312e8172014-10-23 17:01:36 -07001319 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1320 boolean currentValue =
1321 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1322 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1323 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001324 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1325 } else {
1326 prefSet.removePreference(mEnableVideoCalling);
1327 }
Etan Cohena51d4ef2014-12-15 10:44:42 -08001328
1329 if (ImsManager.isVolteEnabledByPlatform(this) &&
1330 !mPhone.getContext().getResources().getBoolean(
1331 com.android.internal.R.bool.config_carrier_volte_tty_supported)) {
1332 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
1333 tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
1334 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001335 }
1336
Andrew Lee785ca2a2014-12-18 12:46:41 -08001337 @Override
1338 protected void onNewIntent(Intent newIntent) {
1339 setIntent(newIntent);
1340
1341 mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent());
1342 mSubscriptionInfoHelper.setActionBarTitle(
1343 getActionBar(), getResources(), R.string.call_settings_with_label);
1344 mPhone = mSubscriptionInfoHelper.getPhone();
1345 }
1346
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001347 private static void log(String msg) {
1348 Log.d(LOG_TAG, msg);
1349 }
1350
1351 /**
1352 * Updates the look of the VM preference widgets based on current VM provider settings.
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001353 * Note that the provider name is loaded fxrorm the found activity via loadLabel in
1354 * {@link VoicemailProviderListPreference#initVoiceMailProviders()} in order for it to be
1355 * localizable.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001356 */
1357 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1358 final String key = currentProviderSetting;
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001359 final VoicemailProvider provider = mVoicemailProviders.getVoicemailProvider(key);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001360
1361 /* This is the case when we are coming up on a freshly wiped phone and there is no
1362 persisted value for the list preference mVoicemailProviders.
1363 In this case we want to show the UI asking the user to select a voicemail provider as
1364 opposed to silently falling back to default one. */
1365 if (provider == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001366 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> null.");
1367
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001368 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1369 mVoicemailSettings.setEnabled(false);
1370 mVoicemailSettings.setIntent(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001371 mVoicemailNotificationVibrate.setEnabled(false);
1372 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001373 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> " + provider.toString());
1374
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001375 final String providerName = provider.name;
1376 mVoicemailProviders.setSummary(providerName);
1377 mVoicemailSettings.setEnabled(true);
1378 mVoicemailSettings.setIntent(provider.intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001379 mVoicemailNotificationVibrate.setEnabled(true);
1380 }
1381 }
1382
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001383
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001384 /**
1385 * Simulates user clicking on a passed preference.
1386 * Usually needed when the preference is a dialog preference and we want to invoke
1387 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001388 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001389 */
1390 private void simulatePreferenceClick(Preference preference) {
1391 // Go through settings until we find our setting
1392 // and then simulate a click on it to bring up the dialog
1393 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1394 for (int idx = 0; idx < adapter.getCount(); idx++) {
1395 if (adapter.getItem(idx) == preference) {
1396 getPreferenceScreen().onItemClick(this.getListView(),
1397 null, idx, adapter.getItemId(idx));
1398 break;
1399 }
1400 }
1401 }
1402
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001403 @Override
1404 public boolean onOptionsItemSelected(MenuItem item) {
1405 final int itemId = item.getItemId();
1406 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001407 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001408 return true;
1409 }
1410 return super.onOptionsItemSelected(item);
1411 }
Andrew Lee8d66d812014-11-24 14:54:02 -08001412
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001413 /**
1414 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1415 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1416 */
Andrew Lee5efb1122014-12-05 04:20:42 -08001417 public static void goUpToTopLevelSetting(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001418 Activity activity, SubscriptionInfoHelper subscriptionInfoHelper) {
1419 Intent intent = subscriptionInfoHelper.getIntent(CallFeaturesSetting.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001420 intent.setAction(Intent.ACTION_MAIN);
1421 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1422 activity.startActivity(intent);
1423 activity.finish();
1424 }
1425}