diff --git a/res/xml/network_provider_sims_list.xml b/res/xml/network_provider_sims_list.xml index b21341ea21e..cc7589cfa46 100644 --- a/res/xml/network_provider_sims_list.xml +++ b/res/xml/network_provider_sims_list.xml @@ -24,7 +24,7 @@ android:title="@string/summary_placeholder" android:layout="@layout/preference_category_no_label" android:order="20" - settings:controller="com.android.settings.network.NetworkProviderSimsCategoryController"/> + settings:controller="com.android.settings.network.NetworkProviderSimListController"/> createPreferenceControllers(Context context) { - final List controllers = new ArrayList<>(); - if (!SubscriptionUtil.isSimHardwareVisible(getContext())) { - finish(); - return controllers; - } - - NetworkProviderSimsCategoryController simCategoryPrefCtrl = - new NetworkProviderSimsCategoryController(context, KEY_PREFERENCE_CATEGORY_SIM, - getSettingsLifecycle(), this); - controllers.add(simCategoryPrefCtrl); - - return controllers; - } - public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = - new BaseSearchIndexProvider() { - - @Override - public List getXmlResourcesToIndex(Context context, - boolean enabled) { - final ArrayList result = new ArrayList<>(); - final SearchIndexableResource sir = new SearchIndexableResource(context); - sir.xmlResId = R.xml.network_provider_sims_list; - result.add(sir); - return result; - } + new BaseSearchIndexProvider(R.xml.network_provider_sims_list) { @Override protected boolean isPageSearchEnabled(Context context) { diff --git a/src/com/android/settings/network/NetworkProviderSimListController.java b/src/com/android/settings/network/NetworkProviderSimListController.java index 89cb73c7933..02b19802df2 100644 --- a/src/com/android/settings/network/NetworkProviderSimListController.java +++ b/src/com/android/settings/network/NetworkProviderSimListController.java @@ -16,69 +16,60 @@ package com.android.settings.network; -import static androidx.lifecycle.Lifecycle.Event.ON_PAUSE; -import static androidx.lifecycle.Lifecycle.Event.ON_RESUME; - import android.content.Context; import android.graphics.drawable.Drawable; import android.telephony.SubscriptionManager; import android.util.ArrayMap; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; +import androidx.lifecycle.DefaultLifecycleObserver; import androidx.lifecycle.LifecycleOwner; -import androidx.lifecycle.OnLifecycleEvent; import androidx.preference.Preference; import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceScreen; import com.android.settings.R; +import com.android.settings.core.BasePreferenceController; import com.android.settings.network.telephony.MobileNetworkUtils; import com.android.settingslib.RestrictedPreference; -import com.android.settingslib.core.AbstractPreferenceController; -import com.android.settingslib.core.lifecycle.Lifecycle; -import com.android.settingslib.core.lifecycle.LifecycleObserver; import com.android.settingslib.mobile.dataservice.SubscriptionInfoEntity; import java.util.ArrayList; import java.util.List; import java.util.Map; -public class NetworkProviderSimListController extends AbstractPreferenceController implements - LifecycleObserver, MobileNetworkRepository.MobileNetworkCallback, +public class NetworkProviderSimListController extends BasePreferenceController implements + DefaultLifecycleObserver, MobileNetworkRepository.MobileNetworkCallback, DefaultSubscriptionReceiver.DefaultSubscriptionListener { - private static final String TAG = "NetworkProviderSimListCtrl"; - private static final String KEY_PREFERENCE_CATEGORY_SIM = "provider_model_sim_category"; - private static final String KEY_PREFERENCE_SIM = "provider_model_sim_list"; - private SubscriptionManager mSubscriptionManager; + private final SubscriptionManager mSubscriptionManager; + @Nullable private PreferenceCategory mPreferenceCategory; private Map mPreferences; - private LifecycleOwner mLifecycleOwner; - private MobileNetworkRepository mMobileNetworkRepository; + private final MobileNetworkRepository mMobileNetworkRepository; private List mSubInfoEntityList = new ArrayList<>(); - private DefaultSubscriptionReceiver mDataSubscriptionChangedReceiver; + private final DefaultSubscriptionReceiver mDataSubscriptionChangedReceiver; - public NetworkProviderSimListController(Context context, Lifecycle lifecycle, - LifecycleOwner lifecycleOwner) { - super(context); + public NetworkProviderSimListController(Context context, String preferenceKey) { + super(context, preferenceKey); mSubscriptionManager = context.getSystemService(SubscriptionManager.class); mPreferences = new ArrayMap<>(); - mLifecycleOwner = lifecycleOwner; mMobileNetworkRepository = MobileNetworkRepository.getInstance(context); mDataSubscriptionChangedReceiver = new DefaultSubscriptionReceiver(context, this); - lifecycle.addObserver(this); } - @OnLifecycleEvent(ON_RESUME) - public void onResume() { - mMobileNetworkRepository.addRegister(mLifecycleOwner, this, + @Override + public void onResume(@NonNull LifecycleOwner owner) { + mMobileNetworkRepository.addRegister(owner, this, SubscriptionManager.INVALID_SUBSCRIPTION_ID); mMobileNetworkRepository.updateEntity(); mDataSubscriptionChangedReceiver.registerReceiver(); } - @OnLifecycleEvent(ON_PAUSE) - public void onPause() { + @Override + public void onPause(@NonNull LifecycleOwner owner) { mMobileNetworkRepository.removeRegister(this); mDataSubscriptionChangedReceiver.unRegisterReceiver(); } @@ -86,7 +77,7 @@ public class NetworkProviderSimListController extends AbstractPreferenceControll @Override public void displayPreference(PreferenceScreen screen) { super.displayPreference(screen); - mPreferenceCategory = screen.findPreference(KEY_PREFERENCE_CATEGORY_SIM); + mPreferenceCategory = screen.findPreference(getPreferenceKey()); update(); } @@ -154,31 +145,22 @@ public class NetworkProviderSimListController extends AbstractPreferenceControll } @Override - public boolean isAvailable() { - if (!getAvailablePhysicalSubscriptions().isEmpty()) { - return true; - } - return false; + public int getAvailabilityStatus() { + return getAvailablePhysicalSubscriptions().isEmpty() + ? CONDITIONALLY_UNAVAILABLE : AVAILABLE; } @VisibleForTesting protected List getAvailablePhysicalSubscriptions() { - List subList = new ArrayList<>(); - for (SubscriptionInfoEntity info : mSubInfoEntityList) { - subList.add(info); - } - return subList; - } - - @Override - public String getPreferenceKey() { - return KEY_PREFERENCE_SIM; + return new ArrayList<>(mSubInfoEntityList); } @Override public void onAvailableSubInfoChanged(List subInfoEntityList) { mSubInfoEntityList = subInfoEntityList; - mPreferenceCategory.setVisible(isAvailable()); + if (mPreferenceCategory != null) { + mPreferenceCategory.setVisible(isAvailable()); + } update(); } diff --git a/src/com/android/settings/network/NetworkProviderSimsCategoryController.java b/src/com/android/settings/network/NetworkProviderSimsCategoryController.java deleted file mode 100644 index f983e62d52f..00000000000 --- a/src/com/android/settings/network/NetworkProviderSimsCategoryController.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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; - -import android.content.Context; -import android.util.Log; - -import androidx.lifecycle.LifecycleOwner; -import androidx.preference.Preference; -import androidx.preference.PreferenceCategory; -import androidx.preference.PreferenceScreen; - -import com.android.settings.R; -import com.android.settings.widget.PreferenceCategoryController; -import com.android.settingslib.core.lifecycle.Lifecycle; -import com.android.settingslib.core.lifecycle.LifecycleObserver; - -public class NetworkProviderSimsCategoryController extends PreferenceCategoryController implements - LifecycleObserver { - private static final String LOG_TAG = "NetworkProviderSimsCategoryController"; - private static final String KEY_PREFERENCE_CATEGORY_SIM = "provider_model_sim_category"; - private NetworkProviderSimListController mNetworkProviderSimListController; - private PreferenceCategory mPreferenceCategory; - - public NetworkProviderSimsCategoryController(Context context, String key, Lifecycle lifecycle, - LifecycleOwner lifecycleOwner) { - super(context, key); - mNetworkProviderSimListController = - new NetworkProviderSimListController(mContext, lifecycle, lifecycleOwner); - } - - @Override - public int getAvailabilityStatus() { - if (mNetworkProviderSimListController == null - || !mNetworkProviderSimListController.isAvailable()) { - return CONDITIONALLY_UNAVAILABLE; - } else { - return AVAILABLE; - } - } - - @Override - public void displayPreference(PreferenceScreen screen) { - super.displayPreference(screen); - mNetworkProviderSimListController.displayPreference(screen); - mPreferenceCategory = screen.findPreference(KEY_PREFERENCE_CATEGORY_SIM); - if (mPreferenceCategory == null) { - Log.d(LOG_TAG, "displayPreference(), Can not find the category."); - return; - } - mPreferenceCategory.setVisible(isAvailable()); - } -} diff --git a/tests/unit/src/com/android/settings/network/NetworkProviderSimListControllerTest.java b/tests/unit/src/com/android/settings/network/NetworkProviderSimListControllerTest.java index c4e0f64c22c..a98f83bd7cb 100644 --- a/tests/unit/src/com/android/settings/network/NetworkProviderSimListControllerTest.java +++ b/tests/unit/src/com/android/settings/network/NetworkProviderSimListControllerTest.java @@ -16,19 +16,16 @@ package com.android.settings.network; -import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE; -import static com.google.common.truth.Truth.assertThat; - import static androidx.lifecycle.Lifecycle.Event; +import static com.google.common.truth.Truth.assertThat; + import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import android.content.Context; -import android.graphics.drawable.Drawable; import android.os.Looper; import android.telephony.SubscriptionManager; import android.telephony.TelephonyManager; @@ -36,18 +33,15 @@ import android.text.TextUtils; import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleRegistry; -import androidx.preference.PreferenceManager; -import androidx.preference.Preference; import androidx.preference.PreferenceCategory; +import androidx.preference.PreferenceManager; import androidx.preference.PreferenceScreen; import androidx.test.annotation.UiThreadTest; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; -import com.android.settings.R; import com.android.settings.testutils.ResourcesUtils; import com.android.settingslib.RestrictedPreference; -import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.mobile.dataservice.SubscriptionInfoEntity; import org.junit.Before; @@ -83,8 +77,6 @@ public class NetworkProviderSimListControllerTest { @Mock private SubscriptionManager mSubscriptionManager; @Mock - private Lifecycle mLifecycle; - @Mock private LifecycleOwner mLifecycleOwner; private LifecycleRegistry mLifecycleRegistry; @@ -100,12 +92,10 @@ public class NetworkProviderSimListControllerTest { * Mock the NetworkProviderSimListController that allows one to set a default voice, * SMS and mobile data subscription ID. */ - @SuppressWarnings("ClassCanBeStatic") - private class MockNetworkProviderSimListController extends - com.android.settings.network.NetworkProviderSimListController { - public MockNetworkProviderSimListController(Context context, Lifecycle lifecycle, - LifecycleOwner lifecycleOwner) { - super(context, lifecycle, lifecycleOwner); + private static class MockNetworkProviderSimListController + extends NetworkProviderSimListController { + MockNetworkProviderSimListController(Context context, String preferenceKey) { + super(context, preferenceKey); } private List mSubscriptionInfoEntity; @@ -136,8 +126,7 @@ public class NetworkProviderSimListControllerTest { mPreference.setKey(KEY_PREFERENCE_SIM_LIST); mPreferenceCategory = new PreferenceCategory(mContext); mPreferenceCategory.setKey(KEY_PREFERENCE_CATEGORY_SIM); - mController = new MockNetworkProviderSimListController(mContext, mLifecycle, - mLifecycleOwner); + mController = new MockNetworkProviderSimListController(mContext, "test_key"); mLifecycleRegistry = new LifecycleRegistry(mLifecycleOwner); when(mLifecycleOwner.getLifecycle()).thenReturn(mLifecycleRegistry); } diff --git a/tests/unit/src/com/android/settings/network/NetworkProviderSimsCategoryControllerTest.java b/tests/unit/src/com/android/settings/network/NetworkProviderSimsCategoryControllerTest.java deleted file mode 100644 index dc17e9184ff..00000000000 --- a/tests/unit/src/com/android/settings/network/NetworkProviderSimsCategoryControllerTest.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (C) 2020 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; - -import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE; - -import static com.google.common.truth.Truth.assertThat; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; - -import android.content.Context; -import android.os.Looper; -import android.telephony.SubscriptionInfo; -import android.telephony.SubscriptionManager; - -import com.android.settings.testutils.ResourcesUtils; -import com.android.settingslib.core.lifecycle.Lifecycle; - -import androidx.lifecycle.LifecycleOwner; -import androidx.preference.PreferenceCategory; -import androidx.preference.PreferenceManager; -import androidx.preference.PreferenceScreen; -import androidx.test.core.app.ApplicationProvider; -import androidx.test.ext.junit.runners.AndroidJUnit4; - -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import java.util.ArrayList; -import java.util.Arrays; - -//TODO: Remove NetworkProviderSimsCategoryControllerTest once it is removed in the b/244769887. -@RunWith(AndroidJUnit4.class) -public class NetworkProviderSimsCategoryControllerTest { - - private static final String KEY_PREFERENCE_CATEGORY_SIM = "provider_model_sim_category"; - private static final String SUB_1 = "SUB_1"; - private static final String SUB_2 = "SUB_2"; - private static final int SUB_ID_1 = 1; - private static final int SUB_ID_2 = 2; - - @Mock - private Lifecycle mLifecycle; - @Mock - private SubscriptionInfo mSubscriptionInfo1; - @Mock - private SubscriptionInfo mSubscriptionInfo2; - - private Context mContext; - private NetworkProviderSimsCategoryController mCategoryController; - private PreferenceManager mPreferenceManager; - private PreferenceScreen mPreferenceScreen; - private PreferenceCategory mPreferenceCategory; - private LifecycleOwner mLifecycleOwner; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - mContext = spy(ApplicationProvider.getApplicationContext()); - - if (Looper.myLooper() == null) { - Looper.prepare(); - } - - mLifecycleOwner = () -> mLifecycle; - mPreferenceManager = new PreferenceManager(mContext); - mPreferenceScreen = mPreferenceManager.createPreferenceScreen(mContext); - mPreferenceCategory = new PreferenceCategory(mContext); - mPreferenceCategory.setKey(KEY_PREFERENCE_CATEGORY_SIM); - mPreferenceScreen.addPreference(mPreferenceCategory); - - mCategoryController = new NetworkProviderSimsCategoryController( - mContext, KEY_PREFERENCE_CATEGORY_SIM, mLifecycle, mLifecycleOwner); - } - - @Ignore - @Test - public void getAvailabilityStatus_returnUnavailable() { - SubscriptionUtil.setAvailableSubscriptionsForTesting(new ArrayList<>()); - - assertThat(mCategoryController.getAvailabilityStatus()).isEqualTo( - CONDITIONALLY_UNAVAILABLE); - } - - @Ignore - @Test - public void displayPreference_isVisible() { - setUpSubscriptionInfoForPhysicalSim(SUB_ID_1, SUB_1, mSubscriptionInfo1); - SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(mSubscriptionInfo1)); - mCategoryController.displayPreference(mPreferenceScreen); - - assertEquals(mPreferenceCategory.isVisible(), true); - } - - @Ignore - @Test - public void updateState_setTitle_withTwoPhysicalSims_returnSims() { - setUpSubscriptionInfoForPhysicalSim(SUB_ID_1, SUB_1, mSubscriptionInfo1); - setUpSubscriptionInfoForPhysicalSim(SUB_ID_2, SUB_2, mSubscriptionInfo2); - SubscriptionUtil.setAvailableSubscriptionsForTesting( - Arrays.asList(mSubscriptionInfo1, mSubscriptionInfo2)); - - mCategoryController.displayPreference(mPreferenceScreen); - mCategoryController.updateState(mPreferenceCategory); - - assertThat(mPreferenceCategory.getPreferenceCount()).isEqualTo(2); - assertThat(mPreferenceCategory.getTitle()).isEqualTo( - ResourcesUtils.getResourcesString(mContext, "provider_network_settings_title")); - } - - @Ignore - @Test - public void updateState_setTitle_withOnePhysicalSim_returnSim() { - setUpSubscriptionInfoForPhysicalSim(SUB_ID_1, SUB_1, mSubscriptionInfo1); - SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(mSubscriptionInfo1)); - - mCategoryController.displayPreference(mPreferenceScreen); - mCategoryController.updateState(mPreferenceCategory); - - assertThat(mPreferenceCategory.getPreferenceCount()).isEqualTo(1); - assertThat(mPreferenceCategory.getTitle()).isEqualTo( - ResourcesUtils.getResourcesString(mContext, "sim_category_title")); - } - - private void setUpSubscriptionInfoForPhysicalSim(int subId, String displayName, - SubscriptionInfo subscriptionInfo) { - when(subscriptionInfo.isEmbedded()).thenReturn(false); - when(subscriptionInfo.getSubscriptionId()).thenReturn(subId); - when(subscriptionInfo.getDisplayName()).thenReturn(displayName); - } - -}