diff options
| author | 2017-08-16 17:35:30 +0000 | |
|---|---|---|
| committer | 2017-08-16 17:35:30 +0000 | |
| commit | 1bf3ea176fa3eb0a37dc20520e3aa0ca00183a7a (patch) | |
| tree | 782b553a09a5b4b708ea764e1784ecf1137013b9 | |
| parent | 3a3cf6e363b6f7bd1393cad787ecad3c968c26cd (diff) | |
| parent | 0480580696778c1174866c4ef793060a17313805 (diff) | |
Merge "Protect against weak pointer crash." into oc-mr1-dev
am: 0480580696
Change-Id: I2efed3ec7096c23bc486d44b5303e24716b31fb7
2 files changed, 29 insertions, 4 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothControllerImpl.java index fc7495213165..5b24f9cd97bf 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothControllerImpl.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothControllerImpl.java @@ -296,10 +296,13 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa @Override public void run() { - mBondState = mDevice.get().getBondState(); - mMaxConnectionState = mDevice.get().getMaxConnectionState(); - mUiHandler.removeMessages(H.MSG_PAIRED_DEVICES_CHANGED); - mUiHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED); + CachedBluetoothDevice device = mDevice.get(); + if (device != null) { + mBondState = device.getBondState(); + mMaxConnectionState = device.getMaxConnectionState(); + mUiHandler.removeMessages(H.MSG_PAIRED_DEVICES_CHANGED); + mUiHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED); + } } } diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BluetoothControllerImplTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BluetoothControllerImplTest.java index 4cc8bcae541a..4e7550b20659 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BluetoothControllerImplTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BluetoothControllerImplTest.java @@ -137,4 +137,26 @@ public class BluetoothControllerImplTest extends SysuiTestCase { verify(callback).onBluetoothDevicesChanged(); mainLooper.destroy(); } + + @Test + public void testNullAsync_DoesNotCrash() throws Exception { + CachedBluetoothDevice device = mock(CachedBluetoothDevice.class); + when(device.getMaxConnectionState()).thenReturn(BluetoothProfile.STATE_CONNECTED); + BluetoothController.Callback callback = mock(BluetoothController.Callback.class); + mBluetoothControllerImpl.addCallback(callback); + + // Grab the main looper, we'll need it later. + TestableLooper mainLooper = new TestableLooper(Looper.getMainLooper()); + + try { + // Trigger the state getting. + assertEquals(BluetoothProfile.STATE_DISCONNECTED, + mBluetoothControllerImpl.getMaxConnectionState(null)); + + mTestableLooper.processMessages(1); + mainLooper.processAllMessages(); + } finally { + mainLooper.destroy(); + } + } } |