From 5469ff8b3f9966c6f141078b37da5f178b7825e4 Mon Sep 17 00:00:00 2001 From: Michael Chan Date: Sun, 8 Nov 2009 22:53:32 -0800 Subject: [PATCH] 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. --- .../bluetooth/BluetoothPairingRequest.java | 4 +- .../settings/bluetooth/BluetoothSettings.java | 10 ++-- .../settings/bluetooth/LocalBluetoothManager.java | 61 ++++++++++++++++++++-- 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/src/com/android/settings/bluetooth/BluetoothPairingRequest.java b/src/com/android/settings/bluetooth/BluetoothPairingRequest.java index 2fa17448ea..4253c5d24e 100644 --- a/src/com/android/settings/bluetooth/BluetoothPairingRequest.java +++ b/src/com/android/settings/bluetooth/BluetoothPairingRequest.java @@ -21,7 +21,6 @@ import com.android.settings.R; 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 @@ public class BluetoothPairingRequest extends BroadcastReceiver { 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 48f2a059d0..8a0795be8a 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.View; 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 @@ public class BluetoothSettings extends PreferenceActivity 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 f131579438..acab88ca52 100644 --- a/src/com/android/settings/bluetooth/LocalBluetoothManager.java +++ b/src/com/android/settings/bluetooth/LocalBluetoothManager.java @@ -18,22 +18,21 @@ package com.android.settings.bluetooth; 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 @@ public class LocalBluetoothManager { private List mCallbacks = new ArrayList(); 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 @@ public class LocalBluetoothManager { 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(); + } } -- 2.11.0