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.settings.R;
import com.android.settingslib.bluetooth.BluetoothDiscoverableTimeoutReceiver; import com.android.settingslib.bluetooth.BluetoothDiscoverableTimeoutReceiver;
import java.time.Duration;
/** /**
* BluetoothDiscoverableEnabler is a helper to manage the "Discoverable" * BluetoothDiscoverableEnabler is a helper to manage the "Discoverable"
* checkbox. It sets/unsets discoverability and keeps track of how much time * 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(); int timeout = getDiscoverableTimeout();
long endTimestamp = System.currentTimeMillis() + timeout * 1000L; long endTimestamp = System.currentTimeMillis() + timeout * 1000L;
LocalBluetoothPreferences.persistDiscoverableEndTimestamp(mContext, endTimestamp); LocalBluetoothPreferences.persistDiscoverableEndTimestamp(mContext, endTimestamp);
mBluetoothAdapter.setDiscoverableTimeout(Duration.ofSeconds(timeout));
mBluetoothAdapter mBluetoothAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, timeout);
updateCountdownSummary(); updateCountdownSummary();
Log.d(TAG, "setEnabled(): enabled = " + enable + "timeout = " + timeout); Log.d(TAG, "setEnabled(): enabled = " + enable + "timeout = " + timeout);

View File

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