blob: 4fa28abadc086c122887bd8fe48ea31e29d90b30 [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 Leeeffef9c2015-02-19 16:02:37 -080052import com.android.ims.ImsConfig;
Andrew Lee312e8172014-10-23 17:01:36 -070053import com.android.ims.ImsManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import com.android.internal.telephony.CallForwardInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import com.android.internal.telephony.Phone;
56import com.android.internal.telephony.PhoneConstants;
Andrew Lee2170a972014-08-13 18:13:01 -070057import com.android.phone.common.util.SettingsUtil;
Andrew Leedb2fe562014-09-03 15:40:43 -070058import com.android.phone.settings.AccountSelectionPreference;
Andrew Lee1af6cf72014-11-04 17:35:26 -080059import com.android.phone.settings.CallForwardInfoUtil;
Andrew Lee6fee2cb2014-12-09 16:42:18 -080060import com.android.phone.settings.TtyModeListPreference;
Andrew Lee84024342014-11-06 23:37:09 -080061import com.android.phone.settings.VoicemailDialogUtil;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -080062import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Andrew Lee64e5c9d2014-11-26 12:00:10 -080063import com.android.phone.settings.VoicemailProviderListPreference;
64import com.android.phone.settings.VoicemailProviderListPreference.VoicemailProvider;
Andrew Leeb490d732014-10-27 15:00:41 -070065import com.android.phone.settings.VoicemailProviderSettings;
Andrew Lee88b51e22014-10-29 15:48:51 -070066import com.android.phone.settings.VoicemailProviderSettingsUtil;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -080067import com.android.phone.settings.VoicemailRingtonePreference;
Andrew Lee5ed870c2014-10-29 11:47:49 -070068import com.android.phone.settings.fdn.FdnSetting;
Sailesh Nepal788959e2014-07-08 23:36:40 -070069import com.android.services.telephony.sip.SipUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070
Andrew Lee2170a972014-08-13 18:13:01 -070071import java.lang.String;
Andrew Leef1776d82014-11-04 14:45:02 -080072import java.util.ArrayList;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073import java.util.Collection;
74import java.util.HashMap;
75import java.util.HashSet;
76import java.util.Iterator;
77import java.util.List;
78import java.util.Map;
79
80/**
81 * Top level "Call settings" UI; see res/xml/call_feature_setting.xml
82 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070083 * This preference screen is the root of the "Call settings" hierarchy available from the Phone
84 * app; the settings here let you control various features related to phone calls (including
85 * voicemail settings, the "Respond via SMS" feature, and others.) It's used only on
86 * voice-capable phone devices.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087 *
88 * Note that this activity is part of the package com.android.phone, even
89 * though you reach it from the "Phone" app (i.e. DialtactsActivity) which
90 * is from the package com.android.contacts.
91 *
92 * For the "Mobile network settings" screen under the main Settings app,
93 * See {@link MobileNetworkSettings}.
94 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070095 * TODO: Settings should be split into PreferenceFragments where possible (ie. voicemail).
Andrew Lee2170a972014-08-13 18:13:01 -070096 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -070097 * @see com.android.phone.MobileNetworkSettings
98 */
99public class CallFeaturesSetting extends PreferenceActivity
100 implements DialogInterface.OnClickListener,
Andrew Lee2170a972014-08-13 18:13:01 -0700101 Preference.OnPreferenceChangeListener,
Andrew Lee2170a972014-08-13 18:13:01 -0700102 EditPhoneNumberPreference.OnDialogClosedListener,
Andrew Leece8ae2a2014-09-10 10:41:48 -0700103 EditPhoneNumberPreference.GetDefaultNumberListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700104 private static final String LOG_TAG = "CallFeaturesSetting";
105 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
Andrew Lee77527ac2014-10-21 16:57:39 -0700106 // STOPSHIP if true. Flag to override behavior default behavior to hide VT setting.
107 private static final boolean ENABLE_VT_FLAG = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700108
109 /**
110 * Intent action to bring up Voicemail Provider settings.
111 *
112 * @see #IGNORE_PROVIDER_EXTRA
113 */
114 public static final String ACTION_ADD_VOICEMAIL =
115 "com.android.phone.CallFeaturesSetting.ADD_VOICEMAIL";
116 // intent action sent by this activity to a voice mail provider
117 // to trigger its configuration UI
118 public static final String ACTION_CONFIGURE_VOICEMAIL =
119 "com.android.phone.CallFeaturesSetting.CONFIGURE_VOICEMAIL";
120 // Extra put in the return from VM provider config containing voicemail number to set
121 public static final String VM_NUMBER_EXTRA = "com.android.phone.VoicemailNumber";
122 // Extra put in the return from VM provider config containing call forwarding number to set
123 public static final String FWD_NUMBER_EXTRA = "com.android.phone.ForwardingNumber";
124 // Extra put in the return from VM provider config containing call forwarding number to set
125 public static final String FWD_NUMBER_TIME_EXTRA = "com.android.phone.ForwardingNumberTime";
126 // If the VM provider returns non null value in this extra we will force the user to
127 // choose another VM provider
128 public static final String SIGNOUT_EXTRA = "com.android.phone.Signout";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700129
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700130 /**
131 * String Extra put into ACTION_ADD_VOICEMAIL call to indicate which provider should be hidden
132 * in the list of providers presented to the user. This allows a provider which is being
133 * disabled (e.g. GV user logging out) to force the user to pick some other provider.
134 */
135 public static final String IGNORE_PROVIDER_EXTRA = "com.android.phone.ProviderToIgnore";
136
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800137 /**
138 * String Extra put into ACTION_ADD_VOICEMAIL to indicate that the voicemail setup screen should
139 * be opened.
140 */
141 public static final String SETUP_VOICEMAIL_EXTRA = "com.android.phone.SetupVoicemail";
142
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700143 // string constants
144 private static final String NUM_PROJECTION[] = {CommonDataKinds.Phone.NUMBER};
145
146 // String keys for preference lookup
147 // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
Andrew Lee8d66d812014-11-24 14:54:02 -0800148 // TODO: Consider moving these strings to strings.xml, so that they are not duplicated here and
149 // in the layout files. These strings need to be treated carefully; if the setting is
150 // persistent, they are used as the key to store shared preferences and the name should not be
151 // changed unless the settings are also migrated.
Andrew Lee97708a42014-09-25 12:39:07 -0700152 private static final String VOICEMAIL_SETTING_SCREEN_PREF_KEY = "button_voicemail_category_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700153 private static final String BUTTON_VOICEMAIL_KEY = "button_voicemail_key";
154 private static final String BUTTON_VOICEMAIL_PROVIDER_KEY = "button_voicemail_provider_key";
155 private static final String BUTTON_VOICEMAIL_SETTING_KEY = "button_voicemail_setting_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700156 private static final String BUTTON_FDN_KEY = "button_fdn_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700157
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700158 private static final String BUTTON_DTMF_KEY = "button_dtmf_settings";
159 private static final String BUTTON_RETRY_KEY = "button_auto_retry_key";
160 private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
161 private static final String BUTTON_HAC_KEY = "button_hac_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700162
163 private static final String BUTTON_GSM_UMTS_OPTIONS = "button_gsm_more_expand_key";
164 private static final String BUTTON_CDMA_OPTIONS = "button_cdma_more_expand_key";
Andrew Lee2b36ba22014-11-05 17:08:49 -0800165 private static final String CALL_FORWARDING_KEY = "call_forwarding_key";
166 private static final String ADDITIONAL_GSM_SETTINGS_KEY = "additional_gsm_call_settings_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700167
Andrew Leece8ae2a2014-09-10 10:41:48 -0700168 private static final String PHONE_ACCOUNT_SETTINGS_KEY =
169 "phone_account_settings_preference_screen";
Andrew Leedb2fe562014-09-03 15:40:43 -0700170
Andrew Leedf14ead2014-10-17 14:22:52 -0700171 private static final String ENABLE_VIDEO_CALLING_KEY = "button_enable_video_calling";
172
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700173 /** Event for Async voicemail change call */
174 private static final int EVENT_VOICEMAIL_CHANGED = 500;
175 private static final int EVENT_FORWARDING_CHANGED = 501;
176 private static final int EVENT_FORWARDING_GET_COMPLETED = 502;
177
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178 public static final String HAC_KEY = "HACSetting";
179 public static final String HAC_VAL_ON = "ON";
180 public static final String HAC_VAL_OFF = "OFF";
181
182 /** Handle to voicemail pref */
183 private static final int VOICEMAIL_PREF_ID = 1;
184 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
185
186 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700187 private AudioManager mAudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188
Andrew Lee5ed870c2014-10-29 11:47:49 -0700189 private SubscriptionInfoHelper mSubscriptionInfoHelper;
190
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700191 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
192
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700193 /** Whether dialpad plays DTMF tone or not. */
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700194 private CheckBoxPreference mButtonAutoRetry;
195 private CheckBoxPreference mButtonHAC;
196 private ListPreference mButtonDTMF;
Andrew Lee6fee2cb2014-12-09 16:42:18 -0800197 private TtyModeListPreference mButtonTTY;
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800198 private VoicemailProviderListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700199 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200 private PreferenceScreen mVoicemailSettings;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800201 private VoicemailRingtonePreference mVoicemailNotificationRingtone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700202 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700203 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700204
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700205 /**
206 * Results of reading forwarding settings
207 */
208 private CallForwardInfo[] mForwardingReadResults = null;
209
210 /**
211 * Result of forwarding number change.
212 * Keys are reasons (eg. unconditional forwarding).
213 */
214 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
215
216 /**
217 * Expected CF read result types.
218 * This set keeps track of the CF types for which we've issued change
219 * commands so we can tell when we've received all of the responses.
220 */
221 private Collection<Integer> mExpectedChangeResultReasons = null;
222
223 /**
224 * Result of vm number change
225 */
226 private AsyncResult mVoicemailChangeResult = null;
227
228 /**
229 * Previous VM provider setting so we can return to it in case of failure.
230 */
231 private String mPreviousVMProviderKey = null;
232
233 /**
234 * Id of the dialog being currently shown.
235 */
236 private int mCurrentDialogId = 0;
237
238 /**
239 * Flag indicating that we are invoking settings for the voicemail provider programmatically
240 * due to vm provider change.
241 */
242 private boolean mVMProviderSettingsForced = false;
243
244 /**
245 * Flag indicating that we are making changes to vm or fwd numbers
246 * due to vm provider change.
247 */
248 private boolean mChangingVMorFwdDueToProviderChange = false;
249
250 /**
251 * True if we are in the process of vm & fwd number change and vm has already been changed.
252 * This is used to decide what to do in case of rollback.
253 */
254 private boolean mVMChangeCompletedSuccessfully = false;
255
256 /**
257 * True if we had full or partial failure setting forwarding numbers and so need to roll them
258 * back.
259 */
260 private boolean mFwdChangesRequireRollback = false;
261
262 /**
263 * Id of error msg to display to user once we are done reverting the VM provider to the previous
264 * one.
265 */
266 private int mVMOrFwdSetError = 0;
267
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700268 /** string to hold old voicemail number as it is being updated. */
269 private String mOldVmNumber;
270
271 // New call forwarding settings and vm number we will be setting
272 // Need to save these since before we get to saving we need to asynchronously
273 // query the existing forwarding settings.
274 private CallForwardInfo[] mNewFwdSettings;
275 private String mNewVMNumber;
276
277 private boolean mForeground;
278
279 @Override
280 public void onPause() {
281 super.onPause();
282 mForeground = false;
Etan Cohena51d4ef2014-12-15 10:44:42 -0800283
284 if (ImsManager.isVolteEnabledByPlatform(this) &&
285 !mPhone.getContext().getResources().getBoolean(
286 com.android.internal.R.bool.config_carrier_volte_tty_supported)) {
287 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
288 tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
289 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700290 }
291
292 /**
293 * We have to pull current settings from the network for all kinds of
294 * voicemail providers so we can tell whether we have to update them,
295 * so use this bit to keep track of whether we're reading settings for the
296 * default provider and should therefore save them out when done.
297 */
298 private boolean mReadingSettingsForDefaultProvider = false;
299
Tyler Gunnbaee2952014-09-10 16:01:02 -0700300 /**
301 * Used to indicate that the voicemail preference should be shown.
302 */
303 private boolean mShowVoicemailPreference = false;
304
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800305 /**
306 * Used to indicate that the voicemail setup screen should be shown.
307 */
308 private boolean mSetupVoicemail = false;
309
Etan Cohena51d4ef2014-12-15 10:44:42 -0800310 private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
311 /**
312 * Enable/disable the TTY setting when in/out of a call (and if carrier doesn't
313 * support VoLTE with TTY).
314 * @see android.telephony.PhoneStateListener#onCallStateChanged(int,
315 * java.lang.String)
316 */
317 @Override
318 public void onCallStateChanged(int state, String incomingNumber) {
319 if (DBG) log("PhoneStateListener.onCallStateChanged: state=" + state);
320 Preference pref = getPreferenceScreen().findPreference(BUTTON_TTY_KEY);
321 if (pref != null) {
322 pref.setEnabled(state == TelephonyManager.CALL_STATE_IDLE);
323 }
324 }
325 };
326
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700327 /*
328 * Click Listeners, handle click based on objects attached to UI.
329 */
330
331 // Click listener for all toggle events
332 @Override
333 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
334 if (preference == mSubMenuVoicemailSettings) {
335 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700336 } else if (preference == mButtonDTMF) {
337 return true;
338 } else if (preference == mButtonTTY) {
Rekha Kumar3d8ea0e2015-03-18 10:04:12 -0700339 if(mPhone.isVideoCallPresent()) {
340 // TTY Mode change is not allowed during a VT call
341 showDialogIfForeground(VoicemailDialogUtil.TTY_SET_RESPONSE_ERROR);
342 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700343 return true;
344 } else if (preference == mButtonAutoRetry) {
345 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
346 android.provider.Settings.Global.CALL_AUTO_RETRY,
347 mButtonAutoRetry.isChecked() ? 1 : 0);
348 return true;
349 } else if (preference == mButtonHAC) {
350 int hac = mButtonHAC.isChecked() ? 1 : 0;
351 // Update HAC value in Settings database
352 Settings.System.putInt(mPhone.getContext().getContentResolver(),
353 Settings.System.HEARING_AID, hac);
354
355 // Update HAC Value in AudioManager
356 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
357 return true;
Andrew Lee696bff52014-12-03 17:58:18 -0800358 } else if (preference.getKey().equals(mVoicemailSettings.getKey())) {
359 // Check key instead of comparing reference because closing the voicemail notification
360 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
361 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800362 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
363
Andrew Lee696bff52014-12-03 17:58:18 -0800364 final Dialog dialog = ((PreferenceScreen) preference).getDialog();
Yorke Leea0f63bf2014-10-09 18:27:20 -0700365 if (dialog != null) {
366 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
367 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800368
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700369 if (preference.getIntent() != null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800370 if (DBG) log("Invoking cfg intent " + preference.getIntent().getPackage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700371
372 // onActivityResult() will be responsible for resetting some of variables.
373 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
374 return true;
375 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800376 if (DBG) log("onPreferenceTreeClick(). No intent; use default behavior in xml.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700377
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800378 // onActivityResult() will not be called, so reset variables here.
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800379 mPreviousVMProviderKey = VoicemailProviderListPreference.DEFAULT_KEY;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700380 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700381 return false;
382 }
Andrew Lee97708a42014-09-25 12:39:07 -0700383 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700384 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
385 if (dialog != null) {
386 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
387 }
Andrew Lee97708a42014-09-25 12:39:07 -0700388 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700389 }
390 return false;
391 }
392
393 /**
394 * Implemented to support onPreferenceChangeListener to look for preference
395 * changes.
396 *
397 * @param preference is the preference to be changed
398 * @param objValue should be the value of the selection, NOT its localized
399 * display value.
400 */
401 @Override
402 public boolean onPreferenceChange(Preference preference, Object objValue) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800403 if (DBG) log("onPreferenceChange: \"" + preference + "\" changed to \"" + objValue + "\"");
Andrew Lee2170a972014-08-13 18:13:01 -0700404
405 if (preference == mButtonDTMF) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700406 int index = mButtonDTMF.findIndexOfValue((String) objValue);
407 Settings.System.putInt(mPhone.getContext().getContentResolver(),
408 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700409 } else if (preference == mVoicemailProviders) {
410 final String newProviderKey = (String) objValue;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800411
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700412 // If previous provider key and the new one is same, we don't need to handle it.
413 if (mPreviousVMProviderKey.equals(newProviderKey)) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800414 if (DBG) log("No change is made to the VM provider setting.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700415 return true;
416 }
417 updateVMPreferenceWidgets(newProviderKey);
418
Andrew Leeb490d732014-10-27 15:00:41 -0700419 final VoicemailProviderSettings newProviderSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800420 VoicemailProviderSettingsUtil.load(this, newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700421
Andrew Lee88b51e22014-10-29 15:48:51 -0700422 // If the user switches to a voice mail provider and we have numbers stored for it we
423 // will automatically change the phone's voice mail and forwarding number to the stored
424 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700425 if (newProviderSettings == null) {
426 // Force the user into a configuration of the chosen provider
427 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
428 mVMProviderSettingsForced = true;
429 simulatePreferenceClick(mVoicemailSettings);
430 } else {
431 if (DBG) log("Saved preferences found - switching to them");
432 // Set this flag so if we get a failure we revert to previous provider
433 mChangingVMorFwdDueToProviderChange = true;
434 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
435 }
Andrew Lee8d66d812014-11-24 14:54:02 -0800436 } else if (preference.getKey().equals(mVoicemailNotificationVibrate.getKey())) {
Andrew Lee696bff52014-12-03 17:58:18 -0800437 // Check key instead of comparing reference because closing the voicemail notification
438 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
439 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee8d66d812014-11-24 14:54:02 -0800440 VoicemailNotificationSettingsUtil.setVibrationEnabled(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800441 mPhone, Boolean.TRUE.equals(objValue));
Andrew Leedf14ead2014-10-17 14:22:52 -0700442 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700443 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
444 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
445 } else {
446 AlertDialog.Builder builder = new AlertDialog.Builder(this);
447 DialogInterface.OnClickListener networkSettingsClickListener =
448 new Dialog.OnClickListener() {
449 @Override
450 public void onClick(DialogInterface dialog, int which) {
451 startActivity(new Intent(mPhone.getContext(),
452 com.android.phone.MobileNetworkSettings.class));
453 }
454 };
455 builder.setMessage(getResources().getString(
456 R.string.enable_video_calling_dialog_msg))
457 .setNeutralButton(getResources().getString(
458 R.string.enable_video_calling_dialog_settings),
459 networkSettingsClickListener)
460 .setPositiveButton(android.R.string.ok, null)
461 .show();
462 return false;
463 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700464 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800465
466 // Always let the preference setting proceed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700467 return true;
468 }
469
470 @Override
471 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800472 if (DBG) log("onDialogClosed: Button clicked is " + buttonClicked);
473
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700474 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
475 return;
476 }
477
478 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700479 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
480 mSubMenuVoicemailSettings.getPhoneNumber(),
481 VoicemailProviderSettings.NO_FORWARDING);
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800482 saveVoiceMailAndForwardingNumber(mVoicemailProviders.getKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700483 }
484 }
485
486 /**
487 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
488 * This method set the default values for the various
489 * EditPhoneNumberPreference dialogs.
490 */
491 @Override
492 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
493 if (preference == mSubMenuVoicemailSettings) {
494 // update the voicemail number field, which takes care of the
495 // mSubMenuVoicemailSettings itself, so we should return null.
496 if (DBG) log("updating default for voicemail dialog");
497 updateVoiceNumberField();
498 return null;
499 }
500
501 String vmDisplay = mPhone.getVoiceMailNumber();
502 if (TextUtils.isEmpty(vmDisplay)) {
503 // if there is no voicemail number, we just return null to
504 // indicate no contribution.
505 return null;
506 }
507
508 // Return the voicemail number prepended with "VM: "
509 if (DBG) log("updating default for call forwarding dialogs");
510 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
511 }
512
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700513 private void switchToPreviousVoicemailProvider() {
514 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700515
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800516 if (mPreviousVMProviderKey == null) {
517 return;
518 }
519
520 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
Andrew Lee84024342014-11-06 23:37:09 -0800521 showDialogIfForeground(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800522 final VoicemailProviderSettings prevSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800523 VoicemailProviderSettingsUtil.load(this, mPreviousVMProviderKey);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800524 if (prevSettings == null) {
525 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
526 + mPreviousVMProviderKey + "\" is null but should be loaded.");
527 }
528
529 if (mVMChangeCompletedSuccessfully) {
530 mNewVMNumber = prevSettings.getVoicemailNumber();
531 Log.i(LOG_TAG, "VM change is already completed successfully."
532 + "Have to revert VM back to " + mNewVMNumber + " again.");
533 mPhone.setVoiceMailNumber(
534 mPhone.getVoiceMailAlphaTag().toString(),
535 mNewVMNumber,
536 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
537 }
538
539 if (mFwdChangesRequireRollback) {
540 Log.i(LOG_TAG, "Requested to rollback forwarding changes.");
541
542 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
543 if (prevFwdSettings != null) {
544 Map<Integer, AsyncResult> results = mForwardingChangeResults;
545 resetForwardingChangeState();
546 for (int i = 0; i < prevFwdSettings.length; i++) {
547 CallForwardInfo fi = prevFwdSettings[i];
548 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
549 // Only revert the settings for which the update succeeded.
550 AsyncResult result = results.get(fi.reason);
551 if (result != null && result.exception == null) {
552 mExpectedChangeResultReasons.add(fi.reason);
553 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
554 mRevertOptionComplete.obtainMessage(
555 EVENT_FORWARDING_CHANGED, i, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700556 }
557 }
558 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700559 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800560 } else {
561 if (DBG) log("No need to revert");
562 onRevertDone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700563 }
564 }
565
566 private void onRevertDone() {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800567 if (DBG) log("onRevertDone: Changing provider key back to " + mPreviousVMProviderKey);
568
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700569 updateVMPreferenceWidgets(mPreviousVMProviderKey);
570 updateVoiceNumberField();
571 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800572 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700573 mVMOrFwdSetError = 0;
574 }
575 }
576
577 @Override
578 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
579 if (DBG) {
580 log("onActivityResult: requestCode: " + requestCode
581 + ", resultCode: " + resultCode
582 + ", data: " + data);
583 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800584
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700585 // there are cases where the contact picker may end up sending us more than one
586 // request. We want to ignore the request if we're not in the correct state.
587 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
588 boolean failure = false;
589
590 // No matter how the processing of result goes lets clear the flag
591 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
592 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
593 mVMProviderSettingsForced = false;
594
595 String vmNum = null;
596 if (resultCode != RESULT_OK) {
597 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
598 failure = true;
599 } else {
600 if (data == null) {
601 if (DBG) log("onActivityResult: vm provider cfg result has no data");
602 failure = true;
603 } else {
604 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
605 if (DBG) log("Provider requested signout");
606 if (isVMProviderSettingsForced) {
607 if (DBG) log("Going back to previous provider on signout");
608 switchToPreviousVoicemailProvider();
609 } else {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800610 final String victim = mVoicemailProviders.getKey();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700611 if (DBG) log("Relaunching activity and ignoring " + victim);
612 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
613 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
614 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
615 this.startActivity(i);
616 }
617 return;
618 }
619 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
620 if (vmNum == null || vmNum.length() == 0) {
621 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
622 failure = true;
623 }
624 }
625 }
626 if (failure) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800627 if (DBG) log("Failure in return from voicemail provider.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700628 if (isVMProviderSettingsForced) {
629 switchToPreviousVoicemailProvider();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700630 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800631
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700632 return;
633 }
634 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
635 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
636
Santos Cordonda120f42014-08-06 04:44:34 -0700637 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700638 // send it to the provider when it's config is invoked so it can use this as default
639 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
640
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800641 if (DBG) log("onActivityResult: cfg result has forwarding number " + fwdNum);
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800642 saveVoiceMailAndForwardingNumber(mVoicemailProviders.getKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700643 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700644 return;
645 }
646
647 if (requestCode == VOICEMAIL_PREF_ID) {
648 if (resultCode != RESULT_OK) {
649 if (DBG) log("onActivityResult: contact picker result not OK.");
650 return;
651 }
652
653 Cursor cursor = null;
654 try {
655 cursor = getContentResolver().query(data.getData(),
656 NUM_PROJECTION, null, null, null);
657 if ((cursor == null) || (!cursor.moveToFirst())) {
658 if (DBG) log("onActivityResult: bad contact data, no results found.");
659 return;
660 }
661 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
662 return;
663 } finally {
664 if (cursor != null) {
665 cursor.close();
666 }
667 }
668 }
669
670 super.onActivityResult(requestCode, resultCode, data);
671 }
672
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700673 /**
674 * Wrapper around showDialog() that will silently do nothing if we're
675 * not in the foreground.
676 *
677 * This is useful here because most of the dialogs we display from
678 * this class are triggered by asynchronous events (like
679 * success/failure messages from the telephony layer) and it's
680 * possible for those events to come in even after the user has gone
681 * to a different screen.
682 */
683 // TODO: this is too brittle: it's still easy to accidentally add new
684 // code here that calls showDialog() directly (which will result in a
685 // WindowManager$BadTokenException if called after the activity has
686 // been stopped.)
687 //
688 // It would be cleaner to do the "if (mForeground)" check in one
689 // central place, maybe by using a single Handler for all asynchronous
690 // events (and have *that* discard events if we're not in the
691 // foreground.)
692 //
693 // Unfortunately it's not that simple, since we sometimes need to do
694 // actual work to handle these events whether or not we're in the
695 // foreground (see the Handler code in mSetOptionComplete for
696 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800697 //
698 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
699 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700700 private void showDialogIfForeground(int id) {
701 if (mForeground) {
702 showDialog(id);
703 }
704 }
705
706 private void dismissDialogSafely(int id) {
707 try {
708 dismissDialog(id);
709 } catch (IllegalArgumentException e) {
710 // This is expected in the case where we were in the background
711 // at the time we would normally have shown the dialog, so we didn't
712 // show it.
713 }
714 }
715
Andrew Lee299e4a72015-01-15 12:44:34 -0800716 /**
717 * TODO: Refactor to make it easier to understand what's done in the different stages.
718 */
Andrew Leeb490d732014-10-27 15:00:41 -0700719 private void saveVoiceMailAndForwardingNumber(
720 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700721 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700722 mNewVMNumber = newSettings.getVoicemailNumber();
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800723 mNewVMNumber = (mNewVMNumber == null) ? "" : mNewVMNumber;
Andrew Leeb490d732014-10-27 15:00:41 -0700724 mNewFwdSettings = newSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700725
Andrew Lee299e4a72015-01-15 12:44:34 -0800726 // No fwd settings on CDMA.
727 boolean isCdma = mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA;
728 if (isCdma) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700729 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700730 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700731 }
732
Andrew Leee3c15212014-10-28 13:12:55 -0700733 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700734 if (mNewVMNumber.equals(mOldVmNumber)
735 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Lee84024342014-11-06 23:37:09 -0800736 showDialogIfForeground(VoicemailDialogUtil.VM_NOCHANGE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700737 return;
738 }
739
Andrew Lee6950c6c2014-11-26 12:05:42 -0800740 VoicemailProviderSettingsUtil.save(this, key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700741 mVMChangeCompletedSuccessfully = false;
742 mFwdChangesRequireRollback = false;
743 mVMOrFwdSetError = 0;
Andrew Lee299e4a72015-01-15 12:44:34 -0800744
745 // Don't read call forwarding settings if CDMA. Call forwarding is not supported by CDMA.
746 if (!key.equals(mPreviousVMProviderKey) && !isCdma) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700747 mReadingSettingsForDefaultProvider =
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800748 mPreviousVMProviderKey.equals(VoicemailProviderListPreference.DEFAULT_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700749 if (DBG) log("Reading current forwarding settings");
Andrew Leeb490d732014-10-27 15:00:41 -0700750 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
751 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
752 for (int i = 0; i < mForwardingReadResults.length; i++) {
753 mPhone.getCallForwardingOption(
754 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700755 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
756 }
Andrew Lee84024342014-11-06 23:37:09 -0800757 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700758 } else {
759 saveVoiceMailAndForwardingNumberStage2();
760 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800761
762 // Refresh the MWI indicator if it is already showing.
763 PhoneGlobals.getInstance().refreshMwiIndicator(mSubscriptionInfoHelper.getSubId());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700764 }
765
766 private final Handler mGetOptionComplete = new Handler() {
767 @Override
768 public void handleMessage(Message msg) {
769 AsyncResult result = (AsyncResult) msg.obj;
770 switch (msg.what) {
771 case EVENT_FORWARDING_GET_COMPLETED:
772 handleForwardingSettingsReadResult(result, msg.arg1);
773 break;
774 }
775 }
776 };
777
778 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
779 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800780
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700781 Throwable error = null;
782 if (ar.exception != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700783 error = ar.exception;
Andrew Lee1af6cf72014-11-04 17:35:26 -0800784 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700785 }
786 if (ar.userObj instanceof Throwable) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800787 error = (Throwable) ar.userObj;
788 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700789 }
790
791 // We may have already gotten an error and decided to ignore the other results.
792 if (mForwardingReadResults == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800793 if (DBG) Log.d(LOG_TAG, "Ignoring fwd reading result: " + idx);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700794 return;
795 }
796
797 // In case of error ignore other results, show an error dialog
798 if (error != null) {
799 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
800 mForwardingReadResults = null;
Andrew Lee84024342014-11-06 23:37:09 -0800801 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
802 showDialogIfForeground(VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700803 return;
804 }
805
Andrew Lee1af6cf72014-11-04 17:35:26 -0800806 // Get the forwarding info.
807 mForwardingReadResults[idx] = CallForwardInfoUtil.getCallForwardInfo(
808 (CallForwardInfo[]) ar.result,
809 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx]);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700810
811 // Check if we got all the results already
812 boolean done = true;
813 for (int i = 0; i < mForwardingReadResults.length; i++) {
814 if (mForwardingReadResults[i] == null) {
815 done = false;
816 break;
817 }
818 }
Andrew Lee1af6cf72014-11-04 17:35:26 -0800819
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700820 if (done) {
821 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
Andrew Lee84024342014-11-06 23:37:09 -0800822 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800823
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700824 if (mReadingSettingsForDefaultProvider) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800825 VoicemailProviderSettingsUtil.save(mPhone.getContext(),
826 VoicemailProviderListPreference.DEFAULT_KEY,
Andrew Lee299e4a72015-01-15 12:44:34 -0800827 new VoicemailProviderSettings(mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700828 mReadingSettingsForDefaultProvider = false;
829 }
830 saveVoiceMailAndForwardingNumberStage2();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700831 }
832 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800833
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700834 private void resetForwardingChangeState() {
835 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
836 mExpectedChangeResultReasons = new HashSet<Integer>();
837 }
838
Andrew Lee299e4a72015-01-15 12:44:34 -0800839 // Called after we are done saving the previous forwarding settings if we needed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700840 private void saveVoiceMailAndForwardingNumberStage2() {
841 mForwardingChangeResults = null;
842 mVoicemailChangeResult = null;
Andrew Leeb490d732014-10-27 15:00:41 -0700843 if (mNewFwdSettings != VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700844 resetForwardingChangeState();
845 for (int i = 0; i < mNewFwdSettings.length; i++) {
846 CallForwardInfo fi = mNewFwdSettings[i];
Andrew Lee1af6cf72014-11-04 17:35:26 -0800847 CallForwardInfo fiForReason =
848 CallForwardInfoUtil.infoForReason(mForwardingReadResults, fi.reason);
849 final boolean doUpdate = CallForwardInfoUtil.isUpdateRequired(fiForReason, fi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700850
851 if (doUpdate) {
852 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
853 mExpectedChangeResultReasons.add(i);
854
Andrew Lee1af6cf72014-11-04 17:35:26 -0800855 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700856 mSetOptionComplete.obtainMessage(
857 EVENT_FORWARDING_CHANGED, fi.reason, 0));
858 }
859 }
Andrew Lee84024342014-11-06 23:37:09 -0800860 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700861 } else {
862 if (DBG) log("Not touching fwd #");
863 setVMNumberWithCarrier();
864 }
865 }
866
867 private void setVMNumberWithCarrier() {
868 if (DBG) log("save voicemail #: " + mNewVMNumber);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800869
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700870 mPhone.setVoiceMailNumber(
871 mPhone.getVoiceMailAlphaTag().toString(),
872 mNewVMNumber,
873 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
874 }
875
876 /**
877 * Callback to handle option update completions
878 */
879 private final Handler mSetOptionComplete = new Handler() {
880 @Override
881 public void handleMessage(Message msg) {
882 AsyncResult result = (AsyncResult) msg.obj;
883 boolean done = false;
884 switch (msg.what) {
885 case EVENT_VOICEMAIL_CHANGED:
886 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700887 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700888 done = true;
889 break;
890 case EVENT_FORWARDING_CHANGED:
891 mForwardingChangeResults.put(msg.arg1, result);
892 if (result.exception != null) {
893 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
894 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700895 }
Andrew Leee438b312014-10-29 16:59:15 -0700896 if (isForwardingCompleted()) {
897 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700898 if (DBG) log("Overall fwd changes completed ok, starting vm change");
899 setVMNumberWithCarrier();
900 } else {
901 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
902 "Check if we need to try rollback for some settings.");
903 mFwdChangesRequireRollback = false;
904 Iterator<Map.Entry<Integer,AsyncResult>> it =
905 mForwardingChangeResults.entrySet().iterator();
906 while (it.hasNext()) {
907 Map.Entry<Integer,AsyncResult> entry = it.next();
908 if (entry.getValue().exception == null) {
909 // If at least one succeeded we have to revert
910 Log.i(LOG_TAG, "Rollback will be required");
911 mFwdChangesRequireRollback = true;
912 break;
913 }
914 }
915 if (!mFwdChangesRequireRollback) {
916 Log.i(LOG_TAG, "No rollback needed.");
917 }
918 done = true;
919 }
920 }
921 break;
922 default:
923 // TODO: should never reach this, may want to throw exception
924 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800925
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700926 if (done) {
927 if (DBG) log("All VM provider related changes done");
928 if (mForwardingChangeResults != null) {
Andrew Lee84024342014-11-06 23:37:09 -0800929 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700930 }
Andrew Leee438b312014-10-29 16:59:15 -0700931 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700932 }
933 }
934 };
935
936 /**
937 * Callback to handle option revert completions
938 */
939 private final Handler mRevertOptionComplete = new Handler() {
940 @Override
941 public void handleMessage(Message msg) {
942 AsyncResult result = (AsyncResult) msg.obj;
943 switch (msg.what) {
944 case EVENT_VOICEMAIL_CHANGED:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700945 if (DBG) log("VM revert complete msg");
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800946 mVoicemailChangeResult = result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700947 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800948
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700949 case EVENT_FORWARDING_CHANGED:
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800950 if (DBG) log("FWD revert complete msg ");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700951 mForwardingChangeResults.put(msg.arg1, result);
952 if (result.exception != null) {
953 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
954 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700955 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700956 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800957
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700958 default:
959 // TODO: should never reach this, may want to throw exception
960 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800961
962 final boolean done = (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null)
963 && (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700964 if (done) {
965 if (DBG) log("All VM reverts done");
Andrew Lee84024342014-11-06 23:37:09 -0800966 dismissDialogSafely(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700967 onRevertDone();
968 }
969 }
970 };
971
972 /**
Andrew Leee438b312014-10-29 16:59:15 -0700973 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700974 */
Andrew Leee438b312014-10-29 16:59:15 -0700975 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700976 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -0700977 return true;
978 }
979
980 for (Integer reason : mExpectedChangeResultReasons) {
981 if (mForwardingChangeResults.get(reason) == null) {
982 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700983 }
984 }
Andrew Leee438b312014-10-29 16:59:15 -0700985
986 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700987 }
Andrew Leee438b312014-10-29 16:59:15 -0700988
989 private boolean isFwdChangeSuccess() {
990 if (mForwardingChangeResults == null) {
991 return true;
992 }
993
994 for (AsyncResult result : mForwardingChangeResults.values()) {
995 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700996 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700997 String msg = exception.getMessage();
998 msg = (msg != null) ? msg : "";
999 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
1000 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001001 }
1002 }
Andrew Leee438b312014-10-29 16:59:15 -07001003 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001004 }
1005
Andrew Leee438b312014-10-29 16:59:15 -07001006 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001007 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -07001008 String msg = mVoicemailChangeResult.exception.getMessage();
1009 msg = (msg != null) ? msg : "";
1010 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
1011 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001012 }
Andrew Leee438b312014-10-29 16:59:15 -07001013 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001014 }
1015
Andrew Leee438b312014-10-29 16:59:15 -07001016 private void handleSetVmOrFwdMessage() {
1017 if (DBG) log("handleSetVMMessage: set VM request complete");
1018
1019 if (!isFwdChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -08001020 handleVmOrFwdSetError(VoicemailDialogUtil.FWD_SET_RESPONSE_ERROR_DIALOG);
Andrew Leee438b312014-10-29 16:59:15 -07001021 } else if (!isVmChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -08001022 handleVmOrFwdSetError(VoicemailDialogUtil.VM_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001023 } else {
Andrew Lee84024342014-11-06 23:37:09 -08001024 handleVmAndFwdSetSuccess(VoicemailDialogUtil.VM_CONFIRM_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001025 }
1026 }
1027
1028 /**
1029 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
1030 * changes to those settings and show "failure" dialog.
1031 *
Andrew Leeab082272014-11-04 15:50:42 -08001032 * @param dialogId ID of the dialog to show for the specific error case. Either
Andrew Lee84024342014-11-06 23:37:09 -08001033 * {@link #FWD_SET_RESPONSE_ERROR_DIALOG} or {@link #VM_RESPONSE_ERROR_DIALOG}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001034 */
Andrew Leeab082272014-11-04 15:50:42 -08001035 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001036 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -08001037 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001038 mChangingVMorFwdDueToProviderChange = false;
1039 switchToPreviousVoicemailProvider();
1040 return;
1041 }
1042 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001043 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001044 updateVoiceNumberField();
1045 }
1046
1047 /**
1048 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1049 * This updates a bunch of variables and show "success" dialog.
1050 */
Andrew Leeab082272014-11-04 15:50:42 -08001051 private void handleVmAndFwdSetSuccess(int dialogId) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001052 if (DBG) log("handleVmAndFwdSetSuccess: key is " + mVoicemailProviders.getKey());
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001053
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001054 mPreviousVMProviderKey = mVoicemailProviders.getKey();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001055 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001056 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001057 updateVoiceNumberField();
1058 }
1059
1060 /**
1061 * Update the voicemail number from what we've recorded on the sim.
1062 */
1063 private void updateVoiceNumberField() {
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001064 if (DBG) log("updateVoiceNumberField()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001065
1066 mOldVmNumber = mPhone.getVoiceMailNumber();
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001067 if (TextUtils.isEmpty(mOldVmNumber)) {
1068 mSubMenuVoicemailSettings.setPhoneNumber("");
1069 mSubMenuVoicemailSettings.setSummary(getString(R.string.voicemail_number_not_set));
1070 } else {
1071 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1072 mSubMenuVoicemailSettings.setSummary(mOldVmNumber);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001073 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001074 }
1075
1076 /*
1077 * Helper Methods for Activity class.
1078 * The initial query commands are split into two pieces now
1079 * for individual expansion. This combined with the ability
1080 * to cancel queries allows for a much better user experience,
1081 * and also ensures that the user only waits to update the
1082 * data that is relevant.
1083 */
1084
1085 @Override
1086 protected void onPrepareDialog(int id, Dialog dialog) {
1087 super.onPrepareDialog(id, dialog);
1088 mCurrentDialogId = id;
1089 }
1090
1091 // dialog creation method, called by showDialog()
1092 @Override
Andrew Lee84024342014-11-06 23:37:09 -08001093 protected Dialog onCreateDialog(int dialogId) {
1094 return VoicemailDialogUtil.getDialog(this, dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001095 }
1096
1097 // This is a method implemented for DialogInterface.OnClickListener.
1098 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1099 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1100 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1101 public void onClick(DialogInterface dialog, int which) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001102 if (DBG) log("onClick: button clicked is " + which);
1103
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001104 dialog.dismiss();
1105 switch (which){
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001106 case DialogInterface.BUTTON_NEGATIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001107 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001108 // We failed to get current forwarding settings and the user
1109 // does not wish to continue.
1110 switchToPreviousVoicemailProvider();
1111 }
1112 break;
1113 case DialogInterface.BUTTON_POSITIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001114 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001115 // We failed to get current forwarding settings but the user
1116 // wishes to continue changing settings to the new vm provider
1117 saveVoiceMailAndForwardingNumberStage2();
1118 } else {
1119 finish();
1120 }
1121 return;
1122 default:
1123 // just let the dialog close and go back to the input
1124 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001125
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001126 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1127 // with settings UI. If we were called to explicitly configure voice mail then
1128 // we finish the settings activity here to come back to whatever the user was doing.
1129 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1130 finish();
1131 }
1132 }
1133
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001134 /*
1135 * Activity class methods
1136 */
1137
1138 @Override
1139 protected void onCreate(Bundle icicle) {
1140 super.onCreate(icicle);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001141 if (DBG) log("onCreate: Intent is " + getIntent());
1142
Jay Shraunerc2684732014-11-12 12:10:37 -08001143 // Make sure we are running as the primary user.
1144 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
1145 Toast.makeText(this, R.string.call_settings_primary_user_only,
1146 Toast.LENGTH_SHORT).show();
1147 finish();
1148 return;
1149 }
1150
Tyler Gunnbaee2952014-09-10 16:01:02 -07001151 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
1152
Tyler Gunnbaee2952014-09-10 16:01:02 -07001153 // Show the voicemail preference in onResume if the calling intent specifies the
1154 // ACTION_ADD_VOICEMAIL action.
1155 mShowVoicemailPreference = (icicle == null) &&
Jay Shraunerbe2fb262014-11-11 15:19:58 -08001156 TextUtils.equals(getIntent().getAction(), ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -08001157 mSetupVoicemail = mShowVoicemailPreference &&
1158 getIntent().getBooleanExtra(SETUP_VOICEMAIL_EXTRA, false);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001159
Andrew Leedd4f6df2014-12-09 19:13:51 -08001160 mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent());
Andrew Lee5ed870c2014-10-29 11:47:49 -07001161 mSubscriptionInfoHelper.setActionBarTitle(
1162 getActionBar(), getResources(), R.string.call_settings_with_label);
Andrew Lee1479dd12014-11-06 23:06:32 -08001163 mPhone = mSubscriptionInfoHelper.getPhone();
Tyler Gunnbaee2952014-09-10 16:01:02 -07001164 }
1165
Tyler Gunnbaee2952014-09-10 16:01:02 -07001166 @Override
1167 protected void onResume() {
1168 super.onResume();
1169 mForeground = true;
1170
1171 PreferenceScreen preferenceScreen = getPreferenceScreen();
1172 if (preferenceScreen != null) {
1173 preferenceScreen.removeAll();
1174 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001175
1176 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001177
Andrew Lee6fee2cb2014-12-09 16:42:18 -08001178 TelecomManager telecomManager = TelecomManager.from(this);
1179 TelephonyManager telephonyManager =
1180 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
1181
1182 Preference phoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
1183 if (telephonyManager.isMultiSimEnabled() || (telecomManager.getSimCallManagers().isEmpty()
1184 && !SipUtil.isVoipSupported(mPhone.getContext()))) {
1185 getPreferenceScreen().removePreference(phoneAccountSettingsPreference);
1186 }
Andrew Leedb2fe562014-09-03 15:40:43 -07001187
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001188 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001189 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1190 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1191 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1192 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001193
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001194 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1195 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1196 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
Andrew Lee6fee2cb2014-12-09 16:42:18 -08001197 mButtonTTY = (TtyModeListPreference) findPreference(
1198 getResources().getString(R.string.tty_mode_key));
Andrew Lee2170a972014-08-13 18:13:01 -07001199
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001200 mVoicemailProviders = (VoicemailProviderListPreference) findPreference(
1201 BUTTON_VOICEMAIL_PROVIDER_KEY);
1202 mVoicemailProviders.init(mPhone, getIntent());
Andrew Lee2c027892014-10-29 11:29:54 -07001203 mVoicemailProviders.setOnPreferenceChangeListener(this);
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001204 mPreviousVMProviderKey = mVoicemailProviders.getValue();
Andrew Lee8d66d812014-11-24 14:54:02 -08001205
Andrew Lee2c027892014-10-29 11:29:54 -07001206 mVoicemailSettingsScreen =
1207 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001208 mVoicemailSettings = (PreferenceScreen) findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
Andrew Lee8d66d812014-11-24 14:54:02 -08001209
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001210 mVoicemailNotificationRingtone = (VoicemailRingtonePreference) findPreference(
1211 getResources().getString(R.string.voicemail_notification_ringtone_key));
1212 mVoicemailNotificationRingtone.init(mPhone);
1213
Andrew Lee8d66d812014-11-24 14:54:02 -08001214 mVoicemailNotificationVibrate = (CheckBoxPreference) findPreference(
1215 getResources().getString(R.string.voicemail_notification_vibrate_key));
1216 mVoicemailNotificationVibrate.setOnPreferenceChangeListener(this);
1217
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001218 updateVMPreferenceWidgets(mVoicemailProviders.getValue());
1219
1220 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001221
Andrew Lee64a7d792014-10-15 17:38:38 -07001222 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1223 mButtonDTMF.setOnPreferenceChangeListener(this);
1224 int dtmf = Settings.System.getInt(getContentResolver(),
1225 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1226 mButtonDTMF.setValueIndex(dtmf);
1227 } else {
1228 prefSet.removePreference(mButtonDTMF);
1229 mButtonDTMF = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001230 }
1231
Andrew Lee64a7d792014-10-15 17:38:38 -07001232 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1233 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1234 int autoretry = Settings.Global.getInt(
1235 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1236 mButtonAutoRetry.setChecked(autoretry != 0);
1237 } else {
1238 prefSet.removePreference(mButtonAutoRetry);
1239 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001240 }
1241
Andrew Lee64a7d792014-10-15 17:38:38 -07001242 if (getResources().getBoolean(R.bool.hac_enabled)) {
1243 mButtonHAC.setOnPreferenceChangeListener(this);
1244 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1245 mButtonHAC.setChecked(hac != 0);
1246 } else {
1247 prefSet.removePreference(mButtonHAC);
1248 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001249 }
1250
Andrew Lee6fee2cb2014-12-09 16:42:18 -08001251 if (!telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported()) {
1252 mButtonTTY.init();
Andrew Lee64a7d792014-10-15 17:38:38 -07001253 } else {
1254 prefSet.removePreference(mButtonTTY);
1255 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001256 }
1257
1258 if (!getResources().getBoolean(R.bool.world_phone)) {
Andrew Lee2b36ba22014-11-05 17:08:49 -08001259 Preference cdmaOptions = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
1260 prefSet.removePreference(cdmaOptions);
1261
1262 // TODO: Support MSIM for this preference option.
1263 Preference gsmOptions = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
1264 prefSet.removePreference(gsmOptions);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001265
1266 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001267 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Junda Liua09d6962014-11-12 13:39:02 -08001268 boolean shouldHideCarrierSettings = Settings.Global.getInt(
1269 getContentResolver(), Settings.Global.HIDE_CARRIER_NETWORK_SETTINGS, 0) == 1;
1270 if (shouldHideCarrierSettings) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001271 prefSet.removePreference(fdnButton);
Junda Liua09d6962014-11-12 13:39:02 -08001272 if (mButtonDTMF != null) {
1273 prefSet.removePreference(mButtonDTMF);
Etan Cohen0ca1c802014-07-07 15:35:48 -07001274 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001275 } else {
Junda Liua09d6962014-11-12 13:39:02 -08001276 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1277 prefSet.removePreference(fdnButton);
1278
1279 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1280 addPreferencesFromResource(R.xml.cdma_call_privacy);
1281 }
1282 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
Andrew Leedd4f6df2014-12-09 19:13:51 -08001283 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(FdnSetting.class));
Junda Liua09d6962014-11-12 13:39:02 -08001284
1285 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
1286 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1287
1288 Preference callForwardingPref = prefSet.findPreference(CALL_FORWARDING_KEY);
1289 callForwardingPref.setIntent(mSubscriptionInfoHelper.getIntent(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001290 GsmUmtsCallForwardOptions.class));
Junda Liua09d6962014-11-12 13:39:02 -08001291
1292 Preference additionalGsmSettingsPref =
1293 prefSet.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
1294 additionalGsmSettingsPref.setIntent(mSubscriptionInfoHelper.getIntent(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001295 GsmUmtsAdditionalCallOptions.class));
Junda Liua09d6962014-11-12 13:39:02 -08001296 }
1297 } else {
1298 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1299 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001300 }
1301 }
1302
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001303 // check the intent that started this activity and pop up the voicemail
1304 // dialog if we've been asked to.
1305 // If we have at least one non default VM provider registered then bring up
1306 // the selection for the VM provider, otherwise bring up a VM number dialog.
1307 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001308 if (mShowVoicemailPreference) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001309 if (DBG) log("ACTION_ADD_VOICEMAIL Intent is thrown");
Tyler Gunn9c1071f2014-12-09 10:07:54 -08001310 if (mSetupVoicemail) {
1311 simulatePreferenceClick(mVoicemailSettingsScreen);
1312 mSetupVoicemail = false;
1313 } else if (mVoicemailProviders.hasMoreThanOneVoicemailProvider()) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001314 if (DBG) log("Voicemail data has more than one provider.");
Tyler Gunnbaee2952014-09-10 16:01:02 -07001315 simulatePreferenceClick(mVoicemailProviders);
1316 } else {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001317 onPreferenceChange(mVoicemailProviders, VoicemailProviderListPreference.DEFAULT_KEY);
1318 mVoicemailProviders.setValue(VoicemailProviderListPreference.DEFAULT_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001319 }
1320 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001321 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001322
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001323 updateVoiceNumberField();
1324 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001325
Andrew Lee8d66d812014-11-24 14:54:02 -08001326 mVoicemailNotificationVibrate.setChecked(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001327 VoicemailNotificationSettingsUtil.isVibrationEnabled(mPhone));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001328
Andrew Lee312e8172014-10-23 17:01:36 -07001329 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1330 boolean currentValue =
1331 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1332 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1333 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001334 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1335 } else {
1336 prefSet.removePreference(mEnableVideoCalling);
1337 }
Etan Cohena51d4ef2014-12-15 10:44:42 -08001338
1339 if (ImsManager.isVolteEnabledByPlatform(this) &&
1340 !mPhone.getContext().getResources().getBoolean(
1341 com.android.internal.R.bool.config_carrier_volte_tty_supported)) {
1342 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
1343 tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
1344 }
Andrew Leeb97fad82015-01-27 15:31:39 -08001345
Andrew Leeeffef9c2015-02-19 16:02:37 -08001346 Preference wifiCallingSettings = findPreference(
1347 getResources().getString(R.string.wifi_calling_settings_key));
Andrew Leeb97fad82015-01-27 15:31:39 -08001348 if (!ImsManager.isWfcEnabledByPlatform(mPhone.getContext())) {
Andrew Leeb97fad82015-01-27 15:31:39 -08001349 prefSet.removePreference(wifiCallingSettings);
Andrew Leeeffef9c2015-02-19 16:02:37 -08001350 } else {
Pavel Zhamaitsiakecae65a2015-03-23 14:45:34 -07001351 int resId = com.android.internal.R.string.wifi_calling_off_summary;
Andrew Leeeffef9c2015-02-19 16:02:37 -08001352 if (ImsManager.isWfcEnabledByUser(mPhone.getContext())) {
1353 int wfcMode = ImsManager.getWfcMode(mPhone.getContext());
1354 switch (wfcMode) {
1355 case ImsConfig.WfcModeFeatureValueConstants.WIFI_ONLY:
Pavel Zhamaitsiakecae65a2015-03-23 14:45:34 -07001356 resId = com.android.internal.R.string.wfc_mode_wifi_only_summary;
Andrew Leeeffef9c2015-02-19 16:02:37 -08001357 break;
1358 case ImsConfig.WfcModeFeatureValueConstants.CELLULAR_PREFERRED:
Pavel Zhamaitsiakecae65a2015-03-23 14:45:34 -07001359 resId = com.android.internal.R.string.wfc_mode_cellular_preferred_summary;
Andrew Leeeffef9c2015-02-19 16:02:37 -08001360 break;
1361 case ImsConfig.WfcModeFeatureValueConstants.WIFI_PREFERRED:
Pavel Zhamaitsiakecae65a2015-03-23 14:45:34 -07001362 resId = com.android.internal.R.string.wfc_mode_wifi_preferred_summary;
Andrew Leeeffef9c2015-02-19 16:02:37 -08001363 break;
1364 default:
1365 if (DBG) log("Unexpected WFC mode value: " + wfcMode);
1366 }
1367 }
1368 wifiCallingSettings.setSummary(resId);
Andrew Leeb97fad82015-01-27 15:31:39 -08001369 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001370 }
1371
Andrew Lee785ca2a2014-12-18 12:46:41 -08001372 @Override
1373 protected void onNewIntent(Intent newIntent) {
1374 setIntent(newIntent);
1375
1376 mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent());
1377 mSubscriptionInfoHelper.setActionBarTitle(
1378 getActionBar(), getResources(), R.string.call_settings_with_label);
1379 mPhone = mSubscriptionInfoHelper.getPhone();
1380 }
1381
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001382 private static void log(String msg) {
1383 Log.d(LOG_TAG, msg);
1384 }
1385
1386 /**
1387 * Updates the look of the VM preference widgets based on current VM provider settings.
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001388 * Note that the provider name is loaded fxrorm the found activity via loadLabel in
1389 * {@link VoicemailProviderListPreference#initVoiceMailProviders()} in order for it to be
1390 * localizable.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001391 */
1392 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1393 final String key = currentProviderSetting;
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001394 final VoicemailProvider provider = mVoicemailProviders.getVoicemailProvider(key);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001395
1396 /* This is the case when we are coming up on a freshly wiped phone and there is no
1397 persisted value for the list preference mVoicemailProviders.
1398 In this case we want to show the UI asking the user to select a voicemail provider as
1399 opposed to silently falling back to default one. */
1400 if (provider == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001401 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> null.");
1402
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001403 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1404 mVoicemailSettings.setEnabled(false);
1405 mVoicemailSettings.setIntent(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001406 mVoicemailNotificationVibrate.setEnabled(false);
1407 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001408 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> " + provider.toString());
1409
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001410 final String providerName = provider.name;
1411 mVoicemailProviders.setSummary(providerName);
1412 mVoicemailSettings.setEnabled(true);
1413 mVoicemailSettings.setIntent(provider.intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001414 mVoicemailNotificationVibrate.setEnabled(true);
1415 }
1416 }
1417
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001418
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001419 /**
1420 * Simulates user clicking on a passed preference.
1421 * Usually needed when the preference is a dialog preference and we want to invoke
1422 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001423 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001424 */
1425 private void simulatePreferenceClick(Preference preference) {
1426 // Go through settings until we find our setting
1427 // and then simulate a click on it to bring up the dialog
1428 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1429 for (int idx = 0; idx < adapter.getCount(); idx++) {
1430 if (adapter.getItem(idx) == preference) {
1431 getPreferenceScreen().onItemClick(this.getListView(),
1432 null, idx, adapter.getItemId(idx));
1433 break;
1434 }
1435 }
1436 }
1437
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001438 @Override
1439 public boolean onOptionsItemSelected(MenuItem item) {
1440 final int itemId = item.getItemId();
1441 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001442 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001443 return true;
1444 }
1445 return super.onOptionsItemSelected(item);
1446 }
Andrew Lee8d66d812014-11-24 14:54:02 -08001447
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001448 /**
1449 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1450 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1451 */
Andrew Lee5efb1122014-12-05 04:20:42 -08001452 public static void goUpToTopLevelSetting(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001453 Activity activity, SubscriptionInfoHelper subscriptionInfoHelper) {
1454 Intent intent = subscriptionInfoHelper.getIntent(CallFeaturesSetting.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001455 intent.setAction(Intent.ACTION_MAIN);
1456 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1457 activity.startActivity(intent);
1458 activity.finish();
1459 }
1460}