Add Bluetooth metadata listenr

If it is updated, we need to refresh UI to display latest information

Fixes: 124455912
Test: RunSettingsRoboTests
Change-Id: I73b03f4931e3c2b0d367bbd2d3b2057b26c84b59
This commit is contained in:
jackqdyulei
2019-02-25 13:58:13 -08:00
parent 5e5dd25b8f
commit 344d761e60
2 changed files with 41 additions and 0 deletions

View File

@@ -16,6 +16,7 @@
package com.android.settings.bluetooth; package com.android.settings.bluetooth;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
@@ -23,6 +24,8 @@ import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter; import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
@@ -59,9 +62,25 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
@VisibleForTesting @VisibleForTesting
final Map<String, Bitmap> mIconCache; final Map<String, Bitmap> mIconCache;
private CachedBluetoothDevice mCachedDevice; private CachedBluetoothDevice mCachedDevice;
@VisibleForTesting
BluetoothAdapter mBluetoothAdapter;
@VisibleForTesting
Handler mHandler = new Handler(Looper.getMainLooper());
@VisibleForTesting
final BluetoothAdapter.MetadataListener mMetadataListener =
new BluetoothAdapter.MetadataListener() {
@Override
public void onMetadataChanged(BluetoothDevice device, int key, String value) {
super.onMetadataChanged(device, key, value);
Log.i(TAG, String.format("Metadata updated in Device %s: %d = %s.", device, key,
value));
refresh();
}
};
public AdvancedBluetoothDetailsHeaderController(Context context, String prefKey) { public AdvancedBluetoothDetailsHeaderController(Context context, String prefKey) {
super(context, prefKey); super(context, prefKey);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mIconCache = new HashMap<>(); mIconCache = new HashMap<>();
} }
@@ -84,11 +103,14 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
@Override @Override
public void onStart() { public void onStart() {
mCachedDevice.registerCallback(this::onDeviceAttributesChanged); mCachedDevice.registerCallback(this::onDeviceAttributesChanged);
mBluetoothAdapter.registerMetadataListener(mCachedDevice.getDevice(), mMetadataListener,
mHandler);
} }
@Override @Override
public void onStop() { public void onStop() {
mCachedDevice.unregisterCallback(this::onDeviceAttributesChanged); mCachedDevice.unregisterCallback(this::onDeviceAttributesChanged);
mBluetoothAdapter.unregisterMetadataListener(mCachedDevice.getDevice());
// Destroy icon bitmap associated with this header // Destroy icon bitmap associated with this header
for (Bitmap bitmap : mIconCache.values()) { for (Bitmap bitmap : mIconCache.values()) {

View File

@@ -21,6 +21,7 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
@@ -65,6 +66,8 @@ public class AdvancedBluetoothDetailsHeaderControllerTest{
private ImageView mImageView; private ImageView mImageView;
@Mock @Mock
private CachedBluetoothDevice mCachedDevice; private CachedBluetoothDevice mCachedDevice;
@Mock
private BluetoothAdapter mBluetoothAdapter;
private AdvancedBluetoothDetailsHeaderController mController; private AdvancedBluetoothDetailsHeaderController mController;
private LayoutPreference mLayoutPreference; private LayoutPreference mLayoutPreference;
@@ -78,6 +81,7 @@ public class AdvancedBluetoothDetailsHeaderControllerTest{
mLayoutPreference = new LayoutPreference(mContext, mLayoutPreference = new LayoutPreference(mContext,
LayoutInflater.from(mContext).inflate(R.layout.advanced_bt_entity_header, null)); LayoutInflater.from(mContext).inflate(R.layout.advanced_bt_entity_header, null));
mController.mLayoutPreference = mLayoutPreference; mController.mLayoutPreference = mLayoutPreference;
mController.mBluetoothAdapter = mBluetoothAdapter;
when(mCachedDevice.getDevice()).thenReturn(mBluetoothDevice); when(mCachedDevice.getDevice()).thenReturn(mBluetoothDevice);
} }
@@ -159,6 +163,21 @@ public class AdvancedBluetoothDetailsHeaderControllerTest{
verify(mImageView).setImageBitmap(mBitmap); verify(mImageView).setImageBitmap(mBitmap);
} }
@Test
public void onStart_registerCallback() {
mController.onStart();
verify(mBluetoothAdapter).registerMetadataListener(mBluetoothDevice,
mController.mMetadataListener, mController.mHandler);
}
@Test
public void onStop_unregisterCallback() {
mController.onStop();
verify(mBluetoothAdapter).unregisterMetadataListener(mBluetoothDevice);
}
private void assertBatteryLevel(LinearLayout linearLayout, int batteryLevel) { private void assertBatteryLevel(LinearLayout linearLayout, int batteryLevel) {
final TextView textView = linearLayout.findViewById(R.id.bt_battery_summary); final TextView textView = linearLayout.findViewById(R.id.bt_battery_summary);
assertThat(textView.getText().toString()).isEqualTo( assertThat(textView.getText().toString()).isEqualTo(