blob: 18a40362fe8a46f424ae84c196374bcd0417e510 [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.content.pm.ActivityInfo;
29import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
31import android.database.Cursor;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070032import android.media.AudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070033import android.os.AsyncResult;
34import android.os.Bundle;
35import android.os.Handler;
36import android.os.Message;
37import android.os.UserHandle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.preference.CheckBoxPreference;
39import android.preference.ListPreference;
40import android.preference.Preference;
41import android.preference.PreferenceActivity;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.preference.PreferenceScreen;
43import android.provider.ContactsContract.CommonDataKinds;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070045import android.telecom.PhoneAccountHandle;
46import android.telecom.TelecomManager;
Andrew Lee93c345f2014-10-27 15:25:07 -070047import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.text.TextUtils;
49import android.util.Log;
50import android.view.MenuItem;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.widget.ListAdapter;
Jay Shraunerc2684732014-11-12 12:10:37 -080052import android.widget.Toast;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053
Andrew Lee312e8172014-10-23 17:01:36 -070054import com.android.ims.ImsManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import com.android.internal.telephony.CallForwardInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import com.android.internal.telephony.Phone;
57import com.android.internal.telephony.PhoneConstants;
Andrew Lee2170a972014-08-13 18:13:01 -070058import com.android.phone.common.util.SettingsUtil;
Andrew Leedb2fe562014-09-03 15:40:43 -070059import com.android.phone.settings.AccountSelectionPreference;
Andrew Lee1af6cf72014-11-04 17:35:26 -080060import com.android.phone.settings.CallForwardInfoUtil;
Andrew Lee84024342014-11-06 23:37:09 -080061import com.android.phone.settings.VoicemailDialogUtil;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -080062import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Andrew Leeb490d732014-10-27 15:00:41 -070063import com.android.phone.settings.VoicemailProviderSettings;
Andrew Lee88b51e22014-10-29 15:48:51 -070064import com.android.phone.settings.VoicemailProviderSettingsUtil;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -080065import com.android.phone.settings.VoicemailRingtonePreference;
Andrew Lee5ed870c2014-10-29 11:47:49 -070066import com.android.phone.settings.fdn.FdnSetting;
Sailesh Nepal788959e2014-07-08 23:36:40 -070067import com.android.services.telephony.sip.SipUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068
Andrew Lee2170a972014-08-13 18:13:01 -070069import java.lang.String;
Andrew Leef1776d82014-11-04 14:45:02 -080070import java.util.ArrayList;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import java.util.Collection;
72import java.util.HashMap;
73import java.util.HashSet;
74import java.util.Iterator;
75import java.util.List;
76import java.util.Map;
77
78/**
79 * Top level "Call settings" UI; see res/xml/call_feature_setting.xml
80 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070081 * This preference screen is the root of the "Call settings" hierarchy available from the Phone
82 * app; the settings here let you control various features related to phone calls (including
83 * voicemail settings, the "Respond via SMS" feature, and others.) It's used only on
84 * voice-capable phone devices.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070085 *
86 * Note that this activity is part of the package com.android.phone, even
87 * though you reach it from the "Phone" app (i.e. DialtactsActivity) which
88 * is from the package com.android.contacts.
89 *
90 * For the "Mobile network settings" screen under the main Settings app,
91 * See {@link MobileNetworkSettings}.
92 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070093 * TODO: Settings should be split into PreferenceFragments where possible (ie. voicemail).
Andrew Lee2170a972014-08-13 18:13:01 -070094 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095 * @see com.android.phone.MobileNetworkSettings
96 */
97public class CallFeaturesSetting extends PreferenceActivity
98 implements DialogInterface.OnClickListener,
Andrew Lee2170a972014-08-13 18:13:01 -070099 Preference.OnPreferenceChangeListener,
Andrew Lee2170a972014-08-13 18:13:01 -0700100 EditPhoneNumberPreference.OnDialogClosedListener,
Andrew Leece8ae2a2014-09-10 10:41:48 -0700101 EditPhoneNumberPreference.GetDefaultNumberListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102 private static final String LOG_TAG = "CallFeaturesSetting";
103 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
Andrew Lee77527ac2014-10-21 16:57:39 -0700104 // STOPSHIP if true. Flag to override behavior default behavior to hide VT setting.
105 private static final boolean ENABLE_VT_FLAG = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700106
107 /**
108 * Intent action to bring up Voicemail Provider settings.
109 *
110 * @see #IGNORE_PROVIDER_EXTRA
111 */
112 public static final String ACTION_ADD_VOICEMAIL =
113 "com.android.phone.CallFeaturesSetting.ADD_VOICEMAIL";
114 // intent action sent by this activity to a voice mail provider
115 // to trigger its configuration UI
116 public static final String ACTION_CONFIGURE_VOICEMAIL =
117 "com.android.phone.CallFeaturesSetting.CONFIGURE_VOICEMAIL";
118 // Extra put in the return from VM provider config containing voicemail number to set
119 public static final String VM_NUMBER_EXTRA = "com.android.phone.VoicemailNumber";
120 // Extra put in the return from VM provider config containing call forwarding number to set
121 public static final String FWD_NUMBER_EXTRA = "com.android.phone.ForwardingNumber";
122 // Extra put in the return from VM provider config containing call forwarding number to set
123 public static final String FWD_NUMBER_TIME_EXTRA = "com.android.phone.ForwardingNumberTime";
124 // If the VM provider returns non null value in this extra we will force the user to
125 // choose another VM provider
126 public static final String SIGNOUT_EXTRA = "com.android.phone.Signout";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128 // Key identifying the default vocie mail provider
129 public static final String DEFAULT_VM_PROVIDER_KEY = "";
130
131 /**
132 * String Extra put into ACTION_ADD_VOICEMAIL call to indicate which provider should be hidden
133 * in the list of providers presented to the user. This allows a provider which is being
134 * disabled (e.g. GV user logging out) to force the user to pick some other provider.
135 */
136 public static final String IGNORE_PROVIDER_EXTRA = "com.android.phone.ProviderToIgnore";
137
138 // string constants
139 private static final String NUM_PROJECTION[] = {CommonDataKinds.Phone.NUMBER};
140
141 // String keys for preference lookup
142 // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
Andrew Lee8d66d812014-11-24 14:54:02 -0800143 // TODO: Consider moving these strings to strings.xml, so that they are not duplicated here and
144 // in the layout files. These strings need to be treated carefully; if the setting is
145 // persistent, they are used as the key to store shared preferences and the name should not be
146 // changed unless the settings are also migrated.
Andrew Lee97708a42014-09-25 12:39:07 -0700147 private static final String VOICEMAIL_SETTING_SCREEN_PREF_KEY = "button_voicemail_category_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700148 private static final String BUTTON_VOICEMAIL_KEY = "button_voicemail_key";
149 private static final String BUTTON_VOICEMAIL_PROVIDER_KEY = "button_voicemail_provider_key";
150 private static final String BUTTON_VOICEMAIL_SETTING_KEY = "button_voicemail_setting_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700151 private static final String BUTTON_FDN_KEY = "button_fdn_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700152
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700153 private static final String BUTTON_DTMF_KEY = "button_dtmf_settings";
154 private static final String BUTTON_RETRY_KEY = "button_auto_retry_key";
155 private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
156 private static final String BUTTON_HAC_KEY = "button_hac_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700157
158 private static final String BUTTON_GSM_UMTS_OPTIONS = "button_gsm_more_expand_key";
159 private static final String BUTTON_CDMA_OPTIONS = "button_cdma_more_expand_key";
Andrew Lee2b36ba22014-11-05 17:08:49 -0800160 private static final String CALL_FORWARDING_KEY = "call_forwarding_key";
161 private static final String ADDITIONAL_GSM_SETTINGS_KEY = "additional_gsm_call_settings_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700162
Andrew Leece8ae2a2014-09-10 10:41:48 -0700163 private static final String PHONE_ACCOUNT_SETTINGS_KEY =
164 "phone_account_settings_preference_screen";
Andrew Leedb2fe562014-09-03 15:40:43 -0700165
Andrew Leedf14ead2014-10-17 14:22:52 -0700166 private static final String ENABLE_VIDEO_CALLING_KEY = "button_enable_video_calling";
167
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168 /** Event for Async voicemail change call */
169 private static final int EVENT_VOICEMAIL_CHANGED = 500;
170 private static final int EVENT_FORWARDING_CHANGED = 501;
171 private static final int EVENT_FORWARDING_GET_COMPLETED = 502;
172
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700173 public static final String HAC_KEY = "HACSetting";
174 public static final String HAC_VAL_ON = "ON";
175 public static final String HAC_VAL_OFF = "OFF";
176
177 /** Handle to voicemail pref */
178 private static final int VOICEMAIL_PREF_ID = 1;
179 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
180
181 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700182 private AudioManager mAudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700183
Andrew Lee5ed870c2014-10-29 11:47:49 -0700184 private SubscriptionInfoHelper mSubscriptionInfoHelper;
185
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700186 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
187
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188 /** Whether dialpad plays DTMF tone or not. */
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189 private CheckBoxPreference mButtonAutoRetry;
190 private CheckBoxPreference mButtonHAC;
191 private ListPreference mButtonDTMF;
192 private ListPreference mButtonTTY;
Andrew Leece8ae2a2014-09-10 10:41:48 -0700193 private Preference mPhoneAccountSettingsPreference;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700194 private ListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700195 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700196 private PreferenceScreen mVoicemailSettings;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800197 private VoicemailRingtonePreference mVoicemailNotificationRingtone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700199 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200
201 private class VoiceMailProvider {
Andrew Leef1776d82014-11-04 14:45:02 -0800202 public String name;
203 public Intent intent;
204
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700205 public VoiceMailProvider(String name, Intent intent) {
206 this.name = name;
207 this.intent = intent;
208 }
Andrew Leef1776d82014-11-04 14:45:02 -0800209
210 public String toString() {
211 return "[ Name: " + name + ", Intent: " + intent + " ]";
212 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700213 }
214
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700215 /**
216 * Results of reading forwarding settings
217 */
218 private CallForwardInfo[] mForwardingReadResults = null;
219
220 /**
221 * Result of forwarding number change.
222 * Keys are reasons (eg. unconditional forwarding).
223 */
224 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
225
226 /**
227 * Expected CF read result types.
228 * This set keeps track of the CF types for which we've issued change
229 * commands so we can tell when we've received all of the responses.
230 */
231 private Collection<Integer> mExpectedChangeResultReasons = null;
232
233 /**
234 * Result of vm number change
235 */
236 private AsyncResult mVoicemailChangeResult = null;
237
238 /**
239 * Previous VM provider setting so we can return to it in case of failure.
240 */
241 private String mPreviousVMProviderKey = null;
242
243 /**
244 * Id of the dialog being currently shown.
245 */
246 private int mCurrentDialogId = 0;
247
248 /**
249 * Flag indicating that we are invoking settings for the voicemail provider programmatically
250 * due to vm provider change.
251 */
252 private boolean mVMProviderSettingsForced = false;
253
254 /**
255 * Flag indicating that we are making changes to vm or fwd numbers
256 * due to vm provider change.
257 */
258 private boolean mChangingVMorFwdDueToProviderChange = false;
259
260 /**
261 * True if we are in the process of vm & fwd number change and vm has already been changed.
262 * This is used to decide what to do in case of rollback.
263 */
264 private boolean mVMChangeCompletedSuccessfully = false;
265
266 /**
267 * True if we had full or partial failure setting forwarding numbers and so need to roll them
268 * back.
269 */
270 private boolean mFwdChangesRequireRollback = false;
271
272 /**
273 * Id of error msg to display to user once we are done reverting the VM provider to the previous
274 * one.
275 */
276 private int mVMOrFwdSetError = 0;
277
278 /**
279 * Data about discovered voice mail settings providers.
280 * Is populated by querying which activities can handle ACTION_CONFIGURE_VOICEMAIL.
281 * They key in this map is package name + activity name.
282 * We always add an entry for the default provider with a key of empty
283 * string and intent value of null.
284 * @see #initVoiceMailProviders()
285 */
286 private final Map<String, VoiceMailProvider> mVMProvidersData =
287 new HashMap<String, VoiceMailProvider>();
288
289 /** string to hold old voicemail number as it is being updated. */
290 private String mOldVmNumber;
291
292 // New call forwarding settings and vm number we will be setting
293 // Need to save these since before we get to saving we need to asynchronously
294 // query the existing forwarding settings.
295 private CallForwardInfo[] mNewFwdSettings;
296 private String mNewVMNumber;
297
298 private boolean mForeground;
299
300 @Override
301 public void onPause() {
302 super.onPause();
303 mForeground = false;
304 }
305
306 /**
307 * We have to pull current settings from the network for all kinds of
308 * voicemail providers so we can tell whether we have to update them,
309 * so use this bit to keep track of whether we're reading settings for the
310 * default provider and should therefore save them out when done.
311 */
312 private boolean mReadingSettingsForDefaultProvider = false;
313
Tyler Gunnbaee2952014-09-10 16:01:02 -0700314 /**
315 * Used to indicate that the voicemail preference should be shown.
316 */
317 private boolean mShowVoicemailPreference = false;
318
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700319 /*
320 * Click Listeners, handle click based on objects attached to UI.
321 */
322
323 // Click listener for all toggle events
324 @Override
325 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
326 if (preference == mSubMenuVoicemailSettings) {
327 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700328 } else if (preference == mButtonDTMF) {
329 return true;
330 } else if (preference == mButtonTTY) {
331 return true;
332 } else if (preference == mButtonAutoRetry) {
333 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
334 android.provider.Settings.Global.CALL_AUTO_RETRY,
335 mButtonAutoRetry.isChecked() ? 1 : 0);
336 return true;
337 } else if (preference == mButtonHAC) {
338 int hac = mButtonHAC.isChecked() ? 1 : 0;
339 // Update HAC value in Settings database
340 Settings.System.putInt(mPhone.getContext().getContentResolver(),
341 Settings.System.HEARING_AID, hac);
342
343 // Update HAC Value in AudioManager
344 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
345 return true;
346 } else if (preference == mVoicemailSettings) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800347 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
348
Yorke Leea0f63bf2014-10-09 18:27:20 -0700349 final Dialog dialog = mVoicemailSettings.getDialog();
350 if (dialog != null) {
351 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
352 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800353
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700354 if (preference.getIntent() != null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800355 if (DBG) log("Invoking cfg intent " + preference.getIntent().getPackage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700356
357 // onActivityResult() will be responsible for resetting some of variables.
358 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
359 return true;
360 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800361 if (DBG) log("onPreferenceTreeClick(). No intent; use default behavior in xml.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700362
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800363 // onActivityResult() will not be called, so reset variables here.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700364 mPreviousVMProviderKey = DEFAULT_VM_PROVIDER_KEY;
365 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700366 return false;
367 }
Andrew Lee97708a42014-09-25 12:39:07 -0700368 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700369 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
370 if (dialog != null) {
371 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
372 }
Andrew Lee97708a42014-09-25 12:39:07 -0700373 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700374 }
375 return false;
376 }
377
378 /**
379 * Implemented to support onPreferenceChangeListener to look for preference
380 * changes.
381 *
382 * @param preference is the preference to be changed
383 * @param objValue should be the value of the selection, NOT its localized
384 * display value.
385 */
386 @Override
387 public boolean onPreferenceChange(Preference preference, Object objValue) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800388 if (DBG) log("onPreferenceChange: \"" + preference + "\" changed to \"" + objValue + "\"");
Andrew Lee2170a972014-08-13 18:13:01 -0700389
390 if (preference == mButtonDTMF) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700391 int index = mButtonDTMF.findIndexOfValue((String) objValue);
392 Settings.System.putInt(mPhone.getContext().getContentResolver(),
393 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
394 } else if (preference == mButtonTTY) {
395 handleTTYChange(preference, objValue);
396 } else if (preference == mVoicemailProviders) {
397 final String newProviderKey = (String) objValue;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800398
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700399 // If previous provider key and the new one is same, we don't need to handle it.
400 if (mPreviousVMProviderKey.equals(newProviderKey)) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800401 if (DBG) log("No change is made to the VM provider setting.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700402 return true;
403 }
404 updateVMPreferenceWidgets(newProviderKey);
405
Andrew Leeb490d732014-10-27 15:00:41 -0700406 final VoicemailProviderSettings newProviderSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800407 VoicemailProviderSettingsUtil.load(this, newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700408
Andrew Lee88b51e22014-10-29 15:48:51 -0700409 // If the user switches to a voice mail provider and we have numbers stored for it we
410 // will automatically change the phone's voice mail and forwarding number to the stored
411 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700412 if (newProviderSettings == null) {
413 // Force the user into a configuration of the chosen provider
414 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
415 mVMProviderSettingsForced = true;
416 simulatePreferenceClick(mVoicemailSettings);
417 } else {
418 if (DBG) log("Saved preferences found - switching to them");
419 // Set this flag so if we get a failure we revert to previous provider
420 mChangingVMorFwdDueToProviderChange = true;
421 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
422 }
Andrew Lee8d66d812014-11-24 14:54:02 -0800423 } else if (preference.getKey().equals(mVoicemailNotificationVibrate.getKey())) {
424 VoicemailNotificationSettingsUtil.setVibrationEnabled(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800425 mPhone, Boolean.TRUE.equals(objValue));
Andrew Leedf14ead2014-10-17 14:22:52 -0700426 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700427 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
428 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
429 } else {
430 AlertDialog.Builder builder = new AlertDialog.Builder(this);
431 DialogInterface.OnClickListener networkSettingsClickListener =
432 new Dialog.OnClickListener() {
433 @Override
434 public void onClick(DialogInterface dialog, int which) {
435 startActivity(new Intent(mPhone.getContext(),
436 com.android.phone.MobileNetworkSettings.class));
437 }
438 };
439 builder.setMessage(getResources().getString(
440 R.string.enable_video_calling_dialog_msg))
441 .setNeutralButton(getResources().getString(
442 R.string.enable_video_calling_dialog_settings),
443 networkSettingsClickListener)
444 .setPositiveButton(android.R.string.ok, null)
445 .show();
446 return false;
447 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700448 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800449
450 // Always let the preference setting proceed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700451 return true;
452 }
453
454 @Override
455 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800456 if (DBG) log("onDialogClosed: Button clicked is " + buttonClicked);
457
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700458 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
459 return;
460 }
461
462 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700463 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
464 mSubMenuVoicemailSettings.getPhoneNumber(),
465 VoicemailProviderSettings.NO_FORWARDING);
466 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700467 }
468 }
469
470 /**
471 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
472 * This method set the default values for the various
473 * EditPhoneNumberPreference dialogs.
474 */
475 @Override
476 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
477 if (preference == mSubMenuVoicemailSettings) {
478 // update the voicemail number field, which takes care of the
479 // mSubMenuVoicemailSettings itself, so we should return null.
480 if (DBG) log("updating default for voicemail dialog");
481 updateVoiceNumberField();
482 return null;
483 }
484
485 String vmDisplay = mPhone.getVoiceMailNumber();
486 if (TextUtils.isEmpty(vmDisplay)) {
487 // if there is no voicemail number, we just return null to
488 // indicate no contribution.
489 return null;
490 }
491
492 // Return the voicemail number prepended with "VM: "
493 if (DBG) log("updating default for call forwarding dialogs");
494 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
495 }
496
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700497 private void switchToPreviousVoicemailProvider() {
498 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700499
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800500 if (mPreviousVMProviderKey == null) {
501 return;
502 }
503
504 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
Andrew Lee84024342014-11-06 23:37:09 -0800505 showDialogIfForeground(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800506 final VoicemailProviderSettings prevSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800507 VoicemailProviderSettingsUtil.load(this, mPreviousVMProviderKey);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800508 if (prevSettings == null) {
509 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
510 + mPreviousVMProviderKey + "\" is null but should be loaded.");
511 }
512
513 if (mVMChangeCompletedSuccessfully) {
514 mNewVMNumber = prevSettings.getVoicemailNumber();
515 Log.i(LOG_TAG, "VM change is already completed successfully."
516 + "Have to revert VM back to " + mNewVMNumber + " again.");
517 mPhone.setVoiceMailNumber(
518 mPhone.getVoiceMailAlphaTag().toString(),
519 mNewVMNumber,
520 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
521 }
522
523 if (mFwdChangesRequireRollback) {
524 Log.i(LOG_TAG, "Requested to rollback forwarding changes.");
525
526 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
527 if (prevFwdSettings != null) {
528 Map<Integer, AsyncResult> results = mForwardingChangeResults;
529 resetForwardingChangeState();
530 for (int i = 0; i < prevFwdSettings.length; i++) {
531 CallForwardInfo fi = prevFwdSettings[i];
532 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
533 // Only revert the settings for which the update succeeded.
534 AsyncResult result = results.get(fi.reason);
535 if (result != null && result.exception == null) {
536 mExpectedChangeResultReasons.add(fi.reason);
537 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
538 mRevertOptionComplete.obtainMessage(
539 EVENT_FORWARDING_CHANGED, i, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700540 }
541 }
542 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700543 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800544 } else {
545 if (DBG) log("No need to revert");
546 onRevertDone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700547 }
548 }
549
550 private void onRevertDone() {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800551 if (DBG) log("onRevertDone: Changing provider key back to " + mPreviousVMProviderKey);
552
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700553 updateVMPreferenceWidgets(mPreviousVMProviderKey);
554 updateVoiceNumberField();
555 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800556 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700557 mVMOrFwdSetError = 0;
558 }
559 }
560
561 @Override
562 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
563 if (DBG) {
564 log("onActivityResult: requestCode: " + requestCode
565 + ", resultCode: " + resultCode
566 + ", data: " + data);
567 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800568
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700569 // there are cases where the contact picker may end up sending us more than one
570 // request. We want to ignore the request if we're not in the correct state.
571 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
572 boolean failure = false;
573
574 // No matter how the processing of result goes lets clear the flag
575 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
576 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
577 mVMProviderSettingsForced = false;
578
579 String vmNum = null;
580 if (resultCode != RESULT_OK) {
581 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
582 failure = true;
583 } else {
584 if (data == null) {
585 if (DBG) log("onActivityResult: vm provider cfg result has no data");
586 failure = true;
587 } else {
588 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
589 if (DBG) log("Provider requested signout");
590 if (isVMProviderSettingsForced) {
591 if (DBG) log("Going back to previous provider on signout");
592 switchToPreviousVoicemailProvider();
593 } else {
594 final String victim = getCurrentVoicemailProviderKey();
595 if (DBG) log("Relaunching activity and ignoring " + victim);
596 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
597 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
598 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
599 this.startActivity(i);
600 }
601 return;
602 }
603 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
604 if (vmNum == null || vmNum.length() == 0) {
605 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
606 failure = true;
607 }
608 }
609 }
610 if (failure) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800611 if (DBG) log("Failure in return from voicemail provider.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700612 if (isVMProviderSettingsForced) {
613 switchToPreviousVoicemailProvider();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700614 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800615
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700616 return;
617 }
618 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
619 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
620
Santos Cordonda120f42014-08-06 04:44:34 -0700621 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700622 // send it to the provider when it's config is invoked so it can use this as default
623 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
624
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800625 if (DBG) log("onActivityResult: cfg result has forwarding number " + fwdNum);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700626 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700627 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700628 return;
629 }
630
631 if (requestCode == VOICEMAIL_PREF_ID) {
632 if (resultCode != RESULT_OK) {
633 if (DBG) log("onActivityResult: contact picker result not OK.");
634 return;
635 }
636
637 Cursor cursor = null;
638 try {
639 cursor = getContentResolver().query(data.getData(),
640 NUM_PROJECTION, null, null, null);
641 if ((cursor == null) || (!cursor.moveToFirst())) {
642 if (DBG) log("onActivityResult: bad contact data, no results found.");
643 return;
644 }
645 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
646 return;
647 } finally {
648 if (cursor != null) {
649 cursor.close();
650 }
651 }
652 }
653
654 super.onActivityResult(requestCode, resultCode, data);
655 }
656
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700657 /**
658 * Wrapper around showDialog() that will silently do nothing if we're
659 * not in the foreground.
660 *
661 * This is useful here because most of the dialogs we display from
662 * this class are triggered by asynchronous events (like
663 * success/failure messages from the telephony layer) and it's
664 * possible for those events to come in even after the user has gone
665 * to a different screen.
666 */
667 // TODO: this is too brittle: it's still easy to accidentally add new
668 // code here that calls showDialog() directly (which will result in a
669 // WindowManager$BadTokenException if called after the activity has
670 // been stopped.)
671 //
672 // It would be cleaner to do the "if (mForeground)" check in one
673 // central place, maybe by using a single Handler for all asynchronous
674 // events (and have *that* discard events if we're not in the
675 // foreground.)
676 //
677 // Unfortunately it's not that simple, since we sometimes need to do
678 // actual work to handle these events whether or not we're in the
679 // foreground (see the Handler code in mSetOptionComplete for
680 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800681 //
682 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
683 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700684 private void showDialogIfForeground(int id) {
685 if (mForeground) {
686 showDialog(id);
687 }
688 }
689
690 private void dismissDialogSafely(int id) {
691 try {
692 dismissDialog(id);
693 } catch (IllegalArgumentException e) {
694 // This is expected in the case where we were in the background
695 // at the time we would normally have shown the dialog, so we didn't
696 // show it.
697 }
698 }
699
Andrew Leeb490d732014-10-27 15:00:41 -0700700 private void saveVoiceMailAndForwardingNumber(
701 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700702 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700703 mNewVMNumber = newSettings.getVoicemailNumber();
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800704 mNewVMNumber = (mNewVMNumber == null) ? "" : mNewVMNumber;
Andrew Leeb490d732014-10-27 15:00:41 -0700705 mNewFwdSettings = newSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700706
707 // No fwd settings on CDMA
708 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
709 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700710 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700711 }
712
Andrew Leee3c15212014-10-28 13:12:55 -0700713 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700714 if (mNewVMNumber.equals(mOldVmNumber)
715 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Lee84024342014-11-06 23:37:09 -0800716 showDialogIfForeground(VoicemailDialogUtil.VM_NOCHANGE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700717 return;
718 }
719
Andrew Lee6950c6c2014-11-26 12:05:42 -0800720 VoicemailProviderSettingsUtil.save(this, key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700721 mVMChangeCompletedSuccessfully = false;
722 mFwdChangesRequireRollback = false;
723 mVMOrFwdSetError = 0;
724 if (!key.equals(mPreviousVMProviderKey)) {
725 mReadingSettingsForDefaultProvider =
726 mPreviousVMProviderKey.equals(DEFAULT_VM_PROVIDER_KEY);
727 if (DBG) log("Reading current forwarding settings");
Andrew Leeb490d732014-10-27 15:00:41 -0700728 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
729 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
730 for (int i = 0; i < mForwardingReadResults.length; i++) {
731 mPhone.getCallForwardingOption(
732 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700733 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
734 }
Andrew Lee84024342014-11-06 23:37:09 -0800735 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700736 } else {
737 saveVoiceMailAndForwardingNumberStage2();
738 }
739 }
740
741 private final Handler mGetOptionComplete = new Handler() {
742 @Override
743 public void handleMessage(Message msg) {
744 AsyncResult result = (AsyncResult) msg.obj;
745 switch (msg.what) {
746 case EVENT_FORWARDING_GET_COMPLETED:
747 handleForwardingSettingsReadResult(result, msg.arg1);
748 break;
749 }
750 }
751 };
752
753 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
754 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800755
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700756 Throwable error = null;
757 if (ar.exception != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700758 error = ar.exception;
Andrew Lee1af6cf72014-11-04 17:35:26 -0800759 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700760 }
761 if (ar.userObj instanceof Throwable) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800762 error = (Throwable) ar.userObj;
763 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700764 }
765
766 // We may have already gotten an error and decided to ignore the other results.
767 if (mForwardingReadResults == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800768 if (DBG) Log.d(LOG_TAG, "Ignoring fwd reading result: " + idx);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700769 return;
770 }
771
772 // In case of error ignore other results, show an error dialog
773 if (error != null) {
774 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
775 mForwardingReadResults = null;
Andrew Lee84024342014-11-06 23:37:09 -0800776 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
777 showDialogIfForeground(VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700778 return;
779 }
780
Andrew Lee1af6cf72014-11-04 17:35:26 -0800781 // Get the forwarding info.
782 mForwardingReadResults[idx] = CallForwardInfoUtil.getCallForwardInfo(
783 (CallForwardInfo[]) ar.result,
784 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx]);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700785
786 // Check if we got all the results already
787 boolean done = true;
788 for (int i = 0; i < mForwardingReadResults.length; i++) {
789 if (mForwardingReadResults[i] == null) {
790 done = false;
791 break;
792 }
793 }
Andrew Lee1af6cf72014-11-04 17:35:26 -0800794
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700795 if (done) {
796 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
Andrew Lee84024342014-11-06 23:37:09 -0800797 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800798
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700799 if (mReadingSettingsForDefaultProvider) {
Andrew Lee6950c6c2014-11-26 12:05:42 -0800800 VoicemailProviderSettingsUtil.save(this, DEFAULT_VM_PROVIDER_KEY,
Andrew Lee88b51e22014-10-29 15:48:51 -0700801 new VoicemailProviderSettings(this.mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700802 mReadingSettingsForDefaultProvider = false;
803 }
804 saveVoiceMailAndForwardingNumberStage2();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700805 }
806 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800807
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700808 private void resetForwardingChangeState() {
809 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
810 mExpectedChangeResultReasons = new HashSet<Integer>();
811 }
812
813 // Called after we are done saving the previous forwarding settings if
814 // we needed.
815 private void saveVoiceMailAndForwardingNumberStage2() {
816 mForwardingChangeResults = null;
817 mVoicemailChangeResult = null;
Andrew Leeb490d732014-10-27 15:00:41 -0700818 if (mNewFwdSettings != VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700819 resetForwardingChangeState();
820 for (int i = 0; i < mNewFwdSettings.length; i++) {
821 CallForwardInfo fi = mNewFwdSettings[i];
Andrew Lee1af6cf72014-11-04 17:35:26 -0800822 CallForwardInfo fiForReason =
823 CallForwardInfoUtil.infoForReason(mForwardingReadResults, fi.reason);
824 final boolean doUpdate = CallForwardInfoUtil.isUpdateRequired(fiForReason, fi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700825
826 if (doUpdate) {
827 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
828 mExpectedChangeResultReasons.add(i);
829
Andrew Lee1af6cf72014-11-04 17:35:26 -0800830 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700831 mSetOptionComplete.obtainMessage(
832 EVENT_FORWARDING_CHANGED, fi.reason, 0));
833 }
834 }
Andrew Lee84024342014-11-06 23:37:09 -0800835 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700836 } else {
837 if (DBG) log("Not touching fwd #");
838 setVMNumberWithCarrier();
839 }
840 }
841
842 private void setVMNumberWithCarrier() {
843 if (DBG) log("save voicemail #: " + mNewVMNumber);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800844
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700845 mPhone.setVoiceMailNumber(
846 mPhone.getVoiceMailAlphaTag().toString(),
847 mNewVMNumber,
848 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
849 }
850
851 /**
852 * Callback to handle option update completions
853 */
854 private final Handler mSetOptionComplete = new Handler() {
855 @Override
856 public void handleMessage(Message msg) {
857 AsyncResult result = (AsyncResult) msg.obj;
858 boolean done = false;
859 switch (msg.what) {
860 case EVENT_VOICEMAIL_CHANGED:
861 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700862 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700863 done = true;
864 break;
865 case EVENT_FORWARDING_CHANGED:
866 mForwardingChangeResults.put(msg.arg1, result);
867 if (result.exception != null) {
868 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
869 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700870 }
Andrew Leee438b312014-10-29 16:59:15 -0700871 if (isForwardingCompleted()) {
872 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700873 if (DBG) log("Overall fwd changes completed ok, starting vm change");
874 setVMNumberWithCarrier();
875 } else {
876 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
877 "Check if we need to try rollback for some settings.");
878 mFwdChangesRequireRollback = false;
879 Iterator<Map.Entry<Integer,AsyncResult>> it =
880 mForwardingChangeResults.entrySet().iterator();
881 while (it.hasNext()) {
882 Map.Entry<Integer,AsyncResult> entry = it.next();
883 if (entry.getValue().exception == null) {
884 // If at least one succeeded we have to revert
885 Log.i(LOG_TAG, "Rollback will be required");
886 mFwdChangesRequireRollback = true;
887 break;
888 }
889 }
890 if (!mFwdChangesRequireRollback) {
891 Log.i(LOG_TAG, "No rollback needed.");
892 }
893 done = true;
894 }
895 }
896 break;
897 default:
898 // TODO: should never reach this, may want to throw exception
899 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800900
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700901 if (done) {
902 if (DBG) log("All VM provider related changes done");
903 if (mForwardingChangeResults != null) {
Andrew Lee84024342014-11-06 23:37:09 -0800904 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700905 }
Andrew Leee438b312014-10-29 16:59:15 -0700906 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700907 }
908 }
909 };
910
911 /**
912 * Callback to handle option revert completions
913 */
914 private final Handler mRevertOptionComplete = new Handler() {
915 @Override
916 public void handleMessage(Message msg) {
917 AsyncResult result = (AsyncResult) msg.obj;
918 switch (msg.what) {
919 case EVENT_VOICEMAIL_CHANGED:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700920 if (DBG) log("VM revert complete msg");
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800921 mVoicemailChangeResult = result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700922 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800923
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700924 case EVENT_FORWARDING_CHANGED:
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800925 if (DBG) log("FWD revert complete msg ");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700926 mForwardingChangeResults.put(msg.arg1, result);
927 if (result.exception != null) {
928 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
929 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700930 }
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 default:
934 // TODO: should never reach this, may want to throw exception
935 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800936
937 final boolean done = (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null)
938 && (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700939 if (done) {
940 if (DBG) log("All VM reverts done");
Andrew Lee84024342014-11-06 23:37:09 -0800941 dismissDialogSafely(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700942 onRevertDone();
943 }
944 }
945 };
946
947 /**
Andrew Leee438b312014-10-29 16:59:15 -0700948 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700949 */
Andrew Leee438b312014-10-29 16:59:15 -0700950 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700951 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -0700952 return true;
953 }
954
955 for (Integer reason : mExpectedChangeResultReasons) {
956 if (mForwardingChangeResults.get(reason) == null) {
957 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700958 }
959 }
Andrew Leee438b312014-10-29 16:59:15 -0700960
961 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700962 }
Andrew Leee438b312014-10-29 16:59:15 -0700963
964 private boolean isFwdChangeSuccess() {
965 if (mForwardingChangeResults == null) {
966 return true;
967 }
968
969 for (AsyncResult result : mForwardingChangeResults.values()) {
970 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700971 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700972 String msg = exception.getMessage();
973 msg = (msg != null) ? msg : "";
974 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
975 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700976 }
977 }
Andrew Leee438b312014-10-29 16:59:15 -0700978 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700979 }
980
Andrew Leee438b312014-10-29 16:59:15 -0700981 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700982 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700983 String msg = mVoicemailChangeResult.exception.getMessage();
984 msg = (msg != null) ? msg : "";
985 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
986 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700987 }
Andrew Leee438b312014-10-29 16:59:15 -0700988 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700989 }
990
Andrew Leee438b312014-10-29 16:59:15 -0700991 private void handleSetVmOrFwdMessage() {
992 if (DBG) log("handleSetVMMessage: set VM request complete");
993
994 if (!isFwdChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -0800995 handleVmOrFwdSetError(VoicemailDialogUtil.FWD_SET_RESPONSE_ERROR_DIALOG);
Andrew Leee438b312014-10-29 16:59:15 -0700996 } else if (!isVmChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -0800997 handleVmOrFwdSetError(VoicemailDialogUtil.VM_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700998 } else {
Andrew Lee84024342014-11-06 23:37:09 -0800999 handleVmAndFwdSetSuccess(VoicemailDialogUtil.VM_CONFIRM_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001000 }
1001 }
1002
1003 /**
1004 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
1005 * changes to those settings and show "failure" dialog.
1006 *
Andrew Leeab082272014-11-04 15:50:42 -08001007 * @param dialogId ID of the dialog to show for the specific error case. Either
Andrew Lee84024342014-11-06 23:37:09 -08001008 * {@link #FWD_SET_RESPONSE_ERROR_DIALOG} or {@link #VM_RESPONSE_ERROR_DIALOG}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001009 */
Andrew Leeab082272014-11-04 15:50:42 -08001010 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001011 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -08001012 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001013 mChangingVMorFwdDueToProviderChange = false;
1014 switchToPreviousVoicemailProvider();
1015 return;
1016 }
1017 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001018 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001019 updateVoiceNumberField();
1020 }
1021
1022 /**
1023 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1024 * This updates a bunch of variables and show "success" dialog.
1025 */
Andrew Leeab082272014-11-04 15:50:42 -08001026 private void handleVmAndFwdSetSuccess(int dialogId) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001027 if (DBG) log("handleVmAndFwdSetSuccess: key is " + getCurrentVoicemailProviderKey());
1028
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001029 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1030 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001031 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001032 updateVoiceNumberField();
1033 }
1034
1035 /**
1036 * Update the voicemail number from what we've recorded on the sim.
1037 */
1038 private void updateVoiceNumberField() {
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001039 if (DBG) log("updateVoiceNumberField()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001040
1041 mOldVmNumber = mPhone.getVoiceMailNumber();
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001042 if (TextUtils.isEmpty(mOldVmNumber)) {
1043 mSubMenuVoicemailSettings.setPhoneNumber("");
1044 mSubMenuVoicemailSettings.setSummary(getString(R.string.voicemail_number_not_set));
1045 } else {
1046 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1047 mSubMenuVoicemailSettings.setSummary(mOldVmNumber);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001048 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001049 }
1050
1051 /*
1052 * Helper Methods for Activity class.
1053 * The initial query commands are split into two pieces now
1054 * for individual expansion. This combined with the ability
1055 * to cancel queries allows for a much better user experience,
1056 * and also ensures that the user only waits to update the
1057 * data that is relevant.
1058 */
1059
1060 @Override
1061 protected void onPrepareDialog(int id, Dialog dialog) {
1062 super.onPrepareDialog(id, dialog);
1063 mCurrentDialogId = id;
1064 }
1065
1066 // dialog creation method, called by showDialog()
1067 @Override
Andrew Lee84024342014-11-06 23:37:09 -08001068 protected Dialog onCreateDialog(int dialogId) {
1069 return VoicemailDialogUtil.getDialog(this, dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001070 }
1071
1072 // This is a method implemented for DialogInterface.OnClickListener.
1073 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1074 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1075 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1076 public void onClick(DialogInterface dialog, int which) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001077 if (DBG) log("onClick: button clicked is " + which);
1078
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001079 dialog.dismiss();
1080 switch (which){
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001081 case DialogInterface.BUTTON_NEGATIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001082 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001083 // We failed to get current forwarding settings and the user
1084 // does not wish to continue.
1085 switchToPreviousVoicemailProvider();
1086 }
1087 break;
1088 case DialogInterface.BUTTON_POSITIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001089 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001090 // We failed to get current forwarding settings but the user
1091 // wishes to continue changing settings to the new vm provider
1092 saveVoiceMailAndForwardingNumberStage2();
1093 } else {
1094 finish();
1095 }
1096 return;
1097 default:
1098 // just let the dialog close and go back to the input
1099 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001100
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001101 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1102 // with settings UI. If we were called to explicitly configure voice mail then
1103 // we finish the settings activity here to come back to whatever the user was doing.
1104 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1105 finish();
1106 }
1107 }
1108
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001109 /*
1110 * Activity class methods
1111 */
1112
1113 @Override
1114 protected void onCreate(Bundle icicle) {
1115 super.onCreate(icicle);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001116 if (DBG) log("onCreate: Intent is " + getIntent());
1117
Jay Shraunerc2684732014-11-12 12:10:37 -08001118 // Make sure we are running as the primary user.
1119 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
1120 Toast.makeText(this, R.string.call_settings_primary_user_only,
1121 Toast.LENGTH_SHORT).show();
1122 finish();
1123 return;
1124 }
1125
Tyler Gunnbaee2952014-09-10 16:01:02 -07001126 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
1127
Tyler Gunnbaee2952014-09-10 16:01:02 -07001128 // Show the voicemail preference in onResume if the calling intent specifies the
1129 // ACTION_ADD_VOICEMAIL action.
1130 mShowVoicemailPreference = (icicle == null) &&
Jay Shraunerbe2fb262014-11-11 15:19:58 -08001131 TextUtils.equals(getIntent().getAction(), ACTION_ADD_VOICEMAIL);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001132
1133 mSubscriptionInfoHelper = new SubscriptionInfoHelper(getIntent());
1134 mSubscriptionInfoHelper.setActionBarTitle(
1135 getActionBar(), getResources(), R.string.call_settings_with_label);
Andrew Lee1479dd12014-11-06 23:06:32 -08001136 mPhone = mSubscriptionInfoHelper.getPhone();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001137 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001138
1139 private void initPhoneAccountPreferences() {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001140 mPhoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001141
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001142 TelecomManager telecomManager = TelecomManager.from(this);
Andrew Lee93c345f2014-10-27 15:25:07 -07001143 TelephonyManager telephonyManager =
1144 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001145
Andrew Lee93c345f2014-10-27 15:25:07 -07001146 if ((telecomManager.getSimCallManagers().isEmpty() && !SipUtil.isVoipSupported(this))
1147 || telephonyManager.getPhoneCount() > 1) {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001148 getPreferenceScreen().removePreference(mPhoneAccountSettingsPreference);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001149 }
1150 }
1151
Tyler Gunnbaee2952014-09-10 16:01:02 -07001152 @Override
1153 protected void onResume() {
1154 super.onResume();
1155 mForeground = true;
1156
1157 PreferenceScreen preferenceScreen = getPreferenceScreen();
1158 if (preferenceScreen != null) {
1159 preferenceScreen.removeAll();
1160 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001161
1162 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001163
Andrew Leedb2fe562014-09-03 15:40:43 -07001164 initPhoneAccountPreferences();
1165
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001166 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001167 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1168 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1169 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1170 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001171
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001172 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1173 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1174 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
1175 mButtonTTY = (ListPreference) findPreference(BUTTON_TTY_KEY);
Andrew Lee312e8172014-10-23 17:01:36 -07001176 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001177
Andrew Lee8d66d812014-11-24 14:54:02 -08001178 mVoicemailProviders = (ListPreference) findPreference(BUTTON_VOICEMAIL_PROVIDER_KEY);
Andrew Lee2c027892014-10-29 11:29:54 -07001179 mVoicemailProviders.setOnPreferenceChangeListener(this);
Andrew Lee8d66d812014-11-24 14:54:02 -08001180
Andrew Lee2c027892014-10-29 11:29:54 -07001181 mVoicemailSettingsScreen =
1182 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
1183 mVoicemailSettings = (PreferenceScreen)findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
Andrew Lee8d66d812014-11-24 14:54:02 -08001184
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001185 mVoicemailNotificationRingtone = (VoicemailRingtonePreference) findPreference(
1186 getResources().getString(R.string.voicemail_notification_ringtone_key));
1187 mVoicemailNotificationRingtone.init(mPhone);
1188
Andrew Lee8d66d812014-11-24 14:54:02 -08001189 mVoicemailNotificationVibrate = (CheckBoxPreference) findPreference(
1190 getResources().getString(R.string.voicemail_notification_vibrate_key));
1191 mVoicemailNotificationVibrate.setOnPreferenceChangeListener(this);
1192
Andrew Lee2c027892014-10-29 11:29:54 -07001193 initVoiceMailProviders();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001194
Andrew Lee64a7d792014-10-15 17:38:38 -07001195 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1196 mButtonDTMF.setOnPreferenceChangeListener(this);
1197 int dtmf = Settings.System.getInt(getContentResolver(),
1198 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1199 mButtonDTMF.setValueIndex(dtmf);
1200 } else {
1201 prefSet.removePreference(mButtonDTMF);
1202 mButtonDTMF = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001203 }
1204
Andrew Lee64a7d792014-10-15 17:38:38 -07001205 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1206 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1207 int autoretry = Settings.Global.getInt(
1208 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1209 mButtonAutoRetry.setChecked(autoretry != 0);
1210 } else {
1211 prefSet.removePreference(mButtonAutoRetry);
1212 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001213 }
1214
Andrew Lee64a7d792014-10-15 17:38:38 -07001215 if (getResources().getBoolean(R.bool.hac_enabled)) {
1216 mButtonHAC.setOnPreferenceChangeListener(this);
1217 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1218 mButtonHAC.setChecked(hac != 0);
1219 } else {
1220 prefSet.removePreference(mButtonHAC);
1221 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001222 }
1223
Andrew Lee64a7d792014-10-15 17:38:38 -07001224 TelecomManager telecomManager = TelecomManager.from(this);
1225 if (telecomManager != null && telecomManager.isTtySupported()) {
1226 mButtonTTY.setOnPreferenceChangeListener(this);
1227 int settingsTtyMode = Settings.Secure.getInt(getContentResolver(),
1228 Settings.Secure.PREFERRED_TTY_MODE,
1229 TelecomManager.TTY_MODE_OFF);
1230 mButtonTTY.setValue(Integer.toString(settingsTtyMode));
1231 updatePreferredTtyModeSummary(settingsTtyMode);
1232 } else {
1233 prefSet.removePreference(mButtonTTY);
1234 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001235 }
1236
1237 if (!getResources().getBoolean(R.bool.world_phone)) {
Andrew Lee2b36ba22014-11-05 17:08:49 -08001238 Preference cdmaOptions = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
1239 prefSet.removePreference(cdmaOptions);
1240
1241 // TODO: Support MSIM for this preference option.
1242 Preference gsmOptions = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
1243 prefSet.removePreference(gsmOptions);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001244
1245 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001246 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Junda Liua09d6962014-11-12 13:39:02 -08001247 boolean shouldHideCarrierSettings = Settings.Global.getInt(
1248 getContentResolver(), Settings.Global.HIDE_CARRIER_NETWORK_SETTINGS, 0) == 1;
1249 if (shouldHideCarrierSettings) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001250 prefSet.removePreference(fdnButton);
Junda Liua09d6962014-11-12 13:39:02 -08001251 if (mButtonDTMF != null) {
1252 prefSet.removePreference(mButtonDTMF);
Etan Cohen0ca1c802014-07-07 15:35:48 -07001253 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001254 } else {
Junda Liua09d6962014-11-12 13:39:02 -08001255 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1256 prefSet.removePreference(fdnButton);
1257
1258 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1259 addPreferencesFromResource(R.xml.cdma_call_privacy);
1260 }
1261 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
1262 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(this, FdnSetting.class));
1263
1264 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
1265 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1266
1267 Preference callForwardingPref = prefSet.findPreference(CALL_FORWARDING_KEY);
1268 callForwardingPref.setIntent(mSubscriptionInfoHelper.getIntent(
1269 this, GsmUmtsCallForwardOptions.class));
1270
1271 Preference additionalGsmSettingsPref =
1272 prefSet.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
1273 additionalGsmSettingsPref.setIntent(mSubscriptionInfoHelper.getIntent(
1274 this, GsmUmtsAdditionalCallOptions.class));
1275 }
1276 } else {
1277 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1278 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001279 }
1280 }
1281
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001282 // check the intent that started this activity and pop up the voicemail
1283 // dialog if we've been asked to.
1284 // If we have at least one non default VM provider registered then bring up
1285 // the selection for the VM provider, otherwise bring up a VM number dialog.
1286 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001287 if (mShowVoicemailPreference) {
Tyler Gunnbaee2952014-09-10 16:01:02 -07001288 if (DBG) {
1289 log("ACTION_ADD_VOICEMAIL Intent is thrown. current VM data size: "
1290 + mVMProvidersData.size());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001291 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001292 if (mVMProvidersData.size() > 1) {
1293 simulatePreferenceClick(mVoicemailProviders);
1294 } else {
1295 onPreferenceChange(mVoicemailProviders, DEFAULT_VM_PROVIDER_KEY);
1296 mVoicemailProviders.setValue(DEFAULT_VM_PROVIDER_KEY);
1297 }
1298 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001299 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001300
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001301 updateVoiceNumberField();
1302 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001303
Andrew Lee8d66d812014-11-24 14:54:02 -08001304 mVoicemailNotificationVibrate.setChecked(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001305 VoicemailNotificationSettingsUtil.isVibrationEnabled(mPhone));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001306
Andrew Lee312e8172014-10-23 17:01:36 -07001307 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1308 boolean currentValue =
1309 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1310 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1311 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001312 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1313 } else {
1314 prefSet.removePreference(mEnableVideoCalling);
1315 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001316 }
1317
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001318 private void handleTTYChange(Preference preference, Object objValue) {
1319 int buttonTtyMode;
1320 buttonTtyMode = Integer.valueOf((String) objValue).intValue();
1321 int settingsTtyMode = android.provider.Settings.Secure.getInt(
1322 getContentResolver(),
Sailesh Nepalbf900542014-07-15 16:18:32 -07001323 android.provider.Settings.Secure.PREFERRED_TTY_MODE,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001324 TelecomManager.TTY_MODE_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001325 if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
1326 Integer.toString(buttonTtyMode));
1327
1328 if (buttonTtyMode != settingsTtyMode) {
1329 switch(buttonTtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001330 case TelecomManager.TTY_MODE_OFF:
1331 case TelecomManager.TTY_MODE_FULL:
1332 case TelecomManager.TTY_MODE_HCO:
1333 case TelecomManager.TTY_MODE_VCO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001334 android.provider.Settings.Secure.putInt(getContentResolver(),
1335 android.provider.Settings.Secure.PREFERRED_TTY_MODE, buttonTtyMode);
1336 break;
1337 default:
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001338 buttonTtyMode = TelecomManager.TTY_MODE_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001339 }
1340
1341 mButtonTTY.setValue(Integer.toString(buttonTtyMode));
1342 updatePreferredTtyModeSummary(buttonTtyMode);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001343 Intent ttyModeChanged = new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
1344 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001345 sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
1346 }
1347 }
1348
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001349 private void updatePreferredTtyModeSummary(int TtyMode) {
1350 String [] txts = getResources().getStringArray(R.array.tty_mode_entries);
1351 switch(TtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001352 case TelecomManager.TTY_MODE_OFF:
1353 case TelecomManager.TTY_MODE_HCO:
1354 case TelecomManager.TTY_MODE_VCO:
1355 case TelecomManager.TTY_MODE_FULL:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001356 mButtonTTY.setSummary(txts[TtyMode]);
1357 break;
1358 default:
1359 mButtonTTY.setEnabled(false);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001360 mButtonTTY.setSummary(txts[TelecomManager.TTY_MODE_OFF]);
Sailesh Nepalbf900542014-07-15 16:18:32 -07001361 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001362 }
1363 }
1364
1365 private static void log(String msg) {
1366 Log.d(LOG_TAG, msg);
1367 }
1368
1369 /**
1370 * Updates the look of the VM preference widgets based on current VM provider settings.
1371 * Note that the provider name is loaded form the found activity via loadLabel in
1372 * {@link #initVoiceMailProviders()} in order for it to be localizable.
1373 */
1374 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1375 final String key = currentProviderSetting;
1376 final VoiceMailProvider provider = mVMProvidersData.get(key);
1377
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
1400 /**
1401 * Enumerates existing VM providers and puts their data into the list and populates
1402 * the preference list objects with their names.
1403 * In case we are called with ACTION_ADD_VOICEMAIL intent the intent may have
1404 * an extra string called IGNORE_PROVIDER_EXTRA with "package.activityName" of the provider
1405 * which should be hidden when we bring up the list of possible VM providers to choose.
1406 */
1407 private void initVoiceMailProviders() {
1408 if (DBG) log("initVoiceMailProviders()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001409
1410 String providerToIgnore = null;
Andrew Leef1776d82014-11-04 14:45:02 -08001411 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)
1412 && getIntent().hasExtra(IGNORE_PROVIDER_EXTRA)) {
1413 providerToIgnore = getIntent().getStringExtra(IGNORE_PROVIDER_EXTRA);
1414 // Remove this provider from the list.
1415 if (!TextUtils.isEmpty(providerToIgnore)) {
1416 if (DBG) log("Found ACTION_ADD_VOICEMAIL. providerToIgnore= " + providerToIgnore);
Andrew Lee6950c6c2014-11-26 12:05:42 -08001417 VoicemailProviderSettingsUtil.delete(this, providerToIgnore);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001418 }
1419 }
1420
1421 mVMProvidersData.clear();
1422
Andrew Leef1776d82014-11-04 14:45:02 -08001423 List<String> entries = new ArrayList<String>();
1424 List<String> values = new ArrayList<String>();
1425
1426 // Add default voicemail provider.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001427 final String myCarrier = getString(R.string.voicemail_default);
1428 mVMProvidersData.put(DEFAULT_VM_PROVIDER_KEY, new VoiceMailProvider(myCarrier, null));
Andrew Leef1776d82014-11-04 14:45:02 -08001429 entries.add(myCarrier);
1430 values.add(DEFAULT_VM_PROVIDER_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001431
Andrew Leef1776d82014-11-04 14:45:02 -08001432 // Add other voicemail providers.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001433 PackageManager pm = getPackageManager();
Andrew Leef1776d82014-11-04 14:45:02 -08001434 Intent intent = new Intent(ACTION_CONFIGURE_VOICEMAIL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001435 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001436 for (int i = 0; i < resolveInfos.size(); i++) {
1437 final ResolveInfo ri= resolveInfos.get(i);
1438 final ActivityInfo currentActivityInfo = ri.activityInfo;
Andrew Lee6214e2b2014-11-04 13:57:38 -08001439 final String key = currentActivityInfo.name;
Andrew Leef1776d82014-11-04 14:45:02 -08001440
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001441 if (key.equals(providerToIgnore)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001442 continue;
1443 }
Andrew Leef1776d82014-11-04 14:45:02 -08001444
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001445 if (DBG) log("Loading key: " + key);
1446 final String nameForDisplay = ri.loadLabel(pm).toString();
1447 Intent providerIntent = new Intent();
1448 providerIntent.setAction(ACTION_CONFIGURE_VOICEMAIL);
Andrew Leef1776d82014-11-04 14:45:02 -08001449 providerIntent.setClassName(currentActivityInfo.packageName, currentActivityInfo.name);
1450 VoiceMailProvider vmProvider = new VoiceMailProvider(nameForDisplay, providerIntent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001451
Andrew Leef1776d82014-11-04 14:45:02 -08001452 if (DBG) log("Store VoiceMailProvider. Key: " + key + " -> " + vmProvider.toString());
1453 mVMProvidersData.put(key, vmProvider);
1454 entries.add(vmProvider.name);
1455 values.add(key);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001456 }
1457
Andrew Leef1776d82014-11-04 14:45:02 -08001458 mVoicemailProviders.setEntries(entries.toArray(new String[0]));
1459 mVoicemailProviders.setEntryValues(values.toArray(new String[0]));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001460
Andrew Leef1776d82014-11-04 14:45:02 -08001461 // Remember the current Voicemail Provider key as a "previous" key. This will be used when
1462 // we fail to update Voicemail Provider, which requires rollback. We will update this when
1463 // the VM Provider setting is successfully updated.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001464 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1465 if (DBG) log("Set up the first mPreviousVMProviderKey: " + mPreviousVMProviderKey);
1466
1467 // Finally update the preference texts.
1468 updateVMPreferenceWidgets(mPreviousVMProviderKey);
1469 }
1470
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001471 /**
1472 * Simulates user clicking on a passed preference.
1473 * Usually needed when the preference is a dialog preference and we want to invoke
1474 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001475 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001476 */
1477 private void simulatePreferenceClick(Preference preference) {
1478 // Go through settings until we find our setting
1479 // and then simulate a click on it to bring up the dialog
1480 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1481 for (int idx = 0; idx < adapter.getCount(); idx++) {
1482 if (adapter.getItem(idx) == preference) {
1483 getPreferenceScreen().onItemClick(this.getListView(),
1484 null, idx, adapter.getItemId(idx));
1485 break;
1486 }
1487 }
1488 }
1489
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001490 private String getCurrentVoicemailProviderKey() {
1491 final String key = mVoicemailProviders.getValue();
1492 return (key != null) ? key : DEFAULT_VM_PROVIDER_KEY;
1493 }
1494
1495 @Override
1496 public boolean onOptionsItemSelected(MenuItem item) {
1497 final int itemId = item.getItemId();
1498 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001499 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001500 return true;
1501 }
1502 return super.onOptionsItemSelected(item);
1503 }
Andrew Lee8d66d812014-11-24 14:54:02 -08001504
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001505 /**
1506 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1507 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1508 */
1509 public static void goUpToTopLevelSetting(Activity activity) {
1510 Intent intent = new Intent(activity, CallFeaturesSetting.class);
1511 intent.setAction(Intent.ACTION_MAIN);
1512 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1513 activity.startActivity(intent);
1514 activity.finish();
1515 }
1516}