blob: 06c61083c950cb3840aeec7abd0b9d53ff6f6746 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
19import android.app.ActionBar;
20import android.app.Activity;
Evan Charlton1c696832014-04-15 14:24:23 -070021import android.app.ActivityOptions;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.AlertDialog;
23import android.app.Dialog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.ContentResolver;
25import android.content.Context;
26import android.content.DialogInterface;
27import android.content.Intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.content.pm.ActivityInfo;
29import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
31import android.database.Cursor;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070032import android.media.AudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070033import android.os.AsyncResult;
34import android.os.Bundle;
35import android.os.Handler;
36import android.os.Message;
37import android.os.UserHandle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.preference.CheckBoxPreference;
39import android.preference.ListPreference;
40import android.preference.Preference;
41import android.preference.PreferenceActivity;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.preference.PreferenceScreen;
43import android.provider.ContactsContract.CommonDataKinds;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070045import android.telecom.PhoneAccountHandle;
46import android.telecom.TelecomManager;
Andrew Lee93c345f2014-10-27 15:25:07 -070047import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.text.TextUtils;
49import android.util.Log;
50import android.view.MenuItem;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.widget.ListAdapter;
Jay Shraunerc2684732014-11-12 12:10:37 -080052import android.widget.Toast;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053
Andrew Lee312e8172014-10-23 17:01:36 -070054import com.android.ims.ImsManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import com.android.internal.telephony.CallForwardInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import com.android.internal.telephony.Phone;
57import com.android.internal.telephony.PhoneConstants;
Andrew Lee2170a972014-08-13 18:13:01 -070058import com.android.phone.common.util.SettingsUtil;
Andrew Leedb2fe562014-09-03 15:40:43 -070059import com.android.phone.settings.AccountSelectionPreference;
Andrew Lee1af6cf72014-11-04 17:35:26 -080060import com.android.phone.settings.CallForwardInfoUtil;
Andrew Lee84024342014-11-06 23:37:09 -080061import com.android.phone.settings.VoicemailDialogUtil;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -080062import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Andrew Leeb490d732014-10-27 15:00:41 -070063import com.android.phone.settings.VoicemailProviderSettings;
Andrew Lee88b51e22014-10-29 15:48:51 -070064import com.android.phone.settings.VoicemailProviderSettingsUtil;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -080065import com.android.phone.settings.VoicemailRingtonePreference;
Andrew Lee5ed870c2014-10-29 11:47:49 -070066import com.android.phone.settings.fdn.FdnSetting;
Sailesh Nepal788959e2014-07-08 23:36:40 -070067import com.android.services.telephony.sip.SipUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068
Andrew Lee2170a972014-08-13 18:13:01 -070069import java.lang.String;
Andrew Leef1776d82014-11-04 14:45:02 -080070import java.util.ArrayList;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import java.util.Collection;
72import java.util.HashMap;
73import java.util.HashSet;
74import java.util.Iterator;
75import java.util.List;
76import java.util.Map;
77
78/**
79 * Top level "Call settings" UI; see res/xml/call_feature_setting.xml
80 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070081 * This preference screen is the root of the "Call settings" hierarchy available from the Phone
82 * app; the settings here let you control various features related to phone calls (including
83 * voicemail settings, the "Respond via SMS" feature, and others.) It's used only on
84 * voice-capable phone devices.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070085 *
86 * Note that this activity is part of the package com.android.phone, even
87 * though you reach it from the "Phone" app (i.e. DialtactsActivity) which
88 * is from the package com.android.contacts.
89 *
90 * For the "Mobile network settings" screen under the main Settings app,
91 * See {@link MobileNetworkSettings}.
92 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070093 * TODO: Settings should be split into PreferenceFragments where possible (ie. voicemail).
Andrew Lee2170a972014-08-13 18:13:01 -070094 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095 * @see com.android.phone.MobileNetworkSettings
96 */
97public class CallFeaturesSetting extends PreferenceActivity
98 implements DialogInterface.OnClickListener,
Andrew Lee2170a972014-08-13 18:13:01 -070099 Preference.OnPreferenceChangeListener,
Andrew Lee2170a972014-08-13 18:13:01 -0700100 EditPhoneNumberPreference.OnDialogClosedListener,
Andrew Leece8ae2a2014-09-10 10:41:48 -0700101 EditPhoneNumberPreference.GetDefaultNumberListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102 private static final String LOG_TAG = "CallFeaturesSetting";
103 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
Andrew Lee77527ac2014-10-21 16:57:39 -0700104 // STOPSHIP if true. Flag to override behavior default behavior to hide VT setting.
105 private static final boolean ENABLE_VT_FLAG = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700106
107 /**
108 * Intent action to bring up Voicemail Provider settings.
109 *
110 * @see #IGNORE_PROVIDER_EXTRA
111 */
112 public static final String ACTION_ADD_VOICEMAIL =
113 "com.android.phone.CallFeaturesSetting.ADD_VOICEMAIL";
114 // intent action sent by this activity to a voice mail provider
115 // to trigger its configuration UI
116 public static final String ACTION_CONFIGURE_VOICEMAIL =
117 "com.android.phone.CallFeaturesSetting.CONFIGURE_VOICEMAIL";
118 // Extra put in the return from VM provider config containing voicemail number to set
119 public static final String VM_NUMBER_EXTRA = "com.android.phone.VoicemailNumber";
120 // Extra put in the return from VM provider config containing call forwarding number to set
121 public static final String FWD_NUMBER_EXTRA = "com.android.phone.ForwardingNumber";
122 // Extra put in the return from VM provider config containing call forwarding number to set
123 public static final String FWD_NUMBER_TIME_EXTRA = "com.android.phone.ForwardingNumberTime";
124 // If the VM provider returns non null value in this extra we will force the user to
125 // choose another VM provider
126 public static final String SIGNOUT_EXTRA = "com.android.phone.Signout";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128 // Key identifying the default vocie mail provider
129 public static final String DEFAULT_VM_PROVIDER_KEY = "";
130
131 /**
132 * String Extra put into ACTION_ADD_VOICEMAIL call to indicate which provider should be hidden
133 * in the list of providers presented to the user. This allows a provider which is being
134 * disabled (e.g. GV user logging out) to force the user to pick some other provider.
135 */
136 public static final String IGNORE_PROVIDER_EXTRA = "com.android.phone.ProviderToIgnore";
137
138 // string constants
139 private static final String NUM_PROJECTION[] = {CommonDataKinds.Phone.NUMBER};
140
141 // String keys for preference lookup
142 // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
Andrew Lee8d66d812014-11-24 14:54:02 -0800143 // TODO: Consider moving these strings to strings.xml, so that they are not duplicated here and
144 // in the layout files. These strings need to be treated carefully; if the setting is
145 // persistent, they are used as the key to store shared preferences and the name should not be
146 // changed unless the settings are also migrated.
Andrew Lee97708a42014-09-25 12:39:07 -0700147 private static final String VOICEMAIL_SETTING_SCREEN_PREF_KEY = "button_voicemail_category_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700148 private static final String BUTTON_VOICEMAIL_KEY = "button_voicemail_key";
149 private static final String BUTTON_VOICEMAIL_PROVIDER_KEY = "button_voicemail_provider_key";
150 private static final String BUTTON_VOICEMAIL_SETTING_KEY = "button_voicemail_setting_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700151 private static final String BUTTON_FDN_KEY = "button_fdn_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700152
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700153 private static final String BUTTON_DTMF_KEY = "button_dtmf_settings";
154 private static final String BUTTON_RETRY_KEY = "button_auto_retry_key";
155 private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
156 private static final String BUTTON_HAC_KEY = "button_hac_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700157
158 private static final String BUTTON_GSM_UMTS_OPTIONS = "button_gsm_more_expand_key";
159 private static final String BUTTON_CDMA_OPTIONS = "button_cdma_more_expand_key";
Andrew Lee2b36ba22014-11-05 17:08:49 -0800160 private static final String CALL_FORWARDING_KEY = "call_forwarding_key";
161 private static final String ADDITIONAL_GSM_SETTINGS_KEY = "additional_gsm_call_settings_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700162
Andrew Leece8ae2a2014-09-10 10:41:48 -0700163 private static final String PHONE_ACCOUNT_SETTINGS_KEY =
164 "phone_account_settings_preference_screen";
Andrew Leedb2fe562014-09-03 15:40:43 -0700165
Andrew Leedf14ead2014-10-17 14:22:52 -0700166 private static final String ENABLE_VIDEO_CALLING_KEY = "button_enable_video_calling";
167
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168 /** Event for Async voicemail change call */
169 private static final int EVENT_VOICEMAIL_CHANGED = 500;
170 private static final int EVENT_FORWARDING_CHANGED = 501;
171 private static final int EVENT_FORWARDING_GET_COMPLETED = 502;
172
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700173 public static final String HAC_KEY = "HACSetting";
174 public static final String HAC_VAL_ON = "ON";
175 public static final String HAC_VAL_OFF = "OFF";
176
177 /** Handle to voicemail pref */
178 private static final int VOICEMAIL_PREF_ID = 1;
179 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
180
181 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700182 private AudioManager mAudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700183
Andrew Lee5ed870c2014-10-29 11:47:49 -0700184 private SubscriptionInfoHelper mSubscriptionInfoHelper;
185
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700186 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
187
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188 /** Whether dialpad plays DTMF tone or not. */
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189 private CheckBoxPreference mButtonAutoRetry;
190 private CheckBoxPreference mButtonHAC;
191 private ListPreference mButtonDTMF;
192 private ListPreference mButtonTTY;
Andrew Leece8ae2a2014-09-10 10:41:48 -0700193 private Preference mPhoneAccountSettingsPreference;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700194 private ListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700195 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700196 private PreferenceScreen mVoicemailSettings;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800197 private VoicemailRingtonePreference mVoicemailNotificationRingtone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700199 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200
201 private class VoiceMailProvider {
Andrew Leef1776d82014-11-04 14:45:02 -0800202 public String name;
203 public Intent intent;
204
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700205 public VoiceMailProvider(String name, Intent intent) {
206 this.name = name;
207 this.intent = intent;
208 }
Andrew Leef1776d82014-11-04 14:45:02 -0800209
210 public String toString() {
211 return "[ Name: " + name + ", Intent: " + intent + " ]";
212 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700213 }
214
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700215 /**
216 * Results of reading forwarding settings
217 */
218 private CallForwardInfo[] mForwardingReadResults = null;
219
220 /**
221 * Result of forwarding number change.
222 * Keys are reasons (eg. unconditional forwarding).
223 */
224 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
225
226 /**
227 * Expected CF read result types.
228 * This set keeps track of the CF types for which we've issued change
229 * commands so we can tell when we've received all of the responses.
230 */
231 private Collection<Integer> mExpectedChangeResultReasons = null;
232
233 /**
234 * Result of vm number change
235 */
236 private AsyncResult mVoicemailChangeResult = null;
237
238 /**
239 * Previous VM provider setting so we can return to it in case of failure.
240 */
241 private String mPreviousVMProviderKey = null;
242
243 /**
244 * Id of the dialog being currently shown.
245 */
246 private int mCurrentDialogId = 0;
247
248 /**
249 * Flag indicating that we are invoking settings for the voicemail provider programmatically
250 * due to vm provider change.
251 */
252 private boolean mVMProviderSettingsForced = false;
253
254 /**
255 * Flag indicating that we are making changes to vm or fwd numbers
256 * due to vm provider change.
257 */
258 private boolean mChangingVMorFwdDueToProviderChange = false;
259
260 /**
261 * True if we are in the process of vm & fwd number change and vm has already been changed.
262 * This is used to decide what to do in case of rollback.
263 */
264 private boolean mVMChangeCompletedSuccessfully = false;
265
266 /**
267 * True if we had full or partial failure setting forwarding numbers and so need to roll them
268 * back.
269 */
270 private boolean mFwdChangesRequireRollback = false;
271
272 /**
273 * Id of error msg to display to user once we are done reverting the VM provider to the previous
274 * one.
275 */
276 private int mVMOrFwdSetError = 0;
277
278 /**
279 * Data about discovered voice mail settings providers.
280 * Is populated by querying which activities can handle ACTION_CONFIGURE_VOICEMAIL.
281 * They key in this map is package name + activity name.
282 * We always add an entry for the default provider with a key of empty
283 * string and intent value of null.
284 * @see #initVoiceMailProviders()
285 */
286 private final Map<String, VoiceMailProvider> mVMProvidersData =
287 new HashMap<String, VoiceMailProvider>();
288
289 /** string to hold old voicemail number as it is being updated. */
290 private String mOldVmNumber;
291
292 // New call forwarding settings and vm number we will be setting
293 // Need to save these since before we get to saving we need to asynchronously
294 // query the existing forwarding settings.
295 private CallForwardInfo[] mNewFwdSettings;
296 private String mNewVMNumber;
297
298 private boolean mForeground;
299
300 @Override
301 public void onPause() {
302 super.onPause();
303 mForeground = false;
304 }
305
306 /**
307 * We have to pull current settings from the network for all kinds of
308 * voicemail providers so we can tell whether we have to update them,
309 * so use this bit to keep track of whether we're reading settings for the
310 * default provider and should therefore save them out when done.
311 */
312 private boolean mReadingSettingsForDefaultProvider = false;
313
Tyler Gunnbaee2952014-09-10 16:01:02 -0700314 /**
315 * Used to indicate that the voicemail preference should be shown.
316 */
317 private boolean mShowVoicemailPreference = false;
318
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700319 /*
320 * Click Listeners, handle click based on objects attached to UI.
321 */
322
323 // Click listener for all toggle events
324 @Override
325 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
326 if (preference == mSubMenuVoicemailSettings) {
327 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700328 } else if (preference == mButtonDTMF) {
329 return true;
330 } else if (preference == mButtonTTY) {
331 return true;
332 } else if (preference == mButtonAutoRetry) {
333 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
334 android.provider.Settings.Global.CALL_AUTO_RETRY,
335 mButtonAutoRetry.isChecked() ? 1 : 0);
336 return true;
337 } else if (preference == mButtonHAC) {
338 int hac = mButtonHAC.isChecked() ? 1 : 0;
339 // Update HAC value in Settings database
340 Settings.System.putInt(mPhone.getContext().getContentResolver(),
341 Settings.System.HEARING_AID, hac);
342
343 // Update HAC Value in AudioManager
344 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
345 return true;
Andrew Lee696bff52014-12-03 17:58:18 -0800346 } else if (preference.getKey().equals(mVoicemailSettings.getKey())) {
347 // Check key instead of comparing reference because closing the voicemail notification
348 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
349 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800350 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
351
Andrew Lee696bff52014-12-03 17:58:18 -0800352 final Dialog dialog = ((PreferenceScreen) preference).getDialog();
Yorke Leea0f63bf2014-10-09 18:27:20 -0700353 if (dialog != null) {
354 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
355 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800356
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700357 if (preference.getIntent() != null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800358 if (DBG) log("Invoking cfg intent " + preference.getIntent().getPackage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700359
360 // onActivityResult() will be responsible for resetting some of variables.
361 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
362 return true;
363 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800364 if (DBG) log("onPreferenceTreeClick(). No intent; use default behavior in xml.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700365
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800366 // onActivityResult() will not be called, so reset variables here.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700367 mPreviousVMProviderKey = DEFAULT_VM_PROVIDER_KEY;
368 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700369 return false;
370 }
Andrew Lee97708a42014-09-25 12:39:07 -0700371 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700372 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
373 if (dialog != null) {
374 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
375 }
Andrew Lee97708a42014-09-25 12:39:07 -0700376 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700377 }
378 return false;
379 }
380
381 /**
382 * Implemented to support onPreferenceChangeListener to look for preference
383 * changes.
384 *
385 * @param preference is the preference to be changed
386 * @param objValue should be the value of the selection, NOT its localized
387 * display value.
388 */
389 @Override
390 public boolean onPreferenceChange(Preference preference, Object objValue) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800391 if (DBG) log("onPreferenceChange: \"" + preference + "\" changed to \"" + objValue + "\"");
Andrew Lee2170a972014-08-13 18:13:01 -0700392
393 if (preference == mButtonDTMF) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700394 int index = mButtonDTMF.findIndexOfValue((String) objValue);
395 Settings.System.putInt(mPhone.getContext().getContentResolver(),
396 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
397 } else if (preference == mButtonTTY) {
398 handleTTYChange(preference, objValue);
399 } else if (preference == mVoicemailProviders) {
400 final String newProviderKey = (String) objValue;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800401
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700402 // If previous provider key and the new one is same, we don't need to handle it.
403 if (mPreviousVMProviderKey.equals(newProviderKey)) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800404 if (DBG) log("No change is made to the VM provider setting.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700405 return true;
406 }
407 updateVMPreferenceWidgets(newProviderKey);
408
Andrew Leeb490d732014-10-27 15:00:41 -0700409 final VoicemailProviderSettings newProviderSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800410 VoicemailProviderSettingsUtil.load(this, newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700411
Andrew Lee88b51e22014-10-29 15:48:51 -0700412 // If the user switches to a voice mail provider and we have numbers stored for it we
413 // will automatically change the phone's voice mail and forwarding number to the stored
414 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700415 if (newProviderSettings == null) {
416 // Force the user into a configuration of the chosen provider
417 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
418 mVMProviderSettingsForced = true;
419 simulatePreferenceClick(mVoicemailSettings);
420 } else {
421 if (DBG) log("Saved preferences found - switching to them");
422 // Set this flag so if we get a failure we revert to previous provider
423 mChangingVMorFwdDueToProviderChange = true;
424 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
425 }
Andrew Lee8d66d812014-11-24 14:54:02 -0800426 } else if (preference.getKey().equals(mVoicemailNotificationVibrate.getKey())) {
Andrew Lee696bff52014-12-03 17:58:18 -0800427 // Check key instead of comparing reference because closing the voicemail notification
428 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
429 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee8d66d812014-11-24 14:54:02 -0800430 VoicemailNotificationSettingsUtil.setVibrationEnabled(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800431 mPhone, Boolean.TRUE.equals(objValue));
Andrew Leedf14ead2014-10-17 14:22:52 -0700432 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700433 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
434 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
435 } else {
436 AlertDialog.Builder builder = new AlertDialog.Builder(this);
437 DialogInterface.OnClickListener networkSettingsClickListener =
438 new Dialog.OnClickListener() {
439 @Override
440 public void onClick(DialogInterface dialog, int which) {
441 startActivity(new Intent(mPhone.getContext(),
442 com.android.phone.MobileNetworkSettings.class));
443 }
444 };
445 builder.setMessage(getResources().getString(
446 R.string.enable_video_calling_dialog_msg))
447 .setNeutralButton(getResources().getString(
448 R.string.enable_video_calling_dialog_settings),
449 networkSettingsClickListener)
450 .setPositiveButton(android.R.string.ok, null)
451 .show();
452 return false;
453 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700454 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800455
456 // Always let the preference setting proceed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700457 return true;
458 }
459
460 @Override
461 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800462 if (DBG) log("onDialogClosed: Button clicked is " + buttonClicked);
463
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700464 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
465 return;
466 }
467
468 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700469 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
470 mSubMenuVoicemailSettings.getPhoneNumber(),
471 VoicemailProviderSettings.NO_FORWARDING);
472 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700473 }
474 }
475
476 /**
477 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
478 * This method set the default values for the various
479 * EditPhoneNumberPreference dialogs.
480 */
481 @Override
482 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
483 if (preference == mSubMenuVoicemailSettings) {
484 // update the voicemail number field, which takes care of the
485 // mSubMenuVoicemailSettings itself, so we should return null.
486 if (DBG) log("updating default for voicemail dialog");
487 updateVoiceNumberField();
488 return null;
489 }
490
491 String vmDisplay = mPhone.getVoiceMailNumber();
492 if (TextUtils.isEmpty(vmDisplay)) {
493 // if there is no voicemail number, we just return null to
494 // indicate no contribution.
495 return null;
496 }
497
498 // Return the voicemail number prepended with "VM: "
499 if (DBG) log("updating default for call forwarding dialogs");
500 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
501 }
502
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700503 private void switchToPreviousVoicemailProvider() {
504 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700505
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800506 if (mPreviousVMProviderKey == null) {
507 return;
508 }
509
510 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
Andrew Lee84024342014-11-06 23:37:09 -0800511 showDialogIfForeground(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800512 final VoicemailProviderSettings prevSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800513 VoicemailProviderSettingsUtil.load(this, mPreviousVMProviderKey);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800514 if (prevSettings == null) {
515 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
516 + mPreviousVMProviderKey + "\" is null but should be loaded.");
517 }
518
519 if (mVMChangeCompletedSuccessfully) {
520 mNewVMNumber = prevSettings.getVoicemailNumber();
521 Log.i(LOG_TAG, "VM change is already completed successfully."
522 + "Have to revert VM back to " + mNewVMNumber + " again.");
523 mPhone.setVoiceMailNumber(
524 mPhone.getVoiceMailAlphaTag().toString(),
525 mNewVMNumber,
526 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
527 }
528
529 if (mFwdChangesRequireRollback) {
530 Log.i(LOG_TAG, "Requested to rollback forwarding changes.");
531
532 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
533 if (prevFwdSettings != null) {
534 Map<Integer, AsyncResult> results = mForwardingChangeResults;
535 resetForwardingChangeState();
536 for (int i = 0; i < prevFwdSettings.length; i++) {
537 CallForwardInfo fi = prevFwdSettings[i];
538 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
539 // Only revert the settings for which the update succeeded.
540 AsyncResult result = results.get(fi.reason);
541 if (result != null && result.exception == null) {
542 mExpectedChangeResultReasons.add(fi.reason);
543 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
544 mRevertOptionComplete.obtainMessage(
545 EVENT_FORWARDING_CHANGED, i, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700546 }
547 }
548 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700549 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800550 } else {
551 if (DBG) log("No need to revert");
552 onRevertDone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700553 }
554 }
555
556 private void onRevertDone() {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800557 if (DBG) log("onRevertDone: Changing provider key back to " + mPreviousVMProviderKey);
558
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700559 updateVMPreferenceWidgets(mPreviousVMProviderKey);
560 updateVoiceNumberField();
561 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800562 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700563 mVMOrFwdSetError = 0;
564 }
565 }
566
567 @Override
568 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
569 if (DBG) {
570 log("onActivityResult: requestCode: " + requestCode
571 + ", resultCode: " + resultCode
572 + ", data: " + data);
573 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800574
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700575 // there are cases where the contact picker may end up sending us more than one
576 // request. We want to ignore the request if we're not in the correct state.
577 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
578 boolean failure = false;
579
580 // No matter how the processing of result goes lets clear the flag
581 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
582 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
583 mVMProviderSettingsForced = false;
584
585 String vmNum = null;
586 if (resultCode != RESULT_OK) {
587 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
588 failure = true;
589 } else {
590 if (data == null) {
591 if (DBG) log("onActivityResult: vm provider cfg result has no data");
592 failure = true;
593 } else {
594 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
595 if (DBG) log("Provider requested signout");
596 if (isVMProviderSettingsForced) {
597 if (DBG) log("Going back to previous provider on signout");
598 switchToPreviousVoicemailProvider();
599 } else {
600 final String victim = getCurrentVoicemailProviderKey();
601 if (DBG) log("Relaunching activity and ignoring " + victim);
602 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
603 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
604 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
605 this.startActivity(i);
606 }
607 return;
608 }
609 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
610 if (vmNum == null || vmNum.length() == 0) {
611 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
612 failure = true;
613 }
614 }
615 }
616 if (failure) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800617 if (DBG) log("Failure in return from voicemail provider.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700618 if (isVMProviderSettingsForced) {
619 switchToPreviousVoicemailProvider();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700620 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800621
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700622 return;
623 }
624 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
625 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
626
Santos Cordonda120f42014-08-06 04:44:34 -0700627 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700628 // send it to the provider when it's config is invoked so it can use this as default
629 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
630
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800631 if (DBG) log("onActivityResult: cfg result has forwarding number " + fwdNum);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700632 saveVoiceMailAndForwardingNumber(getCurrentVoicemailProviderKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700633 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700634 return;
635 }
636
637 if (requestCode == VOICEMAIL_PREF_ID) {
638 if (resultCode != RESULT_OK) {
639 if (DBG) log("onActivityResult: contact picker result not OK.");
640 return;
641 }
642
643 Cursor cursor = null;
644 try {
645 cursor = getContentResolver().query(data.getData(),
646 NUM_PROJECTION, null, null, null);
647 if ((cursor == null) || (!cursor.moveToFirst())) {
648 if (DBG) log("onActivityResult: bad contact data, no results found.");
649 return;
650 }
651 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
652 return;
653 } finally {
654 if (cursor != null) {
655 cursor.close();
656 }
657 }
658 }
659
660 super.onActivityResult(requestCode, resultCode, data);
661 }
662
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700663 /**
664 * Wrapper around showDialog() that will silently do nothing if we're
665 * not in the foreground.
666 *
667 * This is useful here because most of the dialogs we display from
668 * this class are triggered by asynchronous events (like
669 * success/failure messages from the telephony layer) and it's
670 * possible for those events to come in even after the user has gone
671 * to a different screen.
672 */
673 // TODO: this is too brittle: it's still easy to accidentally add new
674 // code here that calls showDialog() directly (which will result in a
675 // WindowManager$BadTokenException if called after the activity has
676 // been stopped.)
677 //
678 // It would be cleaner to do the "if (mForeground)" check in one
679 // central place, maybe by using a single Handler for all asynchronous
680 // events (and have *that* discard events if we're not in the
681 // foreground.)
682 //
683 // Unfortunately it's not that simple, since we sometimes need to do
684 // actual work to handle these events whether or not we're in the
685 // foreground (see the Handler code in mSetOptionComplete for
686 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800687 //
688 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
689 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700690 private void showDialogIfForeground(int id) {
691 if (mForeground) {
692 showDialog(id);
693 }
694 }
695
696 private void dismissDialogSafely(int id) {
697 try {
698 dismissDialog(id);
699 } catch (IllegalArgumentException e) {
700 // This is expected in the case where we were in the background
701 // at the time we would normally have shown the dialog, so we didn't
702 // show it.
703 }
704 }
705
Andrew Leeb490d732014-10-27 15:00:41 -0700706 private void saveVoiceMailAndForwardingNumber(
707 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700708 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700709 mNewVMNumber = newSettings.getVoicemailNumber();
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800710 mNewVMNumber = (mNewVMNumber == null) ? "" : mNewVMNumber;
Andrew Leeb490d732014-10-27 15:00:41 -0700711 mNewFwdSettings = newSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700712
713 // No fwd settings on CDMA
714 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
715 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700716 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700717 }
718
Andrew Leee3c15212014-10-28 13:12:55 -0700719 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700720 if (mNewVMNumber.equals(mOldVmNumber)
721 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Lee84024342014-11-06 23:37:09 -0800722 showDialogIfForeground(VoicemailDialogUtil.VM_NOCHANGE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700723 return;
724 }
725
Andrew Lee6950c6c2014-11-26 12:05:42 -0800726 VoicemailProviderSettingsUtil.save(this, key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700727 mVMChangeCompletedSuccessfully = false;
728 mFwdChangesRequireRollback = false;
729 mVMOrFwdSetError = 0;
730 if (!key.equals(mPreviousVMProviderKey)) {
731 mReadingSettingsForDefaultProvider =
732 mPreviousVMProviderKey.equals(DEFAULT_VM_PROVIDER_KEY);
733 if (DBG) log("Reading current forwarding settings");
Andrew Leeb490d732014-10-27 15:00:41 -0700734 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
735 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
736 for (int i = 0; i < mForwardingReadResults.length; i++) {
737 mPhone.getCallForwardingOption(
738 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700739 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
740 }
Andrew Lee84024342014-11-06 23:37:09 -0800741 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700742 } else {
743 saveVoiceMailAndForwardingNumberStage2();
744 }
745 }
746
747 private final Handler mGetOptionComplete = new Handler() {
748 @Override
749 public void handleMessage(Message msg) {
750 AsyncResult result = (AsyncResult) msg.obj;
751 switch (msg.what) {
752 case EVENT_FORWARDING_GET_COMPLETED:
753 handleForwardingSettingsReadResult(result, msg.arg1);
754 break;
755 }
756 }
757 };
758
759 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
760 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800761
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700762 Throwable error = null;
763 if (ar.exception != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700764 error = ar.exception;
Andrew Lee1af6cf72014-11-04 17:35:26 -0800765 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700766 }
767 if (ar.userObj instanceof Throwable) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800768 error = (Throwable) ar.userObj;
769 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700770 }
771
772 // We may have already gotten an error and decided to ignore the other results.
773 if (mForwardingReadResults == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800774 if (DBG) Log.d(LOG_TAG, "Ignoring fwd reading result: " + idx);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700775 return;
776 }
777
778 // In case of error ignore other results, show an error dialog
779 if (error != null) {
780 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
781 mForwardingReadResults = null;
Andrew Lee84024342014-11-06 23:37:09 -0800782 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
783 showDialogIfForeground(VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700784 return;
785 }
786
Andrew Lee1af6cf72014-11-04 17:35:26 -0800787 // Get the forwarding info.
788 mForwardingReadResults[idx] = CallForwardInfoUtil.getCallForwardInfo(
789 (CallForwardInfo[]) ar.result,
790 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx]);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700791
792 // Check if we got all the results already
793 boolean done = true;
794 for (int i = 0; i < mForwardingReadResults.length; i++) {
795 if (mForwardingReadResults[i] == null) {
796 done = false;
797 break;
798 }
799 }
Andrew Lee1af6cf72014-11-04 17:35:26 -0800800
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700801 if (done) {
802 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
Andrew Lee84024342014-11-06 23:37:09 -0800803 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800804
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700805 if (mReadingSettingsForDefaultProvider) {
Andrew Lee6950c6c2014-11-26 12:05:42 -0800806 VoicemailProviderSettingsUtil.save(this, DEFAULT_VM_PROVIDER_KEY,
Andrew Lee88b51e22014-10-29 15:48:51 -0700807 new VoicemailProviderSettings(this.mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700808 mReadingSettingsForDefaultProvider = false;
809 }
810 saveVoiceMailAndForwardingNumberStage2();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700811 }
812 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800813
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700814 private void resetForwardingChangeState() {
815 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
816 mExpectedChangeResultReasons = new HashSet<Integer>();
817 }
818
819 // Called after we are done saving the previous forwarding settings if
820 // we needed.
821 private void saveVoiceMailAndForwardingNumberStage2() {
822 mForwardingChangeResults = null;
823 mVoicemailChangeResult = null;
Andrew Leeb490d732014-10-27 15:00:41 -0700824 if (mNewFwdSettings != VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700825 resetForwardingChangeState();
826 for (int i = 0; i < mNewFwdSettings.length; i++) {
827 CallForwardInfo fi = mNewFwdSettings[i];
Andrew Lee1af6cf72014-11-04 17:35:26 -0800828 CallForwardInfo fiForReason =
829 CallForwardInfoUtil.infoForReason(mForwardingReadResults, fi.reason);
830 final boolean doUpdate = CallForwardInfoUtil.isUpdateRequired(fiForReason, fi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700831
832 if (doUpdate) {
833 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
834 mExpectedChangeResultReasons.add(i);
835
Andrew Lee1af6cf72014-11-04 17:35:26 -0800836 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700837 mSetOptionComplete.obtainMessage(
838 EVENT_FORWARDING_CHANGED, fi.reason, 0));
839 }
840 }
Andrew Lee84024342014-11-06 23:37:09 -0800841 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700842 } else {
843 if (DBG) log("Not touching fwd #");
844 setVMNumberWithCarrier();
845 }
846 }
847
848 private void setVMNumberWithCarrier() {
849 if (DBG) log("save voicemail #: " + mNewVMNumber);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800850
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700851 mPhone.setVoiceMailNumber(
852 mPhone.getVoiceMailAlphaTag().toString(),
853 mNewVMNumber,
854 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
855 }
856
857 /**
858 * Callback to handle option update completions
859 */
860 private final Handler mSetOptionComplete = new Handler() {
861 @Override
862 public void handleMessage(Message msg) {
863 AsyncResult result = (AsyncResult) msg.obj;
864 boolean done = false;
865 switch (msg.what) {
866 case EVENT_VOICEMAIL_CHANGED:
867 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700868 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700869 done = true;
870 break;
871 case EVENT_FORWARDING_CHANGED:
872 mForwardingChangeResults.put(msg.arg1, result);
873 if (result.exception != null) {
874 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
875 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700876 }
Andrew Leee438b312014-10-29 16:59:15 -0700877 if (isForwardingCompleted()) {
878 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700879 if (DBG) log("Overall fwd changes completed ok, starting vm change");
880 setVMNumberWithCarrier();
881 } else {
882 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
883 "Check if we need to try rollback for some settings.");
884 mFwdChangesRequireRollback = false;
885 Iterator<Map.Entry<Integer,AsyncResult>> it =
886 mForwardingChangeResults.entrySet().iterator();
887 while (it.hasNext()) {
888 Map.Entry<Integer,AsyncResult> entry = it.next();
889 if (entry.getValue().exception == null) {
890 // If at least one succeeded we have to revert
891 Log.i(LOG_TAG, "Rollback will be required");
892 mFwdChangesRequireRollback = true;
893 break;
894 }
895 }
896 if (!mFwdChangesRequireRollback) {
897 Log.i(LOG_TAG, "No rollback needed.");
898 }
899 done = true;
900 }
901 }
902 break;
903 default:
904 // TODO: should never reach this, may want to throw exception
905 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800906
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700907 if (done) {
908 if (DBG) log("All VM provider related changes done");
909 if (mForwardingChangeResults != null) {
Andrew Lee84024342014-11-06 23:37:09 -0800910 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700911 }
Andrew Leee438b312014-10-29 16:59:15 -0700912 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700913 }
914 }
915 };
916
917 /**
918 * Callback to handle option revert completions
919 */
920 private final Handler mRevertOptionComplete = new Handler() {
921 @Override
922 public void handleMessage(Message msg) {
923 AsyncResult result = (AsyncResult) msg.obj;
924 switch (msg.what) {
925 case EVENT_VOICEMAIL_CHANGED:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700926 if (DBG) log("VM revert complete msg");
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800927 mVoicemailChangeResult = result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700928 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800929
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700930 case EVENT_FORWARDING_CHANGED:
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800931 if (DBG) log("FWD revert complete msg ");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700932 mForwardingChangeResults.put(msg.arg1, result);
933 if (result.exception != null) {
934 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
935 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700936 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700937 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800938
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700939 default:
940 // TODO: should never reach this, may want to throw exception
941 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800942
943 final boolean done = (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null)
944 && (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700945 if (done) {
946 if (DBG) log("All VM reverts done");
Andrew Lee84024342014-11-06 23:37:09 -0800947 dismissDialogSafely(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700948 onRevertDone();
949 }
950 }
951 };
952
953 /**
Andrew Leee438b312014-10-29 16:59:15 -0700954 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700955 */
Andrew Leee438b312014-10-29 16:59:15 -0700956 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700957 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -0700958 return true;
959 }
960
961 for (Integer reason : mExpectedChangeResultReasons) {
962 if (mForwardingChangeResults.get(reason) == null) {
963 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700964 }
965 }
Andrew Leee438b312014-10-29 16:59:15 -0700966
967 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700968 }
Andrew Leee438b312014-10-29 16:59:15 -0700969
970 private boolean isFwdChangeSuccess() {
971 if (mForwardingChangeResults == null) {
972 return true;
973 }
974
975 for (AsyncResult result : mForwardingChangeResults.values()) {
976 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700977 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700978 String msg = exception.getMessage();
979 msg = (msg != null) ? msg : "";
980 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
981 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700982 }
983 }
Andrew Leee438b312014-10-29 16:59:15 -0700984 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700985 }
986
Andrew Leee438b312014-10-29 16:59:15 -0700987 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700988 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700989 String msg = mVoicemailChangeResult.exception.getMessage();
990 msg = (msg != null) ? msg : "";
991 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
992 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700993 }
Andrew Leee438b312014-10-29 16:59:15 -0700994 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700995 }
996
Andrew Leee438b312014-10-29 16:59:15 -0700997 private void handleSetVmOrFwdMessage() {
998 if (DBG) log("handleSetVMMessage: set VM request complete");
999
1000 if (!isFwdChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -08001001 handleVmOrFwdSetError(VoicemailDialogUtil.FWD_SET_RESPONSE_ERROR_DIALOG);
Andrew Leee438b312014-10-29 16:59:15 -07001002 } else if (!isVmChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -08001003 handleVmOrFwdSetError(VoicemailDialogUtil.VM_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001004 } else {
Andrew Lee84024342014-11-06 23:37:09 -08001005 handleVmAndFwdSetSuccess(VoicemailDialogUtil.VM_CONFIRM_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001006 }
1007 }
1008
1009 /**
1010 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
1011 * changes to those settings and show "failure" dialog.
1012 *
Andrew Leeab082272014-11-04 15:50:42 -08001013 * @param dialogId ID of the dialog to show for the specific error case. Either
Andrew Lee84024342014-11-06 23:37:09 -08001014 * {@link #FWD_SET_RESPONSE_ERROR_DIALOG} or {@link #VM_RESPONSE_ERROR_DIALOG}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001015 */
Andrew Leeab082272014-11-04 15:50:42 -08001016 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001017 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -08001018 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001019 mChangingVMorFwdDueToProviderChange = false;
1020 switchToPreviousVoicemailProvider();
1021 return;
1022 }
1023 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001024 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001025 updateVoiceNumberField();
1026 }
1027
1028 /**
1029 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1030 * This updates a bunch of variables and show "success" dialog.
1031 */
Andrew Leeab082272014-11-04 15:50:42 -08001032 private void handleVmAndFwdSetSuccess(int dialogId) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001033 if (DBG) log("handleVmAndFwdSetSuccess: key is " + getCurrentVoicemailProviderKey());
1034
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001035 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1036 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001037 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001038 updateVoiceNumberField();
1039 }
1040
1041 /**
1042 * Update the voicemail number from what we've recorded on the sim.
1043 */
1044 private void updateVoiceNumberField() {
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001045 if (DBG) log("updateVoiceNumberField()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001046
1047 mOldVmNumber = mPhone.getVoiceMailNumber();
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001048 if (TextUtils.isEmpty(mOldVmNumber)) {
1049 mSubMenuVoicemailSettings.setPhoneNumber("");
1050 mSubMenuVoicemailSettings.setSummary(getString(R.string.voicemail_number_not_set));
1051 } else {
1052 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1053 mSubMenuVoicemailSettings.setSummary(mOldVmNumber);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001054 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001055 }
1056
1057 /*
1058 * Helper Methods for Activity class.
1059 * The initial query commands are split into two pieces now
1060 * for individual expansion. This combined with the ability
1061 * to cancel queries allows for a much better user experience,
1062 * and also ensures that the user only waits to update the
1063 * data that is relevant.
1064 */
1065
1066 @Override
1067 protected void onPrepareDialog(int id, Dialog dialog) {
1068 super.onPrepareDialog(id, dialog);
1069 mCurrentDialogId = id;
1070 }
1071
1072 // dialog creation method, called by showDialog()
1073 @Override
Andrew Lee84024342014-11-06 23:37:09 -08001074 protected Dialog onCreateDialog(int dialogId) {
1075 return VoicemailDialogUtil.getDialog(this, dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001076 }
1077
1078 // This is a method implemented for DialogInterface.OnClickListener.
1079 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1080 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1081 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1082 public void onClick(DialogInterface dialog, int which) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001083 if (DBG) log("onClick: button clicked is " + which);
1084
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001085 dialog.dismiss();
1086 switch (which){
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001087 case DialogInterface.BUTTON_NEGATIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001088 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001089 // We failed to get current forwarding settings and the user
1090 // does not wish to continue.
1091 switchToPreviousVoicemailProvider();
1092 }
1093 break;
1094 case DialogInterface.BUTTON_POSITIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001095 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001096 // We failed to get current forwarding settings but the user
1097 // wishes to continue changing settings to the new vm provider
1098 saveVoiceMailAndForwardingNumberStage2();
1099 } else {
1100 finish();
1101 }
1102 return;
1103 default:
1104 // just let the dialog close and go back to the input
1105 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001106
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001107 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1108 // with settings UI. If we were called to explicitly configure voice mail then
1109 // we finish the settings activity here to come back to whatever the user was doing.
1110 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1111 finish();
1112 }
1113 }
1114
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001115 /*
1116 * Activity class methods
1117 */
1118
1119 @Override
1120 protected void onCreate(Bundle icicle) {
1121 super.onCreate(icicle);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001122 if (DBG) log("onCreate: Intent is " + getIntent());
1123
Jay Shraunerc2684732014-11-12 12:10:37 -08001124 // Make sure we are running as the primary user.
1125 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
1126 Toast.makeText(this, R.string.call_settings_primary_user_only,
1127 Toast.LENGTH_SHORT).show();
1128 finish();
1129 return;
1130 }
1131
Tyler Gunnbaee2952014-09-10 16:01:02 -07001132 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
1133
Tyler Gunnbaee2952014-09-10 16:01:02 -07001134 // Show the voicemail preference in onResume if the calling intent specifies the
1135 // ACTION_ADD_VOICEMAIL action.
1136 mShowVoicemailPreference = (icicle == null) &&
Jay Shraunerbe2fb262014-11-11 15:19:58 -08001137 TextUtils.equals(getIntent().getAction(), ACTION_ADD_VOICEMAIL);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001138
1139 mSubscriptionInfoHelper = new SubscriptionInfoHelper(getIntent());
1140 mSubscriptionInfoHelper.setActionBarTitle(
1141 getActionBar(), getResources(), R.string.call_settings_with_label);
Andrew Lee1479dd12014-11-06 23:06:32 -08001142 mPhone = mSubscriptionInfoHelper.getPhone();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001143 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001144
1145 private void initPhoneAccountPreferences() {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001146 mPhoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001147
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001148 TelecomManager telecomManager = TelecomManager.from(this);
Andrew Lee93c345f2014-10-27 15:25:07 -07001149 TelephonyManager telephonyManager =
1150 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001151
Andrew Lee93c345f2014-10-27 15:25:07 -07001152 if ((telecomManager.getSimCallManagers().isEmpty() && !SipUtil.isVoipSupported(this))
1153 || telephonyManager.getPhoneCount() > 1) {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001154 getPreferenceScreen().removePreference(mPhoneAccountSettingsPreference);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001155 }
1156 }
1157
Tyler Gunnbaee2952014-09-10 16:01:02 -07001158 @Override
1159 protected void onResume() {
1160 super.onResume();
1161 mForeground = true;
1162
1163 PreferenceScreen preferenceScreen = getPreferenceScreen();
1164 if (preferenceScreen != null) {
1165 preferenceScreen.removeAll();
1166 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001167
1168 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001169
Andrew Leedb2fe562014-09-03 15:40:43 -07001170 initPhoneAccountPreferences();
1171
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001172 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001173 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1174 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1175 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1176 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001177
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001178 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1179 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1180 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
1181 mButtonTTY = (ListPreference) findPreference(BUTTON_TTY_KEY);
Andrew Lee312e8172014-10-23 17:01:36 -07001182 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001183
Andrew Lee8d66d812014-11-24 14:54:02 -08001184 mVoicemailProviders = (ListPreference) findPreference(BUTTON_VOICEMAIL_PROVIDER_KEY);
Andrew Lee2c027892014-10-29 11:29:54 -07001185 mVoicemailProviders.setOnPreferenceChangeListener(this);
Andrew Lee8d66d812014-11-24 14:54:02 -08001186
Andrew Lee2c027892014-10-29 11:29:54 -07001187 mVoicemailSettingsScreen =
1188 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
1189 mVoicemailSettings = (PreferenceScreen)findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
Andrew Lee8d66d812014-11-24 14:54:02 -08001190
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001191 mVoicemailNotificationRingtone = (VoicemailRingtonePreference) findPreference(
1192 getResources().getString(R.string.voicemail_notification_ringtone_key));
1193 mVoicemailNotificationRingtone.init(mPhone);
1194
Andrew Lee8d66d812014-11-24 14:54:02 -08001195 mVoicemailNotificationVibrate = (CheckBoxPreference) findPreference(
1196 getResources().getString(R.string.voicemail_notification_vibrate_key));
1197 mVoicemailNotificationVibrate.setOnPreferenceChangeListener(this);
1198
Andrew Lee2c027892014-10-29 11:29:54 -07001199 initVoiceMailProviders();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001200
Andrew Lee64a7d792014-10-15 17:38:38 -07001201 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1202 mButtonDTMF.setOnPreferenceChangeListener(this);
1203 int dtmf = Settings.System.getInt(getContentResolver(),
1204 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1205 mButtonDTMF.setValueIndex(dtmf);
1206 } else {
1207 prefSet.removePreference(mButtonDTMF);
1208 mButtonDTMF = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001209 }
1210
Andrew Lee64a7d792014-10-15 17:38:38 -07001211 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1212 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1213 int autoretry = Settings.Global.getInt(
1214 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1215 mButtonAutoRetry.setChecked(autoretry != 0);
1216 } else {
1217 prefSet.removePreference(mButtonAutoRetry);
1218 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001219 }
1220
Andrew Lee64a7d792014-10-15 17:38:38 -07001221 if (getResources().getBoolean(R.bool.hac_enabled)) {
1222 mButtonHAC.setOnPreferenceChangeListener(this);
1223 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1224 mButtonHAC.setChecked(hac != 0);
1225 } else {
1226 prefSet.removePreference(mButtonHAC);
1227 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001228 }
1229
Andrew Lee64a7d792014-10-15 17:38:38 -07001230 TelecomManager telecomManager = TelecomManager.from(this);
1231 if (telecomManager != null && telecomManager.isTtySupported()) {
1232 mButtonTTY.setOnPreferenceChangeListener(this);
1233 int settingsTtyMode = Settings.Secure.getInt(getContentResolver(),
1234 Settings.Secure.PREFERRED_TTY_MODE,
1235 TelecomManager.TTY_MODE_OFF);
1236 mButtonTTY.setValue(Integer.toString(settingsTtyMode));
1237 updatePreferredTtyModeSummary(settingsTtyMode);
1238 } else {
1239 prefSet.removePreference(mButtonTTY);
1240 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001241 }
1242
1243 if (!getResources().getBoolean(R.bool.world_phone)) {
Andrew Lee2b36ba22014-11-05 17:08:49 -08001244 Preference cdmaOptions = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
1245 prefSet.removePreference(cdmaOptions);
1246
1247 // TODO: Support MSIM for this preference option.
1248 Preference gsmOptions = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
1249 prefSet.removePreference(gsmOptions);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001250
1251 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001252 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Junda Liua09d6962014-11-12 13:39:02 -08001253 boolean shouldHideCarrierSettings = Settings.Global.getInt(
1254 getContentResolver(), Settings.Global.HIDE_CARRIER_NETWORK_SETTINGS, 0) == 1;
1255 if (shouldHideCarrierSettings) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001256 prefSet.removePreference(fdnButton);
Junda Liua09d6962014-11-12 13:39:02 -08001257 if (mButtonDTMF != null) {
1258 prefSet.removePreference(mButtonDTMF);
Etan Cohen0ca1c802014-07-07 15:35:48 -07001259 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001260 } else {
Junda Liua09d6962014-11-12 13:39:02 -08001261 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1262 prefSet.removePreference(fdnButton);
1263
1264 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1265 addPreferencesFromResource(R.xml.cdma_call_privacy);
1266 }
1267 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
1268 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(this, FdnSetting.class));
1269
1270 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
1271 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1272
1273 Preference callForwardingPref = prefSet.findPreference(CALL_FORWARDING_KEY);
1274 callForwardingPref.setIntent(mSubscriptionInfoHelper.getIntent(
1275 this, GsmUmtsCallForwardOptions.class));
1276
1277 Preference additionalGsmSettingsPref =
1278 prefSet.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
1279 additionalGsmSettingsPref.setIntent(mSubscriptionInfoHelper.getIntent(
1280 this, GsmUmtsAdditionalCallOptions.class));
1281 }
1282 } else {
1283 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1284 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001285 }
1286 }
1287
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001288 // check the intent that started this activity and pop up the voicemail
1289 // dialog if we've been asked to.
1290 // If we have at least one non default VM provider registered then bring up
1291 // the selection for the VM provider, otherwise bring up a VM number dialog.
1292 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001293 if (mShowVoicemailPreference) {
Tyler Gunnbaee2952014-09-10 16:01:02 -07001294 if (DBG) {
1295 log("ACTION_ADD_VOICEMAIL Intent is thrown. current VM data size: "
1296 + mVMProvidersData.size());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001297 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001298 if (mVMProvidersData.size() > 1) {
1299 simulatePreferenceClick(mVoicemailProviders);
1300 } else {
1301 onPreferenceChange(mVoicemailProviders, DEFAULT_VM_PROVIDER_KEY);
1302 mVoicemailProviders.setValue(DEFAULT_VM_PROVIDER_KEY);
1303 }
1304 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001305 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001306
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001307 updateVoiceNumberField();
1308 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001309
Andrew Lee8d66d812014-11-24 14:54:02 -08001310 mVoicemailNotificationVibrate.setChecked(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001311 VoicemailNotificationSettingsUtil.isVibrationEnabled(mPhone));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001312
Andrew Lee312e8172014-10-23 17:01:36 -07001313 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1314 boolean currentValue =
1315 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1316 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1317 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001318 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1319 } else {
1320 prefSet.removePreference(mEnableVideoCalling);
1321 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001322 }
1323
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001324 private void handleTTYChange(Preference preference, Object objValue) {
1325 int buttonTtyMode;
1326 buttonTtyMode = Integer.valueOf((String) objValue).intValue();
1327 int settingsTtyMode = android.provider.Settings.Secure.getInt(
1328 getContentResolver(),
Sailesh Nepalbf900542014-07-15 16:18:32 -07001329 android.provider.Settings.Secure.PREFERRED_TTY_MODE,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001330 TelecomManager.TTY_MODE_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001331 if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
1332 Integer.toString(buttonTtyMode));
1333
1334 if (buttonTtyMode != settingsTtyMode) {
1335 switch(buttonTtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001336 case TelecomManager.TTY_MODE_OFF:
1337 case TelecomManager.TTY_MODE_FULL:
1338 case TelecomManager.TTY_MODE_HCO:
1339 case TelecomManager.TTY_MODE_VCO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001340 android.provider.Settings.Secure.putInt(getContentResolver(),
1341 android.provider.Settings.Secure.PREFERRED_TTY_MODE, buttonTtyMode);
1342 break;
1343 default:
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001344 buttonTtyMode = TelecomManager.TTY_MODE_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001345 }
1346
1347 mButtonTTY.setValue(Integer.toString(buttonTtyMode));
1348 updatePreferredTtyModeSummary(buttonTtyMode);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001349 Intent ttyModeChanged = new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
1350 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001351 sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
1352 }
1353 }
1354
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001355 private void updatePreferredTtyModeSummary(int TtyMode) {
1356 String [] txts = getResources().getStringArray(R.array.tty_mode_entries);
1357 switch(TtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001358 case TelecomManager.TTY_MODE_OFF:
1359 case TelecomManager.TTY_MODE_HCO:
1360 case TelecomManager.TTY_MODE_VCO:
1361 case TelecomManager.TTY_MODE_FULL:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001362 mButtonTTY.setSummary(txts[TtyMode]);
1363 break;
1364 default:
1365 mButtonTTY.setEnabled(false);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001366 mButtonTTY.setSummary(txts[TelecomManager.TTY_MODE_OFF]);
Sailesh Nepalbf900542014-07-15 16:18:32 -07001367 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001368 }
1369 }
1370
1371 private static void log(String msg) {
1372 Log.d(LOG_TAG, msg);
1373 }
1374
1375 /**
1376 * Updates the look of the VM preference widgets based on current VM provider settings.
1377 * Note that the provider name is loaded form the found activity via loadLabel in
1378 * {@link #initVoiceMailProviders()} in order for it to be localizable.
1379 */
1380 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1381 final String key = currentProviderSetting;
1382 final VoiceMailProvider provider = mVMProvidersData.get(key);
1383
1384 /* This is the case when we are coming up on a freshly wiped phone and there is no
1385 persisted value for the list preference mVoicemailProviders.
1386 In this case we want to show the UI asking the user to select a voicemail provider as
1387 opposed to silently falling back to default one. */
1388 if (provider == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001389 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> null.");
1390
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001391 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1392 mVoicemailSettings.setEnabled(false);
1393 mVoicemailSettings.setIntent(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001394 mVoicemailNotificationVibrate.setEnabled(false);
1395 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001396 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> " + provider.toString());
1397
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001398 final String providerName = provider.name;
1399 mVoicemailProviders.setSummary(providerName);
1400 mVoicemailSettings.setEnabled(true);
1401 mVoicemailSettings.setIntent(provider.intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001402 mVoicemailNotificationVibrate.setEnabled(true);
1403 }
1404 }
1405
1406 /**
1407 * Enumerates existing VM providers and puts their data into the list and populates
1408 * the preference list objects with their names.
1409 * In case we are called with ACTION_ADD_VOICEMAIL intent the intent may have
1410 * an extra string called IGNORE_PROVIDER_EXTRA with "package.activityName" of the provider
1411 * which should be hidden when we bring up the list of possible VM providers to choose.
1412 */
1413 private void initVoiceMailProviders() {
1414 if (DBG) log("initVoiceMailProviders()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001415
1416 String providerToIgnore = null;
Andrew Leef1776d82014-11-04 14:45:02 -08001417 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)
1418 && getIntent().hasExtra(IGNORE_PROVIDER_EXTRA)) {
1419 providerToIgnore = getIntent().getStringExtra(IGNORE_PROVIDER_EXTRA);
1420 // Remove this provider from the list.
1421 if (!TextUtils.isEmpty(providerToIgnore)) {
1422 if (DBG) log("Found ACTION_ADD_VOICEMAIL. providerToIgnore= " + providerToIgnore);
Andrew Lee6950c6c2014-11-26 12:05:42 -08001423 VoicemailProviderSettingsUtil.delete(this, providerToIgnore);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001424 }
1425 }
1426
1427 mVMProvidersData.clear();
1428
Andrew Leef1776d82014-11-04 14:45:02 -08001429 List<String> entries = new ArrayList<String>();
1430 List<String> values = new ArrayList<String>();
1431
1432 // Add default voicemail provider.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001433 final String myCarrier = getString(R.string.voicemail_default);
1434 mVMProvidersData.put(DEFAULT_VM_PROVIDER_KEY, new VoiceMailProvider(myCarrier, null));
Andrew Leef1776d82014-11-04 14:45:02 -08001435 entries.add(myCarrier);
1436 values.add(DEFAULT_VM_PROVIDER_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001437
Andrew Leef1776d82014-11-04 14:45:02 -08001438 // Add other voicemail providers.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001439 PackageManager pm = getPackageManager();
Andrew Leef1776d82014-11-04 14:45:02 -08001440 Intent intent = new Intent(ACTION_CONFIGURE_VOICEMAIL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001441 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001442 for (int i = 0; i < resolveInfos.size(); i++) {
1443 final ResolveInfo ri= resolveInfos.get(i);
1444 final ActivityInfo currentActivityInfo = ri.activityInfo;
Andrew Lee6214e2b2014-11-04 13:57:38 -08001445 final String key = currentActivityInfo.name;
Andrew Leef1776d82014-11-04 14:45:02 -08001446
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001447 if (key.equals(providerToIgnore)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001448 continue;
1449 }
Andrew Leef1776d82014-11-04 14:45:02 -08001450
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001451 if (DBG) log("Loading key: " + key);
1452 final String nameForDisplay = ri.loadLabel(pm).toString();
1453 Intent providerIntent = new Intent();
1454 providerIntent.setAction(ACTION_CONFIGURE_VOICEMAIL);
Andrew Leef1776d82014-11-04 14:45:02 -08001455 providerIntent.setClassName(currentActivityInfo.packageName, currentActivityInfo.name);
1456 VoiceMailProvider vmProvider = new VoiceMailProvider(nameForDisplay, providerIntent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001457
Andrew Leef1776d82014-11-04 14:45:02 -08001458 if (DBG) log("Store VoiceMailProvider. Key: " + key + " -> " + vmProvider.toString());
1459 mVMProvidersData.put(key, vmProvider);
1460 entries.add(vmProvider.name);
1461 values.add(key);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001462 }
1463
Andrew Leef1776d82014-11-04 14:45:02 -08001464 mVoicemailProviders.setEntries(entries.toArray(new String[0]));
1465 mVoicemailProviders.setEntryValues(values.toArray(new String[0]));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001466
Andrew Leef1776d82014-11-04 14:45:02 -08001467 // Remember the current Voicemail Provider key as a "previous" key. This will be used when
1468 // we fail to update Voicemail Provider, which requires rollback. We will update this when
1469 // the VM Provider setting is successfully updated.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001470 mPreviousVMProviderKey = getCurrentVoicemailProviderKey();
1471 if (DBG) log("Set up the first mPreviousVMProviderKey: " + mPreviousVMProviderKey);
1472
1473 // Finally update the preference texts.
1474 updateVMPreferenceWidgets(mPreviousVMProviderKey);
1475 }
1476
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001477 /**
1478 * Simulates user clicking on a passed preference.
1479 * Usually needed when the preference is a dialog preference and we want to invoke
1480 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001481 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001482 */
1483 private void simulatePreferenceClick(Preference preference) {
1484 // Go through settings until we find our setting
1485 // and then simulate a click on it to bring up the dialog
1486 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1487 for (int idx = 0; idx < adapter.getCount(); idx++) {
1488 if (adapter.getItem(idx) == preference) {
1489 getPreferenceScreen().onItemClick(this.getListView(),
1490 null, idx, adapter.getItemId(idx));
1491 break;
1492 }
1493 }
1494 }
1495
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001496 private String getCurrentVoicemailProviderKey() {
1497 final String key = mVoicemailProviders.getValue();
1498 return (key != null) ? key : DEFAULT_VM_PROVIDER_KEY;
1499 }
1500
1501 @Override
1502 public boolean onOptionsItemSelected(MenuItem item) {
1503 final int itemId = item.getItemId();
1504 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001505 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001506 return true;
1507 }
1508 return super.onOptionsItemSelected(item);
1509 }
Andrew Lee8d66d812014-11-24 14:54:02 -08001510
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}