blob: 9956397a6b6c27692590214976c4cb4e7e1b61a7 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
19import android.app.ActionBar;
20import android.app.Activity;
Evan Charlton1c696832014-04-15 14:24:23 -070021import android.app.ActivityOptions;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.AlertDialog;
23import android.app.Dialog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.ContentResolver;
25import android.content.Context;
26import android.content.DialogInterface;
27import android.content.Intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.database.Cursor;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.media.AudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070030import android.os.AsyncResult;
31import android.os.Bundle;
32import android.os.Handler;
33import android.os.Message;
34import android.os.UserHandle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.preference.CheckBoxPreference;
36import android.preference.ListPreference;
37import android.preference.Preference;
38import android.preference.PreferenceActivity;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.preference.PreferenceScreen;
40import android.provider.ContactsContract.CommonDataKinds;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070042import android.telecom.PhoneAccountHandle;
43import android.telecom.TelecomManager;
Andrew Lee93c345f2014-10-27 15:25:07 -070044import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.text.TextUtils;
46import android.util.Log;
47import android.view.MenuItem;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.widget.ListAdapter;
Jay Shraunerc2684732014-11-12 12:10:37 -080049import android.widget.Toast;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050
Andrew Lee312e8172014-10-23 17:01:36 -070051import com.android.ims.ImsManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import com.android.internal.telephony.CallForwardInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import com.android.internal.telephony.Phone;
54import com.android.internal.telephony.PhoneConstants;
Andrew Lee2170a972014-08-13 18:13:01 -070055import com.android.phone.common.util.SettingsUtil;
Andrew Leedb2fe562014-09-03 15:40:43 -070056import com.android.phone.settings.AccountSelectionPreference;
Andrew Lee1af6cf72014-11-04 17:35:26 -080057import com.android.phone.settings.CallForwardInfoUtil;
Andrew Lee84024342014-11-06 23:37:09 -080058import com.android.phone.settings.VoicemailDialogUtil;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -080059import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Andrew Lee64e5c9d2014-11-26 12:00:10 -080060import com.android.phone.settings.VoicemailProviderListPreference;
61import com.android.phone.settings.VoicemailProviderListPreference.VoicemailProvider;
Andrew Leeb490d732014-10-27 15:00:41 -070062import com.android.phone.settings.VoicemailProviderSettings;
Andrew Lee88b51e22014-10-29 15:48:51 -070063import com.android.phone.settings.VoicemailProviderSettingsUtil;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -080064import com.android.phone.settings.VoicemailRingtonePreference;
Andrew Lee5ed870c2014-10-29 11:47:49 -070065import com.android.phone.settings.fdn.FdnSetting;
Sailesh Nepal788959e2014-07-08 23:36:40 -070066import com.android.services.telephony.sip.SipUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067
Andrew Lee2170a972014-08-13 18:13:01 -070068import java.lang.String;
Andrew Leef1776d82014-11-04 14:45:02 -080069import java.util.ArrayList;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import java.util.Collection;
71import java.util.HashMap;
72import java.util.HashSet;
73import java.util.Iterator;
74import java.util.List;
75import java.util.Map;
76
77/**
78 * Top level "Call settings" UI; see res/xml/call_feature_setting.xml
79 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070080 * This preference screen is the root of the "Call settings" hierarchy available from the Phone
81 * app; the settings here let you control various features related to phone calls (including
82 * voicemail settings, the "Respond via SMS" feature, and others.) It's used only on
83 * voice-capable phone devices.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084 *
85 * Note that this activity is part of the package com.android.phone, even
86 * though you reach it from the "Phone" app (i.e. DialtactsActivity) which
87 * is from the package com.android.contacts.
88 *
89 * For the "Mobile network settings" screen under the main Settings app,
90 * See {@link MobileNetworkSettings}.
91 *
Andrew Leece8ae2a2014-09-10 10:41:48 -070092 * TODO: Settings should be split into PreferenceFragments where possible (ie. voicemail).
Andrew Lee2170a972014-08-13 18:13:01 -070093 *
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094 * @see com.android.phone.MobileNetworkSettings
95 */
96public class CallFeaturesSetting extends PreferenceActivity
97 implements DialogInterface.OnClickListener,
Andrew Lee2170a972014-08-13 18:13:01 -070098 Preference.OnPreferenceChangeListener,
Andrew Lee2170a972014-08-13 18:13:01 -070099 EditPhoneNumberPreference.OnDialogClosedListener,
Andrew Leece8ae2a2014-09-10 10:41:48 -0700100 EditPhoneNumberPreference.GetDefaultNumberListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700101 private static final String LOG_TAG = "CallFeaturesSetting";
102 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
Andrew Lee77527ac2014-10-21 16:57:39 -0700103 // STOPSHIP if true. Flag to override behavior default behavior to hide VT setting.
104 private static final boolean ENABLE_VT_FLAG = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700105
106 /**
107 * Intent action to bring up Voicemail Provider settings.
108 *
109 * @see #IGNORE_PROVIDER_EXTRA
110 */
111 public static final String ACTION_ADD_VOICEMAIL =
112 "com.android.phone.CallFeaturesSetting.ADD_VOICEMAIL";
113 // intent action sent by this activity to a voice mail provider
114 // to trigger its configuration UI
115 public static final String ACTION_CONFIGURE_VOICEMAIL =
116 "com.android.phone.CallFeaturesSetting.CONFIGURE_VOICEMAIL";
117 // Extra put in the return from VM provider config containing voicemail number to set
118 public static final String VM_NUMBER_EXTRA = "com.android.phone.VoicemailNumber";
119 // Extra put in the return from VM provider config containing call forwarding number to set
120 public static final String FWD_NUMBER_EXTRA = "com.android.phone.ForwardingNumber";
121 // Extra put in the return from VM provider config containing call forwarding number to set
122 public static final String FWD_NUMBER_TIME_EXTRA = "com.android.phone.ForwardingNumberTime";
123 // If the VM provider returns non null value in this extra we will force the user to
124 // choose another VM provider
125 public static final String SIGNOUT_EXTRA = "com.android.phone.Signout";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700126
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127 /**
128 * String Extra put into ACTION_ADD_VOICEMAIL call to indicate which provider should be hidden
129 * in the list of providers presented to the user. This allows a provider which is being
130 * disabled (e.g. GV user logging out) to force the user to pick some other provider.
131 */
132 public static final String IGNORE_PROVIDER_EXTRA = "com.android.phone.ProviderToIgnore";
133
134 // string constants
135 private static final String NUM_PROJECTION[] = {CommonDataKinds.Phone.NUMBER};
136
137 // String keys for preference lookup
138 // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
Andrew Lee8d66d812014-11-24 14:54:02 -0800139 // TODO: Consider moving these strings to strings.xml, so that they are not duplicated here and
140 // in the layout files. These strings need to be treated carefully; if the setting is
141 // persistent, they are used as the key to store shared preferences and the name should not be
142 // changed unless the settings are also migrated.
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";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700147 private static final String BUTTON_FDN_KEY = "button_fdn_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700148
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700149 private static final String BUTTON_DTMF_KEY = "button_dtmf_settings";
150 private static final String BUTTON_RETRY_KEY = "button_auto_retry_key";
151 private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
152 private static final String BUTTON_HAC_KEY = "button_hac_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700153
154 private static final String BUTTON_GSM_UMTS_OPTIONS = "button_gsm_more_expand_key";
155 private static final String BUTTON_CDMA_OPTIONS = "button_cdma_more_expand_key";
Andrew Lee2b36ba22014-11-05 17:08:49 -0800156 private static final String CALL_FORWARDING_KEY = "call_forwarding_key";
157 private static final String ADDITIONAL_GSM_SETTINGS_KEY = "additional_gsm_call_settings_key";
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700158
Andrew Leece8ae2a2014-09-10 10:41:48 -0700159 private static final String PHONE_ACCOUNT_SETTINGS_KEY =
160 "phone_account_settings_preference_screen";
Andrew Leedb2fe562014-09-03 15:40:43 -0700161
Andrew Leedf14ead2014-10-17 14:22:52 -0700162 private static final String ENABLE_VIDEO_CALLING_KEY = "button_enable_video_calling";
163
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700164 /** Event for Async voicemail change call */
165 private static final int EVENT_VOICEMAIL_CHANGED = 500;
166 private static final int EVENT_FORWARDING_CHANGED = 501;
167 private static final int EVENT_FORWARDING_GET_COMPLETED = 502;
168
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700169 public static final String HAC_KEY = "HACSetting";
170 public static final String HAC_VAL_ON = "ON";
171 public static final String HAC_VAL_OFF = "OFF";
172
173 /** Handle to voicemail pref */
174 private static final int VOICEMAIL_PREF_ID = 1;
175 private static final int VOICEMAIL_PROVIDER_CFG_ID = 2;
176
177 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178 private AudioManager mAudioManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700179
Andrew Lee5ed870c2014-10-29 11:47:49 -0700180 private SubscriptionInfoHelper mSubscriptionInfoHelper;
181
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700182 private EditPhoneNumberPreference mSubMenuVoicemailSettings;
183
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700184 /** Whether dialpad plays DTMF tone or not. */
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185 private CheckBoxPreference mButtonAutoRetry;
186 private CheckBoxPreference mButtonHAC;
187 private ListPreference mButtonDTMF;
188 private ListPreference mButtonTTY;
Andrew Leece8ae2a2014-09-10 10:41:48 -0700189 private Preference mPhoneAccountSettingsPreference;
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800190 private VoicemailProviderListPreference mVoicemailProviders;
Andrew Lee97708a42014-09-25 12:39:07 -0700191 private PreferenceScreen mVoicemailSettingsScreen;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700192 private PreferenceScreen mVoicemailSettings;
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800193 private VoicemailRingtonePreference mVoicemailNotificationRingtone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700194 private CheckBoxPreference mVoicemailNotificationVibrate;
Andrew Leedf14ead2014-10-17 14:22:52 -0700195 private CheckBoxPreference mEnableVideoCalling;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700196
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700197 /**
198 * Results of reading forwarding settings
199 */
200 private CallForwardInfo[] mForwardingReadResults = null;
201
202 /**
203 * Result of forwarding number change.
204 * Keys are reasons (eg. unconditional forwarding).
205 */
206 private Map<Integer, AsyncResult> mForwardingChangeResults = null;
207
208 /**
209 * Expected CF read result types.
210 * This set keeps track of the CF types for which we've issued change
211 * commands so we can tell when we've received all of the responses.
212 */
213 private Collection<Integer> mExpectedChangeResultReasons = null;
214
215 /**
216 * Result of vm number change
217 */
218 private AsyncResult mVoicemailChangeResult = null;
219
220 /**
221 * Previous VM provider setting so we can return to it in case of failure.
222 */
223 private String mPreviousVMProviderKey = null;
224
225 /**
226 * Id of the dialog being currently shown.
227 */
228 private int mCurrentDialogId = 0;
229
230 /**
231 * Flag indicating that we are invoking settings for the voicemail provider programmatically
232 * due to vm provider change.
233 */
234 private boolean mVMProviderSettingsForced = false;
235
236 /**
237 * Flag indicating that we are making changes to vm or fwd numbers
238 * due to vm provider change.
239 */
240 private boolean mChangingVMorFwdDueToProviderChange = false;
241
242 /**
243 * True if we are in the process of vm & fwd number change and vm has already been changed.
244 * This is used to decide what to do in case of rollback.
245 */
246 private boolean mVMChangeCompletedSuccessfully = false;
247
248 /**
249 * True if we had full or partial failure setting forwarding numbers and so need to roll them
250 * back.
251 */
252 private boolean mFwdChangesRequireRollback = false;
253
254 /**
255 * Id of error msg to display to user once we are done reverting the VM provider to the previous
256 * one.
257 */
258 private int mVMOrFwdSetError = 0;
259
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700260 /** string to hold old voicemail number as it is being updated. */
261 private String mOldVmNumber;
262
263 // New call forwarding settings and vm number we will be setting
264 // Need to save these since before we get to saving we need to asynchronously
265 // query the existing forwarding settings.
266 private CallForwardInfo[] mNewFwdSettings;
267 private String mNewVMNumber;
268
269 private boolean mForeground;
270
271 @Override
272 public void onPause() {
273 super.onPause();
274 mForeground = false;
275 }
276
277 /**
278 * We have to pull current settings from the network for all kinds of
279 * voicemail providers so we can tell whether we have to update them,
280 * so use this bit to keep track of whether we're reading settings for the
281 * default provider and should therefore save them out when done.
282 */
283 private boolean mReadingSettingsForDefaultProvider = false;
284
Tyler Gunnbaee2952014-09-10 16:01:02 -0700285 /**
286 * Used to indicate that the voicemail preference should be shown.
287 */
288 private boolean mShowVoicemailPreference = false;
289
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700290 /*
291 * Click Listeners, handle click based on objects attached to UI.
292 */
293
294 // Click listener for all toggle events
295 @Override
296 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
297 if (preference == mSubMenuVoicemailSettings) {
298 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299 } else if (preference == mButtonDTMF) {
300 return true;
301 } else if (preference == mButtonTTY) {
302 return true;
303 } else if (preference == mButtonAutoRetry) {
304 android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
305 android.provider.Settings.Global.CALL_AUTO_RETRY,
306 mButtonAutoRetry.isChecked() ? 1 : 0);
307 return true;
308 } else if (preference == mButtonHAC) {
309 int hac = mButtonHAC.isChecked() ? 1 : 0;
310 // Update HAC value in Settings database
311 Settings.System.putInt(mPhone.getContext().getContentResolver(),
312 Settings.System.HEARING_AID, hac);
313
314 // Update HAC Value in AudioManager
315 mAudioManager.setParameter(HAC_KEY, hac != 0 ? HAC_VAL_ON : HAC_VAL_OFF);
316 return true;
Andrew Lee696bff52014-12-03 17:58:18 -0800317 } else if (preference.getKey().equals(mVoicemailSettings.getKey())) {
318 // Check key instead of comparing reference because closing the voicemail notification
319 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
320 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800321 if (DBG) log("onPreferenceTreeClick: Voicemail Settings Preference is clicked.");
322
Andrew Lee696bff52014-12-03 17:58:18 -0800323 final Dialog dialog = ((PreferenceScreen) preference).getDialog();
Yorke Leea0f63bf2014-10-09 18:27:20 -0700324 if (dialog != null) {
325 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
326 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800327
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700328 if (preference.getIntent() != null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800329 if (DBG) log("Invoking cfg intent " + preference.getIntent().getPackage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700330
331 // onActivityResult() will be responsible for resetting some of variables.
332 this.startActivityForResult(preference.getIntent(), VOICEMAIL_PROVIDER_CFG_ID);
333 return true;
334 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800335 if (DBG) log("onPreferenceTreeClick(). No intent; use default behavior in xml.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700336
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800337 // onActivityResult() will not be called, so reset variables here.
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800338 mPreviousVMProviderKey = VoicemailProviderListPreference.DEFAULT_KEY;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700339 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700340 return false;
341 }
Andrew Lee97708a42014-09-25 12:39:07 -0700342 } else if (preference == mVoicemailSettingsScreen) {
Yorke Leea0f63bf2014-10-09 18:27:20 -0700343 final Dialog dialog = mVoicemailSettingsScreen.getDialog();
344 if (dialog != null) {
345 dialog.getActionBar().setDisplayHomeAsUpEnabled(false);
346 }
Andrew Lee97708a42014-09-25 12:39:07 -0700347 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700348 }
349 return false;
350 }
351
352 /**
353 * Implemented to support onPreferenceChangeListener to look for preference
354 * changes.
355 *
356 * @param preference is the preference to be changed
357 * @param objValue should be the value of the selection, NOT its localized
358 * display value.
359 */
360 @Override
361 public boolean onPreferenceChange(Preference preference, Object objValue) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800362 if (DBG) log("onPreferenceChange: \"" + preference + "\" changed to \"" + objValue + "\"");
Andrew Lee2170a972014-08-13 18:13:01 -0700363
364 if (preference == mButtonDTMF) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700365 int index = mButtonDTMF.findIndexOfValue((String) objValue);
366 Settings.System.putInt(mPhone.getContext().getContentResolver(),
367 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
368 } else if (preference == mButtonTTY) {
369 handleTTYChange(preference, objValue);
370 } else if (preference == mVoicemailProviders) {
371 final String newProviderKey = (String) objValue;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800372
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700373 // If previous provider key and the new one is same, we don't need to handle it.
374 if (mPreviousVMProviderKey.equals(newProviderKey)) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800375 if (DBG) log("No change is made to the VM provider setting.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700376 return true;
377 }
378 updateVMPreferenceWidgets(newProviderKey);
379
Andrew Leeb490d732014-10-27 15:00:41 -0700380 final VoicemailProviderSettings newProviderSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800381 VoicemailProviderSettingsUtil.load(this, newProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700382
Andrew Lee88b51e22014-10-29 15:48:51 -0700383 // If the user switches to a voice mail provider and we have numbers stored for it we
384 // will automatically change the phone's voice mail and forwarding number to the stored
385 // ones. Otherwise we will bring up provider's configuration UI.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700386 if (newProviderSettings == null) {
387 // Force the user into a configuration of the chosen provider
388 Log.w(LOG_TAG, "Saved preferences not found - invoking config");
389 mVMProviderSettingsForced = true;
390 simulatePreferenceClick(mVoicemailSettings);
391 } else {
392 if (DBG) log("Saved preferences found - switching to them");
393 // Set this flag so if we get a failure we revert to previous provider
394 mChangingVMorFwdDueToProviderChange = true;
395 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
396 }
Andrew Lee8d66d812014-11-24 14:54:02 -0800397 } else if (preference.getKey().equals(mVoicemailNotificationVibrate.getKey())) {
Andrew Lee696bff52014-12-03 17:58:18 -0800398 // Check key instead of comparing reference because closing the voicemail notification
399 // ringtone dialog invokes onResume(), but leaves the old preference screen up,
400 // TODO: Revert to checking reference after migrating voicemail to its own activity.
Andrew Lee8d66d812014-11-24 14:54:02 -0800401 VoicemailNotificationSettingsUtil.setVibrationEnabled(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -0800402 mPhone, Boolean.TRUE.equals(objValue));
Andrew Leedf14ead2014-10-17 14:22:52 -0700403 } else if (preference == mEnableVideoCalling) {
Andrew Lee312e8172014-10-23 17:01:36 -0700404 if (ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())) {
405 PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
406 } else {
407 AlertDialog.Builder builder = new AlertDialog.Builder(this);
408 DialogInterface.OnClickListener networkSettingsClickListener =
409 new Dialog.OnClickListener() {
410 @Override
411 public void onClick(DialogInterface dialog, int which) {
412 startActivity(new Intent(mPhone.getContext(),
413 com.android.phone.MobileNetworkSettings.class));
414 }
415 };
416 builder.setMessage(getResources().getString(
417 R.string.enable_video_calling_dialog_msg))
418 .setNeutralButton(getResources().getString(
419 R.string.enable_video_calling_dialog_settings),
420 networkSettingsClickListener)
421 .setPositiveButton(android.R.string.ok, null)
422 .show();
423 return false;
424 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700425 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800426
427 // Always let the preference setting proceed.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700428 return true;
429 }
430
431 @Override
432 public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800433 if (DBG) log("onDialogClosed: Button clicked is " + buttonClicked);
434
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700435 if (buttonClicked == DialogInterface.BUTTON_NEGATIVE) {
436 return;
437 }
438
439 if (preference == mSubMenuVoicemailSettings) {
Andrew Leee438b312014-10-29 16:59:15 -0700440 VoicemailProviderSettings newSettings = new VoicemailProviderSettings(
441 mSubMenuVoicemailSettings.getPhoneNumber(),
442 VoicemailProviderSettings.NO_FORWARDING);
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800443 saveVoiceMailAndForwardingNumber(mVoicemailProviders.getKey(), newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700444 }
445 }
446
447 /**
448 * Implemented for EditPhoneNumberPreference.GetDefaultNumberListener.
449 * This method set the default values for the various
450 * EditPhoneNumberPreference dialogs.
451 */
452 @Override
453 public String onGetDefaultNumber(EditPhoneNumberPreference preference) {
454 if (preference == mSubMenuVoicemailSettings) {
455 // update the voicemail number field, which takes care of the
456 // mSubMenuVoicemailSettings itself, so we should return null.
457 if (DBG) log("updating default for voicemail dialog");
458 updateVoiceNumberField();
459 return null;
460 }
461
462 String vmDisplay = mPhone.getVoiceMailNumber();
463 if (TextUtils.isEmpty(vmDisplay)) {
464 // if there is no voicemail number, we just return null to
465 // indicate no contribution.
466 return null;
467 }
468
469 // Return the voicemail number prepended with "VM: "
470 if (DBG) log("updating default for call forwarding dialogs");
471 return getString(R.string.voicemail_abbreviated) + " " + vmDisplay;
472 }
473
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700474 private void switchToPreviousVoicemailProvider() {
475 if (DBG) log("switchToPreviousVoicemailProvider " + mPreviousVMProviderKey);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700476
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800477 if (mPreviousVMProviderKey == null) {
478 return;
479 }
480
481 if (mVMChangeCompletedSuccessfully || mFwdChangesRequireRollback) {
Andrew Lee84024342014-11-06 23:37:09 -0800482 showDialogIfForeground(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800483 final VoicemailProviderSettings prevSettings =
Andrew Lee6950c6c2014-11-26 12:05:42 -0800484 VoicemailProviderSettingsUtil.load(this, mPreviousVMProviderKey);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800485 if (prevSettings == null) {
486 Log.e(LOG_TAG, "VoicemailProviderSettings for the key \""
487 + mPreviousVMProviderKey + "\" is null but should be loaded.");
488 }
489
490 if (mVMChangeCompletedSuccessfully) {
491 mNewVMNumber = prevSettings.getVoicemailNumber();
492 Log.i(LOG_TAG, "VM change is already completed successfully."
493 + "Have to revert VM back to " + mNewVMNumber + " again.");
494 mPhone.setVoiceMailNumber(
495 mPhone.getVoiceMailAlphaTag().toString(),
496 mNewVMNumber,
497 Message.obtain(mRevertOptionComplete, EVENT_VOICEMAIL_CHANGED));
498 }
499
500 if (mFwdChangesRequireRollback) {
501 Log.i(LOG_TAG, "Requested to rollback forwarding changes.");
502
503 final CallForwardInfo[] prevFwdSettings = prevSettings.getForwardingSettings();
504 if (prevFwdSettings != null) {
505 Map<Integer, AsyncResult> results = mForwardingChangeResults;
506 resetForwardingChangeState();
507 for (int i = 0; i < prevFwdSettings.length; i++) {
508 CallForwardInfo fi = prevFwdSettings[i];
509 if (DBG) log("Reverting fwd #: " + i + ": " + fi.toString());
510 // Only revert the settings for which the update succeeded.
511 AsyncResult result = results.get(fi.reason);
512 if (result != null && result.exception == null) {
513 mExpectedChangeResultReasons.add(fi.reason);
514 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
515 mRevertOptionComplete.obtainMessage(
516 EVENT_FORWARDING_CHANGED, i, 0));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700517 }
518 }
519 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700520 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800521 } else {
522 if (DBG) log("No need to revert");
523 onRevertDone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700524 }
525 }
526
527 private void onRevertDone() {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800528 if (DBG) log("onRevertDone: Changing provider key back to " + mPreviousVMProviderKey);
529
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700530 updateVMPreferenceWidgets(mPreviousVMProviderKey);
531 updateVoiceNumberField();
532 if (mVMOrFwdSetError != 0) {
Andrew Leeab082272014-11-04 15:50:42 -0800533 showDialogIfForeground(mVMOrFwdSetError);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700534 mVMOrFwdSetError = 0;
535 }
536 }
537
538 @Override
539 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
540 if (DBG) {
541 log("onActivityResult: requestCode: " + requestCode
542 + ", resultCode: " + resultCode
543 + ", data: " + data);
544 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800545
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700546 // there are cases where the contact picker may end up sending us more than one
547 // request. We want to ignore the request if we're not in the correct state.
548 if (requestCode == VOICEMAIL_PROVIDER_CFG_ID) {
549 boolean failure = false;
550
551 // No matter how the processing of result goes lets clear the flag
552 if (DBG) log("mVMProviderSettingsForced: " + mVMProviderSettingsForced);
553 final boolean isVMProviderSettingsForced = mVMProviderSettingsForced;
554 mVMProviderSettingsForced = false;
555
556 String vmNum = null;
557 if (resultCode != RESULT_OK) {
558 if (DBG) log("onActivityResult: vm provider cfg result not OK.");
559 failure = true;
560 } else {
561 if (data == null) {
562 if (DBG) log("onActivityResult: vm provider cfg result has no data");
563 failure = true;
564 } else {
565 if (data.getBooleanExtra(SIGNOUT_EXTRA, false)) {
566 if (DBG) log("Provider requested signout");
567 if (isVMProviderSettingsForced) {
568 if (DBG) log("Going back to previous provider on signout");
569 switchToPreviousVoicemailProvider();
570 } else {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800571 final String victim = mVoicemailProviders.getKey();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700572 if (DBG) log("Relaunching activity and ignoring " + victim);
573 Intent i = new Intent(ACTION_ADD_VOICEMAIL);
574 i.putExtra(IGNORE_PROVIDER_EXTRA, victim);
575 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
576 this.startActivity(i);
577 }
578 return;
579 }
580 vmNum = data.getStringExtra(VM_NUMBER_EXTRA);
581 if (vmNum == null || vmNum.length() == 0) {
582 if (DBG) log("onActivityResult: vm provider cfg result has no vmnum");
583 failure = true;
584 }
585 }
586 }
587 if (failure) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800588 if (DBG) log("Failure in return from voicemail provider.");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700589 if (isVMProviderSettingsForced) {
590 switchToPreviousVoicemailProvider();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700591 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800592
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700593 return;
594 }
595 mChangingVMorFwdDueToProviderChange = isVMProviderSettingsForced;
596 final String fwdNum = data.getStringExtra(FWD_NUMBER_EXTRA);
597
Santos Cordonda120f42014-08-06 04:44:34 -0700598 // TODO: It would be nice to load the current network setting for this and
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700599 // send it to the provider when it's config is invoked so it can use this as default
600 final int fwdNumTime = data.getIntExtra(FWD_NUMBER_TIME_EXTRA, 20);
601
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800602 if (DBG) log("onActivityResult: cfg result has forwarding number " + fwdNum);
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800603 saveVoiceMailAndForwardingNumber(mVoicemailProviders.getKey(),
Andrew Leeb490d732014-10-27 15:00:41 -0700604 new VoicemailProviderSettings(vmNum, fwdNum, fwdNumTime));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700605 return;
606 }
607
608 if (requestCode == VOICEMAIL_PREF_ID) {
609 if (resultCode != RESULT_OK) {
610 if (DBG) log("onActivityResult: contact picker result not OK.");
611 return;
612 }
613
614 Cursor cursor = null;
615 try {
616 cursor = getContentResolver().query(data.getData(),
617 NUM_PROJECTION, null, null, null);
618 if ((cursor == null) || (!cursor.moveToFirst())) {
619 if (DBG) log("onActivityResult: bad contact data, no results found.");
620 return;
621 }
622 mSubMenuVoicemailSettings.onPickActivityResult(cursor.getString(0));
623 return;
624 } finally {
625 if (cursor != null) {
626 cursor.close();
627 }
628 }
629 }
630
631 super.onActivityResult(requestCode, resultCode, data);
632 }
633
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700634 /**
635 * Wrapper around showDialog() that will silently do nothing if we're
636 * not in the foreground.
637 *
638 * This is useful here because most of the dialogs we display from
639 * this class are triggered by asynchronous events (like
640 * success/failure messages from the telephony layer) and it's
641 * possible for those events to come in even after the user has gone
642 * to a different screen.
643 */
644 // TODO: this is too brittle: it's still easy to accidentally add new
645 // code here that calls showDialog() directly (which will result in a
646 // WindowManager$BadTokenException if called after the activity has
647 // been stopped.)
648 //
649 // It would be cleaner to do the "if (mForeground)" check in one
650 // central place, maybe by using a single Handler for all asynchronous
651 // events (and have *that* discard events if we're not in the
652 // foreground.)
653 //
654 // Unfortunately it's not that simple, since we sometimes need to do
655 // actual work to handle these events whether or not we're in the
656 // foreground (see the Handler code in mSetOptionComplete for
657 // example.)
Andrew Leeab082272014-11-04 15:50:42 -0800658 //
659 // TODO: It's a bit worrisome that we don't do anything in error cases when we're not in the
660 // foreground. Consider displaying a toast instead.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700661 private void showDialogIfForeground(int id) {
662 if (mForeground) {
663 showDialog(id);
664 }
665 }
666
667 private void dismissDialogSafely(int id) {
668 try {
669 dismissDialog(id);
670 } catch (IllegalArgumentException e) {
671 // This is expected in the case where we were in the background
672 // at the time we would normally have shown the dialog, so we didn't
673 // show it.
674 }
675 }
676
Andrew Leeb490d732014-10-27 15:00:41 -0700677 private void saveVoiceMailAndForwardingNumber(
678 String key, VoicemailProviderSettings newSettings) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700679 if (DBG) log("saveVoiceMailAndForwardingNumber: " + newSettings.toString());
Andrew Leeb490d732014-10-27 15:00:41 -0700680 mNewVMNumber = newSettings.getVoicemailNumber();
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800681 mNewVMNumber = (mNewVMNumber == null) ? "" : mNewVMNumber;
Andrew Leeb490d732014-10-27 15:00:41 -0700682 mNewFwdSettings = newSettings.getForwardingSettings();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700683
684 // No fwd settings on CDMA
685 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
686 if (DBG) log("ignoring forwarding setting since this is CDMA phone");
Andrew Leeb490d732014-10-27 15:00:41 -0700687 mNewFwdSettings = VoicemailProviderSettings.NO_FORWARDING;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700688 }
689
Andrew Leee3c15212014-10-28 13:12:55 -0700690 // Throw a warning if the voicemail is the same and we did not change forwarding.
Andrew Leeb490d732014-10-27 15:00:41 -0700691 if (mNewVMNumber.equals(mOldVmNumber)
692 && mNewFwdSettings == VoicemailProviderSettings.NO_FORWARDING) {
Andrew Lee84024342014-11-06 23:37:09 -0800693 showDialogIfForeground(VoicemailDialogUtil.VM_NOCHANGE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700694 return;
695 }
696
Andrew Lee6950c6c2014-11-26 12:05:42 -0800697 VoicemailProviderSettingsUtil.save(this, key, newSettings);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700698 mVMChangeCompletedSuccessfully = false;
699 mFwdChangesRequireRollback = false;
700 mVMOrFwdSetError = 0;
701 if (!key.equals(mPreviousVMProviderKey)) {
702 mReadingSettingsForDefaultProvider =
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800703 mPreviousVMProviderKey.equals(VoicemailProviderListPreference.DEFAULT_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700704 if (DBG) log("Reading current forwarding settings");
Andrew Leeb490d732014-10-27 15:00:41 -0700705 int numSettingsReasons = VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS.length;
706 mForwardingReadResults = new CallForwardInfo[numSettingsReasons];
707 for (int i = 0; i < mForwardingReadResults.length; i++) {
708 mPhone.getCallForwardingOption(
709 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[i],
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700710 mGetOptionComplete.obtainMessage(EVENT_FORWARDING_GET_COMPLETED, i, 0));
711 }
Andrew Lee84024342014-11-06 23:37:09 -0800712 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700713 } else {
714 saveVoiceMailAndForwardingNumberStage2();
715 }
716 }
717
718 private final Handler mGetOptionComplete = new Handler() {
719 @Override
720 public void handleMessage(Message msg) {
721 AsyncResult result = (AsyncResult) msg.obj;
722 switch (msg.what) {
723 case EVENT_FORWARDING_GET_COMPLETED:
724 handleForwardingSettingsReadResult(result, msg.arg1);
725 break;
726 }
727 }
728 };
729
730 private void handleForwardingSettingsReadResult(AsyncResult ar, int idx) {
731 if (DBG) Log.d(LOG_TAG, "handleForwardingSettingsReadResult: " + idx);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800732
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700733 Throwable error = null;
734 if (ar.exception != null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700735 error = ar.exception;
Andrew Lee1af6cf72014-11-04 17:35:26 -0800736 if (DBG) Log.d(LOG_TAG, "FwdRead: ar.exception=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700737 }
738 if (ar.userObj instanceof Throwable) {
Andrew Lee1af6cf72014-11-04 17:35:26 -0800739 error = (Throwable) ar.userObj;
740 if (DBG) Log.d(LOG_TAG, "FwdRead: userObj=" + error.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700741 }
742
743 // We may have already gotten an error and decided to ignore the other results.
744 if (mForwardingReadResults == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800745 if (DBG) Log.d(LOG_TAG, "Ignoring fwd reading result: " + idx);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700746 return;
747 }
748
749 // In case of error ignore other results, show an error dialog
750 if (error != null) {
751 if (DBG) Log.d(LOG_TAG, "Error discovered for fwd read : " + idx);
752 mForwardingReadResults = null;
Andrew Lee84024342014-11-06 23:37:09 -0800753 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
754 showDialogIfForeground(VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700755 return;
756 }
757
Andrew Lee1af6cf72014-11-04 17:35:26 -0800758 // Get the forwarding info.
759 mForwardingReadResults[idx] = CallForwardInfoUtil.getCallForwardInfo(
760 (CallForwardInfo[]) ar.result,
761 VoicemailProviderSettings.FORWARDING_SETTINGS_REASONS[idx]);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700762
763 // Check if we got all the results already
764 boolean done = true;
765 for (int i = 0; i < mForwardingReadResults.length; i++) {
766 if (mForwardingReadResults[i] == null) {
767 done = false;
768 break;
769 }
770 }
Andrew Lee1af6cf72014-11-04 17:35:26 -0800771
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700772 if (done) {
773 if (DBG) Log.d(LOG_TAG, "Done receiving fwd info");
Andrew Lee84024342014-11-06 23:37:09 -0800774 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_READING_DIALOG);
Andrew Lee1af6cf72014-11-04 17:35:26 -0800775
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700776 if (mReadingSettingsForDefaultProvider) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -0800777 VoicemailProviderSettingsUtil.save(mPhone.getContext(),
778 VoicemailProviderListPreference.DEFAULT_KEY,
Andrew Lee88b51e22014-10-29 15:48:51 -0700779 new VoicemailProviderSettings(this.mOldVmNumber, mForwardingReadResults));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700780 mReadingSettingsForDefaultProvider = false;
781 }
782 saveVoiceMailAndForwardingNumberStage2();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700783 }
784 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800785
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700786 private void resetForwardingChangeState() {
787 mForwardingChangeResults = new HashMap<Integer, AsyncResult>();
788 mExpectedChangeResultReasons = new HashSet<Integer>();
789 }
790
791 // Called after we are done saving the previous forwarding settings if
792 // we needed.
793 private void saveVoiceMailAndForwardingNumberStage2() {
794 mForwardingChangeResults = null;
795 mVoicemailChangeResult = null;
Andrew Leeb490d732014-10-27 15:00:41 -0700796 if (mNewFwdSettings != VoicemailProviderSettings.NO_FORWARDING) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700797 resetForwardingChangeState();
798 for (int i = 0; i < mNewFwdSettings.length; i++) {
799 CallForwardInfo fi = mNewFwdSettings[i];
Andrew Lee1af6cf72014-11-04 17:35:26 -0800800 CallForwardInfo fiForReason =
801 CallForwardInfoUtil.infoForReason(mForwardingReadResults, fi.reason);
802 final boolean doUpdate = CallForwardInfoUtil.isUpdateRequired(fiForReason, fi);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700803
804 if (doUpdate) {
805 if (DBG) log("Setting fwd #: " + i + ": " + fi.toString());
806 mExpectedChangeResultReasons.add(i);
807
Andrew Lee1af6cf72014-11-04 17:35:26 -0800808 CallForwardInfoUtil.setCallForwardingOption(mPhone, fi,
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700809 mSetOptionComplete.obtainMessage(
810 EVENT_FORWARDING_CHANGED, fi.reason, 0));
811 }
812 }
Andrew Lee84024342014-11-06 23:37:09 -0800813 showDialogIfForeground(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700814 } else {
815 if (DBG) log("Not touching fwd #");
816 setVMNumberWithCarrier();
817 }
818 }
819
820 private void setVMNumberWithCarrier() {
821 if (DBG) log("save voicemail #: " + mNewVMNumber);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800822
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700823 mPhone.setVoiceMailNumber(
824 mPhone.getVoiceMailAlphaTag().toString(),
825 mNewVMNumber,
826 Message.obtain(mSetOptionComplete, EVENT_VOICEMAIL_CHANGED));
827 }
828
829 /**
830 * Callback to handle option update completions
831 */
832 private final Handler mSetOptionComplete = new Handler() {
833 @Override
834 public void handleMessage(Message msg) {
835 AsyncResult result = (AsyncResult) msg.obj;
836 boolean done = false;
837 switch (msg.what) {
838 case EVENT_VOICEMAIL_CHANGED:
839 mVoicemailChangeResult = result;
Andrew Leee438b312014-10-29 16:59:15 -0700840 mVMChangeCompletedSuccessfully = isVmChangeSuccess();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700841 done = true;
842 break;
843 case EVENT_FORWARDING_CHANGED:
844 mForwardingChangeResults.put(msg.arg1, result);
845 if (result.exception != null) {
846 Log.w(LOG_TAG, "Error in setting fwd# " + msg.arg1 + ": " +
847 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700848 }
Andrew Leee438b312014-10-29 16:59:15 -0700849 if (isForwardingCompleted()) {
850 if (isFwdChangeSuccess()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700851 if (DBG) log("Overall fwd changes completed ok, starting vm change");
852 setVMNumberWithCarrier();
853 } else {
854 Log.w(LOG_TAG, "Overall fwd changes completed in failure. " +
855 "Check if we need to try rollback for some settings.");
856 mFwdChangesRequireRollback = false;
857 Iterator<Map.Entry<Integer,AsyncResult>> it =
858 mForwardingChangeResults.entrySet().iterator();
859 while (it.hasNext()) {
860 Map.Entry<Integer,AsyncResult> entry = it.next();
861 if (entry.getValue().exception == null) {
862 // If at least one succeeded we have to revert
863 Log.i(LOG_TAG, "Rollback will be required");
864 mFwdChangesRequireRollback = true;
865 break;
866 }
867 }
868 if (!mFwdChangesRequireRollback) {
869 Log.i(LOG_TAG, "No rollback needed.");
870 }
871 done = true;
872 }
873 }
874 break;
875 default:
876 // TODO: should never reach this, may want to throw exception
877 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800878
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700879 if (done) {
880 if (DBG) log("All VM provider related changes done");
881 if (mForwardingChangeResults != null) {
Andrew Lee84024342014-11-06 23:37:09 -0800882 dismissDialogSafely(VoicemailDialogUtil.VM_FWD_SAVING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700883 }
Andrew Leee438b312014-10-29 16:59:15 -0700884 handleSetVmOrFwdMessage();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700885 }
886 }
887 };
888
889 /**
890 * Callback to handle option revert completions
891 */
892 private final Handler mRevertOptionComplete = new Handler() {
893 @Override
894 public void handleMessage(Message msg) {
895 AsyncResult result = (AsyncResult) msg.obj;
896 switch (msg.what) {
897 case EVENT_VOICEMAIL_CHANGED:
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700898 if (DBG) log("VM revert complete msg");
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800899 mVoicemailChangeResult = result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700900 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800901
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700902 case EVENT_FORWARDING_CHANGED:
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800903 if (DBG) log("FWD revert complete msg ");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700904 mForwardingChangeResults.put(msg.arg1, result);
905 if (result.exception != null) {
906 if (DBG) log("Error in reverting fwd# " + msg.arg1 + ": " +
907 result.exception.getMessage());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700908 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700909 break;
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800910
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700911 default:
912 // TODO: should never reach this, may want to throw exception
913 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -0800914
915 final boolean done = (!mVMChangeCompletedSuccessfully || mVoicemailChangeResult != null)
916 && (!mFwdChangesRequireRollback || isForwardingCompleted());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700917 if (done) {
918 if (DBG) log("All VM reverts done");
Andrew Lee84024342014-11-06 23:37:09 -0800919 dismissDialogSafely(VoicemailDialogUtil.VM_REVERTING_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700920 onRevertDone();
921 }
922 }
923 };
924
925 /**
Andrew Leee438b312014-10-29 16:59:15 -0700926 * Return true if there is a change result for every reason for which we expect a result.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700927 */
Andrew Leee438b312014-10-29 16:59:15 -0700928 private boolean isForwardingCompleted() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700929 if (mForwardingChangeResults == null) {
Andrew Leee438b312014-10-29 16:59:15 -0700930 return true;
931 }
932
933 for (Integer reason : mExpectedChangeResultReasons) {
934 if (mForwardingChangeResults.get(reason) == null) {
935 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700936 }
937 }
Andrew Leee438b312014-10-29 16:59:15 -0700938
939 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700940 }
Andrew Leee438b312014-10-29 16:59:15 -0700941
942 private boolean isFwdChangeSuccess() {
943 if (mForwardingChangeResults == null) {
944 return true;
945 }
946
947 for (AsyncResult result : mForwardingChangeResults.values()) {
948 Throwable exception = result.exception;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700949 if (exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700950 String msg = exception.getMessage();
951 msg = (msg != null) ? msg : "";
952 Log.w(LOG_TAG, "Failed to change forwarding setting. Reason: " + msg);
953 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700954 }
955 }
Andrew Leee438b312014-10-29 16:59:15 -0700956 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700957 }
958
Andrew Leee438b312014-10-29 16:59:15 -0700959 private boolean isVmChangeSuccess() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700960 if (mVoicemailChangeResult.exception != null) {
Andrew Leee438b312014-10-29 16:59:15 -0700961 String msg = mVoicemailChangeResult.exception.getMessage();
962 msg = (msg != null) ? msg : "";
963 Log.w(LOG_TAG, "Failed to change voicemail. Reason: " + msg);
964 return false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700965 }
Andrew Leee438b312014-10-29 16:59:15 -0700966 return true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700967 }
968
Andrew Leee438b312014-10-29 16:59:15 -0700969 private void handleSetVmOrFwdMessage() {
970 if (DBG) log("handleSetVMMessage: set VM request complete");
971
972 if (!isFwdChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -0800973 handleVmOrFwdSetError(VoicemailDialogUtil.FWD_SET_RESPONSE_ERROR_DIALOG);
Andrew Leee438b312014-10-29 16:59:15 -0700974 } else if (!isVmChangeSuccess()) {
Andrew Lee84024342014-11-06 23:37:09 -0800975 handleVmOrFwdSetError(VoicemailDialogUtil.VM_RESPONSE_ERROR_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700976 } else {
Andrew Lee84024342014-11-06 23:37:09 -0800977 handleVmAndFwdSetSuccess(VoicemailDialogUtil.VM_CONFIRM_DIALOG);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700978 }
979 }
980
981 /**
982 * Called when Voicemail Provider or its forwarding settings failed. Rolls back partly made
983 * changes to those settings and show "failure" dialog.
984 *
Andrew Leeab082272014-11-04 15:50:42 -0800985 * @param dialogId ID of the dialog to show for the specific error case. Either
Andrew Lee84024342014-11-06 23:37:09 -0800986 * {@link #FWD_SET_RESPONSE_ERROR_DIALOG} or {@link #VM_RESPONSE_ERROR_DIALOG}
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700987 */
Andrew Leeab082272014-11-04 15:50:42 -0800988 private void handleVmOrFwdSetError(int dialogId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700989 if (mChangingVMorFwdDueToProviderChange) {
Andrew Leeab082272014-11-04 15:50:42 -0800990 mVMOrFwdSetError = dialogId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700991 mChangingVMorFwdDueToProviderChange = false;
992 switchToPreviousVoicemailProvider();
993 return;
994 }
995 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -0800996 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700997 updateVoiceNumberField();
998 }
999
1000 /**
1001 * Called when Voicemail Provider and its forwarding settings were successfully finished.
1002 * This updates a bunch of variables and show "success" dialog.
1003 */
Andrew Leeab082272014-11-04 15:50:42 -08001004 private void handleVmAndFwdSetSuccess(int dialogId) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001005 if (DBG) log("handleVmAndFwdSetSuccess: key is " + mVoicemailProviders.getKey());
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001006
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001007 mPreviousVMProviderKey = mVoicemailProviders.getKey();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001008 mChangingVMorFwdDueToProviderChange = false;
Andrew Leeab082272014-11-04 15:50:42 -08001009 showDialogIfForeground(dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001010 updateVoiceNumberField();
1011 }
1012
1013 /**
1014 * Update the voicemail number from what we've recorded on the sim.
1015 */
1016 private void updateVoiceNumberField() {
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001017 if (DBG) log("updateVoiceNumberField()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001018
1019 mOldVmNumber = mPhone.getVoiceMailNumber();
Andrew Lee2d5d1a42014-11-05 12:34:14 -08001020 if (TextUtils.isEmpty(mOldVmNumber)) {
1021 mSubMenuVoicemailSettings.setPhoneNumber("");
1022 mSubMenuVoicemailSettings.setSummary(getString(R.string.voicemail_number_not_set));
1023 } else {
1024 mSubMenuVoicemailSettings.setPhoneNumber(mOldVmNumber);
1025 mSubMenuVoicemailSettings.setSummary(mOldVmNumber);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001026 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001027 }
1028
1029 /*
1030 * Helper Methods for Activity class.
1031 * The initial query commands are split into two pieces now
1032 * for individual expansion. This combined with the ability
1033 * to cancel queries allows for a much better user experience,
1034 * and also ensures that the user only waits to update the
1035 * data that is relevant.
1036 */
1037
1038 @Override
1039 protected void onPrepareDialog(int id, Dialog dialog) {
1040 super.onPrepareDialog(id, dialog);
1041 mCurrentDialogId = id;
1042 }
1043
1044 // dialog creation method, called by showDialog()
1045 @Override
Andrew Lee84024342014-11-06 23:37:09 -08001046 protected Dialog onCreateDialog(int dialogId) {
1047 return VoicemailDialogUtil.getDialog(this, dialogId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001048 }
1049
1050 // This is a method implemented for DialogInterface.OnClickListener.
1051 // Used with the error dialog to close the app, voicemail dialog to just dismiss.
1052 // Close button is mapped to BUTTON_POSITIVE for the errors that close the activity,
1053 // while those that are mapped to BUTTON_NEUTRAL only move the preference focus.
1054 public void onClick(DialogInterface dialog, int which) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001055 if (DBG) log("onClick: button clicked is " + which);
1056
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001057 dialog.dismiss();
1058 switch (which){
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001059 case DialogInterface.BUTTON_NEGATIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001060 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001061 // We failed to get current forwarding settings and the user
1062 // does not wish to continue.
1063 switchToPreviousVoicemailProvider();
1064 }
1065 break;
1066 case DialogInterface.BUTTON_POSITIVE:
Andrew Lee84024342014-11-06 23:37:09 -08001067 if (mCurrentDialogId == VoicemailDialogUtil.FWD_GET_RESPONSE_ERROR_DIALOG) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001068 // We failed to get current forwarding settings but the user
1069 // wishes to continue changing settings to the new vm provider
1070 saveVoiceMailAndForwardingNumberStage2();
1071 } else {
1072 finish();
1073 }
1074 return;
1075 default:
1076 // just let the dialog close and go back to the input
1077 }
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001078
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001079 // In all dialogs, all buttons except BUTTON_POSITIVE lead to the end of user interaction
1080 // with settings UI. If we were called to explicitly configure voice mail then
1081 // we finish the settings activity here to come back to whatever the user was doing.
1082 if (getIntent().getAction().equals(ACTION_ADD_VOICEMAIL)) {
1083 finish();
1084 }
1085 }
1086
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001087 /*
1088 * Activity class methods
1089 */
1090
1091 @Override
1092 protected void onCreate(Bundle icicle) {
1093 super.onCreate(icicle);
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001094 if (DBG) log("onCreate: Intent is " + getIntent());
1095
Jay Shraunerc2684732014-11-12 12:10:37 -08001096 // Make sure we are running as the primary user.
1097 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
1098 Toast.makeText(this, R.string.call_settings_primary_user_only,
1099 Toast.LENGTH_SHORT).show();
1100 finish();
1101 return;
1102 }
1103
Tyler Gunnbaee2952014-09-10 16:01:02 -07001104 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
1105
Tyler Gunnbaee2952014-09-10 16:01:02 -07001106 // Show the voicemail preference in onResume if the calling intent specifies the
1107 // ACTION_ADD_VOICEMAIL action.
1108 mShowVoicemailPreference = (icicle == null) &&
Jay Shraunerbe2fb262014-11-11 15:19:58 -08001109 TextUtils.equals(getIntent().getAction(), ACTION_ADD_VOICEMAIL);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001110
1111 mSubscriptionInfoHelper = new SubscriptionInfoHelper(getIntent());
1112 mSubscriptionInfoHelper.setActionBarTitle(
1113 getActionBar(), getResources(), R.string.call_settings_with_label);
Andrew Lee1479dd12014-11-06 23:06:32 -08001114 mPhone = mSubscriptionInfoHelper.getPhone();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001115 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001116
1117 private void initPhoneAccountPreferences() {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001118 mPhoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001119
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001120 TelecomManager telecomManager = TelecomManager.from(this);
Andrew Lee93c345f2014-10-27 15:25:07 -07001121 TelephonyManager telephonyManager =
1122 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001123
Andrew Lee93c345f2014-10-27 15:25:07 -07001124 if ((telecomManager.getSimCallManagers().isEmpty() && !SipUtil.isVoipSupported(this))
1125 || telephonyManager.getPhoneCount() > 1) {
Andrew Leece8ae2a2014-09-10 10:41:48 -07001126 getPreferenceScreen().removePreference(mPhoneAccountSettingsPreference);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001127 }
1128 }
1129
Tyler Gunnbaee2952014-09-10 16:01:02 -07001130 @Override
1131 protected void onResume() {
1132 super.onResume();
1133 mForeground = true;
1134
1135 PreferenceScreen preferenceScreen = getPreferenceScreen();
1136 if (preferenceScreen != null) {
1137 preferenceScreen.removeAll();
1138 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001139
1140 addPreferencesFromResource(R.xml.call_feature_setting);
Andrew Lee5ed870c2014-10-29 11:47:49 -07001141
Andrew Leedb2fe562014-09-03 15:40:43 -07001142 initPhoneAccountPreferences();
1143
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001144 PreferenceScreen prefSet = getPreferenceScreen();
Andrew Lee64a7d792014-10-15 17:38:38 -07001145 mSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference(BUTTON_VOICEMAIL_KEY);
1146 mSubMenuVoicemailSettings.setParentActivity(this, VOICEMAIL_PREF_ID, this);
1147 mSubMenuVoicemailSettings.setDialogOnClosedListener(this);
1148 mSubMenuVoicemailSettings.setDialogTitle(R.string.voicemail_settings_number_label);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001149
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001150 mButtonDTMF = (ListPreference) findPreference(BUTTON_DTMF_KEY);
1151 mButtonAutoRetry = (CheckBoxPreference) findPreference(BUTTON_RETRY_KEY);
1152 mButtonHAC = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
1153 mButtonTTY = (ListPreference) findPreference(BUTTON_TTY_KEY);
Andrew Lee2170a972014-08-13 18:13:01 -07001154
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001155 mVoicemailProviders = (VoicemailProviderListPreference) findPreference(
1156 BUTTON_VOICEMAIL_PROVIDER_KEY);
1157 mVoicemailProviders.init(mPhone, getIntent());
Andrew Lee2c027892014-10-29 11:29:54 -07001158 mVoicemailProviders.setOnPreferenceChangeListener(this);
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001159 mPreviousVMProviderKey = mVoicemailProviders.getValue();
Andrew Lee8d66d812014-11-24 14:54:02 -08001160
Andrew Lee2c027892014-10-29 11:29:54 -07001161 mVoicemailSettingsScreen =
1162 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001163 mVoicemailSettings = (PreferenceScreen) findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
Andrew Lee8d66d812014-11-24 14:54:02 -08001164
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001165 mVoicemailNotificationRingtone = (VoicemailRingtonePreference) findPreference(
1166 getResources().getString(R.string.voicemail_notification_ringtone_key));
1167 mVoicemailNotificationRingtone.init(mPhone);
1168
Andrew Lee8d66d812014-11-24 14:54:02 -08001169 mVoicemailNotificationVibrate = (CheckBoxPreference) findPreference(
1170 getResources().getString(R.string.voicemail_notification_vibrate_key));
1171 mVoicemailNotificationVibrate.setOnPreferenceChangeListener(this);
1172
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001173 updateVMPreferenceWidgets(mVoicemailProviders.getValue());
1174
1175 mEnableVideoCalling = (CheckBoxPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001176
Andrew Lee64a7d792014-10-15 17:38:38 -07001177 if (getResources().getBoolean(R.bool.dtmf_type_enabled)) {
1178 mButtonDTMF.setOnPreferenceChangeListener(this);
1179 int dtmf = Settings.System.getInt(getContentResolver(),
1180 Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
1181 mButtonDTMF.setValueIndex(dtmf);
1182 } else {
1183 prefSet.removePreference(mButtonDTMF);
1184 mButtonDTMF = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001185 }
1186
Andrew Lee64a7d792014-10-15 17:38:38 -07001187 if (getResources().getBoolean(R.bool.auto_retry_enabled)) {
1188 mButtonAutoRetry.setOnPreferenceChangeListener(this);
1189 int autoretry = Settings.Global.getInt(
1190 getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
1191 mButtonAutoRetry.setChecked(autoretry != 0);
1192 } else {
1193 prefSet.removePreference(mButtonAutoRetry);
1194 mButtonAutoRetry = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001195 }
1196
Andrew Lee64a7d792014-10-15 17:38:38 -07001197 if (getResources().getBoolean(R.bool.hac_enabled)) {
1198 mButtonHAC.setOnPreferenceChangeListener(this);
1199 int hac = Settings.System.getInt(getContentResolver(), Settings.System.HEARING_AID, 0);
1200 mButtonHAC.setChecked(hac != 0);
1201 } else {
1202 prefSet.removePreference(mButtonHAC);
1203 mButtonHAC = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001204 }
1205
Andrew Lee64a7d792014-10-15 17:38:38 -07001206 TelecomManager telecomManager = TelecomManager.from(this);
1207 if (telecomManager != null && telecomManager.isTtySupported()) {
1208 mButtonTTY.setOnPreferenceChangeListener(this);
1209 int settingsTtyMode = Settings.Secure.getInt(getContentResolver(),
1210 Settings.Secure.PREFERRED_TTY_MODE,
1211 TelecomManager.TTY_MODE_OFF);
1212 mButtonTTY.setValue(Integer.toString(settingsTtyMode));
1213 updatePreferredTtyModeSummary(settingsTtyMode);
1214 } else {
1215 prefSet.removePreference(mButtonTTY);
1216 mButtonTTY = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001217 }
1218
1219 if (!getResources().getBoolean(R.bool.world_phone)) {
Andrew Lee2b36ba22014-11-05 17:08:49 -08001220 Preference cdmaOptions = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
1221 prefSet.removePreference(cdmaOptions);
1222
1223 // TODO: Support MSIM for this preference option.
1224 Preference gsmOptions = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
1225 prefSet.removePreference(gsmOptions);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001226
1227 int phoneType = mPhone.getPhoneType();
Andrew Lee5ed870c2014-10-29 11:47:49 -07001228 Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
Junda Liua09d6962014-11-12 13:39:02 -08001229 boolean shouldHideCarrierSettings = Settings.Global.getInt(
1230 getContentResolver(), Settings.Global.HIDE_CARRIER_NETWORK_SETTINGS, 0) == 1;
1231 if (shouldHideCarrierSettings) {
Andrew Lee5ed870c2014-10-29 11:47:49 -07001232 prefSet.removePreference(fdnButton);
Junda Liua09d6962014-11-12 13:39:02 -08001233 if (mButtonDTMF != null) {
1234 prefSet.removePreference(mButtonDTMF);
Etan Cohen0ca1c802014-07-07 15:35:48 -07001235 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001236 } else {
Junda Liua09d6962014-11-12 13:39:02 -08001237 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
1238 prefSet.removePreference(fdnButton);
1239
1240 if (!getResources().getBoolean(R.bool.config_voice_privacy_disable)) {
1241 addPreferencesFromResource(R.xml.cdma_call_privacy);
1242 }
1243 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
1244 fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(this, FdnSetting.class));
1245
1246 if (getResources().getBoolean(R.bool.config_additional_call_setting)) {
1247 addPreferencesFromResource(R.xml.gsm_umts_call_options);
1248
1249 Preference callForwardingPref = prefSet.findPreference(CALL_FORWARDING_KEY);
1250 callForwardingPref.setIntent(mSubscriptionInfoHelper.getIntent(
1251 this, GsmUmtsCallForwardOptions.class));
1252
1253 Preference additionalGsmSettingsPref =
1254 prefSet.findPreference(ADDITIONAL_GSM_SETTINGS_KEY);
1255 additionalGsmSettingsPref.setIntent(mSubscriptionInfoHelper.getIntent(
1256 this, GsmUmtsAdditionalCallOptions.class));
1257 }
1258 } else {
1259 throw new IllegalStateException("Unexpected phone type: " + phoneType);
1260 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001261 }
1262 }
1263
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001264 // check the intent that started this activity and pop up the voicemail
1265 // dialog if we've been asked to.
1266 // If we have at least one non default VM provider registered then bring up
1267 // the selection for the VM provider, otherwise bring up a VM number dialog.
1268 // We only bring up the dialog the first time we are called (not after orientation change)
Andrew Lee2c027892014-10-29 11:29:54 -07001269 if (mShowVoicemailPreference) {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001270 if (DBG) log("ACTION_ADD_VOICEMAIL Intent is thrown");
1271 if (mVoicemailProviders.hasMoreThanOneVoicemailProvider()) {
1272 if (DBG) log("Voicemail data has more than one provider.");
Tyler Gunnbaee2952014-09-10 16:01:02 -07001273 simulatePreferenceClick(mVoicemailProviders);
1274 } else {
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001275 onPreferenceChange(mVoicemailProviders, VoicemailProviderListPreference.DEFAULT_KEY);
1276 mVoicemailProviders.setValue(VoicemailProviderListPreference.DEFAULT_KEY);
Tyler Gunnbaee2952014-09-10 16:01:02 -07001277 }
1278 mShowVoicemailPreference = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001279 }
Tyler Gunnbaee2952014-09-10 16:01:02 -07001280
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001281 updateVoiceNumberField();
1282 mVMProviderSettingsForced = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001283
Andrew Lee8d66d812014-11-24 14:54:02 -08001284 mVoicemailNotificationVibrate.setChecked(
Andrew Lee1ec7c7e2014-12-03 15:33:21 -08001285 VoicemailNotificationSettingsUtil.isVibrationEnabled(mPhone));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001286
Andrew Lee312e8172014-10-23 17:01:36 -07001287 if (ImsManager.isVtEnabledByPlatform(mPhone.getContext()) && ENABLE_VT_FLAG) {
1288 boolean currentValue =
1289 ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
1290 ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled() : false;
1291 mEnableVideoCalling.setChecked(currentValue);
Andrew Lee77527ac2014-10-21 16:57:39 -07001292 mEnableVideoCalling.setOnPreferenceChangeListener(this);
1293 } else {
1294 prefSet.removePreference(mEnableVideoCalling);
1295 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001296 }
1297
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001298 private void handleTTYChange(Preference preference, Object objValue) {
1299 int buttonTtyMode;
1300 buttonTtyMode = Integer.valueOf((String) objValue).intValue();
1301 int settingsTtyMode = android.provider.Settings.Secure.getInt(
1302 getContentResolver(),
Sailesh Nepalbf900542014-07-15 16:18:32 -07001303 android.provider.Settings.Secure.PREFERRED_TTY_MODE,
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001304 TelecomManager.TTY_MODE_OFF);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001305 if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
1306 Integer.toString(buttonTtyMode));
1307
1308 if (buttonTtyMode != settingsTtyMode) {
1309 switch(buttonTtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001310 case TelecomManager.TTY_MODE_OFF:
1311 case TelecomManager.TTY_MODE_FULL:
1312 case TelecomManager.TTY_MODE_HCO:
1313 case TelecomManager.TTY_MODE_VCO:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001314 android.provider.Settings.Secure.putInt(getContentResolver(),
1315 android.provider.Settings.Secure.PREFERRED_TTY_MODE, buttonTtyMode);
1316 break;
1317 default:
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001318 buttonTtyMode = TelecomManager.TTY_MODE_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001319 }
1320
1321 mButtonTTY.setValue(Integer.toString(buttonTtyMode));
1322 updatePreferredTtyModeSummary(buttonTtyMode);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001323 Intent ttyModeChanged = new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
1324 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001325 sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
1326 }
1327 }
1328
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001329 private void updatePreferredTtyModeSummary(int TtyMode) {
1330 String [] txts = getResources().getStringArray(R.array.tty_mode_entries);
1331 switch(TtyMode) {
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001332 case TelecomManager.TTY_MODE_OFF:
1333 case TelecomManager.TTY_MODE_HCO:
1334 case TelecomManager.TTY_MODE_VCO:
1335 case TelecomManager.TTY_MODE_FULL:
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001336 mButtonTTY.setSummary(txts[TtyMode]);
1337 break;
1338 default:
1339 mButtonTTY.setEnabled(false);
Tyler Gunn4d45d1c2014-09-12 22:17:53 -07001340 mButtonTTY.setSummary(txts[TelecomManager.TTY_MODE_OFF]);
Sailesh Nepalbf900542014-07-15 16:18:32 -07001341 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001342 }
1343 }
1344
1345 private static void log(String msg) {
1346 Log.d(LOG_TAG, msg);
1347 }
1348
1349 /**
1350 * Updates the look of the VM preference widgets based on current VM provider settings.
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001351 * Note that the provider name is loaded fxrorm the found activity via loadLabel in
1352 * {@link VoicemailProviderListPreference#initVoiceMailProviders()} in order for it to be
1353 * localizable.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001354 */
1355 private void updateVMPreferenceWidgets(String currentProviderSetting) {
1356 final String key = currentProviderSetting;
Andrew Lee64e5c9d2014-11-26 12:00:10 -08001357 final VoicemailProvider provider = mVoicemailProviders.getVoicemailProvider(key);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001358
1359 /* This is the case when we are coming up on a freshly wiped phone and there is no
1360 persisted value for the list preference mVoicemailProviders.
1361 In this case we want to show the UI asking the user to select a voicemail provider as
1362 opposed to silently falling back to default one. */
1363 if (provider == null) {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001364 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> null.");
1365
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001366 mVoicemailProviders.setSummary(getString(R.string.sum_voicemail_choose_provider));
1367 mVoicemailSettings.setEnabled(false);
1368 mVoicemailSettings.setIntent(null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001369 mVoicemailNotificationVibrate.setEnabled(false);
1370 } else {
Andrew Lee4ddb9bb2014-11-05 15:03:20 -08001371 if (DBG) log("updateVMPreferenceWidget: key: " + key + " -> " + provider.toString());
1372
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001373 final String providerName = provider.name;
1374 mVoicemailProviders.setSummary(providerName);
1375 mVoicemailSettings.setEnabled(true);
1376 mVoicemailSettings.setIntent(provider.intent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001377 mVoicemailNotificationVibrate.setEnabled(true);
1378 }
1379 }
1380
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001381
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001382 /**
1383 * Simulates user clicking on a passed preference.
1384 * Usually needed when the preference is a dialog preference and we want to invoke
1385 * a dialog for this preference programmatically.
Santos Cordonda120f42014-08-06 04:44:34 -07001386 * TODO: figure out if there is a cleaner way to cause preference dlg to come up
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001387 */
1388 private void simulatePreferenceClick(Preference preference) {
1389 // Go through settings until we find our setting
1390 // and then simulate a click on it to bring up the dialog
1391 final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
1392 for (int idx = 0; idx < adapter.getCount(); idx++) {
1393 if (adapter.getItem(idx) == preference) {
1394 getPreferenceScreen().onItemClick(this.getListView(),
1395 null, idx, adapter.getItemId(idx));
1396 break;
1397 }
1398 }
1399 }
1400
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001401 @Override
1402 public boolean onOptionsItemSelected(MenuItem item) {
1403 final int itemId = item.getItemId();
1404 if (itemId == android.R.id.home) { // See ActionBar#setDisplayHomeAsUpEnabled()
Yorke Leef2d0cac2013-09-09 19:42:56 -07001405 onBackPressed();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001406 return true;
1407 }
1408 return super.onOptionsItemSelected(item);
1409 }
Andrew Lee8d66d812014-11-24 14:54:02 -08001410
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001411 /**
1412 * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
1413 * This is useful for implementing "HomeAsUp" capability for second-level Settings.
1414 */
Andrew Lee5efb1122014-12-05 04:20:42 -08001415 public static void goUpToTopLevelSetting(
1416 Activity activity, SubscriptionInfoHelper subscriptionInfoHelper) {
1417 Intent intent = subscriptionInfoHelper.getIntent(activity, CallFeaturesSetting.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001418 intent.setAction(Intent.ACTION_MAIN);
1419 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1420 activity.startActivity(intent);
1421 activity.finish();
1422 }
1423}