From 7dda07fe8165b7522e70feaedbbad031f3770d59 Mon Sep 17 00:00:00 2001 From: Arc Wang Date: Fri, 3 Jan 2020 15:56:31 +0800 Subject: [PATCH] [Wi-Fi] Implement Wi-Fi Auto-connect feature This feature allows users to specify if a Wi-Fi network will be auto connected after disconnection. Bug: 136472407 Test: make RunSettingsRoboTests ROBOTEST_FILTER=WifiAutoConnectPreferenceController2Test Change-Id: I8a7c02ba3fc4cbfbfb431fa99c42279ca29e9492 --- res/values/strings.xml | 4 + res/xml/wifi_network_details_fragment2.xml | 141 ++++++++++++++++++ .../WifiAutoConnectPreferenceController2.java | 57 +++++++ .../details2/WifiNetworkDetailsFragment2.java | 7 +- .../grandfather_slice_controller_not_in_xml | 1 + ...iAutoConnectPreferenceController2Test.java | 89 +++++++++++ 6 files changed, 298 insertions(+), 1 deletion(-) create mode 100644 res/xml/wifi_network_details_fragment2.xml create mode 100644 src/com/android/settings/wifi/details2/WifiAutoConnectPreferenceController2.java create mode 100644 tests/robotests/src/com/android/settings/wifi/details2/WifiAutoConnectPreferenceController2Test.java diff --git a/res/values/strings.xml b/res/values/strings.xml index 74f4621e338..cfd313c5e6b 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -2091,6 +2091,10 @@ Wi\u2011Fi password: %1$s Hotspot password: %1$s + + Auto\u2011connect + + Allow connection to this network when in range Add device diff --git a/res/xml/wifi_network_details_fragment2.xml b/res/xml/wifi_network_details_fragment2.xml new file mode 100644 index 00000000000..3058678c036 --- /dev/null +++ b/res/xml/wifi_network_details_fragment2.xml @@ -0,0 +1,141 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/com/android/settings/wifi/details2/WifiAutoConnectPreferenceController2.java b/src/com/android/settings/wifi/details2/WifiAutoConnectPreferenceController2.java new file mode 100644 index 00000000000..ffbb68289b5 --- /dev/null +++ b/src/com/android/settings/wifi/details2/WifiAutoConnectPreferenceController2.java @@ -0,0 +1,57 @@ +/* + * 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.details2; + +import android.content.Context; + +import com.android.settings.core.TogglePreferenceController; +import com.android.wifitrackerlib.WifiEntry; + +/** + * {@link TogglePreferenceController} that controls whether the Wi-Fi Auto-connect feature should be + * enabled. + */ +public class WifiAutoConnectPreferenceController2 extends TogglePreferenceController { + + private static final String KEY_AUTO_CONNECT = "auto_connect"; + + private WifiEntry mWifiEntry; + + public WifiAutoConnectPreferenceController2(Context context) { + super(context, KEY_AUTO_CONNECT); + } + + public void setWifiEntry(WifiEntry wifiEntry) { + mWifiEntry = wifiEntry; + } + + @Override + public int getAvailabilityStatus() { + return mWifiEntry.canSetAutoJoinEnabled() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; + } + + @Override + public boolean isChecked() { + return mWifiEntry.isAutoJoinEnabled(); + } + + @Override + public boolean setChecked(boolean isChecked) { + mWifiEntry.setAutoJoinEnabled(isChecked); + return true; + } +} diff --git a/src/com/android/settings/wifi/details2/WifiNetworkDetailsFragment2.java b/src/com/android/settings/wifi/details2/WifiNetworkDetailsFragment2.java index bdd63dc2bf0..d07d6d6b3de 100644 --- a/src/com/android/settings/wifi/details2/WifiNetworkDetailsFragment2.java +++ b/src/com/android/settings/wifi/details2/WifiNetworkDetailsFragment2.java @@ -91,7 +91,7 @@ public class WifiNetworkDetailsFragment2 extends DashboardFragment implements @Override protected int getPreferenceScreenResId() { - return R.xml.wifi_network_details_fragment; + return R.xml.wifi_network_details_fragment2; } @Override @@ -155,6 +155,11 @@ public class WifiNetworkDetailsFragment2 extends DashboardFragment implements mMetricsFeatureProvider); controllers.add(mWifiDetailPreferenceController2); + final WifiAutoConnectPreferenceController2 wifiAutoConnectPreferenceController2 = + new WifiAutoConnectPreferenceController2(context); + wifiAutoConnectPreferenceController2.setWifiEntry(wifiEntry); + controllers.add(wifiAutoConnectPreferenceController2); + final AddDevicePreferenceController2 addDevicePreferenceController2 = new AddDevicePreferenceController2(context); addDevicePreferenceController2.setWifiEntry(wifiEntry); diff --git a/tests/robotests/assets/grandfather_slice_controller_not_in_xml b/tests/robotests/assets/grandfather_slice_controller_not_in_xml index 94d581581bb..5cd855f6c94 100644 --- a/tests/robotests/assets/grandfather_slice_controller_not_in_xml +++ b/tests/robotests/assets/grandfather_slice_controller_not_in_xml @@ -7,4 +7,5 @@ com.android.settings.network.telephony.Enhanced4gBasePreferenceController com.android.settings.testutils.FakeToggleController com.android.settings.testutils.FakeSliderController com.android.settings.testutils.FakeInvalidSliderController +com.android.settings.wifi.details2.WifiAutoConnectPreferenceController2 diff --git a/tests/robotests/src/com/android/settings/wifi/details2/WifiAutoConnectPreferenceController2Test.java b/tests/robotests/src/com/android/settings/wifi/details2/WifiAutoConnectPreferenceController2Test.java new file mode 100644 index 00000000000..7e604a3decc --- /dev/null +++ b/tests/robotests/src/com/android/settings/wifi/details2/WifiAutoConnectPreferenceController2Test.java @@ -0,0 +1,89 @@ +/* + * 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.details2; + +import static com.android.settings.core.BasePreferenceController.AVAILABLE; +import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.android.wifitrackerlib.WifiEntry; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; + +@RunWith(RobolectricTestRunner.class) +public class WifiAutoConnectPreferenceController2Test { + + private WifiAutoConnectPreferenceController2 mController; + @Mock + private WifiEntry mWifiEntry; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mController = new WifiAutoConnectPreferenceController2(RuntimeEnvironment.application); + mController.setWifiEntry(mWifiEntry); + } + + @Test + public void getAvailabilityStatus_shouldFollowCanSetAutoJoinEnabled() { + // Test able to set auto join. + when(mWifiEntry.canSetAutoJoinEnabled()).thenReturn(true); + + assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); + + // Test not able to set auto join. + when(mWifiEntry.canSetAutoJoinEnabled()).thenReturn(false); + + assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); + } + + @Test + public void isChecked_shouldFollowIsAutoJoinEnabled() { + // Test auto join enabled. + when(mWifiEntry.isAutoJoinEnabled()).thenReturn(true); + + assertThat(mController.isChecked()).isTrue(); + + // Test auto join disabled. + when(mWifiEntry.isAutoJoinEnabled()).thenReturn(false); + + assertThat(mController.isChecked()).isFalse(); + } + + @Test + public void setChecked_shouldSetAutoJoinEnabled() { + // Test checked. + mController.setChecked(true); + + verify(mWifiEntry).setAutoJoinEnabled(true); + + // Test unchecked. + mController.setChecked(false); + + verify(mWifiEntry).setAutoJoinEnabled(false); + } +}