diff --git a/src/com/android/settings/network/telephony/NetworkProviderBackupCallingGroup.java b/src/com/android/settings/network/telephony/NetworkProviderBackupCallingGroup.java new file mode 100644 index 00000000000..58dd18f2c62 --- /dev/null +++ b/src/com/android/settings/network/telephony/NetworkProviderBackupCallingGroup.java @@ -0,0 +1,289 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settings.network.telephony; + +import static androidx.lifecycle.Lifecycle.Event; + +import android.content.Context; +import android.os.PersistableBundle; +import android.telephony.CarrierConfigManager; +import android.telephony.SubscriptionInfo; +import android.telephony.SubscriptionManager; +import android.telephony.TelephonyManager; +import android.telephony.ims.ImsException; +import android.telephony.ims.ImsManager; +import android.telephony.ims.ImsMmTelManager; +import android.util.ArrayMap; +import android.util.Log; + +import androidx.annotation.VisibleForTesting; +import androidx.lifecycle.LifecycleObserver; +import androidx.lifecycle.OnLifecycleEvent; +import androidx.preference.Preference; +import androidx.preference.PreferenceGroup; +import androidx.preference.PreferenceScreen; +import androidx.preference.SwitchPreference; + +import com.android.settings.R; +import com.android.settings.network.SubscriptionUtil; +import com.android.settings.network.SubscriptionsChangeListener; +import com.android.settings.network.ims.WifiCallingQueryImsState; +import com.android.settingslib.core.lifecycle.Lifecycle; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Preference controller for "Backup Calling" + **/ +public class NetworkProviderBackupCallingGroup extends + TelephonyTogglePreferenceController implements LifecycleObserver, + SubscriptionsChangeListener.SubscriptionsChangeListenerClient { + private static final String TAG = "NetworkProviderBackupCallingGroup"; + private static final String KEY_PREFERENCE_BACKUPCALLING_GROUP = + "provider_model_backup_call_group"; + private static final int PREF_START_ORDER = 10; + + private String mPreferenceGroupKey; + private PreferenceGroup mPreferenceGroup; + private Map mBackupCallingForSubPreferences; + private List mSubInfoListForBackupCall; + private Map mTelephonyManagerList = new HashMap<>(); + private SubscriptionsChangeListener mSubscriptionsChangeListener; + + public NetworkProviderBackupCallingGroup(Context context, Lifecycle lifecycle, + List subscriptionList, String preferenceGroupKey) { + super(context, preferenceGroupKey); + mPreferenceGroupKey = preferenceGroupKey; + mSubInfoListForBackupCall = subscriptionList; + mBackupCallingForSubPreferences = new ArrayMap<>(); + setSubscriptionInfoList(context); + lifecycle.addObserver(this); + } + + @OnLifecycleEvent(Event.ON_RESUME) + public void onResume() { + if (mSubscriptionsChangeListener == null) { + mSubscriptionsChangeListener = new SubscriptionsChangeListener(mContext, this); + } + mSubscriptionsChangeListener.start(); + } + + @OnLifecycleEvent(Event.ON_PAUSE) + public void onPause() { + if (mSubscriptionsChangeListener != null) { + mSubscriptionsChangeListener.stop(); + } + } + + @Override + public int getAvailabilityStatus(int subId) { + if (mSubInfoListForBackupCall == null + || getSubscriptionInfoFromList(mSubInfoListForBackupCall, subId) == null) { + return CONDITIONALLY_UNAVAILABLE; + } + + return (mSubInfoListForBackupCall.size() > 1) ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; + } + + private boolean setCrossSimCallingEnabled(int subId, boolean checked) { + ImsMmTelManager imsMmTelMgr = getImsMmTelManager(subId); + if (imsMmTelMgr == null) { + Log.d(TAG, "setCrossSimCallingEnabled(), ImsMmTelManager is null"); + return false; + } + + try { + imsMmTelMgr.setCrossSimCallingEnabled(checked); + } catch (ImsException exception) { + Log.w(TAG, "fail to get cross SIM calling configuration", exception); + return false; + } + return true; + } + + @Override + public boolean setChecked(boolean checked) { + return false; + } + + private boolean isCrossSimCallingEnabled(int subId) { + ImsMmTelManager imsMmTelMgr = getImsMmTelManager(subId); + if (imsMmTelMgr == null) { + Log.d(TAG, "isCrossSimCallingEnabled(), ImsMmTelManager is null"); + return false; + } + try { + return imsMmTelMgr.isCrossSimCallingEnabled(); + } catch (ImsException exception) { + Log.w(TAG, "fail to get cross SIM calling configuration", exception); + } + return false; + } + + @Override + public boolean isChecked() { + return false; + } + + @Override + public void displayPreference(PreferenceScreen screen) { + mPreferenceGroup = screen.findPreference(mPreferenceGroupKey); + update(); + } + + @Override + public void updateState(Preference preference) { + super.updateState(preference); + // Do nothing in this case since preference is invisible + if (preference == null) { + return; + } + update(); + } + + private void update() { + if (mPreferenceGroup == null) { + return; + } + + setSubscriptionInfoList(mContext); + if (mSubInfoListForBackupCall == null || mSubInfoListForBackupCall.size() < 2) { + for (SwitchPreference pref : mBackupCallingForSubPreferences.values()) { + mPreferenceGroup.removePreference(pref); + } + mBackupCallingForSubPreferences.clear(); + return; + } + + Map toRemovePreferences = mBackupCallingForSubPreferences; + mBackupCallingForSubPreferences = new ArrayMap<>(); + setSubscriptionInfoForPreference(toRemovePreferences); + } + + private void setSubscriptionInfoForPreference( + Map toRemovePreferences) { + int order = PREF_START_ORDER; + for (SubscriptionInfo subInfo : mSubInfoListForBackupCall) { + final int subId = subInfo.getSubscriptionId(); + + SwitchPreference pref = toRemovePreferences.remove(subId); + if (pref == null) { + pref = new SwitchPreference(mPreferenceGroup.getContext()); + mPreferenceGroup.addPreference(pref); + } + + CharSequence displayName = (subInfo == null) ? "" + : SubscriptionUtil.getUniqueSubscriptionDisplayName(subInfo, mContext); + pref.setTitle(displayName); + pref.setOrder(order++); + pref.setSummary(getSummary(displayName)); + boolean enabled = isCrossSimCallingEnabled(subId); + pref.setChecked(enabled); + pref.setOnPreferenceClickListener(clickedPref -> { + setCrossSimCallingEnabled(subId, !enabled); + return true; + }); + mBackupCallingForSubPreferences.put(subId, pref); + } + } + + private String getSummary(CharSequence displayName) { + String summary = String.format( + getResourcesForSubId().getString(R.string.backup_calling_setting_summary), + displayName) + .toString(); + return summary; + } + + private void setSubscriptionInfoList(Context context) { + if (mSubInfoListForBackupCall != null) { + mSubInfoListForBackupCall.removeIf(info -> { + int subId = info.getSubscriptionId(); + setTelephonyManagerForSubscriptionId(context, subId); + if (!hasBackupCallingFeature(subId) && mSubInfoListForBackupCall.contains(info)) { + return true; + } + return false; + }); + } else { + Log.d(TAG, "No active subscriptions"); + } + } + + private void setTelephonyManagerForSubscriptionId(Context context, int subId) { + TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class) + .createForSubscriptionId(subId); + mTelephonyManagerList.put(subId, telephonyManager); + } + + @VisibleForTesting + protected boolean hasBackupCallingFeature(int subscriptionId) { + return isCrossSimEnabledByPlatform(mContext, subscriptionId); + } + + /** + * Copied from {@link BackupCallingPreferenceController} + **/ + @VisibleForTesting + protected boolean isCrossSimEnabledByPlatform(Context context, int subscriptionId) { + // TODO : Change into API which created for accessing + // com.android.ims.ImsManager#isCrossSimEnabledByPlatform() + if ((new WifiCallingQueryImsState(context, subscriptionId)).isWifiCallingSupported()) { + PersistableBundle bundle = getCarrierConfigForSubId(subscriptionId); + return (bundle != null) && bundle.getBoolean( + CarrierConfigManager.KEY_CARRIER_CROSS_SIM_IMS_AVAILABLE_BOOL, + false /*default*/); + } + Log.d(TAG, "WifiCalling is not supported by framework. subId = " + subscriptionId); + return false; + } + + private ImsMmTelManager getImsMmTelManager(int subId) { + if (!SubscriptionManager.isUsableSubscriptionId(subId)) { + return null; + } + ImsManager imsMgr = mContext.getSystemService(ImsManager.class); + return (imsMgr == null) ? null : imsMgr.getImsMmTelManager(subId); + } + + private SubscriptionInfo getSubscriptionInfoFromList( + List subInfoList, int subId) { + for (SubscriptionInfo subInfo : subInfoList) { + if ((subInfo != null) && (subInfo.getSubscriptionId() == subId)) { + return subInfo; + } + } + return null; + } + + @Override + public String getPreferenceKey() { + return KEY_PREFERENCE_BACKUPCALLING_GROUP; + } + + @Override + public void onAirplaneModeChanged(boolean airplaneModeEnabled) {} + + @Override + public void onSubscriptionsChanged() { + SubscriptionManager subscriptionManager = + mContext.getSystemService(SubscriptionManager.class); + mSubInfoListForBackupCall = SubscriptionUtil.getActiveSubscriptions(subscriptionManager); + update(); + } +} diff --git a/src/com/android/settings/network/telephony/NetworkProviderBackupCallingPreferenceController.java b/src/com/android/settings/network/telephony/NetworkProviderBackupCallingPreferenceController.java index b66cea643a2..5c336efc507 100644 --- a/src/com/android/settings/network/telephony/NetworkProviderBackupCallingPreferenceController.java +++ b/src/com/android/settings/network/telephony/NetworkProviderBackupCallingPreferenceController.java @@ -20,10 +20,8 @@ import android.content.Context; import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionManager; -import androidx.preference.Preference; import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceScreen; -import androidx.preference.SwitchPreference; import com.android.settings.core.BasePreferenceController; import com.android.settings.network.SubscriptionUtil; @@ -31,8 +29,6 @@ import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.LifecycleObserver; import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; /** * Preference controller for "Backup Calling" summary list @@ -41,16 +37,24 @@ public class NetworkProviderBackupCallingPreferenceController extends BasePreferenceController implements LifecycleObserver { private static final String TAG = "NetProvBackupCallingCtrl"; + private static final String KEY_PREFERENCE_CATEGORY = "provider_model_backup_calling_category"; - private Context mContext; private PreferenceCategory mPreferenceCategory; + private PreferenceScreen mPreferenceScreen; + private NetworkProviderBackupCallingGroup mNetworkProviderBackupCallingGroup; + private List mSubscriptionList; /** * Preference controller for "Backup Calling" summary list */ public NetworkProviderBackupCallingPreferenceController(Context context, String key) { super(context, key); - mContext = context; + } + + protected NetworkProviderBackupCallingGroup createBackupCallingControllerForSub( + Lifecycle lifecycle, List subscriptionList) { + return new NetworkProviderBackupCallingGroup(mContext, lifecycle, subscriptionList, + KEY_PREFERENCE_CATEGORY); } /** @@ -59,73 +63,34 @@ public class NetworkProviderBackupCallingPreferenceController extends * @param lifecycle Lifecycle of UI which owns this Preference */ public void init(Lifecycle lifecycle) { - lifecycle.addObserver(this); + mSubscriptionList = getActiveSubscriptionList(); + mNetworkProviderBackupCallingGroup = createBackupCallingControllerForSub(lifecycle, + mSubscriptionList); + } + + private List getActiveSubscriptionList() { + SubscriptionManager subscriptionManager = + mContext.getSystemService(SubscriptionManager.class); + return SubscriptionUtil.getActiveSubscriptions(subscriptionManager); } @Override public int getAvailabilityStatus() { - List subList = getActiveSubscriptions(); - if (subList.size() < 2) { + if (mNetworkProviderBackupCallingGroup == null + || mSubscriptionList == null + || mSubscriptionList.size() < 2) { return CONDITIONALLY_UNAVAILABLE; + } else { + return AVAILABLE; } - return (getPreferences(subList).size() >= 1) ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; } @Override public void displayPreference(PreferenceScreen screen) { super.displayPreference(screen); - - PreferenceCategory prefCategory = screen.findPreference(getPreferenceKey()); - updatePreferenceList(prefCategory); - prefCategory.setVisible(isAvailable()); - } - - @Override - public void updateState(Preference preference) { - super.updateState(preference); - // Do nothing in this case since preference is invisible - if (preference == null) { - return; - } - updatePreferenceList((PreferenceCategory) preference); - } - - private String getPreferenceKey(int subscriptionId) { - return getPreferenceKey() + "_subId_" + subscriptionId; - } - - private SwitchPreference getPreference(SubscriptionInfo subInfo) { - int subId = subInfo.getSubscriptionId(); - BackupCallingPreferenceController prefCtrl = - new BackupCallingPreferenceController(mContext, getPreferenceKey(subId)); - prefCtrl.init(subId); - if (prefCtrl.getAvailabilityStatus(subId) != BasePreferenceController.AVAILABLE) { - return null; - } - SwitchPreference pref = new SwitchPreference(mContext); - prefCtrl.updateState(pref); - pref.setTitle(SubscriptionUtil.getUniqueSubscriptionDisplayName(subInfo, mContext)); - return pref; - } - - private List getPreferences(List subList) { - return subList.stream() - .map(subInfo -> getPreference(subInfo)) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - } - - private List getActiveSubscriptions() { - return SubscriptionUtil.getActiveSubscriptions( - mContext.getSystemService(SubscriptionManager.class)); - } - - private void updatePreferenceList(PreferenceCategory prefCategory) { - List prefList = getPreferences(getActiveSubscriptions()); - - prefCategory.removeAll(); - for (SwitchPreference pref : prefList) { - prefCategory.addPreference(pref); - } + mPreferenceScreen = screen; + mPreferenceCategory = screen.findPreference(KEY_PREFERENCE_CATEGORY); + mPreferenceCategory.setVisible(isAvailable()); + mNetworkProviderBackupCallingGroup.displayPreference(screen); } } diff --git a/tests/unit/src/com/android/settings/network/telephony/NetworkProviderBackupCallingGroupTest.java b/tests/unit/src/com/android/settings/network/telephony/NetworkProviderBackupCallingGroupTest.java new file mode 100644 index 00000000000..e7a6af39319 --- /dev/null +++ b/tests/unit/src/com/android/settings/network/telephony/NetworkProviderBackupCallingGroupTest.java @@ -0,0 +1,173 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settings.network.telephony; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.os.Looper; +import android.os.PersistableBundle; +import android.telephony.CarrierConfigManager; +import android.telephony.SubscriptionInfo; +import android.telephony.SubscriptionManager; +import android.telephony.TelephonyManager; +import android.util.FeatureFlagUtils; +import android.util.Log; + +import androidx.preference.Preference; +import androidx.preference.PreferenceGroup; +import androidx.preference.PreferenceManager; +import androidx.preference.PreferenceScreen; +import androidx.preference.SwitchPreference; +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import com.android.settings.network.CarrierWifiTogglePreferenceController; +import com.android.settings.testutils.ResourcesUtils; +import com.android.settingslib.core.lifecycle.Lifecycle; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.List; + +@RunWith(AndroidJUnit4.class) +public class NetworkProviderBackupCallingGroupTest { + + private static final int SUB_ID_1 = 1; + private static final int SUB_ID_2 = 2; + + private static final String KEY_PREFERENCE_CATEGORY_BACKUP_CALLING = + "provider_model_backup_calling_category"; + private static final String DISPLAY_NAME_1 = "Test Display Name 1"; + private static final String DISPLAY_NAME_2 = "Test Display Name 2"; + + @Mock + private PreferenceGroup mPreferenceGroup; + @Mock + private CarrierConfigManager mCarrierConfigManager; + @Mock + private Lifecycle mLifecycle; + @Mock + private SubscriptionManager mSubscriptionManager; + @Mock + private SubscriptionInfo mSubscriptionInfo1; + @Mock + private SubscriptionInfo mSubscriptionInfo2; + @Mock + private List mSubscriptionInfoList; + @Mock + private TelephonyManager mTelephonyManager1; + @Mock + private TelephonyManager mTelephonyManager2; + + @Mock + private NetworkProviderBackupCallingGroup mNetworkProviderBackupCallingGroup; + private Context mContext; + private PersistableBundle mCarrierConfig; + private PreferenceManager mPreferenceManager; + private PreferenceScreen mPreferenceScreen; + private SwitchPreference mSwitchPreference1; + private SwitchPreference mSwitchPreference2; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + mContext = spy(ApplicationProvider.getApplicationContext()); + when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager1); + when(mTelephonyManager1.createForSubscriptionId(SUB_ID_1)).thenReturn(mTelephonyManager1); + when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager2); + when(mTelephonyManager2.createForSubscriptionId(SUB_ID_2)).thenReturn(mTelephonyManager2); + when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager); + when(mSubscriptionInfo1.getSubscriptionId()).thenReturn(SUB_ID_1); + when(mSubscriptionInfo1.getDisplayName()).thenReturn(DISPLAY_NAME_1); + doReturn(true).when(mNetworkProviderBackupCallingGroup).isCrossSimEnabledByPlatform( + mContext, SUB_ID_1); + mSubscriptionInfoList.add(mSubscriptionInfo1); + when(mSubscriptionInfo2.getSubscriptionId()).thenReturn(SUB_ID_2); + when(mSubscriptionInfo2.getDisplayName()).thenReturn(DISPLAY_NAME_2); + doReturn(true).when(mNetworkProviderBackupCallingGroup).isCrossSimEnabledByPlatform( + mContext, SUB_ID_2); + mSubscriptionInfoList.add(mSubscriptionInfo2); + when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn( + mSubscriptionInfoList); + + mCarrierConfig = new PersistableBundle(); + doReturn(mCarrierConfig).when(mCarrierConfigManager).getConfigForSubId(SUB_ID_1); + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CROSS_SIM_IMS_AVAILABLE_BOOL, + true); + doReturn(mCarrierConfig).when(mCarrierConfigManager).getConfigForSubId(SUB_ID_2); + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CROSS_SIM_IMS_AVAILABLE_BOOL, + true); + + if (Looper.myLooper() == null) { + Looper.prepare(); + } + + mPreferenceManager = new PreferenceManager(mContext); + mPreferenceScreen = mPreferenceManager.createPreferenceScreen(mContext); + when(mPreferenceGroup.getKey()).thenReturn(KEY_PREFERENCE_CATEGORY_BACKUP_CALLING); + when(mPreferenceGroup.getPreferenceCount()).thenReturn(2); + mPreferenceScreen.addPreference(mPreferenceGroup); + + mNetworkProviderBackupCallingGroup = spy(new NetworkProviderBackupCallingGroup( + mContext, mLifecycle, mSubscriptionInfoList, + KEY_PREFERENCE_CATEGORY_BACKUP_CALLING)); + } + + @Test + public void shouldShowBackupCallingForSub_invalidSubId_returnFalse() { + assertThat(mNetworkProviderBackupCallingGroup.hasBackupCallingFeature( + SubscriptionManager.INVALID_SUBSCRIPTION_ID)).isEqualTo(false); + } + + @Test + public void shouldShowBackupCallingForSub_carrierConfigIsUnavailable_returnFalse() { + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CROSS_SIM_IMS_AVAILABLE_BOOL, + false); + + assertThat(mNetworkProviderBackupCallingGroup.hasBackupCallingFeature(SUB_ID_1)) + .isEqualTo(false); + } + + @Test + public void + shouldShowBackupCallingForSub_crossSimDisabled_returnFalse() { + FeatureFlagUtils.setEnabled(mContext, FeatureFlagUtils.SETTINGS_PROVIDER_MODEL, true); + doReturn(false).when(mNetworkProviderBackupCallingGroup).isCrossSimEnabledByPlatform( + mContext, SUB_ID_1); + + assertThat(mNetworkProviderBackupCallingGroup.hasBackupCallingFeature(SUB_ID_1)) + .isEqualTo(false); + } + + @Test + public void shouldBackupCallingForSub_crossSimEnabled_returnTrue() { + FeatureFlagUtils.setEnabled(mContext, FeatureFlagUtils.SETTINGS_PROVIDER_MODEL, true); + doReturn(true).when(mNetworkProviderBackupCallingGroup).isCrossSimEnabledByPlatform( + mContext, SUB_ID_1); + + assertThat(mNetworkProviderBackupCallingGroup.hasBackupCallingFeature(SUB_ID_1)) + .isEqualTo(true); + } +}