Add toggle for enable/disable auto hotspot off

Bug: 69323125
Test: RunSettingsRoboTests
Change-Id: I18896401240f75bdf2bdf187c2c7a7b623b20c43
This commit is contained in:
jackqdyulei
2017-12-20 11:22:52 -08:00
parent 444989ad35
commit c1cc490f74
5 changed files with 164 additions and 1 deletions

View File

@@ -2102,6 +2102,11 @@
<!-- Wifi hotspot footer info [CHAR LIMIT=NONE]--> <!-- Wifi hotspot footer info [CHAR LIMIT=NONE]-->
<string name="wifi_hotspot_footer_info_local_only">Apps can create a hotspot to share content with nearby devices.</string> <string name="wifi_hotspot_footer_info_local_only">Apps can create a hotspot to share content with nearby devices.</string>
<!-- Title for the toggle to turn off hotspot automatically [CHAR LIMIT=NONE]-->
<string name="wifi_hotspot_auto_off_title">Turn off hotspot automatically</string>
<!-- Summary for the toggle to turn off hotspot automatically [CHAR LIMIT=NONE]-->
<string name="wifi_hotspot_auto_off_summary">Wi\u2011Fi hotspot will turn off if no devices are connected</string>
<!-- Summary text when turning hotspot on --> <!-- Summary text when turning hotspot on -->
<string name="wifi_tether_starting">Turning hotspot on\u2026</string> <string name="wifi_tether_starting">Turning hotspot on\u2026</string>
<!-- Summary text when turning hotspot off --> <!-- Summary text when turning hotspot off -->

View File

@@ -17,7 +17,9 @@
<PreferenceScreen <PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/wifi_hotspot_checkbox_text"> xmlns:settings="http://schemas.android.com/apk/res-auto"
android:title="@string/wifi_hotspot_checkbox_text"
settings:initialExpandedChildrenCount="2">
<com.android.settings.widget.ValidatedEditTextPreference <com.android.settings.widget.ValidatedEditTextPreference
android:key="wifi_tether_network_name" android:key="wifi_tether_network_name"
@@ -28,6 +30,11 @@
android:key="wifi_tether_network_password" android:key="wifi_tether_network_password"
android:title="@string/wifi_hotspot_password_title" /> android:title="@string/wifi_hotspot_password_title" />
<SwitchPreference
android:key="wifi_tether_auto_turn_off"
android:title="@string/wifi_hotspot_auto_off_title"
android:summary="@string/wifi_hotspot_auto_off_summary"/>
<ListPreference <ListPreference
android:key="wifi_tether_network_ap_band" android:key="wifi_tether_network_ap_band"
android:title="@string/wifi_hotspot_ap_band_title" android:title="@string/wifi_hotspot_ap_band_title"

View File

@@ -0,0 +1,53 @@
/*
* Copyright (C) 2017 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 android.provider.Settings;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;
import com.android.settings.core.BasePreferenceController;
public class WifiTetherAutoOffPreferenceController extends BasePreferenceController implements
Preference.OnPreferenceChangeListener {
public WifiTetherAutoOffPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public void updateState(Preference preference) {
final boolean settingsOn = Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.SOFT_AP_TIMEOUT_ENABLED, 0) != 0;
((SwitchPreference) preference).setChecked(settingsOn);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean settingsOn = (Boolean) newValue;
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.SOFT_AP_TIMEOUT_ENABLED, settingsOn ? 1 : 0);
return true;
}
}

View File

@@ -48,6 +48,7 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
private static final String TAG = "WifiTetherSettings"; private static final String TAG = "WifiTetherSettings";
private static final IntentFilter TETHER_STATE_CHANGE_FILTER; private static final IntentFilter TETHER_STATE_CHANGE_FILTER;
private static final String KEY_WIFI_TETHER_AUTO_OFF = "wifi_tether_auto_turn_off";
private WifiTetherSwitchBarController mSwitchBarController; private WifiTetherSwitchBarController mSwitchBarController;
private WifiTetherSSIDPreferenceController mSSIDPreferenceController; private WifiTetherSSIDPreferenceController mSSIDPreferenceController;
@@ -133,6 +134,8 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
controllers.add(mSSIDPreferenceController); controllers.add(mSSIDPreferenceController);
controllers.add(mPasswordPreferenceController); controllers.add(mPasswordPreferenceController);
controllers.add(mApBandPreferenceController); controllers.add(mApBandPreferenceController);
controllers.add(
new WifiTetherAutoOffPreferenceController(context, KEY_WIFI_TETHER_AUTO_OFF));
return controllers; return controllers;
} }

View File

@@ -0,0 +1,95 @@
/*
* Copyright (C) 2017 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 com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.provider.Settings;
import android.support.v14.preference.SwitchPreference;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class WifiTetherAutoOffPreferenceControllerTest {
private static final String KEY_PREF = "wifi_tether_auto_off";
private static final int ON = 1;
private static final int OFF = 0;
private Context mContext;
private WifiTetherAutoOffPreferenceController mController;
private SwitchPreference mSwitchPreference;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = new WifiTetherAutoOffPreferenceController(mContext, KEY_PREF);
mSwitchPreference = new SwitchPreference(mContext);
}
@Test
public void testOnPreferenceChange_toggleOn_settingsOn() {
mController.onPreferenceChange(null, true);
assertThat(getAutoOffSetting()).isEqualTo(ON);
}
@Test
public void testOnPreferenceChange_toggleOff_settingsOff() {
mController.onPreferenceChange(null, false);
assertThat(getAutoOffSetting()).isEqualTo(OFF);
}
@Test
public void testUpdateState_settingsOn_toggleOn() {
setAutoOffSetting(ON);
mController.updateState(mSwitchPreference);
assertThat(mSwitchPreference.isChecked()).isTrue();
}
@Test
public void testUpdateState_settingsOff_toggleOff() {
setAutoOffSetting(OFF);
mController.updateState(mSwitchPreference);
assertThat(mSwitchPreference.isChecked()).isFalse();
}
private int getAutoOffSetting() {
return Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.SOFT_AP_TIMEOUT_ENABLED, OFF);
}
private void setAutoOffSetting(int config) {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.SOFT_AP_TIMEOUT_ENABLED, config);
}
}