Bluetooth timeout feature

* Now use AOSP settings storage instead of LineageSettings.
  This is so the Bluetooth APEX module can access the setting.

Also includes squashed change:

Author: Tommy Webb <tommy@calyxinstitute.org>
Date:   Thu May 18 13:49:24 2023 -0400

    fixup! Bluetooth timeout feature

    Only allow admin users to adjust the Bluetooth timeout.

    Issue: calyxos#1633
    Change-Id: I8b3ae8f0faffde194ee417548e7d200842000fb6

Issue: calyxos#124
Change-Id: I4f4646139cd42b7ad20b486ed77dd00499aa3c6e
Signed-off-by: Pranav Vashi <neobuddy89@gmail.com>
This commit is contained in:
Oliver Scott
2020-10-02 12:37:30 -04:00
committed by Joey
parent 67f93a4e84
commit 03f2f52e6f
5 changed files with 187 additions and 0 deletions

View File

@@ -4249,6 +4249,13 @@
</intent-filter>
</receiver>
<receiver android:name="com.android.settingslib.bluetooth.BluetoothTimeoutReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.bluetooth.intent.TIMEOUT" />
</intent-filter>
</receiver>
<!-- Watch for ContactsContract.Profile changes and update the user's photo. -->
<receiver android:name=".users.ProfileUpdateReceiver"
android:exported="true">

View File

@@ -17,4 +17,35 @@
<item>1</item>
<item>0</item>
</string-array>
<!-- Custom timeout for Wifi/Bluetooth -->
<string-array name="custom_timeout_entries">
<item>@string/custom_timeout_summary_never</item>
<item>@string/custom_timeout_summary_15secs</item>
<item>@string/custom_timeout_summary_30secs</item>
<item>@string/custom_timeout_summary_1min</item>
<item>@string/custom_timeout_summary_2mins</item>
<item>@string/custom_timeout_summary_5mins</item>
<item>@string/custom_timeout_summary_10mins</item>
<item>@string/custom_timeout_summary_30mins</item>
<item>@string/custom_timeout_summary_1hour</item>
<item>@string/custom_timeout_summary_2hours</item>
<item>@string/custom_timeout_summary_4hours</item>
<item>@string/custom_timeout_summary_8hours</item>
</string-array>
<string-array name="custom_timeout_values" translatable="false">
<item>0</item>
<item>15000</item>
<item>30000</item>
<item>60000</item>
<item>120000</item>
<item>300000</item>
<item>600000</item>
<item>1800000</item>
<item>3600000</item>
<item>7200000</item>
<item>14400000</item>
<item>28800000</item>
</string-array>
</resources>

View File

@@ -87,4 +87,24 @@
<string name="device_market_name">Market Name</string>
<string name="soc_model">SoC Model</string>
<string name="total_ram">Total RAM</string>
<!-- Bluetooth auto timeout -->
<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>
<string name="custom_timeout_summary_never">Never</string>
<string name="custom_timeout_summary_5secs">5 seconds</string>
<string name="custom_timeout_summary_10secs">10 seconds</string>
<string name="custom_timeout_summary_15secs">15 seconds</string>
<string name="custom_timeout_summary_30secs">30 seconds</string>
<string name="custom_timeout_summary_1min">1 minute</string>
<string name="custom_timeout_summary_2mins">2 minutes</string>
<string name="custom_timeout_summary_5mins">5 minutes</string>
<string name="custom_timeout_summary_10mins">10 minutes</string>
<string name="custom_timeout_summary_20mins">20 minutes</string>
<string name="custom_timeout_summary_30mins">30 minutes</string>
<string name="custom_timeout_summary_1hour">1 hour</string>
<string name="custom_timeout_summary_2hours">2 hours</string>
<string name="custom_timeout_summary_4hours">4 hours</string>
<string name="custom_timeout_summary_8hours">8 hours</string>
</resources>

View File

@@ -61,6 +61,14 @@
settings:useAdminDisabledSummary="true"
settings:userRestriction="no_config_bluetooth" />
<androidx.preference.ListPreference
android:key="bluetooth_timeout"
android:title="@string/bluetooth_timeout"
android:summary="@string/summary_placeholder"
android:entries="@array/custom_timeout_entries"
android:entryValues="@array/custom_timeout_values"
settings:controller="com.android.settings.bluetooth.BluetoothTimeoutPreferenceController"/>
<PreferenceCategory
android:key="previously_connected_devices"
android:title="@string/connected_device_saved_title"

View File

@@ -0,0 +1,121 @@
/*
* Copyright (C) 2020-2021 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.bluetooth;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.os.UserManager;
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 BluetoothTimeoutPreferenceController extends BasePreferenceController implements
PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
private static final String TAG = "BluetoothTimeoutPrefCtrl";
public static final int FALLBACK_BLUETOOTH_TIMEOUT_VALUE = 0;
private final String mBluetoothTimeoutKey;
protected BluetoothAdapter mBluetoothAdapter;
public BluetoothTimeoutPreferenceController(Context context, String key) {
super(context, key);
mBluetoothTimeoutKey = key;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Log.e(TAG, "Bluetooth is not supported on this device");
return;
}
}
@Override
public int getAvailabilityStatus() {
if (mBluetoothAdapter != null) {
return UserManager.get(mContext).isAdminUser() ? AVAILABLE : DISABLED_FOR_USER;
}
return UNSUPPORTED_ON_DEVICE;
}
@Override
public String getPreferenceKey() {
return mBluetoothTimeoutKey;
}
@Override
public void updateState(Preference preference) {
final ListPreference timeoutListPreference = (ListPreference) preference;
final long currentTimeout = Settings.Global.getLong(mContext.getContentResolver(),
Settings.Global.BLUETOOTH_OFF_TIMEOUT, FALLBACK_BLUETOOTH_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.BLUETOOTH_OFF_TIMEOUT, value);
updateTimeoutPreferenceDescription((ListPreference) preference, value);
} catch (NumberFormatException e) {
Log.e(TAG, "could not persist bluetooth 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.bluetooth_timeout_summary_auto,
timeoutDescription);
else
summary = mContext.getString(R.string.bluetooth_timeout_summary);
}
preference.setSummary(summary);
}
}