Merge "DO NOT MERGE Add a new setting for Mobile Network within Network&Internet" into pi-car-dev
diff --git a/res/drawable/ic_settings_cellular.xml b/res/drawable/ic_settings_cellular.xml
new file mode 100644
index 0000000..0882f89
--- /dev/null
+++ b/res/drawable/ic_settings_cellular.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright 2019 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.
+-->
+
+<vector
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="24dp"
+ android:height="24dp"
+ android:tint="@color/car_tint"
+ android:viewportHeight="24.0"
+ android:viewportWidth="24.0">
+ <path
+ android:fillColor="#FFFFFFFF"
+ android:pathData="M2,22h20V2z"/>
+</vector>
diff --git a/res/values/preference_keys.xml b/res/values/preference_keys.xml
index 22aecd4..f3ca201 100644
--- a/res/values/preference_keys.xml
+++ b/res/values/preference_keys.xml
@@ -41,6 +41,9 @@
<string name="pk_device_extra_settings" translatable="false">device_extra_settings</string>
<string name="pk_personal_extra_settings" translatable="false">personal_extra_settings</string>
+ <!-- Network -->
+ <string name="pk_mobile_network_settings_entry" translatable="false">mobile_network_settings_entry</string>
+
<!-- WIFI -->
<string name="pk_wifi_settings_entry" translatable="false">wifi_settings_entry</string>
<string name="pk_wifi_frequency" translatable="false">wifi_frequency</string>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 1cc8494..19235c7 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -37,6 +37,8 @@
<!-- Network and internet settings [CHAR LIMIT=40] -->
<string name="network_and_internet">Network & internet</string>
+ <!-- Mobile network settings [CHAR LIMIT=30] -->
+ <string name="mobile_network_settings">Mobile network</string>
<!-- wifi settings--><skip/>
<!-- Used in the 1st-level settings screen to go to the 2nd-level settings screen [CHAR LIMIT=20]-->
<string name="wifi_settings">Wi\u2011Fi</string>
diff --git a/res/xml/mobile_network_fragment.xml b/res/xml/mobile_network_fragment.xml
new file mode 100644
index 0000000..77253a7
--- /dev/null
+++ b/res/xml/mobile_network_fragment.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright 2019 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.
+-->
+
+<PreferenceScreen
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ android:title="@string/mobile_network_settings"/>
diff --git a/res/xml/network_and_internet_fragment.xml b/res/xml/network_and_internet_fragment.xml
index 62dee9c..09c1bf9 100644
--- a/res/xml/network_and_internet_fragment.xml
+++ b/res/xml/network_and_internet_fragment.xml
@@ -25,4 +25,10 @@
android:key="@string/pk_wifi_settings_entry"
android:title="@string/wifi_settings"
settings:controller="com.android.car.settings.wifi.WifiEntryPreferenceController"/>
+ <Preference
+ android:fragment="com.android.car.settings.network.MobileNetworkFragment"
+ android:icon="@drawable/ic_settings_cellular"
+ android:key="@string/pk_mobile_network_settings_entry"
+ android:title="@string/mobile_network_settings"
+ settings:controller="com.android.car.settings.network.MobileNetworkEntryPreferenceController"/>
</PreferenceScreen>
diff --git a/src/com/android/car/settings/network/MobileNetworkEntryPreferenceController.java b/src/com/android/car/settings/network/MobileNetworkEntryPreferenceController.java
new file mode 100644
index 0000000..96de5eb
--- /dev/null
+++ b/src/com/android/car/settings/network/MobileNetworkEntryPreferenceController.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2019 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.car.settings.network;
+
+import android.car.drivingstate.CarUxRestrictions;
+import android.car.userlib.CarUserManagerHelper;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.ConnectivityManager;
+import android.net.Network;
+import android.net.NetworkCapabilities;
+import android.os.UserManager;
+import android.provider.Settings;
+import android.telephony.PhoneStateListener;
+import android.telephony.ServiceState;
+import android.telephony.TelephonyManager;
+
+import androidx.preference.Preference;
+
+import com.android.car.settings.common.FragmentController;
+import com.android.car.settings.common.PreferenceController;
+
+/** Controls the preference for accessing mobile network settings. */
+public class MobileNetworkEntryPreferenceController extends PreferenceController<Preference> {
+
+ private final CarUserManagerHelper mCarUserManagerHelper;
+ private final TelephonyManager mTelephonyManager;
+ private final ConnectivityManager mConnectivityManager;
+ private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
+ @Override
+ public void onServiceStateChanged(ServiceState serviceState) {
+ refreshUi();
+ }
+ };
+ private final BroadcastReceiver mAirplaneModeChangedReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ refreshUi();
+ }
+ };
+ private final IntentFilter mIntentFilter = new IntentFilter(
+ Intent.ACTION_AIRPLANE_MODE_CHANGED);
+
+ public MobileNetworkEntryPreferenceController(Context context, String preferenceKey,
+ FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
+ super(context, preferenceKey, fragmentController, uxRestrictions);
+ mCarUserManagerHelper = new CarUserManagerHelper(context);
+ mTelephonyManager = context.getSystemService(TelephonyManager.class);
+ mConnectivityManager = getContext().getSystemService(ConnectivityManager.class);
+ }
+
+ @Override
+ protected Class<Preference> getPreferenceType() {
+ return Preference.class;
+ }
+
+ @Override
+ protected void onStartInternal() {
+ mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
+ getContext().registerReceiver(mAirplaneModeChangedReceiver, mIntentFilter);
+ }
+
+ @Override
+ protected void onStopInternal() {
+ mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
+ getContext().unregisterReceiver(mAirplaneModeChangedReceiver);
+ }
+
+ @Override
+ protected int getAvailabilityStatus() {
+ if (!hasMobileNetwork()) {
+ return UNSUPPORTED_ON_DEVICE;
+ }
+
+ boolean isNotAdmin = !mCarUserManagerHelper.getCurrentProcessUserInfo().isAdmin();
+ boolean hasRestriction = mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
+ UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
+ if (isNotAdmin || hasRestriction) {
+ return DISABLED_FOR_USER;
+ }
+ return AVAILABLE;
+ }
+
+ @Override
+ protected void updateState(Preference preference) {
+ preference.setSummary(mTelephonyManager.getNetworkOperatorName());
+ preference.setEnabled(isAirplaneModeOff());
+ }
+
+ private boolean isAirplaneModeOff() {
+ return Settings.Global.getInt(getContext().getContentResolver(),
+ Settings.Global.AIRPLANE_MODE_ON, 0) == 0;
+ }
+
+ private boolean hasMobileNetwork() {
+ Network[] networks = mConnectivityManager.getAllNetworks();
+ for (Network network : networks) {
+ NetworkCapabilities capabilities = mConnectivityManager.getNetworkCapabilities(network);
+ if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
+ return true;
+ }
+ }
+ return false;
+ }
+}
diff --git a/src/com/android/car/settings/network/MobileNetworkFragment.java b/src/com/android/car/settings/network/MobileNetworkFragment.java
new file mode 100644
index 0000000..27cb83e
--- /dev/null
+++ b/src/com/android/car/settings/network/MobileNetworkFragment.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2019 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.car.settings.network;
+
+import androidx.annotation.XmlRes;
+
+import com.android.car.settings.R;
+import com.android.car.settings.common.SettingsFragment;
+
+/** Mobile network settings homepage. */
+public class MobileNetworkFragment extends SettingsFragment {
+
+ @Override
+ @XmlRes
+ protected int getPreferenceScreenResId() {
+ return R.xml.mobile_network_fragment;
+ }
+}
diff --git a/tests/robotests/src/com/android/car/settings/network/MobileNetworkEntryPreferenceControllerTest.java b/tests/robotests/src/com/android/car/settings/network/MobileNetworkEntryPreferenceControllerTest.java
new file mode 100644
index 0000000..9e8ac60
--- /dev/null
+++ b/tests/robotests/src/com/android/car/settings/network/MobileNetworkEntryPreferenceControllerTest.java
@@ -0,0 +1,256 @@
+/*
+ * Copyright (C) 2019 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.car.settings.network;
+
+import static com.android.car.settings.common.PreferenceController.AVAILABLE;
+import static com.android.car.settings.common.PreferenceController.DISABLED_FOR_USER;
+import static com.android.car.settings.common.PreferenceController.UNSUPPORTED_ON_DEVICE;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.when;
+import static org.robolectric.shadow.api.Shadow.extract;
+
+import android.car.userlib.CarUserManagerHelper;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.UserInfo;
+import android.net.ConnectivityManager;
+import android.net.NetworkCapabilities;
+import android.os.UserManager;
+import android.provider.Settings;
+import android.telephony.PhoneStateListener;
+import android.telephony.TelephonyManager;
+
+import androidx.lifecycle.Lifecycle;
+import androidx.preference.Preference;
+
+import com.android.car.settings.CarSettingsRobolectricTestRunner;
+import com.android.car.settings.common.PreferenceControllerTestHelper;
+import com.android.car.settings.testutils.ShadowCarUserManagerHelper;
+import com.android.car.settings.testutils.ShadowConnectivityManager;
+import com.android.car.settings.testutils.ShadowTelephonyManager;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+import org.robolectric.shadows.ShadowApplication;
+import org.robolectric.shadows.ShadowNetwork;
+
+@RunWith(CarSettingsRobolectricTestRunner.class)
+@Config(shadows = {ShadowCarUserManagerHelper.class, ShadowConnectivityManager.class,
+ ShadowTelephonyManager.class})
+public class MobileNetworkEntryPreferenceControllerTest {
+
+ private static final String TEST_NETWORK_NAME = "test network name";
+ private static final UserInfo TEST_ADMIN_USER = new UserInfo(10, "test_name",
+ UserInfo.FLAG_ADMIN);
+ private static final UserInfo TEST_NON_ADMIN_USER = new UserInfo(10, "test_name",
+ /* flags= */ 0);
+
+ private Context mContext;
+ private Preference mPreference;
+ private PreferenceControllerTestHelper<MobileNetworkEntryPreferenceController>
+ mControllerHelper;
+ private MobileNetworkEntryPreferenceController mController;
+ @Mock
+ private CarUserManagerHelper mCarUserManagerHelper;
+ @Mock
+ private NetworkCapabilities mNetworkCapabilities;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper);
+ mContext = RuntimeEnvironment.application;
+ mPreference = new Preference(mContext);
+ mControllerHelper = new PreferenceControllerTestHelper<>(mContext,
+ MobileNetworkEntryPreferenceController.class, mPreference);
+ mController = mControllerHelper.getController();
+
+ // Setup to always make preference available.
+ getShadowConnectivityManager().clearAllNetworks();
+ getShadowConnectivityManager().addNetworkCapabilities(
+ ShadowNetwork.newInstance(ConnectivityManager.TYPE_MOBILE), mNetworkCapabilities);
+ when(mNetworkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)).thenReturn(
+ true);
+ when(mCarUserManagerHelper.getCurrentProcessUserInfo()).thenReturn(TEST_ADMIN_USER);
+ when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
+ UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)).thenReturn(false);
+ }
+
+ @After
+ public void tearDown() {
+ ShadowCarUserManagerHelper.reset();
+ ShadowConnectivityManager.reset();
+ ShadowTelephonyManager.reset();
+ }
+
+ @Test
+ public void onStart_phoneStateListenerSet() {
+ mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_START);
+ assertThat(getShadowTelephonyManager().getListenersForFlags(
+ PhoneStateListener.LISTEN_SERVICE_STATE).size()).isEqualTo(1);
+ }
+
+ @Test
+ public void onStop_phoneStateListenerUnset() {
+ mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_START);
+ assertThat(getShadowTelephonyManager().getListenersForFlags(
+ PhoneStateListener.LISTEN_SERVICE_STATE).size()).isEqualTo(1);
+
+ mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_STOP);
+ assertThat(getShadowTelephonyManager().getListenersForFlags(
+ PhoneStateListener.LISTEN_SERVICE_STATE).size()).isEqualTo(0);
+ }
+
+ @Test
+ public void onStart_airplaneModeChangedListenerSet() {
+ mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_START);
+ // One receiver (bluetooth pairing request) is always registered through the manifest.
+ assertThat(ShadowApplication.getInstance().getRegisteredReceivers().size()).isGreaterThan(
+ 0);
+
+ boolean hasMatch = false;
+ for (ShadowApplication.Wrapper wrapper :
+ ShadowApplication.getInstance().getRegisteredReceivers()) {
+ if (wrapper.getIntentFilter().getAction(0) == Intent.ACTION_AIRPLANE_MODE_CHANGED) {
+ hasMatch = true;
+ }
+ }
+ assertThat(hasMatch).isTrue();
+ }
+
+ @Test
+ public void onStop_airplaneModeChangedListenerUnset() {
+ mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_START);
+ int prevSize = ShadowApplication.getInstance().getRegisteredReceivers().size();
+ assertThat(prevSize).isGreaterThan(0);
+ mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_STOP);
+ assertThat(ShadowApplication.getInstance().getRegisteredReceivers().size()).isLessThan(
+ prevSize);
+
+ boolean hasMatch = false;
+ for (ShadowApplication.Wrapper wrapper :
+ ShadowApplication.getInstance().getRegisteredReceivers()) {
+ if (wrapper.getIntentFilter().getAction(0) == Intent.ACTION_AIRPLANE_MODE_CHANGED) {
+ hasMatch = true;
+ }
+ }
+ assertThat(hasMatch).isFalse();
+ }
+
+ @Test
+ public void getAvailabilityStatus_noMobileNetwork_unsupported() {
+ when(mNetworkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)).thenReturn(
+ false);
+
+ assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
+ }
+
+ @Test
+ public void getAvailabilityStatus_notAdmin_disabledForUser() {
+ when(mNetworkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)).thenReturn(
+ true);
+ when(mCarUserManagerHelper.getCurrentProcessUserInfo()).thenReturn(TEST_NON_ADMIN_USER);
+
+ assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER);
+ }
+
+ @Test
+ public void getAvailabilityStatus_hasRestriction_disabledForUser() {
+ when(mNetworkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)).thenReturn(
+ true);
+ when(mCarUserManagerHelper.getCurrentProcessUserInfo()).thenReturn(TEST_ADMIN_USER);
+ when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
+ UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)).thenReturn(true);
+
+ assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER);
+ }
+
+ @Test
+ public void getAvailabilityStatus_hasMobileNetwork_isAdmin_noRestriction_available() {
+ when(mNetworkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)).thenReturn(
+ true);
+ when(mCarUserManagerHelper.getCurrentProcessUserInfo()).thenReturn(TEST_ADMIN_USER);
+ when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
+ UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)).thenReturn(false);
+
+ assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
+ }
+
+ @Test
+ public void refreshUi_airplaneModeOn_preferenceDisabled() {
+ Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
+ mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_CREATE);
+ mController.refreshUi();
+
+ assertThat(mPreference.isEnabled()).isFalse();
+ }
+
+ @Test
+ public void refreshUi_airplaneModeOff_preferenceEnabled() {
+ Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0);
+ mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_CREATE);
+ mController.refreshUi();
+
+ assertThat(mPreference.isEnabled()).isTrue();
+ }
+
+ @Test
+ public void refreshUi_summarySet() {
+ getShadowTelephonyManager().setNetworkOperatorName(TEST_NETWORK_NAME);
+ mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_CREATE);
+ mController.refreshUi();
+
+ assertThat(mPreference.getSummary()).isEqualTo(TEST_NETWORK_NAME);
+ }
+
+ @Test
+ public void sendBroadcast_airplaneModeOn_disablePreference() {
+ mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_START);
+ mPreference.setEnabled(true);
+ Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
+ mContext.sendBroadcast(new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED));
+
+ assertThat(mPreference.isEnabled()).isFalse();
+ }
+
+ @Test
+ public void sendBroadcast_airplaneModeOff_enablePreference() {
+ mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_START);
+ mPreference.setEnabled(false);
+ Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0);
+ mContext.sendBroadcast(new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED));
+
+ assertThat(mPreference.isEnabled()).isTrue();
+ }
+
+ private ShadowTelephonyManager getShadowTelephonyManager() {
+ return (ShadowTelephonyManager) extract(mContext.getSystemService(TelephonyManager.class));
+ }
+
+ private ShadowConnectivityManager getShadowConnectivityManager() {
+ return (ShadowConnectivityManager) extract(
+ mContext.getSystemService(ConnectivityManager.class));
+ }
+}
diff --git a/tests/robotests/src/com/android/car/settings/testutils/ShadowConnectivityManager.java b/tests/robotests/src/com/android/car/settings/testutils/ShadowConnectivityManager.java
index 03a06dd..d0cce43 100644
--- a/tests/robotests/src/com/android/car/settings/testutils/ShadowConnectivityManager.java
+++ b/tests/robotests/src/com/android/car/settings/testutils/ShadowConnectivityManager.java
@@ -16,21 +16,40 @@
package com.android.car.settings.testutils;
+import static org.mockito.Mockito.mock;
+
import android.net.ConnectivityManager;
+import android.net.Network;
+import android.net.NetworkCapabilities;
+import android.net.NetworkInfo;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Resetter;
-@Implements(ConnectivityManager.class)
+import java.util.HashMap;
+import java.util.Map;
+
+@Implements(value = ConnectivityManager.class, inheritImplementationMethods = true)
public class ShadowConnectivityManager extends org.robolectric.shadows.ShadowConnectivityManager {
private static int sResetCalledCount = 0;
+ private final Map<Network, NetworkCapabilities> mCapabilitiesMap = new HashMap<>();
public static boolean verifyFactoryResetCalled(int numTimes) {
return sResetCalledCount == numTimes;
}
+ public void addNetworkCapabilities(Network network, NetworkCapabilities capabilities) {
+ super.addNetwork(network, mock(NetworkInfo.class));
+ mCapabilitiesMap.put(network, capabilities);
+ }
+
+ @Implementation
+ public NetworkCapabilities getNetworkCapabilities(Network network) {
+ return mCapabilitiesMap.get(network);
+ }
+
@Implementation
protected void factoryReset() {
sResetCalledCount++;
diff --git a/tests/robotests/src/com/android/car/settings/testutils/ShadowTelephonyManager.java b/tests/robotests/src/com/android/car/settings/testutils/ShadowTelephonyManager.java
index e349bfd..87c45db 100644
--- a/tests/robotests/src/com/android/car/settings/testutils/ShadowTelephonyManager.java
+++ b/tests/robotests/src/com/android/car/settings/testutils/ShadowTelephonyManager.java
@@ -16,6 +16,9 @@
package com.android.car.settings.testutils;
+import static android.telephony.PhoneStateListener.LISTEN_NONE;
+
+import android.telephony.PhoneStateListener;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
@@ -23,14 +26,17 @@
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Resetter;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
-@Implements(TelephonyManager.class)
+@Implements(value = TelephonyManager.class, inheritImplementationMethods = true)
public class ShadowTelephonyManager extends org.robolectric.shadows.ShadowTelephonyManager {
public static final String SUBSCRIBER_ID = "test_id";
private static Map<Integer, Integer> sSubIdsWithResetCalledCount = new HashMap<>();
+ private final Map<PhoneStateListener, Integer> mPhoneStateRegistrations = new HashMap<>();
public static boolean verifyFactoryResetCalled(int subId, int numTimes) {
if (!sSubIdsWithResetCalledCount.containsKey(subId)) return false;
@@ -38,6 +44,27 @@
}
@Implementation
+ public void listen(PhoneStateListener listener, int flags) {
+ super.listen(listener, flags);
+
+ if (flags == LISTEN_NONE) {
+ mPhoneStateRegistrations.remove(listener);
+ } else {
+ mPhoneStateRegistrations.put(listener, flags);
+ }
+ }
+
+ public List<PhoneStateListener> getListenersForFlags(int flags) {
+ List<PhoneStateListener> listeners = new ArrayList<>();
+ for (PhoneStateListener listener : mPhoneStateRegistrations.keySet()) {
+ if ((mPhoneStateRegistrations.get(listener) & flags) != 0) {
+ listeners.add(listener);
+ }
+ }
+ return listeners;
+ }
+
+ @Implementation
protected void factoryReset(int subId) {
sSubIdsWithResetCalledCount.put(subId,
sSubIdsWithResetCalledCount.getOrDefault(subId, 0) + 1);