blob: fbaddbdb2d3f58fa3dbd5321229bc5529436b78c [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;
Andrew Lee93c345f2014-10-27 15:25:07 -070044import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.text.TextUtils;
46import android.util.Log;
47import android.view.MenuItem;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.widget.ListAdapter;
Jay Shraunerc2684732014-11-12 12:10:37 -080049import android.widget.Toast;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050
Andrew Lee312e8172014-10-23 17:01:36 -070051import com.android.ims.ImsManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import com.android.internal.telephony.CallForwardInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import com.android.internal.telephony.Phone;
54import com.android.internal.telephony.PhoneConstants;
Andrew Lee2170a972014-08-13 18:13:01 -070055import com.android.phone.common.util.SettingsUtil;
Andrew Leedb2fe562014-09-03 15:40:43 -070056import com.android.phone.settings.AccountSelectionPreference;
Andrew Lee1af6cf72014-11-04 17:35:26 -080057import com.android.phone.settings.CallForwardInfoUtil;
Andrew Lee6fee2cb2014-12-09 16:42:18 -080058import com.android.phone.settings.TtyModeListPreference;
Andrew Lee84024342014-11-06 23:37:09 -080059import com.android.phone.settings.VoicemailDialogUtil;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -080060import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Andrew Lee64e5c9d2014-11-26 12:00:10 -080061import com.android.phone.settings.VoicemailProviderListPreference;
62import com.android.phone.settings.VoicemailProviderListPreference.VoicemailProvider;
Andrew Leeb490d732014-10-27 15:00:41 -070063import com.android.phone.settings.VoicemailProviderSettings;
Andrew Lee88b51e22014-10-29 15:48:51 -070064import com.android.phone.settings.VoicemailProviderSettingsUtil;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -080065import com.android.phone.settings.VoicemailRingtonePreference;
Andrew Lee5ed870c2014-10-29 11:47:49 -070066import com.android.phone.settings.fdn.FdnSetting;
Sailesh Nepal788959e2014-07-08 23:36:40 -070067import com.android.services.telephony.sip.SipUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068
Andrew Lee2170a972014-08-13 18:13:01 -070069import java.lang.String;
Andrew Leef1776d82014-11-04 14:45:02 -080070import java.util.ArrayList;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import java.util.Collection;
72import java.util.HashMap;
73import java.util.HashSet;
74import java.util.Iterator;
75import java.util.List;
76import java.util.Map;
77
78/**
79 * Top level "Call settings" UI; see res/xml/call_feature_setting.xml
80 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070081 * This preference screen is the root of the "Call settings" hierarchy available from the Phone
82 * app; the settings here let you control various features related to phone calls (including
83 * voicemail settings, the "Respond via SMS" feature, and others.) It's used only on
84 * voice-capable phone devices.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070085 *
86 * Note that this activity is part of the package com.android.phone, even
87 * though you reach it from the "Phone" app (i.e. DialtactsActivity) which
88 * is from the package com.android.contacts.
89 *
90 * For the "Mobile network settings" screen under the main Settings app,
91 * See {@link MobileNetworkSettings}.
92 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070093 * TODO: Settings should be split into PreferenceFragments where possible (ie. voicemail).
Andrew Lee2170a972014-08-13 18:13:01 -070094 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095 * @see com.android.phone.MobileNetworkSettings
96 */
97public class CallFeaturesSetting extends PreferenceActivity
98 implements DialogInterface.OnClickListener,
Andrew Lee2170a972014-08-13 18:13:01 -070099 Preference.OnPreferenceChangeListener,
Andrew Lee2170a972014-08-13 18:13:01 -0700100 EditPhoneNumberPreference.OnDialogClosedListener,
Andrew Leece8ae2a2014-09-10 10:41:48 -0700101 EditPhoneNumberPreference.GetDefaultNumberListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102 private static final String LOG_TAG = "CallFeaturesSetting";
103 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
Andrew Lee77527ac2014-10-21 16:57:39 -0700104 // STOPSHIP if true. Flag to override behavior default behavior to hide VT setting.
105 private static final boolean ENABLE_VT_FLAG = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700106
107 /**
108 * Intent action to bring up Voicemail Provider settings.
109 *
110 * @see #IGNORE_PROVIDER_EXTRA
111 */
112 public static final String ACTION_ADD_VOICEMAIL =
113 "com.android.phone.CallFeaturesSetting.ADD_VOICEMAIL";
114 // intent action sent by this activity to a voice mail provider
115 // to trigger its configuration UI
116 public static final String ACTION_CONFIGURE_VOICEMAIL =
117 "com.android.phone.CallFeaturesSetting.CONFIGURE_VOICEMAIL";
118 // Extra put in the return from VM provider config containing voicemail number to set
119 public static final String VM_NUMBER_EXTRA = "com.android.phone.VoicemailNumber";
120 // Extra put in the return from VM provider config containing call forwarding number to set
121 public static final String FWD_NUMBER_EXTRA = "com.android.phone.ForwardingNumber";
122 // Extra put in the return from VM provider config containing call forwarding number to set
123 public static final String FWD_NUMBER_TIME_EXTRA = "com.android.phone.ForwardingNumberTime";
124 // If the VM provider returns non null value in this extra we will force the user to
125 // choose another VM provider
126 public static final String SIGNOUT_EXTRA = "com.android.phone.Signout";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128 /**
129 * String Extra put into ACTION_ADD_VOICEMAIL call to indicate which provider should be hidden
130 * in the list of providers presented to the user. This allows a provider which is being
131 * disabled (e.g. GV user logging out) to force the user to pick some other provider.
132 */
133 public static final String IGNORE_PROVIDER_EXTRA = "com.android.phone.ProviderToIgnore";
134
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800135 /**
136 * String Extra put into ACTION_ADD_VOICEMAIL to indicate that the voicemail setup screen should
137 * be opened.
138 */
139 public static final String SETUP_VOICEMAIL_EXTRA = "com.android.phone.SetupVoicemail";
140
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700141 // string constants
142 private static final String NUM_PROJECTION[] = {CommonDataKinds.Phone.NUMBER};
143
144 // String keys for preference lookup
145 // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
Andrew Lee8d66d812014-11-24 14:54:02 -0800146 // TODO: Consider moving these strings to strings.xml, so that they are not duplicated here and
147 // in the layout files. These strings need to be treated carefully; if the setting is
148 // persistent, they are used as the key to store shared preferences and the name should not be
149 // changed unless the settings are also migrated.
Andrew Lee97708a42014-09-25 12:39:07 -0700150 private static final String VOICEMAIL_SETTING_SCREEN_PREF_KEY = "button_voicemail_category_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700151 private static final String BUTTON_VOICEMAIL_KEY = "button_voicemail_key";
152 private static final String BUTTON_VOICEMAIL_PROVIDER_KEY = "button_voicemail_provider_key";
153 private static final String BUTTON_VOICEMAIL_SETTING_KEY = "button_voicemail_setting_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700154 private static final String BUTTON_FDN_KEY = "button_fdn_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700155
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700156 private static final String BUTTON_DTMF_KEY = "button_dtmf_settings";
157 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 /** Whether dialpad plays DTMF tone or not. */
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700192 private CheckBoxPreference mButtonAutoRetry;
193 private CheckBoxPreference mButtonHAC;
194 private ListPreference mButtonDTMF;
Andrew Lee6fee2cb2014-12-09 16:42:18 -0800195 private TtyModeListPreference mButtonTTY;
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800196 private VoicemailProviderListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700197 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198 private PreferenceScreen mVoicemailSettings;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800199 private VoicemailRingtonePreference mVoicemailNotificationRingtone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700201 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700202
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700203 /**
204 * Results of reading forwarding settings
205 */
206 private CallForwardInfo[] mForwardingReadResults = null;
207
208 /**
209 * Result of forwarding number change.
210 * Keys are reasons (eg. unconditional forwarding).
211 */
212 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
213
214 /**
215 * Expected CF read result types.
216 * This set keeps track of the CF types for which we've issued change
217 * commands so we can tell when we've received all of the responses.
218 */
219 private Collection<Integer> mExpectedChangeResultReasons = null;
220
221 /**
222 * Result of vm number change
223 */
224 private AsyncResult mVoicemailChangeResult = null;
225
226 /**
227 * Previous VM provider setting so we can return to it in case of failure.
228 */
229 private String mPreviousVMProviderKey = null;
230
231 /**
232 * Id of the dialog being currently shown.
233 */
234 private int mCurrentDialogId = 0;
235
236 /**
237 * Flag indicating that we are invoking settings for the voicemail provider programmatically
238 * due to vm provider change.
239 */
240 private boolean mVMProviderSettingsForced = false;
241
242 /**
243 * Flag indicating that we are making changes to vm or fwd numbers
244 * due to vm provider change.
245 */
246 private boolean mChangingVMorFwdDueToProviderChange = false;
247
248 /**
249 * True if we are in the process of vm & fwd number change and vm has already been changed.
250 * This is used to decide what to do in case of rollback.
251 */
252 private boolean mVMChangeCompletedSuccessfully = false;
253
254 /**
255 * True if we had full or partial failure setting forwarding numbers and so need to roll them
256 * back.
257 */
258 private boolean mFwdChangesRequireRollback = false;
259
260 /**
261 * Id of error msg to display to user once we are done reverting the VM provider to the previous
262 * one.
263 */
264 private int mVMOrFwdSetError = 0;
265
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700266 /** string to hold old voicemail number as it is being updated. */
267 private String mOldVmNumber;
268
269 // New call forwarding settings and vm number we will be setting
270 // Need to save these since before we get to saving we need to asynchronously
271 // query the existing forwarding settings.
272 private CallForwardInfo[] mNewFwdSettings;
273 private String mNewVMNumber;
274
275 private boolean mForeground;
276
277 @Override
278 public void onPause() {
279 super.onPause();
280 mForeground = false;
281 }
282
283 /**
284 * We have to pull current settings from the network for all kinds of
285 * voicemail providers so we can tell whether we have to update them,
286 * so use this bit to keep track of whether we're reading settings for the
287 * default provider and should therefore save them out when done.
288 */
289 private boolean mReadingSettingsForDefaultProvider = false;
290
Tyler Gunnbaee2952014-09-10 16:01:02 -0700291 /**
292 * Used to indicate that the voicemail preference should be shown.
293 */
294 private boolean mShowVoicemailPreference = false;
295
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800296 /**
297 * Used to indicate that the voicemail setup screen should be shown.
298 */
299 private boolean mSetupVoicemail = false;
300
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301 /*
302 * Click Listeners, handle click based on objects attached to UI.
303 */
304
305 // Click listener for all toggle events
306 @Override
307 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
308 if (preference == mSubMenuVoicemailSettings) {
309 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700310 } else if (preference == mButtonDTMF) {
311 return true;
312 } else if (preference == mButtonTTY) {
313 return true;
314 } else if (preference == mButtonAutoRetry) {
315 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
316 android.provider.Settings.Global.CALL_AUTO_RETRY,
317 mButtonAutoRetry.isChecked() ? 1 : 0);
318 return true;
319 } else if (preference == mButtonHAC) {
320 int hac = mButtonHAC.isChecked() ? 1 : 0;
321 // Update HAC value in Settings database
322 Settings.System.putInt(mPhone.getContext().getContentResolver(),
323 Settings.System.HEARING_AID, hac);
324
325 // Update HAC Value in AudioManager
326 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
327 return true;
Andrew Lee696bff52014-12-03 17:58:18 -0800328 } else if (preference.getKey().equals(mVoicemailSettings.getKey())) {
329 // Check key instead of comparing reference because closing the voicemail notification
330 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
331 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800332 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
333
Andrew Lee696bff52014-12-03 17:58:18 -0800334 final Dialog dialog = ((PreferenceScreen) preference).getDialog();
Yorke Leea0f63bf2014-10-09 18:27:20 -0700335 if (dialog != null) {
336 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
337 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800338
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700339 if (preference.getIntent() != null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800340 if (DBG) log("Invoking cfg intent " + preference.getIntent().getPackage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700341
342 // onActivityResult() will be responsible for resetting some of variables.
343 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
344 return true;
345 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800346 if (DBG) log("onPreferenceTreeClick(). No intent; use default behavior in xml.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700347
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800348 // onActivityResult() will not be called, so reset variables here.
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800349 mPreviousVMProviderKey = VoicemailProviderListPreference.DEFAULT_KEY;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700350 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700351 return false;
352 }
Andrew Lee97708a42014-09-25 12:39:07 -0700353 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700354 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
355 if (dialog != null) {
356 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
357 }
Andrew Lee97708a42014-09-25 12:39:07 -0700358 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700359 }
360 return false;
361 }
362
363 /**
364 * Implemented to support onPreferenceChangeListener to look for preference
365 * changes.
366 *
367 * @param preference is the preference to be changed
368 * @param objValue should be the value of the selection, NOT its localized
369 * display value.
370 */
371 @Override
372 public boolean onPreferenceChange(Preference preference, Object objValue) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800373 if (DBG) log("onPreferenceChange: \"" + preference + "\" changed to \"" + objValue + "\"");
Andrew Lee2170a972014-08-13 18:13:01 -0700374
375 if (preference == mButtonDTMF) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700376 int index = mButtonDTMF.findIndexOfValue((String) objValue);
377 Settings.System.putInt(mPhone.getContext().getContentResolver(),
378 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700379 } else if (preference == mVoicemailProviders) {
380 final String newProviderKey = (String) objValue;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800381
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700382 // If previous provider key and the new one is same, we don't need to handle it.
383 if (mPreviousVMProviderKey.equals(newProviderKey)) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800384 if (DBG) log("No change is made to the VM provider setting.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700385 return true;
386 }
387 updateVMPreferenceWidgets(newProviderKey);
388
Andrew Leeb490d732014-10-27 15:00:41 -0700389 final VoicemailProviderSettings newProviderSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800390 VoicemailProviderSettingsUtil.load(this, newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700391
Andrew Lee88b51e22014-10-29 15:48:51 -0700392 // If the user switches to a voice mail provider and we have numbers stored for it we
393 // will automatically change the phone's voice mail and forwarding number to the stored
394 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700395 if (newProviderSettings == null) {
396 // Force the user into a configuration of the chosen provider
397 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
398 mVMProviderSettingsForced = true;
399 simulatePreferenceClick(mVoicemailSettings);
400 } else {
401 if (DBG) log("Saved preferences found - switching to them");
402 // Set this flag so if we get a failure we revert to previous provider
403 mChangingVMorFwdDueToProviderChange = true;
404 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
405 }
Andrew Lee8d66d812014-11-24 14:54:02 -0800406 } else if (preference.getKey().equals(mVoicemailNotificationVibrate.getKey())) {
Andrew Lee696bff52014-12-03 17:58:18 -0800407 // Check key instead of comparing reference because closing the voicemail notification
408 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
409 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee8d66d812014-11-24 14:54:02 -0800410 VoicemailNotificationSettingsUtil.setVibrationEnabled(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800411 mPhone, Boolean.TRUE.equals(objValue));
Andrew Leedf14ead2014-10-17 14:22:52 -0700412 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700413 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
414 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
415 } else {
416 AlertDialog.Builder builder = new AlertDialog.Builder(this);
417 DialogInterface.OnClickListener networkSettingsClickListener =
418 new Dialog.OnClickListener() {
419 @Override
420 public void onClick(DialogInterface dialog, int which) {
421 startActivity(new Intent(mPhone.getContext(),
422 com.android.phone.MobileNetworkSettings.class));
423 }
424 };
425 builder.setMessage(getResources().getString(
426 R.string.enable_video_calling_dialog_msg))
427 .setNeutralButton(getResources().getString(
428 R.string.enable_video_calling_dialog_settings),
429 networkSettingsClickListener)
430 .setPositiveButton(android.R.string.ok, null)
431 .show();
432 return false;
433 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700434 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800435
436 // Always let the preference setting proceed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700437 return true;
438 }
439
440 @Override
441 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800442 if (DBG) log("onDialogClosed: Button clicked is " + buttonClicked);
443
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700444 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
445 return;
446 }
447
448 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700449 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
450 mSubMenuVoicemailSettings.getPhoneNumber(),
451 VoicemailProviderSettings.NO_FORWARDING);
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800452 saveVoiceMailAndForwardingNumber(mVoicemailProviders.getKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700453 }
454 }
455
456 /**
457 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
458 * This method set the default values for the various
459 * EditPhoneNumberPreference dialogs.
460 */
461 @Override
462 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
463 if (preference == mSubMenuVoicemailSettings) {
464 // update the voicemail number field, which takes care of the
465 // mSubMenuVoicemailSettings itself, so we should return null.
466 if (DBG) log("updating default for voicemail dialog");
467 updateVoiceNumberField();
468 return null;
469 }
470
471 String vmDisplay = mPhone.getVoiceMailNumber();
472 if (TextUtils.isEmpty(vmDisplay)) {
473 // if there is no voicemail number, we just return null to
474 // indicate no contribution.
475 return null;
476 }
477
478 // Return the voicemail number prepended with "VM: "
479 if (DBG) log("updating default for call forwarding dialogs");
480 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
481 }
482
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700483 private void switchToPreviousVoicemailProvider() {
484 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700485
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800486 if (mPreviousVMProviderKey == null) {
487 return;
488 }
489
490 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
Andrew Lee84024342014-11-06 23:37:09 -0800491 showDialogIfForeground(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800492 final VoicemailProviderSettings prevSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800493 VoicemailProviderSettingsUtil.load(this, mPreviousVMProviderKey);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800494 if (prevSettings == null) {
495 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
496 + mPreviousVMProviderKey + "\" is null but should be loaded.");
497 }
498
499 if (mVMChangeCompletedSuccessfully) {
500 mNewVMNumber = prevSettings.getVoicemailNumber();
501 Log.i(LOG_TAG, "VM change is already completed successfully."
502 + "Have to revert VM back to " + mNewVMNumber + " again.");
503 mPhone.setVoiceMailNumber(
504 mPhone.getVoiceMailAlphaTag().toString(),
505 mNewVMNumber,
506 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
507 }
508
509 if (mFwdChangesRequireRollback) {
510 Log.i(LOG_TAG, "Requested to rollback forwarding changes.");
511
512 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
513 if (prevFwdSettings != null) {
514 Map<Integer, AsyncResult> results = mForwardingChangeResults;
515 resetForwardingChangeState();
516 for (int i = 0; i < prevFwdSettings.length; i++) {
517 CallForwardInfo fi = prevFwdSettings[i];
518 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
519 // Only revert the settings for which the update succeeded.
520 AsyncResult result = results.get(fi.reason);
521 if (result != null && result.exception == null) {
522 mExpectedChangeResultReasons.add(fi.reason);
523 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
524 mRevertOptionComplete.obtainMessage(
525 EVENT_FORWARDING_CHANGED, i, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700526 }
527 }
528 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700529 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800530 } else {
531 if (DBG) log("No need to revert");
532 onRevertDone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700533 }
534 }
535
536 private void onRevertDone() {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800537 if (DBG) log("onRevertDone: Changing provider key back to " + mPreviousVMProviderKey);
538
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700539 updateVMPreferenceWidgets(mPreviousVMProviderKey);
540 updateVoiceNumberField();
541 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800542 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700543 mVMOrFwdSetError = 0;
544 }
545 }
546
547 @Override
548 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
549 if (DBG) {
550 log("onActivityResult: requestCode: " + requestCode
551 + ", resultCode: " + resultCode
552 + ", data: " + data);
553 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800554
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700555 // there are cases where the contact picker may end up sending us more than one
556 // request. We want to ignore the request if we're not in the correct state.
557 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
558 boolean failure = false;
559
560 // No matter how the processing of result goes lets clear the flag
561 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
562 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
563 mVMProviderSettingsForced = false;
564
565 String vmNum = null;
566 if (resultCode != RESULT_OK) {
567 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
568 failure = true;
569 } else {
570 if (data == null) {
571 if (DBG) log("onActivityResult: vm provider cfg result has no data");
572 failure = true;
573 } else {
574 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
575 if (DBG) log("Provider requested signout");
576 if (isVMProviderSettingsForced) {
577 if (DBG) log("Going back to previous provider on signout");
578 switchToPreviousVoicemailProvider();
579 } else {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800580 final String victim = mVoicemailProviders.getKey();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700581 if (DBG) log("Relaunching activity and ignoring " + victim);
582 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
583 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
584 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
585 this.startActivity(i);
586 }
587 return;
588 }
589 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
590 if (vmNum == null || vmNum.length() == 0) {
591 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
592 failure = true;
593 }
594 }
595 }
596 if (failure) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800597 if (DBG) log("Failure in return from voicemail provider.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700598 if (isVMProviderSettingsForced) {
599 switchToPreviousVoicemailProvider();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700600 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800601
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700602 return;
603 }
604 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
605 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
606
Santos Cordonda120f42014-08-06 04:44:34 -0700607 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700608 // send it to the provider when it's config is invoked so it can use this as default
609 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
610
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800611 if (DBG) log("onActivityResult: cfg result has forwarding number " + fwdNum);
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800612 saveVoiceMailAndForwardingNumber(mVoicemailProviders.getKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700613 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700614 return;
615 }
616
617 if (requestCode == VOICEMAIL_PREF_ID) {
618 if (resultCode != RESULT_OK) {
619 if (DBG) log("onActivityResult: contact picker result not OK.");
620 return;
621 }
622
623 Cursor cursor = null;
624 try {
625 cursor = getContentResolver().query(data.getData(),
626 NUM_PROJECTION, null, null, null);
627 if ((cursor == null) || (!cursor.moveToFirst())) {
628 if (DBG) log("onActivityResult: bad contact data, no results found.");
629 return;
630 }
631 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
632 return;
633 } finally {
634 if (cursor != null) {
635 cursor.close();
636 }
637 }
638 }
639
640 super.onActivityResult(requestCode, resultCode, data);
641 }
642
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700643 /**
644 * Wrapper around showDialog() that will silently do nothing if we're
645 * not in the foreground.
646 *
647 * This is useful here because most of the dialogs we display from
648 * this class are triggered by asynchronous events (like
649 * success/failure messages from the telephony layer) and it's
650 * possible for those events to come in even after the user has gone
651 * to a different screen.
652 */
653 // TODO: this is too brittle: it's still easy to accidentally add new
654 // code here that calls showDialog() directly (which will result in a
655 // WindowManager$BadTokenException if called after the activity has
656 // been stopped.)
657 //
658 // It would be cleaner to do the "if (mForeground)" check in one
659 // central place, maybe by using a single Handler for all asynchronous
660 // events (and have *that* discard events if we're not in the
661 // foreground.)
662 //
663 // Unfortunately it's not that simple, since we sometimes need to do
664 // actual work to handle these events whether or not we're in the
665 // foreground (see the Handler code in mSetOptionComplete for
666 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800667 //
668 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
669 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700670 private void showDialogIfForeground(int id) {
671 if (mForeground) {
672 showDialog(id);
673 }
674 }
675
676 private void dismissDialogSafely(int id) {
677 try {
678 dismissDialog(id);
679 } catch (IllegalArgumentException e) {
680 // This is expected in the case where we were in the background
681 // at the time we would normally have shown the dialog, so we didn't
682 // show it.
683 }
684 }
685
Andrew Leeb490d732014-10-27 15:00:41 -0700686 private void saveVoiceMailAndForwardingNumber(
687 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700688 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700689 mNewVMNumber = newSettings.getVoicemailNumber();
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800690 mNewVMNumber = (mNewVMNumber == null) ? "" : mNewVMNumber;
Andrew Leeb490d732014-10-27 15:00:41 -0700691 mNewFwdSettings = newSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700692
693 // No fwd settings on CDMA
694 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
695 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700696 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700697 }
698
Andrew Leee3c15212014-10-28 13:12:55 -0700699 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700700 if (mNewVMNumber.equals(mOldVmNumber)
701 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Lee84024342014-11-06 23:37:09 -0800702 showDialogIfForeground(VoicemailDialogUtil.VM_NOCHANGE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700703 return;
704 }
705
Andrew Lee6950c6c2014-11-26 12:05:42 -0800706 VoicemailProviderSettingsUtil.save(this, key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700707 mVMChangeCompletedSuccessfully = false;
708 mFwdChangesRequireRollback = false;
709 mVMOrFwdSetError = 0;
710 if (!key.equals(mPreviousVMProviderKey)) {
711 mReadingSettingsForDefaultProvider =
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800712 mPreviousVMProviderKey.equals(VoicemailProviderListPreference.DEFAULT_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700713 if (DBG) log("Reading current forwarding settings");
Andrew Leeb490d732014-10-27 15:00:41 -0700714 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
715 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
716 for (int i = 0; i < mForwardingReadResults.length; i++) {
717 mPhone.getCallForwardingOption(
718 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700719 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
720 }
Andrew Lee84024342014-11-06 23:37:09 -0800721 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700722 } else {
723 saveVoiceMailAndForwardingNumberStage2();
724 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800725
726 // Refresh the MWI indicator if it is already showing.
727 PhoneGlobals.getInstance().refreshMwiIndicator(mSubscriptionInfoHelper.getSubId());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700728 }
729
730 private final Handler mGetOptionComplete = new Handler() {
731 @Override
732 public void handleMessage(Message msg) {
733 AsyncResult result = (AsyncResult) msg.obj;
734 switch (msg.what) {
735 case EVENT_FORWARDING_GET_COMPLETED:
736 handleForwardingSettingsReadResult(result, msg.arg1);
737 break;
738 }
739 }
740 };
741
742 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
743 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800744
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700745 Throwable error = null;
746 if (ar.exception != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700747 error = ar.exception;
Andrew Lee1af6cf72014-11-04 17:35:26 -0800748 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700749 }
750 if (ar.userObj instanceof Throwable) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800751 error = (Throwable) ar.userObj;
752 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700753 }
754
755 // We may have already gotten an error and decided to ignore the other results.
756 if (mForwardingReadResults == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800757 if (DBG) Log.d(LOG_TAG, "Ignoring fwd reading result: " + idx);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700758 return;
759 }
760
761 // In case of error ignore other results, show an error dialog
762 if (error != null) {
763 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
764 mForwardingReadResults = null;
Andrew Lee84024342014-11-06 23:37:09 -0800765 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
766 showDialogIfForeground(VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700767 return;
768 }
769
Andrew Lee1af6cf72014-11-04 17:35:26 -0800770 // Get the forwarding info.
771 mForwardingReadResults[idx] = CallForwardInfoUtil.getCallForwardInfo(
772 (CallForwardInfo[]) ar.result,
773 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx]);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700774
775 // Check if we got all the results already
776 boolean done = true;
777 for (int i = 0; i < mForwardingReadResults.length; i++) {
778 if (mForwardingReadResults[i] == null) {
779 done = false;
780 break;
781 }
782 }
Andrew Lee1af6cf72014-11-04 17:35:26 -0800783
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700784 if (done) {
785 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
Andrew Lee84024342014-11-06 23:37:09 -0800786 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800787
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700788 if (mReadingSettingsForDefaultProvider) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800789 VoicemailProviderSettingsUtil.save(mPhone.getContext(),
790 VoicemailProviderListPreference.DEFAULT_KEY,
Andrew Lee88b51e22014-10-29 15:48:51 -0700791 new VoicemailProviderSettings(this.mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700792 mReadingSettingsForDefaultProvider = false;
793 }
794 saveVoiceMailAndForwardingNumberStage2();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700795 }
796 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800797
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700798 private void resetForwardingChangeState() {
799 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
800 mExpectedChangeResultReasons = new HashSet<Integer>();
801 }
802
803 // Called after we are done saving the previous forwarding settings if
804 // we needed.
805 private void saveVoiceMailAndForwardingNumberStage2() {
806 mForwardingChangeResults = null;
807 mVoicemailChangeResult = null;
Andrew Leeb490d732014-10-27 15:00:41 -0700808 if (mNewFwdSettings != VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700809 resetForwardingChangeState();
810 for (int i = 0; i < mNewFwdSettings.length; i++) {
811 CallForwardInfo fi = mNewFwdSettings[i];
Andrew Lee1af6cf72014-11-04 17:35:26 -0800812 CallForwardInfo fiForReason =
813 CallForwardInfoUtil.infoForReason(mForwardingReadResults, fi.reason);
814 final boolean doUpdate = CallForwardInfoUtil.isUpdateRequired(fiForReason, fi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700815
816 if (doUpdate) {
817 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
818 mExpectedChangeResultReasons.add(i);
819
Andrew Lee1af6cf72014-11-04 17:35:26 -0800820 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700821 mSetOptionComplete.obtainMessage(
822 EVENT_FORWARDING_CHANGED, fi.reason, 0));
823 }
824 }
Andrew Lee84024342014-11-06 23:37:09 -0800825 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700826 } else {
827 if (DBG) log("Not touching fwd #");
828 setVMNumberWithCarrier();
829 }
830 }
831
832 private void setVMNumberWithCarrier() {
833 if (DBG) log("save voicemail #: " + mNewVMNumber);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800834
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700835 mPhone.setVoiceMailNumber(
836 mPhone.getVoiceMailAlphaTag().toString(),
837 mNewVMNumber,
838 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
839 }
840
841 /**
842 * Callback to handle option update completions
843 */
844 private final Handler mSetOptionComplete = new Handler() {
845 @Override
846 public void handleMessage(Message msg) {
847 AsyncResult result = (AsyncResult) msg.obj;
848 boolean done = false;
849 switch (msg.what) {
850 case EVENT_VOICEMAIL_CHANGED:
851 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700852 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700853 done = true;
854 break;
855 case EVENT_FORWARDING_CHANGED:
856 mForwardingChangeResults.put(msg.arg1, result);
857 if (result.exception != null) {
858 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
859 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700860 }
Andrew Leee438b312014-10-29 16:59:15 -0700861 if (isForwardingCompleted()) {
862 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700863 if (DBG) log("Overall fwd changes completed ok, starting vm change");
864 setVMNumberWithCarrier();
865 } else {
866 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
867 "Check if we need to try rollback for some settings.");
868 mFwdChangesRequireRollback = false;
869 Iterator<Map.Entry<Integer,AsyncResult>> it =
870 mForwardingChangeResults.entrySet().iterator();
871 while (it.hasNext()) {
872 Map.Entry<Integer,AsyncResult> entry = it.next();
873 if (entry.getValue().exception == null) {
874 // If at least one succeeded we have to revert
875 Log.i(LOG_TAG, "Rollback will be required");
876 mFwdChangesRequireRollback = true;
877 break;
878 }
879 }
880 if (!mFwdChangesRequireRollback) {
881 Log.i(LOG_TAG, "No rollback needed.");
882 }
883 done = true;
884 }
885 }
886 break;
887 default:
888 // TODO: should never reach this, may want to throw exception
889 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800890
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700891 if (done) {
892 if (DBG) log("All VM provider related changes done");
893 if (mForwardingChangeResults != null) {
Andrew Lee84024342014-11-06 23:37:09 -0800894 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700895 }
Andrew Leee438b312014-10-29 16:59:15 -0700896 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700897 }
898 }
899 };
900
901 /**
902 * Callback to handle option revert completions
903 */
904 private final Handler mRevertOptionComplete = new Handler() {
905 @Override
906 public void handleMessage(Message msg) {
907 AsyncResult result = (AsyncResult) msg.obj;
908 switch (msg.what) {
909 case EVENT_VOICEMAIL_CHANGED:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700910 if (DBG) log("VM revert complete msg");
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800911 mVoicemailChangeResult = result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700912 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800913
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700914 case EVENT_FORWARDING_CHANGED:
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800915 if (DBG) log("FWD revert complete msg ");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700916 mForwardingChangeResults.put(msg.arg1, result);
917 if (result.exception != null) {
918 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
919 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700920 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700921 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800922
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700923 default:
924 // TODO: should never reach this, may want to throw exception
925 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800926
927 final boolean done = (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null)
928 && (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700929 if (done) {
930 if (DBG) log("All VM reverts done");
Andrew Lee84024342014-11-06 23:37:09 -0800931 dismissDialogSafely(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700932 onRevertDone();
933 }
934 }
935 };
936
937 /**
Andrew Leee438b312014-10-29 16:59:15 -0700938 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700939 */
Andrew Leee438b312014-10-29 16:59:15 -0700940 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700941 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -0700942 return true;
943 }
944
945 for (Integer reason : mExpectedChangeResultReasons) {
946 if (mForwardingChangeResults.get(reason) == null) {
947 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700948 }
949 }
Andrew Leee438b312014-10-29 16:59:15 -0700950
951 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700952 }
Andrew Leee438b312014-10-29 16:59:15 -0700953
954 private boolean isFwdChangeSuccess() {
955 if (mForwardingChangeResults == null) {
956 return true;
957 }
958
959 for (AsyncResult result : mForwardingChangeResults.values()) {
960 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700961 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700962 String msg = exception.getMessage();
963 msg = (msg != null) ? msg : "";
964 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
965 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700966 }
967 }
Andrew Leee438b312014-10-29 16:59:15 -0700968 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700969 }
970
Andrew Leee438b312014-10-29 16:59:15 -0700971 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700972 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700973 String msg = mVoicemailChangeResult.exception.getMessage();
974 msg = (msg != null) ? msg : "";
975 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
976 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700977 }
Andrew Leee438b312014-10-29 16:59:15 -0700978 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700979 }
980
Andrew Leee438b312014-10-29 16:59:15 -0700981 private void handleSetVmOrFwdMessage() {
982 if (DBG) log("handleSetVMMessage: set VM request complete");
983
984 if (!isFwdChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -0800985 handleVmOrFwdSetError(VoicemailDialogUtil.FWD_SET_RESPONSE_ERROR_DIALOG);
Andrew Leee438b312014-10-29 16:59:15 -0700986 } else if (!isVmChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -0800987 handleVmOrFwdSetError(VoicemailDialogUtil.VM_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700988 } else {
Andrew Lee84024342014-11-06 23:37:09 -0800989 handleVmAndFwdSetSuccess(VoicemailDialogUtil.VM_CONFIRM_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700990 }
991 }
992
993 /**
994 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
995 * changes to those settings and show "failure" dialog.
996 *
Andrew Leeab082272014-11-04 15:50:42 -0800997 * @param dialogId ID of the dialog to show for the specific error case. Either
Andrew Lee84024342014-11-06 23:37:09 -0800998 * {@link #FWD_SET_RESPONSE_ERROR_DIALOG} or {@link #VM_RESPONSE_ERROR_DIALOG}
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700999 */
Andrew Leeab082272014-11-04 15:50:42 -08001000 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001001 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -08001002 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001003 mChangingVMorFwdDueToProviderChange = false;
1004 switchToPreviousVoicemailProvider();
1005 return;
1006 }
1007 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001008 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001009 updateVoiceNumberField();
1010 }
1011
1012 /**
1013 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1014 * This updates a bunch of variables and show "success" dialog.
1015 */
Andrew Leeab082272014-11-04 15:50:42 -08001016 private void handleVmAndFwdSetSuccess(int dialogId) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001017 if (DBG) log("handleVmAndFwdSetSuccess: key is " + mVoicemailProviders.getKey());
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001018
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001019 mPreviousVMProviderKey = mVoicemailProviders.getKey();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001020 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001021 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001022 updateVoiceNumberField();
1023 }
1024
1025 /**
1026 * Update the voicemail number from what we've recorded on the sim.
1027 */
1028 private void updateVoiceNumberField() {
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001029 if (DBG) log("updateVoiceNumberField()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001030
1031 mOldVmNumber = mPhone.getVoiceMailNumber();
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001032 if (TextUtils.isEmpty(mOldVmNumber)) {
1033 mSubMenuVoicemailSettings.setPhoneNumber("");
1034 mSubMenuVoicemailSettings.setSummary(getString(R.string.voicemail_number_not_set));
1035 } else {
1036 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1037 mSubMenuVoicemailSettings.setSummary(mOldVmNumber);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001038 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001039 }
1040
1041 /*
1042 * Helper Methods for Activity class.
1043 * The initial query commands are split into two pieces now
1044 * for individual expansion. This combined with the ability
1045 * to cancel queries allows for a much better user experience,
1046 * and also ensures that the user only waits to update the
1047 * data that is relevant.
1048 */
1049
1050 @Override
1051 protected void onPrepareDialog(int id, Dialog dialog) {
1052 super.onPrepareDialog(id, dialog);
1053 mCurrentDialogId = id;
1054 }
1055
1056 // dialog creation method, called by showDialog()
1057 @Override
Andrew Lee84024342014-11-06 23:37:09 -08001058 protected Dialog onCreateDialog(int dialogId) {
1059 return VoicemailDialogUtil.getDialog(this, dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001060 }
1061
1062 // This is a method implemented for DialogInterface.OnClickListener.
1063 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1064 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1065 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1066 public void onClick(DialogInterface dialog, int which) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001067 if (DBG) log("onClick: button clicked is " + which);
1068
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001069 dialog.dismiss();
1070 switch (which){
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001071 case DialogInterface.BUTTON_NEGATIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001072 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001073 // We failed to get current forwarding settings and the user
1074 // does not wish to continue.
1075 switchToPreviousVoicemailProvider();
1076 }
1077 break;
1078 case DialogInterface.BUTTON_POSITIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001079 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001080 // We failed to get current forwarding settings but the user
1081 // wishes to continue changing settings to the new vm provider
1082 saveVoiceMailAndForwardingNumberStage2();
1083 } else {
1084 finish();
1085 }
1086 return;
1087 default:
1088 // just let the dialog close and go back to the input
1089 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001090
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001091 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1092 // with settings UI. If we were called to explicitly configure voice mail then
1093 // we finish the settings activity here to come back to whatever the user was doing.
1094 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1095 finish();
1096 }
1097 }
1098
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001099 /*
1100 * Activity class methods
1101 */
1102
1103 @Override
1104 protected void onCreate(Bundle icicle) {
1105 super.onCreate(icicle);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001106 if (DBG) log("onCreate: Intent is " + getIntent());
1107
Jay Shraunerc2684732014-11-12 12:10:37 -08001108 // Make sure we are running as the primary user.
1109 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
1110 Toast.makeText(this, R.string.call_settings_primary_user_only,
1111 Toast.LENGTH_SHORT).show();
1112 finish();
1113 return;
1114 }
1115
Tyler Gunnbaee2952014-09-10 16:01:02 -07001116 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
1117
Tyler Gunnbaee2952014-09-10 16:01:02 -07001118 // Show the voicemail preference in onResume if the calling intent specifies the
1119 // ACTION_ADD_VOICEMAIL action.
1120 mShowVoicemailPreference = (icicle == null) &&
Jay Shraunerbe2fb262014-11-11 15:19:58 -08001121 TextUtils.equals(getIntent().getAction(), ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -08001122 mSetupVoicemail = mShowVoicemailPreference &&
1123 getIntent().getBooleanExtra(SETUP_VOICEMAIL_EXTRA, false);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001124
Andrew Leedd4f6df2014-12-09 19:13:51 -08001125 mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent());
Andrew Lee5ed870c2014-10-29 11:47:49 -07001126 mSubscriptionInfoHelper.setActionBarTitle(
1127 getActionBar(), getResources(), R.string.call_settings_with_label);
Andrew Lee1479dd12014-11-06 23:06:32 -08001128 mPhone = mSubscriptionInfoHelper.getPhone();
Tyler Gunnbaee2952014-09-10 16:01:02 -07001129 }
1130
Tyler Gunnbaee2952014-09-10 16:01:02 -07001131 @Override
1132 protected void onResume() {
1133 super.onResume();
1134 mForeground = true;
1135
1136 PreferenceScreen preferenceScreen = getPreferenceScreen();
1137 if (preferenceScreen != null) {
1138 preferenceScreen.removeAll();
1139 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001140
1141 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001142
Andrew Lee6fee2cb2014-12-09 16:42:18 -08001143 TelecomManager telecomManager = TelecomManager.from(this);
1144 TelephonyManager telephonyManager =
1145 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
1146
1147 Preference phoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
1148 if (telephonyManager.isMultiSimEnabled() || (telecomManager.getSimCallManagers().isEmpty()
1149 && !SipUtil.isVoipSupported(mPhone.getContext()))) {
1150 getPreferenceScreen().removePreference(phoneAccountSettingsPreference);
1151 }
Andrew Leedb2fe562014-09-03 15:40:43 -07001152
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001153 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001154 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1155 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1156 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1157 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001158
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001159 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1160 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1161 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
Andrew Lee6fee2cb2014-12-09 16:42:18 -08001162 mButtonTTY = (TtyModeListPreference) findPreference(
1163 getResources().getString(R.string.tty_mode_key));
Andrew Lee2170a972014-08-13 18:13:01 -07001164
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001165 mVoicemailProviders = (VoicemailProviderListPreference) findPreference(
1166 BUTTON_VOICEMAIL_PROVIDER_KEY);
1167 mVoicemailProviders.init(mPhone, getIntent());
Andrew Lee2c027892014-10-29 11:29:54 -07001168 mVoicemailProviders.setOnPreferenceChangeListener(this);
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001169 mPreviousVMProviderKey = mVoicemailProviders.getValue();
Andrew Lee8d66d812014-11-24 14:54:02 -08001170
Andrew Lee2c027892014-10-29 11:29:54 -07001171 mVoicemailSettingsScreen =
1172 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001173 mVoicemailSettings = (PreferenceScreen) findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
Andrew Lee8d66d812014-11-24 14:54:02 -08001174
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001175 mVoicemailNotificationRingtone = (VoicemailRingtonePreference) findPreference(
1176 getResources().getString(R.string.voicemail_notification_ringtone_key));
1177 mVoicemailNotificationRingtone.init(mPhone);
1178
Andrew Lee8d66d812014-11-24 14:54:02 -08001179 mVoicemailNotificationVibrate = (CheckBoxPreference) findPreference(
1180 getResources().getString(R.string.voicemail_notification_vibrate_key));
1181 mVoicemailNotificationVibrate.setOnPreferenceChangeListener(this);
1182
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001183 updateVMPreferenceWidgets(mVoicemailProviders.getValue());
1184
1185 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001186
Andrew Lee64a7d792014-10-15 17:38:38 -07001187 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1188 mButtonDTMF.setOnPreferenceChangeListener(this);
1189 int dtmf = Settings.System.getInt(getContentResolver(),
1190 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1191 mButtonDTMF.setValueIndex(dtmf);
1192 } else {
1193 prefSet.removePreference(mButtonDTMF);
1194 mButtonDTMF = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001195 }
1196
Andrew Lee64a7d792014-10-15 17:38:38 -07001197 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1198 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1199 int autoretry = Settings.Global.getInt(
1200 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1201 mButtonAutoRetry.setChecked(autoretry != 0);
1202 } else {
1203 prefSet.removePreference(mButtonAutoRetry);
1204 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001205 }
1206
Andrew Lee64a7d792014-10-15 17:38:38 -07001207 if (getResources().getBoolean(R.bool.hac_enabled)) {
1208 mButtonHAC.setOnPreferenceChangeListener(this);
1209 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1210 mButtonHAC.setChecked(hac != 0);
1211 } else {
1212 prefSet.removePreference(mButtonHAC);
1213 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001214 }
1215
Andrew Lee6fee2cb2014-12-09 16:42:18 -08001216 if (!telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported()) {
1217 mButtonTTY.init();
Andrew Lee64a7d792014-10-15 17:38:38 -07001218 } else {
1219 prefSet.removePreference(mButtonTTY);
1220 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001221 }
1222
1223 if (!getResources().getBoolean(R.bool.world_phone)) {
Andrew Lee2b36ba22014-11-05 17:08:49 -08001224 Preference cdmaOptions = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
1225 prefSet.removePreference(cdmaOptions);
1226
1227 // TODO: Support MSIM for this preference option.
1228 Preference gsmOptions = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
1229 prefSet.removePreference(gsmOptions);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001230
1231 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001232 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Junda Liua09d6962014-11-12 13:39:02 -08001233 boolean shouldHideCarrierSettings = Settings.Global.getInt(
1234 getContentResolver(), Settings.Global.HIDE_CARRIER_NETWORK_SETTINGS, 0) == 1;
1235 if (shouldHideCarrierSettings) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001236 prefSet.removePreference(fdnButton);
Junda Liua09d6962014-11-12 13:39:02 -08001237 if (mButtonDTMF != null) {
1238 prefSet.removePreference(mButtonDTMF);
Etan Cohen0ca1c802014-07-07 15:35:48 -07001239 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001240 } else {
Junda Liua09d6962014-11-12 13:39:02 -08001241 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1242 prefSet.removePreference(fdnButton);
1243
1244 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1245 addPreferencesFromResource(R.xml.cdma_call_privacy);
1246 }
1247 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
Andrew Leedd4f6df2014-12-09 19:13:51 -08001248 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(FdnSetting.class));
Junda Liua09d6962014-11-12 13:39:02 -08001249
1250 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
1251 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1252
1253 Preference callForwardingPref = prefSet.findPreference(CALL_FORWARDING_KEY);
1254 callForwardingPref.setIntent(mSubscriptionInfoHelper.getIntent(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001255 GsmUmtsCallForwardOptions.class));
Junda Liua09d6962014-11-12 13:39:02 -08001256
1257 Preference additionalGsmSettingsPref =
1258 prefSet.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
1259 additionalGsmSettingsPref.setIntent(mSubscriptionInfoHelper.getIntent(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001260 GsmUmtsAdditionalCallOptions.class));
Junda Liua09d6962014-11-12 13:39:02 -08001261 }
1262 } else {
1263 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1264 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001265 }
1266 }
1267
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001268 // check the intent that started this activity and pop up the voicemail
1269 // dialog if we've been asked to.
1270 // If we have at least one non default VM provider registered then bring up
1271 // the selection for the VM provider, otherwise bring up a VM number dialog.
1272 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001273 if (mShowVoicemailPreference) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001274 if (DBG) log("ACTION_ADD_VOICEMAIL Intent is thrown");
Tyler Gunn9c1071f2014-12-09 10:07:54 -08001275 if (mSetupVoicemail) {
1276 simulatePreferenceClick(mVoicemailSettingsScreen);
1277 mSetupVoicemail = false;
1278 } else if (mVoicemailProviders.hasMoreThanOneVoicemailProvider()) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001279 if (DBG) log("Voicemail data has more than one provider.");
Tyler Gunnbaee2952014-09-10 16:01:02 -07001280 simulatePreferenceClick(mVoicemailProviders);
1281 } else {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001282 onPreferenceChange(mVoicemailProviders, VoicemailProviderListPreference.DEFAULT_KEY);
1283 mVoicemailProviders.setValue(VoicemailProviderListPreference.DEFAULT_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001284 }
1285 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001286 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001287
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001288 updateVoiceNumberField();
1289 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001290
Andrew Lee8d66d812014-11-24 14:54:02 -08001291 mVoicemailNotificationVibrate.setChecked(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001292 VoicemailNotificationSettingsUtil.isVibrationEnabled(mPhone));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001293
Andrew Lee312e8172014-10-23 17:01:36 -07001294 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1295 boolean currentValue =
1296 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1297 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1298 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001299 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1300 } else {
1301 prefSet.removePreference(mEnableVideoCalling);
1302 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001303 }
1304
Andrew Lee785ca2a2014-12-18 12:46:41 -08001305 @Override
1306 protected void onNewIntent(Intent newIntent) {
1307 setIntent(newIntent);
1308
1309 mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent());
1310 mSubscriptionInfoHelper.setActionBarTitle(
1311 getActionBar(), getResources(), R.string.call_settings_with_label);
1312 mPhone = mSubscriptionInfoHelper.getPhone();
1313 }
1314
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001315 private static void log(String msg) {
1316 Log.d(LOG_TAG, msg);
1317 }
1318
1319 /**
1320 * Updates the look of the VM preference widgets based on current VM provider settings.
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001321 * Note that the provider name is loaded fxrorm the found activity via loadLabel in
1322 * {@link VoicemailProviderListPreference#initVoiceMailProviders()} in order for it to be
1323 * localizable.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001324 */
1325 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1326 final String key = currentProviderSetting;
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001327 final VoicemailProvider provider = mVoicemailProviders.getVoicemailProvider(key);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001328
1329 /* This is the case when we are coming up on a freshly wiped phone and there is no
1330 persisted value for the list preference mVoicemailProviders.
1331 In this case we want to show the UI asking the user to select a voicemail provider as
1332 opposed to silently falling back to default one. */
1333 if (provider == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001334 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> null.");
1335
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001336 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1337 mVoicemailSettings.setEnabled(false);
1338 mVoicemailSettings.setIntent(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001339 mVoicemailNotificationVibrate.setEnabled(false);
1340 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001341 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> " + provider.toString());
1342
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001343 final String providerName = provider.name;
1344 mVoicemailProviders.setSummary(providerName);
1345 mVoicemailSettings.setEnabled(true);
1346 mVoicemailSettings.setIntent(provider.intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001347 mVoicemailNotificationVibrate.setEnabled(true);
1348 }
1349 }
1350
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001351
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001352 /**
1353 * Simulates user clicking on a passed preference.
1354 * Usually needed when the preference is a dialog preference and we want to invoke
1355 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001356 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001357 */
1358 private void simulatePreferenceClick(Preference preference) {
1359 // Go through settings until we find our setting
1360 // and then simulate a click on it to bring up the dialog
1361 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1362 for (int idx = 0; idx < adapter.getCount(); idx++) {
1363 if (adapter.getItem(idx) == preference) {
1364 getPreferenceScreen().onItemClick(this.getListView(),
1365 null, idx, adapter.getItemId(idx));
1366 break;
1367 }
1368 }
1369 }
1370
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001371 @Override
1372 public boolean onOptionsItemSelected(MenuItem item) {
1373 final int itemId = item.getItemId();
1374 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001375 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001376 return true;
1377 }
1378 return super.onOptionsItemSelected(item);
1379 }
Andrew Lee8d66d812014-11-24 14:54:02 -08001380
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001381 /**
1382 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1383 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1384 */
Andrew Lee5efb1122014-12-05 04:20:42 -08001385 public static void goUpToTopLevelSetting(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001386 Activity activity, SubscriptionInfoHelper subscriptionInfoHelper) {
1387 Intent intent = subscriptionInfoHelper.getIntent(CallFeaturesSetting.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001388 intent.setAction(Intent.ACTION_MAIN);
1389 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1390 activity.startActivity(intent);
1391 activity.finish();
1392 }
1393}