blob: 2755bd787c6e5f7fd421d674ea58b0ed2a4ede9f [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
19import android.app.ActionBar;
20import android.app.Activity;
Evan Charlton1c696832014-04-15 14:24:23 -070021import android.app.ActivityOptions;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.AlertDialog;
23import android.app.Dialog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.ContentResolver;
25import android.content.Context;
26import android.content.DialogInterface;
27import android.content.Intent;
28import android.content.SharedPreferences;
29import android.content.SharedPreferences.Editor;
30import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
33import android.database.Cursor;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.media.AudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.os.AsyncResult;
36import android.os.Bundle;
37import android.os.Handler;
38import android.os.Message;
39import android.os.UserHandle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.preference.CheckBoxPreference;
41import android.preference.ListPreference;
42import android.preference.Preference;
43import android.preference.PreferenceActivity;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.preference.PreferenceManager;
45import android.preference.PreferenceScreen;
46import android.provider.ContactsContract.CommonDataKinds;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070048import android.telecom.PhoneAccountHandle;
49import android.telecom.TelecomManager;
Andrew Lee93c345f2014-10-27 15:25:07 -070050import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.text.TextUtils;
52import android.util.Log;
53import android.view.MenuItem;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.widget.ListAdapter;
55
Andrew Lee312e8172014-10-23 17:01:36 -070056import com.android.ims.ImsManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import com.android.internal.telephony.CallForwardInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import com.android.internal.telephony.Phone;
59import com.android.internal.telephony.PhoneConstants;
Andrew Lee2170a972014-08-13 18:13:01 -070060import com.android.phone.common.util.SettingsUtil;
Andrew Leedb2fe562014-09-03 15:40:43 -070061import com.android.phone.settings.AccountSelectionPreference;
Andrew Lee1af6cf72014-11-04 17:35:26 -080062import com.android.phone.settings.CallForwardInfoUtil;
Andrew Lee84024342014-11-06 23:37:09 -080063import com.android.phone.settings.VoicemailDialogUtil;
Andrew Leeb490d732014-10-27 15:00:41 -070064import com.android.phone.settings.VoicemailProviderSettings;
Andrew Lee88b51e22014-10-29 15:48:51 -070065import com.android.phone.settings.VoicemailProviderSettingsUtil;
Andrew Lee5ed870c2014-10-29 11:47:49 -070066import com.android.phone.settings.fdn.FdnSetting;
Sailesh Nepal788959e2014-07-08 23:36:40 -070067import com.android.services.telephony.sip.SipUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068
Andrew Lee2170a972014-08-13 18:13:01 -070069import java.lang.String;
Andrew Leef1776d82014-11-04 14:45:02 -080070import java.util.ArrayList;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import java.util.Collection;
72import java.util.HashMap;
73import java.util.HashSet;
74import java.util.Iterator;
75import java.util.List;
76import java.util.Map;
77
78/**
79 * Top level "Call settings" UI; see res/xml/call_feature_setting.xml
80 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070081 * This preference screen is the root of the "Call settings" hierarchy available from the Phone
82 * app; the settings here let you control various features related to phone calls (including
83 * voicemail settings, the "Respond via SMS" feature, and others.) It's used only on
84 * voice-capable phone devices.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070085 *
86 * Note that this activity is part of the package com.android.phone, even
87 * though you reach it from the "Phone" app (i.e. DialtactsActivity) which
88 * is from the package com.android.contacts.
89 *
90 * For the "Mobile network settings" screen under the main Settings app,
91 * See {@link MobileNetworkSettings}.
92 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070093 * TODO: Settings should be split into PreferenceFragments where possible (ie. voicemail).
Andrew Lee2170a972014-08-13 18:13:01 -070094 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095 * @see com.android.phone.MobileNetworkSettings
96 */
97public class CallFeaturesSetting extends PreferenceActivity
98 implements DialogInterface.OnClickListener,
Andrew Lee2170a972014-08-13 18:13:01 -070099 Preference.OnPreferenceChangeListener,
Andrew Lee2170a972014-08-13 18:13:01 -0700100 EditPhoneNumberPreference.OnDialogClosedListener,
Andrew Leece8ae2a2014-09-10 10:41:48 -0700101 EditPhoneNumberPreference.GetDefaultNumberListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102 private static final String LOG_TAG = "CallFeaturesSetting";
103 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
Andrew Lee77527ac2014-10-21 16:57:39 -0700104 // STOPSHIP if true. Flag to override behavior default behavior to hide VT setting.
105 private static final boolean ENABLE_VT_FLAG = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700106
107 /**
108 * Intent action to bring up Voicemail Provider settings.
109 *
110 * @see #IGNORE_PROVIDER_EXTRA
111 */
112 public static final String ACTION_ADD_VOICEMAIL =
113 "com.android.phone.CallFeaturesSetting.ADD_VOICEMAIL";
114 // intent action sent by this activity to a voice mail provider
115 // to trigger its configuration UI
116 public static final String ACTION_CONFIGURE_VOICEMAIL =
117 "com.android.phone.CallFeaturesSetting.CONFIGURE_VOICEMAIL";
118 // Extra put in the return from VM provider config containing voicemail number to set
119 public static final String VM_NUMBER_EXTRA = "com.android.phone.VoicemailNumber";
120 // Extra put in the return from VM provider config containing call forwarding number to set
121 public static final String FWD_NUMBER_EXTRA = "com.android.phone.ForwardingNumber";
122 // Extra put in the return from VM provider config containing call forwarding number to set
123 public static final String FWD_NUMBER_TIME_EXTRA = "com.android.phone.ForwardingNumberTime";
124 // If the VM provider returns non null value in this extra we will force the user to
125 // choose another VM provider
126 public static final String SIGNOUT_EXTRA = "com.android.phone.Signout";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128 // Key identifying the default vocie mail provider
129 public static final String DEFAULT_VM_PROVIDER_KEY = "";
130
131 /**
132 * String Extra put into ACTION_ADD_VOICEMAIL call to indicate which provider should be hidden
133 * in the list of providers presented to the user. This allows a provider which is being
134 * disabled (e.g. GV user logging out) to force the user to pick some other provider.
135 */
136 public static final String IGNORE_PROVIDER_EXTRA = "com.android.phone.ProviderToIgnore";
137
138 // string constants
139 private static final String NUM_PROJECTION[] = {CommonDataKinds.Phone.NUMBER};
140
141 // String keys for preference lookup
142 // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
Andrew Lee97708a42014-09-25 12:39:07 -0700143 private static final String VOICEMAIL_SETTING_SCREEN_PREF_KEY = "button_voicemail_category_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700144 private static final String BUTTON_VOICEMAIL_KEY = "button_voicemail_key";
145 private static final String BUTTON_VOICEMAIL_PROVIDER_KEY = "button_voicemail_provider_key";
146 private static final String BUTTON_VOICEMAIL_SETTING_KEY = "button_voicemail_setting_key";
147 // New preference key for voicemail notification vibration
148 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY =
149 "button_voicemail_notification_vibrate_key";
150 // Old preference key for voicemail notification vibration. Used for migration to the new
151 // preference key only.
152 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY =
153 "button_voicemail_notification_vibrate_when_key";
154 /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_RINGTONE_KEY =
155 "button_voicemail_notification_ringtone_key";
156 private static final String BUTTON_FDN_KEY = "button_fdn_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700157
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700158 private static final String BUTTON_DTMF_KEY = "button_dtmf_settings";
159 private static final String BUTTON_RETRY_KEY = "button_auto_retry_key";
160 private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
161 private static final String BUTTON_HAC_KEY = "button_hac_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700162
163 private static final String BUTTON_GSM_UMTS_OPTIONS = "button_gsm_more_expand_key";
164 private static final String BUTTON_CDMA_OPTIONS = "button_cdma_more_expand_key";
Andrew Lee2b36ba22014-11-05 17:08:49 -0800165 private static final String CALL_FORWARDING_KEY = "call_forwarding_key";
166 private static final String ADDITIONAL_GSM_SETTINGS_KEY = "additional_gsm_call_settings_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700167
Andrew Leece8ae2a2014-09-10 10:41:48 -0700168 private static final String PHONE_ACCOUNT_SETTINGS_KEY =
169 "phone_account_settings_preference_screen";
Andrew Leedb2fe562014-09-03 15:40:43 -0700170
Andrew Leedf14ead2014-10-17 14:22:52 -0700171 private static final String ENABLE_VIDEO_CALLING_KEY = "button_enable_video_calling";
172
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700173 /** Event for Async voicemail change call */
174 private static final int EVENT_VOICEMAIL_CHANGED = 500;
175 private static final int EVENT_FORWARDING_CHANGED = 501;
176 private static final int EVENT_FORWARDING_GET_COMPLETED = 502;
177
Andrew Lee2170a972014-08-13 18:13:01 -0700178 private static final int MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY = 1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700179
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700180 public static final String HAC_KEY = "HACSetting";
181 public static final String HAC_VAL_ON = "ON";
182 public static final String HAC_VAL_OFF = "OFF";
183
184 /** Handle to voicemail pref */
185 private static final int VOICEMAIL_PREF_ID = 1;
186 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
187
188 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189 private AudioManager mAudioManager;
Andrew Lee88b51e22014-10-29 15:48:51 -0700190 private VoicemailProviderSettingsUtil mVmProviderSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700191
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700192 // voicemail notification vibration string constants
193 private static final String VOICEMAIL_VIBRATION_ALWAYS = "always";
194 private static final String VOICEMAIL_VIBRATION_NEVER = "never";
195
Andrew Lee5ed870c2014-10-29 11:47:49 -0700196 private SubscriptionInfoHelper mSubscriptionInfoHelper;
197
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
199
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200 /** Whether dialpad plays DTMF tone or not. */
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700201 private CheckBoxPreference mButtonAutoRetry;
202 private CheckBoxPreference mButtonHAC;
203 private ListPreference mButtonDTMF;
204 private ListPreference mButtonTTY;
Andrew Leece8ae2a2014-09-10 10:41:48 -0700205 private Preference mPhoneAccountSettingsPreference;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700206 private ListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700207 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700208 private PreferenceScreen mVoicemailSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700209 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700210 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700211
212 private class VoiceMailProvider {
Andrew Leef1776d82014-11-04 14:45:02 -0800213 public String name;
214 public Intent intent;
215
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700216 public VoiceMailProvider(String name, Intent intent) {
217 this.name = name;
218 this.intent = intent;
219 }
Andrew Leef1776d82014-11-04 14:45:02 -0800220
221 public String toString() {
222 return "[ Name: " + name + ", Intent: " + intent + " ]";
223 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700224 }
225
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700226 /**
227 * Results of reading forwarding settings
228 */
229 private CallForwardInfo[] mForwardingReadResults = null;
230
231 /**
232 * Result of forwarding number change.
233 * Keys are reasons (eg. unconditional forwarding).
234 */
235 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
236
237 /**
238 * Expected CF read result types.
239 * This set keeps track of the CF types for which we've issued change
240 * commands so we can tell when we've received all of the responses.
241 */
242 private Collection<Integer> mExpectedChangeResultReasons = null;
243
244 /**
245 * Result of vm number change
246 */
247 private AsyncResult mVoicemailChangeResult = null;
248
249 /**
250 * Previous VM provider setting so we can return to it in case of failure.
251 */
252 private String mPreviousVMProviderKey = null;
253
254 /**
255 * Id of the dialog being currently shown.
256 */
257 private int mCurrentDialogId = 0;
258
259 /**
260 * Flag indicating that we are invoking settings for the voicemail provider programmatically
261 * due to vm provider change.
262 */
263 private boolean mVMProviderSettingsForced = false;
264
265 /**
266 * Flag indicating that we are making changes to vm or fwd numbers
267 * due to vm provider change.
268 */
269 private boolean mChangingVMorFwdDueToProviderChange = false;
270
271 /**
272 * True if we are in the process of vm & fwd number change and vm has already been changed.
273 * This is used to decide what to do in case of rollback.
274 */
275 private boolean mVMChangeCompletedSuccessfully = false;
276
277 /**
278 * True if we had full or partial failure setting forwarding numbers and so need to roll them
279 * back.
280 */
281 private boolean mFwdChangesRequireRollback = false;
282
283 /**
284 * Id of error msg to display to user once we are done reverting the VM provider to the previous
285 * one.
286 */
287 private int mVMOrFwdSetError = 0;
288
289 /**
290 * Data about discovered voice mail settings providers.
291 * Is populated by querying which activities can handle ACTION_CONFIGURE_VOICEMAIL.
292 * They key in this map is package name + activity name.
293 * We always add an entry for the default provider with a key of empty
294 * string and intent value of null.
295 * @see #initVoiceMailProviders()
296 */
297 private final Map<String, VoiceMailProvider> mVMProvidersData =
298 new HashMap<String, VoiceMailProvider>();
299
300 /** string to hold old voicemail number as it is being updated. */
301 private String mOldVmNumber;
302
303 // New call forwarding settings and vm number we will be setting
304 // Need to save these since before we get to saving we need to asynchronously
305 // query the existing forwarding settings.
306 private CallForwardInfo[] mNewFwdSettings;
307 private String mNewVMNumber;
308
309 private boolean mForeground;
310
311 @Override
312 public void onPause() {
313 super.onPause();
314 mForeground = false;
315 }
316
317 /**
318 * We have to pull current settings from the network for all kinds of
319 * voicemail providers so we can tell whether we have to update them,
320 * so use this bit to keep track of whether we're reading settings for the
321 * default provider and should therefore save them out when done.
322 */
323 private boolean mReadingSettingsForDefaultProvider = false;
324
Tyler Gunnbaee2952014-09-10 16:01:02 -0700325 /**
326 * Used to indicate that the voicemail preference should be shown.
327 */
328 private boolean mShowVoicemailPreference = false;
329
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700330 /*
331 * Click Listeners, handle click based on objects attached to UI.
332 */
333
334 // Click listener for all toggle events
335 @Override
336 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
337 if (preference == mSubMenuVoicemailSettings) {
338 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700339 } else if (preference == mButtonDTMF) {
340 return true;
341 } else if (preference == mButtonTTY) {
342 return true;
343 } else if (preference == mButtonAutoRetry) {
344 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
345 android.provider.Settings.Global.CALL_AUTO_RETRY,
346 mButtonAutoRetry.isChecked() ? 1 : 0);
347 return true;
348 } else if (preference == mButtonHAC) {
349 int hac = mButtonHAC.isChecked() ? 1 : 0;
350 // Update HAC value in Settings database
351 Settings.System.putInt(mPhone.getContext().getContentResolver(),
352 Settings.System.HEARING_AID, hac);
353
354 // Update HAC Value in AudioManager
355 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
356 return true;
357 } else if (preference == mVoicemailSettings) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800358 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
359
Yorke Leea0f63bf2014-10-09 18:27:20 -0700360 final Dialog dialog = mVoicemailSettings.getDialog();
361 if (dialog != null) {
362 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
363 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800364
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700365 if (preference.getIntent() != null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800366 if (DBG) log("Invoking cfg intent " + preference.getIntent().getPackage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700367
368 // onActivityResult() will be responsible for resetting some of variables.
369 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
370 return true;
371 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800372 if (DBG) log("onPreferenceTreeClick(). No intent; use default behavior in xml.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700373
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800374 // onActivityResult() will not be called, so reset variables here.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700375 mPreviousVMProviderKey = DEFAULT_VM_PROVIDER_KEY;
376 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700377 return false;
378 }
Andrew Lee97708a42014-09-25 12:39:07 -0700379 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700380 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
381 if (dialog != null) {
382 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
383 }
Andrew Lee97708a42014-09-25 12:39:07 -0700384 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700385 }
386 return false;
387 }
388
389 /**
390 * Implemented to support onPreferenceChangeListener to look for preference
391 * changes.
392 *
393 * @param preference is the preference to be changed
394 * @param objValue should be the value of the selection, NOT its localized
395 * display value.
396 */
397 @Override
398 public boolean onPreferenceChange(Preference preference, Object objValue) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800399 if (DBG) log("onPreferenceChange: \"" + preference + "\" changed to \"" + objValue + "\"");
Andrew Lee2170a972014-08-13 18:13:01 -0700400
401 if (preference == mButtonDTMF) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700402 int index = mButtonDTMF.findIndexOfValue((String) objValue);
403 Settings.System.putInt(mPhone.getContext().getContentResolver(),
404 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
405 } else if (preference == mButtonTTY) {
406 handleTTYChange(preference, objValue);
407 } else if (preference == mVoicemailProviders) {
408 final String newProviderKey = (String) objValue;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800409
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700410 // If previous provider key and the new one is same, we don't need to handle it.
411 if (mPreviousVMProviderKey.equals(newProviderKey)) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800412 if (DBG) log("No change is made to the VM provider setting.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700413 return true;
414 }
415 updateVMPreferenceWidgets(newProviderKey);
416
Andrew Leeb490d732014-10-27 15:00:41 -0700417 final VoicemailProviderSettings newProviderSettings =
Andrew Lee88b51e22014-10-29 15:48:51 -0700418 mVmProviderSettingsUtil.load(newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700419
Andrew Lee88b51e22014-10-29 15:48:51 -0700420 // If the user switches to a voice mail provider and we have numbers stored for it we
421 // will automatically change the phone's voice mail and forwarding number to the stored
422 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700423 if (newProviderSettings == null) {
424 // Force the user into a configuration of the chosen provider
425 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
426 mVMProviderSettingsForced = true;
427 simulatePreferenceClick(mVoicemailSettings);
428 } else {
429 if (DBG) log("Saved preferences found - switching to them");
430 // Set this flag so if we get a failure we revert to previous provider
431 mChangingVMorFwdDueToProviderChange = true;
432 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
433 }
Andrew Leedf14ead2014-10-17 14:22:52 -0700434 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700435 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
436 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
437 } else {
438 AlertDialog.Builder builder = new AlertDialog.Builder(this);
439 DialogInterface.OnClickListener networkSettingsClickListener =
440 new Dialog.OnClickListener() {
441 @Override
442 public void onClick(DialogInterface dialog, int which) {
443 startActivity(new Intent(mPhone.getContext(),
444 com.android.phone.MobileNetworkSettings.class));
445 }
446 };
447 builder.setMessage(getResources().getString(
448 R.string.enable_video_calling_dialog_msg))
449 .setNeutralButton(getResources().getString(
450 R.string.enable_video_calling_dialog_settings),
451 networkSettingsClickListener)
452 .setPositiveButton(android.R.string.ok, null)
453 .show();
454 return false;
455 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700456 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800457
458 // Always let the preference setting proceed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700459 return true;
460 }
461
462 @Override
463 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800464 if (DBG) log("onDialogClosed: Button clicked is " + buttonClicked);
465
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700466 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
467 return;
468 }
469
470 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700471 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
472 mSubMenuVoicemailSettings.getPhoneNumber(),
473 VoicemailProviderSettings.NO_FORWARDING);
474 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700475 }
476 }
477
478 /**
479 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
480 * This method set the default values for the various
481 * EditPhoneNumberPreference dialogs.
482 */
483 @Override
484 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
485 if (preference == mSubMenuVoicemailSettings) {
486 // update the voicemail number field, which takes care of the
487 // mSubMenuVoicemailSettings itself, so we should return null.
488 if (DBG) log("updating default for voicemail dialog");
489 updateVoiceNumberField();
490 return null;
491 }
492
493 String vmDisplay = mPhone.getVoiceMailNumber();
494 if (TextUtils.isEmpty(vmDisplay)) {
495 // if there is no voicemail number, we just return null to
496 // indicate no contribution.
497 return null;
498 }
499
500 // Return the voicemail number prepended with "VM: "
501 if (DBG) log("updating default for call forwarding dialogs");
502 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
503 }
504
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700505 private void switchToPreviousVoicemailProvider() {
506 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700507
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800508 if (mPreviousVMProviderKey == null) {
509 return;
510 }
511
512 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
Andrew Lee84024342014-11-06 23:37:09 -0800513 showDialogIfForeground(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800514 final VoicemailProviderSettings prevSettings =
515 mVmProviderSettingsUtil.load(mPreviousVMProviderKey);
516 if (prevSettings == null) {
517 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
518 + mPreviousVMProviderKey + "\" is null but should be loaded.");
519 }
520
521 if (mVMChangeCompletedSuccessfully) {
522 mNewVMNumber = prevSettings.getVoicemailNumber();
523 Log.i(LOG_TAG, "VM change is already completed successfully."
524 + "Have to revert VM back to " + mNewVMNumber + " again.");
525 mPhone.setVoiceMailNumber(
526 mPhone.getVoiceMailAlphaTag().toString(),
527 mNewVMNumber,
528 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
529 }
530
531 if (mFwdChangesRequireRollback) {
532 Log.i(LOG_TAG, "Requested to rollback forwarding changes.");
533
534 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
535 if (prevFwdSettings != null) {
536 Map<Integer, AsyncResult> results = mForwardingChangeResults;
537 resetForwardingChangeState();
538 for (int i = 0; i < prevFwdSettings.length; i++) {
539 CallForwardInfo fi = prevFwdSettings[i];
540 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
541 // Only revert the settings for which the update succeeded.
542 AsyncResult result = results.get(fi.reason);
543 if (result != null && result.exception == null) {
544 mExpectedChangeResultReasons.add(fi.reason);
545 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
546 mRevertOptionComplete.obtainMessage(
547 EVENT_FORWARDING_CHANGED, i, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700548 }
549 }
550 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700551 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800552 } else {
553 if (DBG) log("No need to revert");
554 onRevertDone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700555 }
556 }
557
558 private void onRevertDone() {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800559 if (DBG) log("onRevertDone: Changing provider key back to " + mPreviousVMProviderKey);
560
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700561 updateVMPreferenceWidgets(mPreviousVMProviderKey);
562 updateVoiceNumberField();
563 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800564 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700565 mVMOrFwdSetError = 0;
566 }
567 }
568
569 @Override
570 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
571 if (DBG) {
572 log("onActivityResult: requestCode: " + requestCode
573 + ", resultCode: " + resultCode
574 + ", data: " + data);
575 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800576
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700577 // there are cases where the contact picker may end up sending us more than one
578 // request. We want to ignore the request if we're not in the correct state.
579 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
580 boolean failure = false;
581
582 // No matter how the processing of result goes lets clear the flag
583 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
584 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
585 mVMProviderSettingsForced = false;
586
587 String vmNum = null;
588 if (resultCode != RESULT_OK) {
589 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
590 failure = true;
591 } else {
592 if (data == null) {
593 if (DBG) log("onActivityResult: vm provider cfg result has no data");
594 failure = true;
595 } else {
596 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
597 if (DBG) log("Provider requested signout");
598 if (isVMProviderSettingsForced) {
599 if (DBG) log("Going back to previous provider on signout");
600 switchToPreviousVoicemailProvider();
601 } else {
602 final String victim = getCurrentVoicemailProviderKey();
603 if (DBG) log("Relaunching activity and ignoring " + victim);
604 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
605 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
606 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
607 this.startActivity(i);
608 }
609 return;
610 }
611 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
612 if (vmNum == null || vmNum.length() == 0) {
613 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
614 failure = true;
615 }
616 }
617 }
618 if (failure) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800619 if (DBG) log("Failure in return from voicemail provider.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700620 if (isVMProviderSettingsForced) {
621 switchToPreviousVoicemailProvider();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700622 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800623
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700624 return;
625 }
626 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
627 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
628
Santos Cordonda120f42014-08-06 04:44:34 -0700629 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700630 // send it to the provider when it's config is invoked so it can use this as default
631 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
632
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800633 if (DBG) log("onActivityResult: cfg result has forwarding number " + fwdNum);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700634 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700635 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700636 return;
637 }
638
639 if (requestCode == VOICEMAIL_PREF_ID) {
640 if (resultCode != RESULT_OK) {
641 if (DBG) log("onActivityResult: contact picker result not OK.");
642 return;
643 }
644
645 Cursor cursor = null;
646 try {
647 cursor = getContentResolver().query(data.getData(),
648 NUM_PROJECTION, null, null, null);
649 if ((cursor == null) || (!cursor.moveToFirst())) {
650 if (DBG) log("onActivityResult: bad contact data, no results found.");
651 return;
652 }
653 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
654 return;
655 } finally {
656 if (cursor != null) {
657 cursor.close();
658 }
659 }
660 }
661
662 super.onActivityResult(requestCode, resultCode, data);
663 }
664
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700665 /**
666 * Wrapper around showDialog() that will silently do nothing if we're
667 * not in the foreground.
668 *
669 * This is useful here because most of the dialogs we display from
670 * this class are triggered by asynchronous events (like
671 * success/failure messages from the telephony layer) and it's
672 * possible for those events to come in even after the user has gone
673 * to a different screen.
674 */
675 // TODO: this is too brittle: it's still easy to accidentally add new
676 // code here that calls showDialog() directly (which will result in a
677 // WindowManager$BadTokenException if called after the activity has
678 // been stopped.)
679 //
680 // It would be cleaner to do the "if (mForeground)" check in one
681 // central place, maybe by using a single Handler for all asynchronous
682 // events (and have *that* discard events if we're not in the
683 // foreground.)
684 //
685 // Unfortunately it's not that simple, since we sometimes need to do
686 // actual work to handle these events whether or not we're in the
687 // foreground (see the Handler code in mSetOptionComplete for
688 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800689 //
690 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
691 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700692 private void showDialogIfForeground(int id) {
693 if (mForeground) {
694 showDialog(id);
695 }
696 }
697
698 private void dismissDialogSafely(int id) {
699 try {
700 dismissDialog(id);
701 } catch (IllegalArgumentException e) {
702 // This is expected in the case where we were in the background
703 // at the time we would normally have shown the dialog, so we didn't
704 // show it.
705 }
706 }
707
Andrew Leeb490d732014-10-27 15:00:41 -0700708 private void saveVoiceMailAndForwardingNumber(
709 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700710 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700711 mNewVMNumber = newSettings.getVoicemailNumber();
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800712 mNewVMNumber = (mNewVMNumber == null) ? "" : mNewVMNumber;
Andrew Leeb490d732014-10-27 15:00:41 -0700713 mNewFwdSettings = newSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700714
715 // No fwd settings on CDMA
716 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
717 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700718 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700719 }
720
Andrew Leee3c15212014-10-28 13:12:55 -0700721 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700722 if (mNewVMNumber.equals(mOldVmNumber)
723 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Lee84024342014-11-06 23:37:09 -0800724 showDialogIfForeground(VoicemailDialogUtil.VM_NOCHANGE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700725 return;
726 }
727
Andrew Lee88b51e22014-10-29 15:48:51 -0700728 mVmProviderSettingsUtil.save(key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700729 mVMChangeCompletedSuccessfully = false;
730 mFwdChangesRequireRollback = false;
731 mVMOrFwdSetError = 0;
732 if (!key.equals(mPreviousVMProviderKey)) {
733 mReadingSettingsForDefaultProvider =
734 mPreviousVMProviderKey.equals(DEFAULT_VM_PROVIDER_KEY);
735 if (DBG) log("Reading current forwarding settings");
Andrew Leeb490d732014-10-27 15:00:41 -0700736 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
737 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
738 for (int i = 0; i < mForwardingReadResults.length; i++) {
739 mPhone.getCallForwardingOption(
740 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700741 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
742 }
Andrew Lee84024342014-11-06 23:37:09 -0800743 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700744 } else {
745 saveVoiceMailAndForwardingNumberStage2();
746 }
747 }
748
749 private final Handler mGetOptionComplete = new Handler() {
750 @Override
751 public void handleMessage(Message msg) {
752 AsyncResult result = (AsyncResult) msg.obj;
753 switch (msg.what) {
754 case EVENT_FORWARDING_GET_COMPLETED:
755 handleForwardingSettingsReadResult(result, msg.arg1);
756 break;
757 }
758 }
759 };
760
761 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
762 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800763
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700764 Throwable error = null;
765 if (ar.exception != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700766 error = ar.exception;
Andrew Lee1af6cf72014-11-04 17:35:26 -0800767 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700768 }
769 if (ar.userObj instanceof Throwable) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800770 error = (Throwable) ar.userObj;
771 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700772 }
773
774 // We may have already gotten an error and decided to ignore the other results.
775 if (mForwardingReadResults == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800776 if (DBG) Log.d(LOG_TAG, "Ignoring fwd reading result: " + idx);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700777 return;
778 }
779
780 // In case of error ignore other results, show an error dialog
781 if (error != null) {
782 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
783 mForwardingReadResults = null;
Andrew Lee84024342014-11-06 23:37:09 -0800784 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
785 showDialogIfForeground(VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700786 return;
787 }
788
Andrew Lee1af6cf72014-11-04 17:35:26 -0800789 // Get the forwarding info.
790 mForwardingReadResults[idx] = CallForwardInfoUtil.getCallForwardInfo(
791 (CallForwardInfo[]) ar.result,
792 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx]);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700793
794 // Check if we got all the results already
795 boolean done = true;
796 for (int i = 0; i < mForwardingReadResults.length; i++) {
797 if (mForwardingReadResults[i] == null) {
798 done = false;
799 break;
800 }
801 }
Andrew Lee1af6cf72014-11-04 17:35:26 -0800802
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700803 if (done) {
804 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
Andrew Lee84024342014-11-06 23:37:09 -0800805 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800806
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700807 if (mReadingSettingsForDefaultProvider) {
Andrew Lee88b51e22014-10-29 15:48:51 -0700808 mVmProviderSettingsUtil.save(DEFAULT_VM_PROVIDER_KEY,
809 new VoicemailProviderSettings(this.mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700810 mReadingSettingsForDefaultProvider = false;
811 }
812 saveVoiceMailAndForwardingNumberStage2();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700813 }
814 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800815
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700816 private void resetForwardingChangeState() {
817 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
818 mExpectedChangeResultReasons = new HashSet<Integer>();
819 }
820
821 // Called after we are done saving the previous forwarding settings if
822 // we needed.
823 private void saveVoiceMailAndForwardingNumberStage2() {
824 mForwardingChangeResults = null;
825 mVoicemailChangeResult = null;
Andrew Leeb490d732014-10-27 15:00:41 -0700826 if (mNewFwdSettings != VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700827 resetForwardingChangeState();
828 for (int i = 0; i < mNewFwdSettings.length; i++) {
829 CallForwardInfo fi = mNewFwdSettings[i];
Andrew Lee1af6cf72014-11-04 17:35:26 -0800830 CallForwardInfo fiForReason =
831 CallForwardInfoUtil.infoForReason(mForwardingReadResults, fi.reason);
832 final boolean doUpdate = CallForwardInfoUtil.isUpdateRequired(fiForReason, fi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700833
834 if (doUpdate) {
835 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
836 mExpectedChangeResultReasons.add(i);
837
Andrew Lee1af6cf72014-11-04 17:35:26 -0800838 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700839 mSetOptionComplete.obtainMessage(
840 EVENT_FORWARDING_CHANGED, fi.reason, 0));
841 }
842 }
Andrew Lee84024342014-11-06 23:37:09 -0800843 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700844 } else {
845 if (DBG) log("Not touching fwd #");
846 setVMNumberWithCarrier();
847 }
848 }
849
850 private void setVMNumberWithCarrier() {
851 if (DBG) log("save voicemail #: " + mNewVMNumber);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800852
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700853 mPhone.setVoiceMailNumber(
854 mPhone.getVoiceMailAlphaTag().toString(),
855 mNewVMNumber,
856 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
857 }
858
859 /**
860 * Callback to handle option update completions
861 */
862 private final Handler mSetOptionComplete = new Handler() {
863 @Override
864 public void handleMessage(Message msg) {
865 AsyncResult result = (AsyncResult) msg.obj;
866 boolean done = false;
867 switch (msg.what) {
868 case EVENT_VOICEMAIL_CHANGED:
869 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700870 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700871 done = true;
872 break;
873 case EVENT_FORWARDING_CHANGED:
874 mForwardingChangeResults.put(msg.arg1, result);
875 if (result.exception != null) {
876 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
877 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700878 }
Andrew Leee438b312014-10-29 16:59:15 -0700879 if (isForwardingCompleted()) {
880 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700881 if (DBG) log("Overall fwd changes completed ok, starting vm change");
882 setVMNumberWithCarrier();
883 } else {
884 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
885 "Check if we need to try rollback for some settings.");
886 mFwdChangesRequireRollback = false;
887 Iterator<Map.Entry<Integer,AsyncResult>> it =
888 mForwardingChangeResults.entrySet().iterator();
889 while (it.hasNext()) {
890 Map.Entry<Integer,AsyncResult> entry = it.next();
891 if (entry.getValue().exception == null) {
892 // If at least one succeeded we have to revert
893 Log.i(LOG_TAG, "Rollback will be required");
894 mFwdChangesRequireRollback = true;
895 break;
896 }
897 }
898 if (!mFwdChangesRequireRollback) {
899 Log.i(LOG_TAG, "No rollback needed.");
900 }
901 done = true;
902 }
903 }
904 break;
905 default:
906 // TODO: should never reach this, may want to throw exception
907 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800908
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700909 if (done) {
910 if (DBG) log("All VM provider related changes done");
911 if (mForwardingChangeResults != null) {
Andrew Lee84024342014-11-06 23:37:09 -0800912 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700913 }
Andrew Leee438b312014-10-29 16:59:15 -0700914 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700915 }
916 }
917 };
918
919 /**
920 * Callback to handle option revert completions
921 */
922 private final Handler mRevertOptionComplete = new Handler() {
923 @Override
924 public void handleMessage(Message msg) {
925 AsyncResult result = (AsyncResult) msg.obj;
926 switch (msg.what) {
927 case EVENT_VOICEMAIL_CHANGED:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700928 if (DBG) log("VM revert complete msg");
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800929 mVoicemailChangeResult = result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700930 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800931
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700932 case EVENT_FORWARDING_CHANGED:
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800933 if (DBG) log("FWD revert complete msg ");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700934 mForwardingChangeResults.put(msg.arg1, result);
935 if (result.exception != null) {
936 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
937 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700938 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700939 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800940
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700941 default:
942 // TODO: should never reach this, may want to throw exception
943 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800944
945 final boolean done = (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null)
946 && (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700947 if (done) {
948 if (DBG) log("All VM reverts done");
Andrew Lee84024342014-11-06 23:37:09 -0800949 dismissDialogSafely(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700950 onRevertDone();
951 }
952 }
953 };
954
955 /**
Andrew Leee438b312014-10-29 16:59:15 -0700956 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700957 */
Andrew Leee438b312014-10-29 16:59:15 -0700958 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700959 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -0700960 return true;
961 }
962
963 for (Integer reason : mExpectedChangeResultReasons) {
964 if (mForwardingChangeResults.get(reason) == null) {
965 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700966 }
967 }
Andrew Leee438b312014-10-29 16:59:15 -0700968
969 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700970 }
Andrew Leee438b312014-10-29 16:59:15 -0700971
972 private boolean isFwdChangeSuccess() {
973 if (mForwardingChangeResults == null) {
974 return true;
975 }
976
977 for (AsyncResult result : mForwardingChangeResults.values()) {
978 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700979 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700980 String msg = exception.getMessage();
981 msg = (msg != null) ? msg : "";
982 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
983 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700984 }
985 }
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 boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700990 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700991 String msg = mVoicemailChangeResult.exception.getMessage();
992 msg = (msg != null) ? msg : "";
993 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
994 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700995 }
Andrew Leee438b312014-10-29 16:59:15 -0700996 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700997 }
998
Andrew Leee438b312014-10-29 16:59:15 -0700999 private void handleSetVmOrFwdMessage() {
1000 if (DBG) log("handleSetVMMessage: set VM request complete");
1001
1002 if (!isFwdChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -08001003 handleVmOrFwdSetError(VoicemailDialogUtil.FWD_SET_RESPONSE_ERROR_DIALOG);
Andrew Leee438b312014-10-29 16:59:15 -07001004 } else if (!isVmChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -08001005 handleVmOrFwdSetError(VoicemailDialogUtil.VM_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001006 } else {
Andrew Lee84024342014-11-06 23:37:09 -08001007 handleVmAndFwdSetSuccess(VoicemailDialogUtil.VM_CONFIRM_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001008 }
1009 }
1010
1011 /**
1012 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
1013 * changes to those settings and show "failure" dialog.
1014 *
Andrew Leeab082272014-11-04 15:50:42 -08001015 * @param dialogId ID of the dialog to show for the specific error case. Either
Andrew Lee84024342014-11-06 23:37:09 -08001016 * {@link #FWD_SET_RESPONSE_ERROR_DIALOG} or {@link #VM_RESPONSE_ERROR_DIALOG}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001017 */
Andrew Leeab082272014-11-04 15:50:42 -08001018 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001019 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -08001020 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001021 mChangingVMorFwdDueToProviderChange = false;
1022 switchToPreviousVoicemailProvider();
1023 return;
1024 }
1025 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001026 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001027 updateVoiceNumberField();
1028 }
1029
1030 /**
1031 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1032 * This updates a bunch of variables and show "success" dialog.
1033 */
Andrew Leeab082272014-11-04 15:50:42 -08001034 private void handleVmAndFwdSetSuccess(int dialogId) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001035 if (DBG) log("handleVmAndFwdSetSuccess: key is " + getCurrentVoicemailProviderKey());
1036
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001037 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1038 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001039 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001040 updateVoiceNumberField();
1041 }
1042
1043 /**
1044 * Update the voicemail number from what we've recorded on the sim.
1045 */
1046 private void updateVoiceNumberField() {
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001047 if (DBG) log("updateVoiceNumberField()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001048
1049 mOldVmNumber = mPhone.getVoiceMailNumber();
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001050 if (TextUtils.isEmpty(mOldVmNumber)) {
1051 mSubMenuVoicemailSettings.setPhoneNumber("");
1052 mSubMenuVoicemailSettings.setSummary(getString(R.string.voicemail_number_not_set));
1053 } else {
1054 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1055 mSubMenuVoicemailSettings.setSummary(mOldVmNumber);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001056 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001057 }
1058
1059 /*
1060 * Helper Methods for Activity class.
1061 * The initial query commands are split into two pieces now
1062 * for individual expansion. This combined with the ability
1063 * to cancel queries allows for a much better user experience,
1064 * and also ensures that the user only waits to update the
1065 * data that is relevant.
1066 */
1067
1068 @Override
1069 protected void onPrepareDialog(int id, Dialog dialog) {
1070 super.onPrepareDialog(id, dialog);
1071 mCurrentDialogId = id;
1072 }
1073
1074 // dialog creation method, called by showDialog()
1075 @Override
Andrew Lee84024342014-11-06 23:37:09 -08001076 protected Dialog onCreateDialog(int dialogId) {
1077 return VoicemailDialogUtil.getDialog(this, dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001078 }
1079
1080 // This is a method implemented for DialogInterface.OnClickListener.
1081 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1082 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1083 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1084 public void onClick(DialogInterface dialog, int which) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001085 if (DBG) log("onClick: button clicked is " + which);
1086
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001087 dialog.dismiss();
1088 switch (which){
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001089 case DialogInterface.BUTTON_NEGATIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001090 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001091 // We failed to get current forwarding settings and the user
1092 // does not wish to continue.
1093 switchToPreviousVoicemailProvider();
1094 }
1095 break;
1096 case DialogInterface.BUTTON_POSITIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001097 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001098 // We failed to get current forwarding settings but the user
1099 // wishes to continue changing settings to the new vm provider
1100 saveVoiceMailAndForwardingNumberStage2();
1101 } else {
1102 finish();
1103 }
1104 return;
1105 default:
1106 // just let the dialog close and go back to the input
1107 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001108
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001109 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1110 // with settings UI. If we were called to explicitly configure voice mail then
1111 // we finish the settings activity here to come back to whatever the user was doing.
1112 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1113 finish();
1114 }
1115 }
1116
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001117 /*
1118 * Activity class methods
1119 */
1120
1121 @Override
1122 protected void onCreate(Bundle icicle) {
1123 super.onCreate(icicle);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001124 if (DBG) log("onCreate: Intent is " + getIntent());
1125
Tyler Gunnbaee2952014-09-10 16:01:02 -07001126 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Andrew Lee88b51e22014-10-29 15:48:51 -07001127 mVmProviderSettingsUtil = new VoicemailProviderSettingsUtil(getApplicationContext());
Tyler Gunnbaee2952014-09-10 16:01:02 -07001128
Tyler Gunnbaee2952014-09-10 16:01:02 -07001129 // Show the voicemail preference in onResume if the calling intent specifies the
1130 // ACTION_ADD_VOICEMAIL action.
1131 mShowVoicemailPreference = (icicle == null) &&
Jay Shraunerbe2fb262014-11-11 15:19:58 -08001132 TextUtils.equals(getIntent().getAction(), ACTION_ADD_VOICEMAIL);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001133
1134 mSubscriptionInfoHelper = new SubscriptionInfoHelper(getIntent());
1135 mSubscriptionInfoHelper.setActionBarTitle(
1136 getActionBar(), getResources(), R.string.call_settings_with_label);
Andrew Lee1479dd12014-11-06 23:06:32 -08001137 mPhone = mSubscriptionInfoHelper.getPhone();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001138 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001139
1140 private void initPhoneAccountPreferences() {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001141 mPhoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001142
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001143 TelecomManager telecomManager = TelecomManager.from(this);
Andrew Lee93c345f2014-10-27 15:25:07 -07001144 TelephonyManager telephonyManager =
1145 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001146
Andrew Lee93c345f2014-10-27 15:25:07 -07001147 if ((telecomManager.getSimCallManagers().isEmpty() && !SipUtil.isVoipSupported(this))
1148 || telephonyManager.getPhoneCount() > 1) {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001149 getPreferenceScreen().removePreference(mPhoneAccountSettingsPreference);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001150 }
1151 }
1152
Tyler Gunnbaee2952014-09-10 16:01:02 -07001153 @Override
1154 protected void onResume() {
1155 super.onResume();
1156 mForeground = true;
1157
1158 PreferenceScreen preferenceScreen = getPreferenceScreen();
1159 if (preferenceScreen != null) {
1160 preferenceScreen.removeAll();
1161 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001162
1163 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001164
Andrew Leedb2fe562014-09-03 15:40:43 -07001165 initPhoneAccountPreferences();
1166
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001167 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001168 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1169 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1170 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1171 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001172
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001173 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1174 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1175 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
1176 mButtonTTY = (ListPreference) findPreference(BUTTON_TTY_KEY);
1177 mVoicemailProviders = (ListPreference) findPreference(BUTTON_VOICEMAIL_PROVIDER_KEY);
Andrew Lee312e8172014-10-23 17:01:36 -07001178 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001179
Andrew Lee2c027892014-10-29 11:29:54 -07001180 mVoicemailProviders.setOnPreferenceChangeListener(this);
1181 mVoicemailSettingsScreen =
1182 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
1183 mVoicemailSettings = (PreferenceScreen)findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
1184 mVoicemailNotificationVibrate =
1185 (CheckBoxPreference) findPreference(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY);
1186 initVoiceMailProviders();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001187
Andrew Lee64a7d792014-10-15 17:38:38 -07001188 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1189 mButtonDTMF.setOnPreferenceChangeListener(this);
1190 int dtmf = Settings.System.getInt(getContentResolver(),
1191 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1192 mButtonDTMF.setValueIndex(dtmf);
1193 } else {
1194 prefSet.removePreference(mButtonDTMF);
1195 mButtonDTMF = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001196 }
1197
Andrew Lee64a7d792014-10-15 17:38:38 -07001198 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1199 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1200 int autoretry = Settings.Global.getInt(
1201 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1202 mButtonAutoRetry.setChecked(autoretry != 0);
1203 } else {
1204 prefSet.removePreference(mButtonAutoRetry);
1205 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001206 }
1207
Andrew Lee64a7d792014-10-15 17:38:38 -07001208 if (getResources().getBoolean(R.bool.hac_enabled)) {
1209 mButtonHAC.setOnPreferenceChangeListener(this);
1210 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1211 mButtonHAC.setChecked(hac != 0);
1212 } else {
1213 prefSet.removePreference(mButtonHAC);
1214 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001215 }
1216
Andrew Lee64a7d792014-10-15 17:38:38 -07001217 TelecomManager telecomManager = TelecomManager.from(this);
1218 if (telecomManager != null && telecomManager.isTtySupported()) {
1219 mButtonTTY.setOnPreferenceChangeListener(this);
1220 int settingsTtyMode = Settings.Secure.getInt(getContentResolver(),
1221 Settings.Secure.PREFERRED_TTY_MODE,
1222 TelecomManager.TTY_MODE_OFF);
1223 mButtonTTY.setValue(Integer.toString(settingsTtyMode));
1224 updatePreferredTtyModeSummary(settingsTtyMode);
1225 } else {
1226 prefSet.removePreference(mButtonTTY);
1227 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001228 }
1229
1230 if (!getResources().getBoolean(R.bool.world_phone)) {
Andrew Lee2b36ba22014-11-05 17:08:49 -08001231 Preference cdmaOptions = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
1232 prefSet.removePreference(cdmaOptions);
1233
1234 // TODO: Support MSIM for this preference option.
1235 Preference gsmOptions = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
1236 prefSet.removePreference(gsmOptions);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001237
1238 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001239 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001240 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001241 prefSet.removePreference(fdnButton);
1242
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001243 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1244 addPreferencesFromResource(R.xml.cdma_call_privacy);
1245 }
1246 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001247 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(this, FdnSetting.class));
1248
Andrew Lee2170a972014-08-13 18:13:01 -07001249 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
Etan Cohen0ca1c802014-07-07 15:35:48 -07001250 addPreferencesFromResource(R.xml.gsm_umts_call_options);
Andrew Lee2b36ba22014-11-05 17:08:49 -08001251
1252 Preference callForwardingPref = prefSet.findPreference(CALL_FORWARDING_KEY);
1253 callForwardingPref.setIntent(mSubscriptionInfoHelper.getIntent(
1254 this, GsmUmtsCallForwardOptions.class));
1255
1256 Preference additionalGsmSettingsPref =
1257 prefSet.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
1258 additionalGsmSettingsPref.setIntent(mSubscriptionInfoHelper.getIntent(
1259 this, GsmUmtsAdditionalCallOptions.class));
Etan Cohen0ca1c802014-07-07 15:35:48 -07001260 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001261 } else {
1262 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1263 }
1264 }
1265
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001266 // check the intent that started this activity and pop up the voicemail
1267 // dialog if we've been asked to.
1268 // If we have at least one non default VM provider registered then bring up
1269 // the selection for the VM provider, otherwise bring up a VM number dialog.
1270 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001271 if (mShowVoicemailPreference) {
Tyler Gunnbaee2952014-09-10 16:01:02 -07001272 if (DBG) {
1273 log("ACTION_ADD_VOICEMAIL Intent is thrown. current VM data size: "
1274 + mVMProvidersData.size());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001275 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001276 if (mVMProvidersData.size() > 1) {
1277 simulatePreferenceClick(mVoicemailProviders);
1278 } else {
1279 onPreferenceChange(mVoicemailProviders, DEFAULT_VM_PROVIDER_KEY);
1280 mVoicemailProviders.setValue(DEFAULT_VM_PROVIDER_KEY);
1281 }
1282 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001283 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001284
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001285 updateVoiceNumberField();
1286 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001287
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001288 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
1289 mPhone.getContext());
1290 if (migrateVoicemailVibrationSettingsIfNeeded(prefs)) {
1291 mVoicemailNotificationVibrate.setChecked(prefs.getBoolean(
1292 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, false));
1293 }
1294
Andrew Lee312e8172014-10-23 17:01:36 -07001295 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1296 boolean currentValue =
1297 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1298 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1299 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001300 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1301 } else {
1302 prefSet.removePreference(mEnableVideoCalling);
1303 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001304 }
1305
1306 // Migrate settings from BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY to
1307 // BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, if the latter does not exist.
1308 // Returns true if migration was performed.
1309 public static boolean migrateVoicemailVibrationSettingsIfNeeded(SharedPreferences prefs) {
1310 if (!prefs.contains(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY)) {
1311 String vibrateWhen = prefs.getString(
1312 BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY, VOICEMAIL_VIBRATION_NEVER);
1313 // If vibrateWhen is always, then voicemailVibrate should be True.
1314 // otherwise if vibrateWhen is "only in silent mode", or "never", then
1315 // voicemailVibrate = False.
1316 boolean voicemailVibrate = vibrateWhen.equals(VOICEMAIL_VIBRATION_ALWAYS);
1317 final SharedPreferences.Editor editor = prefs.edit();
1318 editor.putBoolean(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, voicemailVibrate);
1319 editor.commit();
1320 return true;
1321 }
1322 return false;
1323 }
1324
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001325 private void handleTTYChange(Preference preference, Object objValue) {
1326 int buttonTtyMode;
1327 buttonTtyMode = Integer.valueOf((String) objValue).intValue();
1328 int settingsTtyMode = android.provider.Settings.Secure.getInt(
1329 getContentResolver(),
Sailesh Nepalbf900542014-07-15 16:18:32 -07001330 android.provider.Settings.Secure.PREFERRED_TTY_MODE,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001331 TelecomManager.TTY_MODE_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001332 if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
1333 Integer.toString(buttonTtyMode));
1334
1335 if (buttonTtyMode != settingsTtyMode) {
1336 switch(buttonTtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001337 case TelecomManager.TTY_MODE_OFF:
1338 case TelecomManager.TTY_MODE_FULL:
1339 case TelecomManager.TTY_MODE_HCO:
1340 case TelecomManager.TTY_MODE_VCO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001341 android.provider.Settings.Secure.putInt(getContentResolver(),
1342 android.provider.Settings.Secure.PREFERRED_TTY_MODE, buttonTtyMode);
1343 break;
1344 default:
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001345 buttonTtyMode = TelecomManager.TTY_MODE_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001346 }
1347
1348 mButtonTTY.setValue(Integer.toString(buttonTtyMode));
1349 updatePreferredTtyModeSummary(buttonTtyMode);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001350 Intent ttyModeChanged = new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
1351 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001352 sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
1353 }
1354 }
1355
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001356 private void updatePreferredTtyModeSummary(int TtyMode) {
1357 String [] txts = getResources().getStringArray(R.array.tty_mode_entries);
1358 switch(TtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001359 case TelecomManager.TTY_MODE_OFF:
1360 case TelecomManager.TTY_MODE_HCO:
1361 case TelecomManager.TTY_MODE_VCO:
1362 case TelecomManager.TTY_MODE_FULL:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001363 mButtonTTY.setSummary(txts[TtyMode]);
1364 break;
1365 default:
1366 mButtonTTY.setEnabled(false);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001367 mButtonTTY.setSummary(txts[TelecomManager.TTY_MODE_OFF]);
Sailesh Nepalbf900542014-07-15 16:18:32 -07001368 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001369 }
1370 }
1371
1372 private static void log(String msg) {
1373 Log.d(LOG_TAG, msg);
1374 }
1375
1376 /**
1377 * Updates the look of the VM preference widgets based on current VM provider settings.
1378 * Note that the provider name is loaded form the found activity via loadLabel in
1379 * {@link #initVoiceMailProviders()} in order for it to be localizable.
1380 */
1381 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1382 final String key = currentProviderSetting;
1383 final VoiceMailProvider provider = mVMProvidersData.get(key);
1384
1385 /* This is the case when we are coming up on a freshly wiped phone and there is no
1386 persisted value for the list preference mVoicemailProviders.
1387 In this case we want to show the UI asking the user to select a voicemail provider as
1388 opposed to silently falling back to default one. */
1389 if (provider == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001390 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> null.");
1391
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001392 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1393 mVoicemailSettings.setEnabled(false);
1394 mVoicemailSettings.setIntent(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001395 mVoicemailNotificationVibrate.setEnabled(false);
1396 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001397 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> " + provider.toString());
1398
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001399 final String providerName = provider.name;
1400 mVoicemailProviders.setSummary(providerName);
1401 mVoicemailSettings.setEnabled(true);
1402 mVoicemailSettings.setIntent(provider.intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001403 mVoicemailNotificationVibrate.setEnabled(true);
1404 }
1405 }
1406
1407 /**
1408 * Enumerates existing VM providers and puts their data into the list and populates
1409 * the preference list objects with their names.
1410 * In case we are called with ACTION_ADD_VOICEMAIL intent the intent may have
1411 * an extra string called IGNORE_PROVIDER_EXTRA with "package.activityName" of the provider
1412 * which should be hidden when we bring up the list of possible VM providers to choose.
1413 */
1414 private void initVoiceMailProviders() {
1415 if (DBG) log("initVoiceMailProviders()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001416
1417 String providerToIgnore = null;
Andrew Leef1776d82014-11-04 14:45:02 -08001418 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)
1419 && getIntent().hasExtra(IGNORE_PROVIDER_EXTRA)) {
1420 providerToIgnore = getIntent().getStringExtra(IGNORE_PROVIDER_EXTRA);
1421 // Remove this provider from the list.
1422 if (!TextUtils.isEmpty(providerToIgnore)) {
1423 if (DBG) log("Found ACTION_ADD_VOICEMAIL. providerToIgnore= " + providerToIgnore);
Andrew Lee88b51e22014-10-29 15:48:51 -07001424 mVmProviderSettingsUtil.delete(providerToIgnore);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001425 }
1426 }
1427
1428 mVMProvidersData.clear();
1429
Andrew Leef1776d82014-11-04 14:45:02 -08001430 List<String> entries = new ArrayList<String>();
1431 List<String> values = new ArrayList<String>();
1432
1433 // Add default voicemail provider.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001434 final String myCarrier = getString(R.string.voicemail_default);
1435 mVMProvidersData.put(DEFAULT_VM_PROVIDER_KEY, new VoiceMailProvider(myCarrier, null));
Andrew Leef1776d82014-11-04 14:45:02 -08001436 entries.add(myCarrier);
1437 values.add(DEFAULT_VM_PROVIDER_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001438
Andrew Leef1776d82014-11-04 14:45:02 -08001439 // Add other voicemail providers.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001440 PackageManager pm = getPackageManager();
Andrew Leef1776d82014-11-04 14:45:02 -08001441 Intent intent = new Intent(ACTION_CONFIGURE_VOICEMAIL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001442 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001443 for (int i = 0; i < resolveInfos.size(); i++) {
1444 final ResolveInfo ri= resolveInfos.get(i);
1445 final ActivityInfo currentActivityInfo = ri.activityInfo;
Andrew Lee6214e2b2014-11-04 13:57:38 -08001446 final String key = currentActivityInfo.name;
Andrew Leef1776d82014-11-04 14:45:02 -08001447
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001448 if (key.equals(providerToIgnore)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001449 continue;
1450 }
Andrew Leef1776d82014-11-04 14:45:02 -08001451
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001452 if (DBG) log("Loading key: " + key);
1453 final String nameForDisplay = ri.loadLabel(pm).toString();
1454 Intent providerIntent = new Intent();
1455 providerIntent.setAction(ACTION_CONFIGURE_VOICEMAIL);
Andrew Leef1776d82014-11-04 14:45:02 -08001456 providerIntent.setClassName(currentActivityInfo.packageName, currentActivityInfo.name);
1457 VoiceMailProvider vmProvider = new VoiceMailProvider(nameForDisplay, providerIntent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001458
Andrew Leef1776d82014-11-04 14:45:02 -08001459 if (DBG) log("Store VoiceMailProvider. Key: " + key + " -> " + vmProvider.toString());
1460 mVMProvidersData.put(key, vmProvider);
1461 entries.add(vmProvider.name);
1462 values.add(key);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001463 }
1464
Andrew Leef1776d82014-11-04 14:45:02 -08001465 mVoicemailProviders.setEntries(entries.toArray(new String[0]));
1466 mVoicemailProviders.setEntryValues(values.toArray(new String[0]));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001467
Andrew Leef1776d82014-11-04 14:45:02 -08001468 // Remember the current Voicemail Provider key as a "previous" key. This will be used when
1469 // we fail to update Voicemail Provider, which requires rollback. We will update this when
1470 // the VM Provider setting is successfully updated.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001471 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1472 if (DBG) log("Set up the first mPreviousVMProviderKey: " + mPreviousVMProviderKey);
1473
1474 // Finally update the preference texts.
1475 updateVMPreferenceWidgets(mPreviousVMProviderKey);
1476 }
1477
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001478 /**
1479 * Simulates user clicking on a passed preference.
1480 * Usually needed when the preference is a dialog preference and we want to invoke
1481 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001482 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001483 */
1484 private void simulatePreferenceClick(Preference preference) {
1485 // Go through settings until we find our setting
1486 // and then simulate a click on it to bring up the dialog
1487 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1488 for (int idx = 0; idx < adapter.getCount(); idx++) {
1489 if (adapter.getItem(idx) == preference) {
1490 getPreferenceScreen().onItemClick(this.getListView(),
1491 null, idx, adapter.getItemId(idx));
1492 break;
1493 }
1494 }
1495 }
1496
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001497 private String getCurrentVoicemailProviderKey() {
1498 final String key = mVoicemailProviders.getValue();
1499 return (key != null) ? key : DEFAULT_VM_PROVIDER_KEY;
1500 }
1501
1502 @Override
1503 public boolean onOptionsItemSelected(MenuItem item) {
1504 final int itemId = item.getItemId();
1505 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001506 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001507 return true;
1508 }
1509 return super.onOptionsItemSelected(item);
1510 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001511 /**
1512 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1513 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1514 */
1515 public static void goUpToTopLevelSetting(Activity activity) {
1516 Intent intent = new Intent(activity, CallFeaturesSetting.class);
1517 intent.setAction(Intent.ACTION_MAIN);
1518 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1519 activity.startActivity(intent);
1520 activity.finish();
1521 }
1522}