Wi-Fi timeout feature
Also includes squashed change: Author: Tommy Webb <tommy@calyxinstitute.org> Date: Thu May 18 13:47:42 2023 -0400 fixup! Wi-Fi timeout feature Only allow admin users to adjust the Wi-Fi timeout. Issue: calyxos#1633 Change-Id: I9379bdd1fae136b2900692b4ea49c10106b3f285 Issue: calyxos#228 Change-Id: Ib35e45c38ca8c4f7146c8868b92ab98ca8d3c5b3 Signed-off-by: Pranav Vashi <neobuddy89@gmail.com>
This commit is contained in:
@@ -88,7 +88,10 @@
|
||||
<string name="soc_model">SoC Model</string>
|
||||
<string name="total_ram">Total RAM</string>
|
||||
|
||||
<!-- Bluetooth auto timeout -->
|
||||
<!-- Wifi/Bluetooth auto timeout -->
|
||||
<string name="wifi_timeout">WiFi timeout</string>
|
||||
<string name="wifi_timeout_summary">Do not automatically turn off WiFi</string>
|
||||
<string name="wifi_timeout_summary_auto">WiFi will turn off after <xliff:g id="timeout_description">%1$s</xliff:g> if no network connected</string>
|
||||
<string name="bluetooth_timeout">Bluetooth timeout</string>
|
||||
<string name="bluetooth_timeout_summary">Do not automatically turn off Bluetooth</string>
|
||||
<string name="bluetooth_timeout_summary_auto">Bluetooth will turn off after <xliff:g id="timeout_description">%1$s</xliff:g> if no devices connected</string>
|
||||
|
||||
@@ -26,6 +26,14 @@
|
||||
android:summary="@string/wifi_wakeup_summary"
|
||||
settings:controller="com.android.settings.wifi.WifiWakeupPreferenceController"/>
|
||||
|
||||
<ListPreference
|
||||
android:key="wifi_timeout"
|
||||
android:title="@string/wifi_timeout"
|
||||
android:summary="@string/wifi_timeout_summary"
|
||||
android:entries="@array/custom_timeout_entries"
|
||||
android:entryValues="@array/custom_timeout_values"
|
||||
settings:controller="com.android.settings.wifi.WifiTimeoutPreferenceController"/>
|
||||
|
||||
<SwitchPreferenceCompat
|
||||
android:key="notify_open_networks"
|
||||
android:title="@string/wifi_notify_open_networks"
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The Calyx Institute
|
||||
*
|
||||
* 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;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.UserManager;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.preference.ListPreference;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
|
||||
public class WifiTimeoutPreferenceController extends BasePreferenceController implements
|
||||
PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
|
||||
private static final String TAG = "WifiTimeoutPrefCtrl";
|
||||
|
||||
public static final int FALLBACK_WIFI_TIMEOUT_VALUE = 0;
|
||||
|
||||
private final String mWifiTimeoutKey;
|
||||
|
||||
protected WifiManager mWifiManager;
|
||||
|
||||
public WifiTimeoutPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
mWifiTimeoutKey = key;
|
||||
|
||||
mWifiManager = context.getSystemService(WifiManager.class);
|
||||
if (mWifiManager == null) {
|
||||
Log.e(TAG, "Wifi is not supported on this device");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
if (mWifiManager != null) {
|
||||
return UserManager.get(mContext).isAdminUser() ? AVAILABLE : DISABLED_FOR_USER;
|
||||
}
|
||||
return UNSUPPORTED_ON_DEVICE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return mWifiTimeoutKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
final ListPreference timeoutListPreference = (ListPreference) preference;
|
||||
final long currentTimeout = Settings.Global.getLong(mContext.getContentResolver(),
|
||||
Settings.Global.WIFI_OFF_TIMEOUT, FALLBACK_WIFI_TIMEOUT_VALUE);
|
||||
timeoutListPreference.setValue(String.valueOf(currentTimeout));
|
||||
updateTimeoutPreferenceDescription(timeoutListPreference,
|
||||
Long.parseLong(timeoutListPreference.getValue()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
try {
|
||||
long value = Long.parseLong((String) newValue);
|
||||
Settings.Global.putLong(mContext.getContentResolver(),
|
||||
Settings.Global.WIFI_OFF_TIMEOUT, value);
|
||||
updateTimeoutPreferenceDescription((ListPreference) preference, value);
|
||||
} catch (NumberFormatException e) {
|
||||
Log.e(TAG, "could not persist wifi timeout setting", e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static CharSequence getTimeoutDescription(
|
||||
long currentTimeout, CharSequence[] entries, CharSequence[] values) {
|
||||
if (currentTimeout < 0 || entries == null || values == null
|
||||
|| values.length != entries.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
long timeout = Long.parseLong(values[i].toString());
|
||||
if (currentTimeout == timeout) {
|
||||
return entries[i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void updateTimeoutPreferenceDescription(ListPreference preference,
|
||||
long currentTimeout) {
|
||||
final CharSequence[] entries = preference.getEntries();
|
||||
final CharSequence[] values = preference.getEntryValues();
|
||||
final CharSequence timeoutDescription = getTimeoutDescription(
|
||||
currentTimeout, entries, values);
|
||||
String summary = "";
|
||||
if (timeoutDescription != null) {
|
||||
if (currentTimeout != 0)
|
||||
summary = mContext.getString(R.string.wifi_timeout_summary_auto, timeoutDescription);
|
||||
else
|
||||
summary = mContext.getString(R.string.wifi_timeout_summary);
|
||||
}
|
||||
preference.setSummary(summary);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user