Merge "Refactor Bluetooth scan mode APIs"
diff --git a/src/com/android/settings/bluetooth/BluetoothDiscoverableEnabler.java b/src/com/android/settings/bluetooth/BluetoothDiscoverableEnabler.java
index 0a9003d..55c76e3 100755
--- a/src/com/android/settings/bluetooth/BluetoothDiscoverableEnabler.java
+++ b/src/com/android/settings/bluetooth/BluetoothDiscoverableEnabler.java
@@ -31,6 +31,8 @@
 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 @@
             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);
diff --git a/src/com/android/settings/bluetooth/RequestPermissionActivity.java b/src/com/android/settings/bluetooth/RequestPermissionActivity.java
index 362849d..588bc73 100644
--- a/src/com/android/settings/bluetooth/RequestPermissionActivity.java
+++ b/src/com/android/settings/bluetooth/RequestPermissionActivity.java
@@ -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 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,22 +264,26 @@
         if (mRequest == REQUEST_ENABLE || mRequest == REQUEST_DISABLE) {
             // BT toggled. Done
             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 {
-            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) {