1 /*
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 
17 package com.android.phone;
18 
19 import android.app.ActionBar;
20 import android.app.Activity;
21 import android.app.AlertDialog;
22 import android.app.Dialog;
23 import android.content.BroadcastReceiver;
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.content.Intent;
28 import android.content.IntentFilter;
29 import android.content.pm.PackageManager;
30 import android.content.pm.PackageManager.NameNotFoundException;
31 import android.content.pm.ResolveInfo;
32 import android.content.res.Resources;
33 import android.os.Bundle;
34 import android.os.Handler;
35 import android.os.HandlerExecutor;
36 import android.os.Looper;
37 import android.os.PersistableBundle;
38 import android.os.UserManager;
39 import android.preference.Preference;
40 import android.preference.PreferenceActivity;
41 import android.preference.PreferenceScreen;
42 import android.preference.SwitchPreference;
43 import android.provider.Settings;
44 import android.telecom.PhoneAccountHandle;
45 import android.telecom.TelecomManager;
46 import android.telephony.CarrierConfigManager;
47 import android.telephony.SubscriptionManager;
48 import android.telephony.TelephonyCallback;
49 import android.telephony.TelephonyManager;
50 import android.telephony.ims.ProvisioningManager;
51 import android.telephony.ims.feature.ImsFeature;
52 import android.util.Log;
53 import android.view.MenuItem;
54 import android.widget.Toast;
55 
56 import com.android.ims.ImsConfig;
57 import com.android.ims.ImsException;
58 import com.android.ims.ImsManager;
59 import com.android.internal.telephony.Phone;
60 import com.android.internal.telephony.PhoneConstants;
61 import com.android.internal.telephony.flags.Flags;
62 import com.android.phone.settings.PhoneAccountSettingsFragment;
63 import com.android.phone.settings.SuppServicesUiUtil;
64 import com.android.phone.settings.VoicemailSettingsActivity;
65 import com.android.phone.settings.fdn.FdnSetting;
66 
67 import java.util.List;
68 
69 /**
70  * Top level "Call settings" UI; see res/xml/call_feature_setting.xml
71  *
72  * This preference screen is the root of the "Call settings" hierarchy available from the Phone
73  * app; the settings here let you control various features related to phone calls (including
74  * voicemail settings, the "Respond via SMS" feature, and others.)  It's used only on
75  * voice-capable phone devices.
76  *
77  * Note that this activity is part of the package com.android.phone, even
78  * though you reach it from the "Phone" app (i.e. DialtactsActivity) which
79  * is from the package com.android.contacts.
80  *
81  * For the "Mobile network settings" screen under the main Settings app,
82  * See {@link com.android.settings.network.telephony.MobileNetworkActivity}.
83  */
84 public class CallFeaturesSetting extends PreferenceActivity
85         implements Preference.OnPreferenceChangeListener {
86     private static final String LOG_TAG = "CallFeaturesSetting";
87     private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
88 
89     // String keys for preference lookup
90     // TODO: Naming these "BUTTON_*" is confusing since they're not actually buttons(!)
91     // TODO: Consider moving these strings to strings.xml, so that they are not duplicated here and
92     // in the layout files. These strings need to be treated carefully; if the setting is
93     // persistent, they are used as the key to store shared preferences and the name should not be
94     // changed unless the settings are also migrated.
95     private static final String VOICEMAIL_SETTING_SCREEN_PREF_KEY = "button_voicemail_category_key";
96     private static final String BUTTON_FDN_KEY   = "button_fdn_key";
97     private static final String BUTTON_RETRY_KEY       = "button_auto_retry_key";
98     private static final String BUTTON_GSM_UMTS_OPTIONS = "button_gsm_more_expand_key";
99     private static final String BUTTON_CDMA_OPTIONS = "button_cdma_more_expand_key";
100 
101     private static final String PHONE_ACCOUNT_SETTINGS_KEY =
102             "phone_account_settings_preference_screen";
103 
104     private static final String ENABLE_VIDEO_CALLING_KEY = "button_enable_video_calling";
105     private static final String BUTTON_VP_KEY = "button_voice_privacy_key";
106 
107     private Phone mPhone;
108     private ImsManager mImsMgr;
109     private SubscriptionInfoHelper mSubscriptionInfoHelper;
110     private TelecomManager mTelecomManager;
111     private TelephonyCallback mTelephonyCallback;
112 
113     private SwitchPreference mButtonAutoRetry;
114     private PreferenceScreen mVoicemailSettingsScreen;
115     private SwitchPreference mEnableVideoCalling;
116     private Preference mButtonWifiCalling;
117     private boolean mDisallowedConfig = false;
118 
119     /*
120      * Click Listeners, handle click based on objects attached to UI.
121      */
122 
123     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
124         @Override
125         public void onReceive(Context context, Intent intent) {
126             log("onReceive: " + intent.getAction());
127 
128             if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
129                 log("ACTION_AIRPLANE_MODE_CHANGED");
130 
131                 boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
132                 handleAirplaneModeChange(isAirplaneModeOn);
133             }
134         }
135     };
136 
handleAirplaneModeChange(boolean isAirplaneModeOn)137     private void handleAirplaneModeChange(boolean isAirplaneModeOn) {
138         PersistableBundle b = null;
139         if (mSubscriptionInfoHelper.hasSubId()) {
140             b = PhoneGlobals.getInstance().getCarrierConfigForSubId(
141                     mSubscriptionInfoHelper.getSubId());
142         } else {
143             b = PhoneGlobals.getInstance().getCarrierConfig();
144         }
145 
146         if (b != null && b.getBoolean(
147                 CarrierConfigManager.KEY_DISABLE_SUPPLEMENTARY_SERVICES_IN_AIRPLANE_MODE_BOOL)) {
148             PreferenceScreen preferenceScreen = getPreferenceScreen();
149             Preference callForwarding = preferenceScreen.findPreference(
150                     GsmUmtsCallOptions.CALL_FORWARDING_KEY);
151             Preference callBarring = preferenceScreen.findPreference(
152                     GsmUmtsCallOptions.CALL_BARRING_KEY);
153             Preference additional = preferenceScreen.findPreference(
154                     GsmUmtsCallOptions.ADDITIONAL_GSM_SETTINGS_KEY);
155             if (callForwarding != null) {
156                 callForwarding.setEnabled(!isAirplaneModeOn);
157             }
158             if (callBarring != null) {
159                 callBarring.setEnabled(!isAirplaneModeOn);
160             }
161             if (additional != null) {
162                 additional.setEnabled(!isAirplaneModeOn);
163             }
164         }
165     }
166 
167     // Click listener for all toggle events
168     @Override
onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)169     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
170         if (preference == mButtonAutoRetry) {
171             android.provider.Settings.Global.putInt(mPhone.getContext().getContentResolver(),
172                     android.provider.Settings.Global.CALL_AUTO_RETRY,
173                     mButtonAutoRetry.isChecked() ? 1 : 0);
174             return true;
175         } else if (preference == preferenceScreen.findPreference(
176                 GsmUmtsCallOptions.CALL_FORWARDING_KEY)) {
177             return doSsOverUtPrecautions(preference);
178         } else if (preference == preferenceScreen.findPreference(
179                 GsmUmtsCallOptions.CALL_BARRING_KEY)) {
180             return doSsOverUtPrecautions(preference);
181         }
182         return false;
183     }
184 
doSsOverUtPrecautions(Preference preference)185     private boolean doSsOverUtPrecautions(Preference preference) {
186         PersistableBundle b = null;
187         if (mSubscriptionInfoHelper.hasSubId()) {
188             b = PhoneGlobals.getInstance().getCarrierConfigForSubId(
189                     mSubscriptionInfoHelper.getSubId());
190         } else {
191             b = PhoneGlobals.getInstance().getCarrierConfig();
192         }
193 
194         String configKey;
195         if (preference.getKey().equals(GsmUmtsCallOptions.CALL_FORWARDING_KEY)) {
196             configKey = CarrierConfigManager.KEY_CALL_FORWARDING_OVER_UT_WARNING_BOOL;
197         } else {
198             configKey = CarrierConfigManager.KEY_CALL_BARRING_OVER_UT_WARNING_BOOL;
199         }
200         if (b != null && b.getBoolean(configKey)
201                 && mPhone != null
202                 && SuppServicesUiUtil.isSsOverUtPrecautions(this, mPhone)) {
203             SuppServicesUiUtil.showBlockingSuppServicesDialog(this, mPhone,
204                     preference.getKey()).show();
205             return true;
206         }
207         return false;
208     }
209 
210     /**
211      * Implemented to support onPreferenceChangeListener to look for preference
212      * changes.
213      *
214      * @param preference is the preference to be changed
215      * @param objValue should be the value of the selection, NOT its localized
216      * display value.
217      */
218     @Override
onPreferenceChange(Preference preference, Object objValue)219     public boolean onPreferenceChange(Preference preference, Object objValue) {
220         if (DBG) log("onPreferenceChange: \"" + preference + "\" changed to \"" + objValue + "\"");
221 
222         if (preference == mEnableVideoCalling) {
223             if (mImsMgr.isEnhanced4gLteModeSettingEnabledByUser()) {
224                 mImsMgr.setVtSetting((boolean) objValue);
225             } else {
226                 AlertDialog.Builder builder = FrameworksUtils.makeAlertDialogBuilder(this);
227                 DialogInterface.OnClickListener networkSettingsClickListener =
228                         new Dialog.OnClickListener() {
229                             @Override
230                             public void onClick(DialogInterface dialog, int which) {
231                                 Intent intent = new Intent();
232                                 ComponentName mobileNetworkSettingsComponent = new ComponentName(
233                                         getString(R.string.mobile_network_settings_package),
234                                         getString(R.string.mobile_network_settings_class));
235                                 intent.setComponent(mobileNetworkSettingsComponent);
236                                 startActivity(intent);
237                             }
238                         };
239                 builder.setMessage(getResourcesForSubId().getString(
240                                 R.string.enable_video_calling_dialog_msg))
241                         .setNeutralButton(getResourcesForSubId().getString(
242                                 R.string.enable_video_calling_dialog_settings),
243                                 networkSettingsClickListener)
244                         .setPositiveButton(android.R.string.ok, null)
245                         .show();
246                 return false;
247             }
248         }
249 
250         // Always let the preference setting proceed.
251         return true;
252     }
253 
254     @Override
onCreate(Bundle icicle)255     protected void onCreate(Bundle icicle) {
256         super.onCreate(icicle);
257         if (DBG) log("onCreate: Intent is " + getIntent());
258 
259         // Make sure we are running as an admin user.
260         UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
261         if (!userManager.isAdminUser()) {
262             Toast.makeText(this, R.string.call_settings_admin_user_only,
263                     Toast.LENGTH_SHORT).show();
264             finish();
265             return;
266         }
267 
268         // Check if mobile network configs are restricted.
269         if (Flags.ensureAccessToCallSettingsIsRestricted() &&
270                 userManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
271             mDisallowedConfig = true;
272             Log.i(LOG_TAG, "Mobile network configs are restricted, disabling mobile network "
273                     + "settings");
274         }
275 
276         mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent());
277         mPhone = mSubscriptionInfoHelper.getPhone();
278         mSubscriptionInfoHelper.setActionBarTitle(
279                 getActionBar(), getResourcesForSubId(), R.string.call_settings_with_label);
280         mTelecomManager = getSystemService(TelecomManager.class);
281         mTelephonyCallback = new CallFeaturesTelephonyCallback();
282 
283         ActionBar actionBar = getActionBar();
284         if (actionBar != null) {
285             // sets the talkback voice prompt to say "Back" instead of "Navigate Up"
286             actionBar.setHomeActionContentDescription(R.string.ota_call_end);
287         }
288     }
289 
updateImsManager(Phone phone)290     private void updateImsManager(Phone phone) {
291         log("updateImsManager :: phone.getContext()=" + phone.getContext()
292                 + " phone.getPhoneId()=" + phone.getPhoneId());
293         mImsMgr = ImsManager.getInstance(phone.getContext(), phone.getPhoneId());
294         if (mImsMgr == null) {
295             log("updateImsManager :: Could not get ImsManager instance!");
296         } else {
297             log("updateImsManager :: mImsMgr=" + mImsMgr);
298         }
299     }
300 
listenPhoneState(boolean listen)301     private void listenPhoneState(boolean listen) {
302         TelephonyManager telephonyManager = getSystemService(TelephonyManager.class)
303                 .createForSubscriptionId(mPhone.getSubId());
304         if (listen) {
305             telephonyManager.registerTelephonyCallback(
306                     new HandlerExecutor(new Handler(Looper.getMainLooper())), mTelephonyCallback);
307         } else {
308             telephonyManager.unregisterTelephonyCallback(mTelephonyCallback);
309         }
310     }
311 
312     private final class CallFeaturesTelephonyCallback extends TelephonyCallback implements
313             TelephonyCallback.CallStateListener {
314         @Override
onCallStateChanged(int state)315         public void onCallStateChanged(int state) {
316             if (DBG) log("PhoneStateListener onCallStateChanged: state is " + state);
317             boolean isCallStateIdle = state == TelephonyManager.CALL_STATE_IDLE;
318             if (mEnableVideoCalling != null) {
319                 mEnableVideoCalling.setEnabled(isCallStateIdle);
320             }
321             if (mButtonWifiCalling != null) {
322                 mButtonWifiCalling.setEnabled(isCallStateIdle);
323             }
324         }
325     }
326 
327     private final ProvisioningManager.Callback mProvisioningCallback =
328             new ProvisioningManager.Callback() {
329         @Override
330         public void onProvisioningIntChanged(int item, int value) {
331             if (item == ImsConfig.ConfigConstants.VOICE_OVER_WIFI_SETTING_ENABLED
332                     || item == ImsConfig.ConfigConstants.VLT_SETTING_ENABLED
333                     || item == ImsConfig.ConfigConstants.LVC_SETTING_ENABLED) {
334                 updateVtWfc();
335             }
336         }
337     };
338 
339     @Override
onPause()340     protected void onPause() {
341         super.onPause();
342         listenPhoneState(false);
343         unregisterReceiver(mReceiver);
344 
345         // Remove callback for provisioning changes.
346         try {
347             if (mImsMgr != null) {
348                 mImsMgr.getConfigInterface().removeConfigCallback(
349                         mProvisioningCallback.getBinder());
350             }
351         } catch (ImsException e) {
352             Log.w(LOG_TAG, "onPause: Unable to remove callback for provisioning changes");
353         }
354     }
355 
356     @Override
onResume()357     protected void onResume() {
358         super.onResume();
359 
360         updateImsManager(mPhone);
361         listenPhoneState(true);
362         PreferenceScreen preferenceScreen = getPreferenceScreen();
363         if (preferenceScreen != null) {
364             preferenceScreen.removeAll();
365         }
366 
367         addPreferencesFromResource(R.xml.call_feature_setting);
368 
369         TelephonyManager telephonyManager = getSystemService(TelephonyManager.class)
370                 .createForSubscriptionId(mPhone.getSubId());
371 
372         // Note: The PhoneAccountSettingsActivity accessible via the
373         // android.telecom.action.CHANGE_PHONE_ACCOUNTS intent is accessible directly from
374         // the AOSP Dialer settings page on multi-sim devices.
375         // Where a device does NOT make the PhoneAccountSettingsActivity directly accessible from
376         // its Dialer app, this check must be modified in the device's AOSP branch to ensure that
377         // the PhoneAccountSettingsActivity is always accessible.
378         if (telephonyManager.isMultiSimEnabled()) {
379             Preference phoneAccountSettingsPreference = findPreference(PHONE_ACCOUNT_SETTINGS_KEY);
380             getPreferenceScreen().removePreference(phoneAccountSettingsPreference);
381         }
382 
383         PreferenceScreen prefSet = getPreferenceScreen();
384         mVoicemailSettingsScreen =
385                 (PreferenceScreen) findPreference(VOICEMAIL_SETTING_SCREEN_PREF_KEY);
386         mVoicemailSettingsScreen.setIntent(mSubscriptionInfoHelper.getIntent(
387                 VoicemailSettingsActivity.class));
388 
389         maybeHideVoicemailSettings();
390 
391         mButtonAutoRetry = (SwitchPreference) findPreference(BUTTON_RETRY_KEY);
392 
393         mEnableVideoCalling = (SwitchPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
394         mButtonWifiCalling = findPreference(getResourcesForSubId().getString(
395                 R.string.wifi_calling_settings_key));
396 
397         PersistableBundle carrierConfig =
398                 PhoneGlobals.getInstance().getCarrierConfigForSubId(mPhone.getSubId());
399 
400         if (carrierConfig.getBoolean(CarrierConfigManager.KEY_AUTO_RETRY_ENABLED_BOOL)) {
401             mButtonAutoRetry.setOnPreferenceChangeListener(this);
402             int autoretry = Settings.Global.getInt(
403                     getContentResolver(), Settings.Global.CALL_AUTO_RETRY, 0);
404             mButtonAutoRetry.setChecked(autoretry != 0);
405         } else {
406             prefSet.removePreference(mButtonAutoRetry);
407             mButtonAutoRetry = null;
408         }
409 
410         Preference cdmaOptions = prefSet.findPreference(BUTTON_CDMA_OPTIONS);
411         Preference gsmOptions = prefSet.findPreference(BUTTON_GSM_UMTS_OPTIONS);
412         Preference fdnButton = prefSet.findPreference(BUTTON_FDN_KEY);
413         fdnButton.setIntent(mSubscriptionInfoHelper.getIntent(FdnSetting.class));
414         if (carrierConfig.getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL)) {
415             cdmaOptions.setIntent(mSubscriptionInfoHelper.getIntent(CdmaCallOptions.class));
416             gsmOptions.setIntent(mSubscriptionInfoHelper.getIntent(GsmUmtsCallOptions.class));
417         } else {
418             // Remove GSM options and repopulate the preferences in this Activity if phone type is
419             // GSM.
420             prefSet.removePreference(gsmOptions);
421 
422             int phoneType = mPhone.getPhoneType();
423             if (carrierConfig.getBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL)) {
424                 prefSet.removePreference(fdnButton);
425             } else {
426                 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
427                     // For now, just keep CdmaCallOptions as one entity. Eventually CDMA should
428                     // follow the same pattern as GSM below, where VP and Call forwarding are
429                     // populated here and Call waiting is populated in another "Additional Settings"
430                     // submenu for CDMA.
431                     prefSet.removePreference(fdnButton);
432                     cdmaOptions.setSummary(null);
433                     cdmaOptions.setTitle(R.string.additional_gsm_call_settings);
434                     cdmaOptions.setIntent(mSubscriptionInfoHelper.getIntent(CdmaCallOptions.class));
435                 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
436                     prefSet.removePreference(cdmaOptions);
437                     if (mPhone.getIccCard() == null || !mPhone.getIccCard().getIccFdnAvailable()) {
438                         prefSet.removePreference(fdnButton);
439                     }
440                     if (carrierConfig.getBoolean(
441                             CarrierConfigManager.KEY_ADDITIONAL_CALL_SETTING_BOOL)) {
442                         addPreferencesFromResource(R.xml.gsm_umts_call_options);
443                         GsmUmtsCallOptions.init(prefSet, mSubscriptionInfoHelper);
444                     }
445                 } else {
446                     throw new IllegalStateException("Unexpected phone type: " + phoneType);
447                 }
448             }
449         }
450         updateVtWfc();
451 
452         // Register callback for provisioning changes.
453         try {
454             if (mImsMgr != null) {
455                 mImsMgr.getConfigInterface().addConfigCallback(mProvisioningCallback);
456             }
457         } catch (ImsException e) {
458             Log.w(LOG_TAG, "onResume: Unable to register callback for provisioning changes.");
459         }
460 
461         IntentFilter intentFilter =
462                 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
463         registerReceiver(mReceiver, intentFilter);
464     }
465 
updateVtWfc()466     private void updateVtWfc() {
467         PreferenceScreen prefSet = getPreferenceScreen();
468         TelephonyManager telephonyManager = getSystemService(TelephonyManager.class)
469                 .createForSubscriptionId(mPhone.getSubId());
470         PersistableBundle carrierConfig =
471                 PhoneGlobals.getInstance().getCarrierConfigForSubId(mPhone.getSubId());
472         boolean useWfcHomeModeForRoaming = carrierConfig.getBoolean(
473                     CarrierConfigManager.KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL,
474                     false);
475         boolean isDataEnabled;
476         isDataEnabled = mPhone.getDataSettingsManager().isDataEnabled();
477         if (mImsMgr.isVtEnabledByPlatform() && mImsMgr.isVtProvisionedOnDevice()
478                 && (carrierConfig.getBoolean(
479                         CarrierConfigManager.KEY_IGNORE_DATA_ENABLED_CHANGED_FOR_VIDEO_CALLS)
480                 || isDataEnabled) && !mDisallowedConfig) {
481             boolean currentValue =
482                     mImsMgr.isEnhanced4gLteModeSettingEnabledByUser()
483                     ? mImsMgr.isVtEnabledByUser() : false;
484             mEnableVideoCalling.setChecked(currentValue);
485             mEnableVideoCalling.setOnPreferenceChangeListener(this);
486             prefSet.addPreference(mEnableVideoCalling);
487         } else {
488             prefSet.removePreference(mEnableVideoCalling);
489         }
490 
491         final PhoneAccountHandle simCallManager = mTelecomManager.getSimCallManagerForSubscription(
492                 mPhone.getSubId());
493         if (simCallManager != null) {
494             Intent intent = PhoneAccountSettingsFragment.buildPhoneAccountConfigureIntent(
495                     this, simCallManager);
496             if (intent != null) {
497                 PackageManager pm = mPhone.getContext().getPackageManager();
498                 List<ResolveInfo> resolutions = pm.queryIntentActivities(intent, 0);
499                 if (!resolutions.isEmpty()) {
500                     mButtonWifiCalling.setTitle(resolutions.get(0).loadLabel(pm));
501                     mButtonWifiCalling.setSummary(null);
502                     mButtonWifiCalling.setIntent(intent);
503                     prefSet.addPreference(mButtonWifiCalling);
504                 } else {
505                     prefSet.removePreference(mButtonWifiCalling);
506                 }
507             } else {
508                 prefSet.removePreference(mButtonWifiCalling);
509             }
510         } else if (!mImsMgr.isWfcEnabledByPlatform() || !mImsMgr.isWfcProvisionedOnDevice()) {
511             prefSet.removePreference(mButtonWifiCalling);
512         } else {
513             String title = getResourcesForSubId().getString(R.string.wifi_calling);
514             mButtonWifiCalling.setTitle(title);
515 
516             int resId = com.android.internal.R.string.wifi_calling_off_summary;
517             if (mImsMgr.isWfcEnabledByUser()) {
518                 boolean isRoaming = telephonyManager.isNetworkRoaming();
519                 // Also check carrier config for roaming mode
520                 int wfcMode = mImsMgr.getWfcMode(isRoaming && !useWfcHomeModeForRoaming);
521                 switch (wfcMode) {
522                     case ImsConfig.WfcModeFeatureValueConstants.WIFI_ONLY:
523                         resId = com.android.internal.R.string.wfc_mode_wifi_only_summary;
524                         break;
525                     case ImsConfig.WfcModeFeatureValueConstants.CELLULAR_PREFERRED:
526                         resId = com.android.internal.R.string.wfc_mode_cellular_preferred_summary;
527                         break;
528                     case ImsConfig.WfcModeFeatureValueConstants.WIFI_PREFERRED:
529                         resId = com.android.internal.R.string.wfc_mode_wifi_preferred_summary;
530                         break;
531                     default:
532                         if (DBG) log("Unexpected WFC mode value: " + wfcMode);
533                 }
534             }
535             mButtonWifiCalling.setSummary(getResourcesForSubId().getString(resId));
536             Intent intent = mButtonWifiCalling.getIntent();
537             if (intent != null) {
538                 intent.putExtra(Settings.EXTRA_SUB_ID, mPhone.getSubId());
539             }
540             prefSet.addPreference(mButtonWifiCalling);
541         }
542 
543         try {
544             if (mImsMgr.getImsServiceState() != ImsFeature.STATE_READY) {
545                 log("Feature state not ready so remove vt and wfc settings for "
546                         + " phone =" + mPhone.getPhoneId());
547                 prefSet.removePreference(mButtonWifiCalling);
548                 prefSet.removePreference(mEnableVideoCalling);
549             }
550         } catch (ImsException ex) {
551             log("Exception when trying to get ImsServiceStatus: " + ex);
552             prefSet.removePreference(mButtonWifiCalling);
553             prefSet.removePreference(mEnableVideoCalling);
554         }
555     }
556 
557     /**
558      * Hides the top level voicemail settings entry point if the default dialer contains a
559      * particular manifest metadata key. This is required when the default dialer wants to display
560      * its own version of voicemail settings.
561      */
maybeHideVoicemailSettings()562     private void maybeHideVoicemailSettings() {
563         String defaultDialer = getSystemService(TelecomManager.class).getDefaultDialerPackage();
564         if (defaultDialer == null) {
565             return;
566         }
567         try {
568             Bundle metadata = getPackageManager()
569                     .getApplicationInfo(defaultDialer, PackageManager.GET_META_DATA).metaData;
570             if (metadata == null) {
571                 return;
572             }
573             if (!metadata
574                     .getBoolean(TelephonyManager.METADATA_HIDE_VOICEMAIL_SETTINGS_MENU, false)) {
575                 if (DBG) {
576                     log("maybeHideVoicemailSettings(): not disabled by default dialer");
577                 }
578                 return;
579             }
580             getPreferenceScreen().removePreference(mVoicemailSettingsScreen);
581             if (DBG) {
582                 log("maybeHideVoicemailSettings(): disabled by default dialer");
583             }
584         } catch (NameNotFoundException e) {
585             // do nothing
586             if (DBG) {
587                 log("maybeHideVoicemailSettings(): not controlled by default dialer");
588             }
589         }
590     }
591 
592     @Override
onNewIntent(Intent newIntent)593     protected void onNewIntent(Intent newIntent) {
594         setIntent(newIntent);
595 
596         mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent());
597         mPhone = mSubscriptionInfoHelper.getPhone();
598         mSubscriptionInfoHelper.setActionBarTitle(
599                 getActionBar(), getResourcesForSubId(), R.string.call_settings_with_label);
600     }
601 
log(String msg)602     private static void log(String msg) {
603         Log.d(LOG_TAG, msg);
604     }
605 
606     @Override
onOptionsItemSelected(MenuItem item)607     public boolean onOptionsItemSelected(MenuItem item) {
608         final int itemId = item.getItemId();
609         if (itemId == android.R.id.home) {  // See ActionBar#setDisplayHomeAsUpEnabled()
610             onBackPressed();
611             return true;
612         }
613         return super.onOptionsItemSelected(item);
614     }
615 
616     /**
617      * Finish current Activity and go up to the top level Settings ({@link CallFeaturesSetting}).
618      * This is useful for implementing "HomeAsUp" capability for second-level Settings.
619      */
goUpToTopLevelSetting( Activity activity, SubscriptionInfoHelper subscriptionInfoHelper)620     public static void goUpToTopLevelSetting(
621             Activity activity, SubscriptionInfoHelper subscriptionInfoHelper) {
622         Intent intent = subscriptionInfoHelper.getIntent(CallFeaturesSetting.class);
623         intent.setAction(Intent.ACTION_MAIN);
624         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
625         activity.startActivity(intent);
626         activity.finish();
627     }
628 
getResourcesForSubId()629     private Resources getResourcesForSubId() {
630         if (mPhone != null) {
631             return SubscriptionManager.getResourcesForSubId(mPhone.getContext(), mPhone.getSubId());
632         } else {
633             return getResources();
634         }
635     }
636 }
637