blob: 3bb3080b1d4f591b5fb4d5656898af939e814167 [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 Leeeffef9c2015-02-19 16:02:37 -080050import com.android.ims.ImsConfig;
Andrew Lee312e8172014-10-23 17:01:36 -070051import com.android.ims.ImsManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import com.android.internal.telephony.CallForwardInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import com.android.internal.telephony.Phone;
54import com.android.internal.telephony.PhoneConstants;
Andrew Lee2170a972014-08-13 18:13:01 -070055import com.android.phone.common.util.SettingsUtil;
Andrew Leedb2fe562014-09-03 15:40:43 -070056import com.android.phone.settings.AccountSelectionPreference;
Andrew Lee1af6cf72014-11-04 17:35:26 -080057import com.android.phone.settings.CallForwardInfoUtil;
Andrew Leefb7f92e2015-02-26 16:23:32 -080058import com.android.phone.settings.SettingsConstants;
Andrew Lee84024342014-11-06 23:37:09 -080059import com.android.phone.settings.VoicemailDialogUtil;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -080060import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Andrew Lee64e5c9d2014-11-26 12:00:10 -080061import com.android.phone.settings.VoicemailProviderListPreference;
62import com.android.phone.settings.VoicemailProviderListPreference.VoicemailProvider;
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 /**
129 * String Extra put into ACTION_ADD_VOICEMAIL call to indicate which provider should be hidden
130 * in the list of providers presented to the user. This allows a provider which is being
131 * disabled (e.g. GV user logging out) to force the user to pick some other provider.
132 */
133 public static final String IGNORE_PROVIDER_EXTRA = "com.android.phone.ProviderToIgnore";
134
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800135 /**
136 * String Extra put into ACTION_ADD_VOICEMAIL to indicate that the voicemail setup screen should
137 * be opened.
138 */
139 public static final String SETUP_VOICEMAIL_EXTRA = "com.android.phone.SetupVoicemail";
140
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700141 // string constants
142 private static final String NUM_PROJECTION[] = {CommonDataKinds.Phone.NUMBER};
143
144 // String keys for preference lookup
145 // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
Andrew Lee8d66d812014-11-24 14:54:02 -0800146 // TODO: Consider moving these strings to strings.xml, so that they are not duplicated here and
147 // in the layout files. These strings need to be treated carefully; if the setting is
148 // persistent, they are used as the key to store shared preferences and the name should not be
149 // changed unless the settings are also migrated.
Andrew Lee97708a42014-09-25 12:39:07 -0700150 private static final String VOICEMAIL_SETTING_SCREEN_PREF_KEY = "button_voicemail_category_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700151 private static final String BUTTON_VOICEMAIL_KEY = "button_voicemail_key";
152 private static final String BUTTON_VOICEMAIL_PROVIDER_KEY = "button_voicemail_provider_key";
153 private static final String BUTTON_VOICEMAIL_SETTING_KEY = "button_voicemail_setting_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700154 private static final String BUTTON_FDN_KEY = "button_fdn_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700155
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700156 private static final String BUTTON_RETRY_KEY = "button_auto_retry_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 /** Handle to voicemail pref */
174 private static final int VOICEMAIL_PREF_ID = 1;
175 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
176
177 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178
Andrew Lee5ed870c2014-10-29 11:47:49 -0700179 private SubscriptionInfoHelper mSubscriptionInfoHelper;
180
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700181 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
182
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700183 private CheckBoxPreference mButtonAutoRetry;
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800184 private VoicemailProviderListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700185 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700186 private PreferenceScreen mVoicemailSettings;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800187 private VoicemailRingtonePreference mVoicemailNotificationRingtone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700189 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700190
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700191 /**
192 * Results of reading forwarding settings
193 */
194 private CallForwardInfo[] mForwardingReadResults = null;
195
196 /**
197 * Result of forwarding number change.
198 * Keys are reasons (eg. unconditional forwarding).
199 */
200 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
201
202 /**
203 * Expected CF read result types.
204 * This set keeps track of the CF types for which we've issued change
205 * commands so we can tell when we've received all of the responses.
206 */
207 private Collection<Integer> mExpectedChangeResultReasons = null;
208
209 /**
210 * Result of vm number change
211 */
212 private AsyncResult mVoicemailChangeResult = null;
213
214 /**
215 * Previous VM provider setting so we can return to it in case of failure.
216 */
217 private String mPreviousVMProviderKey = null;
218
219 /**
220 * Id of the dialog being currently shown.
221 */
222 private int mCurrentDialogId = 0;
223
224 /**
225 * Flag indicating that we are invoking settings for the voicemail provider programmatically
226 * due to vm provider change.
227 */
228 private boolean mVMProviderSettingsForced = false;
229
230 /**
231 * Flag indicating that we are making changes to vm or fwd numbers
232 * due to vm provider change.
233 */
234 private boolean mChangingVMorFwdDueToProviderChange = false;
235
236 /**
237 * True if we are in the process of vm & fwd number change and vm has already been changed.
238 * This is used to decide what to do in case of rollback.
239 */
240 private boolean mVMChangeCompletedSuccessfully = false;
241
242 /**
243 * True if we had full or partial failure setting forwarding numbers and so need to roll them
244 * back.
245 */
246 private boolean mFwdChangesRequireRollback = false;
247
248 /**
249 * Id of error msg to display to user once we are done reverting the VM provider to the previous
250 * one.
251 */
252 private int mVMOrFwdSetError = 0;
253
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700254 /** string to hold old voicemail number as it is being updated. */
255 private String mOldVmNumber;
256
257 // New call forwarding settings and vm number we will be setting
258 // Need to save these since before we get to saving we need to asynchronously
259 // query the existing forwarding settings.
260 private CallForwardInfo[] mNewFwdSettings;
261 private String mNewVMNumber;
262
263 private boolean mForeground;
264
265 @Override
266 public void onPause() {
267 super.onPause();
268 mForeground = false;
269 }
270
271 /**
Tyler Gunnbaee2952014-09-10 16:01:02 -0700272 * Used to indicate that the voicemail preference should be shown.
273 */
274 private boolean mShowVoicemailPreference = false;
275
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800276 /**
277 * Used to indicate that the voicemail setup screen should be shown.
278 */
279 private boolean mSetupVoicemail = false;
280
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700281 /*
282 * Click Listeners, handle click based on objects attached to UI.
283 */
284
285 // Click listener for all toggle events
286 @Override
287 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
288 if (preference == mSubMenuVoicemailSettings) {
289 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700290 } else if (preference == mButtonAutoRetry) {
291 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
292 android.provider.Settings.Global.CALL_AUTO_RETRY,
293 mButtonAutoRetry.isChecked() ? 1 : 0);
294 return true;
Andrew Lee696bff52014-12-03 17:58:18 -0800295 } else if (preference.getKey().equals(mVoicemailSettings.getKey())) {
296 // Check key instead of comparing reference because closing the voicemail notification
297 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
298 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800299 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
300
Andrew Lee696bff52014-12-03 17:58:18 -0800301 final Dialog dialog = ((PreferenceScreen) preference).getDialog();
Yorke Leea0f63bf2014-10-09 18:27:20 -0700302 if (dialog != null) {
303 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
304 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800305
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700306 if (preference.getIntent() != null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800307 if (DBG) log("Invoking cfg intent " + preference.getIntent().getPackage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700308
309 // onActivityResult() will be responsible for resetting some of variables.
310 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
311 return true;
312 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800313 if (DBG) log("onPreferenceTreeClick(). No intent; use default behavior in xml.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700314
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800315 // onActivityResult() will not be called, so reset variables here.
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800316 mPreviousVMProviderKey = VoicemailProviderListPreference.DEFAULT_KEY;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700317 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700318 return false;
319 }
Andrew Lee97708a42014-09-25 12:39:07 -0700320 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700321 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
322 if (dialog != null) {
323 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
324 }
Andrew Lee97708a42014-09-25 12:39:07 -0700325 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700326 }
327 return false;
328 }
329
330 /**
331 * Implemented to support onPreferenceChangeListener to look for preference
332 * changes.
333 *
334 * @param preference is the preference to be changed
335 * @param objValue should be the value of the selection, NOT its localized
336 * display value.
337 */
338 @Override
339 public boolean onPreferenceChange(Preference preference, Object objValue) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800340 if (DBG) log("onPreferenceChange: \"" + preference + "\" changed to \"" + objValue + "\"");
Andrew Lee2170a972014-08-13 18:13:01 -0700341
Andrew Leea1239f22015-03-02 17:44:07 -0800342 if (preference == mVoicemailProviders) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700343 final String newProviderKey = (String) objValue;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800344
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700345 // If previous provider key and the new one is same, we don't need to handle it.
346 if (mPreviousVMProviderKey.equals(newProviderKey)) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800347 if (DBG) log("No change is made to the VM provider setting.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700348 return true;
349 }
350 updateVMPreferenceWidgets(newProviderKey);
351
Andrew Leeb490d732014-10-27 15:00:41 -0700352 final VoicemailProviderSettings newProviderSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800353 VoicemailProviderSettingsUtil.load(this, newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700354
Andrew Lee88b51e22014-10-29 15:48:51 -0700355 // If the user switches to a voice mail provider and we have numbers stored for it we
356 // will automatically change the phone's voice mail and forwarding number to the stored
357 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700358 if (newProviderSettings == null) {
359 // Force the user into a configuration of the chosen provider
360 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
361 mVMProviderSettingsForced = true;
362 simulatePreferenceClick(mVoicemailSettings);
363 } else {
364 if (DBG) log("Saved preferences found - switching to them");
365 // Set this flag so if we get a failure we revert to previous provider
366 mChangingVMorFwdDueToProviderChange = true;
367 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
368 }
Andrew Lee8d66d812014-11-24 14:54:02 -0800369 } else if (preference.getKey().equals(mVoicemailNotificationVibrate.getKey())) {
Andrew Lee696bff52014-12-03 17:58:18 -0800370 // Check key instead of comparing reference because closing the voicemail notification
371 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
372 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee8d66d812014-11-24 14:54:02 -0800373 VoicemailNotificationSettingsUtil.setVibrationEnabled(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800374 mPhone, Boolean.TRUE.equals(objValue));
Andrew Leedf14ead2014-10-17 14:22:52 -0700375 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700376 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
377 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
378 } else {
379 AlertDialog.Builder builder = new AlertDialog.Builder(this);
380 DialogInterface.OnClickListener networkSettingsClickListener =
381 new Dialog.OnClickListener() {
382 @Override
383 public void onClick(DialogInterface dialog, int which) {
384 startActivity(new Intent(mPhone.getContext(),
385 com.android.phone.MobileNetworkSettings.class));
386 }
387 };
388 builder.setMessage(getResources().getString(
389 R.string.enable_video_calling_dialog_msg))
390 .setNeutralButton(getResources().getString(
391 R.string.enable_video_calling_dialog_settings),
392 networkSettingsClickListener)
393 .setPositiveButton(android.R.string.ok, null)
394 .show();
395 return false;
396 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700397 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800398
399 // Always let the preference setting proceed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700400 return true;
401 }
402
403 @Override
404 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800405 if (DBG) log("onDialogClosed: Button clicked is " + buttonClicked);
406
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700407 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
408 return;
409 }
410
411 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700412 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
413 mSubMenuVoicemailSettings.getPhoneNumber(),
414 VoicemailProviderSettings.NO_FORWARDING);
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800415 saveVoiceMailAndForwardingNumber(mVoicemailProviders.getKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700416 }
417 }
418
419 /**
420 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
421 * This method set the default values for the various
422 * EditPhoneNumberPreference dialogs.
423 */
424 @Override
425 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
426 if (preference == mSubMenuVoicemailSettings) {
427 // update the voicemail number field, which takes care of the
428 // mSubMenuVoicemailSettings itself, so we should return null.
429 if (DBG) log("updating default for voicemail dialog");
430 updateVoiceNumberField();
431 return null;
432 }
433
434 String vmDisplay = mPhone.getVoiceMailNumber();
435 if (TextUtils.isEmpty(vmDisplay)) {
436 // if there is no voicemail number, we just return null to
437 // indicate no contribution.
438 return null;
439 }
440
441 // Return the voicemail number prepended with "VM: "
442 if (DBG) log("updating default for call forwarding dialogs");
443 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
444 }
445
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700446 private void switchToPreviousVoicemailProvider() {
447 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700448
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800449 if (mPreviousVMProviderKey == null) {
450 return;
451 }
452
453 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
Andrew Lee84024342014-11-06 23:37:09 -0800454 showDialogIfForeground(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800455 final VoicemailProviderSettings prevSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800456 VoicemailProviderSettingsUtil.load(this, mPreviousVMProviderKey);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800457 if (prevSettings == null) {
458 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
459 + mPreviousVMProviderKey + "\" is null but should be loaded.");
460 }
461
462 if (mVMChangeCompletedSuccessfully) {
463 mNewVMNumber = prevSettings.getVoicemailNumber();
464 Log.i(LOG_TAG, "VM change is already completed successfully."
465 + "Have to revert VM back to " + mNewVMNumber + " again.");
466 mPhone.setVoiceMailNumber(
467 mPhone.getVoiceMailAlphaTag().toString(),
468 mNewVMNumber,
469 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
470 }
471
472 if (mFwdChangesRequireRollback) {
473 Log.i(LOG_TAG, "Requested to rollback forwarding changes.");
474
475 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
476 if (prevFwdSettings != null) {
477 Map<Integer, AsyncResult> results = mForwardingChangeResults;
478 resetForwardingChangeState();
479 for (int i = 0; i < prevFwdSettings.length; i++) {
480 CallForwardInfo fi = prevFwdSettings[i];
481 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
482 // Only revert the settings for which the update succeeded.
483 AsyncResult result = results.get(fi.reason);
484 if (result != null && result.exception == null) {
485 mExpectedChangeResultReasons.add(fi.reason);
486 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
487 mRevertOptionComplete.obtainMessage(
488 EVENT_FORWARDING_CHANGED, i, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700489 }
490 }
491 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700492 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800493 } else {
494 if (DBG) log("No need to revert");
495 onRevertDone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700496 }
497 }
498
499 private void onRevertDone() {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800500 if (DBG) log("onRevertDone: Changing provider key back to " + mPreviousVMProviderKey);
501
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700502 updateVMPreferenceWidgets(mPreviousVMProviderKey);
503 updateVoiceNumberField();
504 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800505 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700506 mVMOrFwdSetError = 0;
507 }
508 }
509
510 @Override
511 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
512 if (DBG) {
513 log("onActivityResult: requestCode: " + requestCode
514 + ", resultCode: " + resultCode
515 + ", data: " + data);
516 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800517
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700518 // there are cases where the contact picker may end up sending us more than one
519 // request. We want to ignore the request if we're not in the correct state.
520 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
521 boolean failure = false;
522
523 // No matter how the processing of result goes lets clear the flag
524 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
525 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
526 mVMProviderSettingsForced = false;
527
528 String vmNum = null;
529 if (resultCode != RESULT_OK) {
530 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
531 failure = true;
532 } else {
533 if (data == null) {
534 if (DBG) log("onActivityResult: vm provider cfg result has no data");
535 failure = true;
536 } else {
537 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
538 if (DBG) log("Provider requested signout");
539 if (isVMProviderSettingsForced) {
540 if (DBG) log("Going back to previous provider on signout");
541 switchToPreviousVoicemailProvider();
542 } else {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800543 final String victim = mVoicemailProviders.getKey();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700544 if (DBG) log("Relaunching activity and ignoring " + victim);
545 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
546 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
547 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
548 this.startActivity(i);
549 }
550 return;
551 }
552 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
553 if (vmNum == null || vmNum.length() == 0) {
554 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
555 failure = true;
556 }
557 }
558 }
559 if (failure) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800560 if (DBG) log("Failure in return from voicemail provider.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700561 if (isVMProviderSettingsForced) {
562 switchToPreviousVoicemailProvider();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700563 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800564
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700565 return;
566 }
567 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
568 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
569
Santos Cordonda120f42014-08-06 04:44:34 -0700570 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700571 // send it to the provider when it's config is invoked so it can use this as default
572 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
573
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800574 if (DBG) log("onActivityResult: cfg result has forwarding number " + fwdNum);
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800575 saveVoiceMailAndForwardingNumber(mVoicemailProviders.getKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700576 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700577 return;
578 }
579
580 if (requestCode == VOICEMAIL_PREF_ID) {
581 if (resultCode != RESULT_OK) {
582 if (DBG) log("onActivityResult: contact picker result not OK.");
583 return;
584 }
585
586 Cursor cursor = null;
587 try {
588 cursor = getContentResolver().query(data.getData(),
589 NUM_PROJECTION, null, null, null);
590 if ((cursor == null) || (!cursor.moveToFirst())) {
591 if (DBG) log("onActivityResult: bad contact data, no results found.");
592 return;
593 }
594 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
595 return;
596 } finally {
597 if (cursor != null) {
598 cursor.close();
599 }
600 }
601 }
602
603 super.onActivityResult(requestCode, resultCode, data);
604 }
605
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700606 /**
607 * Wrapper around showDialog() that will silently do nothing if we're
608 * not in the foreground.
609 *
610 * This is useful here because most of the dialogs we display from
611 * this class are triggered by asynchronous events (like
612 * success/failure messages from the telephony layer) and it's
613 * possible for those events to come in even after the user has gone
614 * to a different screen.
615 */
616 // TODO: this is too brittle: it's still easy to accidentally add new
617 // code here that calls showDialog() directly (which will result in a
618 // WindowManager$BadTokenException if called after the activity has
619 // been stopped.)
620 //
621 // It would be cleaner to do the "if (mForeground)" check in one
622 // central place, maybe by using a single Handler for all asynchronous
623 // events (and have *that* discard events if we're not in the
624 // foreground.)
625 //
626 // Unfortunately it's not that simple, since we sometimes need to do
627 // actual work to handle these events whether or not we're in the
628 // foreground (see the Handler code in mSetOptionComplete for
629 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800630 //
631 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
632 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700633 private void showDialogIfForeground(int id) {
634 if (mForeground) {
635 showDialog(id);
636 }
637 }
638
639 private void dismissDialogSafely(int id) {
640 try {
641 dismissDialog(id);
642 } catch (IllegalArgumentException e) {
643 // This is expected in the case where we were in the background
644 // at the time we would normally have shown the dialog, so we didn't
645 // show it.
646 }
647 }
648
Andrew Lee299e4a72015-01-15 12:44:34 -0800649 /**
650 * TODO: Refactor to make it easier to understand what's done in the different stages.
651 */
Andrew Leeb490d732014-10-27 15:00:41 -0700652 private void saveVoiceMailAndForwardingNumber(
653 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700654 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700655 mNewVMNumber = newSettings.getVoicemailNumber();
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800656 mNewVMNumber = (mNewVMNumber == null) ? "" : mNewVMNumber;
Andrew Leeb490d732014-10-27 15:00:41 -0700657 mNewFwdSettings = newSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700658
Andrew Leeb3bf3422015-03-13 17:59:00 -0700659 // Call forwarding is not suppported on CDMA.
660 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
661 if (DBG) log("Ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700662 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700663 }
664
Andrew Leee3c15212014-10-28 13:12:55 -0700665 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700666 if (mNewVMNumber.equals(mOldVmNumber)
667 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Lee84024342014-11-06 23:37:09 -0800668 showDialogIfForeground(VoicemailDialogUtil.VM_NOCHANGE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700669 return;
670 }
671
Andrew Lee6950c6c2014-11-26 12:05:42 -0800672 VoicemailProviderSettingsUtil.save(this, key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700673 mVMChangeCompletedSuccessfully = false;
674 mFwdChangesRequireRollback = false;
675 mVMOrFwdSetError = 0;
Andrew Lee299e4a72015-01-15 12:44:34 -0800676
Andrew Leeb3bf3422015-03-13 17:59:00 -0700677 if (mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING
678 || key.equals(mPreviousVMProviderKey)) {
679 if (DBG) log("Set voicemail number. No changes to forwarding number.");
Andrew Lee9c822312015-03-13 07:52:08 -0700680 setVoicemailNumberWithCarrier();
681 } else {
Andrew Leeb3bf3422015-03-13 17:59:00 -0700682 if (DBG) log("Reading current forwarding settings.");
Andrew Leeb490d732014-10-27 15:00:41 -0700683 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
684 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
685 for (int i = 0; i < mForwardingReadResults.length; i++) {
686 mPhone.getCallForwardingOption(
687 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700688 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
689 }
Andrew Lee84024342014-11-06 23:37:09 -0800690 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700691 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800692
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800693 PhoneGlobals.getInstance().refreshMwiIndicator(mSubscriptionInfoHelper.getSubId());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700694 }
695
696 private final Handler mGetOptionComplete = new Handler() {
697 @Override
698 public void handleMessage(Message msg) {
699 AsyncResult result = (AsyncResult) msg.obj;
700 switch (msg.what) {
701 case EVENT_FORWARDING_GET_COMPLETED:
702 handleForwardingSettingsReadResult(result, msg.arg1);
703 break;
704 }
705 }
706 };
707
708 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
709 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800710
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700711 Throwable error = null;
712 if (ar.exception != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700713 error = ar.exception;
Andrew Lee1af6cf72014-11-04 17:35:26 -0800714 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700715 }
716 if (ar.userObj instanceof Throwable) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800717 error = (Throwable) ar.userObj;
718 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700719 }
720
721 // We may have already gotten an error and decided to ignore the other results.
722 if (mForwardingReadResults == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800723 if (DBG) Log.d(LOG_TAG, "Ignoring fwd reading result: " + idx);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700724 return;
725 }
726
727 // In case of error ignore other results, show an error dialog
728 if (error != null) {
729 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
730 mForwardingReadResults = null;
Andrew Lee84024342014-11-06 23:37:09 -0800731 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
732 showDialogIfForeground(VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700733 return;
734 }
735
Andrew Lee1af6cf72014-11-04 17:35:26 -0800736 // Get the forwarding info.
737 mForwardingReadResults[idx] = CallForwardInfoUtil.getCallForwardInfo(
738 (CallForwardInfo[]) ar.result,
739 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx]);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700740
741 // Check if we got all the results already
742 boolean done = true;
743 for (int i = 0; i < mForwardingReadResults.length; i++) {
744 if (mForwardingReadResults[i] == null) {
745 done = false;
746 break;
747 }
748 }
Andrew Lee1af6cf72014-11-04 17:35:26 -0800749
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700750 if (done) {
751 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
Andrew Lee84024342014-11-06 23:37:09 -0800752 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800753
Andrew Leeb3bf3422015-03-13 17:59:00 -0700754 if (mPreviousVMProviderKey.equals(VoicemailProviderListPreference.DEFAULT_KEY)) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800755 VoicemailProviderSettingsUtil.save(mPhone.getContext(),
756 VoicemailProviderListPreference.DEFAULT_KEY,
Andrew Lee299e4a72015-01-15 12:44:34 -0800757 new VoicemailProviderSettings(mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700758 }
759 saveVoiceMailAndForwardingNumberStage2();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700760 }
761 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800762
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700763 private void resetForwardingChangeState() {
764 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
765 mExpectedChangeResultReasons = new HashSet<Integer>();
766 }
767
Andrew Lee299e4a72015-01-15 12:44:34 -0800768 // Called after we are done saving the previous forwarding settings if we needed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700769 private void saveVoiceMailAndForwardingNumberStage2() {
770 mForwardingChangeResults = null;
771 mVoicemailChangeResult = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700772
Andrew Lee9c822312015-03-13 07:52:08 -0700773 resetForwardingChangeState();
774 for (int i = 0; i < mNewFwdSettings.length; i++) {
775 CallForwardInfo fi = mNewFwdSettings[i];
776 CallForwardInfo fiForReason =
777 CallForwardInfoUtil.infoForReason(mForwardingReadResults, fi.reason);
778 final boolean doUpdate = CallForwardInfoUtil.isUpdateRequired(fiForReason, fi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700779
Andrew Lee9c822312015-03-13 07:52:08 -0700780 if (doUpdate) {
781 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
782 mExpectedChangeResultReasons.add(i);
783
784 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
785 mSetOptionComplete.obtainMessage(
786 EVENT_FORWARDING_CHANGED, fi.reason, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700787 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700788 }
Andrew Lee9c822312015-03-13 07:52:08 -0700789 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700790 }
791
Andrew Lee9c822312015-03-13 07:52:08 -0700792 private void setVoicemailNumberWithCarrier() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700793 if (DBG) log("save voicemail #: " + mNewVMNumber);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800794
Andrew Lee9c822312015-03-13 07:52:08 -0700795 mVoicemailChangeResult = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700796 mPhone.setVoiceMailNumber(
797 mPhone.getVoiceMailAlphaTag().toString(),
798 mNewVMNumber,
799 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
800 }
801
802 /**
803 * Callback to handle option update completions
804 */
805 private final Handler mSetOptionComplete = new Handler() {
806 @Override
807 public void handleMessage(Message msg) {
808 AsyncResult result = (AsyncResult) msg.obj;
809 boolean done = false;
810 switch (msg.what) {
811 case EVENT_VOICEMAIL_CHANGED:
812 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700813 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700814 done = true;
815 break;
816 case EVENT_FORWARDING_CHANGED:
817 mForwardingChangeResults.put(msg.arg1, result);
818 if (result.exception != null) {
819 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
820 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700821 }
Andrew Leee438b312014-10-29 16:59:15 -0700822 if (isForwardingCompleted()) {
823 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700824 if (DBG) log("Overall fwd changes completed ok, starting vm change");
Andrew Lee9c822312015-03-13 07:52:08 -0700825 setVoicemailNumberWithCarrier();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700826 } else {
827 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
828 "Check if we need to try rollback for some settings.");
829 mFwdChangesRequireRollback = false;
830 Iterator<Map.Entry<Integer,AsyncResult>> it =
831 mForwardingChangeResults.entrySet().iterator();
832 while (it.hasNext()) {
833 Map.Entry<Integer,AsyncResult> entry = it.next();
834 if (entry.getValue().exception == null) {
835 // If at least one succeeded we have to revert
836 Log.i(LOG_TAG, "Rollback will be required");
837 mFwdChangesRequireRollback = true;
838 break;
839 }
840 }
841 if (!mFwdChangesRequireRollback) {
842 Log.i(LOG_TAG, "No rollback needed.");
843 }
844 done = true;
845 }
846 }
847 break;
848 default:
849 // TODO: should never reach this, may want to throw exception
850 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800851
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700852 if (done) {
853 if (DBG) log("All VM provider related changes done");
854 if (mForwardingChangeResults != null) {
Andrew Lee84024342014-11-06 23:37:09 -0800855 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700856 }
Andrew Leee438b312014-10-29 16:59:15 -0700857 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700858 }
859 }
860 };
861
862 /**
863 * Callback to handle option revert completions
864 */
865 private final Handler mRevertOptionComplete = new Handler() {
866 @Override
867 public void handleMessage(Message msg) {
868 AsyncResult result = (AsyncResult) msg.obj;
869 switch (msg.what) {
870 case EVENT_VOICEMAIL_CHANGED:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700871 if (DBG) log("VM revert complete msg");
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800872 mVoicemailChangeResult = result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700873 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800874
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700875 case EVENT_FORWARDING_CHANGED:
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800876 if (DBG) log("FWD revert complete msg ");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700877 mForwardingChangeResults.put(msg.arg1, result);
878 if (result.exception != null) {
879 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
880 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700881 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700882 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800883
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700884 default:
885 // TODO: should never reach this, may want to throw exception
886 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800887
888 final boolean done = (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null)
889 && (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700890 if (done) {
891 if (DBG) log("All VM reverts done");
Andrew Lee84024342014-11-06 23:37:09 -0800892 dismissDialogSafely(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700893 onRevertDone();
894 }
895 }
896 };
897
898 /**
Andrew Leee438b312014-10-29 16:59:15 -0700899 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700900 */
Andrew Leee438b312014-10-29 16:59:15 -0700901 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700902 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -0700903 return true;
904 }
905
906 for (Integer reason : mExpectedChangeResultReasons) {
907 if (mForwardingChangeResults.get(reason) == null) {
908 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700909 }
910 }
Andrew Leee438b312014-10-29 16:59:15 -0700911
912 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700913 }
Andrew Leee438b312014-10-29 16:59:15 -0700914
915 private boolean isFwdChangeSuccess() {
916 if (mForwardingChangeResults == null) {
917 return true;
918 }
919
920 for (AsyncResult result : mForwardingChangeResults.values()) {
921 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700922 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700923 String msg = exception.getMessage();
924 msg = (msg != null) ? msg : "";
925 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
926 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700927 }
928 }
Andrew Leee438b312014-10-29 16:59:15 -0700929 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700930 }
931
Andrew Leee438b312014-10-29 16:59:15 -0700932 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700933 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700934 String msg = mVoicemailChangeResult.exception.getMessage();
935 msg = (msg != null) ? msg : "";
936 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
937 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700938 }
Andrew Leee438b312014-10-29 16:59:15 -0700939 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700940 }
941
Andrew Leee438b312014-10-29 16:59:15 -0700942 private void handleSetVmOrFwdMessage() {
943 if (DBG) log("handleSetVMMessage: set VM request complete");
944
945 if (!isFwdChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -0800946 handleVmOrFwdSetError(VoicemailDialogUtil.FWD_SET_RESPONSE_ERROR_DIALOG);
Andrew Leee438b312014-10-29 16:59:15 -0700947 } else if (!isVmChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -0800948 handleVmOrFwdSetError(VoicemailDialogUtil.VM_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700949 } else {
Andrew Lee84024342014-11-06 23:37:09 -0800950 handleVmAndFwdSetSuccess(VoicemailDialogUtil.VM_CONFIRM_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700951 }
952 }
953
954 /**
955 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
956 * changes to those settings and show "failure" dialog.
957 *
Andrew Leeab082272014-11-04 15:50:42 -0800958 * @param dialogId ID of the dialog to show for the specific error case. Either
Andrew Lee84024342014-11-06 23:37:09 -0800959 * {@link #FWD_SET_RESPONSE_ERROR_DIALOG} or {@link #VM_RESPONSE_ERROR_DIALOG}
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700960 */
Andrew Leeab082272014-11-04 15:50:42 -0800961 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700962 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -0800963 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700964 mChangingVMorFwdDueToProviderChange = false;
965 switchToPreviousVoicemailProvider();
966 return;
967 }
968 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -0800969 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700970 updateVoiceNumberField();
971 }
972
973 /**
974 * Called when Voicemail Provider and its forwarding settings were successfully finished.
975 * This updates a bunch of variables and show "success" dialog.
976 */
Andrew Leeab082272014-11-04 15:50:42 -0800977 private void handleVmAndFwdSetSuccess(int dialogId) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800978 if (DBG) log("handleVmAndFwdSetSuccess: key is " + mVoicemailProviders.getKey());
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800979
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800980 mPreviousVMProviderKey = mVoicemailProviders.getKey();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700981 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -0800982 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700983 updateVoiceNumberField();
984 }
985
986 /**
987 * Update the voicemail number from what we've recorded on the sim.
988 */
989 private void updateVoiceNumberField() {
Andrew Lee2d5d1a42014-11-05 12:34:14 -0800990 if (DBG) log("updateVoiceNumberField()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700991
992 mOldVmNumber = mPhone.getVoiceMailNumber();
Andrew Lee2d5d1a42014-11-05 12:34:14 -0800993 if (TextUtils.isEmpty(mOldVmNumber)) {
994 mSubMenuVoicemailSettings.setPhoneNumber("");
995 mSubMenuVoicemailSettings.setSummary(getString(R.string.voicemail_number_not_set));
996 } else {
997 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
998 mSubMenuVoicemailSettings.setSummary(mOldVmNumber);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700999 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001000 }
1001
1002 /*
1003 * Helper Methods for Activity class.
1004 * The initial query commands are split into two pieces now
1005 * for individual expansion. This combined with the ability
1006 * to cancel queries allows for a much better user experience,
1007 * and also ensures that the user only waits to update the
1008 * data that is relevant.
1009 */
1010
1011 @Override
1012 protected void onPrepareDialog(int id, Dialog dialog) {
1013 super.onPrepareDialog(id, dialog);
1014 mCurrentDialogId = id;
1015 }
1016
1017 // dialog creation method, called by showDialog()
1018 @Override
Andrew Lee84024342014-11-06 23:37:09 -08001019 protected Dialog onCreateDialog(int dialogId) {
1020 return VoicemailDialogUtil.getDialog(this, dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001021 }
1022
1023 // This is a method implemented for DialogInterface.OnClickListener.
1024 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1025 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1026 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1027 public void onClick(DialogInterface dialog, int which) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001028 if (DBG) log("onClick: button clicked is " + which);
1029
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001030 dialog.dismiss();
1031 switch (which){
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001032 case DialogInterface.BUTTON_NEGATIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001033 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001034 // We failed to get current forwarding settings and the user
1035 // does not wish to continue.
1036 switchToPreviousVoicemailProvider();
1037 }
1038 break;
1039 case DialogInterface.BUTTON_POSITIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001040 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001041 // We failed to get current forwarding settings but the user
1042 // wishes to continue changing settings to the new vm provider
Andrew Lee9c822312015-03-13 07:52:08 -07001043 setVoicemailNumberWithCarrier();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001044 } else {
1045 finish();
1046 }
1047 return;
1048 default:
1049 // just let the dialog close and go back to the input
1050 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001051
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001052 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1053 // with settings UI. If we were called to explicitly configure voice mail then
1054 // we finish the settings activity here to come back to whatever the user was doing.
1055 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1056 finish();
1057 }
1058 }
1059
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001060 /*
1061 * Activity class methods
1062 */
1063
1064 @Override
1065 protected void onCreate(Bundle icicle) {
1066 super.onCreate(icicle);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001067 if (DBG) log("onCreate: Intent is " + getIntent());
1068
Jay Shraunerc2684732014-11-12 12:10:37 -08001069 // Make sure we are running as the primary user.
1070 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
1071 Toast.makeText(this, R.string.call_settings_primary_user_only,
1072 Toast.LENGTH_SHORT).show();
1073 finish();
1074 return;
1075 }
1076
Tyler Gunnbaee2952014-09-10 16:01:02 -07001077 // Show the voicemail preference in onResume if the calling intent specifies the
1078 // ACTION_ADD_VOICEMAIL action.
1079 mShowVoicemailPreference = (icicle == null) &&
Jay Shraunerbe2fb262014-11-11 15:19:58 -08001080 TextUtils.equals(getIntent().getAction(), ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -08001081 mSetupVoicemail = mShowVoicemailPreference &&
1082 getIntent().getBooleanExtra(SETUP_VOICEMAIL_EXTRA, false);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001083
Andrew Leedd4f6df2014-12-09 19:13:51 -08001084 mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent());
Andrew Lee5ed870c2014-10-29 11:47:49 -07001085 mSubscriptionInfoHelper.setActionBarTitle(
1086 getActionBar(), getResources(), R.string.call_settings_with_label);
Andrew Lee1479dd12014-11-06 23:06:32 -08001087 mPhone = mSubscriptionInfoHelper.getPhone();
Tyler Gunnbaee2952014-09-10 16:01:02 -07001088 }
1089
Tyler Gunnbaee2952014-09-10 16:01:02 -07001090 @Override
1091 protected void onResume() {
1092 super.onResume();
1093 mForeground = true;
1094
1095 PreferenceScreen preferenceScreen = getPreferenceScreen();
1096 if (preferenceScreen != null) {
1097 preferenceScreen.removeAll();
1098 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001099
1100 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001101
Andrew Lee6fee2cb2014-12-09 16:42:18 -08001102 TelecomManager telecomManager = TelecomManager.from(this);
1103 TelephonyManager telephonyManager =
1104 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
1105
1106 Preference phoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
1107 if (telephonyManager.isMultiSimEnabled() || (telecomManager.getSimCallManagers().isEmpty()
1108 && !SipUtil.isVoipSupported(mPhone.getContext()))) {
1109 getPreferenceScreen().removePreference(phoneAccountSettingsPreference);
1110 }
Andrew Leedb2fe562014-09-03 15:40:43 -07001111
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001112 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001113 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1114 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1115 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1116 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001117
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001118 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001119
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001120 mVoicemailProviders = (VoicemailProviderListPreference) findPreference(
1121 BUTTON_VOICEMAIL_PROVIDER_KEY);
1122 mVoicemailProviders.init(mPhone, getIntent());
Andrew Lee2c027892014-10-29 11:29:54 -07001123 mVoicemailProviders.setOnPreferenceChangeListener(this);
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001124 mPreviousVMProviderKey = mVoicemailProviders.getValue();
Andrew Lee8d66d812014-11-24 14:54:02 -08001125
Andrew Lee2c027892014-10-29 11:29:54 -07001126 mVoicemailSettingsScreen =
1127 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001128 mVoicemailSettings = (PreferenceScreen) findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
Andrew Lee8d66d812014-11-24 14:54:02 -08001129
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001130 mVoicemailNotificationRingtone = (VoicemailRingtonePreference) findPreference(
1131 getResources().getString(R.string.voicemail_notification_ringtone_key));
1132 mVoicemailNotificationRingtone.init(mPhone);
1133
Andrew Lee8d66d812014-11-24 14:54:02 -08001134 mVoicemailNotificationVibrate = (CheckBoxPreference) findPreference(
1135 getResources().getString(R.string.voicemail_notification_vibrate_key));
1136 mVoicemailNotificationVibrate.setOnPreferenceChangeListener(this);
1137
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001138 updateVMPreferenceWidgets(mVoicemailProviders.getValue());
1139
1140 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001141
Andrew Lee64a7d792014-10-15 17:38:38 -07001142 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1143 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1144 int autoretry = Settings.Global.getInt(
1145 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1146 mButtonAutoRetry.setChecked(autoretry != 0);
1147 } else {
1148 prefSet.removePreference(mButtonAutoRetry);
1149 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001150 }
1151
Andrew Lee9431b832015-03-09 18:46:45 -07001152 if (!PhoneGlobals.getInstance().phoneMgr.isWorldPhone()) {
Andrew Lee2b36ba22014-11-05 17:08:49 -08001153 Preference cdmaOptions = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
1154 prefSet.removePreference(cdmaOptions);
1155
1156 // TODO: Support MSIM for this preference option.
1157 Preference gsmOptions = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
1158 prefSet.removePreference(gsmOptions);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001159
1160 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001161 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Junda Liua09d6962014-11-12 13:39:02 -08001162 boolean shouldHideCarrierSettings = Settings.Global.getInt(
1163 getContentResolver(), Settings.Global.HIDE_CARRIER_NETWORK_SETTINGS, 0) == 1;
1164 if (shouldHideCarrierSettings) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001165 prefSet.removePreference(fdnButton);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001166 } else {
Junda Liua09d6962014-11-12 13:39:02 -08001167 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1168 prefSet.removePreference(fdnButton);
1169
1170 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1171 addPreferencesFromResource(R.xml.cdma_call_privacy);
1172 }
1173 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
Andrew Leedd4f6df2014-12-09 19:13:51 -08001174 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(FdnSetting.class));
Junda Liua09d6962014-11-12 13:39:02 -08001175
1176 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
1177 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1178
1179 Preference callForwardingPref = prefSet.findPreference(CALL_FORWARDING_KEY);
1180 callForwardingPref.setIntent(mSubscriptionInfoHelper.getIntent(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001181 GsmUmtsCallForwardOptions.class));
Junda Liua09d6962014-11-12 13:39:02 -08001182
1183 Preference additionalGsmSettingsPref =
1184 prefSet.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
1185 additionalGsmSettingsPref.setIntent(mSubscriptionInfoHelper.getIntent(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001186 GsmUmtsAdditionalCallOptions.class));
Junda Liua09d6962014-11-12 13:39:02 -08001187 }
1188 } else {
1189 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1190 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001191 }
1192 }
1193
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001194 // check the intent that started this activity and pop up the voicemail
1195 // dialog if we've been asked to.
1196 // If we have at least one non default VM provider registered then bring up
1197 // the selection for the VM provider, otherwise bring up a VM number dialog.
1198 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001199 if (mShowVoicemailPreference) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001200 if (DBG) log("ACTION_ADD_VOICEMAIL Intent is thrown");
Tyler Gunn9c1071f2014-12-09 10:07:54 -08001201 if (mSetupVoicemail) {
1202 simulatePreferenceClick(mVoicemailSettingsScreen);
1203 mSetupVoicemail = false;
1204 } else if (mVoicemailProviders.hasMoreThanOneVoicemailProvider()) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001205 if (DBG) log("Voicemail data has more than one provider.");
Tyler Gunnbaee2952014-09-10 16:01:02 -07001206 simulatePreferenceClick(mVoicemailProviders);
1207 } else {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001208 onPreferenceChange(mVoicemailProviders, VoicemailProviderListPreference.DEFAULT_KEY);
1209 mVoicemailProviders.setValue(VoicemailProviderListPreference.DEFAULT_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001210 }
1211 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001212 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001213
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001214 updateVoiceNumberField();
1215 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001216
Andrew Lee8d66d812014-11-24 14:54:02 -08001217 mVoicemailNotificationVibrate.setChecked(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001218 VoicemailNotificationSettingsUtil.isVibrationEnabled(mPhone));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001219
Andrew Lee312e8172014-10-23 17:01:36 -07001220 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1221 boolean currentValue =
1222 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1223 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1224 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001225 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1226 } else {
1227 prefSet.removePreference(mEnableVideoCalling);
1228 }
Etan Cohena51d4ef2014-12-15 10:44:42 -08001229
1230 if (ImsManager.isVolteEnabledByPlatform(this) &&
1231 !mPhone.getContext().getResources().getBoolean(
1232 com.android.internal.R.bool.config_carrier_volte_tty_supported)) {
1233 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Vinit Deshpande84cccac2015-03-16 00:08:19 -07001234 /* tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); */
Etan Cohena51d4ef2014-12-15 10:44:42 -08001235 }
Andrew Leeb97fad82015-01-27 15:31:39 -08001236
Andrew Leeeffef9c2015-02-19 16:02:37 -08001237 Preference wifiCallingSettings = findPreference(
1238 getResources().getString(R.string.wifi_calling_settings_key));
Andrew Leeb97fad82015-01-27 15:31:39 -08001239 if (!ImsManager.isWfcEnabledByPlatform(mPhone.getContext())) {
Andrew Leeb97fad82015-01-27 15:31:39 -08001240 prefSet.removePreference(wifiCallingSettings);
Andrew Leeeffef9c2015-02-19 16:02:37 -08001241 } else {
1242 int resId = R.string.wifi_calling_off_summary;
1243 if (ImsManager.isWfcEnabledByUser(mPhone.getContext())) {
1244 int wfcMode = ImsManager.getWfcMode(mPhone.getContext());
1245 switch (wfcMode) {
1246 case ImsConfig.WfcModeFeatureValueConstants.WIFI_ONLY:
1247 resId = R.string.wfc_mode_wifi_only_summary;
1248 break;
1249 case ImsConfig.WfcModeFeatureValueConstants.CELLULAR_PREFERRED:
1250 resId = R.string.wfc_mode_cellular_preferred_summary;
1251 break;
1252 case ImsConfig.WfcModeFeatureValueConstants.WIFI_PREFERRED:
1253 resId = R.string.wfc_mode_wifi_preferred_summary;
1254 break;
1255 default:
1256 if (DBG) log("Unexpected WFC mode value: " + wfcMode);
1257 }
1258 }
1259 wifiCallingSettings.setSummary(resId);
Andrew Leeb97fad82015-01-27 15:31:39 -08001260 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001261 }
1262
Andrew Lee785ca2a2014-12-18 12:46:41 -08001263 @Override
1264 protected void onNewIntent(Intent newIntent) {
1265 setIntent(newIntent);
1266
1267 mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent());
1268 mSubscriptionInfoHelper.setActionBarTitle(
1269 getActionBar(), getResources(), R.string.call_settings_with_label);
1270 mPhone = mSubscriptionInfoHelper.getPhone();
1271 }
1272
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001273 private static void log(String msg) {
1274 Log.d(LOG_TAG, msg);
1275 }
1276
1277 /**
1278 * Updates the look of the VM preference widgets based on current VM provider settings.
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001279 * Note that the provider name is loaded fxrorm the found activity via loadLabel in
1280 * {@link VoicemailProviderListPreference#initVoiceMailProviders()} in order for it to be
1281 * localizable.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001282 */
1283 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1284 final String key = currentProviderSetting;
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001285 final VoicemailProvider provider = mVoicemailProviders.getVoicemailProvider(key);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001286
1287 /* This is the case when we are coming up on a freshly wiped phone and there is no
1288 persisted value for the list preference mVoicemailProviders.
1289 In this case we want to show the UI asking the user to select a voicemail provider as
1290 opposed to silently falling back to default one. */
1291 if (provider == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001292 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> null.");
1293
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001294 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1295 mVoicemailSettings.setEnabled(false);
1296 mVoicemailSettings.setIntent(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001297 mVoicemailNotificationVibrate.setEnabled(false);
1298 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001299 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> " + provider.toString());
1300
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001301 final String providerName = provider.name;
1302 mVoicemailProviders.setSummary(providerName);
1303 mVoicemailSettings.setEnabled(true);
1304 mVoicemailSettings.setIntent(provider.intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001305 mVoicemailNotificationVibrate.setEnabled(true);
1306 }
1307 }
1308
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001309
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001310 /**
1311 * Simulates user clicking on a passed preference.
1312 * Usually needed when the preference is a dialog preference and we want to invoke
1313 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001314 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001315 */
1316 private void simulatePreferenceClick(Preference preference) {
1317 // Go through settings until we find our setting
1318 // and then simulate a click on it to bring up the dialog
1319 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1320 for (int idx = 0; idx < adapter.getCount(); idx++) {
1321 if (adapter.getItem(idx) == preference) {
1322 getPreferenceScreen().onItemClick(this.getListView(),
1323 null, idx, adapter.getItemId(idx));
1324 break;
1325 }
1326 }
1327 }
1328
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001329 @Override
1330 public boolean onOptionsItemSelected(MenuItem item) {
1331 final int itemId = item.getItemId();
1332 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001333 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001334 return true;
1335 }
1336 return super.onOptionsItemSelected(item);
1337 }
Andrew Lee8d66d812014-11-24 14:54:02 -08001338
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001339 /**
1340 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1341 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1342 */
Andrew Lee5efb1122014-12-05 04:20:42 -08001343 public static void goUpToTopLevelSetting(
Andrew Leedd4f6df2014-12-09 19:13:51 -08001344 Activity activity, SubscriptionInfoHelper subscriptionInfoHelper) {
1345 Intent intent = subscriptionInfoHelper.getIntent(CallFeaturesSetting.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001346 intent.setAction(Intent.ACTION_MAIN);
1347 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1348 activity.startActivity(intent);
1349 activity.finish();
1350 }
1351}