Merge "Refactor Bluetooth scan mode APIs"

This commit is contained in:
Etienne Ruffieux
2021-11-29 20:27:45 +00:00
committed by Android (Google) Code Review
2 changed files with 27 additions and 19 deletions

View File

@@ -31,6 +31,8 @@ import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settingslib.bluetooth.BluetoothDiscoverableTimeoutReceiver;
import java.time.Duration;
/**
* BluetoothDiscoverableEnabler is a helper to manage the "Discoverable"
* checkbox. It sets/unsets discoverability and keeps track of how much time
@@ -136,9 +138,8 @@ final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceClick
int timeout = getDiscoverableTimeout();
long endTimestamp = System.currentTimeMillis() + timeout * 1000L;
LocalBluetoothPreferences.persistDiscoverableEndTimestamp(mContext, endTimestamp);
mBluetoothAdapter
.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, timeout);
mBluetoothAdapter.setDiscoverableTimeout(Duration.ofSeconds(timeout));
mBluetoothAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
updateCountdownSummary();
Log.d(TAG, "setEnabled(): enabled = " + enable + "timeout = " + timeout);

View File

@@ -16,10 +16,13 @@
package com.android.settings.bluetooth;
import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
import android.annotation.NonNull;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothStatusCodes;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
@@ -39,7 +42,7 @@ import androidx.appcompat.app.AlertDialog;
import com.android.settings.R;
import com.android.settingslib.bluetooth.BluetoothDiscoverableTimeoutReceiver;
import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
import java.time.Duration;
/**
* RequestPermissionActivity asks the user whether to enable discovery. This is
@@ -261,8 +264,11 @@ public class RequestPermissionActivity extends Activity implements
if (mRequest == REQUEST_ENABLE || mRequest == REQUEST_DISABLE) {
// BT toggled. Done
returnCode = RESULT_OK;
} else if (mBluetoothAdapter.setScanMode(
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, mTimeout)) {
} else {
mBluetoothAdapter.setDiscoverableTimeout(Duration.ofSeconds(mTimeout));
if (mBluetoothAdapter.setScanMode(
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)
== BluetoothStatusCodes.SUCCESS) {
// If already in discoverable mode, this will extend the timeout.
long endTime = System.currentTimeMillis() + (long) mTimeout * 1000;
LocalBluetoothPreferences.persistDiscoverableEndTimestamp(
@@ -278,6 +284,7 @@ public class RequestPermissionActivity extends Activity implements
} else {
returnCode = RESULT_CANCELED;
}
}
if (mDialog != null) {
mDialog.dismiss();