Merge "Refactor Bluetooth scan mode APIs"
This commit is contained in:
committed by
Android (Google) Code Review
commit
6a709815df
@@ -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);
|
||||||
|
@@ -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,22 +264,26 @@ 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(
|
|
||||||
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, mTimeout)) {
|
|
||||||
// If already in discoverable mode, this will extend the timeout.
|
|
||||||
long endTime = System.currentTimeMillis() + (long) mTimeout * 1000;
|
|
||||||
LocalBluetoothPreferences.persistDiscoverableEndTimestamp(
|
|
||||||
this, endTime);
|
|
||||||
if (0 < mTimeout) {
|
|
||||||
BluetoothDiscoverableTimeoutReceiver.setDiscoverableAlarm(this, endTime);
|
|
||||||
}
|
|
||||||
returnCode = mTimeout;
|
|
||||||
// Activity.RESULT_FIRST_USER should be 1
|
|
||||||
if (returnCode < RESULT_FIRST_USER) {
|
|
||||||
returnCode = RESULT_FIRST_USER;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
returnCode = RESULT_CANCELED;
|
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(
|
||||||
|
this, endTime);
|
||||||
|
if (0 < mTimeout) {
|
||||||
|
BluetoothDiscoverableTimeoutReceiver.setDiscoverableAlarm(this, endTime);
|
||||||
|
}
|
||||||
|
returnCode = mTimeout;
|
||||||
|
// Activity.RESULT_FIRST_USER should be 1
|
||||||
|
if (returnCode < RESULT_FIRST_USER) {
|
||||||
|
returnCode = RESULT_FIRST_USER;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
returnCode = RESULT_CANCELED;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mDialog != null) {
|
if (mDialog != null) {
|
||||||
|
Reference in New Issue
Block a user