Merge "Add toggle for enable/disable auto hotspot off"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index a01967f..851a925 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -2108,6 +2108,11 @@
     <!-- 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>
 
+    <!-- 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 -->
     <string name="wifi_tether_starting">Turning hotspot on\u2026</string>
     <!-- Summary text when turning hotspot off -->
diff --git a/res/xml/wifi_tether_settings.xml b/res/xml/wifi_tether_settings.xml
index d6b8a25..bb9af88 100644
--- a/res/xml/wifi_tether_settings.xml
+++ b/res/xml/wifi_tether_settings.xml
@@ -17,7 +17,9 @@
 
 <PreferenceScreen
     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
         android:key="wifi_tether_network_name"
@@ -28,6 +30,11 @@
         android:key="wifi_tether_network_password"
         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
         android:key="wifi_tether_network_ap_band"
         android:title="@string/wifi_hotspot_ap_band_title"
diff --git a/src/com/android/settings/wifi/tether/WifiTetherAutoOffPreferenceController.java b/src/com/android/settings/wifi/tether/WifiTetherAutoOffPreferenceController.java
new file mode 100644
index 0000000..5fa4e25
--- /dev/null
+++ b/src/com/android/settings/wifi/tether/WifiTetherAutoOffPreferenceController.java
@@ -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;
+    }
+}
diff --git a/src/com/android/settings/wifi/tether/WifiTetherSettings.java b/src/com/android/settings/wifi/tether/WifiTetherSettings.java
index d06ad4a..e9533b1 100644
--- a/src/com/android/settings/wifi/tether/WifiTetherSettings.java
+++ b/src/com/android/settings/wifi/tether/WifiTetherSettings.java
@@ -48,6 +48,7 @@
 
     private static final String TAG = "WifiTetherSettings";
     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 WifiTetherSSIDPreferenceController mSSIDPreferenceController;
@@ -133,6 +134,8 @@
         controllers.add(mSSIDPreferenceController);
         controllers.add(mPasswordPreferenceController);
         controllers.add(mApBandPreferenceController);
+        controllers.add(
+                new WifiTetherAutoOffPreferenceController(context, KEY_WIFI_TETHER_AUTO_OFF));
         return controllers;
     }
 
diff --git a/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherAutoOffPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherAutoOffPreferenceControllerTest.java
new file mode 100644
index 0000000..3f820c3
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherAutoOffPreferenceControllerTest.java
@@ -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);
+    }
+}