Don't stutter animations during Bluetooth broadcasts.

Bug: 3163109
Change-Id: I89ba3da4ee1f0999f3c7113f2f5fdce6fdafebd7
This commit is contained in:
Brad Fitzpatrick
2010-11-03 15:53:47 -07:00
parent 38f8a1d0fa
commit 66dc706570

View File

@@ -32,8 +32,13 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Handler;
import android.util.Log; import android.util.Log;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/** /**
* BluetoothEventRedirector receives broadcasts and callbacks from the Bluetooth * BluetoothEventRedirector receives broadcasts and callbacks from the Bluetooth
* API and dispatches the event on the UI thread to the right class in the * API and dispatches the event on the UI thread to the right class in the
@@ -42,9 +47,14 @@ import android.util.Log;
public class BluetoothEventRedirector { public class BluetoothEventRedirector {
private static final String TAG = "BluetoothEventRedirector"; private static final String TAG = "BluetoothEventRedirector";
private LocalBluetoothManager mManager; private final LocalBluetoothManager mManager;
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { private final ThreadPoolExecutor mSerialExecutor = new ThreadPoolExecutor(
0, 1, 1000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
private final Handler mHandler = new Handler();
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
Log.v(TAG, "Received " + intent.getAction()); Log.v(TAG, "Received " + intent.getAction());
@@ -57,13 +67,11 @@ public class BluetoothEventRedirector {
BluetoothAdapter.ERROR); BluetoothAdapter.ERROR);
mManager.setBluetoothStateInt(state); mManager.setBluetoothStateInt(state);
} else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) { } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {
persistDiscoveringTimestamp(); PendingResult pr = goAsync(); // so loading shared prefs doesn't kill animation
mManager.onScanningStateChanged(true); persistDiscoveringTimestamp(pr, true);
} else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) { } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
persistDiscoveringTimestamp(); PendingResult pr = goAsync(); // so loading shared prefs doesn't kill animation
mManager.onScanningStateChanged(false); persistDiscoveringTimestamp(pr, false);
} else if (action.equals(BluetoothDevice.ACTION_FOUND)) { } else if (action.equals(BluetoothDevice.ACTION_FOUND)) {
short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE); short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
BluetoothClass btClass = intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS); BluetoothClass btClass = intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS);
@@ -222,10 +230,26 @@ public class BluetoothEventRedirector {
return null; return null;
} }
private void persistDiscoveringTimestamp() { private void persistDiscoveringTimestamp(
SharedPreferences.Editor editor = mManager.getSharedPreferences().edit(); final BroadcastReceiver.PendingResult pr, final boolean newState) {
editor.putLong(LocalBluetoothManager.SHARED_PREFERENCES_KEY_DISCOVERING_TIMESTAMP, // Load the shared preferences and edit it on a background
System.currentTimeMillis()); // thread (but serialized!), but then post back to the main
editor.apply(); // thread to run the onScanningStateChanged callbacks which
// update the UI...
mSerialExecutor.submit(new Runnable() {
public void run() {
SharedPreferences.Editor editor = mManager.getSharedPreferences().edit();
editor.putLong(
LocalBluetoothManager.SHARED_PREFERENCES_KEY_DISCOVERING_TIMESTAMP,
System.currentTimeMillis());
editor.apply();
mHandler.post(new Runnable() {
public void run() {
mManager.onScanningStateChanged(newState);
pr.finish();
}
});
}
});
} }
} }