blob: e67071a43dac4ef8c1bea4a24984319b155f0b65 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
19import android.app.ActionBar;
20import android.app.Activity;
Evan Charlton1c696832014-04-15 14:24:23 -070021import android.app.ActivityOptions;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.AlertDialog;
23import android.app.Dialog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.ContentResolver;
25import android.content.Context;
26import android.content.DialogInterface;
27import android.content.Intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.content.pm.ActivityInfo;
29import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
31import android.database.Cursor;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070032import android.media.AudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070033import android.os.AsyncResult;
34import android.os.Bundle;
35import android.os.Handler;
36import android.os.Message;
37import android.os.UserHandle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.preference.CheckBoxPreference;
39import android.preference.ListPreference;
40import android.preference.Preference;
41import android.preference.PreferenceActivity;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.preference.PreferenceScreen;
43import android.provider.ContactsContract.CommonDataKinds;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070045import android.telecom.PhoneAccountHandle;
46import android.telecom.TelecomManager;
Andrew Lee93c345f2014-10-27 15:25:07 -070047import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.text.TextUtils;
49import android.util.Log;
50import android.view.MenuItem;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.widget.ListAdapter;
Jay Shraunerc2684732014-11-12 12:10:37 -080052import android.widget.Toast;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053
Andrew Lee312e8172014-10-23 17:01:36 -070054import com.android.ims.ImsManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import com.android.internal.telephony.CallForwardInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import com.android.internal.telephony.Phone;
57import com.android.internal.telephony.PhoneConstants;
Andrew Lee2170a972014-08-13 18:13:01 -070058import com.android.phone.common.util.SettingsUtil;
Andrew Leedb2fe562014-09-03 15:40:43 -070059import com.android.phone.settings.AccountSelectionPreference;
Andrew Lee1af6cf72014-11-04 17:35:26 -080060import com.android.phone.settings.CallForwardInfoUtil;
Andrew Lee84024342014-11-06 23:37:09 -080061import com.android.phone.settings.VoicemailDialogUtil;
Andrew 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 Lee8d66d812014-11-24 14:54:02 -080064import com.android.phone.settings.VoicemailNotificationSettingsUtil;
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 // Key identifying the default vocie mail provider
128 public static final String DEFAULT_VM_PROVIDER_KEY = "";
129
130 /**
131 * String Extra put into ACTION_ADD_VOICEMAIL call to indicate which provider should be hidden
132 * in the list of providers presented to the user. This allows a provider which is being
133 * disabled (e.g. GV user logging out) to force the user to pick some other provider.
134 */
135 public static final String IGNORE_PROVIDER_EXTRA = "com.android.phone.ProviderToIgnore";
136
137 // string constants
138 private static final String NUM_PROJECTION[] = {CommonDataKinds.Phone.NUMBER};
139
140 // String keys for preference lookup
141 // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
Andrew Lee8d66d812014-11-24 14:54:02 -0800142 // TODO: Consider moving these strings to strings.xml, so that they are not duplicated here and
143 // in the layout files. These strings need to be treated carefully; if the setting is
144 // persistent, they are used as the key to store shared preferences and the name should not be
145 // changed unless the settings are also migrated.
Andrew Lee97708a42014-09-25 12:39:07 -0700146 private static final String VOICEMAIL_SETTING_SCREEN_PREF_KEY = "button_voicemail_category_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700147 private static final String BUTTON_VOICEMAIL_KEY = "button_voicemail_key";
148 private static final String BUTTON_VOICEMAIL_PROVIDER_KEY = "button_voicemail_provider_key";
149 private static final String BUTTON_VOICEMAIL_SETTING_KEY = "button_voicemail_setting_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150 private static final String BUTTON_FDN_KEY = "button_fdn_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700151
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700152 private static final String BUTTON_DTMF_KEY = "button_dtmf_settings";
153 private static final String BUTTON_RETRY_KEY = "button_auto_retry_key";
154 private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
155 private static final String BUTTON_HAC_KEY = "button_hac_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 public static final String HAC_KEY = "HACSetting";
173 public static final String HAC_VAL_ON = "ON";
174 public static final String HAC_VAL_OFF = "OFF";
175
176 /** Handle to voicemail pref */
177 private static final int VOICEMAIL_PREF_ID = 1;
178 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
179
180 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700181 private AudioManager mAudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700182
Andrew Lee5ed870c2014-10-29 11:47:49 -0700183 private SubscriptionInfoHelper mSubscriptionInfoHelper;
184
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
186
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700187 /** Whether dialpad plays DTMF tone or not. */
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188 private CheckBoxPreference mButtonAutoRetry;
189 private CheckBoxPreference mButtonHAC;
190 private ListPreference mButtonDTMF;
191 private ListPreference mButtonTTY;
Andrew Leece8ae2a2014-09-10 10:41:48 -0700192 private Preference mPhoneAccountSettingsPreference;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700193 private ListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700194 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700195 private PreferenceScreen mVoicemailSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700196 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700197 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198
199 private class VoiceMailProvider {
Andrew Leef1776d82014-11-04 14:45:02 -0800200 public String name;
201 public Intent intent;
202
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700203 public VoiceMailProvider(String name, Intent intent) {
204 this.name = name;
205 this.intent = intent;
206 }
Andrew Leef1776d82014-11-04 14:45:02 -0800207
208 public String toString() {
209 return "[ Name: " + name + ", Intent: " + intent + " ]";
210 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700211 }
212
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700213 /**
214 * Results of reading forwarding settings
215 */
216 private CallForwardInfo[] mForwardingReadResults = null;
217
218 /**
219 * Result of forwarding number change.
220 * Keys are reasons (eg. unconditional forwarding).
221 */
222 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
223
224 /**
225 * Expected CF read result types.
226 * This set keeps track of the CF types for which we've issued change
227 * commands so we can tell when we've received all of the responses.
228 */
229 private Collection<Integer> mExpectedChangeResultReasons = null;
230
231 /**
232 * Result of vm number change
233 */
234 private AsyncResult mVoicemailChangeResult = null;
235
236 /**
237 * Previous VM provider setting so we can return to it in case of failure.
238 */
239 private String mPreviousVMProviderKey = null;
240
241 /**
242 * Id of the dialog being currently shown.
243 */
244 private int mCurrentDialogId = 0;
245
246 /**
247 * Flag indicating that we are invoking settings for the voicemail provider programmatically
248 * due to vm provider change.
249 */
250 private boolean mVMProviderSettingsForced = false;
251
252 /**
253 * Flag indicating that we are making changes to vm or fwd numbers
254 * due to vm provider change.
255 */
256 private boolean mChangingVMorFwdDueToProviderChange = false;
257
258 /**
259 * True if we are in the process of vm & fwd number change and vm has already been changed.
260 * This is used to decide what to do in case of rollback.
261 */
262 private boolean mVMChangeCompletedSuccessfully = false;
263
264 /**
265 * True if we had full or partial failure setting forwarding numbers and so need to roll them
266 * back.
267 */
268 private boolean mFwdChangesRequireRollback = false;
269
270 /**
271 * Id of error msg to display to user once we are done reverting the VM provider to the previous
272 * one.
273 */
274 private int mVMOrFwdSetError = 0;
275
276 /**
277 * Data about discovered voice mail settings providers.
278 * Is populated by querying which activities can handle ACTION_CONFIGURE_VOICEMAIL.
279 * They key in this map is package name + activity name.
280 * We always add an entry for the default provider with a key of empty
281 * string and intent value of null.
282 * @see #initVoiceMailProviders()
283 */
284 private final Map<String, VoiceMailProvider> mVMProvidersData =
285 new HashMap<String, VoiceMailProvider>();
286
287 /** string to hold old voicemail number as it is being updated. */
288 private String mOldVmNumber;
289
290 // New call forwarding settings and vm number we will be setting
291 // Need to save these since before we get to saving we need to asynchronously
292 // query the existing forwarding settings.
293 private CallForwardInfo[] mNewFwdSettings;
294 private String mNewVMNumber;
295
296 private boolean mForeground;
297
298 @Override
299 public void onPause() {
300 super.onPause();
301 mForeground = false;
302 }
303
304 /**
305 * We have to pull current settings from the network for all kinds of
306 * voicemail providers so we can tell whether we have to update them,
307 * so use this bit to keep track of whether we're reading settings for the
308 * default provider and should therefore save them out when done.
309 */
310 private boolean mReadingSettingsForDefaultProvider = false;
311
Tyler Gunnbaee2952014-09-10 16:01:02 -0700312 /**
313 * Used to indicate that the voicemail preference should be shown.
314 */
315 private boolean mShowVoicemailPreference = false;
316
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700317 /*
318 * Click Listeners, handle click based on objects attached to UI.
319 */
320
321 // Click listener for all toggle events
322 @Override
323 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
324 if (preference == mSubMenuVoicemailSettings) {
325 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700326 } else if (preference == mButtonDTMF) {
327 return true;
328 } else if (preference == mButtonTTY) {
329 return true;
330 } else if (preference == mButtonAutoRetry) {
331 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
332 android.provider.Settings.Global.CALL_AUTO_RETRY,
333 mButtonAutoRetry.isChecked() ? 1 : 0);
334 return true;
335 } else if (preference == mButtonHAC) {
336 int hac = mButtonHAC.isChecked() ? 1 : 0;
337 // Update HAC value in Settings database
338 Settings.System.putInt(mPhone.getContext().getContentResolver(),
339 Settings.System.HEARING_AID, hac);
340
341 // Update HAC Value in AudioManager
342 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
343 return true;
344 } else if (preference == mVoicemailSettings) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800345 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
346
Yorke Leea0f63bf2014-10-09 18:27:20 -0700347 final Dialog dialog = mVoicemailSettings.getDialog();
348 if (dialog != null) {
349 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
350 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800351
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700352 if (preference.getIntent() != null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800353 if (DBG) log("Invoking cfg intent " + preference.getIntent().getPackage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700354
355 // onActivityResult() will be responsible for resetting some of variables.
356 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
357 return true;
358 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800359 if (DBG) log("onPreferenceTreeClick(). No intent; use default behavior in xml.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700360
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800361 // onActivityResult() will not be called, so reset variables here.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700362 mPreviousVMProviderKey = DEFAULT_VM_PROVIDER_KEY;
363 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700364 return false;
365 }
Andrew Lee97708a42014-09-25 12:39:07 -0700366 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700367 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
368 if (dialog != null) {
369 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
370 }
Andrew Lee97708a42014-09-25 12:39:07 -0700371 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700372 }
373 return false;
374 }
375
376 /**
377 * Implemented to support onPreferenceChangeListener to look for preference
378 * changes.
379 *
380 * @param preference is the preference to be changed
381 * @param objValue should be the value of the selection, NOT its localized
382 * display value.
383 */
384 @Override
385 public boolean onPreferenceChange(Preference preference, Object objValue) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800386 if (DBG) log("onPreferenceChange: \"" + preference + "\" changed to \"" + objValue + "\"");
Andrew Lee2170a972014-08-13 18:13:01 -0700387
388 if (preference == mButtonDTMF) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700389 int index = mButtonDTMF.findIndexOfValue((String) objValue);
390 Settings.System.putInt(mPhone.getContext().getContentResolver(),
391 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
392 } else if (preference == mButtonTTY) {
393 handleTTYChange(preference, objValue);
394 } else if (preference == mVoicemailProviders) {
395 final String newProviderKey = (String) objValue;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800396
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700397 // If previous provider key and the new one is same, we don't need to handle it.
398 if (mPreviousVMProviderKey.equals(newProviderKey)) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800399 if (DBG) log("No change is made to the VM provider setting.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700400 return true;
401 }
402 updateVMPreferenceWidgets(newProviderKey);
403
Andrew Leeb490d732014-10-27 15:00:41 -0700404 final VoicemailProviderSettings newProviderSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800405 VoicemailProviderSettingsUtil.load(this, newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700406
Andrew Lee88b51e22014-10-29 15:48:51 -0700407 // If the user switches to a voice mail provider and we have numbers stored for it we
408 // will automatically change the phone's voice mail and forwarding number to the stored
409 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700410 if (newProviderSettings == null) {
411 // Force the user into a configuration of the chosen provider
412 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
413 mVMProviderSettingsForced = true;
414 simulatePreferenceClick(mVoicemailSettings);
415 } else {
416 if (DBG) log("Saved preferences found - switching to them");
417 // Set this flag so if we get a failure we revert to previous provider
418 mChangingVMorFwdDueToProviderChange = true;
419 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
420 }
Andrew Lee8d66d812014-11-24 14:54:02 -0800421 } else if (preference.getKey().equals(mVoicemailNotificationVibrate.getKey())) {
422 VoicemailNotificationSettingsUtil.setVibrationEnabled(
423 mPhone.getContext(), Boolean.TRUE.equals(objValue));
Andrew Leedf14ead2014-10-17 14:22:52 -0700424 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700425 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
426 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
427 } else {
428 AlertDialog.Builder builder = new AlertDialog.Builder(this);
429 DialogInterface.OnClickListener networkSettingsClickListener =
430 new Dialog.OnClickListener() {
431 @Override
432 public void onClick(DialogInterface dialog, int which) {
433 startActivity(new Intent(mPhone.getContext(),
434 com.android.phone.MobileNetworkSettings.class));
435 }
436 };
437 builder.setMessage(getResources().getString(
438 R.string.enable_video_calling_dialog_msg))
439 .setNeutralButton(getResources().getString(
440 R.string.enable_video_calling_dialog_settings),
441 networkSettingsClickListener)
442 .setPositiveButton(android.R.string.ok, null)
443 .show();
444 return false;
445 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700446 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800447
448 // Always let the preference setting proceed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700449 return true;
450 }
451
452 @Override
453 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800454 if (DBG) log("onDialogClosed: Button clicked is " + buttonClicked);
455
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700456 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
457 return;
458 }
459
460 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700461 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
462 mSubMenuVoicemailSettings.getPhoneNumber(),
463 VoicemailProviderSettings.NO_FORWARDING);
464 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700465 }
466 }
467
468 /**
469 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
470 * This method set the default values for the various
471 * EditPhoneNumberPreference dialogs.
472 */
473 @Override
474 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
475 if (preference == mSubMenuVoicemailSettings) {
476 // update the voicemail number field, which takes care of the
477 // mSubMenuVoicemailSettings itself, so we should return null.
478 if (DBG) log("updating default for voicemail dialog");
479 updateVoiceNumberField();
480 return null;
481 }
482
483 String vmDisplay = mPhone.getVoiceMailNumber();
484 if (TextUtils.isEmpty(vmDisplay)) {
485 // if there is no voicemail number, we just return null to
486 // indicate no contribution.
487 return null;
488 }
489
490 // Return the voicemail number prepended with "VM: "
491 if (DBG) log("updating default for call forwarding dialogs");
492 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
493 }
494
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700495 private void switchToPreviousVoicemailProvider() {
496 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700497
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800498 if (mPreviousVMProviderKey == null) {
499 return;
500 }
501
502 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
Andrew Lee84024342014-11-06 23:37:09 -0800503 showDialogIfForeground(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800504 final VoicemailProviderSettings prevSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800505 VoicemailProviderSettingsUtil.load(this, mPreviousVMProviderKey);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800506 if (prevSettings == null) {
507 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
508 + mPreviousVMProviderKey + "\" is null but should be loaded.");
509 }
510
511 if (mVMChangeCompletedSuccessfully) {
512 mNewVMNumber = prevSettings.getVoicemailNumber();
513 Log.i(LOG_TAG, "VM change is already completed successfully."
514 + "Have to revert VM back to " + mNewVMNumber + " again.");
515 mPhone.setVoiceMailNumber(
516 mPhone.getVoiceMailAlphaTag().toString(),
517 mNewVMNumber,
518 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
519 }
520
521 if (mFwdChangesRequireRollback) {
522 Log.i(LOG_TAG, "Requested to rollback forwarding changes.");
523
524 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
525 if (prevFwdSettings != null) {
526 Map<Integer, AsyncResult> results = mForwardingChangeResults;
527 resetForwardingChangeState();
528 for (int i = 0; i < prevFwdSettings.length; i++) {
529 CallForwardInfo fi = prevFwdSettings[i];
530 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
531 // Only revert the settings for which the update succeeded.
532 AsyncResult result = results.get(fi.reason);
533 if (result != null && result.exception == null) {
534 mExpectedChangeResultReasons.add(fi.reason);
535 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
536 mRevertOptionComplete.obtainMessage(
537 EVENT_FORWARDING_CHANGED, i, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700538 }
539 }
540 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700541 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800542 } else {
543 if (DBG) log("No need to revert");
544 onRevertDone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700545 }
546 }
547
548 private void onRevertDone() {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800549 if (DBG) log("onRevertDone: Changing provider key back to " + mPreviousVMProviderKey);
550
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700551 updateVMPreferenceWidgets(mPreviousVMProviderKey);
552 updateVoiceNumberField();
553 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800554 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700555 mVMOrFwdSetError = 0;
556 }
557 }
558
559 @Override
560 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
561 if (DBG) {
562 log("onActivityResult: requestCode: " + requestCode
563 + ", resultCode: " + resultCode
564 + ", data: " + data);
565 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800566
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700567 // there are cases where the contact picker may end up sending us more than one
568 // request. We want to ignore the request if we're not in the correct state.
569 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
570 boolean failure = false;
571
572 // No matter how the processing of result goes lets clear the flag
573 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
574 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
575 mVMProviderSettingsForced = false;
576
577 String vmNum = null;
578 if (resultCode != RESULT_OK) {
579 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
580 failure = true;
581 } else {
582 if (data == null) {
583 if (DBG) log("onActivityResult: vm provider cfg result has no data");
584 failure = true;
585 } else {
586 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
587 if (DBG) log("Provider requested signout");
588 if (isVMProviderSettingsForced) {
589 if (DBG) log("Going back to previous provider on signout");
590 switchToPreviousVoicemailProvider();
591 } else {
592 final String victim = getCurrentVoicemailProviderKey();
593 if (DBG) log("Relaunching activity and ignoring " + victim);
594 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
595 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
596 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
597 this.startActivity(i);
598 }
599 return;
600 }
601 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
602 if (vmNum == null || vmNum.length() == 0) {
603 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
604 failure = true;
605 }
606 }
607 }
608 if (failure) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800609 if (DBG) log("Failure in return from voicemail provider.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700610 if (isVMProviderSettingsForced) {
611 switchToPreviousVoicemailProvider();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700612 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800613
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700614 return;
615 }
616 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
617 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
618
Santos Cordonda120f42014-08-06 04:44:34 -0700619 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700620 // send it to the provider when it's config is invoked so it can use this as default
621 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
622
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800623 if (DBG) log("onActivityResult: cfg result has forwarding number " + fwdNum);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700624 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700625 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700626 return;
627 }
628
629 if (requestCode == VOICEMAIL_PREF_ID) {
630 if (resultCode != RESULT_OK) {
631 if (DBG) log("onActivityResult: contact picker result not OK.");
632 return;
633 }
634
635 Cursor cursor = null;
636 try {
637 cursor = getContentResolver().query(data.getData(),
638 NUM_PROJECTION, null, null, null);
639 if ((cursor == null) || (!cursor.moveToFirst())) {
640 if (DBG) log("onActivityResult: bad contact data, no results found.");
641 return;
642 }
643 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
644 return;
645 } finally {
646 if (cursor != null) {
647 cursor.close();
648 }
649 }
650 }
651
652 super.onActivityResult(requestCode, resultCode, data);
653 }
654
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700655 /**
656 * Wrapper around showDialog() that will silently do nothing if we're
657 * not in the foreground.
658 *
659 * This is useful here because most of the dialogs we display from
660 * this class are triggered by asynchronous events (like
661 * success/failure messages from the telephony layer) and it's
662 * possible for those events to come in even after the user has gone
663 * to a different screen.
664 */
665 // TODO: this is too brittle: it's still easy to accidentally add new
666 // code here that calls showDialog() directly (which will result in a
667 // WindowManager$BadTokenException if called after the activity has
668 // been stopped.)
669 //
670 // It would be cleaner to do the "if (mForeground)" check in one
671 // central place, maybe by using a single Handler for all asynchronous
672 // events (and have *that* discard events if we're not in the
673 // foreground.)
674 //
675 // Unfortunately it's not that simple, since we sometimes need to do
676 // actual work to handle these events whether or not we're in the
677 // foreground (see the Handler code in mSetOptionComplete for
678 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800679 //
680 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
681 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700682 private void showDialogIfForeground(int id) {
683 if (mForeground) {
684 showDialog(id);
685 }
686 }
687
688 private void dismissDialogSafely(int id) {
689 try {
690 dismissDialog(id);
691 } catch (IllegalArgumentException e) {
692 // This is expected in the case where we were in the background
693 // at the time we would normally have shown the dialog, so we didn't
694 // show it.
695 }
696 }
697
Andrew Leeb490d732014-10-27 15:00:41 -0700698 private void saveVoiceMailAndForwardingNumber(
699 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700700 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700701 mNewVMNumber = newSettings.getVoicemailNumber();
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800702 mNewVMNumber = (mNewVMNumber == null) ? "" : mNewVMNumber;
Andrew Leeb490d732014-10-27 15:00:41 -0700703 mNewFwdSettings = newSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700704
705 // No fwd settings on CDMA
706 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
707 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700708 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700709 }
710
Andrew Leee3c15212014-10-28 13:12:55 -0700711 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700712 if (mNewVMNumber.equals(mOldVmNumber)
713 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Lee84024342014-11-06 23:37:09 -0800714 showDialogIfForeground(VoicemailDialogUtil.VM_NOCHANGE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700715 return;
716 }
717
Andrew Lee6950c6c2014-11-26 12:05:42 -0800718 VoicemailProviderSettingsUtil.save(this, key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700719 mVMChangeCompletedSuccessfully = false;
720 mFwdChangesRequireRollback = false;
721 mVMOrFwdSetError = 0;
722 if (!key.equals(mPreviousVMProviderKey)) {
723 mReadingSettingsForDefaultProvider =
724 mPreviousVMProviderKey.equals(DEFAULT_VM_PROVIDER_KEY);
725 if (DBG) log("Reading current forwarding settings");
Andrew Leeb490d732014-10-27 15:00:41 -0700726 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
727 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
728 for (int i = 0; i < mForwardingReadResults.length; i++) {
729 mPhone.getCallForwardingOption(
730 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700731 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
732 }
Andrew Lee84024342014-11-06 23:37:09 -0800733 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700734 } else {
735 saveVoiceMailAndForwardingNumberStage2();
736 }
737 }
738
739 private final Handler mGetOptionComplete = new Handler() {
740 @Override
741 public void handleMessage(Message msg) {
742 AsyncResult result = (AsyncResult) msg.obj;
743 switch (msg.what) {
744 case EVENT_FORWARDING_GET_COMPLETED:
745 handleForwardingSettingsReadResult(result, msg.arg1);
746 break;
747 }
748 }
749 };
750
751 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
752 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800753
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700754 Throwable error = null;
755 if (ar.exception != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700756 error = ar.exception;
Andrew Lee1af6cf72014-11-04 17:35:26 -0800757 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700758 }
759 if (ar.userObj instanceof Throwable) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800760 error = (Throwable) ar.userObj;
761 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700762 }
763
764 // We may have already gotten an error and decided to ignore the other results.
765 if (mForwardingReadResults == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800766 if (DBG) Log.d(LOG_TAG, "Ignoring fwd reading result: " + idx);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700767 return;
768 }
769
770 // In case of error ignore other results, show an error dialog
771 if (error != null) {
772 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
773 mForwardingReadResults = null;
Andrew Lee84024342014-11-06 23:37:09 -0800774 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
775 showDialogIfForeground(VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700776 return;
777 }
778
Andrew Lee1af6cf72014-11-04 17:35:26 -0800779 // Get the forwarding info.
780 mForwardingReadResults[idx] = CallForwardInfoUtil.getCallForwardInfo(
781 (CallForwardInfo[]) ar.result,
782 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx]);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700783
784 // Check if we got all the results already
785 boolean done = true;
786 for (int i = 0; i < mForwardingReadResults.length; i++) {
787 if (mForwardingReadResults[i] == null) {
788 done = false;
789 break;
790 }
791 }
Andrew Lee1af6cf72014-11-04 17:35:26 -0800792
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700793 if (done) {
794 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
Andrew Lee84024342014-11-06 23:37:09 -0800795 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800796
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700797 if (mReadingSettingsForDefaultProvider) {
Andrew Lee6950c6c2014-11-26 12:05:42 -0800798 VoicemailProviderSettingsUtil.save(this, DEFAULT_VM_PROVIDER_KEY,
Andrew Lee88b51e22014-10-29 15:48:51 -0700799 new VoicemailProviderSettings(this.mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700800 mReadingSettingsForDefaultProvider = false;
801 }
802 saveVoiceMailAndForwardingNumberStage2();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700803 }
804 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800805
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700806 private void resetForwardingChangeState() {
807 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
808 mExpectedChangeResultReasons = new HashSet<Integer>();
809 }
810
811 // Called after we are done saving the previous forwarding settings if
812 // we needed.
813 private void saveVoiceMailAndForwardingNumberStage2() {
814 mForwardingChangeResults = null;
815 mVoicemailChangeResult = null;
Andrew Leeb490d732014-10-27 15:00:41 -0700816 if (mNewFwdSettings != VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700817 resetForwardingChangeState();
818 for (int i = 0; i < mNewFwdSettings.length; i++) {
819 CallForwardInfo fi = mNewFwdSettings[i];
Andrew Lee1af6cf72014-11-04 17:35:26 -0800820 CallForwardInfo fiForReason =
821 CallForwardInfoUtil.infoForReason(mForwardingReadResults, fi.reason);
822 final boolean doUpdate = CallForwardInfoUtil.isUpdateRequired(fiForReason, fi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700823
824 if (doUpdate) {
825 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
826 mExpectedChangeResultReasons.add(i);
827
Andrew Lee1af6cf72014-11-04 17:35:26 -0800828 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700829 mSetOptionComplete.obtainMessage(
830 EVENT_FORWARDING_CHANGED, fi.reason, 0));
831 }
832 }
Andrew Lee84024342014-11-06 23:37:09 -0800833 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700834 } else {
835 if (DBG) log("Not touching fwd #");
836 setVMNumberWithCarrier();
837 }
838 }
839
840 private void setVMNumberWithCarrier() {
841 if (DBG) log("save voicemail #: " + mNewVMNumber);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800842
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700843 mPhone.setVoiceMailNumber(
844 mPhone.getVoiceMailAlphaTag().toString(),
845 mNewVMNumber,
846 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
847 }
848
849 /**
850 * Callback to handle option update completions
851 */
852 private final Handler mSetOptionComplete = new Handler() {
853 @Override
854 public void handleMessage(Message msg) {
855 AsyncResult result = (AsyncResult) msg.obj;
856 boolean done = false;
857 switch (msg.what) {
858 case EVENT_VOICEMAIL_CHANGED:
859 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700860 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700861 done = true;
862 break;
863 case EVENT_FORWARDING_CHANGED:
864 mForwardingChangeResults.put(msg.arg1, result);
865 if (result.exception != null) {
866 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
867 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700868 }
Andrew Leee438b312014-10-29 16:59:15 -0700869 if (isForwardingCompleted()) {
870 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700871 if (DBG) log("Overall fwd changes completed ok, starting vm change");
872 setVMNumberWithCarrier();
873 } else {
874 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
875 "Check if we need to try rollback for some settings.");
876 mFwdChangesRequireRollback = false;
877 Iterator<Map.Entry<Integer,AsyncResult>> it =
878 mForwardingChangeResults.entrySet().iterator();
879 while (it.hasNext()) {
880 Map.Entry<Integer,AsyncResult> entry = it.next();
881 if (entry.getValue().exception == null) {
882 // If at least one succeeded we have to revert
883 Log.i(LOG_TAG, "Rollback will be required");
884 mFwdChangesRequireRollback = true;
885 break;
886 }
887 }
888 if (!mFwdChangesRequireRollback) {
889 Log.i(LOG_TAG, "No rollback needed.");
890 }
891 done = true;
892 }
893 }
894 break;
895 default:
896 // TODO: should never reach this, may want to throw exception
897 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800898
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700899 if (done) {
900 if (DBG) log("All VM provider related changes done");
901 if (mForwardingChangeResults != null) {
Andrew Lee84024342014-11-06 23:37:09 -0800902 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700903 }
Andrew Leee438b312014-10-29 16:59:15 -0700904 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700905 }
906 }
907 };
908
909 /**
910 * Callback to handle option revert completions
911 */
912 private final Handler mRevertOptionComplete = new Handler() {
913 @Override
914 public void handleMessage(Message msg) {
915 AsyncResult result = (AsyncResult) msg.obj;
916 switch (msg.what) {
917 case EVENT_VOICEMAIL_CHANGED:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700918 if (DBG) log("VM revert complete msg");
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800919 mVoicemailChangeResult = result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700920 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800921
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700922 case EVENT_FORWARDING_CHANGED:
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800923 if (DBG) log("FWD revert complete msg ");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700924 mForwardingChangeResults.put(msg.arg1, result);
925 if (result.exception != null) {
926 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
927 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700928 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700929 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800930
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700931 default:
932 // TODO: should never reach this, may want to throw exception
933 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800934
935 final boolean done = (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null)
936 && (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700937 if (done) {
938 if (DBG) log("All VM reverts done");
Andrew Lee84024342014-11-06 23:37:09 -0800939 dismissDialogSafely(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700940 onRevertDone();
941 }
942 }
943 };
944
945 /**
Andrew Leee438b312014-10-29 16:59:15 -0700946 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700947 */
Andrew Leee438b312014-10-29 16:59:15 -0700948 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700949 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -0700950 return true;
951 }
952
953 for (Integer reason : mExpectedChangeResultReasons) {
954 if (mForwardingChangeResults.get(reason) == null) {
955 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700956 }
957 }
Andrew Leee438b312014-10-29 16:59:15 -0700958
959 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700960 }
Andrew Leee438b312014-10-29 16:59:15 -0700961
962 private boolean isFwdChangeSuccess() {
963 if (mForwardingChangeResults == null) {
964 return true;
965 }
966
967 for (AsyncResult result : mForwardingChangeResults.values()) {
968 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700969 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700970 String msg = exception.getMessage();
971 msg = (msg != null) ? msg : "";
972 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
973 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700974 }
975 }
Andrew Leee438b312014-10-29 16:59:15 -0700976 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700977 }
978
Andrew Leee438b312014-10-29 16:59:15 -0700979 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700980 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700981 String msg = mVoicemailChangeResult.exception.getMessage();
982 msg = (msg != null) ? msg : "";
983 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
984 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700985 }
Andrew Leee438b312014-10-29 16:59:15 -0700986 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700987 }
988
Andrew Leee438b312014-10-29 16:59:15 -0700989 private void handleSetVmOrFwdMessage() {
990 if (DBG) log("handleSetVMMessage: set VM request complete");
991
992 if (!isFwdChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -0800993 handleVmOrFwdSetError(VoicemailDialogUtil.FWD_SET_RESPONSE_ERROR_DIALOG);
Andrew Leee438b312014-10-29 16:59:15 -0700994 } else if (!isVmChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -0800995 handleVmOrFwdSetError(VoicemailDialogUtil.VM_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700996 } else {
Andrew Lee84024342014-11-06 23:37:09 -0800997 handleVmAndFwdSetSuccess(VoicemailDialogUtil.VM_CONFIRM_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700998 }
999 }
1000
1001 /**
1002 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
1003 * changes to those settings and show "failure" dialog.
1004 *
Andrew Leeab082272014-11-04 15:50:42 -08001005 * @param dialogId ID of the dialog to show for the specific error case. Either
Andrew Lee84024342014-11-06 23:37:09 -08001006 * {@link #FWD_SET_RESPONSE_ERROR_DIALOG} or {@link #VM_RESPONSE_ERROR_DIALOG}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001007 */
Andrew Leeab082272014-11-04 15:50:42 -08001008 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001009 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -08001010 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001011 mChangingVMorFwdDueToProviderChange = false;
1012 switchToPreviousVoicemailProvider();
1013 return;
1014 }
1015 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001016 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001017 updateVoiceNumberField();
1018 }
1019
1020 /**
1021 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1022 * This updates a bunch of variables and show "success" dialog.
1023 */
Andrew Leeab082272014-11-04 15:50:42 -08001024 private void handleVmAndFwdSetSuccess(int dialogId) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001025 if (DBG) log("handleVmAndFwdSetSuccess: key is " + getCurrentVoicemailProviderKey());
1026
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001027 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1028 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001029 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001030 updateVoiceNumberField();
1031 }
1032
1033 /**
1034 * Update the voicemail number from what we've recorded on the sim.
1035 */
1036 private void updateVoiceNumberField() {
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001037 if (DBG) log("updateVoiceNumberField()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001038
1039 mOldVmNumber = mPhone.getVoiceMailNumber();
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001040 if (TextUtils.isEmpty(mOldVmNumber)) {
1041 mSubMenuVoicemailSettings.setPhoneNumber("");
1042 mSubMenuVoicemailSettings.setSummary(getString(R.string.voicemail_number_not_set));
1043 } else {
1044 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1045 mSubMenuVoicemailSettings.setSummary(mOldVmNumber);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001046 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001047 }
1048
1049 /*
1050 * Helper Methods for Activity class.
1051 * The initial query commands are split into two pieces now
1052 * for individual expansion. This combined with the ability
1053 * to cancel queries allows for a much better user experience,
1054 * and also ensures that the user only waits to update the
1055 * data that is relevant.
1056 */
1057
1058 @Override
1059 protected void onPrepareDialog(int id, Dialog dialog) {
1060 super.onPrepareDialog(id, dialog);
1061 mCurrentDialogId = id;
1062 }
1063
1064 // dialog creation method, called by showDialog()
1065 @Override
Andrew Lee84024342014-11-06 23:37:09 -08001066 protected Dialog onCreateDialog(int dialogId) {
1067 return VoicemailDialogUtil.getDialog(this, dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001068 }
1069
1070 // This is a method implemented for DialogInterface.OnClickListener.
1071 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1072 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1073 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1074 public void onClick(DialogInterface dialog, int which) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001075 if (DBG) log("onClick: button clicked is " + which);
1076
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001077 dialog.dismiss();
1078 switch (which){
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001079 case DialogInterface.BUTTON_NEGATIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001080 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001081 // We failed to get current forwarding settings and the user
1082 // does not wish to continue.
1083 switchToPreviousVoicemailProvider();
1084 }
1085 break;
1086 case DialogInterface.BUTTON_POSITIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001087 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001088 // We failed to get current forwarding settings but the user
1089 // wishes to continue changing settings to the new vm provider
1090 saveVoiceMailAndForwardingNumberStage2();
1091 } else {
1092 finish();
1093 }
1094 return;
1095 default:
1096 // just let the dialog close and go back to the input
1097 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001098
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001099 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1100 // with settings UI. If we were called to explicitly configure voice mail then
1101 // we finish the settings activity here to come back to whatever the user was doing.
1102 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1103 finish();
1104 }
1105 }
1106
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001107 /*
1108 * Activity class methods
1109 */
1110
1111 @Override
1112 protected void onCreate(Bundle icicle) {
1113 super.onCreate(icicle);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001114 if (DBG) log("onCreate: Intent is " + getIntent());
1115
Jay Shraunerc2684732014-11-12 12:10:37 -08001116 // Make sure we are running as the primary user.
1117 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
1118 Toast.makeText(this, R.string.call_settings_primary_user_only,
1119 Toast.LENGTH_SHORT).show();
1120 finish();
1121 return;
1122 }
1123
Tyler Gunnbaee2952014-09-10 16:01:02 -07001124 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
1125
Tyler Gunnbaee2952014-09-10 16:01:02 -07001126 // Show the voicemail preference in onResume if the calling intent specifies the
1127 // ACTION_ADD_VOICEMAIL action.
1128 mShowVoicemailPreference = (icicle == null) &&
Jay Shraunerbe2fb262014-11-11 15:19:58 -08001129 TextUtils.equals(getIntent().getAction(), ACTION_ADD_VOICEMAIL);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001130
1131 mSubscriptionInfoHelper = new SubscriptionInfoHelper(getIntent());
1132 mSubscriptionInfoHelper.setActionBarTitle(
1133 getActionBar(), getResources(), R.string.call_settings_with_label);
Andrew Lee1479dd12014-11-06 23:06:32 -08001134 mPhone = mSubscriptionInfoHelper.getPhone();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001135 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001136
1137 private void initPhoneAccountPreferences() {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001138 mPhoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001139
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001140 TelecomManager telecomManager = TelecomManager.from(this);
Andrew Lee93c345f2014-10-27 15:25:07 -07001141 TelephonyManager telephonyManager =
1142 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001143
Andrew Lee93c345f2014-10-27 15:25:07 -07001144 if ((telecomManager.getSimCallManagers().isEmpty() && !SipUtil.isVoipSupported(this))
1145 || telephonyManager.getPhoneCount() > 1) {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001146 getPreferenceScreen().removePreference(mPhoneAccountSettingsPreference);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001147 }
1148 }
1149
Tyler Gunnbaee2952014-09-10 16:01:02 -07001150 @Override
1151 protected void onResume() {
1152 super.onResume();
1153 mForeground = true;
1154
1155 PreferenceScreen preferenceScreen = getPreferenceScreen();
1156 if (preferenceScreen != null) {
1157 preferenceScreen.removeAll();
1158 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001159
1160 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001161
Andrew Leedb2fe562014-09-03 15:40:43 -07001162 initPhoneAccountPreferences();
1163
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001164 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001165 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1166 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1167 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1168 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001169
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001170 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1171 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1172 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
1173 mButtonTTY = (ListPreference) findPreference(BUTTON_TTY_KEY);
Andrew Lee312e8172014-10-23 17:01:36 -07001174 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001175
Andrew Lee8d66d812014-11-24 14:54:02 -08001176 mVoicemailProviders = (ListPreference) findPreference(BUTTON_VOICEMAIL_PROVIDER_KEY);
Andrew Lee2c027892014-10-29 11:29:54 -07001177 mVoicemailProviders.setOnPreferenceChangeListener(this);
Andrew Lee8d66d812014-11-24 14:54:02 -08001178
Andrew Lee2c027892014-10-29 11:29:54 -07001179 mVoicemailSettingsScreen =
1180 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
1181 mVoicemailSettings = (PreferenceScreen)findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
Andrew Lee8d66d812014-11-24 14:54:02 -08001182
1183 mVoicemailNotificationVibrate = (CheckBoxPreference) findPreference(
1184 getResources().getString(R.string.voicemail_notification_vibrate_key));
1185 mVoicemailNotificationVibrate.setOnPreferenceChangeListener(this);
1186
Andrew Lee2c027892014-10-29 11:29:54 -07001187 initVoiceMailProviders();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001188
Andrew Lee64a7d792014-10-15 17:38:38 -07001189 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1190 mButtonDTMF.setOnPreferenceChangeListener(this);
1191 int dtmf = Settings.System.getInt(getContentResolver(),
1192 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1193 mButtonDTMF.setValueIndex(dtmf);
1194 } else {
1195 prefSet.removePreference(mButtonDTMF);
1196 mButtonDTMF = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001197 }
1198
Andrew Lee64a7d792014-10-15 17:38:38 -07001199 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1200 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1201 int autoretry = Settings.Global.getInt(
1202 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1203 mButtonAutoRetry.setChecked(autoretry != 0);
1204 } else {
1205 prefSet.removePreference(mButtonAutoRetry);
1206 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001207 }
1208
Andrew Lee64a7d792014-10-15 17:38:38 -07001209 if (getResources().getBoolean(R.bool.hac_enabled)) {
1210 mButtonHAC.setOnPreferenceChangeListener(this);
1211 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1212 mButtonHAC.setChecked(hac != 0);
1213 } else {
1214 prefSet.removePreference(mButtonHAC);
1215 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001216 }
1217
Andrew Lee64a7d792014-10-15 17:38:38 -07001218 TelecomManager telecomManager = TelecomManager.from(this);
1219 if (telecomManager != null && telecomManager.isTtySupported()) {
1220 mButtonTTY.setOnPreferenceChangeListener(this);
1221 int settingsTtyMode = Settings.Secure.getInt(getContentResolver(),
1222 Settings.Secure.PREFERRED_TTY_MODE,
1223 TelecomManager.TTY_MODE_OFF);
1224 mButtonTTY.setValue(Integer.toString(settingsTtyMode));
1225 updatePreferredTtyModeSummary(settingsTtyMode);
1226 } else {
1227 prefSet.removePreference(mButtonTTY);
1228 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001229 }
1230
1231 if (!getResources().getBoolean(R.bool.world_phone)) {
Andrew Lee2b36ba22014-11-05 17:08:49 -08001232 Preference cdmaOptions = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
1233 prefSet.removePreference(cdmaOptions);
1234
1235 // TODO: Support MSIM for this preference option.
1236 Preference gsmOptions = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
1237 prefSet.removePreference(gsmOptions);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001238
1239 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001240 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Junda Liua09d6962014-11-12 13:39:02 -08001241 boolean shouldHideCarrierSettings = Settings.Global.getInt(
1242 getContentResolver(), Settings.Global.HIDE_CARRIER_NETWORK_SETTINGS, 0) == 1;
1243 if (shouldHideCarrierSettings) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001244 prefSet.removePreference(fdnButton);
Junda Liua09d6962014-11-12 13:39:02 -08001245 if (mButtonDTMF != null) {
1246 prefSet.removePreference(mButtonDTMF);
Etan Cohen0ca1c802014-07-07 15:35:48 -07001247 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001248 } else {
Junda Liua09d6962014-11-12 13:39:02 -08001249 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1250 prefSet.removePreference(fdnButton);
1251
1252 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1253 addPreferencesFromResource(R.xml.cdma_call_privacy);
1254 }
1255 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
1256 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(this, FdnSetting.class));
1257
1258 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
1259 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1260
1261 Preference callForwardingPref = prefSet.findPreference(CALL_FORWARDING_KEY);
1262 callForwardingPref.setIntent(mSubscriptionInfoHelper.getIntent(
1263 this, GsmUmtsCallForwardOptions.class));
1264
1265 Preference additionalGsmSettingsPref =
1266 prefSet.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
1267 additionalGsmSettingsPref.setIntent(mSubscriptionInfoHelper.getIntent(
1268 this, GsmUmtsAdditionalCallOptions.class));
1269 }
1270 } else {
1271 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1272 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001273 }
1274 }
1275
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001276 // check the intent that started this activity and pop up the voicemail
1277 // dialog if we've been asked to.
1278 // If we have at least one non default VM provider registered then bring up
1279 // the selection for the VM provider, otherwise bring up a VM number dialog.
1280 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001281 if (mShowVoicemailPreference) {
Tyler Gunnbaee2952014-09-10 16:01:02 -07001282 if (DBG) {
1283 log("ACTION_ADD_VOICEMAIL Intent is thrown. current VM data size: "
1284 + mVMProvidersData.size());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001285 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001286 if (mVMProvidersData.size() > 1) {
1287 simulatePreferenceClick(mVoicemailProviders);
1288 } else {
1289 onPreferenceChange(mVoicemailProviders, DEFAULT_VM_PROVIDER_KEY);
1290 mVoicemailProviders.setValue(DEFAULT_VM_PROVIDER_KEY);
1291 }
1292 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001293 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001294
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001295 updateVoiceNumberField();
1296 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001297
Andrew Lee8d66d812014-11-24 14:54:02 -08001298 mVoicemailNotificationVibrate.setChecked(
1299 VoicemailNotificationSettingsUtil.isVibrationEnabled(mPhone.getContext()));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001300
Andrew Lee312e8172014-10-23 17:01:36 -07001301 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1302 boolean currentValue =
1303 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1304 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1305 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001306 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1307 } else {
1308 prefSet.removePreference(mEnableVideoCalling);
1309 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001310 }
1311
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001312 private void handleTTYChange(Preference preference, Object objValue) {
1313 int buttonTtyMode;
1314 buttonTtyMode = Integer.valueOf((String) objValue).intValue();
1315 int settingsTtyMode = android.provider.Settings.Secure.getInt(
1316 getContentResolver(),
Sailesh Nepalbf900542014-07-15 16:18:32 -07001317 android.provider.Settings.Secure.PREFERRED_TTY_MODE,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001318 TelecomManager.TTY_MODE_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001319 if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
1320 Integer.toString(buttonTtyMode));
1321
1322 if (buttonTtyMode != settingsTtyMode) {
1323 switch(buttonTtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001324 case TelecomManager.TTY_MODE_OFF:
1325 case TelecomManager.TTY_MODE_FULL:
1326 case TelecomManager.TTY_MODE_HCO:
1327 case TelecomManager.TTY_MODE_VCO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001328 android.provider.Settings.Secure.putInt(getContentResolver(),
1329 android.provider.Settings.Secure.PREFERRED_TTY_MODE, buttonTtyMode);
1330 break;
1331 default:
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001332 buttonTtyMode = TelecomManager.TTY_MODE_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001333 }
1334
1335 mButtonTTY.setValue(Integer.toString(buttonTtyMode));
1336 updatePreferredTtyModeSummary(buttonTtyMode);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001337 Intent ttyModeChanged = new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
1338 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001339 sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
1340 }
1341 }
1342
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001343 private void updatePreferredTtyModeSummary(int TtyMode) {
1344 String [] txts = getResources().getStringArray(R.array.tty_mode_entries);
1345 switch(TtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001346 case TelecomManager.TTY_MODE_OFF:
1347 case TelecomManager.TTY_MODE_HCO:
1348 case TelecomManager.TTY_MODE_VCO:
1349 case TelecomManager.TTY_MODE_FULL:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001350 mButtonTTY.setSummary(txts[TtyMode]);
1351 break;
1352 default:
1353 mButtonTTY.setEnabled(false);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001354 mButtonTTY.setSummary(txts[TelecomManager.TTY_MODE_OFF]);
Sailesh Nepalbf900542014-07-15 16:18:32 -07001355 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001356 }
1357 }
1358
1359 private static void log(String msg) {
1360 Log.d(LOG_TAG, msg);
1361 }
1362
1363 /**
1364 * Updates the look of the VM preference widgets based on current VM provider settings.
1365 * Note that the provider name is loaded form the found activity via loadLabel in
1366 * {@link #initVoiceMailProviders()} in order for it to be localizable.
1367 */
1368 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1369 final String key = currentProviderSetting;
1370 final VoiceMailProvider provider = mVMProvidersData.get(key);
1371
1372 /* This is the case when we are coming up on a freshly wiped phone and there is no
1373 persisted value for the list preference mVoicemailProviders.
1374 In this case we want to show the UI asking the user to select a voicemail provider as
1375 opposed to silently falling back to default one. */
1376 if (provider == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001377 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> null.");
1378
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001379 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1380 mVoicemailSettings.setEnabled(false);
1381 mVoicemailSettings.setIntent(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001382 mVoicemailNotificationVibrate.setEnabled(false);
1383 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001384 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> " + provider.toString());
1385
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001386 final String providerName = provider.name;
1387 mVoicemailProviders.setSummary(providerName);
1388 mVoicemailSettings.setEnabled(true);
1389 mVoicemailSettings.setIntent(provider.intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001390 mVoicemailNotificationVibrate.setEnabled(true);
1391 }
1392 }
1393
1394 /**
1395 * Enumerates existing VM providers and puts their data into the list and populates
1396 * the preference list objects with their names.
1397 * In case we are called with ACTION_ADD_VOICEMAIL intent the intent may have
1398 * an extra string called IGNORE_PROVIDER_EXTRA with "package.activityName" of the provider
1399 * which should be hidden when we bring up the list of possible VM providers to choose.
1400 */
1401 private void initVoiceMailProviders() {
1402 if (DBG) log("initVoiceMailProviders()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001403
1404 String providerToIgnore = null;
Andrew Leef1776d82014-11-04 14:45:02 -08001405 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)
1406 && getIntent().hasExtra(IGNORE_PROVIDER_EXTRA)) {
1407 providerToIgnore = getIntent().getStringExtra(IGNORE_PROVIDER_EXTRA);
1408 // Remove this provider from the list.
1409 if (!TextUtils.isEmpty(providerToIgnore)) {
1410 if (DBG) log("Found ACTION_ADD_VOICEMAIL. providerToIgnore= " + providerToIgnore);
Andrew Lee6950c6c2014-11-26 12:05:42 -08001411 VoicemailProviderSettingsUtil.delete(this, providerToIgnore);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001412 }
1413 }
1414
1415 mVMProvidersData.clear();
1416
Andrew Leef1776d82014-11-04 14:45:02 -08001417 List<String> entries = new ArrayList<String>();
1418 List<String> values = new ArrayList<String>();
1419
1420 // Add default voicemail provider.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001421 final String myCarrier = getString(R.string.voicemail_default);
1422 mVMProvidersData.put(DEFAULT_VM_PROVIDER_KEY, new VoiceMailProvider(myCarrier, null));
Andrew Leef1776d82014-11-04 14:45:02 -08001423 entries.add(myCarrier);
1424 values.add(DEFAULT_VM_PROVIDER_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001425
Andrew Leef1776d82014-11-04 14:45:02 -08001426 // Add other voicemail providers.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001427 PackageManager pm = getPackageManager();
Andrew Leef1776d82014-11-04 14:45:02 -08001428 Intent intent = new Intent(ACTION_CONFIGURE_VOICEMAIL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001429 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001430 for (int i = 0; i < resolveInfos.size(); i++) {
1431 final ResolveInfo ri= resolveInfos.get(i);
1432 final ActivityInfo currentActivityInfo = ri.activityInfo;
Andrew Lee6214e2b2014-11-04 13:57:38 -08001433 final String key = currentActivityInfo.name;
Andrew Leef1776d82014-11-04 14:45:02 -08001434
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001435 if (key.equals(providerToIgnore)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001436 continue;
1437 }
Andrew Leef1776d82014-11-04 14:45:02 -08001438
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001439 if (DBG) log("Loading key: " + key);
1440 final String nameForDisplay = ri.loadLabel(pm).toString();
1441 Intent providerIntent = new Intent();
1442 providerIntent.setAction(ACTION_CONFIGURE_VOICEMAIL);
Andrew Leef1776d82014-11-04 14:45:02 -08001443 providerIntent.setClassName(currentActivityInfo.packageName, currentActivityInfo.name);
1444 VoiceMailProvider vmProvider = new VoiceMailProvider(nameForDisplay, providerIntent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001445
Andrew Leef1776d82014-11-04 14:45:02 -08001446 if (DBG) log("Store VoiceMailProvider. Key: " + key + " -> " + vmProvider.toString());
1447 mVMProvidersData.put(key, vmProvider);
1448 entries.add(vmProvider.name);
1449 values.add(key);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001450 }
1451
Andrew Leef1776d82014-11-04 14:45:02 -08001452 mVoicemailProviders.setEntries(entries.toArray(new String[0]));
1453 mVoicemailProviders.setEntryValues(values.toArray(new String[0]));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001454
Andrew Leef1776d82014-11-04 14:45:02 -08001455 // Remember the current Voicemail Provider key as a "previous" key. This will be used when
1456 // we fail to update Voicemail Provider, which requires rollback. We will update this when
1457 // the VM Provider setting is successfully updated.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001458 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1459 if (DBG) log("Set up the first mPreviousVMProviderKey: " + mPreviousVMProviderKey);
1460
1461 // Finally update the preference texts.
1462 updateVMPreferenceWidgets(mPreviousVMProviderKey);
1463 }
1464
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001465 /**
1466 * Simulates user clicking on a passed preference.
1467 * Usually needed when the preference is a dialog preference and we want to invoke
1468 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001469 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001470 */
1471 private void simulatePreferenceClick(Preference preference) {
1472 // Go through settings until we find our setting
1473 // and then simulate a click on it to bring up the dialog
1474 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1475 for (int idx = 0; idx < adapter.getCount(); idx++) {
1476 if (adapter.getItem(idx) == preference) {
1477 getPreferenceScreen().onItemClick(this.getListView(),
1478 null, idx, adapter.getItemId(idx));
1479 break;
1480 }
1481 }
1482 }
1483
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001484 private String getCurrentVoicemailProviderKey() {
1485 final String key = mVoicemailProviders.getValue();
1486 return (key != null) ? key : DEFAULT_VM_PROVIDER_KEY;
1487 }
1488
1489 @Override
1490 public boolean onOptionsItemSelected(MenuItem item) {
1491 final int itemId = item.getItemId();
1492 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001493 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001494 return true;
1495 }
1496 return super.onOptionsItemSelected(item);
1497 }
Andrew Lee8d66d812014-11-24 14:54:02 -08001498
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001499 /**
1500 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1501 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1502 */
1503 public static void goUpToTopLevelSetting(Activity activity) {
1504 Intent intent = new Intent(activity, CallFeaturesSetting.class);
1505 intent.setAction(Intent.ACTION_MAIN);
1506 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1507 activity.startActivity(intent);
1508 activity.finish();
1509 }
1510}