Align with Bluetooth metadata API changes

* Align with the changes of Bluetooth metadata APIs.
* Move metadata utils from Settings to SettingsLib.

Bug: 124448651
Test: make RunSettingsRoboTests
Change-Id: Ic9ad91536ef3ff6807a08bbffa3dd796ef1ad523
This commit is contained in:
Ugo Yu
2019-03-05 16:43:08 +08:00
parent dc4f7ce282
commit 9f11ccf319
8 changed files with 64 additions and 131 deletions

View File

@@ -39,6 +39,7 @@ import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.fuelgauge.BatteryMeterView;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnDestroy;
@@ -69,13 +70,12 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
@VisibleForTesting
Handler mHandler = new Handler(Looper.getMainLooper());
@VisibleForTesting
final BluetoothAdapter.MetadataListener mMetadataListener =
new BluetoothAdapter.MetadataListener() {
final BluetoothAdapter.OnMetadataChangedListener mMetadataListener =
new BluetoothAdapter.OnMetadataChangedListener() {
@Override
public void onMetadataChanged(BluetoothDevice device, int key, String value) {
super.onMetadataChanged(device, key, value);
public void onMetadataChanged(BluetoothDevice device, int key, byte[] value) {
Log.i(TAG, String.format("Metadata updated in Device %s: %d = %s.", device, key,
value));
value == null ? null : new String(value)));
refresh();
}
};
@@ -88,9 +88,9 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
@Override
public int getAvailabilityStatus() {
final boolean unthetheredHeadset = Utils.getBooleanMetaData(mCachedDevice.getDevice(),
BluetoothDevice.METADATA_IS_UNTHETHERED_HEADSET);
return unthetheredHeadset ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
final boolean untetheredHeadset = BluetoothUtils.getBooleanMetaData(
mCachedDevice.getDevice(), BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET);
return untetheredHeadset ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
}
@Override
@@ -108,8 +108,8 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
return;
}
mCachedDevice.registerCallback(this::onDeviceAttributesChanged);
mBluetoothAdapter.registerMetadataListener(mCachedDevice.getDevice(), mMetadataListener,
mHandler);
mBluetoothAdapter.addOnMetadataChangedListener(mCachedDevice.getDevice(),
mContext.getMainExecutor(), mMetadataListener);
}
@Override
@@ -118,7 +118,8 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
return;
}
mCachedDevice.unregisterCallback(this::onDeviceAttributesChanged);
mBluetoothAdapter.unregisterMetadataListener(mCachedDevice.getDevice());
mBluetoothAdapter.removeOnMetadataChangedListener(mCachedDevice.getDevice(),
mMetadataListener);
}
@Override
@@ -154,21 +155,21 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
}
updateSubLayout(mLayoutPreference.findViewById(R.id.layout_left),
BluetoothDevice.METADATA_UNTHETHERED_LEFT_ICON,
BluetoothDevice.METADATA_UNTHETHERED_LEFT_BATTERY,
BluetoothDevice.METADATA_UNTHETHERED_LEFT_CHARGING,
BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON,
BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY,
BluetoothDevice.METADATA_UNTETHERED_LEFT_CHARGING,
R.string.bluetooth_left_name);
updateSubLayout(mLayoutPreference.findViewById(R.id.layout_middle),
BluetoothDevice.METADATA_UNTHETHERED_CASE_ICON,
BluetoothDevice.METADATA_UNTHETHERED_CASE_BATTERY,
BluetoothDevice.METADATA_UNTHETHERED_CASE_CHARGING,
BluetoothDevice.METADATA_UNTETHERED_CASE_ICON,
BluetoothDevice.METADATA_UNTETHERED_CASE_BATTERY,
BluetoothDevice.METADATA_UNTETHERED_CASE_CHARGING,
R.string.bluetooth_middle_name);
updateSubLayout(mLayoutPreference.findViewById(R.id.layout_right),
BluetoothDevice.METADATA_UNTHETHERED_RIGHT_ICON,
BluetoothDevice.METADATA_UNTHETHERED_RIGHT_BATTERY,
BluetoothDevice.METADATA_UNTHETHERED_RIGHT_CHARGING,
BluetoothDevice.METADATA_UNTETHERED_RIGHT_ICON,
BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY,
BluetoothDevice.METADATA_UNTETHERED_RIGHT_CHARGING,
R.string.bluetooth_right_name);
}
}
@@ -194,15 +195,15 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
return;
}
final BluetoothDevice bluetoothDevice = mCachedDevice.getDevice();
final String iconUri = Utils.getStringMetaData(bluetoothDevice, iconMetaKey);
final String iconUri = BluetoothUtils.getStringMetaData(bluetoothDevice, iconMetaKey);
if (iconUri != null) {
final ImageView imageView = linearLayout.findViewById(R.id.header_icon);
updateIcon(imageView, iconUri);
}
final int batteryLevel = Utils.getIntMetaData(bluetoothDevice, batteryMetaKey);
final boolean charging = Utils.getBooleanMetaData(bluetoothDevice, chargeMetaKey);
if (batteryLevel != Utils.META_INT_ERROR) {
final int batteryLevel = BluetoothUtils.getIntMetaData(bluetoothDevice, batteryMetaKey);
final boolean charging = BluetoothUtils.getBooleanMetaData(bluetoothDevice, chargeMetaKey);
if (batteryLevel != BluetoothUtils.META_INT_ERROR) {
linearLayout.setVisibility(View.VISIBLE);
final ImageView imageView = linearLayout.findViewById(R.id.bt_battery_icon);
imageView.setImageDrawable(createBtBatteryIcon(mContext, batteryLevel, charging));
@@ -233,7 +234,7 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
// Only show bluetooth icon
final BluetoothDevice bluetoothDevice = mCachedDevice.getDevice();
final String iconUri = Utils.getStringMetaData(bluetoothDevice,
final String iconUri = BluetoothUtils.getStringMetaData(bluetoothDevice,
BluetoothDevice.METADATA_MAIN_ICON);
if (iconUri != null) {
final ImageView imageView = linearLayout.findViewById(R.id.header_icon);