Files
packages_apps_Settings/src/com/android/settings/bluetooth/BluetoothTimeoutPreferenceController.java
Oliver Scott 03f2f52e6f 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>
2025-12-10 00:41:26 +09:00

122 lines
4.5 KiB
Java

/*
* 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);
}
}