blob: 7e670aa2b4d7c8b94133573fc969156fb1bfeb4c [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;
28import android.content.SharedPreferences;
29import android.content.SharedPreferences.Editor;
30import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
33import android.database.Cursor;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.media.AudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.os.AsyncResult;
36import android.os.Bundle;
37import android.os.Handler;
38import android.os.Message;
39import android.os.UserHandle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.preference.CheckBoxPreference;
41import android.preference.ListPreference;
42import android.preference.Preference;
43import android.preference.PreferenceActivity;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.preference.PreferenceManager;
45import android.preference.PreferenceScreen;
46import android.provider.ContactsContract.CommonDataKinds;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070048import android.telecom.PhoneAccountHandle;
49import android.telecom.TelecomManager;
Andrew Lee93c345f2014-10-27 15:25:07 -070050import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.text.TextUtils;
52import android.util.Log;
53import android.view.MenuItem;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.widget.ListAdapter;
Jay Shraunerc2684732014-11-12 12:10:37 -080055import android.widget.Toast;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056
Andrew Lee312e8172014-10-23 17:01:36 -070057import com.android.ims.ImsManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import com.android.internal.telephony.CallForwardInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import com.android.internal.telephony.Phone;
60import com.android.internal.telephony.PhoneConstants;
Andrew Lee2170a972014-08-13 18:13:01 -070061import com.android.phone.common.util.SettingsUtil;
Andrew Leedb2fe562014-09-03 15:40:43 -070062import com.android.phone.settings.AccountSelectionPreference;
Andrew Lee1af6cf72014-11-04 17:35:26 -080063import com.android.phone.settings.CallForwardInfoUtil;
Andrew Lee84024342014-11-06 23:37:09 -080064import com.android.phone.settings.VoicemailDialogUtil;
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 Lee5ed870c2014-10-29 11:47:49 -070067import com.android.phone.settings.fdn.FdnSetting;
Sailesh Nepal788959e2014-07-08 23:36:40 -070068import com.android.services.telephony.sip.SipUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069
Andrew Lee2170a972014-08-13 18:13:01 -070070import java.lang.String;
Andrew Leef1776d82014-11-04 14:45:02 -080071import java.util.ArrayList;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072import java.util.Collection;
73import java.util.HashMap;
74import java.util.HashSet;
75import java.util.Iterator;
76import java.util.List;
77import java.util.Map;
78
79/**
80 * Top level "Call settings" UI; see res/xml/call_feature_setting.xml
81 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070082 * This preference screen is the root of the "Call settings" hierarchy available from the Phone
83 * app; the settings here let you control various features related to phone calls (including
84 * voicemail settings, the "Respond via SMS" feature, and others.) It's used only on
85 * voice-capable phone devices.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070086 *
87 * Note that this activity is part of the package com.android.phone, even
88 * though you reach it from the "Phone" app (i.e. DialtactsActivity) which
89 * is from the package com.android.contacts.
90 *
91 * For the "Mobile network settings" screen under the main Settings app,
92 * See {@link MobileNetworkSettings}.
93 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070094 * TODO: Settings should be split into PreferenceFragments where possible (ie. voicemail).
Andrew Lee2170a972014-08-13 18:13:01 -070095 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -070096 * @see com.android.phone.MobileNetworkSettings
97 */
98public class CallFeaturesSetting extends PreferenceActivity
99 implements DialogInterface.OnClickListener,
Andrew Lee2170a972014-08-13 18:13:01 -0700100 Preference.OnPreferenceChangeListener,
Andrew Lee2170a972014-08-13 18:13:01 -0700101 EditPhoneNumberPreference.OnDialogClosedListener,
Andrew Leece8ae2a2014-09-10 10:41:48 -0700102 EditPhoneNumberPreference.GetDefaultNumberListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700103 private static final String LOG_TAG = "CallFeaturesSetting";
104 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
Andrew Lee77527ac2014-10-21 16:57:39 -0700105 // STOPSHIP if true. Flag to override behavior default behavior to hide VT setting.
106 private static final boolean ENABLE_VT_FLAG = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107
108 /**
109 * Intent action to bring up Voicemail Provider settings.
110 *
111 * @see #IGNORE_PROVIDER_EXTRA
112 */
113 public static final String ACTION_ADD_VOICEMAIL =
114 "com.android.phone.CallFeaturesSetting.ADD_VOICEMAIL";
115 // intent action sent by this activity to a voice mail provider
116 // to trigger its configuration UI
117 public static final String ACTION_CONFIGURE_VOICEMAIL =
118 "com.android.phone.CallFeaturesSetting.CONFIGURE_VOICEMAIL";
119 // Extra put in the return from VM provider config containing voicemail number to set
120 public static final String VM_NUMBER_EXTRA = "com.android.phone.VoicemailNumber";
121 // Extra put in the return from VM provider config containing call forwarding number to set
122 public static final String FWD_NUMBER_EXTRA = "com.android.phone.ForwardingNumber";
123 // Extra put in the return from VM provider config containing call forwarding number to set
124 public static final String FWD_NUMBER_TIME_EXTRA = "com.android.phone.ForwardingNumberTime";
125 // If the VM provider returns non null value in this extra we will force the user to
126 // choose another VM provider
127 public static final String SIGNOUT_EXTRA = "com.android.phone.Signout";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700129 // Key identifying the default vocie mail provider
130 public static final String DEFAULT_VM_PROVIDER_KEY = "";
131
132 /**
133 * String Extra put into ACTION_ADD_VOICEMAIL call to indicate which provider should be hidden
134 * in the list of providers presented to the user. This allows a provider which is being
135 * disabled (e.g. GV user logging out) to force the user to pick some other provider.
136 */
137 public static final String IGNORE_PROVIDER_EXTRA = "com.android.phone.ProviderToIgnore";
138
139 // string constants
140 private static final String NUM_PROJECTION[] = {CommonDataKinds.Phone.NUMBER};
141
142 // String keys for preference lookup
143 // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
Andrew Lee97708a42014-09-25 12:39:07 -0700144 private static final String VOICEMAIL_SETTING_SCREEN_PREF_KEY = "button_voicemail_category_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700145 private static final String BUTTON_VOICEMAIL_KEY = "button_voicemail_key";
146 private static final String BUTTON_VOICEMAIL_PROVIDER_KEY = "button_voicemail_provider_key";
147 private static final String BUTTON_VOICEMAIL_SETTING_KEY = "button_voicemail_setting_key";
148 // New preference key for voicemail notification vibration
149 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY =
150 "button_voicemail_notification_vibrate_key";
151 // Old preference key for voicemail notification vibration. Used for migration to the new
152 // preference key only.
153 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY =
154 "button_voicemail_notification_vibrate_when_key";
155 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_RINGTONE_KEY =
156 "button_voicemail_notification_ringtone_key";
157 private static final String BUTTON_FDN_KEY = "button_fdn_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700158
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700159 private static final String BUTTON_DTMF_KEY = "button_dtmf_settings";
160 private static final String BUTTON_RETRY_KEY = "button_auto_retry_key";
161 private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
162 private static final String BUTTON_HAC_KEY = "button_hac_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700163
164 private static final String BUTTON_GSM_UMTS_OPTIONS = "button_gsm_more_expand_key";
165 private static final String BUTTON_CDMA_OPTIONS = "button_cdma_more_expand_key";
Andrew Lee2b36ba22014-11-05 17:08:49 -0800166 private static final String CALL_FORWARDING_KEY = "call_forwarding_key";
167 private static final String ADDITIONAL_GSM_SETTINGS_KEY = "additional_gsm_call_settings_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168
Andrew Leece8ae2a2014-09-10 10:41:48 -0700169 private static final String PHONE_ACCOUNT_SETTINGS_KEY =
170 "phone_account_settings_preference_screen";
Andrew Leedb2fe562014-09-03 15:40:43 -0700171
Andrew Leedf14ead2014-10-17 14:22:52 -0700172 private static final String ENABLE_VIDEO_CALLING_KEY = "button_enable_video_calling";
173
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700174 /** Event for Async voicemail change call */
175 private static final int EVENT_VOICEMAIL_CHANGED = 500;
176 private static final int EVENT_FORWARDING_CHANGED = 501;
177 private static final int EVENT_FORWARDING_GET_COMPLETED = 502;
178
Andrew Lee2170a972014-08-13 18:13:01 -0700179 private static final int MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY = 1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700180
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700181 public static final String HAC_KEY = "HACSetting";
182 public static final String HAC_VAL_ON = "ON";
183 public static final String HAC_VAL_OFF = "OFF";
184
185 /** Handle to voicemail pref */
186 private static final int VOICEMAIL_PREF_ID = 1;
187 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
188
189 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700190 private AudioManager mAudioManager;
Andrew Lee88b51e22014-10-29 15:48:51 -0700191 private VoicemailProviderSettingsUtil mVmProviderSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700192
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700193 // voicemail notification vibration string constants
194 private static final String VOICEMAIL_VIBRATION_ALWAYS = "always";
195 private static final String VOICEMAIL_VIBRATION_NEVER = "never";
196
Andrew Lee5ed870c2014-10-29 11:47:49 -0700197 private SubscriptionInfoHelper mSubscriptionInfoHelper;
198
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700199 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
200
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700201 /** Whether dialpad plays DTMF tone or not. */
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700202 private CheckBoxPreference mButtonAutoRetry;
203 private CheckBoxPreference mButtonHAC;
204 private ListPreference mButtonDTMF;
205 private ListPreference mButtonTTY;
Andrew Leece8ae2a2014-09-10 10:41:48 -0700206 private Preference mPhoneAccountSettingsPreference;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700207 private ListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700208 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700209 private PreferenceScreen mVoicemailSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700210 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700211 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700212
213 private class VoiceMailProvider {
Andrew Leef1776d82014-11-04 14:45:02 -0800214 public String name;
215 public Intent intent;
216
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700217 public VoiceMailProvider(String name, Intent intent) {
218 this.name = name;
219 this.intent = intent;
220 }
Andrew Leef1776d82014-11-04 14:45:02 -0800221
222 public String toString() {
223 return "[ Name: " + name + ", Intent: " + intent + " ]";
224 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700225 }
226
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700227 /**
228 * Results of reading forwarding settings
229 */
230 private CallForwardInfo[] mForwardingReadResults = null;
231
232 /**
233 * Result of forwarding number change.
234 * Keys are reasons (eg. unconditional forwarding).
235 */
236 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
237
238 /**
239 * Expected CF read result types.
240 * This set keeps track of the CF types for which we've issued change
241 * commands so we can tell when we've received all of the responses.
242 */
243 private Collection<Integer> mExpectedChangeResultReasons = null;
244
245 /**
246 * Result of vm number change
247 */
248 private AsyncResult mVoicemailChangeResult = null;
249
250 /**
251 * Previous VM provider setting so we can return to it in case of failure.
252 */
253 private String mPreviousVMProviderKey = null;
254
255 /**
256 * Id of the dialog being currently shown.
257 */
258 private int mCurrentDialogId = 0;
259
260 /**
261 * Flag indicating that we are invoking settings for the voicemail provider programmatically
262 * due to vm provider change.
263 */
264 private boolean mVMProviderSettingsForced = false;
265
266 /**
267 * Flag indicating that we are making changes to vm or fwd numbers
268 * due to vm provider change.
269 */
270 private boolean mChangingVMorFwdDueToProviderChange = false;
271
272 /**
273 * True if we are in the process of vm & fwd number change and vm has already been changed.
274 * This is used to decide what to do in case of rollback.
275 */
276 private boolean mVMChangeCompletedSuccessfully = false;
277
278 /**
279 * True if we had full or partial failure setting forwarding numbers and so need to roll them
280 * back.
281 */
282 private boolean mFwdChangesRequireRollback = false;
283
284 /**
285 * Id of error msg to display to user once we are done reverting the VM provider to the previous
286 * one.
287 */
288 private int mVMOrFwdSetError = 0;
289
290 /**
291 * Data about discovered voice mail settings providers.
292 * Is populated by querying which activities can handle ACTION_CONFIGURE_VOICEMAIL.
293 * They key in this map is package name + activity name.
294 * We always add an entry for the default provider with a key of empty
295 * string and intent value of null.
296 * @see #initVoiceMailProviders()
297 */
298 private final Map<String, VoiceMailProvider> mVMProvidersData =
299 new HashMap<String, VoiceMailProvider>();
300
301 /** string to hold old voicemail number as it is being updated. */
302 private String mOldVmNumber;
303
304 // New call forwarding settings and vm number we will be setting
305 // Need to save these since before we get to saving we need to asynchronously
306 // query the existing forwarding settings.
307 private CallForwardInfo[] mNewFwdSettings;
308 private String mNewVMNumber;
309
310 private boolean mForeground;
311
312 @Override
313 public void onPause() {
314 super.onPause();
315 mForeground = false;
316 }
317
318 /**
319 * We have to pull current settings from the network for all kinds of
320 * voicemail providers so we can tell whether we have to update them,
321 * so use this bit to keep track of whether we're reading settings for the
322 * default provider and should therefore save them out when done.
323 */
324 private boolean mReadingSettingsForDefaultProvider = false;
325
Tyler Gunnbaee2952014-09-10 16:01:02 -0700326 /**
327 * Used to indicate that the voicemail preference should be shown.
328 */
329 private boolean mShowVoicemailPreference = false;
330
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700331 /*
332 * Click Listeners, handle click based on objects attached to UI.
333 */
334
335 // Click listener for all toggle events
336 @Override
337 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
338 if (preference == mSubMenuVoicemailSettings) {
339 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700340 } else if (preference == mButtonDTMF) {
341 return true;
342 } else if (preference == mButtonTTY) {
343 return true;
344 } else if (preference == mButtonAutoRetry) {
345 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
346 android.provider.Settings.Global.CALL_AUTO_RETRY,
347 mButtonAutoRetry.isChecked() ? 1 : 0);
348 return true;
349 } else if (preference == mButtonHAC) {
350 int hac = mButtonHAC.isChecked() ? 1 : 0;
351 // Update HAC value in Settings database
352 Settings.System.putInt(mPhone.getContext().getContentResolver(),
353 Settings.System.HEARING_AID, hac);
354
355 // Update HAC Value in AudioManager
356 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
357 return true;
358 } else if (preference == mVoicemailSettings) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800359 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
360
Yorke Leea0f63bf2014-10-09 18:27:20 -0700361 final Dialog dialog = mVoicemailSettings.getDialog();
362 if (dialog != null) {
363 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
364 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800365
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700366 if (preference.getIntent() != null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800367 if (DBG) log("Invoking cfg intent " + preference.getIntent().getPackage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700368
369 // onActivityResult() will be responsible for resetting some of variables.
370 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
371 return true;
372 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800373 if (DBG) log("onPreferenceTreeClick(). No intent; use default behavior in xml.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700374
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800375 // onActivityResult() will not be called, so reset variables here.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700376 mPreviousVMProviderKey = DEFAULT_VM_PROVIDER_KEY;
377 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700378 return false;
379 }
Andrew Lee97708a42014-09-25 12:39:07 -0700380 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700381 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
382 if (dialog != null) {
383 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
384 }
Andrew Lee97708a42014-09-25 12:39:07 -0700385 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700386 }
387 return false;
388 }
389
390 /**
391 * Implemented to support onPreferenceChangeListener to look for preference
392 * changes.
393 *
394 * @param preference is the preference to be changed
395 * @param objValue should be the value of the selection, NOT its localized
396 * display value.
397 */
398 @Override
399 public boolean onPreferenceChange(Preference preference, Object objValue) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800400 if (DBG) log("onPreferenceChange: \"" + preference + "\" changed to \"" + objValue + "\"");
Andrew Lee2170a972014-08-13 18:13:01 -0700401
402 if (preference == mButtonDTMF) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700403 int index = mButtonDTMF.findIndexOfValue((String) objValue);
404 Settings.System.putInt(mPhone.getContext().getContentResolver(),
405 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
406 } else if (preference == mButtonTTY) {
407 handleTTYChange(preference, objValue);
408 } else if (preference == mVoicemailProviders) {
409 final String newProviderKey = (String) objValue;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800410
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700411 // If previous provider key and the new one is same, we don't need to handle it.
412 if (mPreviousVMProviderKey.equals(newProviderKey)) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800413 if (DBG) log("No change is made to the VM provider setting.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700414 return true;
415 }
416 updateVMPreferenceWidgets(newProviderKey);
417
Andrew Leeb490d732014-10-27 15:00:41 -0700418 final VoicemailProviderSettings newProviderSettings =
Andrew Lee88b51e22014-10-29 15:48:51 -0700419 mVmProviderSettingsUtil.load(newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700420
Andrew Lee88b51e22014-10-29 15:48:51 -0700421 // If the user switches to a voice mail provider and we have numbers stored for it we
422 // will automatically change the phone's voice mail and forwarding number to the stored
423 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700424 if (newProviderSettings == null) {
425 // Force the user into a configuration of the chosen provider
426 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
427 mVMProviderSettingsForced = true;
428 simulatePreferenceClick(mVoicemailSettings);
429 } else {
430 if (DBG) log("Saved preferences found - switching to them");
431 // Set this flag so if we get a failure we revert to previous provider
432 mChangingVMorFwdDueToProviderChange = true;
433 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
434 }
Andrew Leedf14ead2014-10-17 14:22:52 -0700435 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700436 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
437 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
438 } else {
439 AlertDialog.Builder builder = new AlertDialog.Builder(this);
440 DialogInterface.OnClickListener networkSettingsClickListener =
441 new Dialog.OnClickListener() {
442 @Override
443 public void onClick(DialogInterface dialog, int which) {
444 startActivity(new Intent(mPhone.getContext(),
445 com.android.phone.MobileNetworkSettings.class));
446 }
447 };
448 builder.setMessage(getResources().getString(
449 R.string.enable_video_calling_dialog_msg))
450 .setNeutralButton(getResources().getString(
451 R.string.enable_video_calling_dialog_settings),
452 networkSettingsClickListener)
453 .setPositiveButton(android.R.string.ok, null)
454 .show();
455 return false;
456 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700457 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800458
459 // Always let the preference setting proceed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700460 return true;
461 }
462
463 @Override
464 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800465 if (DBG) log("onDialogClosed: Button clicked is " + buttonClicked);
466
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700467 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
468 return;
469 }
470
471 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700472 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
473 mSubMenuVoicemailSettings.getPhoneNumber(),
474 VoicemailProviderSettings.NO_FORWARDING);
475 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700476 }
477 }
478
479 /**
480 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
481 * This method set the default values for the various
482 * EditPhoneNumberPreference dialogs.
483 */
484 @Override
485 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
486 if (preference == mSubMenuVoicemailSettings) {
487 // update the voicemail number field, which takes care of the
488 // mSubMenuVoicemailSettings itself, so we should return null.
489 if (DBG) log("updating default for voicemail dialog");
490 updateVoiceNumberField();
491 return null;
492 }
493
494 String vmDisplay = mPhone.getVoiceMailNumber();
495 if (TextUtils.isEmpty(vmDisplay)) {
496 // if there is no voicemail number, we just return null to
497 // indicate no contribution.
498 return null;
499 }
500
501 // Return the voicemail number prepended with "VM: "
502 if (DBG) log("updating default for call forwarding dialogs");
503 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
504 }
505
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700506 private void switchToPreviousVoicemailProvider() {
507 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700508
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800509 if (mPreviousVMProviderKey == null) {
510 return;
511 }
512
513 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
Andrew Lee84024342014-11-06 23:37:09 -0800514 showDialogIfForeground(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800515 final VoicemailProviderSettings prevSettings =
516 mVmProviderSettingsUtil.load(mPreviousVMProviderKey);
517 if (prevSettings == null) {
518 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
519 + mPreviousVMProviderKey + "\" is null but should be loaded.");
520 }
521
522 if (mVMChangeCompletedSuccessfully) {
523 mNewVMNumber = prevSettings.getVoicemailNumber();
524 Log.i(LOG_TAG, "VM change is already completed successfully."
525 + "Have to revert VM back to " + mNewVMNumber + " again.");
526 mPhone.setVoiceMailNumber(
527 mPhone.getVoiceMailAlphaTag().toString(),
528 mNewVMNumber,
529 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
530 }
531
532 if (mFwdChangesRequireRollback) {
533 Log.i(LOG_TAG, "Requested to rollback forwarding changes.");
534
535 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
536 if (prevFwdSettings != null) {
537 Map<Integer, AsyncResult> results = mForwardingChangeResults;
538 resetForwardingChangeState();
539 for (int i = 0; i < prevFwdSettings.length; i++) {
540 CallForwardInfo fi = prevFwdSettings[i];
541 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
542 // Only revert the settings for which the update succeeded.
543 AsyncResult result = results.get(fi.reason);
544 if (result != null && result.exception == null) {
545 mExpectedChangeResultReasons.add(fi.reason);
546 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
547 mRevertOptionComplete.obtainMessage(
548 EVENT_FORWARDING_CHANGED, i, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700549 }
550 }
551 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700552 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800553 } else {
554 if (DBG) log("No need to revert");
555 onRevertDone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700556 }
557 }
558
559 private void onRevertDone() {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800560 if (DBG) log("onRevertDone: Changing provider key back to " + mPreviousVMProviderKey);
561
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700562 updateVMPreferenceWidgets(mPreviousVMProviderKey);
563 updateVoiceNumberField();
564 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800565 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700566 mVMOrFwdSetError = 0;
567 }
568 }
569
570 @Override
571 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
572 if (DBG) {
573 log("onActivityResult: requestCode: " + requestCode
574 + ", resultCode: " + resultCode
575 + ", data: " + data);
576 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800577
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700578 // there are cases where the contact picker may end up sending us more than one
579 // request. We want to ignore the request if we're not in the correct state.
580 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
581 boolean failure = false;
582
583 // No matter how the processing of result goes lets clear the flag
584 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
585 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
586 mVMProviderSettingsForced = false;
587
588 String vmNum = null;
589 if (resultCode != RESULT_OK) {
590 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
591 failure = true;
592 } else {
593 if (data == null) {
594 if (DBG) log("onActivityResult: vm provider cfg result has no data");
595 failure = true;
596 } else {
597 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
598 if (DBG) log("Provider requested signout");
599 if (isVMProviderSettingsForced) {
600 if (DBG) log("Going back to previous provider on signout");
601 switchToPreviousVoicemailProvider();
602 } else {
603 final String victim = getCurrentVoicemailProviderKey();
604 if (DBG) log("Relaunching activity and ignoring " + victim);
605 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
606 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
607 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
608 this.startActivity(i);
609 }
610 return;
611 }
612 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
613 if (vmNum == null || vmNum.length() == 0) {
614 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
615 failure = true;
616 }
617 }
618 }
619 if (failure) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800620 if (DBG) log("Failure in return from voicemail provider.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700621 if (isVMProviderSettingsForced) {
622 switchToPreviousVoicemailProvider();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700623 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800624
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700625 return;
626 }
627 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
628 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
629
Santos Cordonda120f42014-08-06 04:44:34 -0700630 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700631 // send it to the provider when it's config is invoked so it can use this as default
632 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
633
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800634 if (DBG) log("onActivityResult: cfg result has forwarding number " + fwdNum);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700635 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700636 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700637 return;
638 }
639
640 if (requestCode == VOICEMAIL_PREF_ID) {
641 if (resultCode != RESULT_OK) {
642 if (DBG) log("onActivityResult: contact picker result not OK.");
643 return;
644 }
645
646 Cursor cursor = null;
647 try {
648 cursor = getContentResolver().query(data.getData(),
649 NUM_PROJECTION, null, null, null);
650 if ((cursor == null) || (!cursor.moveToFirst())) {
651 if (DBG) log("onActivityResult: bad contact data, no results found.");
652 return;
653 }
654 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
655 return;
656 } finally {
657 if (cursor != null) {
658 cursor.close();
659 }
660 }
661 }
662
663 super.onActivityResult(requestCode, resultCode, data);
664 }
665
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700666 /**
667 * Wrapper around showDialog() that will silently do nothing if we're
668 * not in the foreground.
669 *
670 * This is useful here because most of the dialogs we display from
671 * this class are triggered by asynchronous events (like
672 * success/failure messages from the telephony layer) and it's
673 * possible for those events to come in even after the user has gone
674 * to a different screen.
675 */
676 // TODO: this is too brittle: it's still easy to accidentally add new
677 // code here that calls showDialog() directly (which will result in a
678 // WindowManager$BadTokenException if called after the activity has
679 // been stopped.)
680 //
681 // It would be cleaner to do the "if (mForeground)" check in one
682 // central place, maybe by using a single Handler for all asynchronous
683 // events (and have *that* discard events if we're not in the
684 // foreground.)
685 //
686 // Unfortunately it's not that simple, since we sometimes need to do
687 // actual work to handle these events whether or not we're in the
688 // foreground (see the Handler code in mSetOptionComplete for
689 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800690 //
691 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
692 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700693 private void showDialogIfForeground(int id) {
694 if (mForeground) {
695 showDialog(id);
696 }
697 }
698
699 private void dismissDialogSafely(int id) {
700 try {
701 dismissDialog(id);
702 } catch (IllegalArgumentException e) {
703 // This is expected in the case where we were in the background
704 // at the time we would normally have shown the dialog, so we didn't
705 // show it.
706 }
707 }
708
Andrew Leeb490d732014-10-27 15:00:41 -0700709 private void saveVoiceMailAndForwardingNumber(
710 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700711 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700712 mNewVMNumber = newSettings.getVoicemailNumber();
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800713 mNewVMNumber = (mNewVMNumber == null) ? "" : mNewVMNumber;
Andrew Leeb490d732014-10-27 15:00:41 -0700714 mNewFwdSettings = newSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700715
716 // No fwd settings on CDMA
717 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
718 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700719 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700720 }
721
Andrew Leee3c15212014-10-28 13:12:55 -0700722 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700723 if (mNewVMNumber.equals(mOldVmNumber)
724 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Lee84024342014-11-06 23:37:09 -0800725 showDialogIfForeground(VoicemailDialogUtil.VM_NOCHANGE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700726 return;
727 }
728
Andrew Lee88b51e22014-10-29 15:48:51 -0700729 mVmProviderSettingsUtil.save(key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700730 mVMChangeCompletedSuccessfully = false;
731 mFwdChangesRequireRollback = false;
732 mVMOrFwdSetError = 0;
733 if (!key.equals(mPreviousVMProviderKey)) {
734 mReadingSettingsForDefaultProvider =
735 mPreviousVMProviderKey.equals(DEFAULT_VM_PROVIDER_KEY);
736 if (DBG) log("Reading current forwarding settings");
Andrew Leeb490d732014-10-27 15:00:41 -0700737 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
738 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
739 for (int i = 0; i < mForwardingReadResults.length; i++) {
740 mPhone.getCallForwardingOption(
741 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700742 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
743 }
Andrew Lee84024342014-11-06 23:37:09 -0800744 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700745 } else {
746 saveVoiceMailAndForwardingNumberStage2();
747 }
748 }
749
750 private final Handler mGetOptionComplete = new Handler() {
751 @Override
752 public void handleMessage(Message msg) {
753 AsyncResult result = (AsyncResult) msg.obj;
754 switch (msg.what) {
755 case EVENT_FORWARDING_GET_COMPLETED:
756 handleForwardingSettingsReadResult(result, msg.arg1);
757 break;
758 }
759 }
760 };
761
762 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
763 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800764
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700765 Throwable error = null;
766 if (ar.exception != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700767 error = ar.exception;
Andrew Lee1af6cf72014-11-04 17:35:26 -0800768 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700769 }
770 if (ar.userObj instanceof Throwable) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800771 error = (Throwable) ar.userObj;
772 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700773 }
774
775 // We may have already gotten an error and decided to ignore the other results.
776 if (mForwardingReadResults == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800777 if (DBG) Log.d(LOG_TAG, "Ignoring fwd reading result: " + idx);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700778 return;
779 }
780
781 // In case of error ignore other results, show an error dialog
782 if (error != null) {
783 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
784 mForwardingReadResults = null;
Andrew Lee84024342014-11-06 23:37:09 -0800785 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
786 showDialogIfForeground(VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700787 return;
788 }
789
Andrew Lee1af6cf72014-11-04 17:35:26 -0800790 // Get the forwarding info.
791 mForwardingReadResults[idx] = CallForwardInfoUtil.getCallForwardInfo(
792 (CallForwardInfo[]) ar.result,
793 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx]);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700794
795 // Check if we got all the results already
796 boolean done = true;
797 for (int i = 0; i < mForwardingReadResults.length; i++) {
798 if (mForwardingReadResults[i] == null) {
799 done = false;
800 break;
801 }
802 }
Andrew Lee1af6cf72014-11-04 17:35:26 -0800803
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700804 if (done) {
805 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
Andrew Lee84024342014-11-06 23:37:09 -0800806 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800807
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700808 if (mReadingSettingsForDefaultProvider) {
Andrew Lee88b51e22014-10-29 15:48:51 -0700809 mVmProviderSettingsUtil.save(DEFAULT_VM_PROVIDER_KEY,
810 new VoicemailProviderSettings(this.mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700811 mReadingSettingsForDefaultProvider = false;
812 }
813 saveVoiceMailAndForwardingNumberStage2();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700814 }
815 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800816
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700817 private void resetForwardingChangeState() {
818 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
819 mExpectedChangeResultReasons = new HashSet<Integer>();
820 }
821
822 // Called after we are done saving the previous forwarding settings if
823 // we needed.
824 private void saveVoiceMailAndForwardingNumberStage2() {
825 mForwardingChangeResults = null;
826 mVoicemailChangeResult = null;
Andrew Leeb490d732014-10-27 15:00:41 -0700827 if (mNewFwdSettings != VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700828 resetForwardingChangeState();
829 for (int i = 0; i < mNewFwdSettings.length; i++) {
830 CallForwardInfo fi = mNewFwdSettings[i];
Andrew Lee1af6cf72014-11-04 17:35:26 -0800831 CallForwardInfo fiForReason =
832 CallForwardInfoUtil.infoForReason(mForwardingReadResults, fi.reason);
833 final boolean doUpdate = CallForwardInfoUtil.isUpdateRequired(fiForReason, fi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700834
835 if (doUpdate) {
836 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
837 mExpectedChangeResultReasons.add(i);
838
Andrew Lee1af6cf72014-11-04 17:35:26 -0800839 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700840 mSetOptionComplete.obtainMessage(
841 EVENT_FORWARDING_CHANGED, fi.reason, 0));
842 }
843 }
Andrew Lee84024342014-11-06 23:37:09 -0800844 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700845 } else {
846 if (DBG) log("Not touching fwd #");
847 setVMNumberWithCarrier();
848 }
849 }
850
851 private void setVMNumberWithCarrier() {
852 if (DBG) log("save voicemail #: " + mNewVMNumber);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800853
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700854 mPhone.setVoiceMailNumber(
855 mPhone.getVoiceMailAlphaTag().toString(),
856 mNewVMNumber,
857 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
858 }
859
860 /**
861 * Callback to handle option update completions
862 */
863 private final Handler mSetOptionComplete = new Handler() {
864 @Override
865 public void handleMessage(Message msg) {
866 AsyncResult result = (AsyncResult) msg.obj;
867 boolean done = false;
868 switch (msg.what) {
869 case EVENT_VOICEMAIL_CHANGED:
870 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700871 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700872 done = true;
873 break;
874 case EVENT_FORWARDING_CHANGED:
875 mForwardingChangeResults.put(msg.arg1, result);
876 if (result.exception != null) {
877 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
878 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700879 }
Andrew Leee438b312014-10-29 16:59:15 -0700880 if (isForwardingCompleted()) {
881 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700882 if (DBG) log("Overall fwd changes completed ok, starting vm change");
883 setVMNumberWithCarrier();
884 } else {
885 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
886 "Check if we need to try rollback for some settings.");
887 mFwdChangesRequireRollback = false;
888 Iterator<Map.Entry<Integer,AsyncResult>> it =
889 mForwardingChangeResults.entrySet().iterator();
890 while (it.hasNext()) {
891 Map.Entry<Integer,AsyncResult> entry = it.next();
892 if (entry.getValue().exception == null) {
893 // If at least one succeeded we have to revert
894 Log.i(LOG_TAG, "Rollback will be required");
895 mFwdChangesRequireRollback = true;
896 break;
897 }
898 }
899 if (!mFwdChangesRequireRollback) {
900 Log.i(LOG_TAG, "No rollback needed.");
901 }
902 done = true;
903 }
904 }
905 break;
906 default:
907 // TODO: should never reach this, may want to throw exception
908 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800909
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700910 if (done) {
911 if (DBG) log("All VM provider related changes done");
912 if (mForwardingChangeResults != null) {
Andrew Lee84024342014-11-06 23:37:09 -0800913 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700914 }
Andrew Leee438b312014-10-29 16:59:15 -0700915 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700916 }
917 }
918 };
919
920 /**
921 * Callback to handle option revert completions
922 */
923 private final Handler mRevertOptionComplete = new Handler() {
924 @Override
925 public void handleMessage(Message msg) {
926 AsyncResult result = (AsyncResult) msg.obj;
927 switch (msg.what) {
928 case EVENT_VOICEMAIL_CHANGED:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700929 if (DBG) log("VM revert complete msg");
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800930 mVoicemailChangeResult = result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700931 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800932
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700933 case EVENT_FORWARDING_CHANGED:
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800934 if (DBG) log("FWD revert complete msg ");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700935 mForwardingChangeResults.put(msg.arg1, result);
936 if (result.exception != null) {
937 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
938 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700939 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700940 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800941
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700942 default:
943 // TODO: should never reach this, may want to throw exception
944 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800945
946 final boolean done = (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null)
947 && (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700948 if (done) {
949 if (DBG) log("All VM reverts done");
Andrew Lee84024342014-11-06 23:37:09 -0800950 dismissDialogSafely(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700951 onRevertDone();
952 }
953 }
954 };
955
956 /**
Andrew Leee438b312014-10-29 16:59:15 -0700957 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700958 */
Andrew Leee438b312014-10-29 16:59:15 -0700959 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700960 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -0700961 return true;
962 }
963
964 for (Integer reason : mExpectedChangeResultReasons) {
965 if (mForwardingChangeResults.get(reason) == null) {
966 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700967 }
968 }
Andrew Leee438b312014-10-29 16:59:15 -0700969
970 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700971 }
Andrew Leee438b312014-10-29 16:59:15 -0700972
973 private boolean isFwdChangeSuccess() {
974 if (mForwardingChangeResults == null) {
975 return true;
976 }
977
978 for (AsyncResult result : mForwardingChangeResults.values()) {
979 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700980 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700981 String msg = exception.getMessage();
982 msg = (msg != null) ? msg : "";
983 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
984 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700985 }
986 }
Andrew Leee438b312014-10-29 16:59:15 -0700987 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700988 }
989
Andrew Leee438b312014-10-29 16:59:15 -0700990 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700991 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700992 String msg = mVoicemailChangeResult.exception.getMessage();
993 msg = (msg != null) ? msg : "";
994 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
995 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700996 }
Andrew Leee438b312014-10-29 16:59:15 -0700997 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700998 }
999
Andrew Leee438b312014-10-29 16:59:15 -07001000 private void handleSetVmOrFwdMessage() {
1001 if (DBG) log("handleSetVMMessage: set VM request complete");
1002
1003 if (!isFwdChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -08001004 handleVmOrFwdSetError(VoicemailDialogUtil.FWD_SET_RESPONSE_ERROR_DIALOG);
Andrew Leee438b312014-10-29 16:59:15 -07001005 } else if (!isVmChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -08001006 handleVmOrFwdSetError(VoicemailDialogUtil.VM_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001007 } else {
Andrew Lee84024342014-11-06 23:37:09 -08001008 handleVmAndFwdSetSuccess(VoicemailDialogUtil.VM_CONFIRM_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001009 }
1010 }
1011
1012 /**
1013 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
1014 * changes to those settings and show "failure" dialog.
1015 *
Andrew Leeab082272014-11-04 15:50:42 -08001016 * @param dialogId ID of the dialog to show for the specific error case. Either
Andrew Lee84024342014-11-06 23:37:09 -08001017 * {@link #FWD_SET_RESPONSE_ERROR_DIALOG} or {@link #VM_RESPONSE_ERROR_DIALOG}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001018 */
Andrew Leeab082272014-11-04 15:50:42 -08001019 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001020 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -08001021 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001022 mChangingVMorFwdDueToProviderChange = false;
1023 switchToPreviousVoicemailProvider();
1024 return;
1025 }
1026 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001027 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001028 updateVoiceNumberField();
1029 }
1030
1031 /**
1032 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1033 * This updates a bunch of variables and show "success" dialog.
1034 */
Andrew Leeab082272014-11-04 15:50:42 -08001035 private void handleVmAndFwdSetSuccess(int dialogId) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001036 if (DBG) log("handleVmAndFwdSetSuccess: key is " + getCurrentVoicemailProviderKey());
1037
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001038 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1039 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001040 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001041 updateVoiceNumberField();
1042 }
1043
1044 /**
1045 * Update the voicemail number from what we've recorded on the sim.
1046 */
1047 private void updateVoiceNumberField() {
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001048 if (DBG) log("updateVoiceNumberField()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001049
1050 mOldVmNumber = mPhone.getVoiceMailNumber();
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001051 if (TextUtils.isEmpty(mOldVmNumber)) {
1052 mSubMenuVoicemailSettings.setPhoneNumber("");
1053 mSubMenuVoicemailSettings.setSummary(getString(R.string.voicemail_number_not_set));
1054 } else {
1055 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1056 mSubMenuVoicemailSettings.setSummary(mOldVmNumber);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001057 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001058 }
1059
1060 /*
1061 * Helper Methods for Activity class.
1062 * The initial query commands are split into two pieces now
1063 * for individual expansion. This combined with the ability
1064 * to cancel queries allows for a much better user experience,
1065 * and also ensures that the user only waits to update the
1066 * data that is relevant.
1067 */
1068
1069 @Override
1070 protected void onPrepareDialog(int id, Dialog dialog) {
1071 super.onPrepareDialog(id, dialog);
1072 mCurrentDialogId = id;
1073 }
1074
1075 // dialog creation method, called by showDialog()
1076 @Override
Andrew Lee84024342014-11-06 23:37:09 -08001077 protected Dialog onCreateDialog(int dialogId) {
1078 return VoicemailDialogUtil.getDialog(this, dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001079 }
1080
1081 // This is a method implemented for DialogInterface.OnClickListener.
1082 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1083 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1084 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1085 public void onClick(DialogInterface dialog, int which) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001086 if (DBG) log("onClick: button clicked is " + which);
1087
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001088 dialog.dismiss();
1089 switch (which){
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001090 case DialogInterface.BUTTON_NEGATIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001091 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001092 // We failed to get current forwarding settings and the user
1093 // does not wish to continue.
1094 switchToPreviousVoicemailProvider();
1095 }
1096 break;
1097 case DialogInterface.BUTTON_POSITIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001098 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001099 // We failed to get current forwarding settings but the user
1100 // wishes to continue changing settings to the new vm provider
1101 saveVoiceMailAndForwardingNumberStage2();
1102 } else {
1103 finish();
1104 }
1105 return;
1106 default:
1107 // just let the dialog close and go back to the input
1108 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001109
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001110 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1111 // with settings UI. If we were called to explicitly configure voice mail then
1112 // we finish the settings activity here to come back to whatever the user was doing.
1113 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1114 finish();
1115 }
1116 }
1117
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001118 /*
1119 * Activity class methods
1120 */
1121
1122 @Override
1123 protected void onCreate(Bundle icicle) {
1124 super.onCreate(icicle);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001125 if (DBG) log("onCreate: Intent is " + getIntent());
1126
Jay Shraunerc2684732014-11-12 12:10:37 -08001127 // Make sure we are running as the primary user.
1128 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
1129 Toast.makeText(this, R.string.call_settings_primary_user_only,
1130 Toast.LENGTH_SHORT).show();
1131 finish();
1132 return;
1133 }
1134
Tyler Gunnbaee2952014-09-10 16:01:02 -07001135 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andrew Lee88b51e22014-10-29 15:48:51 -07001136 mVmProviderSettingsUtil = new VoicemailProviderSettingsUtil(getApplicationContext());
Tyler Gunnbaee2952014-09-10 16:01:02 -07001137
Tyler Gunnbaee2952014-09-10 16:01:02 -07001138 // Show the voicemail preference in onResume if the calling intent specifies the
1139 // ACTION_ADD_VOICEMAIL action.
1140 mShowVoicemailPreference = (icicle == null) &&
Jay Shraunerbe2fb262014-11-11 15:19:58 -08001141 TextUtils.equals(getIntent().getAction(), ACTION_ADD_VOICEMAIL);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001142
1143 mSubscriptionInfoHelper = new SubscriptionInfoHelper(getIntent());
1144 mSubscriptionInfoHelper.setActionBarTitle(
1145 getActionBar(), getResources(), R.string.call_settings_with_label);
Andrew Lee1479dd12014-11-06 23:06:32 -08001146 mPhone = mSubscriptionInfoHelper.getPhone();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001147 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001148
1149 private void initPhoneAccountPreferences() {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001150 mPhoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001151
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001152 TelecomManager telecomManager = TelecomManager.from(this);
Andrew Lee93c345f2014-10-27 15:25:07 -07001153 TelephonyManager telephonyManager =
1154 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001155
Andrew Lee93c345f2014-10-27 15:25:07 -07001156 if ((telecomManager.getSimCallManagers().isEmpty() && !SipUtil.isVoipSupported(this))
1157 || telephonyManager.getPhoneCount() > 1) {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001158 getPreferenceScreen().removePreference(mPhoneAccountSettingsPreference);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001159 }
1160 }
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 Leedb2fe562014-09-03 15:40:43 -07001174 initPhoneAccountPreferences();
1175
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001176 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001177 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1178 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1179 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1180 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001181
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001182 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1183 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1184 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
1185 mButtonTTY = (ListPreference) findPreference(BUTTON_TTY_KEY);
1186 mVoicemailProviders = (ListPreference) findPreference(BUTTON_VOICEMAIL_PROVIDER_KEY);
Andrew Lee312e8172014-10-23 17:01:36 -07001187 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001188
Andrew Lee2c027892014-10-29 11:29:54 -07001189 mVoicemailProviders.setOnPreferenceChangeListener(this);
1190 mVoicemailSettingsScreen =
1191 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
1192 mVoicemailSettings = (PreferenceScreen)findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
1193 mVoicemailNotificationVibrate =
1194 (CheckBoxPreference) findPreference(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY);
1195 initVoiceMailProviders();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001196
Andrew Lee64a7d792014-10-15 17:38:38 -07001197 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1198 mButtonDTMF.setOnPreferenceChangeListener(this);
1199 int dtmf = Settings.System.getInt(getContentResolver(),
1200 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1201 mButtonDTMF.setValueIndex(dtmf);
1202 } else {
1203 prefSet.removePreference(mButtonDTMF);
1204 mButtonDTMF = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001205 }
1206
Andrew Lee64a7d792014-10-15 17:38:38 -07001207 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1208 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1209 int autoretry = Settings.Global.getInt(
1210 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1211 mButtonAutoRetry.setChecked(autoretry != 0);
1212 } else {
1213 prefSet.removePreference(mButtonAutoRetry);
1214 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001215 }
1216
Andrew Lee64a7d792014-10-15 17:38:38 -07001217 if (getResources().getBoolean(R.bool.hac_enabled)) {
1218 mButtonHAC.setOnPreferenceChangeListener(this);
1219 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1220 mButtonHAC.setChecked(hac != 0);
1221 } else {
1222 prefSet.removePreference(mButtonHAC);
1223 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001224 }
1225
Andrew Lee64a7d792014-10-15 17:38:38 -07001226 TelecomManager telecomManager = TelecomManager.from(this);
1227 if (telecomManager != null && telecomManager.isTtySupported()) {
1228 mButtonTTY.setOnPreferenceChangeListener(this);
1229 int settingsTtyMode = Settings.Secure.getInt(getContentResolver(),
1230 Settings.Secure.PREFERRED_TTY_MODE,
1231 TelecomManager.TTY_MODE_OFF);
1232 mButtonTTY.setValue(Integer.toString(settingsTtyMode));
1233 updatePreferredTtyModeSummary(settingsTtyMode);
1234 } else {
1235 prefSet.removePreference(mButtonTTY);
1236 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001237 }
1238
1239 if (!getResources().getBoolean(R.bool.world_phone)) {
Andrew Lee2b36ba22014-11-05 17:08:49 -08001240 Preference cdmaOptions = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
1241 prefSet.removePreference(cdmaOptions);
1242
1243 // TODO: Support MSIM for this preference option.
1244 Preference gsmOptions = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
1245 prefSet.removePreference(gsmOptions);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001246
1247 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001248 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001249 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001250 prefSet.removePreference(fdnButton);
1251
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001252 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1253 addPreferencesFromResource(R.xml.cdma_call_privacy);
1254 }
1255 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001256 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(this, FdnSetting.class));
1257
Andrew Lee2170a972014-08-13 18:13:01 -07001258 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
Etan Cohen0ca1c802014-07-07 15:35:48 -07001259 addPreferencesFromResource(R.xml.gsm_umts_call_options);
Andrew Lee2b36ba22014-11-05 17:08:49 -08001260
1261 Preference callForwardingPref = prefSet.findPreference(CALL_FORWARDING_KEY);
1262 callForwardingPref.setIntent(mSubscriptionInfoHelper.getIntent(
1263 this, GsmUmtsCallForwardOptions.class));
1264
1265 Preference additionalGsmSettingsPref =
1266 prefSet.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
1267 additionalGsmSettingsPref.setIntent(mSubscriptionInfoHelper.getIntent(
1268 this, GsmUmtsAdditionalCallOptions.class));
Etan Cohen0ca1c802014-07-07 15:35:48 -07001269 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001270 } else {
1271 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1272 }
1273 }
1274
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001275 // check the intent that started this activity and pop up the voicemail
1276 // dialog if we've been asked to.
1277 // If we have at least one non default VM provider registered then bring up
1278 // the selection for the VM provider, otherwise bring up a VM number dialog.
1279 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001280 if (mShowVoicemailPreference) {
Tyler Gunnbaee2952014-09-10 16:01:02 -07001281 if (DBG) {
1282 log("ACTION_ADD_VOICEMAIL Intent is thrown. current VM data size: "
1283 + mVMProvidersData.size());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001284 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001285 if (mVMProvidersData.size() > 1) {
1286 simulatePreferenceClick(mVoicemailProviders);
1287 } else {
1288 onPreferenceChange(mVoicemailProviders, DEFAULT_VM_PROVIDER_KEY);
1289 mVoicemailProviders.setValue(DEFAULT_VM_PROVIDER_KEY);
1290 }
1291 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001292 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001293
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001294 updateVoiceNumberField();
1295 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001296
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001297 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
1298 mPhone.getContext());
1299 if (migrateVoicemailVibrationSettingsIfNeeded(prefs)) {
1300 mVoicemailNotificationVibrate.setChecked(prefs.getBoolean(
1301 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, false));
1302 }
1303
Andrew Lee312e8172014-10-23 17:01:36 -07001304 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1305 boolean currentValue =
1306 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1307 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1308 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001309 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1310 } else {
1311 prefSet.removePreference(mEnableVideoCalling);
1312 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001313 }
1314
1315 // Migrate settings from BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY to
1316 // BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, if the latter does not exist.
1317 // Returns true if migration was performed.
1318 public static boolean migrateVoicemailVibrationSettingsIfNeeded(SharedPreferences prefs) {
1319 if (!prefs.contains(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY)) {
1320 String vibrateWhen = prefs.getString(
1321 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY, VOICEMAIL_VIBRATION_NEVER);
1322 // If vibrateWhen is always, then voicemailVibrate should be True.
1323 // otherwise if vibrateWhen is "only in silent mode", or "never", then
1324 // voicemailVibrate = False.
1325 boolean voicemailVibrate = vibrateWhen.equals(VOICEMAIL_VIBRATION_ALWAYS);
1326 final SharedPreferences.Editor editor = prefs.edit();
1327 editor.putBoolean(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, voicemailVibrate);
1328 editor.commit();
1329 return true;
1330 }
1331 return false;
1332 }
1333
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001334 private void handleTTYChange(Preference preference, Object objValue) {
1335 int buttonTtyMode;
1336 buttonTtyMode = Integer.valueOf((String) objValue).intValue();
1337 int settingsTtyMode = android.provider.Settings.Secure.getInt(
1338 getContentResolver(),
Sailesh Nepalbf900542014-07-15 16:18:32 -07001339 android.provider.Settings.Secure.PREFERRED_TTY_MODE,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001340 TelecomManager.TTY_MODE_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001341 if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
1342 Integer.toString(buttonTtyMode));
1343
1344 if (buttonTtyMode != settingsTtyMode) {
1345 switch(buttonTtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001346 case TelecomManager.TTY_MODE_OFF:
1347 case TelecomManager.TTY_MODE_FULL:
1348 case TelecomManager.TTY_MODE_HCO:
1349 case TelecomManager.TTY_MODE_VCO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001350 android.provider.Settings.Secure.putInt(getContentResolver(),
1351 android.provider.Settings.Secure.PREFERRED_TTY_MODE, buttonTtyMode);
1352 break;
1353 default:
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001354 buttonTtyMode = TelecomManager.TTY_MODE_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001355 }
1356
1357 mButtonTTY.setValue(Integer.toString(buttonTtyMode));
1358 updatePreferredTtyModeSummary(buttonTtyMode);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001359 Intent ttyModeChanged = new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
1360 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001361 sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
1362 }
1363 }
1364
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001365 private void updatePreferredTtyModeSummary(int TtyMode) {
1366 String [] txts = getResources().getStringArray(R.array.tty_mode_entries);
1367 switch(TtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001368 case TelecomManager.TTY_MODE_OFF:
1369 case TelecomManager.TTY_MODE_HCO:
1370 case TelecomManager.TTY_MODE_VCO:
1371 case TelecomManager.TTY_MODE_FULL:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001372 mButtonTTY.setSummary(txts[TtyMode]);
1373 break;
1374 default:
1375 mButtonTTY.setEnabled(false);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001376 mButtonTTY.setSummary(txts[TelecomManager.TTY_MODE_OFF]);
Sailesh Nepalbf900542014-07-15 16:18:32 -07001377 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001378 }
1379 }
1380
1381 private static void log(String msg) {
1382 Log.d(LOG_TAG, msg);
1383 }
1384
1385 /**
1386 * Updates the look of the VM preference widgets based on current VM provider settings.
1387 * Note that the provider name is loaded form the found activity via loadLabel in
1388 * {@link #initVoiceMailProviders()} in order for it to be localizable.
1389 */
1390 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1391 final String key = currentProviderSetting;
1392 final VoiceMailProvider provider = mVMProvidersData.get(key);
1393
1394 /* This is the case when we are coming up on a freshly wiped phone and there is no
1395 persisted value for the list preference mVoicemailProviders.
1396 In this case we want to show the UI asking the user to select a voicemail provider as
1397 opposed to silently falling back to default one. */
1398 if (provider == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001399 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> null.");
1400
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001401 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1402 mVoicemailSettings.setEnabled(false);
1403 mVoicemailSettings.setIntent(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001404 mVoicemailNotificationVibrate.setEnabled(false);
1405 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001406 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> " + provider.toString());
1407
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001408 final String providerName = provider.name;
1409 mVoicemailProviders.setSummary(providerName);
1410 mVoicemailSettings.setEnabled(true);
1411 mVoicemailSettings.setIntent(provider.intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001412 mVoicemailNotificationVibrate.setEnabled(true);
1413 }
1414 }
1415
1416 /**
1417 * Enumerates existing VM providers and puts their data into the list and populates
1418 * the preference list objects with their names.
1419 * In case we are called with ACTION_ADD_VOICEMAIL intent the intent may have
1420 * an extra string called IGNORE_PROVIDER_EXTRA with "package.activityName" of the provider
1421 * which should be hidden when we bring up the list of possible VM providers to choose.
1422 */
1423 private void initVoiceMailProviders() {
1424 if (DBG) log("initVoiceMailProviders()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001425
1426 String providerToIgnore = null;
Andrew Leef1776d82014-11-04 14:45:02 -08001427 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)
1428 && getIntent().hasExtra(IGNORE_PROVIDER_EXTRA)) {
1429 providerToIgnore = getIntent().getStringExtra(IGNORE_PROVIDER_EXTRA);
1430 // Remove this provider from the list.
1431 if (!TextUtils.isEmpty(providerToIgnore)) {
1432 if (DBG) log("Found ACTION_ADD_VOICEMAIL. providerToIgnore= " + providerToIgnore);
Andrew Lee88b51e22014-10-29 15:48:51 -07001433 mVmProviderSettingsUtil.delete(providerToIgnore);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001434 }
1435 }
1436
1437 mVMProvidersData.clear();
1438
Andrew Leef1776d82014-11-04 14:45:02 -08001439 List<String> entries = new ArrayList<String>();
1440 List<String> values = new ArrayList<String>();
1441
1442 // Add default voicemail provider.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001443 final String myCarrier = getString(R.string.voicemail_default);
1444 mVMProvidersData.put(DEFAULT_VM_PROVIDER_KEY, new VoiceMailProvider(myCarrier, null));
Andrew Leef1776d82014-11-04 14:45:02 -08001445 entries.add(myCarrier);
1446 values.add(DEFAULT_VM_PROVIDER_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001447
Andrew Leef1776d82014-11-04 14:45:02 -08001448 // Add other voicemail providers.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001449 PackageManager pm = getPackageManager();
Andrew Leef1776d82014-11-04 14:45:02 -08001450 Intent intent = new Intent(ACTION_CONFIGURE_VOICEMAIL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001451 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001452 for (int i = 0; i < resolveInfos.size(); i++) {
1453 final ResolveInfo ri= resolveInfos.get(i);
1454 final ActivityInfo currentActivityInfo = ri.activityInfo;
Andrew Lee6214e2b2014-11-04 13:57:38 -08001455 final String key = currentActivityInfo.name;
Andrew Leef1776d82014-11-04 14:45:02 -08001456
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001457 if (key.equals(providerToIgnore)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001458 continue;
1459 }
Andrew Leef1776d82014-11-04 14:45:02 -08001460
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001461 if (DBG) log("Loading key: " + key);
1462 final String nameForDisplay = ri.loadLabel(pm).toString();
1463 Intent providerIntent = new Intent();
1464 providerIntent.setAction(ACTION_CONFIGURE_VOICEMAIL);
Andrew Leef1776d82014-11-04 14:45:02 -08001465 providerIntent.setClassName(currentActivityInfo.packageName, currentActivityInfo.name);
1466 VoiceMailProvider vmProvider = new VoiceMailProvider(nameForDisplay, providerIntent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001467
Andrew Leef1776d82014-11-04 14:45:02 -08001468 if (DBG) log("Store VoiceMailProvider. Key: " + key + " -> " + vmProvider.toString());
1469 mVMProvidersData.put(key, vmProvider);
1470 entries.add(vmProvider.name);
1471 values.add(key);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001472 }
1473
Andrew Leef1776d82014-11-04 14:45:02 -08001474 mVoicemailProviders.setEntries(entries.toArray(new String[0]));
1475 mVoicemailProviders.setEntryValues(values.toArray(new String[0]));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001476
Andrew Leef1776d82014-11-04 14:45:02 -08001477 // Remember the current Voicemail Provider key as a "previous" key. This will be used when
1478 // we fail to update Voicemail Provider, which requires rollback. We will update this when
1479 // the VM Provider setting is successfully updated.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001480 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1481 if (DBG) log("Set up the first mPreviousVMProviderKey: " + mPreviousVMProviderKey);
1482
1483 // Finally update the preference texts.
1484 updateVMPreferenceWidgets(mPreviousVMProviderKey);
1485 }
1486
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001487 /**
1488 * Simulates user clicking on a passed preference.
1489 * Usually needed when the preference is a dialog preference and we want to invoke
1490 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001491 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001492 */
1493 private void simulatePreferenceClick(Preference preference) {
1494 // Go through settings until we find our setting
1495 // and then simulate a click on it to bring up the dialog
1496 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1497 for (int idx = 0; idx < adapter.getCount(); idx++) {
1498 if (adapter.getItem(idx) == preference) {
1499 getPreferenceScreen().onItemClick(this.getListView(),
1500 null, idx, adapter.getItemId(idx));
1501 break;
1502 }
1503 }
1504 }
1505
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001506 private String getCurrentVoicemailProviderKey() {
1507 final String key = mVoicemailProviders.getValue();
1508 return (key != null) ? key : DEFAULT_VM_PROVIDER_KEY;
1509 }
1510
1511 @Override
1512 public boolean onOptionsItemSelected(MenuItem item) {
1513 final int itemId = item.getItemId();
1514 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001515 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001516 return true;
1517 }
1518 return super.onOptionsItemSelected(item);
1519 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001520 /**
1521 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1522 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1523 */
1524 public static void goUpToTopLevelSetting(Activity activity) {
1525 Intent intent = new Intent(activity, CallFeaturesSetting.class);
1526 intent.setAction(Intent.ACTION_MAIN);
1527 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1528 activity.startActivity(intent);
1529 activity.finish();
1530 }
1531}