blob: 37c21f144705dfe0359078960b478a6cc4effef3 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
19import android.app.ActionBar;
20import android.app.Activity;
Evan Charlton1c696832014-04-15 14:24:23 -070021import android.app.ActivityOptions;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.AlertDialog;
23import android.app.Dialog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.ContentResolver;
25import android.content.Context;
26import android.content.DialogInterface;
27import android.content.Intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.database.Cursor;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.os.AsyncResult;
30import android.os.Bundle;
31import android.os.Handler;
32import android.os.Message;
33import android.os.UserHandle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.preference.CheckBoxPreference;
35import android.preference.ListPreference;
36import android.preference.Preference;
37import android.preference.PreferenceActivity;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.preference.PreferenceScreen;
39import android.provider.ContactsContract.CommonDataKinds;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070041import android.telecom.PhoneAccountHandle;
42import android.telecom.TelecomManager;
Andrew Lee93c345f2014-10-27 15:25:07 -070043import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.text.TextUtils;
45import android.util.Log;
46import android.view.MenuItem;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.widget.ListAdapter;
Jay Shraunerc2684732014-11-12 12:10:37 -080048import android.widget.Toast;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049
Andrew Lee312e8172014-10-23 17:01:36 -070050import com.android.ims.ImsManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import com.android.internal.telephony.CallForwardInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import com.android.internal.telephony.Phone;
53import com.android.internal.telephony.PhoneConstants;
Andrew Lee2170a972014-08-13 18:13:01 -070054import com.android.phone.common.util.SettingsUtil;
Andrew Leedb2fe562014-09-03 15:40:43 -070055import com.android.phone.settings.AccountSelectionPreference;
Andrew Lee1af6cf72014-11-04 17:35:26 -080056import com.android.phone.settings.CallForwardInfoUtil;
Andrew Leefb7f92e2015-02-26 16:23:32 -080057import com.android.phone.settings.SettingsConstants;
Andrew Lee84024342014-11-06 23:37:09 -080058import com.android.phone.settings.VoicemailDialogUtil;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -080059import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Andrew Lee64e5c9d2014-11-26 12:00:10 -080060import com.android.phone.settings.VoicemailProviderListPreference;
61import com.android.phone.settings.VoicemailProviderListPreference.VoicemailProvider;
Andrew Leeb490d732014-10-27 15:00:41 -070062import com.android.phone.settings.VoicemailProviderSettings;
Andrew Lee88b51e22014-10-29 15:48:51 -070063import com.android.phone.settings.VoicemailProviderSettingsUtil;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -080064import com.android.phone.settings.VoicemailRingtonePreference;
Andrew Lee5ed870c2014-10-29 11:47:49 -070065import com.android.phone.settings.fdn.FdnSetting;
Sailesh Nepal788959e2014-07-08 23:36:40 -070066import com.android.services.telephony.sip.SipUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067
Andrew Lee2170a972014-08-13 18:13:01 -070068import java.lang.String;
Andrew Leef1776d82014-11-04 14:45:02 -080069import java.util.ArrayList;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import java.util.Collection;
71import java.util.HashMap;
72import java.util.HashSet;
73import java.util.Iterator;
74import java.util.List;
75import java.util.Map;
76
77/**
78 * Top level "Call settings" UI; see res/xml/call_feature_setting.xml
79 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070080 * This preference screen is the root of the "Call settings" hierarchy available from the Phone
81 * app; the settings here let you control various features related to phone calls (including
82 * voicemail settings, the "Respond via SMS" feature, and others.) It's used only on
83 * voice-capable phone devices.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084 *
85 * Note that this activity is part of the package com.android.phone, even
86 * though you reach it from the "Phone" app (i.e. DialtactsActivity) which
87 * is from the package com.android.contacts.
88 *
89 * For the "Mobile network settings" screen under the main Settings app,
90 * See {@link MobileNetworkSettings}.
91 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070092 * TODO: Settings should be split into PreferenceFragments where possible (ie. voicemail).
Andrew Lee2170a972014-08-13 18:13:01 -070093 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094 * @see com.android.phone.MobileNetworkSettings
95 */
96public class CallFeaturesSetting extends PreferenceActivity
97 implements DialogInterface.OnClickListener,
Andrew Lee2170a972014-08-13 18:13:01 -070098 Preference.OnPreferenceChangeListener,
Andrew Lee2170a972014-08-13 18:13:01 -070099 EditPhoneNumberPreference.OnDialogClosedListener,
Andrew Leece8ae2a2014-09-10 10:41:48 -0700100 EditPhoneNumberPreference.GetDefaultNumberListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700101 private static final String LOG_TAG = "CallFeaturesSetting";
102 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
Andrew Lee77527ac2014-10-21 16:57:39 -0700103 // STOPSHIP if true. Flag to override behavior default behavior to hide VT setting.
104 private static final boolean ENABLE_VT_FLAG = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700105
106 /**
107 * Intent action to bring up Voicemail Provider settings.
108 *
109 * @see #IGNORE_PROVIDER_EXTRA
110 */
111 public static final String ACTION_ADD_VOICEMAIL =
112 "com.android.phone.CallFeaturesSetting.ADD_VOICEMAIL";
113 // intent action sent by this activity to a voice mail provider
114 // to trigger its configuration UI
115 public static final String ACTION_CONFIGURE_VOICEMAIL =
116 "com.android.phone.CallFeaturesSetting.CONFIGURE_VOICEMAIL";
117 // Extra put in the return from VM provider config containing voicemail number to set
118 public static final String VM_NUMBER_EXTRA = "com.android.phone.VoicemailNumber";
119 // Extra put in the return from VM provider config containing call forwarding number to set
120 public static final String FWD_NUMBER_EXTRA = "com.android.phone.ForwardingNumber";
121 // Extra put in the return from VM provider config containing call forwarding number to set
122 public static final String FWD_NUMBER_TIME_EXTRA = "com.android.phone.ForwardingNumberTime";
123 // If the VM provider returns non null value in this extra we will force the user to
124 // choose another VM provider
125 public static final String SIGNOUT_EXTRA = "com.android.phone.Signout";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700126
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127 /**
128 * String Extra put into ACTION_ADD_VOICEMAIL call to indicate which provider should be hidden
129 * in the list of providers presented to the user. This allows a provider which is being
130 * disabled (e.g. GV user logging out) to force the user to pick some other provider.
131 */
132 public static final String IGNORE_PROVIDER_EXTRA = "com.android.phone.ProviderToIgnore";
133
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800134 /**
135 * String Extra put into ACTION_ADD_VOICEMAIL to indicate that the voicemail setup screen should
136 * be opened.
137 */
138 public static final String SETUP_VOICEMAIL_EXTRA = "com.android.phone.SetupVoicemail";
139
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700140 // string constants
141 private static final String NUM_PROJECTION[] = {CommonDataKinds.Phone.NUMBER};
142
143 // String keys for preference lookup
144 // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
Andrew Lee8d66d812014-11-24 14:54:02 -0800145 // TODO: Consider moving these strings to strings.xml, so that they are not duplicated here and
146 // in the layout files. These strings need to be treated carefully; if the setting is
147 // persistent, they are used as the key to store shared preferences and the name should not be
148 // changed unless the settings are also migrated.
Andrew Lee97708a42014-09-25 12:39:07 -0700149 private static final String VOICEMAIL_SETTING_SCREEN_PREF_KEY = "button_voicemail_category_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150 private static final String BUTTON_VOICEMAIL_KEY = "button_voicemail_key";
151 private static final String BUTTON_VOICEMAIL_PROVIDER_KEY = "button_voicemail_provider_key";
152 private static final String BUTTON_VOICEMAIL_SETTING_KEY = "button_voicemail_setting_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700153 private static final String BUTTON_FDN_KEY = "button_fdn_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700154
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700155 private static final String BUTTON_RETRY_KEY = "button_auto_retry_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700156
157 private static final String BUTTON_GSM_UMTS_OPTIONS = "button_gsm_more_expand_key";
158 private static final String BUTTON_CDMA_OPTIONS = "button_cdma_more_expand_key";
Andrew Lee2b36ba22014-11-05 17:08:49 -0800159 private static final String CALL_FORWARDING_KEY = "call_forwarding_key";
160 private static final String ADDITIONAL_GSM_SETTINGS_KEY = "additional_gsm_call_settings_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700161
Andrew Leece8ae2a2014-09-10 10:41:48 -0700162 private static final String PHONE_ACCOUNT_SETTINGS_KEY =
163 "phone_account_settings_preference_screen";
Andrew Leedb2fe562014-09-03 15:40:43 -0700164
Andrew Leedf14ead2014-10-17 14:22:52 -0700165 private static final String ENABLE_VIDEO_CALLING_KEY = "button_enable_video_calling";
166
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700167 /** Event for Async voicemail change call */
168 private static final int EVENT_VOICEMAIL_CHANGED = 500;
169 private static final int EVENT_FORWARDING_CHANGED = 501;
170 private static final int EVENT_FORWARDING_GET_COMPLETED = 502;
171
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700172 /** Handle to voicemail pref */
173 private static final int VOICEMAIL_PREF_ID = 1;
174 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
175
176 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700177
Andrew Lee5ed870c2014-10-29 11:47:49 -0700178 private SubscriptionInfoHelper mSubscriptionInfoHelper;
179
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700180 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
181
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700182 private CheckBoxPreference mButtonAutoRetry;
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800183 private VoicemailProviderListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700184 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185 private PreferenceScreen mVoicemailSettings;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800186 private VoicemailRingtonePreference mVoicemailNotificationRingtone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700187 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700188 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700190 /**
191 * Results of reading forwarding settings
192 */
193 private CallForwardInfo[] mForwardingReadResults = null;
194
195 /**
196 * Result of forwarding number change.
197 * Keys are reasons (eg. unconditional forwarding).
198 */
199 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
200
201 /**
202 * Expected CF read result types.
203 * This set keeps track of the CF types for which we've issued change
204 * commands so we can tell when we've received all of the responses.
205 */
206 private Collection<Integer> mExpectedChangeResultReasons = null;
207
208 /**
209 * Result of vm number change
210 */
211 private AsyncResult mVoicemailChangeResult = null;
212
213 /**
214 * Previous VM provider setting so we can return to it in case of failure.
215 */
216 private String mPreviousVMProviderKey = null;
217
218 /**
219 * Id of the dialog being currently shown.
220 */
221 private int mCurrentDialogId = 0;
222
223 /**
224 * Flag indicating that we are invoking settings for the voicemail provider programmatically
225 * due to vm provider change.
226 */
227 private boolean mVMProviderSettingsForced = false;
228
229 /**
230 * Flag indicating that we are making changes to vm or fwd numbers
231 * due to vm provider change.
232 */
233 private boolean mChangingVMorFwdDueToProviderChange = false;
234
235 /**
236 * True if we are in the process of vm & fwd number change and vm has already been changed.
237 * This is used to decide what to do in case of rollback.
238 */
239 private boolean mVMChangeCompletedSuccessfully = false;
240
241 /**
242 * True if we had full or partial failure setting forwarding numbers and so need to roll them
243 * back.
244 */
245 private boolean mFwdChangesRequireRollback = false;
246
247 /**
248 * Id of error msg to display to user once we are done reverting the VM provider to the previous
249 * one.
250 */
251 private int mVMOrFwdSetError = 0;
252
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700253 /** string to hold old voicemail number as it is being updated. */
254 private String mOldVmNumber;
255
256 // New call forwarding settings and vm number we will be setting
257 // Need to save these since before we get to saving we need to asynchronously
258 // query the existing forwarding settings.
259 private CallForwardInfo[] mNewFwdSettings;
260 private String mNewVMNumber;
261
262 private boolean mForeground;
263
264 @Override
265 public void onPause() {
266 super.onPause();
267 mForeground = false;
268 }
269
270 /**
Tyler Gunnbaee2952014-09-10 16:01:02 -0700271 * Used to indicate that the voicemail preference should be shown.
272 */
273 private boolean mShowVoicemailPreference = false;
274
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800275 /**
276 * Used to indicate that the voicemail setup screen should be shown.
277 */
278 private boolean mSetupVoicemail = false;
279
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700280 /*
281 * Click Listeners, handle click based on objects attached to UI.
282 */
283
284 // Click listener for all toggle events
285 @Override
286 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
287 if (preference == mSubMenuVoicemailSettings) {
288 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700289 } else if (preference == mButtonAutoRetry) {
290 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
291 android.provider.Settings.Global.CALL_AUTO_RETRY,
292 mButtonAutoRetry.isChecked() ? 1 : 0);
293 return true;
Andrew Lee696bff52014-12-03 17:58:18 -0800294 } else if (preference.getKey().equals(mVoicemailSettings.getKey())) {
295 // Check key instead of comparing reference because closing the voicemail notification
296 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
297 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800298 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
299
Andrew Lee696bff52014-12-03 17:58:18 -0800300 final Dialog dialog = ((PreferenceScreen) preference).getDialog();
Yorke Leea0f63bf2014-10-09 18:27:20 -0700301 if (dialog != null) {
302 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
303 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800304
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700305 if (preference.getIntent() != null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800306 if (DBG) log("Invoking cfg intent " + preference.getIntent().getPackage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700307
308 // onActivityResult() will be responsible for resetting some of variables.
309 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
310 return true;
311 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800312 if (DBG) log("onPreferenceTreeClick(). No intent; use default behavior in xml.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700313
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800314 // onActivityResult() will not be called, so reset variables here.
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800315 mPreviousVMProviderKey = VoicemailProviderListPreference.DEFAULT_KEY;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700316 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700317 return false;
318 }
Andrew Lee97708a42014-09-25 12:39:07 -0700319 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700320 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
321 if (dialog != null) {
322 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
323 }
Andrew Lee97708a42014-09-25 12:39:07 -0700324 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700325 }
326 return false;
327 }
328
329 /**
330 * Implemented to support onPreferenceChangeListener to look for preference
331 * changes.
332 *
333 * @param preference is the preference to be changed
334 * @param objValue should be the value of the selection, NOT its localized
335 * display value.
336 */
337 @Override
338 public boolean onPreferenceChange(Preference preference, Object objValue) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800339 if (DBG) log("onPreferenceChange: \"" + preference + "\" changed to \"" + objValue + "\"");
Andrew Lee2170a972014-08-13 18:13:01 -0700340
Andrew Leea1239f22015-03-02 17:44:07 -0800341 if (preference == mVoicemailProviders) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700342 final String newProviderKey = (String) objValue;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800343
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344 // If previous provider key and the new one is same, we don't need to handle it.
345 if (mPreviousVMProviderKey.equals(newProviderKey)) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800346 if (DBG) log("No change is made to the VM provider setting.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700347 return true;
348 }
349 updateVMPreferenceWidgets(newProviderKey);
350
Andrew Leeb490d732014-10-27 15:00:41 -0700351 final VoicemailProviderSettings newProviderSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800352 VoicemailProviderSettingsUtil.load(this, newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700353
Andrew Lee88b51e22014-10-29 15:48:51 -0700354 // If the user switches to a voice mail provider and we have numbers stored for it we
355 // will automatically change the phone's voice mail and forwarding number to the stored
356 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700357 if (newProviderSettings == null) {
358 // Force the user into a configuration of the chosen provider
359 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
360 mVMProviderSettingsForced = true;
361 simulatePreferenceClick(mVoicemailSettings);
362 } else {
363 if (DBG) log("Saved preferences found - switching to them");
364 // Set this flag so if we get a failure we revert to previous provider
365 mChangingVMorFwdDueToProviderChange = true;
366 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
367 }
Andrew Lee8d66d812014-11-24 14:54:02 -0800368 } else if (preference.getKey().equals(mVoicemailNotificationVibrate.getKey())) {
Andrew Lee696bff52014-12-03 17:58:18 -0800369 // Check key instead of comparing reference because closing the voicemail notification
370 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
371 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee8d66d812014-11-24 14:54:02 -0800372 VoicemailNotificationSettingsUtil.setVibrationEnabled(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800373 mPhone, Boolean.TRUE.equals(objValue));
Andrew Leedf14ead2014-10-17 14:22:52 -0700374 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700375 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
376 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
377 } else {
378 AlertDialog.Builder builder = new AlertDialog.Builder(this);
379 DialogInterface.OnClickListener networkSettingsClickListener =
380 new Dialog.OnClickListener() {
381 @Override
382 public void onClick(DialogInterface dialog, int which) {
383 startActivity(new Intent(mPhone.getContext(),
384 com.android.phone.MobileNetworkSettings.class));
385 }
386 };
387 builder.setMessage(getResources().getString(
388 R.string.enable_video_calling_dialog_msg))
389 .setNeutralButton(getResources().getString(
390 R.string.enable_video_calling_dialog_settings),
391 networkSettingsClickListener)
392 .setPositiveButton(android.R.string.ok, null)
393 .show();
394 return false;
395 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700396 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800397
398 // Always let the preference setting proceed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700399 return true;
400 }
401
402 @Override
403 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800404 if (DBG) log("onDialogClosed: Button clicked is " + buttonClicked);
405
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700406 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
407 return;
408 }
409
410 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700411 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
412 mSubMenuVoicemailSettings.getPhoneNumber(),
413 VoicemailProviderSettings.NO_FORWARDING);
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800414 saveVoiceMailAndForwardingNumber(mVoicemailProviders.getKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700415 }
416 }
417
418 /**
419 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
420 * This method set the default values for the various
421 * EditPhoneNumberPreference dialogs.
422 */
423 @Override
424 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
425 if (preference == mSubMenuVoicemailSettings) {
426 // update the voicemail number field, which takes care of the
427 // mSubMenuVoicemailSettings itself, so we should return null.
428 if (DBG) log("updating default for voicemail dialog");
429 updateVoiceNumberField();
430 return null;
431 }
432
433 String vmDisplay = mPhone.getVoiceMailNumber();
434 if (TextUtils.isEmpty(vmDisplay)) {
435 // if there is no voicemail number, we just return null to
436 // indicate no contribution.
437 return null;
438 }
439
440 // Return the voicemail number prepended with "VM: "
441 if (DBG) log("updating default for call forwarding dialogs");
442 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
443 }
444
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700445 private void switchToPreviousVoicemailProvider() {
446 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700447
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800448 if (mPreviousVMProviderKey == null) {
449 return;
450 }
451
452 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
Andrew Lee84024342014-11-06 23:37:09 -0800453 showDialogIfForeground(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800454 final VoicemailProviderSettings prevSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800455 VoicemailProviderSettingsUtil.load(this, mPreviousVMProviderKey);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800456 if (prevSettings == null) {
457 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
458 + mPreviousVMProviderKey + "\" is null but should be loaded.");
459 }
460
461 if (mVMChangeCompletedSuccessfully) {
462 mNewVMNumber = prevSettings.getVoicemailNumber();
463 Log.i(LOG_TAG, "VM change is already completed successfully."
464 + "Have to revert VM back to " + mNewVMNumber + " again.");
465 mPhone.setVoiceMailNumber(
466 mPhone.getVoiceMailAlphaTag().toString(),
467 mNewVMNumber,
468 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
469 }
470
471 if (mFwdChangesRequireRollback) {
472 Log.i(LOG_TAG, "Requested to rollback forwarding changes.");
473
474 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
475 if (prevFwdSettings != null) {
476 Map<Integer, AsyncResult> results = mForwardingChangeResults;
477 resetForwardingChangeState();
478 for (int i = 0; i < prevFwdSettings.length; i++) {
479 CallForwardInfo fi = prevFwdSettings[i];
480 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
481 // Only revert the settings for which the update succeeded.
482 AsyncResult result = results.get(fi.reason);
483 if (result != null && result.exception == null) {
484 mExpectedChangeResultReasons.add(fi.reason);
485 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
486 mRevertOptionComplete.obtainMessage(
487 EVENT_FORWARDING_CHANGED, i, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700488 }
489 }
490 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700491 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800492 } else {
493 if (DBG) log("No need to revert");
494 onRevertDone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700495 }
496 }
497
498 private void onRevertDone() {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800499 if (DBG) log("onRevertDone: Changing provider key back to " + mPreviousVMProviderKey);
500
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700501 updateVMPreferenceWidgets(mPreviousVMProviderKey);
502 updateVoiceNumberField();
503 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800504 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700505 mVMOrFwdSetError = 0;
506 }
507 }
508
509 @Override
510 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
511 if (DBG) {
512 log("onActivityResult: requestCode: " + requestCode
513 + ", resultCode: " + resultCode
514 + ", data: " + data);
515 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800516
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700517 // there are cases where the contact picker may end up sending us more than one
518 // request. We want to ignore the request if we're not in the correct state.
519 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
520 boolean failure = false;
521
522 // No matter how the processing of result goes lets clear the flag
523 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
524 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
525 mVMProviderSettingsForced = false;
526
527 String vmNum = null;
528 if (resultCode != RESULT_OK) {
529 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
530 failure = true;
531 } else {
532 if (data == null) {
533 if (DBG) log("onActivityResult: vm provider cfg result has no data");
534 failure = true;
535 } else {
536 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
537 if (DBG) log("Provider requested signout");
538 if (isVMProviderSettingsForced) {
539 if (DBG) log("Going back to previous provider on signout");
540 switchToPreviousVoicemailProvider();
541 } else {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800542 final String victim = mVoicemailProviders.getKey();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700543 if (DBG) log("Relaunching activity and ignoring " + victim);
544 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
545 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
546 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
547 this.startActivity(i);
548 }
549 return;
550 }
551 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
552 if (vmNum == null || vmNum.length() == 0) {
553 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
554 failure = true;
555 }
556 }
557 }
558 if (failure) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800559 if (DBG) log("Failure in return from voicemail provider.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700560 if (isVMProviderSettingsForced) {
561 switchToPreviousVoicemailProvider();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700562 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800563
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700564 return;
565 }
566 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
567 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
568
Santos Cordonda120f42014-08-06 04:44:34 -0700569 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700570 // send it to the provider when it's config is invoked so it can use this as default
571 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
572
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800573 if (DBG) log("onActivityResult: cfg result has forwarding number " + fwdNum);
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800574 saveVoiceMailAndForwardingNumber(mVoicemailProviders.getKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700575 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700576 return;
577 }
578
579 if (requestCode == VOICEMAIL_PREF_ID) {
580 if (resultCode != RESULT_OK) {
581 if (DBG) log("onActivityResult: contact picker result not OK.");
582 return;
583 }
584
585 Cursor cursor = null;
586 try {
587 cursor = getContentResolver().query(data.getData(),
588 NUM_PROJECTION, null, null, null);
589 if ((cursor == null) || (!cursor.moveToFirst())) {
590 if (DBG) log("onActivityResult: bad contact data, no results found.");
591 return;
592 }
593 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
594 return;
595 } finally {
596 if (cursor != null) {
597 cursor.close();
598 }
599 }
600 }
601
602 super.onActivityResult(requestCode, resultCode, data);
603 }
604
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700605 /**
606 * Wrapper around showDialog() that will silently do nothing if we're
607 * not in the foreground.
608 *
609 * This is useful here because most of the dialogs we display from
610 * this class are triggered by asynchronous events (like
611 * success/failure messages from the telephony layer) and it's
612 * possible for those events to come in even after the user has gone
613 * to a different screen.
614 */
615 // TODO: this is too brittle: it's still easy to accidentally add new
616 // code here that calls showDialog() directly (which will result in a
617 // WindowManager$BadTokenException if called after the activity has
618 // been stopped.)
619 //
620 // It would be cleaner to do the "if (mForeground)" check in one
621 // central place, maybe by using a single Handler for all asynchronous
622 // events (and have *that* discard events if we're not in the
623 // foreground.)
624 //
625 // Unfortunately it's not that simple, since we sometimes need to do
626 // actual work to handle these events whether or not we're in the
627 // foreground (see the Handler code in mSetOptionComplete for
628 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800629 //
630 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
631 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700632 private void showDialogIfForeground(int id) {
633 if (mForeground) {
634 showDialog(id);
635 }
636 }
637
638 private void dismissDialogSafely(int id) {
639 try {
640 dismissDialog(id);
641 } catch (IllegalArgumentException e) {
642 // This is expected in the case where we were in the background
643 // at the time we would normally have shown the dialog, so we didn't
644 // show it.
645 }
646 }
647
Andrew Lee299e4a72015-01-15 12:44:34 -0800648 /**
649 * TODO: Refactor to make it easier to understand what's done in the different stages.
650 */
Andrew Leeb490d732014-10-27 15:00:41 -0700651 private void saveVoiceMailAndForwardingNumber(
652 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700653 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700654 mNewVMNumber = newSettings.getVoicemailNumber();
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800655 mNewVMNumber = (mNewVMNumber == null) ? "" : mNewVMNumber;
Andrew Leeb490d732014-10-27 15:00:41 -0700656 mNewFwdSettings = newSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700657
Andrew Leeb3bf3422015-03-13 17:59:00 -0700658 // Call forwarding is not suppported on CDMA.
659 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
660 if (DBG) log("Ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700661 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700662 }
663
Andrew Leee3c15212014-10-28 13:12:55 -0700664 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700665 if (mNewVMNumber.equals(mOldVmNumber)
666 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Lee84024342014-11-06 23:37:09 -0800667 showDialogIfForeground(VoicemailDialogUtil.VM_NOCHANGE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700668 return;
669 }
670
Andrew Lee6950c6c2014-11-26 12:05:42 -0800671 VoicemailProviderSettingsUtil.save(this, key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700672 mVMChangeCompletedSuccessfully = false;
673 mFwdChangesRequireRollback = false;
674 mVMOrFwdSetError = 0;
Andrew Lee299e4a72015-01-15 12:44:34 -0800675
Andrew Leeb3bf3422015-03-13 17:59:00 -0700676 if (mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING
677 || key.equals(mPreviousVMProviderKey)) {
678 if (DBG) log("Set voicemail number. No changes to forwarding number.");
Andrew Lee9c822312015-03-13 07:52:08 -0700679 setVoicemailNumberWithCarrier();
680 } else {
Andrew Leeb3bf3422015-03-13 17:59:00 -0700681 if (DBG) log("Reading current forwarding settings.");
Andrew Leeb490d732014-10-27 15:00:41 -0700682 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
683 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
684 for (int i = 0; i < mForwardingReadResults.length; i++) {
685 mPhone.getCallForwardingOption(
686 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700687 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
688 }
Andrew Lee84024342014-11-06 23:37:09 -0800689 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700690 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800691
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800692 PhoneGlobals.getInstance().refreshMwiIndicator(mSubscriptionInfoHelper.getSubId());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700693 }
694
695 private final Handler mGetOptionComplete = new Handler() {
696 @Override
697 public void handleMessage(Message msg) {
698 AsyncResult result = (AsyncResult) msg.obj;
699 switch (msg.what) {
700 case EVENT_FORWARDING_GET_COMPLETED:
701 handleForwardingSettingsReadResult(result, msg.arg1);
702 break;
703 }
704 }
705 };
706
707 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
708 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800709
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700710 Throwable error = null;
711 if (ar.exception != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700712 error = ar.exception;
Andrew Lee1af6cf72014-11-04 17:35:26 -0800713 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700714 }
715 if (ar.userObj instanceof Throwable) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800716 error = (Throwable) ar.userObj;
717 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700718 }
719
720 // We may have already gotten an error and decided to ignore the other results.
721 if (mForwardingReadResults == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800722 if (DBG) Log.d(LOG_TAG, "Ignoring fwd reading result: " + idx);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700723 return;
724 }
725
726 // In case of error ignore other results, show an error dialog
727 if (error != null) {
728 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
729 mForwardingReadResults = null;
Andrew Lee84024342014-11-06 23:37:09 -0800730 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
731 showDialogIfForeground(VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700732 return;
733 }
734
Andrew Lee1af6cf72014-11-04 17:35:26 -0800735 // Get the forwarding info.
736 mForwardingReadResults[idx] = CallForwardInfoUtil.getCallForwardInfo(
737 (CallForwardInfo[]) ar.result,
738 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx]);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700739
740 // Check if we got all the results already
741 boolean done = true;
742 for (int i = 0; i < mForwardingReadResults.length; i++) {
743 if (mForwardingReadResults[i] == null) {
744 done = false;
745 break;
746 }
747 }
Andrew Lee1af6cf72014-11-04 17:35:26 -0800748
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700749 if (done) {
750 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
Andrew Lee84024342014-11-06 23:37:09 -0800751 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800752
Andrew Leeb3bf3422015-03-13 17:59:00 -0700753 if (mPreviousVMProviderKey.equals(VoicemailProviderListPreference.DEFAULT_KEY)) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800754 VoicemailProviderSettingsUtil.save(mPhone.getContext(),
755 VoicemailProviderListPreference.DEFAULT_KEY,
Andrew Lee299e4a72015-01-15 12:44:34 -0800756 new VoicemailProviderSettings(mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700757 }
758 saveVoiceMailAndForwardingNumberStage2();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700759 }
760 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800761
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700762 private void resetForwardingChangeState() {
763 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
764 mExpectedChangeResultReasons = new HashSet<Integer>();
765 }
766
Andrew Lee299e4a72015-01-15 12:44:34 -0800767 // Called after we are done saving the previous forwarding settings if we needed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700768 private void saveVoiceMailAndForwardingNumberStage2() {
769 mForwardingChangeResults = null;
770 mVoicemailChangeResult = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700771
Andrew Lee9c822312015-03-13 07:52:08 -0700772 resetForwardingChangeState();
773 for (int i = 0; i < mNewFwdSettings.length; i++) {
774 CallForwardInfo fi = mNewFwdSettings[i];
775 CallForwardInfo fiForReason =
776 CallForwardInfoUtil.infoForReason(mForwardingReadResults, fi.reason);
777 final boolean doUpdate = CallForwardInfoUtil.isUpdateRequired(fiForReason, fi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700778
Andrew Lee9c822312015-03-13 07:52:08 -0700779 if (doUpdate) {
780 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
781 mExpectedChangeResultReasons.add(i);
782
783 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
784 mSetOptionComplete.obtainMessage(
785 EVENT_FORWARDING_CHANGED, fi.reason, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700786 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700787 }
Andrew Lee9c822312015-03-13 07:52:08 -0700788 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700789 }
790
Andrew Lee9c822312015-03-13 07:52:08 -0700791 private void setVoicemailNumberWithCarrier() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700792 if (DBG) log("save voicemail #: " + mNewVMNumber);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800793
Andrew Lee9c822312015-03-13 07:52:08 -0700794 mVoicemailChangeResult = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700795 mPhone.setVoiceMailNumber(
796 mPhone.getVoiceMailAlphaTag().toString(),
797 mNewVMNumber,
798 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
799 }
800
801 /**
802 * Callback to handle option update completions
803 */
804 private final Handler mSetOptionComplete = new Handler() {
805 @Override
806 public void handleMessage(Message msg) {
807 AsyncResult result = (AsyncResult) msg.obj;
808 boolean done = false;
809 switch (msg.what) {
810 case EVENT_VOICEMAIL_CHANGED:
811 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700812 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700813 done = true;
814 break;
815 case EVENT_FORWARDING_CHANGED:
816 mForwardingChangeResults.put(msg.arg1, result);
817 if (result.exception != null) {
818 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
819 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700820 }
Andrew Leee438b312014-10-29 16:59:15 -0700821 if (isForwardingCompleted()) {
822 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700823 if (DBG) log("Overall fwd changes completed ok, starting vm change");
Andrew Lee9c822312015-03-13 07:52:08 -0700824 setVoicemailNumberWithCarrier();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700825 } else {
826 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
827 "Check if we need to try rollback for some settings.");
828 mFwdChangesRequireRollback = false;
829 Iterator<Map.Entry<Integer,AsyncResult>> it =
830 mForwardingChangeResults.entrySet().iterator();
831 while (it.hasNext()) {
832 Map.Entry<Integer,AsyncResult> entry = it.next();
833 if (entry.getValue().exception == null) {
834 // If at least one succeeded we have to revert
835 Log.i(LOG_TAG, "Rollback will be required");
836 mFwdChangesRequireRollback = true;
837 break;
838 }
839 }
840 if (!mFwdChangesRequireRollback) {
841 Log.i(LOG_TAG, "No rollback needed.");
842 }
843 done = true;
844 }
845 }
846 break;
847 default:
848 // TODO: should never reach this, may want to throw exception
849 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800850
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700851 if (done) {
852 if (DBG) log("All VM provider related changes done");
853 if (mForwardingChangeResults != null) {
Andrew Lee84024342014-11-06 23:37:09 -0800854 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700855 }
Andrew Leee438b312014-10-29 16:59:15 -0700856 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700857 }
858 }
859 };
860
861 /**
862 * Callback to handle option revert completions
863 */
864 private final Handler mRevertOptionComplete = new Handler() {
865 @Override
866 public void handleMessage(Message msg) {
867 AsyncResult result = (AsyncResult) msg.obj;
868 switch (msg.what) {
869 case EVENT_VOICEMAIL_CHANGED:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700870 if (DBG) log("VM revert complete msg");
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800871 mVoicemailChangeResult = result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700872 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800873
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700874 case EVENT_FORWARDING_CHANGED:
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800875 if (DBG) log("FWD revert complete msg ");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700876 mForwardingChangeResults.put(msg.arg1, result);
877 if (result.exception != null) {
878 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
879 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700880 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700881 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800882
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700883 default:
884 // TODO: should never reach this, may want to throw exception
885 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800886
887 final boolean done = (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null)
888 && (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700889 if (done) {
890 if (DBG) log("All VM reverts done");
Andrew Lee84024342014-11-06 23:37:09 -0800891 dismissDialogSafely(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700892 onRevertDone();
893 }
894 }
895 };
896
897 /**
Andrew Leee438b312014-10-29 16:59:15 -0700898 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700899 */
Andrew Leee438b312014-10-29 16:59:15 -0700900 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700901 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -0700902 return true;
903 }
904
905 for (Integer reason : mExpectedChangeResultReasons) {
906 if (mForwardingChangeResults.get(reason) == null) {
907 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700908 }
909 }
Andrew Leee438b312014-10-29 16:59:15 -0700910
911 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700912 }
Andrew Leee438b312014-10-29 16:59:15 -0700913
914 private boolean isFwdChangeSuccess() {
915 if (mForwardingChangeResults == null) {
916 return true;
917 }
918
919 for (AsyncResult result : mForwardingChangeResults.values()) {
920 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700921 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700922 String msg = exception.getMessage();
923 msg = (msg != null) ? msg : "";
924 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
925 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700926 }
927 }
Andrew Leee438b312014-10-29 16:59:15 -0700928 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700929 }
930
Andrew Leee438b312014-10-29 16:59:15 -0700931 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700932 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700933 String msg = mVoicemailChangeResult.exception.getMessage();
934 msg = (msg != null) ? msg : "";
935 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
936 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700937 }
Andrew Leee438b312014-10-29 16:59:15 -0700938 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700939 }
940
Andrew Leee438b312014-10-29 16:59:15 -0700941 private void handleSetVmOrFwdMessage() {
942 if (DBG) log("handleSetVMMessage: set VM request complete");
943
944 if (!isFwdChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -0800945 handleVmOrFwdSetError(VoicemailDialogUtil.FWD_SET_RESPONSE_ERROR_DIALOG);
Andrew Leee438b312014-10-29 16:59:15 -0700946 } else if (!isVmChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -0800947 handleVmOrFwdSetError(VoicemailDialogUtil.VM_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700948 } else {
Andrew Lee84024342014-11-06 23:37:09 -0800949 handleVmAndFwdSetSuccess(VoicemailDialogUtil.VM_CONFIRM_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700950 }
951 }
952
953 /**
954 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
955 * changes to those settings and show "failure" dialog.
956 *
Andrew Leeab082272014-11-04 15:50:42 -0800957 * @param dialogId ID of the dialog to show for the specific error case. Either
Andrew Lee84024342014-11-06 23:37:09 -0800958 * {@link #FWD_SET_RESPONSE_ERROR_DIALOG} or {@link #VM_RESPONSE_ERROR_DIALOG}
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700959 */
Andrew Leeab082272014-11-04 15:50:42 -0800960 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700961 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -0800962 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700963 mChangingVMorFwdDueToProviderChange = false;
964 switchToPreviousVoicemailProvider();
965 return;
966 }
967 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -0800968 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700969 updateVoiceNumberField();
970 }
971
972 /**
973 * Called when Voicemail Provider and its forwarding settings were successfully finished.
974 * This updates a bunch of variables and show "success" dialog.
975 */
Andrew Leeab082272014-11-04 15:50:42 -0800976 private void handleVmAndFwdSetSuccess(int dialogId) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800977 if (DBG) log("handleVmAndFwdSetSuccess: key is " + mVoicemailProviders.getKey());
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800978
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800979 mPreviousVMProviderKey = mVoicemailProviders.getKey();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700980 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -0800981 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700982 updateVoiceNumberField();
983 }
984
985 /**
986 * Update the voicemail number from what we've recorded on the sim.
987 */
988 private void updateVoiceNumberField() {
Andrew Lee2d5d1a42014-11-05 12:34:14 -0800989 if (DBG) log("updateVoiceNumberField()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700990
991 mOldVmNumber = mPhone.getVoiceMailNumber();
Andrew Lee2d5d1a42014-11-05 12:34:14 -0800992 if (TextUtils.isEmpty(mOldVmNumber)) {
993 mSubMenuVoicemailSettings.setPhoneNumber("");
994 mSubMenuVoicemailSettings.setSummary(getString(R.string.voicemail_number_not_set));
995 } else {
996 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
997 mSubMenuVoicemailSettings.setSummary(mOldVmNumber);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700998 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700999 }
1000
1001 /*
1002 * Helper Methods for Activity class.
1003 * The initial query commands are split into two pieces now
1004 * for individual expansion. This combined with the ability
1005 * to cancel queries allows for a much better user experience,
1006 * and also ensures that the user only waits to update the
1007 * data that is relevant.
1008 */
1009
1010 @Override
1011 protected void onPrepareDialog(int id, Dialog dialog) {
1012 super.onPrepareDialog(id, dialog);
1013 mCurrentDialogId = id;
1014 }
1015
1016 // dialog creation method, called by showDialog()
1017 @Override
Andrew Lee84024342014-11-06 23:37:09 -08001018 protected Dialog onCreateDialog(int dialogId) {
1019 return VoicemailDialogUtil.getDialog(this, dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001020 }
1021
1022 // This is a method implemented for DialogInterface.OnClickListener.
1023 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1024 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1025 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1026 public void onClick(DialogInterface dialog, int which) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001027 if (DBG) log("onClick: button clicked is " + which);
1028
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001029 dialog.dismiss();
1030 switch (which){
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001031 case DialogInterface.BUTTON_NEGATIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001032 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001033 // We failed to get current forwarding settings and the user
1034 // does not wish to continue.
1035 switchToPreviousVoicemailProvider();
1036 }
1037 break;
1038 case DialogInterface.BUTTON_POSITIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001039 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001040 // We failed to get current forwarding settings but the user
1041 // wishes to continue changing settings to the new vm provider
Andrew Lee9c822312015-03-13 07:52:08 -07001042 setVoicemailNumberWithCarrier();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001043 } else {
1044 finish();
1045 }
1046 return;
1047 default:
1048 // just let the dialog close and go back to the input
1049 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001050
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001051 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1052 // with settings UI. If we were called to explicitly configure voice mail then
1053 // we finish the settings activity here to come back to whatever the user was doing.
1054 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1055 finish();
1056 }
1057 }
1058
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001059 /*
1060 * Activity class methods
1061 */
1062
1063 @Override
1064 protected void onCreate(Bundle icicle) {
1065 super.onCreate(icicle);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001066 if (DBG) log("onCreate: Intent is " + getIntent());
1067
Jay Shraunerc2684732014-11-12 12:10:37 -08001068 // Make sure we are running as the primary user.
1069 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
1070 Toast.makeText(this, R.string.call_settings_primary_user_only,
1071 Toast.LENGTH_SHORT).show();
1072 finish();
1073 return;
1074 }
1075
Tyler Gunnbaee2952014-09-10 16:01:02 -07001076 // Show the voicemail preference in onResume if the calling intent specifies the
1077 // ACTION_ADD_VOICEMAIL action.
1078 mShowVoicemailPreference = (icicle == null) &&
Jay Shraunerbe2fb262014-11-11 15:19:58 -08001079 TextUtils.equals(getIntent().getAction(), ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -08001080 mSetupVoicemail = mShowVoicemailPreference &&
1081 getIntent().getBooleanExtra(SETUP_VOICEMAIL_EXTRA, false);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001082
Andrew Leedd4f6df2014-12-09 19:13:51 -08001083 mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent());
Andrew Lee5ed870c2014-10-29 11:47:49 -07001084 mSubscriptionInfoHelper.setActionBarTitle(
1085 getActionBar(), getResources(), R.string.call_settings_with_label);
Andrew Lee1479dd12014-11-06 23:06:32 -08001086 mPhone = mSubscriptionInfoHelper.getPhone();
Tyler Gunnbaee2952014-09-10 16:01:02 -07001087 }
1088
Tyler Gunnbaee2952014-09-10 16:01:02 -07001089 @Override
1090 protected void onResume() {
1091 super.onResume();
1092 mForeground = true;
1093
1094 PreferenceScreen preferenceScreen = getPreferenceScreen();
1095 if (preferenceScreen != null) {
1096 preferenceScreen.removeAll();
1097 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001098
1099 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001100
Andrew Lee6fee2cb2014-12-09 16:42:18 -08001101 TelecomManager telecomManager = TelecomManager.from(this);
1102 TelephonyManager telephonyManager =
1103 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
1104
1105 Preference phoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
1106 if (telephonyManager.isMultiSimEnabled() || (telecomManager.getSimCallManagers().isEmpty()
1107 && !SipUtil.isVoipSupported(mPhone.getContext()))) {
1108 getPreferenceScreen().removePreference(phoneAccountSettingsPreference);
1109 }
Andrew Leedb2fe562014-09-03 15:40:43 -07001110
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001111 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001112 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1113 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1114 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1115 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001116
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001117 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001118
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001119 mVoicemailProviders = (VoicemailProviderListPreference) findPreference(
1120 BUTTON_VOICEMAIL_PROVIDER_KEY);
1121 mVoicemailProviders.init(mPhone, getIntent());
Andrew Lee2c027892014-10-29 11:29:54 -07001122 mVoicemailProviders.setOnPreferenceChangeListener(this);
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001123 mPreviousVMProviderKey = mVoicemailProviders.getValue();
Andrew Lee8d66d812014-11-24 14:54:02 -08001124
Andrew Lee2c027892014-10-29 11:29:54 -07001125 mVoicemailSettingsScreen =
1126 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001127 mVoicemailSettings = (PreferenceScreen) findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
Andrew Lee8d66d812014-11-24 14:54:02 -08001128
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001129 mVoicemailNotificationRingtone = (VoicemailRingtonePreference) findPreference(
1130 getResources().getString(R.string.voicemail_notification_ringtone_key));
1131 mVoicemailNotificationRingtone.init(mPhone);
1132
Andrew Lee8d66d812014-11-24 14:54:02 -08001133 mVoicemailNotificationVibrate = (CheckBoxPreference) findPreference(
1134 getResources().getString(R.string.voicemail_notification_vibrate_key));
1135 mVoicemailNotificationVibrate.setOnPreferenceChangeListener(this);
1136
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001137 updateVMPreferenceWidgets(mVoicemailProviders.getValue());
1138
1139 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001140
Andrew Lee64a7d792014-10-15 17:38:38 -07001141 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1142 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1143 int autoretry = Settings.Global.getInt(
1144 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1145 mButtonAutoRetry.setChecked(autoretry != 0);
1146 } else {
1147 prefSet.removePreference(mButtonAutoRetry);
1148 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001149 }
1150
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001151 if (!getResources().getBoolean(R.bool.world_phone)) {
Andrew Lee2b36ba22014-11-05 17:08:49 -08001152 Preference cdmaOptions = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
1153 prefSet.removePreference(cdmaOptions);
1154
1155 // TODO: Support MSIM for this preference option.
1156 Preference gsmOptions = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
1157 prefSet.removePreference(gsmOptions);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001158
1159 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001160 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Junda Liua09d6962014-11-12 13:39:02 -08001161 boolean shouldHideCarrierSettings = Settings.Global.getInt(
1162 getContentResolver(), Settings.Global.HIDE_CARRIER_NETWORK_SETTINGS, 0) == 1;
1163 if (shouldHideCarrierSettings) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001164 prefSet.removePreference(fdnButton);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001165 } else {
Junda Liua09d6962014-11-12 13:39:02 -08001166 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1167 prefSet.removePreference(fdnButton);
1168
1169 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1170 addPreferencesFromResource(R.xml.cdma_call_privacy);
1171 }
1172 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
Andrew Leedd4f6df2014-12-09 19:13:51 -08001173 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(FdnSetting.class));
Junda Liua09d6962014-11-12 13:39:02 -08001174
1175 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
1176 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1177
1178 Preference callForwardingPref = prefSet.findPreference(CALL_FORWARDING_KEY);
1179 callForwardingPref.setIntent(mSubscriptionInfoHelper.getIntent(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001180 GsmUmtsCallForwardOptions.class));
Junda Liua09d6962014-11-12 13:39:02 -08001181
1182 Preference additionalGsmSettingsPref =
1183 prefSet.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
1184 additionalGsmSettingsPref.setIntent(mSubscriptionInfoHelper.getIntent(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001185 GsmUmtsAdditionalCallOptions.class));
Junda Liua09d6962014-11-12 13:39:02 -08001186 }
1187 } else {
1188 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1189 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001190 }
1191 }
1192
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001193 // check the intent that started this activity and pop up the voicemail
1194 // dialog if we've been asked to.
1195 // If we have at least one non default VM provider registered then bring up
1196 // the selection for the VM provider, otherwise bring up a VM number dialog.
1197 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001198 if (mShowVoicemailPreference) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001199 if (DBG) log("ACTION_ADD_VOICEMAIL Intent is thrown");
Tyler Gunn9c1071f2014-12-09 10:07:54 -08001200 if (mSetupVoicemail) {
1201 simulatePreferenceClick(mVoicemailSettingsScreen);
1202 mSetupVoicemail = false;
1203 } else if (mVoicemailProviders.hasMoreThanOneVoicemailProvider()) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001204 if (DBG) log("Voicemail data has more than one provider.");
Tyler Gunnbaee2952014-09-10 16:01:02 -07001205 simulatePreferenceClick(mVoicemailProviders);
1206 } else {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001207 onPreferenceChange(mVoicemailProviders, VoicemailProviderListPreference.DEFAULT_KEY);
1208 mVoicemailProviders.setValue(VoicemailProviderListPreference.DEFAULT_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001209 }
1210 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001211 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001212
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001213 updateVoiceNumberField();
1214 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001215
Andrew Lee8d66d812014-11-24 14:54:02 -08001216 mVoicemailNotificationVibrate.setChecked(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001217 VoicemailNotificationSettingsUtil.isVibrationEnabled(mPhone));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001218
Andrew Lee312e8172014-10-23 17:01:36 -07001219 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1220 boolean currentValue =
1221 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1222 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1223 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001224 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1225 } else {
1226 prefSet.removePreference(mEnableVideoCalling);
1227 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001228 }
1229
Andrew Lee785ca2a2014-12-18 12:46:41 -08001230 @Override
1231 protected void onNewIntent(Intent newIntent) {
1232 setIntent(newIntent);
1233
1234 mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent());
1235 mSubscriptionInfoHelper.setActionBarTitle(
1236 getActionBar(), getResources(), R.string.call_settings_with_label);
1237 mPhone = mSubscriptionInfoHelper.getPhone();
1238 }
1239
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001240 private static void log(String msg) {
1241 Log.d(LOG_TAG, msg);
1242 }
1243
1244 /**
1245 * Updates the look of the VM preference widgets based on current VM provider settings.
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001246 * Note that the provider name is loaded fxrorm the found activity via loadLabel in
1247 * {@link VoicemailProviderListPreference#initVoiceMailProviders()} in order for it to be
1248 * localizable.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001249 */
1250 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1251 final String key = currentProviderSetting;
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001252 final VoicemailProvider provider = mVoicemailProviders.getVoicemailProvider(key);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001253
1254 /* This is the case when we are coming up on a freshly wiped phone and there is no
1255 persisted value for the list preference mVoicemailProviders.
1256 In this case we want to show the UI asking the user to select a voicemail provider as
1257 opposed to silently falling back to default one. */
1258 if (provider == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001259 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> null.");
1260
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001261 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1262 mVoicemailSettings.setEnabled(false);
1263 mVoicemailSettings.setIntent(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001264 mVoicemailNotificationVibrate.setEnabled(false);
1265 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001266 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> " + provider.toString());
1267
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001268 final String providerName = provider.name;
1269 mVoicemailProviders.setSummary(providerName);
1270 mVoicemailSettings.setEnabled(true);
1271 mVoicemailSettings.setIntent(provider.intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001272 mVoicemailNotificationVibrate.setEnabled(true);
1273 }
1274 }
1275
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001276
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001277 /**
1278 * Simulates user clicking on a passed preference.
1279 * Usually needed when the preference is a dialog preference and we want to invoke
1280 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001281 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001282 */
1283 private void simulatePreferenceClick(Preference preference) {
1284 // Go through settings until we find our setting
1285 // and then simulate a click on it to bring up the dialog
1286 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1287 for (int idx = 0; idx < adapter.getCount(); idx++) {
1288 if (adapter.getItem(idx) == preference) {
1289 getPreferenceScreen().onItemClick(this.getListView(),
1290 null, idx, adapter.getItemId(idx));
1291 break;
1292 }
1293 }
1294 }
1295
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001296 @Override
1297 public boolean onOptionsItemSelected(MenuItem item) {
1298 final int itemId = item.getItemId();
1299 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001300 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001301 return true;
1302 }
1303 return super.onOptionsItemSelected(item);
1304 }
Andrew Lee8d66d812014-11-24 14:54:02 -08001305
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001306 /**
1307 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1308 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1309 */
Andrew Lee5efb1122014-12-05 04:20:42 -08001310 public static void goUpToTopLevelSetting(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001311 Activity activity, SubscriptionInfoHelper subscriptionInfoHelper) {
1312 Intent intent = subscriptionInfoHelper.getIntent(CallFeaturesSetting.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001313 intent.setAction(Intent.ACTION_MAIN);
1314 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1315 activity.startActivity(intent);
1316 activity.finish();
1317 }
1318}