b/2226832 Showing Pairing Dialog in the foreground

Pairing Dialogs notifications (in the window shade) were not visible in full
screen apps e.g. gallery.  Showing Pairing Dialog in the foreground:
1) if the remote device was picked in the device picker in the last minute or
2) if the device was in discoverable mode in the last minute.
diff --git a/src/com/android/settings/bluetooth/BluetoothPairingRequest.java b/src/com/android/settings/bluetooth/BluetoothPairingRequest.java
index 2fa1744..4253c5d 100644
--- a/src/com/android/settings/bluetooth/BluetoothPairingRequest.java
+++ b/src/com/android/settings/bluetooth/BluetoothPairingRequest.java
@@ -21,7 +21,6 @@
 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
-import android.bluetooth.BluetoothClass;
 import android.bluetooth.BluetoothDevice;
 import android.content.BroadcastReceiver;
 import android.content.Context;
@@ -62,7 +61,8 @@
             pairingIntent.setAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
             pairingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 
-            if (localManager.getForegroundActivity() != null) {
+            String deviceAddress = device != null ? device.getAddress() : null;
+            if (localManager.shouldShowDialogInForeground(deviceAddress)) {
                 // Since the BT-related activity is in the foreground, just open the dialog
                 context.startActivity(pairingIntent);
 
diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java
index 48f2a05..8a0795b 100644
--- a/src/com/android/settings/bluetooth/BluetoothSettings.java
+++ b/src/com/android/settings/bluetooth/BluetoothSettings.java
@@ -16,17 +16,20 @@
 
 package com.android.settings.bluetooth;
 
+import com.android.settings.ProgressCategory;
+import com.android.settings.R;
+
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothClass;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothDevicePicker;
 import android.bluetooth.BluetoothUuid;
-import android.os.ParcelUuid;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.os.Bundle;
+import android.os.ParcelUuid;
 import android.preference.CheckBoxPreference;
 import android.preference.Preference;
 import android.preference.PreferenceActivity;
@@ -38,10 +41,6 @@
 import android.view.ContextMenu.ContextMenuInfo;
 import android.widget.AdapterView.AdapterContextMenuInfo;
 
-import com.android.settings.ProgressCategory;
-import com.android.settings.R;
-import com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile;
-
 import java.util.List;
 import java.util.WeakHashMap;
 
@@ -255,6 +254,7 @@
                 CachedBluetoothDevice device = btPreference.getCachedDevice();
 
                 mSelectedDevice = device.getDevice();
+                mLocalManager.persistSelectedDeviceInPicker(mSelectedDevice.getAddress());
                 if ((device.getBondState() == BluetoothDevice.BOND_BONDED) ||
                         (mNeedAuth == false)) {
                     sendDevicePickedIntent(mSelectedDevice);
diff --git a/src/com/android/settings/bluetooth/LocalBluetoothManager.java b/src/com/android/settings/bluetooth/LocalBluetoothManager.java
index f131579..acab88c 100644
--- a/src/com/android/settings/bluetooth/LocalBluetoothManager.java
+++ b/src/com/android/settings/bluetooth/LocalBluetoothManager.java
@@ -18,22 +18,21 @@
 
 import com.android.settings.R;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Set;
-
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.bluetooth.BluetoothA2dp;
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
 import android.content.Context;
-import android.content.Intent;
 import android.content.SharedPreferences;
 import android.util.Config;
 import android.util.Log;
 import android.widget.Toast;
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
 // TODO: have some notion of shutting down.  Maybe a minute after they leave BT settings?
 /**
  * LocalBluetoothManager provides a simplified interface on top of a subset of
@@ -67,6 +66,18 @@
     private List<Callback> mCallbacks = new ArrayList<Callback>();
 
     private static final int SCAN_EXPIRATION_MS = 5 * 60 * 1000; // 5 mins
+
+    // If a device was picked from the device picker or was in discoverable mode
+    // in the last 60 seconds, show the pairing dialogs in foreground instead
+    // of raising notifications
+    private static long GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND = 60 * 1000;
+
+    private static final String SHARED_PREFERENCES_KEY_LAST_SELECTED_DEVICE =
+        "last_selected_device";
+
+    private static final String SHARED_PREFERENCES_KEY_LAST_SELECTED_DEVICE_TIME =
+        "last_selected_device_time";
+
     private long mLastScan;
 
     public static LocalBluetoothManager getInstance(Context context) {
@@ -288,4 +299,44 @@
         void onDeviceDeleted(CachedBluetoothDevice cachedDevice);
     }
 
+    public boolean shouldShowDialogInForeground(String deviceAddress) {
+        // If Bluetooth Settings is visible
+        if (mForegroundActivity != null) return true;
+
+        long currentTimeMillis = System.currentTimeMillis();
+        SharedPreferences sharedPreferences = getSharedPreferences();
+
+        // If the device was in discoverable mode recently
+        long lastDiscoverableEndTime = sharedPreferences.getLong(
+                BluetoothDiscoverableEnabler.SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP, 0);
+        if ((lastDiscoverableEndTime + GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND)
+                > currentTimeMillis) {
+            return true;
+        }
+
+        // If the device was picked in the device picker recently
+        if (deviceAddress != null) {
+            String lastSelectedDevice = sharedPreferences.getString(
+                    SHARED_PREFERENCES_KEY_LAST_SELECTED_DEVICE, null);
+
+            if (deviceAddress.equals(lastSelectedDevice)) {
+                long lastDeviceSelectedTime = sharedPreferences.getLong(
+                        SHARED_PREFERENCES_KEY_LAST_SELECTED_DEVICE_TIME, 0);
+                if ((lastDeviceSelectedTime + GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND)
+                        > currentTimeMillis) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    void persistSelectedDeviceInPicker(String deviceAddress) {
+        SharedPreferences.Editor editor = getSharedPreferences().edit();
+        editor.putString(LocalBluetoothManager.SHARED_PREFERENCES_KEY_LAST_SELECTED_DEVICE,
+                deviceAddress);
+        editor.putLong(LocalBluetoothManager.SHARED_PREFERENCES_KEY_LAST_SELECTED_DEVICE_TIME,
+                System.currentTimeMillis());
+        editor.commit();
+    }
 }