From 683528db8b4cfcadcf717ee6b8ee463df7fe58c0 Mon Sep 17 00:00:00 2001 From: Marie Janssen Date: Tue, 24 Jan 2017 14:21:29 -0800 Subject: [PATCH] Bluetooth: remove BluetoothDiscoveryReceiver Use new function getDiscoveryEndMillis() to replace use of shared preferences and intents. Test: scan recently, then connect from external BT device Bug: 34395439 Change-Id: Ia498b3bb4868b656cbbb9fbdc607a49af948343b --- AndroidManifest.xml | 9 ---- .../bluetooth/BluetoothDiscoveryReceiver.java | 49 ------------------- .../settings/bluetooth/DockService.java | 8 +-- .../bluetooth/LocalBluetoothPreferences.java | 27 +++------- 4 files changed, 8 insertions(+), 85 deletions(-) delete mode 100644 src/com/android/settings/bluetooth/BluetoothDiscoveryReceiver.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 3f8a9d14062..93c497e9c23 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -2044,15 +2044,6 @@ - - - - - - - - diff --git a/src/com/android/settings/bluetooth/BluetoothDiscoveryReceiver.java b/src/com/android/settings/bluetooth/BluetoothDiscoveryReceiver.java deleted file mode 100644 index 1ba9f85ce42..00000000000 --- a/src/com/android/settings/bluetooth/BluetoothDiscoveryReceiver.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.bluetooth; - -import android.bluetooth.BluetoothAdapter; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.util.Log; - -import com.android.settingslib.bluetooth.BluetoothEventManager; - -/** - * BluetoothDiscoveryReceiver updates a timestamp when the - * Bluetooth adapter starts or finishes discovery mode. This - * is used to decide whether to open an alert dialog or - * create a notification when we receive a pairing request. - * - *

Note that the discovery start/finish intents are also handled - * by {@link BluetoothEventManager} to update the UI, if visible. - */ -public final class BluetoothDiscoveryReceiver extends BroadcastReceiver { - private static final String TAG = "BluetoothDiscoveryReceiver"; - - @Override - public void onReceive(Context context, Intent intent) { - String action = intent.getAction(); - Log.v(TAG, "Received: " + action); - - if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED) || - action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) { - LocalBluetoothPreferences.persistDiscoveringTimestamp(context); - } - } -} diff --git a/src/com/android/settings/bluetooth/DockService.java b/src/com/android/settings/bluetooth/DockService.java index 020ca0c8a6b..52a015ebe38 100644 --- a/src/com/android/settings/bluetooth/DockService.java +++ b/src/com/android/settings/bluetooth/DockService.java @@ -938,13 +938,7 @@ public final class DockService extends Service implements ServiceListener { public void onDeviceAdded(CachedBluetoothDevice cachedDevice) { } public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) { } public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) { } - - @Override - public void onScanningStateChanged(boolean started) { - // TODO: Find a more unified place for a persistent BluetoothCallback to live - // as this is not exactly dock related. - LocalBluetoothPreferences.persistDiscoveringTimestamp(mContext); - } + public void onScanningStateChanged(boolean started) { } @Override public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) { diff --git a/src/com/android/settings/bluetooth/LocalBluetoothPreferences.java b/src/com/android/settings/bluetooth/LocalBluetoothPreferences.java index 401b13c5ad9..6a0bdcf4b21 100644 --- a/src/com/android/settings/bluetooth/LocalBluetoothPreferences.java +++ b/src/com/android/settings/bluetooth/LocalBluetoothPreferences.java @@ -40,8 +40,6 @@ final class LocalBluetoothPreferences { // of raising notifications private static final int GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND = 60 * 1000; - private static final String KEY_DISCOVERING_TIMESTAMP = "last_discovering_time"; - private static final String KEY_LAST_SELECTED_DEVICE = "last_selected_device"; private static final String KEY_LAST_SELECTED_DEVICE_TIME = "last_selected_device_time"; @@ -95,11 +93,14 @@ final class LocalBluetoothPreferences { // If the device was discoverING recently LocalBluetoothAdapter adapter = manager.getBluetoothAdapter(); - if (adapter != null && adapter.isDiscovering()) { - return true; - } else if ((sharedPreferences.getLong(KEY_DISCOVERING_TIMESTAMP, 0) + + if (adapter != null) { + if (adapter.isDiscovering()) { + return true; + } + if ((adapter.getDiscoveryEndMillis() + GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND) > currentTimeMillis) { - return true; + return true; + } } // If the device was picked in the device picker recently @@ -147,20 +148,6 @@ final class LocalBluetoothPreferences { editor.apply(); } - static void persistDiscoveringTimestamp(final Context context) { - // Load the shared preferences and edit it on a background - // thread (but serialized!). - QueuedWork.singleThreadExecutor().submit(new Runnable() { - public void run() { - SharedPreferences.Editor editor = getSharedPreferences(context).edit(); - editor.putLong( - KEY_DISCOVERING_TIMESTAMP, - System.currentTimeMillis()); - editor.apply(); - } - }); - } - static boolean hasDockAutoConnectSetting(Context context, String addr) { return getSharedPreferences(context).contains(KEY_DOCK_AUTO_CONNECT + addr); }