blob: ed600eef8eb7401729b0658fe156f5abeed7b7d9 [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) {
339 return true;
340 } else if (preference == mButtonAutoRetry) {
341 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
342 android.provider.Settings.Global.CALL_AUTO_RETRY,
343 mButtonAutoRetry.isChecked() ? 1 : 0);
344 return true;
345 } else if (preference == mButtonHAC) {
346 int hac = mButtonHAC.isChecked() ? 1 : 0;
347 // Update HAC value in Settings database
348 Settings.System.putInt(mPhone.getContext().getContentResolver(),
349 Settings.System.HEARING_AID, hac);
350
351 // Update HAC Value in AudioManager
352 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
353 return true;
Andrew Lee696bff52014-12-03 17:58:18 -0800354 } else if (preference.getKey().equals(mVoicemailSettings.getKey())) {
355 // Check key instead of comparing reference because closing the voicemail notification
356 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
357 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800358 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
359
Andrew Lee696bff52014-12-03 17:58:18 -0800360 final Dialog dialog = ((PreferenceScreen) preference).getDialog();
Yorke Leea0f63bf2014-10-09 18:27:20 -0700361 if (dialog != null) {
362 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
363 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800364
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700365 if (preference.getIntent() != null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800366 if (DBG) log("Invoking cfg intent " + preference.getIntent().getPackage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700367
368 // onActivityResult() will be responsible for resetting some of variables.
369 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
370 return true;
371 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800372 if (DBG) log("onPreferenceTreeClick(). No intent; use default behavior in xml.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700373
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800374 // onActivityResult() will not be called, so reset variables here.
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800375 mPreviousVMProviderKey = VoicemailProviderListPreference.DEFAULT_KEY;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700376 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700377 return false;
378 }
Andrew Lee97708a42014-09-25 12:39:07 -0700379 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700380 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
381 if (dialog != null) {
382 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
383 }
Andrew Lee97708a42014-09-25 12:39:07 -0700384 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700385 }
386 return false;
387 }
388
389 /**
390 * Implemented to support onPreferenceChangeListener to look for preference
391 * changes.
392 *
393 * @param preference is the preference to be changed
394 * @param objValue should be the value of the selection, NOT its localized
395 * display value.
396 */
397 @Override
398 public boolean onPreferenceChange(Preference preference, Object objValue) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800399 if (DBG) log("onPreferenceChange: \"" + preference + "\" changed to \"" + objValue + "\"");
Andrew Lee2170a972014-08-13 18:13:01 -0700400
401 if (preference == mButtonDTMF) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700402 int index = mButtonDTMF.findIndexOfValue((String) objValue);
403 Settings.System.putInt(mPhone.getContext().getContentResolver(),
404 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700405 } else if (preference == mVoicemailProviders) {
406 final String newProviderKey = (String) objValue;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800407
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700408 // If previous provider key and the new one is same, we don't need to handle it.
409 if (mPreviousVMProviderKey.equals(newProviderKey)) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800410 if (DBG) log("No change is made to the VM provider setting.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700411 return true;
412 }
413 updateVMPreferenceWidgets(newProviderKey);
414
Andrew Leeb490d732014-10-27 15:00:41 -0700415 final VoicemailProviderSettings newProviderSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800416 VoicemailProviderSettingsUtil.load(this, newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700417
Andrew Lee88b51e22014-10-29 15:48:51 -0700418 // If the user switches to a voice mail provider and we have numbers stored for it we
419 // will automatically change the phone's voice mail and forwarding number to the stored
420 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700421 if (newProviderSettings == null) {
422 // Force the user into a configuration of the chosen provider
423 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
424 mVMProviderSettingsForced = true;
425 simulatePreferenceClick(mVoicemailSettings);
426 } else {
427 if (DBG) log("Saved preferences found - switching to them");
428 // Set this flag so if we get a failure we revert to previous provider
429 mChangingVMorFwdDueToProviderChange = true;
430 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
431 }
Andrew Lee8d66d812014-11-24 14:54:02 -0800432 } else if (preference.getKey().equals(mVoicemailNotificationVibrate.getKey())) {
Andrew Lee696bff52014-12-03 17:58:18 -0800433 // Check key instead of comparing reference because closing the voicemail notification
434 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
435 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee8d66d812014-11-24 14:54:02 -0800436 VoicemailNotificationSettingsUtil.setVibrationEnabled(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800437 mPhone, Boolean.TRUE.equals(objValue));
Andrew Leedf14ead2014-10-17 14:22:52 -0700438 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700439 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
440 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
441 } else {
442 AlertDialog.Builder builder = new AlertDialog.Builder(this);
443 DialogInterface.OnClickListener networkSettingsClickListener =
444 new Dialog.OnClickListener() {
445 @Override
446 public void onClick(DialogInterface dialog, int which) {
447 startActivity(new Intent(mPhone.getContext(),
448 com.android.phone.MobileNetworkSettings.class));
449 }
450 };
451 builder.setMessage(getResources().getString(
452 R.string.enable_video_calling_dialog_msg))
453 .setNeutralButton(getResources().getString(
454 R.string.enable_video_calling_dialog_settings),
455 networkSettingsClickListener)
456 .setPositiveButton(android.R.string.ok, null)
457 .show();
458 return false;
459 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700460 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800461
462 // Always let the preference setting proceed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700463 return true;
464 }
465
466 @Override
467 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800468 if (DBG) log("onDialogClosed: Button clicked is " + buttonClicked);
469
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700470 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
471 return;
472 }
473
474 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700475 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
476 mSubMenuVoicemailSettings.getPhoneNumber(),
477 VoicemailProviderSettings.NO_FORWARDING);
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800478 saveVoiceMailAndForwardingNumber(mVoicemailProviders.getKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700479 }
480 }
481
482 /**
483 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
484 * This method set the default values for the various
485 * EditPhoneNumberPreference dialogs.
486 */
487 @Override
488 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
489 if (preference == mSubMenuVoicemailSettings) {
490 // update the voicemail number field, which takes care of the
491 // mSubMenuVoicemailSettings itself, so we should return null.
492 if (DBG) log("updating default for voicemail dialog");
493 updateVoiceNumberField();
494 return null;
495 }
496
497 String vmDisplay = mPhone.getVoiceMailNumber();
498 if (TextUtils.isEmpty(vmDisplay)) {
499 // if there is no voicemail number, we just return null to
500 // indicate no contribution.
501 return null;
502 }
503
504 // Return the voicemail number prepended with "VM: "
505 if (DBG) log("updating default for call forwarding dialogs");
506 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
507 }
508
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700509 private void switchToPreviousVoicemailProvider() {
510 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700511
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800512 if (mPreviousVMProviderKey == null) {
513 return;
514 }
515
516 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
Andrew Lee84024342014-11-06 23:37:09 -0800517 showDialogIfForeground(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800518 final VoicemailProviderSettings prevSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800519 VoicemailProviderSettingsUtil.load(this, mPreviousVMProviderKey);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800520 if (prevSettings == null) {
521 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
522 + mPreviousVMProviderKey + "\" is null but should be loaded.");
523 }
524
525 if (mVMChangeCompletedSuccessfully) {
526 mNewVMNumber = prevSettings.getVoicemailNumber();
527 Log.i(LOG_TAG, "VM change is already completed successfully."
528 + "Have to revert VM back to " + mNewVMNumber + " again.");
529 mPhone.setVoiceMailNumber(
530 mPhone.getVoiceMailAlphaTag().toString(),
531 mNewVMNumber,
532 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
533 }
534
535 if (mFwdChangesRequireRollback) {
536 Log.i(LOG_TAG, "Requested to rollback forwarding changes.");
537
538 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
539 if (prevFwdSettings != null) {
540 Map<Integer, AsyncResult> results = mForwardingChangeResults;
541 resetForwardingChangeState();
542 for (int i = 0; i < prevFwdSettings.length; i++) {
543 CallForwardInfo fi = prevFwdSettings[i];
544 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
545 // Only revert the settings for which the update succeeded.
546 AsyncResult result = results.get(fi.reason);
547 if (result != null && result.exception == null) {
548 mExpectedChangeResultReasons.add(fi.reason);
549 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
550 mRevertOptionComplete.obtainMessage(
551 EVENT_FORWARDING_CHANGED, i, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700552 }
553 }
554 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700555 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800556 } else {
557 if (DBG) log("No need to revert");
558 onRevertDone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700559 }
560 }
561
562 private void onRevertDone() {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800563 if (DBG) log("onRevertDone: Changing provider key back to " + mPreviousVMProviderKey);
564
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700565 updateVMPreferenceWidgets(mPreviousVMProviderKey);
566 updateVoiceNumberField();
567 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800568 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700569 mVMOrFwdSetError = 0;
570 }
571 }
572
573 @Override
574 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
575 if (DBG) {
576 log("onActivityResult: requestCode: " + requestCode
577 + ", resultCode: " + resultCode
578 + ", data: " + data);
579 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800580
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700581 // there are cases where the contact picker may end up sending us more than one
582 // request. We want to ignore the request if we're not in the correct state.
583 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
584 boolean failure = false;
585
586 // No matter how the processing of result goes lets clear the flag
587 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
588 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
589 mVMProviderSettingsForced = false;
590
591 String vmNum = null;
592 if (resultCode != RESULT_OK) {
593 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
594 failure = true;
595 } else {
596 if (data == null) {
597 if (DBG) log("onActivityResult: vm provider cfg result has no data");
598 failure = true;
599 } else {
600 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
601 if (DBG) log("Provider requested signout");
602 if (isVMProviderSettingsForced) {
603 if (DBG) log("Going back to previous provider on signout");
604 switchToPreviousVoicemailProvider();
605 } else {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800606 final String victim = mVoicemailProviders.getKey();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700607 if (DBG) log("Relaunching activity and ignoring " + victim);
608 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
609 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
610 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
611 this.startActivity(i);
612 }
613 return;
614 }
615 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
616 if (vmNum == null || vmNum.length() == 0) {
617 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
618 failure = true;
619 }
620 }
621 }
622 if (failure) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800623 if (DBG) log("Failure in return from voicemail provider.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700624 if (isVMProviderSettingsForced) {
625 switchToPreviousVoicemailProvider();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700626 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800627
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700628 return;
629 }
630 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
631 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
632
Santos Cordonda120f42014-08-06 04:44:34 -0700633 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700634 // send it to the provider when it's config is invoked so it can use this as default
635 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
636
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800637 if (DBG) log("onActivityResult: cfg result has forwarding number " + fwdNum);
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800638 saveVoiceMailAndForwardingNumber(mVoicemailProviders.getKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700639 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700640 return;
641 }
642
643 if (requestCode == VOICEMAIL_PREF_ID) {
644 if (resultCode != RESULT_OK) {
645 if (DBG) log("onActivityResult: contact picker result not OK.");
646 return;
647 }
648
649 Cursor cursor = null;
650 try {
651 cursor = getContentResolver().query(data.getData(),
652 NUM_PROJECTION, null, null, null);
653 if ((cursor == null) || (!cursor.moveToFirst())) {
654 if (DBG) log("onActivityResult: bad contact data, no results found.");
655 return;
656 }
657 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
658 return;
659 } finally {
660 if (cursor != null) {
661 cursor.close();
662 }
663 }
664 }
665
666 super.onActivityResult(requestCode, resultCode, data);
667 }
668
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700669 /**
670 * Wrapper around showDialog() that will silently do nothing if we're
671 * not in the foreground.
672 *
673 * This is useful here because most of the dialogs we display from
674 * this class are triggered by asynchronous events (like
675 * success/failure messages from the telephony layer) and it's
676 * possible for those events to come in even after the user has gone
677 * to a different screen.
678 */
679 // TODO: this is too brittle: it's still easy to accidentally add new
680 // code here that calls showDialog() directly (which will result in a
681 // WindowManager$BadTokenException if called after the activity has
682 // been stopped.)
683 //
684 // It would be cleaner to do the "if (mForeground)" check in one
685 // central place, maybe by using a single Handler for all asynchronous
686 // events (and have *that* discard events if we're not in the
687 // foreground.)
688 //
689 // Unfortunately it's not that simple, since we sometimes need to do
690 // actual work to handle these events whether or not we're in the
691 // foreground (see the Handler code in mSetOptionComplete for
692 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800693 //
694 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
695 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700696 private void showDialogIfForeground(int id) {
697 if (mForeground) {
698 showDialog(id);
699 }
700 }
701
702 private void dismissDialogSafely(int id) {
703 try {
704 dismissDialog(id);
705 } catch (IllegalArgumentException e) {
706 // This is expected in the case where we were in the background
707 // at the time we would normally have shown the dialog, so we didn't
708 // show it.
709 }
710 }
711
Andrew Lee299e4a72015-01-15 12:44:34 -0800712 /**
713 * TODO: Refactor to make it easier to understand what's done in the different stages.
714 */
Andrew Leeb490d732014-10-27 15:00:41 -0700715 private void saveVoiceMailAndForwardingNumber(
716 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700717 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700718 mNewVMNumber = newSettings.getVoicemailNumber();
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800719 mNewVMNumber = (mNewVMNumber == null) ? "" : mNewVMNumber;
Andrew Leeb490d732014-10-27 15:00:41 -0700720 mNewFwdSettings = newSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700721
Andrew Lee299e4a72015-01-15 12:44:34 -0800722 // No fwd settings on CDMA.
723 boolean isCdma = mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA;
724 if (isCdma) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700725 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700726 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700727 }
728
Andrew Leee3c15212014-10-28 13:12:55 -0700729 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700730 if (mNewVMNumber.equals(mOldVmNumber)
731 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Lee84024342014-11-06 23:37:09 -0800732 showDialogIfForeground(VoicemailDialogUtil.VM_NOCHANGE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700733 return;
734 }
735
Andrew Lee6950c6c2014-11-26 12:05:42 -0800736 VoicemailProviderSettingsUtil.save(this, key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700737 mVMChangeCompletedSuccessfully = false;
738 mFwdChangesRequireRollback = false;
739 mVMOrFwdSetError = 0;
Andrew Lee299e4a72015-01-15 12:44:34 -0800740
741 // Don't read call forwarding settings if CDMA. Call forwarding is not supported by CDMA.
742 if (!key.equals(mPreviousVMProviderKey) && !isCdma) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700743 mReadingSettingsForDefaultProvider =
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800744 mPreviousVMProviderKey.equals(VoicemailProviderListPreference.DEFAULT_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700745 if (DBG) log("Reading current forwarding settings");
Andrew Leeb490d732014-10-27 15:00:41 -0700746 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
747 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
748 for (int i = 0; i < mForwardingReadResults.length; i++) {
749 mPhone.getCallForwardingOption(
750 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700751 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
752 }
Andrew Lee84024342014-11-06 23:37:09 -0800753 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700754 } else {
755 saveVoiceMailAndForwardingNumberStage2();
756 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800757
758 // Refresh the MWI indicator if it is already showing.
759 PhoneGlobals.getInstance().refreshMwiIndicator(mSubscriptionInfoHelper.getSubId());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700760 }
761
762 private final Handler mGetOptionComplete = new Handler() {
763 @Override
764 public void handleMessage(Message msg) {
765 AsyncResult result = (AsyncResult) msg.obj;
766 switch (msg.what) {
767 case EVENT_FORWARDING_GET_COMPLETED:
768 handleForwardingSettingsReadResult(result, msg.arg1);
769 break;
770 }
771 }
772 };
773
774 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
775 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800776
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700777 Throwable error = null;
778 if (ar.exception != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700779 error = ar.exception;
Andrew Lee1af6cf72014-11-04 17:35:26 -0800780 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700781 }
782 if (ar.userObj instanceof Throwable) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800783 error = (Throwable) ar.userObj;
784 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700785 }
786
787 // We may have already gotten an error and decided to ignore the other results.
788 if (mForwardingReadResults == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800789 if (DBG) Log.d(LOG_TAG, "Ignoring fwd reading result: " + idx);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700790 return;
791 }
792
793 // In case of error ignore other results, show an error dialog
794 if (error != null) {
795 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
796 mForwardingReadResults = null;
Andrew Lee84024342014-11-06 23:37:09 -0800797 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
798 showDialogIfForeground(VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700799 return;
800 }
801
Andrew Lee1af6cf72014-11-04 17:35:26 -0800802 // Get the forwarding info.
803 mForwardingReadResults[idx] = CallForwardInfoUtil.getCallForwardInfo(
804 (CallForwardInfo[]) ar.result,
805 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx]);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700806
807 // Check if we got all the results already
808 boolean done = true;
809 for (int i = 0; i < mForwardingReadResults.length; i++) {
810 if (mForwardingReadResults[i] == null) {
811 done = false;
812 break;
813 }
814 }
Andrew Lee1af6cf72014-11-04 17:35:26 -0800815
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700816 if (done) {
817 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
Andrew Lee84024342014-11-06 23:37:09 -0800818 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800819
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700820 if (mReadingSettingsForDefaultProvider) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800821 VoicemailProviderSettingsUtil.save(mPhone.getContext(),
822 VoicemailProviderListPreference.DEFAULT_KEY,
Andrew Lee299e4a72015-01-15 12:44:34 -0800823 new VoicemailProviderSettings(mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700824 mReadingSettingsForDefaultProvider = false;
825 }
826 saveVoiceMailAndForwardingNumberStage2();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700827 }
828 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800829
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700830 private void resetForwardingChangeState() {
831 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
832 mExpectedChangeResultReasons = new HashSet<Integer>();
833 }
834
Andrew Lee299e4a72015-01-15 12:44:34 -0800835 // Called after we are done saving the previous forwarding settings if we needed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700836 private void saveVoiceMailAndForwardingNumberStage2() {
837 mForwardingChangeResults = null;
838 mVoicemailChangeResult = null;
Andrew Leeb490d732014-10-27 15:00:41 -0700839 if (mNewFwdSettings != VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700840 resetForwardingChangeState();
841 for (int i = 0; i < mNewFwdSettings.length; i++) {
842 CallForwardInfo fi = mNewFwdSettings[i];
Andrew Lee1af6cf72014-11-04 17:35:26 -0800843 CallForwardInfo fiForReason =
844 CallForwardInfoUtil.infoForReason(mForwardingReadResults, fi.reason);
845 final boolean doUpdate = CallForwardInfoUtil.isUpdateRequired(fiForReason, fi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700846
847 if (doUpdate) {
848 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
849 mExpectedChangeResultReasons.add(i);
850
Andrew Lee1af6cf72014-11-04 17:35:26 -0800851 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700852 mSetOptionComplete.obtainMessage(
853 EVENT_FORWARDING_CHANGED, fi.reason, 0));
854 }
855 }
Andrew Lee84024342014-11-06 23:37:09 -0800856 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700857 } else {
858 if (DBG) log("Not touching fwd #");
859 setVMNumberWithCarrier();
860 }
861 }
862
863 private void setVMNumberWithCarrier() {
864 if (DBG) log("save voicemail #: " + mNewVMNumber);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800865
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700866 mPhone.setVoiceMailNumber(
867 mPhone.getVoiceMailAlphaTag().toString(),
868 mNewVMNumber,
869 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
870 }
871
872 /**
873 * Callback to handle option update completions
874 */
875 private final Handler mSetOptionComplete = new Handler() {
876 @Override
877 public void handleMessage(Message msg) {
878 AsyncResult result = (AsyncResult) msg.obj;
879 boolean done = false;
880 switch (msg.what) {
881 case EVENT_VOICEMAIL_CHANGED:
882 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700883 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700884 done = true;
885 break;
886 case EVENT_FORWARDING_CHANGED:
887 mForwardingChangeResults.put(msg.arg1, result);
888 if (result.exception != null) {
889 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
890 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700891 }
Andrew Leee438b312014-10-29 16:59:15 -0700892 if (isForwardingCompleted()) {
893 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700894 if (DBG) log("Overall fwd changes completed ok, starting vm change");
895 setVMNumberWithCarrier();
896 } else {
897 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
898 "Check if we need to try rollback for some settings.");
899 mFwdChangesRequireRollback = false;
900 Iterator<Map.Entry<Integer,AsyncResult>> it =
901 mForwardingChangeResults.entrySet().iterator();
902 while (it.hasNext()) {
903 Map.Entry<Integer,AsyncResult> entry = it.next();
904 if (entry.getValue().exception == null) {
905 // If at least one succeeded we have to revert
906 Log.i(LOG_TAG, "Rollback will be required");
907 mFwdChangesRequireRollback = true;
908 break;
909 }
910 }
911 if (!mFwdChangesRequireRollback) {
912 Log.i(LOG_TAG, "No rollback needed.");
913 }
914 done = true;
915 }
916 }
917 break;
918 default:
919 // TODO: should never reach this, may want to throw exception
920 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800921
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700922 if (done) {
923 if (DBG) log("All VM provider related changes done");
924 if (mForwardingChangeResults != null) {
Andrew Lee84024342014-11-06 23:37:09 -0800925 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700926 }
Andrew Leee438b312014-10-29 16:59:15 -0700927 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700928 }
929 }
930 };
931
932 /**
933 * Callback to handle option revert completions
934 */
935 private final Handler mRevertOptionComplete = new Handler() {
936 @Override
937 public void handleMessage(Message msg) {
938 AsyncResult result = (AsyncResult) msg.obj;
939 switch (msg.what) {
940 case EVENT_VOICEMAIL_CHANGED:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700941 if (DBG) log("VM revert complete msg");
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800942 mVoicemailChangeResult = result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700943 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800944
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700945 case EVENT_FORWARDING_CHANGED:
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800946 if (DBG) log("FWD revert complete msg ");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700947 mForwardingChangeResults.put(msg.arg1, result);
948 if (result.exception != null) {
949 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
950 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700951 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700952 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800953
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700954 default:
955 // TODO: should never reach this, may want to throw exception
956 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800957
958 final boolean done = (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null)
959 && (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700960 if (done) {
961 if (DBG) log("All VM reverts done");
Andrew Lee84024342014-11-06 23:37:09 -0800962 dismissDialogSafely(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700963 onRevertDone();
964 }
965 }
966 };
967
968 /**
Andrew Leee438b312014-10-29 16:59:15 -0700969 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700970 */
Andrew Leee438b312014-10-29 16:59:15 -0700971 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700972 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -0700973 return true;
974 }
975
976 for (Integer reason : mExpectedChangeResultReasons) {
977 if (mForwardingChangeResults.get(reason) == null) {
978 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700979 }
980 }
Andrew Leee438b312014-10-29 16:59:15 -0700981
982 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700983 }
Andrew Leee438b312014-10-29 16:59:15 -0700984
985 private boolean isFwdChangeSuccess() {
986 if (mForwardingChangeResults == null) {
987 return true;
988 }
989
990 for (AsyncResult result : mForwardingChangeResults.values()) {
991 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700992 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700993 String msg = exception.getMessage();
994 msg = (msg != null) ? msg : "";
995 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
996 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700997 }
998 }
Andrew Leee438b312014-10-29 16:59:15 -0700999 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001000 }
1001
Andrew Leee438b312014-10-29 16:59:15 -07001002 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001003 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -07001004 String msg = mVoicemailChangeResult.exception.getMessage();
1005 msg = (msg != null) ? msg : "";
1006 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
1007 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001008 }
Andrew Leee438b312014-10-29 16:59:15 -07001009 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001010 }
1011
Andrew Leee438b312014-10-29 16:59:15 -07001012 private void handleSetVmOrFwdMessage() {
1013 if (DBG) log("handleSetVMMessage: set VM request complete");
1014
1015 if (!isFwdChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -08001016 handleVmOrFwdSetError(VoicemailDialogUtil.FWD_SET_RESPONSE_ERROR_DIALOG);
Andrew Leee438b312014-10-29 16:59:15 -07001017 } else if (!isVmChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -08001018 handleVmOrFwdSetError(VoicemailDialogUtil.VM_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001019 } else {
Andrew Lee84024342014-11-06 23:37:09 -08001020 handleVmAndFwdSetSuccess(VoicemailDialogUtil.VM_CONFIRM_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001021 }
1022 }
1023
1024 /**
1025 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
1026 * changes to those settings and show "failure" dialog.
1027 *
Andrew Leeab082272014-11-04 15:50:42 -08001028 * @param dialogId ID of the dialog to show for the specific error case. Either
Andrew Lee84024342014-11-06 23:37:09 -08001029 * {@link #FWD_SET_RESPONSE_ERROR_DIALOG} or {@link #VM_RESPONSE_ERROR_DIALOG}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001030 */
Andrew Leeab082272014-11-04 15:50:42 -08001031 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001032 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -08001033 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001034 mChangingVMorFwdDueToProviderChange = false;
1035 switchToPreviousVoicemailProvider();
1036 return;
1037 }
1038 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001039 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001040 updateVoiceNumberField();
1041 }
1042
1043 /**
1044 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1045 * This updates a bunch of variables and show "success" dialog.
1046 */
Andrew Leeab082272014-11-04 15:50:42 -08001047 private void handleVmAndFwdSetSuccess(int dialogId) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001048 if (DBG) log("handleVmAndFwdSetSuccess: key is " + mVoicemailProviders.getKey());
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001049
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001050 mPreviousVMProviderKey = mVoicemailProviders.getKey();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001051 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001052 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001053 updateVoiceNumberField();
1054 }
1055
1056 /**
1057 * Update the voicemail number from what we've recorded on the sim.
1058 */
1059 private void updateVoiceNumberField() {
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001060 if (DBG) log("updateVoiceNumberField()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001061
1062 mOldVmNumber = mPhone.getVoiceMailNumber();
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001063 if (TextUtils.isEmpty(mOldVmNumber)) {
1064 mSubMenuVoicemailSettings.setPhoneNumber("");
1065 mSubMenuVoicemailSettings.setSummary(getString(R.string.voicemail_number_not_set));
1066 } else {
1067 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1068 mSubMenuVoicemailSettings.setSummary(mOldVmNumber);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001069 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001070 }
1071
1072 /*
1073 * Helper Methods for Activity class.
1074 * The initial query commands are split into two pieces now
1075 * for individual expansion. This combined with the ability
1076 * to cancel queries allows for a much better user experience,
1077 * and also ensures that the user only waits to update the
1078 * data that is relevant.
1079 */
1080
1081 @Override
1082 protected void onPrepareDialog(int id, Dialog dialog) {
1083 super.onPrepareDialog(id, dialog);
1084 mCurrentDialogId = id;
1085 }
1086
1087 // dialog creation method, called by showDialog()
1088 @Override
Andrew Lee84024342014-11-06 23:37:09 -08001089 protected Dialog onCreateDialog(int dialogId) {
1090 return VoicemailDialogUtil.getDialog(this, dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001091 }
1092
1093 // This is a method implemented for DialogInterface.OnClickListener.
1094 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1095 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1096 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1097 public void onClick(DialogInterface dialog, int which) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001098 if (DBG) log("onClick: button clicked is " + which);
1099
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001100 dialog.dismiss();
1101 switch (which){
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001102 case DialogInterface.BUTTON_NEGATIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001103 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001104 // We failed to get current forwarding settings and the user
1105 // does not wish to continue.
1106 switchToPreviousVoicemailProvider();
1107 }
1108 break;
1109 case DialogInterface.BUTTON_POSITIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001110 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001111 // We failed to get current forwarding settings but the user
1112 // wishes to continue changing settings to the new vm provider
1113 saveVoiceMailAndForwardingNumberStage2();
1114 } else {
1115 finish();
1116 }
1117 return;
1118 default:
1119 // just let the dialog close and go back to the input
1120 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001121
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001122 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1123 // with settings UI. If we were called to explicitly configure voice mail then
1124 // we finish the settings activity here to come back to whatever the user was doing.
1125 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1126 finish();
1127 }
1128 }
1129
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001130 /*
1131 * Activity class methods
1132 */
1133
1134 @Override
1135 protected void onCreate(Bundle icicle) {
1136 super.onCreate(icicle);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001137 if (DBG) log("onCreate: Intent is " + getIntent());
1138
Jay Shraunerc2684732014-11-12 12:10:37 -08001139 // Make sure we are running as the primary user.
1140 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
1141 Toast.makeText(this, R.string.call_settings_primary_user_only,
1142 Toast.LENGTH_SHORT).show();
1143 finish();
1144 return;
1145 }
1146
Tyler Gunnbaee2952014-09-10 16:01:02 -07001147 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
1148
Tyler Gunnbaee2952014-09-10 16:01:02 -07001149 // Show the voicemail preference in onResume if the calling intent specifies the
1150 // ACTION_ADD_VOICEMAIL action.
1151 mShowVoicemailPreference = (icicle == null) &&
Jay Shraunerbe2fb262014-11-11 15:19:58 -08001152 TextUtils.equals(getIntent().getAction(), ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -08001153 mSetupVoicemail = mShowVoicemailPreference &&
1154 getIntent().getBooleanExtra(SETUP_VOICEMAIL_EXTRA, false);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001155
Andrew Leedd4f6df2014-12-09 19:13:51 -08001156 mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent());
Andrew Lee5ed870c2014-10-29 11:47:49 -07001157 mSubscriptionInfoHelper.setActionBarTitle(
1158 getActionBar(), getResources(), R.string.call_settings_with_label);
Andrew Lee1479dd12014-11-06 23:06:32 -08001159 mPhone = mSubscriptionInfoHelper.getPhone();
Tyler Gunnbaee2952014-09-10 16:01:02 -07001160 }
1161
Tyler Gunnbaee2952014-09-10 16:01:02 -07001162 @Override
1163 protected void onResume() {
1164 super.onResume();
1165 mForeground = true;
1166
1167 PreferenceScreen preferenceScreen = getPreferenceScreen();
1168 if (preferenceScreen != null) {
1169 preferenceScreen.removeAll();
1170 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001171
1172 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001173
Andrew Lee6fee2cb2014-12-09 16:42:18 -08001174 TelecomManager telecomManager = TelecomManager.from(this);
1175 TelephonyManager telephonyManager =
1176 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
1177
1178 Preference phoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
1179 if (telephonyManager.isMultiSimEnabled() || (telecomManager.getSimCallManagers().isEmpty()
1180 && !SipUtil.isVoipSupported(mPhone.getContext()))) {
1181 getPreferenceScreen().removePreference(phoneAccountSettingsPreference);
1182 }
Andrew Leedb2fe562014-09-03 15:40:43 -07001183
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001184 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001185 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1186 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1187 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1188 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001189
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001190 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1191 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1192 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
Andrew Lee6fee2cb2014-12-09 16:42:18 -08001193 mButtonTTY = (TtyModeListPreference) findPreference(
1194 getResources().getString(R.string.tty_mode_key));
Andrew Lee2170a972014-08-13 18:13:01 -07001195
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001196 mVoicemailProviders = (VoicemailProviderListPreference) findPreference(
1197 BUTTON_VOICEMAIL_PROVIDER_KEY);
1198 mVoicemailProviders.init(mPhone, getIntent());
Andrew Lee2c027892014-10-29 11:29:54 -07001199 mVoicemailProviders.setOnPreferenceChangeListener(this);
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001200 mPreviousVMProviderKey = mVoicemailProviders.getValue();
Andrew Lee8d66d812014-11-24 14:54:02 -08001201
Andrew Lee2c027892014-10-29 11:29:54 -07001202 mVoicemailSettingsScreen =
1203 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001204 mVoicemailSettings = (PreferenceScreen) findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
Andrew Lee8d66d812014-11-24 14:54:02 -08001205
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001206 mVoicemailNotificationRingtone = (VoicemailRingtonePreference) findPreference(
1207 getResources().getString(R.string.voicemail_notification_ringtone_key));
1208 mVoicemailNotificationRingtone.init(mPhone);
1209
Andrew Lee8d66d812014-11-24 14:54:02 -08001210 mVoicemailNotificationVibrate = (CheckBoxPreference) findPreference(
1211 getResources().getString(R.string.voicemail_notification_vibrate_key));
1212 mVoicemailNotificationVibrate.setOnPreferenceChangeListener(this);
1213
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001214 updateVMPreferenceWidgets(mVoicemailProviders.getValue());
1215
1216 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001217
Andrew Lee64a7d792014-10-15 17:38:38 -07001218 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1219 mButtonDTMF.setOnPreferenceChangeListener(this);
1220 int dtmf = Settings.System.getInt(getContentResolver(),
1221 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1222 mButtonDTMF.setValueIndex(dtmf);
1223 } else {
1224 prefSet.removePreference(mButtonDTMF);
1225 mButtonDTMF = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001226 }
1227
Andrew Lee64a7d792014-10-15 17:38:38 -07001228 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1229 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1230 int autoretry = Settings.Global.getInt(
1231 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1232 mButtonAutoRetry.setChecked(autoretry != 0);
1233 } else {
1234 prefSet.removePreference(mButtonAutoRetry);
1235 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001236 }
1237
Andrew Lee64a7d792014-10-15 17:38:38 -07001238 if (getResources().getBoolean(R.bool.hac_enabled)) {
1239 mButtonHAC.setOnPreferenceChangeListener(this);
1240 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1241 mButtonHAC.setChecked(hac != 0);
1242 } else {
1243 prefSet.removePreference(mButtonHAC);
1244 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001245 }
1246
Andrew Lee6fee2cb2014-12-09 16:42:18 -08001247 if (!telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported()) {
1248 mButtonTTY.init();
Andrew Lee64a7d792014-10-15 17:38:38 -07001249 } else {
1250 prefSet.removePreference(mButtonTTY);
1251 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001252 }
1253
1254 if (!getResources().getBoolean(R.bool.world_phone)) {
Andrew Lee2b36ba22014-11-05 17:08:49 -08001255 Preference cdmaOptions = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
1256 prefSet.removePreference(cdmaOptions);
1257
1258 // TODO: Support MSIM for this preference option.
1259 Preference gsmOptions = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
1260 prefSet.removePreference(gsmOptions);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001261
1262 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001263 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Junda Liua09d6962014-11-12 13:39:02 -08001264 boolean shouldHideCarrierSettings = Settings.Global.getInt(
1265 getContentResolver(), Settings.Global.HIDE_CARRIER_NETWORK_SETTINGS, 0) == 1;
1266 if (shouldHideCarrierSettings) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001267 prefSet.removePreference(fdnButton);
Junda Liua09d6962014-11-12 13:39:02 -08001268 if (mButtonDTMF != null) {
1269 prefSet.removePreference(mButtonDTMF);
Etan Cohen0ca1c802014-07-07 15:35:48 -07001270 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001271 } else {
Junda Liua09d6962014-11-12 13:39:02 -08001272 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1273 prefSet.removePreference(fdnButton);
1274
1275 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1276 addPreferencesFromResource(R.xml.cdma_call_privacy);
1277 }
1278 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
Andrew Leedd4f6df2014-12-09 19:13:51 -08001279 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(FdnSetting.class));
Junda Liua09d6962014-11-12 13:39:02 -08001280
1281 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
1282 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1283
1284 Preference callForwardingPref = prefSet.findPreference(CALL_FORWARDING_KEY);
1285 callForwardingPref.setIntent(mSubscriptionInfoHelper.getIntent(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001286 GsmUmtsCallForwardOptions.class));
Junda Liua09d6962014-11-12 13:39:02 -08001287
1288 Preference additionalGsmSettingsPref =
1289 prefSet.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
1290 additionalGsmSettingsPref.setIntent(mSubscriptionInfoHelper.getIntent(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001291 GsmUmtsAdditionalCallOptions.class));
Junda Liua09d6962014-11-12 13:39:02 -08001292 }
1293 } else {
1294 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1295 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001296 }
1297 }
1298
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001299 // check the intent that started this activity and pop up the voicemail
1300 // dialog if we've been asked to.
1301 // If we have at least one non default VM provider registered then bring up
1302 // the selection for the VM provider, otherwise bring up a VM number dialog.
1303 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001304 if (mShowVoicemailPreference) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001305 if (DBG) log("ACTION_ADD_VOICEMAIL Intent is thrown");
Tyler Gunn9c1071f2014-12-09 10:07:54 -08001306 if (mSetupVoicemail) {
1307 simulatePreferenceClick(mVoicemailSettingsScreen);
1308 mSetupVoicemail = false;
1309 } else if (mVoicemailProviders.hasMoreThanOneVoicemailProvider()) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001310 if (DBG) log("Voicemail data has more than one provider.");
Tyler Gunnbaee2952014-09-10 16:01:02 -07001311 simulatePreferenceClick(mVoicemailProviders);
1312 } else {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001313 onPreferenceChange(mVoicemailProviders, VoicemailProviderListPreference.DEFAULT_KEY);
1314 mVoicemailProviders.setValue(VoicemailProviderListPreference.DEFAULT_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001315 }
1316 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001317 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001318
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001319 updateVoiceNumberField();
1320 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001321
Andrew Lee8d66d812014-11-24 14:54:02 -08001322 mVoicemailNotificationVibrate.setChecked(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001323 VoicemailNotificationSettingsUtil.isVibrationEnabled(mPhone));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001324
Andrew Lee312e8172014-10-23 17:01:36 -07001325 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1326 boolean currentValue =
1327 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1328 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1329 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001330 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1331 } else {
1332 prefSet.removePreference(mEnableVideoCalling);
1333 }
Etan Cohena51d4ef2014-12-15 10:44:42 -08001334
1335 if (ImsManager.isVolteEnabledByPlatform(this) &&
1336 !mPhone.getContext().getResources().getBoolean(
1337 com.android.internal.R.bool.config_carrier_volte_tty_supported)) {
1338 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
1339 tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
1340 }
Andrew Leeb97fad82015-01-27 15:31:39 -08001341
Andrew Leeeffef9c2015-02-19 16:02:37 -08001342 Preference wifiCallingSettings = findPreference(
1343 getResources().getString(R.string.wifi_calling_settings_key));
Andrew Leeb97fad82015-01-27 15:31:39 -08001344 if (!ImsManager.isWfcEnabledByPlatform(mPhone.getContext())) {
Andrew Leeb97fad82015-01-27 15:31:39 -08001345 prefSet.removePreference(wifiCallingSettings);
Andrew Leeeffef9c2015-02-19 16:02:37 -08001346 } else {
1347 int resId = R.string.wifi_calling_off_summary;
1348 if (ImsManager.isWfcEnabledByUser(mPhone.getContext())) {
1349 int wfcMode = ImsManager.getWfcMode(mPhone.getContext());
1350 switch (wfcMode) {
1351 case ImsConfig.WfcModeFeatureValueConstants.WIFI_ONLY:
1352 resId = R.string.wfc_mode_wifi_only_summary;
1353 break;
1354 case ImsConfig.WfcModeFeatureValueConstants.CELLULAR_PREFERRED:
1355 resId = R.string.wfc_mode_cellular_preferred_summary;
1356 break;
1357 case ImsConfig.WfcModeFeatureValueConstants.WIFI_PREFERRED:
1358 resId = R.string.wfc_mode_wifi_preferred_summary;
1359 break;
1360 default:
1361 if (DBG) log("Unexpected WFC mode value: " + wfcMode);
1362 }
1363 }
1364 wifiCallingSettings.setSummary(resId);
Andrew Leeb97fad82015-01-27 15:31:39 -08001365 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001366 }
1367
Andrew Lee785ca2a2014-12-18 12:46:41 -08001368 @Override
1369 protected void onNewIntent(Intent newIntent) {
1370 setIntent(newIntent);
1371
1372 mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent());
1373 mSubscriptionInfoHelper.setActionBarTitle(
1374 getActionBar(), getResources(), R.string.call_settings_with_label);
1375 mPhone = mSubscriptionInfoHelper.getPhone();
1376 }
1377
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001378 private static void log(String msg) {
1379 Log.d(LOG_TAG, msg);
1380 }
1381
1382 /**
1383 * Updates the look of the VM preference widgets based on current VM provider settings.
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001384 * Note that the provider name is loaded fxrorm the found activity via loadLabel in
1385 * {@link VoicemailProviderListPreference#initVoiceMailProviders()} in order for it to be
1386 * localizable.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001387 */
1388 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1389 final String key = currentProviderSetting;
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001390 final VoicemailProvider provider = mVoicemailProviders.getVoicemailProvider(key);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001391
1392 /* This is the case when we are coming up on a freshly wiped phone and there is no
1393 persisted value for the list preference mVoicemailProviders.
1394 In this case we want to show the UI asking the user to select a voicemail provider as
1395 opposed to silently falling back to default one. */
1396 if (provider == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001397 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> null.");
1398
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001399 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1400 mVoicemailSettings.setEnabled(false);
1401 mVoicemailSettings.setIntent(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001402 mVoicemailNotificationVibrate.setEnabled(false);
1403 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001404 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> " + provider.toString());
1405
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001406 final String providerName = provider.name;
1407 mVoicemailProviders.setSummary(providerName);
1408 mVoicemailSettings.setEnabled(true);
1409 mVoicemailSettings.setIntent(provider.intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001410 mVoicemailNotificationVibrate.setEnabled(true);
1411 }
1412 }
1413
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001414
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001415 /**
1416 * Simulates user clicking on a passed preference.
1417 * Usually needed when the preference is a dialog preference and we want to invoke
1418 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001419 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001420 */
1421 private void simulatePreferenceClick(Preference preference) {
1422 // Go through settings until we find our setting
1423 // and then simulate a click on it to bring up the dialog
1424 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1425 for (int idx = 0; idx < adapter.getCount(); idx++) {
1426 if (adapter.getItem(idx) == preference) {
1427 getPreferenceScreen().onItemClick(this.getListView(),
1428 null, idx, adapter.getItemId(idx));
1429 break;
1430 }
1431 }
1432 }
1433
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001434 @Override
1435 public boolean onOptionsItemSelected(MenuItem item) {
1436 final int itemId = item.getItemId();
1437 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001438 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001439 return true;
1440 }
1441 return super.onOptionsItemSelected(item);
1442 }
Andrew Lee8d66d812014-11-24 14:54:02 -08001443
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001444 /**
1445 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1446 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1447 */
Andrew Lee5efb1122014-12-05 04:20:42 -08001448 public static void goUpToTopLevelSetting(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001449 Activity activity, SubscriptionInfoHelper subscriptionInfoHelper) {
1450 Intent intent = subscriptionInfoHelper.getIntent(CallFeaturesSetting.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001451 intent.setAction(Intent.ACTION_MAIN);
1452 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1453 activity.startActivity(intent);
1454 activity.finish();
1455 }
1456}