diff options
6 files changed, 75 insertions, 10 deletions
diff --git a/src/com/android/settings/bluetooth/BluetoothEventRedirector.java b/src/com/android/settings/bluetooth/BluetoothEventRedirector.java index c1a211602..dbdf6c156 100644 --- a/src/com/android/settings/bluetooth/BluetoothEventRedirector.java +++ b/src/com/android/settings/bluetooth/BluetoothEventRedirector.java @@ -16,6 +16,9 @@ package com.android.settings.bluetooth; +import com.android.settings.R; +import com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile; + import android.bluetooth.BluetoothA2dp; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothClass; @@ -25,11 +28,9 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.util.Log; -import com.android.settings.R; -import com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile; - /** * BluetoothEventRedirector receives broadcasts and callbacks from the Bluetooth * API and dispatches the event on the UI thread to the right class in the @@ -53,9 +54,11 @@ public class BluetoothEventRedirector { BluetoothAdapter.ERROR); mManager.setBluetoothStateInt(state); } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) { + persistDiscoveringTimestamp(); mManager.onScanningStateChanged(true); } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) { + persistDiscoveringTimestamp(); mManager.onScanningStateChanged(false); } else if (action.equals(BluetoothDevice.ACTION_FOUND)) { @@ -191,4 +194,11 @@ public class BluetoothEventRedirector { } return null; } + + private void persistDiscoveringTimestamp() { + SharedPreferences.Editor editor = mManager.getSharedPreferences().edit(); + editor.putLong(LocalBluetoothManager.SHARED_PREFERENCES_KEY_DISCOVERING_TIMESTAMP, + System.currentTimeMillis()); + editor.commit(); + } } diff --git a/src/com/android/settings/bluetooth/LocalBluetoothManager.java b/src/com/android/settings/bluetooth/LocalBluetoothManager.java index 43d4343dd..2ffb1393d 100644 --- a/src/com/android/settings/bluetooth/LocalBluetoothManager.java +++ b/src/com/android/settings/bluetooth/LocalBluetoothManager.java @@ -72,6 +72,9 @@ public class LocalBluetoothManager { // of raising notifications private static long GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND = 60 * 1000; + public static final String SHARED_PREFERENCES_KEY_DISCOVERING_TIMESTAMP = + "last_discovering_time"; + private static final String SHARED_PREFERENCES_KEY_LAST_SELECTED_DEVICE = "last_selected_device"; @@ -314,7 +317,7 @@ public class LocalBluetoothManager { long currentTimeMillis = System.currentTimeMillis(); SharedPreferences sharedPreferences = getSharedPreferences(); - // If the device was in discoverable mode recently + // 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) @@ -322,6 +325,14 @@ public class LocalBluetoothManager { return true; } + // If the device was discoverING recently + if (mAdapter != null && mAdapter.isDiscovering()) { + return true; + } else if ((sharedPreferences.getLong(SHARED_PREFERENCES_KEY_DISCOVERING_TIMESTAMP, 0) + + 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( diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml index 8a0ce2182..6b9e0501d 100644 --- a/tests/AndroidManifest.xml +++ b/tests/AndroidManifest.xml @@ -18,6 +18,7 @@ package="com.android.settings.tests"> <uses-permission android:name="android.permission.BLUETOOTH" /> + <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <application> <uses-library android:name="android.test.runner" /> diff --git a/tests/res/layout/bluetooth_request_permission_test.xml b/tests/res/layout/bluetooth_request_permission_test.xml index 4d54544a6..0a5aec0dc 100644 --- a/tests/res/layout/bluetooth_request_permission_test.xml +++ b/tests/res/layout/bluetooth_request_permission_test.xml @@ -37,10 +37,16 @@ android:layout_weight="1" android:text="@string/enable" /> - <Button android:id="@+id/discover" + <Button android:id="@+id/discoverable" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/discoverable" /> + + <Button android:id="@+id/scan" + android:layout_width="0dip" + android:layout_height="wrap_content" + android:layout_weight="1" + android:text="@string/start_scan" /> </LinearLayout> </LinearLayout> diff --git a/tests/res/values/strings.xml b/tests/res/values/strings.xml index b06782fee..f8a048134 100644 --- a/tests/res/values/strings.xml +++ b/tests/res/values/strings.xml @@ -20,4 +20,6 @@ <!-- Test only. Do not translate. --> <string name="enable">Enable</string> <string name="discoverable">Discoverable</string> + <string name="start_scan">Start Scan</string> + <string name="stop_scan">Stop Scan</string> </resources> diff --git a/tests/src/com/android/settings/tests/BluetoothRequestPermissionTest.java b/tests/src/com/android/settings/tests/BluetoothRequestPermissionTest.java index 105c98e8a..8064e3a49 100644 --- a/tests/src/com/android/settings/tests/BluetoothRequestPermissionTest.java +++ b/tests/src/com/android/settings/tests/BluetoothRequestPermissionTest.java @@ -33,7 +33,7 @@ import android.widget.ListView; public class BluetoothRequestPermissionTest extends Activity { private static final String TAG = "BluetoothRequestPermissionTest"; - + BluetoothAdapter mAdapter; private ArrayAdapter<String> mMsgAdapter; private class BtOnClickListener implements OnClickListener { @@ -48,23 +48,50 @@ public class BluetoothRequestPermissionTest extends Activity { } } + private class BtScanOnClickListener implements OnClickListener { + public void onClick(View v) { + Button scanButton = (Button) v; + if (mAdapter.isDiscovering()) { + mAdapter.cancelDiscovery(); + scanButton.setText(R.string.start_scan); + } else { + mAdapter.startDiscovery(); + scanButton.setText(R.string.stop_scan); + } + } + } + @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.bluetooth_request_permission_test); + mAdapter = BluetoothAdapter.getDefaultAdapter(); Button enable = (Button) findViewById(R.id.enable); enable.setOnClickListener(new BtOnClickListener(true /* enable */)); - Button discover = (Button) findViewById(R.id.discover); - discover.setOnClickListener(new BtOnClickListener(false /* enable & discoverable */)); + Button discoverable = (Button) findViewById(R.id.discoverable); + discoverable.setOnClickListener(new BtOnClickListener(false /* enable & discoverable */)); + + Button scanButton = (Button) findViewById(R.id.scan); + scanButton.setOnClickListener(new BtScanOnClickListener()); + if (mAdapter.isDiscovering()) { + scanButton.setText(R.string.stop_scan); + } else { + scanButton.setText(R.string.start_scan); + } mMsgAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); ListView listView = (ListView) findViewById(R.id.msg_container); listView.setAdapter(mMsgAdapter); - registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)); + IntentFilter filter = new IntentFilter(); + filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); + filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); + filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); + filter.addAction(BluetoothDevice.ACTION_FOUND); + registerReceiver(mReceiver, filter); addMsg("Initialized"); } @@ -113,7 +140,8 @@ public class BluetoothRequestPermissionTest extends Activity { public void onReceive(Context context, Intent intent) { if (intent == null) return; - if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) { + String action = intent.getAction(); + if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { String stateStr = "???"; switch (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothDevice.ERROR)) { case BluetoothAdapter.STATE_OFF: @@ -130,6 +158,13 @@ public class BluetoothRequestPermissionTest extends Activity { break; } addMsg("Bluetooth status = " + stateStr); + } else if (action.equals(BluetoothDevice.ACTION_FOUND)) { + String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME); + addMsg("Found: " + name); + } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) { + addMsg("Scan started..."); + } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) { + addMsg("Scan ended"); } } }; |