blob: 1d054a61dea01b59a6a82af6ca5f1aae655f95c6 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
19import android.app.ActionBar;
20import android.app.Activity;
Evan Charlton1c696832014-04-15 14:24:23 -070021import android.app.ActivityOptions;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.AlertDialog;
23import android.app.Dialog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.ContentResolver;
25import android.content.Context;
26import android.content.DialogInterface;
27import android.content.Intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.database.Cursor;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.media.AudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070030import android.os.AsyncResult;
31import android.os.Bundle;
32import android.os.Handler;
33import android.os.Message;
34import android.os.UserHandle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.preference.CheckBoxPreference;
36import android.preference.ListPreference;
37import android.preference.Preference;
38import android.preference.PreferenceActivity;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.preference.PreferenceScreen;
40import android.provider.ContactsContract.CommonDataKinds;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070042import android.telecom.PhoneAccountHandle;
43import android.telecom.TelecomManager;
Andrew Lee93c345f2014-10-27 15:25:07 -070044import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.text.TextUtils;
46import android.util.Log;
47import android.view.MenuItem;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.widget.ListAdapter;
Jay Shraunerc2684732014-11-12 12:10:37 -080049import android.widget.Toast;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050
Andrew Lee312e8172014-10-23 17:01:36 -070051import com.android.ims.ImsManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import com.android.internal.telephony.CallForwardInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import com.android.internal.telephony.Phone;
54import com.android.internal.telephony.PhoneConstants;
Andrew Lee2170a972014-08-13 18:13:01 -070055import com.android.phone.common.util.SettingsUtil;
Andrew Leedb2fe562014-09-03 15:40:43 -070056import com.android.phone.settings.AccountSelectionPreference;
Andrew Lee1af6cf72014-11-04 17:35:26 -080057import com.android.phone.settings.CallForwardInfoUtil;
Andrew Lee84024342014-11-06 23:37:09 -080058import com.android.phone.settings.VoicemailDialogUtil;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -080059import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Andrew Lee64e5c9d2014-11-26 12:00:10 -080060import com.android.phone.settings.VoicemailProviderListPreference;
61import com.android.phone.settings.VoicemailProviderListPreference.VoicemailProvider;
Andrew Leeb490d732014-10-27 15:00:41 -070062import com.android.phone.settings.VoicemailProviderSettings;
Andrew Lee88b51e22014-10-29 15:48:51 -070063import com.android.phone.settings.VoicemailProviderSettingsUtil;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -080064import com.android.phone.settings.VoicemailRingtonePreference;
Andrew Lee5ed870c2014-10-29 11:47:49 -070065import com.android.phone.settings.fdn.FdnSetting;
Sailesh Nepal788959e2014-07-08 23:36:40 -070066import com.android.services.telephony.sip.SipUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067
Andrew Lee2170a972014-08-13 18:13:01 -070068import java.lang.String;
Andrew Leef1776d82014-11-04 14:45:02 -080069import java.util.ArrayList;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import java.util.Collection;
71import java.util.HashMap;
72import java.util.HashSet;
73import java.util.Iterator;
74import java.util.List;
75import java.util.Map;
76
77/**
78 * Top level "Call settings" UI; see res/xml/call_feature_setting.xml
79 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070080 * This preference screen is the root of the "Call settings" hierarchy available from the Phone
81 * app; the settings here let you control various features related to phone calls (including
82 * voicemail settings, the "Respond via SMS" feature, and others.) It's used only on
83 * voice-capable phone devices.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084 *
85 * Note that this activity is part of the package com.android.phone, even
86 * though you reach it from the "Phone" app (i.e. DialtactsActivity) which
87 * is from the package com.android.contacts.
88 *
89 * For the "Mobile network settings" screen under the main Settings app,
90 * See {@link MobileNetworkSettings}.
91 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070092 * TODO: Settings should be split into PreferenceFragments where possible (ie. voicemail).
Andrew Lee2170a972014-08-13 18:13:01 -070093 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094 * @see com.android.phone.MobileNetworkSettings
95 */
96public class CallFeaturesSetting extends PreferenceActivity
97 implements DialogInterface.OnClickListener,
Andrew Lee2170a972014-08-13 18:13:01 -070098 Preference.OnPreferenceChangeListener,
Andrew Lee2170a972014-08-13 18:13:01 -070099 EditPhoneNumberPreference.OnDialogClosedListener,
Andrew Leece8ae2a2014-09-10 10:41:48 -0700100 EditPhoneNumberPreference.GetDefaultNumberListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700101 private static final String LOG_TAG = "CallFeaturesSetting";
102 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
Andrew Lee77527ac2014-10-21 16:57:39 -0700103 // STOPSHIP if true. Flag to override behavior default behavior to hide VT setting.
104 private static final boolean ENABLE_VT_FLAG = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700105
106 /**
107 * Intent action to bring up Voicemail Provider settings.
108 *
109 * @see #IGNORE_PROVIDER_EXTRA
110 */
111 public static final String ACTION_ADD_VOICEMAIL =
112 "com.android.phone.CallFeaturesSetting.ADD_VOICEMAIL";
113 // intent action sent by this activity to a voice mail provider
114 // to trigger its configuration UI
115 public static final String ACTION_CONFIGURE_VOICEMAIL =
116 "com.android.phone.CallFeaturesSetting.CONFIGURE_VOICEMAIL";
117 // Extra put in the return from VM provider config containing voicemail number to set
118 public static final String VM_NUMBER_EXTRA = "com.android.phone.VoicemailNumber";
119 // Extra put in the return from VM provider config containing call forwarding number to set
120 public static final String FWD_NUMBER_EXTRA = "com.android.phone.ForwardingNumber";
121 // Extra put in the return from VM provider config containing call forwarding number to set
122 public static final String FWD_NUMBER_TIME_EXTRA = "com.android.phone.ForwardingNumberTime";
123 // If the VM provider returns non null value in this extra we will force the user to
124 // choose another VM provider
125 public static final String SIGNOUT_EXTRA = "com.android.phone.Signout";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700126
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127 /**
128 * String Extra put into ACTION_ADD_VOICEMAIL call to indicate which provider should be hidden
129 * in the list of providers presented to the user. This allows a provider which is being
130 * disabled (e.g. GV user logging out) to force the user to pick some other provider.
131 */
132 public static final String IGNORE_PROVIDER_EXTRA = "com.android.phone.ProviderToIgnore";
133
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800134 /**
135 * String Extra put into ACTION_ADD_VOICEMAIL to indicate that the voicemail setup screen should
136 * be opened.
137 */
138 public static final String SETUP_VOICEMAIL_EXTRA = "com.android.phone.SetupVoicemail";
139
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700140 // string constants
141 private static final String NUM_PROJECTION[] = {CommonDataKinds.Phone.NUMBER};
142
143 // String keys for preference lookup
144 // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
Andrew Lee8d66d812014-11-24 14:54:02 -0800145 // TODO: Consider moving these strings to strings.xml, so that they are not duplicated here and
146 // in the layout files. These strings need to be treated carefully; if the setting is
147 // persistent, they are used as the key to store shared preferences and the name should not be
148 // changed unless the settings are also migrated.
Andrew Lee97708a42014-09-25 12:39:07 -0700149 private static final String VOICEMAIL_SETTING_SCREEN_PREF_KEY = "button_voicemail_category_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150 private static final String BUTTON_VOICEMAIL_KEY = "button_voicemail_key";
151 private static final String BUTTON_VOICEMAIL_PROVIDER_KEY = "button_voicemail_provider_key";
152 private static final String BUTTON_VOICEMAIL_SETTING_KEY = "button_voicemail_setting_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700153 private static final String BUTTON_FDN_KEY = "button_fdn_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700154
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700155 private static final String BUTTON_DTMF_KEY = "button_dtmf_settings";
156 private static final String BUTTON_RETRY_KEY = "button_auto_retry_key";
157 private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
158 private static final String BUTTON_HAC_KEY = "button_hac_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700159
160 private static final String BUTTON_GSM_UMTS_OPTIONS = "button_gsm_more_expand_key";
161 private static final String BUTTON_CDMA_OPTIONS = "button_cdma_more_expand_key";
Andrew Lee2b36ba22014-11-05 17:08:49 -0800162 private static final String CALL_FORWARDING_KEY = "call_forwarding_key";
163 private static final String ADDITIONAL_GSM_SETTINGS_KEY = "additional_gsm_call_settings_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700164
Andrew Leece8ae2a2014-09-10 10:41:48 -0700165 private static final String PHONE_ACCOUNT_SETTINGS_KEY =
166 "phone_account_settings_preference_screen";
Andrew Leedb2fe562014-09-03 15:40:43 -0700167
Andrew Leedf14ead2014-10-17 14:22:52 -0700168 private static final String ENABLE_VIDEO_CALLING_KEY = "button_enable_video_calling";
169
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700170 /** Event for Async voicemail change call */
171 private static final int EVENT_VOICEMAIL_CHANGED = 500;
172 private static final int EVENT_FORWARDING_CHANGED = 501;
173 private static final int EVENT_FORWARDING_GET_COMPLETED = 502;
174
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700175 public static final String HAC_KEY = "HACSetting";
176 public static final String HAC_VAL_ON = "ON";
177 public static final String HAC_VAL_OFF = "OFF";
178
179 /** Handle to voicemail pref */
180 private static final int VOICEMAIL_PREF_ID = 1;
181 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
182
183 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700184 private AudioManager mAudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185
Andrew Lee5ed870c2014-10-29 11:47:49 -0700186 private SubscriptionInfoHelper mSubscriptionInfoHelper;
187
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
189
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700190 /** Whether dialpad plays DTMF tone or not. */
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700191 private CheckBoxPreference mButtonAutoRetry;
192 private CheckBoxPreference mButtonHAC;
193 private ListPreference mButtonDTMF;
194 private ListPreference mButtonTTY;
Andrew Leece8ae2a2014-09-10 10:41:48 -0700195 private Preference mPhoneAccountSettingsPreference;
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800196 private VoicemailProviderListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700197 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198 private PreferenceScreen mVoicemailSettings;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800199 private VoicemailRingtonePreference mVoicemailNotificationRingtone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700201 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700202
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700203 /**
204 * Results of reading forwarding settings
205 */
206 private CallForwardInfo[] mForwardingReadResults = null;
207
208 /**
209 * Result of forwarding number change.
210 * Keys are reasons (eg. unconditional forwarding).
211 */
212 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
213
214 /**
215 * Expected CF read result types.
216 * This set keeps track of the CF types for which we've issued change
217 * commands so we can tell when we've received all of the responses.
218 */
219 private Collection<Integer> mExpectedChangeResultReasons = null;
220
221 /**
222 * Result of vm number change
223 */
224 private AsyncResult mVoicemailChangeResult = null;
225
226 /**
227 * Previous VM provider setting so we can return to it in case of failure.
228 */
229 private String mPreviousVMProviderKey = null;
230
231 /**
232 * Id of the dialog being currently shown.
233 */
234 private int mCurrentDialogId = 0;
235
236 /**
237 * Flag indicating that we are invoking settings for the voicemail provider programmatically
238 * due to vm provider change.
239 */
240 private boolean mVMProviderSettingsForced = false;
241
242 /**
243 * Flag indicating that we are making changes to vm or fwd numbers
244 * due to vm provider change.
245 */
246 private boolean mChangingVMorFwdDueToProviderChange = false;
247
248 /**
249 * True if we are in the process of vm & fwd number change and vm has already been changed.
250 * This is used to decide what to do in case of rollback.
251 */
252 private boolean mVMChangeCompletedSuccessfully = false;
253
254 /**
255 * True if we had full or partial failure setting forwarding numbers and so need to roll them
256 * back.
257 */
258 private boolean mFwdChangesRequireRollback = false;
259
260 /**
261 * Id of error msg to display to user once we are done reverting the VM provider to the previous
262 * one.
263 */
264 private int mVMOrFwdSetError = 0;
265
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700266 /** string to hold old voicemail number as it is being updated. */
267 private String mOldVmNumber;
268
269 // New call forwarding settings and vm number we will be setting
270 // Need to save these since before we get to saving we need to asynchronously
271 // query the existing forwarding settings.
272 private CallForwardInfo[] mNewFwdSettings;
273 private String mNewVMNumber;
274
275 private boolean mForeground;
276
277 @Override
278 public void onPause() {
279 super.onPause();
280 mForeground = false;
281 }
282
283 /**
284 * We have to pull current settings from the network for all kinds of
285 * voicemail providers so we can tell whether we have to update them,
286 * so use this bit to keep track of whether we're reading settings for the
287 * default provider and should therefore save them out when done.
288 */
289 private boolean mReadingSettingsForDefaultProvider = false;
290
Tyler Gunnbaee2952014-09-10 16:01:02 -0700291 /**
292 * Used to indicate that the voicemail preference should be shown.
293 */
294 private boolean mShowVoicemailPreference = false;
295
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800296 /**
297 * Used to indicate that the voicemail setup screen should be shown.
298 */
299 private boolean mSetupVoicemail = false;
300
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301 /*
302 * Click Listeners, handle click based on objects attached to UI.
303 */
304
305 // Click listener for all toggle events
306 @Override
307 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
308 if (preference == mSubMenuVoicemailSettings) {
309 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700310 } else if (preference == mButtonDTMF) {
311 return true;
312 } else if (preference == mButtonTTY) {
313 return true;
314 } else if (preference == mButtonAutoRetry) {
315 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
316 android.provider.Settings.Global.CALL_AUTO_RETRY,
317 mButtonAutoRetry.isChecked() ? 1 : 0);
318 return true;
319 } else if (preference == mButtonHAC) {
320 int hac = mButtonHAC.isChecked() ? 1 : 0;
321 // Update HAC value in Settings database
322 Settings.System.putInt(mPhone.getContext().getContentResolver(),
323 Settings.System.HEARING_AID, hac);
324
325 // Update HAC Value in AudioManager
326 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
327 return true;
Andrew Lee696bff52014-12-03 17:58:18 -0800328 } else if (preference.getKey().equals(mVoicemailSettings.getKey())) {
329 // Check key instead of comparing reference because closing the voicemail notification
330 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
331 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800332 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
333
Andrew Lee696bff52014-12-03 17:58:18 -0800334 final Dialog dialog = ((PreferenceScreen) preference).getDialog();
Yorke Leea0f63bf2014-10-09 18:27:20 -0700335 if (dialog != null) {
336 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
337 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800338
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700339 if (preference.getIntent() != null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800340 if (DBG) log("Invoking cfg intent " + preference.getIntent().getPackage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700341
342 // onActivityResult() will be responsible for resetting some of variables.
343 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
344 return true;
345 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800346 if (DBG) log("onPreferenceTreeClick(). No intent; use default behavior in xml.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700347
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800348 // onActivityResult() will not be called, so reset variables here.
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800349 mPreviousVMProviderKey = VoicemailProviderListPreference.DEFAULT_KEY;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700350 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700351 return false;
352 }
Andrew Lee97708a42014-09-25 12:39:07 -0700353 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700354 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
355 if (dialog != null) {
356 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
357 }
Andrew Lee97708a42014-09-25 12:39:07 -0700358 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700359 }
360 return false;
361 }
362
363 /**
364 * Implemented to support onPreferenceChangeListener to look for preference
365 * changes.
366 *
367 * @param preference is the preference to be changed
368 * @param objValue should be the value of the selection, NOT its localized
369 * display value.
370 */
371 @Override
372 public boolean onPreferenceChange(Preference preference, Object objValue) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800373 if (DBG) log("onPreferenceChange: \"" + preference + "\" changed to \"" + objValue + "\"");
Andrew Lee2170a972014-08-13 18:13:01 -0700374
375 if (preference == mButtonDTMF) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700376 int index = mButtonDTMF.findIndexOfValue((String) objValue);
377 Settings.System.putInt(mPhone.getContext().getContentResolver(),
378 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
379 } else if (preference == mButtonTTY) {
380 handleTTYChange(preference, objValue);
381 } else if (preference == mVoicemailProviders) {
382 final String newProviderKey = (String) objValue;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800383
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700384 // If previous provider key and the new one is same, we don't need to handle it.
385 if (mPreviousVMProviderKey.equals(newProviderKey)) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800386 if (DBG) log("No change is made to the VM provider setting.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700387 return true;
388 }
389 updateVMPreferenceWidgets(newProviderKey);
390
Andrew Leeb490d732014-10-27 15:00:41 -0700391 final VoicemailProviderSettings newProviderSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800392 VoicemailProviderSettingsUtil.load(this, newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700393
Andrew Lee88b51e22014-10-29 15:48:51 -0700394 // If the user switches to a voice mail provider and we have numbers stored for it we
395 // will automatically change the phone's voice mail and forwarding number to the stored
396 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700397 if (newProviderSettings == null) {
398 // Force the user into a configuration of the chosen provider
399 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
400 mVMProviderSettingsForced = true;
401 simulatePreferenceClick(mVoicemailSettings);
402 } else {
403 if (DBG) log("Saved preferences found - switching to them");
404 // Set this flag so if we get a failure we revert to previous provider
405 mChangingVMorFwdDueToProviderChange = true;
406 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
407 }
Andrew Lee8d66d812014-11-24 14:54:02 -0800408 } else if (preference.getKey().equals(mVoicemailNotificationVibrate.getKey())) {
Andrew Lee696bff52014-12-03 17:58:18 -0800409 // Check key instead of comparing reference because closing the voicemail notification
410 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
411 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee8d66d812014-11-24 14:54:02 -0800412 VoicemailNotificationSettingsUtil.setVibrationEnabled(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800413 mPhone, Boolean.TRUE.equals(objValue));
Andrew Leedf14ead2014-10-17 14:22:52 -0700414 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700415 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
416 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
417 } else {
418 AlertDialog.Builder builder = new AlertDialog.Builder(this);
419 DialogInterface.OnClickListener networkSettingsClickListener =
420 new Dialog.OnClickListener() {
421 @Override
422 public void onClick(DialogInterface dialog, int which) {
423 startActivity(new Intent(mPhone.getContext(),
424 com.android.phone.MobileNetworkSettings.class));
425 }
426 };
427 builder.setMessage(getResources().getString(
428 R.string.enable_video_calling_dialog_msg))
429 .setNeutralButton(getResources().getString(
430 R.string.enable_video_calling_dialog_settings),
431 networkSettingsClickListener)
432 .setPositiveButton(android.R.string.ok, null)
433 .show();
434 return false;
435 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700436 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800437
438 // Always let the preference setting proceed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700439 return true;
440 }
441
442 @Override
443 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800444 if (DBG) log("onDialogClosed: Button clicked is " + buttonClicked);
445
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700446 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
447 return;
448 }
449
450 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700451 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
452 mSubMenuVoicemailSettings.getPhoneNumber(),
453 VoicemailProviderSettings.NO_FORWARDING);
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800454 saveVoiceMailAndForwardingNumber(mVoicemailProviders.getKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700455 }
456 }
457
458 /**
459 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
460 * This method set the default values for the various
461 * EditPhoneNumberPreference dialogs.
462 */
463 @Override
464 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
465 if (preference == mSubMenuVoicemailSettings) {
466 // update the voicemail number field, which takes care of the
467 // mSubMenuVoicemailSettings itself, so we should return null.
468 if (DBG) log("updating default for voicemail dialog");
469 updateVoiceNumberField();
470 return null;
471 }
472
473 String vmDisplay = mPhone.getVoiceMailNumber();
474 if (TextUtils.isEmpty(vmDisplay)) {
475 // if there is no voicemail number, we just return null to
476 // indicate no contribution.
477 return null;
478 }
479
480 // Return the voicemail number prepended with "VM: "
481 if (DBG) log("updating default for call forwarding dialogs");
482 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
483 }
484
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700485 private void switchToPreviousVoicemailProvider() {
486 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700487
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800488 if (mPreviousVMProviderKey == null) {
489 return;
490 }
491
492 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
Andrew Lee84024342014-11-06 23:37:09 -0800493 showDialogIfForeground(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800494 final VoicemailProviderSettings prevSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800495 VoicemailProviderSettingsUtil.load(this, mPreviousVMProviderKey);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800496 if (prevSettings == null) {
497 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
498 + mPreviousVMProviderKey + "\" is null but should be loaded.");
499 }
500
501 if (mVMChangeCompletedSuccessfully) {
502 mNewVMNumber = prevSettings.getVoicemailNumber();
503 Log.i(LOG_TAG, "VM change is already completed successfully."
504 + "Have to revert VM back to " + mNewVMNumber + " again.");
505 mPhone.setVoiceMailNumber(
506 mPhone.getVoiceMailAlphaTag().toString(),
507 mNewVMNumber,
508 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
509 }
510
511 if (mFwdChangesRequireRollback) {
512 Log.i(LOG_TAG, "Requested to rollback forwarding changes.");
513
514 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
515 if (prevFwdSettings != null) {
516 Map<Integer, AsyncResult> results = mForwardingChangeResults;
517 resetForwardingChangeState();
518 for (int i = 0; i < prevFwdSettings.length; i++) {
519 CallForwardInfo fi = prevFwdSettings[i];
520 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
521 // Only revert the settings for which the update succeeded.
522 AsyncResult result = results.get(fi.reason);
523 if (result != null && result.exception == null) {
524 mExpectedChangeResultReasons.add(fi.reason);
525 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
526 mRevertOptionComplete.obtainMessage(
527 EVENT_FORWARDING_CHANGED, i, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700528 }
529 }
530 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700531 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800532 } else {
533 if (DBG) log("No need to revert");
534 onRevertDone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700535 }
536 }
537
538 private void onRevertDone() {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800539 if (DBG) log("onRevertDone: Changing provider key back to " + mPreviousVMProviderKey);
540
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700541 updateVMPreferenceWidgets(mPreviousVMProviderKey);
542 updateVoiceNumberField();
543 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800544 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700545 mVMOrFwdSetError = 0;
546 }
547 }
548
549 @Override
550 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
551 if (DBG) {
552 log("onActivityResult: requestCode: " + requestCode
553 + ", resultCode: " + resultCode
554 + ", data: " + data);
555 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800556
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700557 // there are cases where the contact picker may end up sending us more than one
558 // request. We want to ignore the request if we're not in the correct state.
559 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
560 boolean failure = false;
561
562 // No matter how the processing of result goes lets clear the flag
563 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
564 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
565 mVMProviderSettingsForced = false;
566
567 String vmNum = null;
568 if (resultCode != RESULT_OK) {
569 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
570 failure = true;
571 } else {
572 if (data == null) {
573 if (DBG) log("onActivityResult: vm provider cfg result has no data");
574 failure = true;
575 } else {
576 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
577 if (DBG) log("Provider requested signout");
578 if (isVMProviderSettingsForced) {
579 if (DBG) log("Going back to previous provider on signout");
580 switchToPreviousVoicemailProvider();
581 } else {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800582 final String victim = mVoicemailProviders.getKey();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700583 if (DBG) log("Relaunching activity and ignoring " + victim);
584 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
585 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
586 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
587 this.startActivity(i);
588 }
589 return;
590 }
591 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
592 if (vmNum == null || vmNum.length() == 0) {
593 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
594 failure = true;
595 }
596 }
597 }
598 if (failure) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800599 if (DBG) log("Failure in return from voicemail provider.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700600 if (isVMProviderSettingsForced) {
601 switchToPreviousVoicemailProvider();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700602 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800603
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700604 return;
605 }
606 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
607 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
608
Santos Cordonda120f42014-08-06 04:44:34 -0700609 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700610 // send it to the provider when it's config is invoked so it can use this as default
611 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
612
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800613 if (DBG) log("onActivityResult: cfg result has forwarding number " + fwdNum);
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800614 saveVoiceMailAndForwardingNumber(mVoicemailProviders.getKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700615 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700616 return;
617 }
618
619 if (requestCode == VOICEMAIL_PREF_ID) {
620 if (resultCode != RESULT_OK) {
621 if (DBG) log("onActivityResult: contact picker result not OK.");
622 return;
623 }
624
625 Cursor cursor = null;
626 try {
627 cursor = getContentResolver().query(data.getData(),
628 NUM_PROJECTION, null, null, null);
629 if ((cursor == null) || (!cursor.moveToFirst())) {
630 if (DBG) log("onActivityResult: bad contact data, no results found.");
631 return;
632 }
633 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
634 return;
635 } finally {
636 if (cursor != null) {
637 cursor.close();
638 }
639 }
640 }
641
642 super.onActivityResult(requestCode, resultCode, data);
643 }
644
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700645 /**
646 * Wrapper around showDialog() that will silently do nothing if we're
647 * not in the foreground.
648 *
649 * This is useful here because most of the dialogs we display from
650 * this class are triggered by asynchronous events (like
651 * success/failure messages from the telephony layer) and it's
652 * possible for those events to come in even after the user has gone
653 * to a different screen.
654 */
655 // TODO: this is too brittle: it's still easy to accidentally add new
656 // code here that calls showDialog() directly (which will result in a
657 // WindowManager$BadTokenException if called after the activity has
658 // been stopped.)
659 //
660 // It would be cleaner to do the "if (mForeground)" check in one
661 // central place, maybe by using a single Handler for all asynchronous
662 // events (and have *that* discard events if we're not in the
663 // foreground.)
664 //
665 // Unfortunately it's not that simple, since we sometimes need to do
666 // actual work to handle these events whether or not we're in the
667 // foreground (see the Handler code in mSetOptionComplete for
668 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800669 //
670 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
671 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700672 private void showDialogIfForeground(int id) {
673 if (mForeground) {
674 showDialog(id);
675 }
676 }
677
678 private void dismissDialogSafely(int id) {
679 try {
680 dismissDialog(id);
681 } catch (IllegalArgumentException e) {
682 // This is expected in the case where we were in the background
683 // at the time we would normally have shown the dialog, so we didn't
684 // show it.
685 }
686 }
687
Andrew Leeb490d732014-10-27 15:00:41 -0700688 private void saveVoiceMailAndForwardingNumber(
689 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700690 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700691 mNewVMNumber = newSettings.getVoicemailNumber();
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800692 mNewVMNumber = (mNewVMNumber == null) ? "" : mNewVMNumber;
Andrew Leeb490d732014-10-27 15:00:41 -0700693 mNewFwdSettings = newSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700694
695 // No fwd settings on CDMA
696 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
697 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700698 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700699 }
700
Andrew Leee3c15212014-10-28 13:12:55 -0700701 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700702 if (mNewVMNumber.equals(mOldVmNumber)
703 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Lee84024342014-11-06 23:37:09 -0800704 showDialogIfForeground(VoicemailDialogUtil.VM_NOCHANGE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700705 return;
706 }
707
Andrew Lee6950c6c2014-11-26 12:05:42 -0800708 VoicemailProviderSettingsUtil.save(this, key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700709 mVMChangeCompletedSuccessfully = false;
710 mFwdChangesRequireRollback = false;
711 mVMOrFwdSetError = 0;
712 if (!key.equals(mPreviousVMProviderKey)) {
713 mReadingSettingsForDefaultProvider =
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800714 mPreviousVMProviderKey.equals(VoicemailProviderListPreference.DEFAULT_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700715 if (DBG) log("Reading current forwarding settings");
Andrew Leeb490d732014-10-27 15:00:41 -0700716 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
717 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
718 for (int i = 0; i < mForwardingReadResults.length; i++) {
719 mPhone.getCallForwardingOption(
720 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700721 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
722 }
Andrew Lee84024342014-11-06 23:37:09 -0800723 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700724 } else {
725 saveVoiceMailAndForwardingNumberStage2();
726 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800727
728 // Refresh the MWI indicator if it is already showing.
729 PhoneGlobals.getInstance().refreshMwiIndicator(mSubscriptionInfoHelper.getSubId());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700730 }
731
732 private final Handler mGetOptionComplete = new Handler() {
733 @Override
734 public void handleMessage(Message msg) {
735 AsyncResult result = (AsyncResult) msg.obj;
736 switch (msg.what) {
737 case EVENT_FORWARDING_GET_COMPLETED:
738 handleForwardingSettingsReadResult(result, msg.arg1);
739 break;
740 }
741 }
742 };
743
744 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
745 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800746
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700747 Throwable error = null;
748 if (ar.exception != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700749 error = ar.exception;
Andrew Lee1af6cf72014-11-04 17:35:26 -0800750 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700751 }
752 if (ar.userObj instanceof Throwable) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800753 error = (Throwable) ar.userObj;
754 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700755 }
756
757 // We may have already gotten an error and decided to ignore the other results.
758 if (mForwardingReadResults == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800759 if (DBG) Log.d(LOG_TAG, "Ignoring fwd reading result: " + idx);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700760 return;
761 }
762
763 // In case of error ignore other results, show an error dialog
764 if (error != null) {
765 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
766 mForwardingReadResults = null;
Andrew Lee84024342014-11-06 23:37:09 -0800767 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
768 showDialogIfForeground(VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700769 return;
770 }
771
Andrew Lee1af6cf72014-11-04 17:35:26 -0800772 // Get the forwarding info.
773 mForwardingReadResults[idx] = CallForwardInfoUtil.getCallForwardInfo(
774 (CallForwardInfo[]) ar.result,
775 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx]);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700776
777 // Check if we got all the results already
778 boolean done = true;
779 for (int i = 0; i < mForwardingReadResults.length; i++) {
780 if (mForwardingReadResults[i] == null) {
781 done = false;
782 break;
783 }
784 }
Andrew Lee1af6cf72014-11-04 17:35:26 -0800785
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700786 if (done) {
787 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
Andrew Lee84024342014-11-06 23:37:09 -0800788 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800789
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700790 if (mReadingSettingsForDefaultProvider) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800791 VoicemailProviderSettingsUtil.save(mPhone.getContext(),
792 VoicemailProviderListPreference.DEFAULT_KEY,
Andrew Lee88b51e22014-10-29 15:48:51 -0700793 new VoicemailProviderSettings(this.mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700794 mReadingSettingsForDefaultProvider = false;
795 }
796 saveVoiceMailAndForwardingNumberStage2();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700797 }
798 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800799
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700800 private void resetForwardingChangeState() {
801 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
802 mExpectedChangeResultReasons = new HashSet<Integer>();
803 }
804
805 // Called after we are done saving the previous forwarding settings if
806 // we needed.
807 private void saveVoiceMailAndForwardingNumberStage2() {
808 mForwardingChangeResults = null;
809 mVoicemailChangeResult = null;
Andrew Leeb490d732014-10-27 15:00:41 -0700810 if (mNewFwdSettings != VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700811 resetForwardingChangeState();
812 for (int i = 0; i < mNewFwdSettings.length; i++) {
813 CallForwardInfo fi = mNewFwdSettings[i];
Andrew Lee1af6cf72014-11-04 17:35:26 -0800814 CallForwardInfo fiForReason =
815 CallForwardInfoUtil.infoForReason(mForwardingReadResults, fi.reason);
816 final boolean doUpdate = CallForwardInfoUtil.isUpdateRequired(fiForReason, fi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700817
818 if (doUpdate) {
819 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
820 mExpectedChangeResultReasons.add(i);
821
Andrew Lee1af6cf72014-11-04 17:35:26 -0800822 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700823 mSetOptionComplete.obtainMessage(
824 EVENT_FORWARDING_CHANGED, fi.reason, 0));
825 }
826 }
Andrew Lee84024342014-11-06 23:37:09 -0800827 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700828 } else {
829 if (DBG) log("Not touching fwd #");
830 setVMNumberWithCarrier();
831 }
832 }
833
834 private void setVMNumberWithCarrier() {
835 if (DBG) log("save voicemail #: " + mNewVMNumber);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800836
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700837 mPhone.setVoiceMailNumber(
838 mPhone.getVoiceMailAlphaTag().toString(),
839 mNewVMNumber,
840 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
841 }
842
843 /**
844 * Callback to handle option update completions
845 */
846 private final Handler mSetOptionComplete = new Handler() {
847 @Override
848 public void handleMessage(Message msg) {
849 AsyncResult result = (AsyncResult) msg.obj;
850 boolean done = false;
851 switch (msg.what) {
852 case EVENT_VOICEMAIL_CHANGED:
853 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700854 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700855 done = true;
856 break;
857 case EVENT_FORWARDING_CHANGED:
858 mForwardingChangeResults.put(msg.arg1, result);
859 if (result.exception != null) {
860 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
861 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700862 }
Andrew Leee438b312014-10-29 16:59:15 -0700863 if (isForwardingCompleted()) {
864 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700865 if (DBG) log("Overall fwd changes completed ok, starting vm change");
866 setVMNumberWithCarrier();
867 } else {
868 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
869 "Check if we need to try rollback for some settings.");
870 mFwdChangesRequireRollback = false;
871 Iterator<Map.Entry<Integer,AsyncResult>> it =
872 mForwardingChangeResults.entrySet().iterator();
873 while (it.hasNext()) {
874 Map.Entry<Integer,AsyncResult> entry = it.next();
875 if (entry.getValue().exception == null) {
876 // If at least one succeeded we have to revert
877 Log.i(LOG_TAG, "Rollback will be required");
878 mFwdChangesRequireRollback = true;
879 break;
880 }
881 }
882 if (!mFwdChangesRequireRollback) {
883 Log.i(LOG_TAG, "No rollback needed.");
884 }
885 done = true;
886 }
887 }
888 break;
889 default:
890 // TODO: should never reach this, may want to throw exception
891 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800892
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700893 if (done) {
894 if (DBG) log("All VM provider related changes done");
895 if (mForwardingChangeResults != null) {
Andrew Lee84024342014-11-06 23:37:09 -0800896 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700897 }
Andrew Leee438b312014-10-29 16:59:15 -0700898 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700899 }
900 }
901 };
902
903 /**
904 * Callback to handle option revert completions
905 */
906 private final Handler mRevertOptionComplete = new Handler() {
907 @Override
908 public void handleMessage(Message msg) {
909 AsyncResult result = (AsyncResult) msg.obj;
910 switch (msg.what) {
911 case EVENT_VOICEMAIL_CHANGED:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700912 if (DBG) log("VM revert complete msg");
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800913 mVoicemailChangeResult = result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700914 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800915
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700916 case EVENT_FORWARDING_CHANGED:
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800917 if (DBG) log("FWD revert complete msg ");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700918 mForwardingChangeResults.put(msg.arg1, result);
919 if (result.exception != null) {
920 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
921 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700922 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700923 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800924
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700925 default:
926 // TODO: should never reach this, may want to throw exception
927 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800928
929 final boolean done = (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null)
930 && (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700931 if (done) {
932 if (DBG) log("All VM reverts done");
Andrew Lee84024342014-11-06 23:37:09 -0800933 dismissDialogSafely(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700934 onRevertDone();
935 }
936 }
937 };
938
939 /**
Andrew Leee438b312014-10-29 16:59:15 -0700940 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700941 */
Andrew Leee438b312014-10-29 16:59:15 -0700942 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700943 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -0700944 return true;
945 }
946
947 for (Integer reason : mExpectedChangeResultReasons) {
948 if (mForwardingChangeResults.get(reason) == null) {
949 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700950 }
951 }
Andrew Leee438b312014-10-29 16:59:15 -0700952
953 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700954 }
Andrew Leee438b312014-10-29 16:59:15 -0700955
956 private boolean isFwdChangeSuccess() {
957 if (mForwardingChangeResults == null) {
958 return true;
959 }
960
961 for (AsyncResult result : mForwardingChangeResults.values()) {
962 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700963 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700964 String msg = exception.getMessage();
965 msg = (msg != null) ? msg : "";
966 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
967 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700968 }
969 }
Andrew Leee438b312014-10-29 16:59:15 -0700970 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700971 }
972
Andrew Leee438b312014-10-29 16:59:15 -0700973 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700974 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700975 String msg = mVoicemailChangeResult.exception.getMessage();
976 msg = (msg != null) ? msg : "";
977 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
978 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700979 }
Andrew Leee438b312014-10-29 16:59:15 -0700980 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700981 }
982
Andrew Leee438b312014-10-29 16:59:15 -0700983 private void handleSetVmOrFwdMessage() {
984 if (DBG) log("handleSetVMMessage: set VM request complete");
985
986 if (!isFwdChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -0800987 handleVmOrFwdSetError(VoicemailDialogUtil.FWD_SET_RESPONSE_ERROR_DIALOG);
Andrew Leee438b312014-10-29 16:59:15 -0700988 } else if (!isVmChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -0800989 handleVmOrFwdSetError(VoicemailDialogUtil.VM_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700990 } else {
Andrew Lee84024342014-11-06 23:37:09 -0800991 handleVmAndFwdSetSuccess(VoicemailDialogUtil.VM_CONFIRM_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700992 }
993 }
994
995 /**
996 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
997 * changes to those settings and show "failure" dialog.
998 *
Andrew Leeab082272014-11-04 15:50:42 -0800999 * @param dialogId ID of the dialog to show for the specific error case. Either
Andrew Lee84024342014-11-06 23:37:09 -08001000 * {@link #FWD_SET_RESPONSE_ERROR_DIALOG} or {@link #VM_RESPONSE_ERROR_DIALOG}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001001 */
Andrew Leeab082272014-11-04 15:50:42 -08001002 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001003 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -08001004 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001005 mChangingVMorFwdDueToProviderChange = false;
1006 switchToPreviousVoicemailProvider();
1007 return;
1008 }
1009 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001010 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001011 updateVoiceNumberField();
1012 }
1013
1014 /**
1015 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1016 * This updates a bunch of variables and show "success" dialog.
1017 */
Andrew Leeab082272014-11-04 15:50:42 -08001018 private void handleVmAndFwdSetSuccess(int dialogId) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001019 if (DBG) log("handleVmAndFwdSetSuccess: key is " + mVoicemailProviders.getKey());
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001020
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001021 mPreviousVMProviderKey = mVoicemailProviders.getKey();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001022 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001023 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001024 updateVoiceNumberField();
1025 }
1026
1027 /**
1028 * Update the voicemail number from what we've recorded on the sim.
1029 */
1030 private void updateVoiceNumberField() {
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001031 if (DBG) log("updateVoiceNumberField()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001032
1033 mOldVmNumber = mPhone.getVoiceMailNumber();
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001034 if (TextUtils.isEmpty(mOldVmNumber)) {
1035 mSubMenuVoicemailSettings.setPhoneNumber("");
1036 mSubMenuVoicemailSettings.setSummary(getString(R.string.voicemail_number_not_set));
1037 } else {
1038 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1039 mSubMenuVoicemailSettings.setSummary(mOldVmNumber);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001040 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001041 }
1042
1043 /*
1044 * Helper Methods for Activity class.
1045 * The initial query commands are split into two pieces now
1046 * for individual expansion. This combined with the ability
1047 * to cancel queries allows for a much better user experience,
1048 * and also ensures that the user only waits to update the
1049 * data that is relevant.
1050 */
1051
1052 @Override
1053 protected void onPrepareDialog(int id, Dialog dialog) {
1054 super.onPrepareDialog(id, dialog);
1055 mCurrentDialogId = id;
1056 }
1057
1058 // dialog creation method, called by showDialog()
1059 @Override
Andrew Lee84024342014-11-06 23:37:09 -08001060 protected Dialog onCreateDialog(int dialogId) {
1061 return VoicemailDialogUtil.getDialog(this, dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001062 }
1063
1064 // This is a method implemented for DialogInterface.OnClickListener.
1065 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1066 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1067 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1068 public void onClick(DialogInterface dialog, int which) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001069 if (DBG) log("onClick: button clicked is " + which);
1070
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001071 dialog.dismiss();
1072 switch (which){
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001073 case DialogInterface.BUTTON_NEGATIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001074 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001075 // We failed to get current forwarding settings and the user
1076 // does not wish to continue.
1077 switchToPreviousVoicemailProvider();
1078 }
1079 break;
1080 case DialogInterface.BUTTON_POSITIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001081 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001082 // We failed to get current forwarding settings but the user
1083 // wishes to continue changing settings to the new vm provider
1084 saveVoiceMailAndForwardingNumberStage2();
1085 } else {
1086 finish();
1087 }
1088 return;
1089 default:
1090 // just let the dialog close and go back to the input
1091 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001092
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001093 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1094 // with settings UI. If we were called to explicitly configure voice mail then
1095 // we finish the settings activity here to come back to whatever the user was doing.
1096 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1097 finish();
1098 }
1099 }
1100
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001101 /*
1102 * Activity class methods
1103 */
1104
1105 @Override
1106 protected void onCreate(Bundle icicle) {
1107 super.onCreate(icicle);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001108 if (DBG) log("onCreate: Intent is " + getIntent());
1109
Jay Shraunerc2684732014-11-12 12:10:37 -08001110 // Make sure we are running as the primary user.
1111 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
1112 Toast.makeText(this, R.string.call_settings_primary_user_only,
1113 Toast.LENGTH_SHORT).show();
1114 finish();
1115 return;
1116 }
1117
Tyler Gunnbaee2952014-09-10 16:01:02 -07001118 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
1119
Tyler Gunnbaee2952014-09-10 16:01:02 -07001120 // Show the voicemail preference in onResume if the calling intent specifies the
1121 // ACTION_ADD_VOICEMAIL action.
1122 mShowVoicemailPreference = (icicle == null) &&
Jay Shraunerbe2fb262014-11-11 15:19:58 -08001123 TextUtils.equals(getIntent().getAction(), ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -08001124 mSetupVoicemail = mShowVoicemailPreference &&
1125 getIntent().getBooleanExtra(SETUP_VOICEMAIL_EXTRA, false);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001126
1127 mSubscriptionInfoHelper = new SubscriptionInfoHelper(getIntent());
1128 mSubscriptionInfoHelper.setActionBarTitle(
1129 getActionBar(), getResources(), R.string.call_settings_with_label);
Andrew Lee1479dd12014-11-06 23:06:32 -08001130 mPhone = mSubscriptionInfoHelper.getPhone();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001131 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001132
1133 private void initPhoneAccountPreferences() {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001134 mPhoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001135
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001136 TelecomManager telecomManager = TelecomManager.from(this);
Andrew Lee93c345f2014-10-27 15:25:07 -07001137 TelephonyManager telephonyManager =
1138 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001139
Andrew Lee93c345f2014-10-27 15:25:07 -07001140 if ((telecomManager.getSimCallManagers().isEmpty() && !SipUtil.isVoipSupported(this))
1141 || telephonyManager.getPhoneCount() > 1) {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001142 getPreferenceScreen().removePreference(mPhoneAccountSettingsPreference);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001143 }
1144 }
1145
Tyler Gunnbaee2952014-09-10 16:01:02 -07001146 @Override
1147 protected void onResume() {
1148 super.onResume();
1149 mForeground = true;
1150
1151 PreferenceScreen preferenceScreen = getPreferenceScreen();
1152 if (preferenceScreen != null) {
1153 preferenceScreen.removeAll();
1154 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001155
1156 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001157
Andrew Leedb2fe562014-09-03 15:40:43 -07001158 initPhoneAccountPreferences();
1159
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001160 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001161 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1162 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1163 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1164 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001165
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001166 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1167 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1168 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
1169 mButtonTTY = (ListPreference) findPreference(BUTTON_TTY_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001170
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001171 mVoicemailProviders = (VoicemailProviderListPreference) findPreference(
1172 BUTTON_VOICEMAIL_PROVIDER_KEY);
1173 mVoicemailProviders.init(mPhone, getIntent());
Andrew Lee2c027892014-10-29 11:29:54 -07001174 mVoicemailProviders.setOnPreferenceChangeListener(this);
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001175 mPreviousVMProviderKey = mVoicemailProviders.getValue();
Andrew Lee8d66d812014-11-24 14:54:02 -08001176
Andrew Lee2c027892014-10-29 11:29:54 -07001177 mVoicemailSettingsScreen =
1178 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001179 mVoicemailSettings = (PreferenceScreen) findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
Andrew Lee8d66d812014-11-24 14:54:02 -08001180
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001181 mVoicemailNotificationRingtone = (VoicemailRingtonePreference) findPreference(
1182 getResources().getString(R.string.voicemail_notification_ringtone_key));
1183 mVoicemailNotificationRingtone.init(mPhone);
1184
Andrew Lee8d66d812014-11-24 14:54:02 -08001185 mVoicemailNotificationVibrate = (CheckBoxPreference) findPreference(
1186 getResources().getString(R.string.voicemail_notification_vibrate_key));
1187 mVoicemailNotificationVibrate.setOnPreferenceChangeListener(this);
1188
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001189 updateVMPreferenceWidgets(mVoicemailProviders.getValue());
1190
1191 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001192
Andrew Lee64a7d792014-10-15 17:38:38 -07001193 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1194 mButtonDTMF.setOnPreferenceChangeListener(this);
1195 int dtmf = Settings.System.getInt(getContentResolver(),
1196 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1197 mButtonDTMF.setValueIndex(dtmf);
1198 } else {
1199 prefSet.removePreference(mButtonDTMF);
1200 mButtonDTMF = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001201 }
1202
Andrew Lee64a7d792014-10-15 17:38:38 -07001203 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1204 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1205 int autoretry = Settings.Global.getInt(
1206 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1207 mButtonAutoRetry.setChecked(autoretry != 0);
1208 } else {
1209 prefSet.removePreference(mButtonAutoRetry);
1210 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001211 }
1212
Andrew Lee64a7d792014-10-15 17:38:38 -07001213 if (getResources().getBoolean(R.bool.hac_enabled)) {
1214 mButtonHAC.setOnPreferenceChangeListener(this);
1215 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1216 mButtonHAC.setChecked(hac != 0);
1217 } else {
1218 prefSet.removePreference(mButtonHAC);
1219 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001220 }
1221
Andrew Lee64a7d792014-10-15 17:38:38 -07001222 TelecomManager telecomManager = TelecomManager.from(this);
1223 if (telecomManager != null && telecomManager.isTtySupported()) {
1224 mButtonTTY.setOnPreferenceChangeListener(this);
1225 int settingsTtyMode = Settings.Secure.getInt(getContentResolver(),
1226 Settings.Secure.PREFERRED_TTY_MODE,
1227 TelecomManager.TTY_MODE_OFF);
1228 mButtonTTY.setValue(Integer.toString(settingsTtyMode));
1229 updatePreferredTtyModeSummary(settingsTtyMode);
1230 } else {
1231 prefSet.removePreference(mButtonTTY);
1232 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001233 }
1234
1235 if (!getResources().getBoolean(R.bool.world_phone)) {
Andrew Lee2b36ba22014-11-05 17:08:49 -08001236 Preference cdmaOptions = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
1237 prefSet.removePreference(cdmaOptions);
1238
1239 // TODO: Support MSIM for this preference option.
1240 Preference gsmOptions = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
1241 prefSet.removePreference(gsmOptions);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001242
1243 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001244 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Junda Liua09d6962014-11-12 13:39:02 -08001245 boolean shouldHideCarrierSettings = Settings.Global.getInt(
1246 getContentResolver(), Settings.Global.HIDE_CARRIER_NETWORK_SETTINGS, 0) == 1;
1247 if (shouldHideCarrierSettings) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001248 prefSet.removePreference(fdnButton);
Junda Liua09d6962014-11-12 13:39:02 -08001249 if (mButtonDTMF != null) {
1250 prefSet.removePreference(mButtonDTMF);
Etan Cohen0ca1c802014-07-07 15:35:48 -07001251 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001252 } else {
Junda Liua09d6962014-11-12 13:39:02 -08001253 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1254 prefSet.removePreference(fdnButton);
1255
1256 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1257 addPreferencesFromResource(R.xml.cdma_call_privacy);
1258 }
1259 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
1260 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(this, FdnSetting.class));
1261
1262 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
1263 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1264
1265 Preference callForwardingPref = prefSet.findPreference(CALL_FORWARDING_KEY);
1266 callForwardingPref.setIntent(mSubscriptionInfoHelper.getIntent(
1267 this, GsmUmtsCallForwardOptions.class));
1268
1269 Preference additionalGsmSettingsPref =
1270 prefSet.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
1271 additionalGsmSettingsPref.setIntent(mSubscriptionInfoHelper.getIntent(
1272 this, GsmUmtsAdditionalCallOptions.class));
1273 }
1274 } else {
1275 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1276 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001277 }
1278 }
1279
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001280 // check the intent that started this activity and pop up the voicemail
1281 // dialog if we've been asked to.
1282 // If we have at least one non default VM provider registered then bring up
1283 // the selection for the VM provider, otherwise bring up a VM number dialog.
1284 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001285 if (mShowVoicemailPreference) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001286 if (DBG) log("ACTION_ADD_VOICEMAIL Intent is thrown");
Tyler Gunn9c1071f2014-12-09 10:07:54 -08001287 if (mSetupVoicemail) {
1288 simulatePreferenceClick(mVoicemailSettingsScreen);
1289 mSetupVoicemail = false;
1290 } else if (mVoicemailProviders.hasMoreThanOneVoicemailProvider()) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001291 if (DBG) log("Voicemail data has more than one provider.");
Tyler Gunnbaee2952014-09-10 16:01:02 -07001292 simulatePreferenceClick(mVoicemailProviders);
1293 } else {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001294 onPreferenceChange(mVoicemailProviders, VoicemailProviderListPreference.DEFAULT_KEY);
1295 mVoicemailProviders.setValue(VoicemailProviderListPreference.DEFAULT_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001296 }
1297 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001298 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001299
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001300 updateVoiceNumberField();
1301 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001302
Andrew Lee8d66d812014-11-24 14:54:02 -08001303 mVoicemailNotificationVibrate.setChecked(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001304 VoicemailNotificationSettingsUtil.isVibrationEnabled(mPhone));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001305
Andrew Lee312e8172014-10-23 17:01:36 -07001306 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1307 boolean currentValue =
1308 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1309 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1310 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001311 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1312 } else {
1313 prefSet.removePreference(mEnableVideoCalling);
1314 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001315 }
1316
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001317 private void handleTTYChange(Preference preference, Object objValue) {
1318 int buttonTtyMode;
1319 buttonTtyMode = Integer.valueOf((String) objValue).intValue();
1320 int settingsTtyMode = android.provider.Settings.Secure.getInt(
1321 getContentResolver(),
Sailesh Nepalbf900542014-07-15 16:18:32 -07001322 android.provider.Settings.Secure.PREFERRED_TTY_MODE,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001323 TelecomManager.TTY_MODE_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001324 if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
1325 Integer.toString(buttonTtyMode));
1326
1327 if (buttonTtyMode != settingsTtyMode) {
1328 switch(buttonTtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001329 case TelecomManager.TTY_MODE_OFF:
1330 case TelecomManager.TTY_MODE_FULL:
1331 case TelecomManager.TTY_MODE_HCO:
1332 case TelecomManager.TTY_MODE_VCO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001333 android.provider.Settings.Secure.putInt(getContentResolver(),
1334 android.provider.Settings.Secure.PREFERRED_TTY_MODE, buttonTtyMode);
1335 break;
1336 default:
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001337 buttonTtyMode = TelecomManager.TTY_MODE_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001338 }
1339
1340 mButtonTTY.setValue(Integer.toString(buttonTtyMode));
1341 updatePreferredTtyModeSummary(buttonTtyMode);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001342 Intent ttyModeChanged = new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
1343 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001344 sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
1345 }
1346 }
1347
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001348 private void updatePreferredTtyModeSummary(int TtyMode) {
1349 String [] txts = getResources().getStringArray(R.array.tty_mode_entries);
1350 switch(TtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001351 case TelecomManager.TTY_MODE_OFF:
1352 case TelecomManager.TTY_MODE_HCO:
1353 case TelecomManager.TTY_MODE_VCO:
1354 case TelecomManager.TTY_MODE_FULL:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001355 mButtonTTY.setSummary(txts[TtyMode]);
1356 break;
1357 default:
1358 mButtonTTY.setEnabled(false);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001359 mButtonTTY.setSummary(txts[TelecomManager.TTY_MODE_OFF]);
Sailesh Nepalbf900542014-07-15 16:18:32 -07001360 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001361 }
1362 }
1363
1364 private static void log(String msg) {
1365 Log.d(LOG_TAG, msg);
1366 }
1367
1368 /**
1369 * Updates the look of the VM preference widgets based on current VM provider settings.
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001370 * Note that the provider name is loaded fxrorm the found activity via loadLabel in
1371 * {@link VoicemailProviderListPreference#initVoiceMailProviders()} in order for it to be
1372 * localizable.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001373 */
1374 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1375 final String key = currentProviderSetting;
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001376 final VoicemailProvider provider = mVoicemailProviders.getVoicemailProvider(key);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001377
1378 /* This is the case when we are coming up on a freshly wiped phone and there is no
1379 persisted value for the list preference mVoicemailProviders.
1380 In this case we want to show the UI asking the user to select a voicemail provider as
1381 opposed to silently falling back to default one. */
1382 if (provider == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001383 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> null.");
1384
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001385 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1386 mVoicemailSettings.setEnabled(false);
1387 mVoicemailSettings.setIntent(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001388 mVoicemailNotificationVibrate.setEnabled(false);
1389 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001390 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> " + provider.toString());
1391
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001392 final String providerName = provider.name;
1393 mVoicemailProviders.setSummary(providerName);
1394 mVoicemailSettings.setEnabled(true);
1395 mVoicemailSettings.setIntent(provider.intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001396 mVoicemailNotificationVibrate.setEnabled(true);
1397 }
1398 }
1399
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001400
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001401 /**
1402 * Simulates user clicking on a passed preference.
1403 * Usually needed when the preference is a dialog preference and we want to invoke
1404 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001405 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001406 */
1407 private void simulatePreferenceClick(Preference preference) {
1408 // Go through settings until we find our setting
1409 // and then simulate a click on it to bring up the dialog
1410 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1411 for (int idx = 0; idx < adapter.getCount(); idx++) {
1412 if (adapter.getItem(idx) == preference) {
1413 getPreferenceScreen().onItemClick(this.getListView(),
1414 null, idx, adapter.getItemId(idx));
1415 break;
1416 }
1417 }
1418 }
1419
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001420 @Override
1421 public boolean onOptionsItemSelected(MenuItem item) {
1422 final int itemId = item.getItemId();
1423 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001424 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001425 return true;
1426 }
1427 return super.onOptionsItemSelected(item);
1428 }
Andrew Lee8d66d812014-11-24 14:54:02 -08001429
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001430 /**
1431 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1432 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1433 */
Andrew Lee5efb1122014-12-05 04:20:42 -08001434 public static void goUpToTopLevelSetting(
1435 Activity activity, SubscriptionInfoHelper subscriptionInfoHelper) {
1436 Intent intent = subscriptionInfoHelper.getIntent(activity, CallFeaturesSetting.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001437 intent.setAction(Intent.ACTION_MAIN);
1438 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1439 activity.startActivity(intent);
1440 activity.finish();
1441 }
1442}