blob: 95d75103bcf094f7568e4361e31f80ea4dcf4ed8 [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_RETRY_KEY = "button_auto_retry_key";
158 private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
159 private static final String BUTTON_HAC_KEY = "button_hac_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700160
161 private static final String BUTTON_GSM_UMTS_OPTIONS = "button_gsm_more_expand_key";
162 private static final String BUTTON_CDMA_OPTIONS = "button_cdma_more_expand_key";
Andrew Lee2b36ba22014-11-05 17:08:49 -0800163 private static final String CALL_FORWARDING_KEY = "call_forwarding_key";
164 private static final String ADDITIONAL_GSM_SETTINGS_KEY = "additional_gsm_call_settings_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700165
Andrew Leece8ae2a2014-09-10 10:41:48 -0700166 private static final String PHONE_ACCOUNT_SETTINGS_KEY =
167 "phone_account_settings_preference_screen";
Andrew Leedb2fe562014-09-03 15:40:43 -0700168
Andrew Leedf14ead2014-10-17 14:22:52 -0700169 private static final String ENABLE_VIDEO_CALLING_KEY = "button_enable_video_calling";
170
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171 /** Event for Async voicemail change call */
172 private static final int EVENT_VOICEMAIL_CHANGED = 500;
173 private static final int EVENT_FORWARDING_CHANGED = 501;
174 private static final int EVENT_FORWARDING_GET_COMPLETED = 502;
175
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700176 public static final String HAC_KEY = "HACSetting";
177 public static final String HAC_VAL_ON = "ON";
178 public static final String HAC_VAL_OFF = "OFF";
179
180 /** Handle to voicemail pref */
181 private static final int VOICEMAIL_PREF_ID = 1;
182 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
183
184 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185 private AudioManager mAudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700186
Andrew Lee5ed870c2014-10-29 11:47:49 -0700187 private SubscriptionInfoHelper mSubscriptionInfoHelper;
188
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
190
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700191 private CheckBoxPreference mButtonAutoRetry;
192 private CheckBoxPreference mButtonHAC;
Andrew Lee6fee2cb2014-12-09 16:42:18 -0800193 private TtyModeListPreference mButtonTTY;
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800194 private VoicemailProviderListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700195 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700196 private PreferenceScreen mVoicemailSettings;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800197 private VoicemailRingtonePreference mVoicemailNotificationRingtone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700199 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700201 /**
202 * Results of reading forwarding settings
203 */
204 private CallForwardInfo[] mForwardingReadResults = null;
205
206 /**
207 * Result of forwarding number change.
208 * Keys are reasons (eg. unconditional forwarding).
209 */
210 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
211
212 /**
213 * Expected CF read result types.
214 * This set keeps track of the CF types for which we've issued change
215 * commands so we can tell when we've received all of the responses.
216 */
217 private Collection<Integer> mExpectedChangeResultReasons = null;
218
219 /**
220 * Result of vm number change
221 */
222 private AsyncResult mVoicemailChangeResult = null;
223
224 /**
225 * Previous VM provider setting so we can return to it in case of failure.
226 */
227 private String mPreviousVMProviderKey = null;
228
229 /**
230 * Id of the dialog being currently shown.
231 */
232 private int mCurrentDialogId = 0;
233
234 /**
235 * Flag indicating that we are invoking settings for the voicemail provider programmatically
236 * due to vm provider change.
237 */
238 private boolean mVMProviderSettingsForced = false;
239
240 /**
241 * Flag indicating that we are making changes to vm or fwd numbers
242 * due to vm provider change.
243 */
244 private boolean mChangingVMorFwdDueToProviderChange = false;
245
246 /**
247 * True if we are in the process of vm & fwd number change and vm has already been changed.
248 * This is used to decide what to do in case of rollback.
249 */
250 private boolean mVMChangeCompletedSuccessfully = false;
251
252 /**
253 * True if we had full or partial failure setting forwarding numbers and so need to roll them
254 * back.
255 */
256 private boolean mFwdChangesRequireRollback = false;
257
258 /**
259 * Id of error msg to display to user once we are done reverting the VM provider to the previous
260 * one.
261 */
262 private int mVMOrFwdSetError = 0;
263
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700264 /** string to hold old voicemail number as it is being updated. */
265 private String mOldVmNumber;
266
267 // New call forwarding settings and vm number we will be setting
268 // Need to save these since before we get to saving we need to asynchronously
269 // query the existing forwarding settings.
270 private CallForwardInfo[] mNewFwdSettings;
271 private String mNewVMNumber;
272
273 private boolean mForeground;
274
275 @Override
276 public void onPause() {
277 super.onPause();
278 mForeground = false;
Etan Cohena51d4ef2014-12-15 10:44:42 -0800279
280 if (ImsManager.isVolteEnabledByPlatform(this) &&
281 !mPhone.getContext().getResources().getBoolean(
282 com.android.internal.R.bool.config_carrier_volte_tty_supported)) {
283 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
284 tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
285 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700286 }
287
288 /**
289 * We have to pull current settings from the network for all kinds of
290 * voicemail providers so we can tell whether we have to update them,
291 * so use this bit to keep track of whether we're reading settings for the
292 * default provider and should therefore save them out when done.
293 */
294 private boolean mReadingSettingsForDefaultProvider = false;
295
Tyler Gunnbaee2952014-09-10 16:01:02 -0700296 /**
297 * Used to indicate that the voicemail preference should be shown.
298 */
299 private boolean mShowVoicemailPreference = false;
300
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800301 /**
302 * Used to indicate that the voicemail setup screen should be shown.
303 */
304 private boolean mSetupVoicemail = false;
305
Etan Cohena51d4ef2014-12-15 10:44:42 -0800306 private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
307 /**
308 * Enable/disable the TTY setting when in/out of a call (and if carrier doesn't
309 * support VoLTE with TTY).
310 * @see android.telephony.PhoneStateListener#onCallStateChanged(int,
311 * java.lang.String)
312 */
313 @Override
314 public void onCallStateChanged(int state, String incomingNumber) {
315 if (DBG) log("PhoneStateListener.onCallStateChanged: state=" + state);
316 Preference pref = getPreferenceScreen().findPreference(BUTTON_TTY_KEY);
317 if (pref != null) {
318 pref.setEnabled(state == TelephonyManager.CALL_STATE_IDLE);
319 }
320 }
321 };
322
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700323 /*
324 * Click Listeners, handle click based on objects attached to UI.
325 */
326
327 // Click listener for all toggle events
328 @Override
329 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
330 if (preference == mSubMenuVoicemailSettings) {
331 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700332 } else if (preference == mButtonTTY) {
333 return true;
334 } else if (preference == mButtonAutoRetry) {
335 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
336 android.provider.Settings.Global.CALL_AUTO_RETRY,
337 mButtonAutoRetry.isChecked() ? 1 : 0);
338 return true;
339 } else if (preference == mButtonHAC) {
340 int hac = mButtonHAC.isChecked() ? 1 : 0;
341 // Update HAC value in Settings database
342 Settings.System.putInt(mPhone.getContext().getContentResolver(),
343 Settings.System.HEARING_AID, hac);
344
345 // Update HAC Value in AudioManager
346 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
347 return true;
Andrew Lee696bff52014-12-03 17:58:18 -0800348 } else if (preference.getKey().equals(mVoicemailSettings.getKey())) {
349 // Check key instead of comparing reference because closing the voicemail notification
350 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
351 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800352 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
353
Andrew Lee696bff52014-12-03 17:58:18 -0800354 final Dialog dialog = ((PreferenceScreen) preference).getDialog();
Yorke Leea0f63bf2014-10-09 18:27:20 -0700355 if (dialog != null) {
356 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
357 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800358
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700359 if (preference.getIntent() != null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800360 if (DBG) log("Invoking cfg intent " + preference.getIntent().getPackage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700361
362 // onActivityResult() will be responsible for resetting some of variables.
363 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
364 return true;
365 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800366 if (DBG) log("onPreferenceTreeClick(). No intent; use default behavior in xml.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700367
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800368 // onActivityResult() will not be called, so reset variables here.
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800369 mPreviousVMProviderKey = VoicemailProviderListPreference.DEFAULT_KEY;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700370 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700371 return false;
372 }
Andrew Lee97708a42014-09-25 12:39:07 -0700373 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700374 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
375 if (dialog != null) {
376 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
377 }
Andrew Lee97708a42014-09-25 12:39:07 -0700378 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700379 }
380 return false;
381 }
382
383 /**
384 * Implemented to support onPreferenceChangeListener to look for preference
385 * changes.
386 *
387 * @param preference is the preference to be changed
388 * @param objValue should be the value of the selection, NOT its localized
389 * display value.
390 */
391 @Override
392 public boolean onPreferenceChange(Preference preference, Object objValue) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800393 if (DBG) log("onPreferenceChange: \"" + preference + "\" changed to \"" + objValue + "\"");
Andrew Lee2170a972014-08-13 18:13:01 -0700394
Andrew Leea1239f22015-03-02 17:44:07 -0800395 if (preference == mVoicemailProviders) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700396 final String newProviderKey = (String) objValue;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800397
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700398 // If previous provider key and the new one is same, we don't need to handle it.
399 if (mPreviousVMProviderKey.equals(newProviderKey)) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800400 if (DBG) log("No change is made to the VM provider setting.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700401 return true;
402 }
403 updateVMPreferenceWidgets(newProviderKey);
404
Andrew Leeb490d732014-10-27 15:00:41 -0700405 final VoicemailProviderSettings newProviderSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800406 VoicemailProviderSettingsUtil.load(this, newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700407
Andrew Lee88b51e22014-10-29 15:48:51 -0700408 // If the user switches to a voice mail provider and we have numbers stored for it we
409 // will automatically change the phone's voice mail and forwarding number to the stored
410 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700411 if (newProviderSettings == null) {
412 // Force the user into a configuration of the chosen provider
413 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
414 mVMProviderSettingsForced = true;
415 simulatePreferenceClick(mVoicemailSettings);
416 } else {
417 if (DBG) log("Saved preferences found - switching to them");
418 // Set this flag so if we get a failure we revert to previous provider
419 mChangingVMorFwdDueToProviderChange = true;
420 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
421 }
Andrew Lee8d66d812014-11-24 14:54:02 -0800422 } else if (preference.getKey().equals(mVoicemailNotificationVibrate.getKey())) {
Andrew Lee696bff52014-12-03 17:58:18 -0800423 // Check key instead of comparing reference because closing the voicemail notification
424 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
425 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee8d66d812014-11-24 14:54:02 -0800426 VoicemailNotificationSettingsUtil.setVibrationEnabled(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800427 mPhone, Boolean.TRUE.equals(objValue));
Andrew Leedf14ead2014-10-17 14:22:52 -0700428 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700429 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
430 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
431 } else {
432 AlertDialog.Builder builder = new AlertDialog.Builder(this);
433 DialogInterface.OnClickListener networkSettingsClickListener =
434 new Dialog.OnClickListener() {
435 @Override
436 public void onClick(DialogInterface dialog, int which) {
437 startActivity(new Intent(mPhone.getContext(),
438 com.android.phone.MobileNetworkSettings.class));
439 }
440 };
441 builder.setMessage(getResources().getString(
442 R.string.enable_video_calling_dialog_msg))
443 .setNeutralButton(getResources().getString(
444 R.string.enable_video_calling_dialog_settings),
445 networkSettingsClickListener)
446 .setPositiveButton(android.R.string.ok, null)
447 .show();
448 return false;
449 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700450 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800451
452 // Always let the preference setting proceed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700453 return true;
454 }
455
456 @Override
457 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800458 if (DBG) log("onDialogClosed: Button clicked is " + buttonClicked);
459
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700460 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
461 return;
462 }
463
464 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700465 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
466 mSubMenuVoicemailSettings.getPhoneNumber(),
467 VoicemailProviderSettings.NO_FORWARDING);
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800468 saveVoiceMailAndForwardingNumber(mVoicemailProviders.getKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700469 }
470 }
471
472 /**
473 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
474 * This method set the default values for the various
475 * EditPhoneNumberPreference dialogs.
476 */
477 @Override
478 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
479 if (preference == mSubMenuVoicemailSettings) {
480 // update the voicemail number field, which takes care of the
481 // mSubMenuVoicemailSettings itself, so we should return null.
482 if (DBG) log("updating default for voicemail dialog");
483 updateVoiceNumberField();
484 return null;
485 }
486
487 String vmDisplay = mPhone.getVoiceMailNumber();
488 if (TextUtils.isEmpty(vmDisplay)) {
489 // if there is no voicemail number, we just return null to
490 // indicate no contribution.
491 return null;
492 }
493
494 // Return the voicemail number prepended with "VM: "
495 if (DBG) log("updating default for call forwarding dialogs");
496 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
497 }
498
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700499 private void switchToPreviousVoicemailProvider() {
500 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700501
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800502 if (mPreviousVMProviderKey == null) {
503 return;
504 }
505
506 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
Andrew Lee84024342014-11-06 23:37:09 -0800507 showDialogIfForeground(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800508 final VoicemailProviderSettings prevSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800509 VoicemailProviderSettingsUtil.load(this, mPreviousVMProviderKey);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800510 if (prevSettings == null) {
511 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
512 + mPreviousVMProviderKey + "\" is null but should be loaded.");
513 }
514
515 if (mVMChangeCompletedSuccessfully) {
516 mNewVMNumber = prevSettings.getVoicemailNumber();
517 Log.i(LOG_TAG, "VM change is already completed successfully."
518 + "Have to revert VM back to " + mNewVMNumber + " again.");
519 mPhone.setVoiceMailNumber(
520 mPhone.getVoiceMailAlphaTag().toString(),
521 mNewVMNumber,
522 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
523 }
524
525 if (mFwdChangesRequireRollback) {
526 Log.i(LOG_TAG, "Requested to rollback forwarding changes.");
527
528 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
529 if (prevFwdSettings != null) {
530 Map<Integer, AsyncResult> results = mForwardingChangeResults;
531 resetForwardingChangeState();
532 for (int i = 0; i < prevFwdSettings.length; i++) {
533 CallForwardInfo fi = prevFwdSettings[i];
534 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
535 // Only revert the settings for which the update succeeded.
536 AsyncResult result = results.get(fi.reason);
537 if (result != null && result.exception == null) {
538 mExpectedChangeResultReasons.add(fi.reason);
539 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
540 mRevertOptionComplete.obtainMessage(
541 EVENT_FORWARDING_CHANGED, i, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700542 }
543 }
544 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700545 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800546 } else {
547 if (DBG) log("No need to revert");
548 onRevertDone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700549 }
550 }
551
552 private void onRevertDone() {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800553 if (DBG) log("onRevertDone: Changing provider key back to " + mPreviousVMProviderKey);
554
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700555 updateVMPreferenceWidgets(mPreviousVMProviderKey);
556 updateVoiceNumberField();
557 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800558 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700559 mVMOrFwdSetError = 0;
560 }
561 }
562
563 @Override
564 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
565 if (DBG) {
566 log("onActivityResult: requestCode: " + requestCode
567 + ", resultCode: " + resultCode
568 + ", data: " + data);
569 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800570
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700571 // there are cases where the contact picker may end up sending us more than one
572 // request. We want to ignore the request if we're not in the correct state.
573 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
574 boolean failure = false;
575
576 // No matter how the processing of result goes lets clear the flag
577 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
578 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
579 mVMProviderSettingsForced = false;
580
581 String vmNum = null;
582 if (resultCode != RESULT_OK) {
583 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
584 failure = true;
585 } else {
586 if (data == null) {
587 if (DBG) log("onActivityResult: vm provider cfg result has no data");
588 failure = true;
589 } else {
590 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
591 if (DBG) log("Provider requested signout");
592 if (isVMProviderSettingsForced) {
593 if (DBG) log("Going back to previous provider on signout");
594 switchToPreviousVoicemailProvider();
595 } else {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800596 final String victim = mVoicemailProviders.getKey();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700597 if (DBG) log("Relaunching activity and ignoring " + victim);
598 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
599 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
600 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
601 this.startActivity(i);
602 }
603 return;
604 }
605 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
606 if (vmNum == null || vmNum.length() == 0) {
607 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
608 failure = true;
609 }
610 }
611 }
612 if (failure) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800613 if (DBG) log("Failure in return from voicemail provider.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700614 if (isVMProviderSettingsForced) {
615 switchToPreviousVoicemailProvider();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700616 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800617
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700618 return;
619 }
620 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
621 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
622
Santos Cordonda120f42014-08-06 04:44:34 -0700623 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700624 // send it to the provider when it's config is invoked so it can use this as default
625 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
626
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800627 if (DBG) log("onActivityResult: cfg result has forwarding number " + fwdNum);
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800628 saveVoiceMailAndForwardingNumber(mVoicemailProviders.getKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700629 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700630 return;
631 }
632
633 if (requestCode == VOICEMAIL_PREF_ID) {
634 if (resultCode != RESULT_OK) {
635 if (DBG) log("onActivityResult: contact picker result not OK.");
636 return;
637 }
638
639 Cursor cursor = null;
640 try {
641 cursor = getContentResolver().query(data.getData(),
642 NUM_PROJECTION, null, null, null);
643 if ((cursor == null) || (!cursor.moveToFirst())) {
644 if (DBG) log("onActivityResult: bad contact data, no results found.");
645 return;
646 }
647 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
648 return;
649 } finally {
650 if (cursor != null) {
651 cursor.close();
652 }
653 }
654 }
655
656 super.onActivityResult(requestCode, resultCode, data);
657 }
658
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700659 /**
660 * Wrapper around showDialog() that will silently do nothing if we're
661 * not in the foreground.
662 *
663 * This is useful here because most of the dialogs we display from
664 * this class are triggered by asynchronous events (like
665 * success/failure messages from the telephony layer) and it's
666 * possible for those events to come in even after the user has gone
667 * to a different screen.
668 */
669 // TODO: this is too brittle: it's still easy to accidentally add new
670 // code here that calls showDialog() directly (which will result in a
671 // WindowManager$BadTokenException if called after the activity has
672 // been stopped.)
673 //
674 // It would be cleaner to do the "if (mForeground)" check in one
675 // central place, maybe by using a single Handler for all asynchronous
676 // events (and have *that* discard events if we're not in the
677 // foreground.)
678 //
679 // Unfortunately it's not that simple, since we sometimes need to do
680 // actual work to handle these events whether or not we're in the
681 // foreground (see the Handler code in mSetOptionComplete for
682 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800683 //
684 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
685 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700686 private void showDialogIfForeground(int id) {
687 if (mForeground) {
688 showDialog(id);
689 }
690 }
691
692 private void dismissDialogSafely(int id) {
693 try {
694 dismissDialog(id);
695 } catch (IllegalArgumentException e) {
696 // This is expected in the case where we were in the background
697 // at the time we would normally have shown the dialog, so we didn't
698 // show it.
699 }
700 }
701
Andrew Lee299e4a72015-01-15 12:44:34 -0800702 /**
703 * TODO: Refactor to make it easier to understand what's done in the different stages.
704 */
Andrew Leeb490d732014-10-27 15:00:41 -0700705 private void saveVoiceMailAndForwardingNumber(
706 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700707 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700708 mNewVMNumber = newSettings.getVoicemailNumber();
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800709 mNewVMNumber = (mNewVMNumber == null) ? "" : mNewVMNumber;
Andrew Leeb490d732014-10-27 15:00:41 -0700710 mNewFwdSettings = newSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700711
Andrew Lee299e4a72015-01-15 12:44:34 -0800712 // No fwd settings on CDMA.
713 boolean isCdma = mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA;
714 if (isCdma) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700715 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700716 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700717 }
718
Andrew Leee3c15212014-10-28 13:12:55 -0700719 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700720 if (mNewVMNumber.equals(mOldVmNumber)
721 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Lee84024342014-11-06 23:37:09 -0800722 showDialogIfForeground(VoicemailDialogUtil.VM_NOCHANGE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700723 return;
724 }
725
Andrew Lee6950c6c2014-11-26 12:05:42 -0800726 VoicemailProviderSettingsUtil.save(this, key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700727 mVMChangeCompletedSuccessfully = false;
728 mFwdChangesRequireRollback = false;
729 mVMOrFwdSetError = 0;
Andrew Lee299e4a72015-01-15 12:44:34 -0800730
731 // Don't read call forwarding settings if CDMA. Call forwarding is not supported by CDMA.
732 if (!key.equals(mPreviousVMProviderKey) && !isCdma) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700733 mReadingSettingsForDefaultProvider =
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800734 mPreviousVMProviderKey.equals(VoicemailProviderListPreference.DEFAULT_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700735 if (DBG) log("Reading current forwarding settings");
Andrew Leeb490d732014-10-27 15:00:41 -0700736 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
737 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
738 for (int i = 0; i < mForwardingReadResults.length; i++) {
739 mPhone.getCallForwardingOption(
740 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700741 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
742 }
Andrew Lee84024342014-11-06 23:37:09 -0800743 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700744 } else {
745 saveVoiceMailAndForwardingNumberStage2();
746 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800747
748 // Refresh the MWI indicator if it is already showing.
749 PhoneGlobals.getInstance().refreshMwiIndicator(mSubscriptionInfoHelper.getSubId());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700750 }
751
752 private final Handler mGetOptionComplete = new Handler() {
753 @Override
754 public void handleMessage(Message msg) {
755 AsyncResult result = (AsyncResult) msg.obj;
756 switch (msg.what) {
757 case EVENT_FORWARDING_GET_COMPLETED:
758 handleForwardingSettingsReadResult(result, msg.arg1);
759 break;
760 }
761 }
762 };
763
764 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
765 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800766
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700767 Throwable error = null;
768 if (ar.exception != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700769 error = ar.exception;
Andrew Lee1af6cf72014-11-04 17:35:26 -0800770 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700771 }
772 if (ar.userObj instanceof Throwable) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800773 error = (Throwable) ar.userObj;
774 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700775 }
776
777 // We may have already gotten an error and decided to ignore the other results.
778 if (mForwardingReadResults == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800779 if (DBG) Log.d(LOG_TAG, "Ignoring fwd reading result: " + idx);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700780 return;
781 }
782
783 // In case of error ignore other results, show an error dialog
784 if (error != null) {
785 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
786 mForwardingReadResults = null;
Andrew Lee84024342014-11-06 23:37:09 -0800787 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
788 showDialogIfForeground(VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700789 return;
790 }
791
Andrew Lee1af6cf72014-11-04 17:35:26 -0800792 // Get the forwarding info.
793 mForwardingReadResults[idx] = CallForwardInfoUtil.getCallForwardInfo(
794 (CallForwardInfo[]) ar.result,
795 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx]);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700796
797 // Check if we got all the results already
798 boolean done = true;
799 for (int i = 0; i < mForwardingReadResults.length; i++) {
800 if (mForwardingReadResults[i] == null) {
801 done = false;
802 break;
803 }
804 }
Andrew Lee1af6cf72014-11-04 17:35:26 -0800805
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700806 if (done) {
807 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
Andrew Lee84024342014-11-06 23:37:09 -0800808 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800809
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700810 if (mReadingSettingsForDefaultProvider) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800811 VoicemailProviderSettingsUtil.save(mPhone.getContext(),
812 VoicemailProviderListPreference.DEFAULT_KEY,
Andrew Lee299e4a72015-01-15 12:44:34 -0800813 new VoicemailProviderSettings(mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700814 mReadingSettingsForDefaultProvider = false;
815 }
816 saveVoiceMailAndForwardingNumberStage2();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700817 }
818 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800819
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700820 private void resetForwardingChangeState() {
821 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
822 mExpectedChangeResultReasons = new HashSet<Integer>();
823 }
824
Andrew Lee299e4a72015-01-15 12:44:34 -0800825 // Called after we are done saving the previous forwarding settings if we needed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700826 private void saveVoiceMailAndForwardingNumberStage2() {
827 mForwardingChangeResults = null;
828 mVoicemailChangeResult = null;
Andrew Leeb490d732014-10-27 15:00:41 -0700829 if (mNewFwdSettings != VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700830 resetForwardingChangeState();
831 for (int i = 0; i < mNewFwdSettings.length; i++) {
832 CallForwardInfo fi = mNewFwdSettings[i];
Andrew Lee1af6cf72014-11-04 17:35:26 -0800833 CallForwardInfo fiForReason =
834 CallForwardInfoUtil.infoForReason(mForwardingReadResults, fi.reason);
835 final boolean doUpdate = CallForwardInfoUtil.isUpdateRequired(fiForReason, fi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700836
837 if (doUpdate) {
838 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
839 mExpectedChangeResultReasons.add(i);
840
Andrew Lee1af6cf72014-11-04 17:35:26 -0800841 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700842 mSetOptionComplete.obtainMessage(
843 EVENT_FORWARDING_CHANGED, fi.reason, 0));
844 }
845 }
Andrew Lee84024342014-11-06 23:37:09 -0800846 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700847 } else {
848 if (DBG) log("Not touching fwd #");
849 setVMNumberWithCarrier();
850 }
851 }
852
853 private void setVMNumberWithCarrier() {
854 if (DBG) log("save voicemail #: " + mNewVMNumber);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800855
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700856 mPhone.setVoiceMailNumber(
857 mPhone.getVoiceMailAlphaTag().toString(),
858 mNewVMNumber,
859 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
860 }
861
862 /**
863 * Callback to handle option update completions
864 */
865 private final Handler mSetOptionComplete = new Handler() {
866 @Override
867 public void handleMessage(Message msg) {
868 AsyncResult result = (AsyncResult) msg.obj;
869 boolean done = false;
870 switch (msg.what) {
871 case EVENT_VOICEMAIL_CHANGED:
872 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700873 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700874 done = true;
875 break;
876 case EVENT_FORWARDING_CHANGED:
877 mForwardingChangeResults.put(msg.arg1, result);
878 if (result.exception != null) {
879 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
880 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700881 }
Andrew Leee438b312014-10-29 16:59:15 -0700882 if (isForwardingCompleted()) {
883 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700884 if (DBG) log("Overall fwd changes completed ok, starting vm change");
885 setVMNumberWithCarrier();
886 } else {
887 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
888 "Check if we need to try rollback for some settings.");
889 mFwdChangesRequireRollback = false;
890 Iterator<Map.Entry<Integer,AsyncResult>> it =
891 mForwardingChangeResults.entrySet().iterator();
892 while (it.hasNext()) {
893 Map.Entry<Integer,AsyncResult> entry = it.next();
894 if (entry.getValue().exception == null) {
895 // If at least one succeeded we have to revert
896 Log.i(LOG_TAG, "Rollback will be required");
897 mFwdChangesRequireRollback = true;
898 break;
899 }
900 }
901 if (!mFwdChangesRequireRollback) {
902 Log.i(LOG_TAG, "No rollback needed.");
903 }
904 done = true;
905 }
906 }
907 break;
908 default:
909 // TODO: should never reach this, may want to throw exception
910 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800911
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700912 if (done) {
913 if (DBG) log("All VM provider related changes done");
914 if (mForwardingChangeResults != null) {
Andrew Lee84024342014-11-06 23:37:09 -0800915 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700916 }
Andrew Leee438b312014-10-29 16:59:15 -0700917 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700918 }
919 }
920 };
921
922 /**
923 * Callback to handle option revert completions
924 */
925 private final Handler mRevertOptionComplete = new Handler() {
926 @Override
927 public void handleMessage(Message msg) {
928 AsyncResult result = (AsyncResult) msg.obj;
929 switch (msg.what) {
930 case EVENT_VOICEMAIL_CHANGED:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700931 if (DBG) log("VM revert complete msg");
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800932 mVoicemailChangeResult = result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700933 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800934
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700935 case EVENT_FORWARDING_CHANGED:
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800936 if (DBG) log("FWD revert complete msg ");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700937 mForwardingChangeResults.put(msg.arg1, result);
938 if (result.exception != null) {
939 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
940 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700941 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700942 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800943
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700944 default:
945 // TODO: should never reach this, may want to throw exception
946 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800947
948 final boolean done = (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null)
949 && (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700950 if (done) {
951 if (DBG) log("All VM reverts done");
Andrew Lee84024342014-11-06 23:37:09 -0800952 dismissDialogSafely(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700953 onRevertDone();
954 }
955 }
956 };
957
958 /**
Andrew Leee438b312014-10-29 16:59:15 -0700959 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700960 */
Andrew Leee438b312014-10-29 16:59:15 -0700961 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700962 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -0700963 return true;
964 }
965
966 for (Integer reason : mExpectedChangeResultReasons) {
967 if (mForwardingChangeResults.get(reason) == null) {
968 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700969 }
970 }
Andrew Leee438b312014-10-29 16:59:15 -0700971
972 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700973 }
Andrew Leee438b312014-10-29 16:59:15 -0700974
975 private boolean isFwdChangeSuccess() {
976 if (mForwardingChangeResults == null) {
977 return true;
978 }
979
980 for (AsyncResult result : mForwardingChangeResults.values()) {
981 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700982 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700983 String msg = exception.getMessage();
984 msg = (msg != null) ? msg : "";
985 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
986 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700987 }
988 }
Andrew Leee438b312014-10-29 16:59:15 -0700989 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700990 }
991
Andrew Leee438b312014-10-29 16:59:15 -0700992 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700993 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700994 String msg = mVoicemailChangeResult.exception.getMessage();
995 msg = (msg != null) ? msg : "";
996 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
997 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700998 }
Andrew Leee438b312014-10-29 16:59:15 -0700999 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001000 }
1001
Andrew Leee438b312014-10-29 16:59:15 -07001002 private void handleSetVmOrFwdMessage() {
1003 if (DBG) log("handleSetVMMessage: set VM request complete");
1004
1005 if (!isFwdChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -08001006 handleVmOrFwdSetError(VoicemailDialogUtil.FWD_SET_RESPONSE_ERROR_DIALOG);
Andrew Leee438b312014-10-29 16:59:15 -07001007 } else if (!isVmChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -08001008 handleVmOrFwdSetError(VoicemailDialogUtil.VM_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001009 } else {
Andrew Lee84024342014-11-06 23:37:09 -08001010 handleVmAndFwdSetSuccess(VoicemailDialogUtil.VM_CONFIRM_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001011 }
1012 }
1013
1014 /**
1015 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
1016 * changes to those settings and show "failure" dialog.
1017 *
Andrew Leeab082272014-11-04 15:50:42 -08001018 * @param dialogId ID of the dialog to show for the specific error case. Either
Andrew Lee84024342014-11-06 23:37:09 -08001019 * {@link #FWD_SET_RESPONSE_ERROR_DIALOG} or {@link #VM_RESPONSE_ERROR_DIALOG}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001020 */
Andrew Leeab082272014-11-04 15:50:42 -08001021 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001022 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -08001023 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001024 mChangingVMorFwdDueToProviderChange = false;
1025 switchToPreviousVoicemailProvider();
1026 return;
1027 }
1028 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001029 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001030 updateVoiceNumberField();
1031 }
1032
1033 /**
1034 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1035 * This updates a bunch of variables and show "success" dialog.
1036 */
Andrew Leeab082272014-11-04 15:50:42 -08001037 private void handleVmAndFwdSetSuccess(int dialogId) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001038 if (DBG) log("handleVmAndFwdSetSuccess: key is " + mVoicemailProviders.getKey());
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001039
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001040 mPreviousVMProviderKey = mVoicemailProviders.getKey();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001041 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001042 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001043 updateVoiceNumberField();
1044 }
1045
1046 /**
1047 * Update the voicemail number from what we've recorded on the sim.
1048 */
1049 private void updateVoiceNumberField() {
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001050 if (DBG) log("updateVoiceNumberField()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001051
1052 mOldVmNumber = mPhone.getVoiceMailNumber();
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001053 if (TextUtils.isEmpty(mOldVmNumber)) {
1054 mSubMenuVoicemailSettings.setPhoneNumber("");
1055 mSubMenuVoicemailSettings.setSummary(getString(R.string.voicemail_number_not_set));
1056 } else {
1057 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1058 mSubMenuVoicemailSettings.setSummary(mOldVmNumber);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001059 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001060 }
1061
1062 /*
1063 * Helper Methods for Activity class.
1064 * The initial query commands are split into two pieces now
1065 * for individual expansion. This combined with the ability
1066 * to cancel queries allows for a much better user experience,
1067 * and also ensures that the user only waits to update the
1068 * data that is relevant.
1069 */
1070
1071 @Override
1072 protected void onPrepareDialog(int id, Dialog dialog) {
1073 super.onPrepareDialog(id, dialog);
1074 mCurrentDialogId = id;
1075 }
1076
1077 // dialog creation method, called by showDialog()
1078 @Override
Andrew Lee84024342014-11-06 23:37:09 -08001079 protected Dialog onCreateDialog(int dialogId) {
1080 return VoicemailDialogUtil.getDialog(this, dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001081 }
1082
1083 // This is a method implemented for DialogInterface.OnClickListener.
1084 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1085 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1086 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1087 public void onClick(DialogInterface dialog, int which) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001088 if (DBG) log("onClick: button clicked is " + which);
1089
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001090 dialog.dismiss();
1091 switch (which){
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001092 case DialogInterface.BUTTON_NEGATIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001093 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001094 // We failed to get current forwarding settings and the user
1095 // does not wish to continue.
1096 switchToPreviousVoicemailProvider();
1097 }
1098 break;
1099 case DialogInterface.BUTTON_POSITIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001100 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001101 // We failed to get current forwarding settings but the user
1102 // wishes to continue changing settings to the new vm provider
1103 saveVoiceMailAndForwardingNumberStage2();
1104 } else {
1105 finish();
1106 }
1107 return;
1108 default:
1109 // just let the dialog close and go back to the input
1110 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001111
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001112 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1113 // with settings UI. If we were called to explicitly configure voice mail then
1114 // we finish the settings activity here to come back to whatever the user was doing.
1115 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1116 finish();
1117 }
1118 }
1119
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001120 /*
1121 * Activity class methods
1122 */
1123
1124 @Override
1125 protected void onCreate(Bundle icicle) {
1126 super.onCreate(icicle);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001127 if (DBG) log("onCreate: Intent is " + getIntent());
1128
Jay Shraunerc2684732014-11-12 12:10:37 -08001129 // Make sure we are running as the primary user.
1130 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
1131 Toast.makeText(this, R.string.call_settings_primary_user_only,
1132 Toast.LENGTH_SHORT).show();
1133 finish();
1134 return;
1135 }
1136
Tyler Gunnbaee2952014-09-10 16:01:02 -07001137 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
1138
Tyler Gunnbaee2952014-09-10 16:01:02 -07001139 // Show the voicemail preference in onResume if the calling intent specifies the
1140 // ACTION_ADD_VOICEMAIL action.
1141 mShowVoicemailPreference = (icicle == null) &&
Jay Shraunerbe2fb262014-11-11 15:19:58 -08001142 TextUtils.equals(getIntent().getAction(), ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -08001143 mSetupVoicemail = mShowVoicemailPreference &&
1144 getIntent().getBooleanExtra(SETUP_VOICEMAIL_EXTRA, false);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001145
Andrew Leedd4f6df2014-12-09 19:13:51 -08001146 mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent());
Andrew Lee5ed870c2014-10-29 11:47:49 -07001147 mSubscriptionInfoHelper.setActionBarTitle(
1148 getActionBar(), getResources(), R.string.call_settings_with_label);
Andrew Lee1479dd12014-11-06 23:06:32 -08001149 mPhone = mSubscriptionInfoHelper.getPhone();
Tyler Gunnbaee2952014-09-10 16:01:02 -07001150 }
1151
Tyler Gunnbaee2952014-09-10 16:01:02 -07001152 @Override
1153 protected void onResume() {
1154 super.onResume();
1155 mForeground = true;
1156
1157 PreferenceScreen preferenceScreen = getPreferenceScreen();
1158 if (preferenceScreen != null) {
1159 preferenceScreen.removeAll();
1160 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001161
1162 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001163
Andrew Lee6fee2cb2014-12-09 16:42:18 -08001164 TelecomManager telecomManager = TelecomManager.from(this);
1165 TelephonyManager telephonyManager =
1166 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
1167
1168 Preference phoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
1169 if (telephonyManager.isMultiSimEnabled() || (telecomManager.getSimCallManagers().isEmpty()
1170 && !SipUtil.isVoipSupported(mPhone.getContext()))) {
1171 getPreferenceScreen().removePreference(phoneAccountSettingsPreference);
1172 }
Andrew Leedb2fe562014-09-03 15:40:43 -07001173
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001174 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001175 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1176 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1177 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1178 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001179
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001180 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1181 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
Andrew Lee6fee2cb2014-12-09 16:42:18 -08001182 mButtonTTY = (TtyModeListPreference) findPreference(
1183 getResources().getString(R.string.tty_mode_key));
Andrew Lee2170a972014-08-13 18:13:01 -07001184
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001185 mVoicemailProviders = (VoicemailProviderListPreference) findPreference(
1186 BUTTON_VOICEMAIL_PROVIDER_KEY);
1187 mVoicemailProviders.init(mPhone, getIntent());
Andrew Lee2c027892014-10-29 11:29:54 -07001188 mVoicemailProviders.setOnPreferenceChangeListener(this);
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001189 mPreviousVMProviderKey = mVoicemailProviders.getValue();
Andrew Lee8d66d812014-11-24 14:54:02 -08001190
Andrew Lee2c027892014-10-29 11:29:54 -07001191 mVoicemailSettingsScreen =
1192 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001193 mVoicemailSettings = (PreferenceScreen) findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
Andrew Lee8d66d812014-11-24 14:54:02 -08001194
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001195 mVoicemailNotificationRingtone = (VoicemailRingtonePreference) findPreference(
1196 getResources().getString(R.string.voicemail_notification_ringtone_key));
1197 mVoicemailNotificationRingtone.init(mPhone);
1198
Andrew Lee8d66d812014-11-24 14:54:02 -08001199 mVoicemailNotificationVibrate = (CheckBoxPreference) findPreference(
1200 getResources().getString(R.string.voicemail_notification_vibrate_key));
1201 mVoicemailNotificationVibrate.setOnPreferenceChangeListener(this);
1202
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001203 updateVMPreferenceWidgets(mVoicemailProviders.getValue());
1204
1205 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001206
Andrew Lee64a7d792014-10-15 17:38:38 -07001207 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1208 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1209 int autoretry = Settings.Global.getInt(
1210 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1211 mButtonAutoRetry.setChecked(autoretry != 0);
1212 } else {
1213 prefSet.removePreference(mButtonAutoRetry);
1214 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001215 }
1216
Andrew Lee64a7d792014-10-15 17:38:38 -07001217 if (getResources().getBoolean(R.bool.hac_enabled)) {
1218 mButtonHAC.setOnPreferenceChangeListener(this);
1219 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1220 mButtonHAC.setChecked(hac != 0);
1221 } else {
1222 prefSet.removePreference(mButtonHAC);
1223 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001224 }
1225
Andrew Lee6fee2cb2014-12-09 16:42:18 -08001226 if (!telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported()) {
1227 mButtonTTY.init();
Andrew Lee64a7d792014-10-15 17:38:38 -07001228 } else {
1229 prefSet.removePreference(mButtonTTY);
1230 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001231 }
1232
1233 if (!getResources().getBoolean(R.bool.world_phone)) {
Andrew Lee2b36ba22014-11-05 17:08:49 -08001234 Preference cdmaOptions = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
1235 prefSet.removePreference(cdmaOptions);
1236
1237 // TODO: Support MSIM for this preference option.
1238 Preference gsmOptions = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
1239 prefSet.removePreference(gsmOptions);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001240
1241 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001242 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Junda Liua09d6962014-11-12 13:39:02 -08001243 boolean shouldHideCarrierSettings = Settings.Global.getInt(
1244 getContentResolver(), Settings.Global.HIDE_CARRIER_NETWORK_SETTINGS, 0) == 1;
1245 if (shouldHideCarrierSettings) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001246 prefSet.removePreference(fdnButton);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001247 } else {
Junda Liua09d6962014-11-12 13:39:02 -08001248 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1249 prefSet.removePreference(fdnButton);
1250
1251 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1252 addPreferencesFromResource(R.xml.cdma_call_privacy);
1253 }
1254 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
Andrew Leedd4f6df2014-12-09 19:13:51 -08001255 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(FdnSetting.class));
Junda Liua09d6962014-11-12 13:39:02 -08001256
1257 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
1258 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1259
1260 Preference callForwardingPref = prefSet.findPreference(CALL_FORWARDING_KEY);
1261 callForwardingPref.setIntent(mSubscriptionInfoHelper.getIntent(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001262 GsmUmtsCallForwardOptions.class));
Junda Liua09d6962014-11-12 13:39:02 -08001263
1264 Preference additionalGsmSettingsPref =
1265 prefSet.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
1266 additionalGsmSettingsPref.setIntent(mSubscriptionInfoHelper.getIntent(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001267 GsmUmtsAdditionalCallOptions.class));
Junda Liua09d6962014-11-12 13:39:02 -08001268 }
1269 } else {
1270 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1271 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001272 }
1273 }
1274
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001275 // check the intent that started this activity and pop up the voicemail
1276 // dialog if we've been asked to.
1277 // If we have at least one non default VM provider registered then bring up
1278 // the selection for the VM provider, otherwise bring up a VM number dialog.
1279 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001280 if (mShowVoicemailPreference) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001281 if (DBG) log("ACTION_ADD_VOICEMAIL Intent is thrown");
Tyler Gunn9c1071f2014-12-09 10:07:54 -08001282 if (mSetupVoicemail) {
1283 simulatePreferenceClick(mVoicemailSettingsScreen);
1284 mSetupVoicemail = false;
1285 } else if (mVoicemailProviders.hasMoreThanOneVoicemailProvider()) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001286 if (DBG) log("Voicemail data has more than one provider.");
Tyler Gunnbaee2952014-09-10 16:01:02 -07001287 simulatePreferenceClick(mVoicemailProviders);
1288 } else {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001289 onPreferenceChange(mVoicemailProviders, VoicemailProviderListPreference.DEFAULT_KEY);
1290 mVoicemailProviders.setValue(VoicemailProviderListPreference.DEFAULT_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001291 }
1292 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001293 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001294
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001295 updateVoiceNumberField();
1296 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001297
Andrew Lee8d66d812014-11-24 14:54:02 -08001298 mVoicemailNotificationVibrate.setChecked(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001299 VoicemailNotificationSettingsUtil.isVibrationEnabled(mPhone));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001300
Andrew Lee312e8172014-10-23 17:01:36 -07001301 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1302 boolean currentValue =
1303 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1304 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1305 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001306 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1307 } else {
1308 prefSet.removePreference(mEnableVideoCalling);
1309 }
Etan Cohena51d4ef2014-12-15 10:44:42 -08001310
1311 if (ImsManager.isVolteEnabledByPlatform(this) &&
1312 !mPhone.getContext().getResources().getBoolean(
1313 com.android.internal.R.bool.config_carrier_volte_tty_supported)) {
1314 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
1315 tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
1316 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001317 }
1318
Andrew Lee785ca2a2014-12-18 12:46:41 -08001319 @Override
1320 protected void onNewIntent(Intent newIntent) {
1321 setIntent(newIntent);
1322
1323 mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent());
1324 mSubscriptionInfoHelper.setActionBarTitle(
1325 getActionBar(), getResources(), R.string.call_settings_with_label);
1326 mPhone = mSubscriptionInfoHelper.getPhone();
1327 }
1328
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001329 private static void log(String msg) {
1330 Log.d(LOG_TAG, msg);
1331 }
1332
1333 /**
1334 * Updates the look of the VM preference widgets based on current VM provider settings.
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001335 * Note that the provider name is loaded fxrorm the found activity via loadLabel in
1336 * {@link VoicemailProviderListPreference#initVoiceMailProviders()} in order for it to be
1337 * localizable.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001338 */
1339 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1340 final String key = currentProviderSetting;
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001341 final VoicemailProvider provider = mVoicemailProviders.getVoicemailProvider(key);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001342
1343 /* This is the case when we are coming up on a freshly wiped phone and there is no
1344 persisted value for the list preference mVoicemailProviders.
1345 In this case we want to show the UI asking the user to select a voicemail provider as
1346 opposed to silently falling back to default one. */
1347 if (provider == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001348 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> null.");
1349
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001350 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1351 mVoicemailSettings.setEnabled(false);
1352 mVoicemailSettings.setIntent(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001353 mVoicemailNotificationVibrate.setEnabled(false);
1354 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001355 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> " + provider.toString());
1356
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001357 final String providerName = provider.name;
1358 mVoicemailProviders.setSummary(providerName);
1359 mVoicemailSettings.setEnabled(true);
1360 mVoicemailSettings.setIntent(provider.intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001361 mVoicemailNotificationVibrate.setEnabled(true);
1362 }
1363 }
1364
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001365
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001366 /**
1367 * Simulates user clicking on a passed preference.
1368 * Usually needed when the preference is a dialog preference and we want to invoke
1369 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001370 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001371 */
1372 private void simulatePreferenceClick(Preference preference) {
1373 // Go through settings until we find our setting
1374 // and then simulate a click on it to bring up the dialog
1375 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1376 for (int idx = 0; idx < adapter.getCount(); idx++) {
1377 if (adapter.getItem(idx) == preference) {
1378 getPreferenceScreen().onItemClick(this.getListView(),
1379 null, idx, adapter.getItemId(idx));
1380 break;
1381 }
1382 }
1383 }
1384
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001385 @Override
1386 public boolean onOptionsItemSelected(MenuItem item) {
1387 final int itemId = item.getItemId();
1388 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001389 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001390 return true;
1391 }
1392 return super.onOptionsItemSelected(item);
1393 }
Andrew Lee8d66d812014-11-24 14:54:02 -08001394
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001395 /**
1396 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1397 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1398 */
Andrew Lee5efb1122014-12-05 04:20:42 -08001399 public static void goUpToTopLevelSetting(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001400 Activity activity, SubscriptionInfoHelper subscriptionInfoHelper) {
1401 Intent intent = subscriptionInfoHelper.getIntent(CallFeaturesSetting.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001402 intent.setAction(Intent.ACTION_MAIN);
1403 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1404 activity.startActivity(intent);
1405 activity.finish();
1406 }
1407}