From 005fe6951ca72354ce0ffd22659c69c42ef439c7 Mon Sep 17 00:00:00 2001 From: Arc Wang Date: Wed, 4 Mar 2020 21:03:54 +0800 Subject: [PATCH] [Wi-Fi] Add STA+AP information in Hotspot & tethering footer Use WifiManager#isStaApConcurrencySupported() to check if a device supports STA+AP and shows the footer information. Bug: 121328949 Test: make RunSettingsRoboTests ROBOTEST_FILTER=TetherSettingsTest make RunSettingsRoboTests ROBOTEST_FILTER=WifiTetherFooterPreferenceControllerTest Change-Id: I3446abb5cc9cdf76da8d17f5de31bd65715c165c --- res/values/strings.xml | 2 + res/xml/all_tether_prefs.xml | 1 - res/xml/tether_prefs.xml | 1 - .../settings/AllInOneTetherSettings.java | 5 +- src/com/android/settings/TetherSettings.java | 16 ++++ .../WifiConnectionPreferenceController.java | 1 + .../WifiMasterSwitchPreferenceController.java | 1 + .../WifiDetailPreferenceController2.java | 1 + .../WifiTetherBasePreferenceController.java | 1 + .../WifiTetherFooterPreferenceController.java | 54 ++++++++++++ .../android/settings/TetherSettingsTest.java | 23 +++++ ...iTetherFooterPreferenceControllerTest.java | 87 +++++++++++++++++++ 12 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 src/com/android/settings/wifi/tether/WifiTetherFooterPreferenceController.java create mode 100644 tests/robotests/src/com/android/settings/wifi/tether/WifiTetherFooterPreferenceControllerTest.java diff --git a/res/values/strings.xml b/res/values/strings.xml index 608eac8b73e..92e25277592 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -3814,6 +3814,8 @@ Use hotspot and tethering to provide internet to other devices through your mobile data connection. Apps can also create a hotspot to share content with nearby devices. + + Use hotspot and tethering to provide internet to other devices through your Wi\u2011Fi or mobile data connection. Apps can also create a hotspot to share content with nearby devices. Help diff --git a/res/xml/all_tether_prefs.xml b/res/xml/all_tether_prefs.xml index f5525229028..294e9752312 100644 --- a/res/xml/all_tether_prefs.xml +++ b/res/xml/all_tether_prefs.xml @@ -88,7 +88,6 @@ diff --git a/res/xml/tether_prefs.xml b/res/xml/tether_prefs.xml index 70d55aa5124..abc49cc9ff9 100644 --- a/res/xml/tether_prefs.xml +++ b/res/xml/tether_prefs.xml @@ -46,7 +46,6 @@ diff --git a/src/com/android/settings/AllInOneTetherSettings.java b/src/com/android/settings/AllInOneTetherSettings.java index a15ad1f7818..251d1357b25 100644 --- a/src/com/android/settings/AllInOneTetherSettings.java +++ b/src/com/android/settings/AllInOneTetherSettings.java @@ -56,6 +56,7 @@ import com.android.settings.widget.SwitchBarController; import com.android.settings.wifi.tether.WifiTetherApBandPreferenceController; import com.android.settings.wifi.tether.WifiTetherAutoOffPreferenceController; import com.android.settings.wifi.tether.WifiTetherBasePreferenceController; +import com.android.settings.wifi.tether.WifiTetherFooterPreferenceController; import com.android.settings.wifi.tether.WifiTetherPasswordPreferenceController; import com.android.settings.wifi.tether.WifiTetherSSIDPreferenceController; import com.android.settings.wifi.tether.WifiTetherSecurityPreferenceController; @@ -71,7 +72,7 @@ import java.util.concurrent.atomic.AtomicReference; * Displays preferences for all Tethering options. */ @SearchIndexable -public final class AllInOneTetherSettings extends RestrictedDashboardFragment +public class AllInOneTetherSettings extends RestrictedDashboardFragment implements DataSaverBackend.Listener, WifiTetherBasePreferenceController.OnTetherConfigUpdateListener, SharedPreferences.OnSharedPreferenceChangeListener { @@ -304,6 +305,8 @@ public final class AllInOneTetherSettings extends RestrictedDashboardFragment new WifiTetherSecurityPreferenceController(context, listener)); controllers.add( new WifiTetherAutoOffPreferenceController(context, KEY_WIFI_TETHER_AUTO_OFF)); + controllers.add( + new WifiTetherFooterPreferenceController(context)); return controllers; } diff --git a/src/com/android/settings/TetherSettings.java b/src/com/android/settings/TetherSettings.java index a29ec9574bc..cce20105236 100644 --- a/src/com/android/settings/TetherSettings.java +++ b/src/com/android/settings/TetherSettings.java @@ -31,6 +31,7 @@ import android.content.IntentFilter; import android.content.pm.PackageManager; import android.hardware.usb.UsbManager; import android.net.ConnectivityManager; +import android.net.wifi.WifiManager; import android.os.Bundle; import android.os.Environment; import android.os.Handler; @@ -71,6 +72,8 @@ public class TetherSettings extends RestrictedSettingsFragment @VisibleForTesting static final String KEY_ENABLE_BLUETOOTH_TETHERING = "enable_bluetooth_tethering"; private static final String KEY_DATA_SAVER_FOOTER = "disabled_on_data_saver"; + @VisibleForTesting + static final String KEY_TETHER_PREFS_FOOTER = "tether_prefs_footer"; private static final String TAG = "TetheringSettings"; @@ -141,6 +144,7 @@ public class TetherSettings extends RestrictedSettingsFragment mUsbTether = (SwitchPreference) findPreference(KEY_USB_TETHER_SETTINGS); mBluetoothTether = (SwitchPreference) findPreference(KEY_ENABLE_BLUETOOTH_TETHERING); + setFooterPreferenceTitle(); mDataSaverBackend.addListener(this); @@ -201,6 +205,18 @@ public class TetherSettings extends RestrictedSettingsFragment public void onBlacklistStatusChanged(int uid, boolean isBlacklisted) { } + @VisibleForTesting + void setFooterPreferenceTitle() { + final Preference footerPreference = findPreference(KEY_TETHER_PREFS_FOOTER); + final WifiManager wifiManager = + (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE); + if (wifiManager.isStaApConcurrencySupported()) { + footerPreference.setTitle(R.string.tethering_footer_info_sta_ap_concurrency); + } else { + footerPreference.setTitle(R.string.tethering_footer_info); + } + } + private class TetherChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context content, Intent intent) { diff --git a/src/com/android/settings/wifi/WifiConnectionPreferenceController.java b/src/com/android/settings/wifi/WifiConnectionPreferenceController.java index 2c6feacc34c..12a6d143656 100644 --- a/src/com/android/settings/wifi/WifiConnectionPreferenceController.java +++ b/src/com/android/settings/wifi/WifiConnectionPreferenceController.java @@ -34,6 +34,7 @@ import com.android.settingslib.wifi.AccessPointPreference; import com.android.settingslib.wifi.WifiTracker; import com.android.settingslib.wifi.WifiTrackerFactory; +// TODO(b/151133650): Replace AbstractPreferenceController with BasePreferenceController. /** * This places a preference into a PreferenceGroup owned by some parent * controller class when there is a wifi connection present. diff --git a/src/com/android/settings/wifi/WifiMasterSwitchPreferenceController.java b/src/com/android/settings/wifi/WifiMasterSwitchPreferenceController.java index 2f57249c3a2..1e18c598d74 100644 --- a/src/com/android/settings/wifi/WifiMasterSwitchPreferenceController.java +++ b/src/com/android/settings/wifi/WifiMasterSwitchPreferenceController.java @@ -32,6 +32,7 @@ import com.android.settingslib.core.lifecycle.events.OnResume; import com.android.settingslib.core.lifecycle.events.OnStart; import com.android.settingslib.core.lifecycle.events.OnStop; +// TODO(b/151133650): Replace AbstractPreferenceController with BasePreferenceController. public class WifiMasterSwitchPreferenceController extends AbstractPreferenceController implements PreferenceControllerMixin, SummaryUpdater.OnSummaryChangeListener, LifecycleObserver, OnResume, OnPause, OnStart, OnStop { diff --git a/src/com/android/settings/wifi/details2/WifiDetailPreferenceController2.java b/src/com/android/settings/wifi/details2/WifiDetailPreferenceController2.java index 4acfee19e21..b5f1cea7621 100644 --- a/src/com/android/settings/wifi/details2/WifiDetailPreferenceController2.java +++ b/src/com/android/settings/wifi/details2/WifiDetailPreferenceController2.java @@ -101,6 +101,7 @@ import java.time.format.FormatStyle; import java.util.StringJoiner; import java.util.stream.Collectors; +// TODO(b/151133650): Replace AbstractPreferenceController with BasePreferenceController. /** * Controller for logic pertaining to displaying Wifi information for the * {@link WifiNetworkDetailsFragment}. diff --git a/src/com/android/settings/wifi/tether/WifiTetherBasePreferenceController.java b/src/com/android/settings/wifi/tether/WifiTetherBasePreferenceController.java index 2a5dea9e48b..7ba02168c70 100644 --- a/src/com/android/settings/wifi/tether/WifiTetherBasePreferenceController.java +++ b/src/com/android/settings/wifi/tether/WifiTetherBasePreferenceController.java @@ -26,6 +26,7 @@ import androidx.preference.PreferenceScreen; import com.android.settings.core.PreferenceControllerMixin; import com.android.settingslib.core.AbstractPreferenceController; +// TODO(b/151133650): Replace AbstractPreferenceController with BasePreferenceController. public abstract class WifiTetherBasePreferenceController extends AbstractPreferenceController implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener { diff --git a/src/com/android/settings/wifi/tether/WifiTetherFooterPreferenceController.java b/src/com/android/settings/wifi/tether/WifiTetherFooterPreferenceController.java new file mode 100644 index 00000000000..c6a0a9f5e7d --- /dev/null +++ b/src/com/android/settings/wifi/tether/WifiTetherFooterPreferenceController.java @@ -0,0 +1,54 @@ +/* + * 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.wifi.tether; + +import android.content.Context; + +import androidx.preference.Preference; + +import com.android.settings.R; + +/** + * Shows hotspot footer information. + */ +public class WifiTetherFooterPreferenceController extends WifiTetherBasePreferenceController { + + private static final String PREF_KEY = "tether_prefs_footer_2"; + + public WifiTetherFooterPreferenceController(Context context) { + super(context, null /* listener */); + } + + @Override + public String getPreferenceKey() { + return PREF_KEY; + } + + @Override + public void updateDisplay() { + if (mWifiManager.isStaApConcurrencySupported()) { + mPreference.setTitle(R.string.tethering_footer_info_sta_ap_concurrency); + } else { + mPreference.setTitle(R.string.tethering_footer_info); + } + } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + return true; + } +} diff --git a/tests/robotests/src/com/android/settings/TetherSettingsTest.java b/tests/robotests/src/com/android/settings/TetherSettingsTest.java index e69bd073f52..17c8fe8596d 100644 --- a/tests/robotests/src/com/android/settings/TetherSettingsTest.java +++ b/tests/robotests/src/com/android/settings/TetherSettingsTest.java @@ -19,14 +19,20 @@ package com.android.settings; import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.content.Context; import android.net.ConnectivityManager; +import android.net.wifi.WifiManager; import android.os.UserHandle; import android.os.UserManager; +import androidx.preference.Preference; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -131,6 +137,23 @@ public class TetherSettingsTest { assertThat(niks).doesNotContain(TetherSettings.KEY_ENABLE_BLUETOOTH_TETHERING); } + @Test + public void testSetFooterPreferenceTitle_isStaApConcurrencySupported_showStaApString() { + final TetherSettings spyTetherSettings = spy(new TetherSettings()); + when(spyTetherSettings.getContext()).thenReturn(mContext); + final Preference mockPreference = mock(Preference.class); + when(spyTetherSettings.findPreference(TetherSettings.KEY_TETHER_PREFS_FOOTER)) + .thenReturn(mockPreference); + final WifiManager mockWifiManager = mock(WifiManager.class); + when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mockWifiManager); + when(mockWifiManager.isStaApConcurrencySupported()).thenReturn(true); + + spyTetherSettings.setFooterPreferenceTitle(); + + verify(mockPreference, never()).setTitle(R.string.tethering_footer_info); + verify(mockPreference).setTitle(R.string.tethering_footer_info_sta_ap_concurrency); + } + private void setupIsTetherAvailable(boolean returnValue) { when(mConnectivityManager.isTetheringSupported()).thenReturn(true); diff --git a/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherFooterPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherFooterPreferenceControllerTest.java new file mode 100644 index 00000000000..b0ed8622e98 --- /dev/null +++ b/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherFooterPreferenceControllerTest.java @@ -0,0 +1,87 @@ +/* + * 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.wifi.tether; + +import static org.mockito.ArgumentMatchers.anyString;; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.wifi.WifiManager; + +import androidx.preference.PreferenceScreen; + +import com.android.settings.R; +import com.android.settingslib.widget.FooterPreference; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Answers; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; + +@RunWith(RobolectricTestRunner.class) +public class WifiTetherFooterPreferenceControllerTest { + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private Context mContext; + @Mock + private ConnectivityManager mConnectivityManager; + @Mock + private WifiManager mWifiManager; + @Mock + private PreferenceScreen mScreen; + @Mock + private FooterPreference mPreference; + + private WifiTetherFooterPreferenceController mController; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager); + when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)) + .thenReturn(mConnectivityManager); + when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"}); + mController = new WifiTetherFooterPreferenceController(mContext); + when(mScreen.findPreference(anyString())).thenReturn(mPreference); + } + + @Test + public void displayPreference_supportStaAp_showConcurrentInfo() { + when(mWifiManager.isStaApConcurrencySupported()).thenReturn(true); + + mController.displayPreference(mScreen); + + verify(mPreference, never()).setTitle(R.string.tethering_footer_info); + verify(mPreference).setTitle(R.string.tethering_footer_info_sta_ap_concurrency); + } + + @Test + public void displayPreference_notSupportStaAp_showNotConcurrentInfo() { + when(mWifiManager.isStaApConcurrencySupported()).thenReturn(false); + + mController.displayPreference(mScreen); + + verify(mPreference).setTitle(R.string.tethering_footer_info); + verify(mPreference, never()).setTitle(R.string.tethering_footer_info_sta_ap_concurrency); + } +}