Show advanced Bluetooth information in device detail page

-Apply new metadata on aosp/1596412

Bug: 182338346
Test: make -j50 RunSettingsRoboTests ROBOTEST_FILTER=BluetoothDetailsHeaderControllerTest
make -j50 RunSettingsRoboTests ROBOTEST_FILTER=AdvancedBluetoothDetailsHeaderControllerTest

Change-Id: Ic48ed9213111d0c6ec19b317d4c22e1400261706
This commit is contained in:
Tim Peng
2021-03-11 13:53:56 +08:00
parent 54e4a3097a
commit 90df1c9de1
4 changed files with 165 additions and 48 deletions

View File

@@ -29,9 +29,10 @@ import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.provider.DeviceConfig;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.util.Pair;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
@@ -43,7 +44,6 @@ import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.SettingsUIDeviceConfig;
import com.android.settings.fuelgauge.BatteryMeterView; import com.android.settings.fuelgauge.BatteryMeterView;
import com.android.settingslib.bluetooth.BluetoothUtils; import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
@@ -84,6 +84,7 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
private static final int LEFT_DEVICE_ID = 1; private static final int LEFT_DEVICE_ID = 1;
private static final int RIGHT_DEVICE_ID = 2; private static final int RIGHT_DEVICE_ID = 2;
private static final int CASE_DEVICE_ID = 3; private static final int CASE_DEVICE_ID = 3;
private static final int MAIN_DEVICE_ID = 4;
@VisibleForTesting @VisibleForTesting
LayoutPreference mLayoutPreference; LayoutPreference mLayoutPreference;
@@ -115,13 +116,11 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
@Override @Override
public int getAvailabilityStatus() { public int getAvailabilityStatus() {
final boolean advancedEnabled = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SETTINGS_UI, if (mCachedDevice == null) {
SettingsUIDeviceConfig.BT_ADVANCED_HEADER_ENABLED, true); return CONDITIONALLY_UNAVAILABLE;
final boolean untetheredHeadset = mCachedDevice != null }
&& BluetoothUtils.getBooleanMetaData( return Utils.isAdvancedDetailsHeader(mCachedDevice.getDevice())
mCachedDevice.getDevice(), BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET); ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
Log.d(TAG, "getAvailabilityStatus() is untethered : " + untetheredHeadset);
return advancedEnabled && untetheredHeadset ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
} }
@Override @Override
@@ -182,27 +181,45 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
updateDisconnectLayout(); updateDisconnectLayout();
return; return;
} }
final BluetoothDevice device = mCachedDevice.getDevice();
final String deviceType = BluetoothUtils.getStringMetaData(device,
BluetoothDevice.METADATA_DEVICE_TYPE);
if (TextUtils.equals(deviceType, BluetoothDevice.DEVICE_TYPE_WATCH)
|| TextUtils.equals(deviceType, BluetoothDevice.DEVICE_TYPE_DEFAULT)) {
mLayoutPreference.findViewById(R.id.layout_left).setVisibility(View.GONE);
mLayoutPreference.findViewById(R.id.layout_right).setVisibility(View.GONE);
updateSubLayout(mLayoutPreference.findViewById(R.id.layout_left), updateSubLayout(mLayoutPreference.findViewById(R.id.layout_middle),
BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON, BluetoothDevice.METADATA_MAIN_ICON,
BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY, BluetoothDevice.METADATA_MAIN_BATTERY,
BluetoothDevice.METADATA_UNTETHERED_LEFT_CHARGING, BluetoothDevice.METADATA_MAIN_CHARGING,
R.string.bluetooth_left_name, /* titleResId */ 0,
LEFT_DEVICE_ID); MAIN_DEVICE_ID);
} else if (TextUtils.equals(deviceType,
BluetoothDevice.DEVICE_TYPE_UNTETHERED_HEADSET)
|| BluetoothUtils.getBooleanMetaData(device,
BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)) {
updateSubLayout(mLayoutPreference.findViewById(R.id.layout_left),
BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON,
BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY,
BluetoothDevice.METADATA_UNTETHERED_LEFT_CHARGING,
R.string.bluetooth_left_name,
LEFT_DEVICE_ID);
updateSubLayout(mLayoutPreference.findViewById(R.id.layout_middle), updateSubLayout(mLayoutPreference.findViewById(R.id.layout_middle),
BluetoothDevice.METADATA_UNTETHERED_CASE_ICON, BluetoothDevice.METADATA_UNTETHERED_CASE_ICON,
BluetoothDevice.METADATA_UNTETHERED_CASE_BATTERY, BluetoothDevice.METADATA_UNTETHERED_CASE_BATTERY,
BluetoothDevice.METADATA_UNTETHERED_CASE_CHARGING, BluetoothDevice.METADATA_UNTETHERED_CASE_CHARGING,
R.string.bluetooth_middle_name, R.string.bluetooth_middle_name,
CASE_DEVICE_ID); CASE_DEVICE_ID);
updateSubLayout(mLayoutPreference.findViewById(R.id.layout_right), updateSubLayout(mLayoutPreference.findViewById(R.id.layout_right),
BluetoothDevice.METADATA_UNTETHERED_RIGHT_ICON, BluetoothDevice.METADATA_UNTETHERED_RIGHT_ICON,
BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY, BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY,
BluetoothDevice.METADATA_UNTETHERED_RIGHT_CHARGING, BluetoothDevice.METADATA_UNTETHERED_RIGHT_CHARGING,
R.string.bluetooth_right_name, R.string.bluetooth_right_name,
RIGHT_DEVICE_ID); RIGHT_DEVICE_ID);
}
} }
} }
@@ -226,17 +243,21 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
} }
private void updateSubLayout(LinearLayout linearLayout, int iconMetaKey, int batteryMetaKey, private void updateSubLayout(LinearLayout linearLayout, int iconMetaKey, int batteryMetaKey,
int chargeMetaKey, int titleResId, int batteryId) { int chargeMetaKey, int titleResId, int deviceId) {
if (linearLayout == null) { if (linearLayout == null) {
return; return;
} }
final BluetoothDevice bluetoothDevice = mCachedDevice.getDevice(); final BluetoothDevice bluetoothDevice = mCachedDevice.getDevice();
final String iconUri = BluetoothUtils.getStringMetaData(bluetoothDevice, iconMetaKey); final String iconUri = BluetoothUtils.getStringMetaData(bluetoothDevice, iconMetaKey);
final ImageView imageView = linearLayout.findViewById(R.id.header_icon);
if (iconUri != null) { if (iconUri != null) {
final ImageView imageView = linearLayout.findViewById(R.id.header_icon);
updateIcon(imageView, iconUri); updateIcon(imageView, iconUri);
} else {
final Pair<Drawable, String> pair =
BluetoothUtils.getBtRainbowDrawableWithDescription(mContext, mCachedDevice);
imageView.setImageDrawable(pair.first);
imageView.setContentDescription(pair.second);
} }
final int batteryLevel = BluetoothUtils.getIntMetaData(bluetoothDevice, batteryMetaKey); final int batteryLevel = BluetoothUtils.getIntMetaData(bluetoothDevice, batteryMetaKey);
final boolean charging = BluetoothUtils.getBooleanMetaData(bluetoothDevice, chargeMetaKey); final boolean charging = BluetoothUtils.getBooleanMetaData(bluetoothDevice, chargeMetaKey);
if (DEBUG) { if (DEBUG) {
@@ -244,24 +265,35 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
+ ", charge : " + chargeMetaKey + ", batteryLevel : " + batteryLevel + ", charge : " + chargeMetaKey + ", batteryLevel : " + batteryLevel
+ ", charging : " + charging + ", iconUri : " + iconUri); + ", charging : " + charging + ", iconUri : " + iconUri);
} }
if (deviceId == LEFT_DEVICE_ID || deviceId == RIGHT_DEVICE_ID) {
if (batteryId != CASE_DEVICE_ID) { showBatteryPredictionIfNecessary(linearLayout, deviceId, batteryLevel);
showBatteryPredictionIfNecessary(linearLayout, batteryId, batteryLevel);
} }
final TextView batterySummaryView = linearLayout.findViewById(R.id.bt_battery_summary);
if (batteryLevel != BluetoothUtils.META_INT_ERROR) { if (batteryLevel != BluetoothUtils.META_INT_ERROR) {
linearLayout.setVisibility(View.VISIBLE); linearLayout.setVisibility(View.VISIBLE);
final TextView textView = linearLayout.findViewById(R.id.bt_battery_summary); batterySummaryView.setText(com.android.settings.Utils.formatPercentage(batteryLevel));
textView.setText(com.android.settings.Utils.formatPercentage(batteryLevel)); batterySummaryView.setVisibility(View.VISIBLE);
textView.setVisibility(View.VISIBLE);
showBatteryIcon(linearLayout, batteryLevel, charging, batteryMetaKey); showBatteryIcon(linearLayout, batteryLevel, charging, batteryMetaKey);
} else { } else {
// Hide it if it doesn't have battery information if (deviceId == MAIN_DEVICE_ID) {
linearLayout.setVisibility(View.GONE); linearLayout.setVisibility(View.VISIBLE);
batterySummaryView.setText(com.android.settings.Utils.formatPercentage(
bluetoothDevice.getBatteryLevel()));
batterySummaryView.setVisibility(View.VISIBLE);
linearLayout.findViewById(R.id.bt_battery_icon).setVisibility(View.GONE);
} else {
// Hide it if it doesn't have battery information
linearLayout.setVisibility(View.GONE);
}
} }
final TextView textView = linearLayout.findViewById(R.id.header_title); final TextView textView = linearLayout.findViewById(R.id.header_title);
textView.setText(titleResId); if (deviceId == MAIN_DEVICE_ID) {
textView.setVisibility(View.VISIBLE); textView.setVisibility(View.GONE);
} else {
textView.setText(titleResId);
textView.setVisibility(View.VISIBLE);
}
} }
private void showBatteryPredictionIfNecessary(LinearLayout linearLayout, int batteryId, private void showBatteryPredictionIfNecessary(LinearLayout linearLayout, int batteryId,

View File

@@ -16,10 +16,8 @@
package com.android.settings.bluetooth; package com.android.settings.bluetooth;
import android.bluetooth.BluetoothDevice;
import android.content.Context; import android.content.Context;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.provider.DeviceConfig;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Pair; import android.util.Pair;
@@ -27,7 +25,6 @@ import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.SettingsUIDeviceConfig;
import com.android.settings.widget.EntityHeaderController; import com.android.settings.widget.EntityHeaderController;
import com.android.settingslib.bluetooth.BluetoothUtils; import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
@@ -56,11 +53,7 @@ public class BluetoothDetailsHeaderController extends BluetoothDetailsController
@Override @Override
public boolean isAvailable() { public boolean isAvailable() {
final boolean advancedEnabled = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SETTINGS_UI, return !Utils.isAdvancedDetailsHeader(mCachedDevice.getDevice());
SettingsUIDeviceConfig.BT_ADVANCED_HEADER_ENABLED, true);
return !advancedEnabled
|| !BluetoothUtils.getBooleanMetaData(mCachedDevice.getDevice(),
BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET);
} }
@Override @Override

View File

@@ -21,14 +21,18 @@ import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile; import android.bluetooth.BluetoothProfile;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.provider.DeviceConfig;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.SettingsUIDeviceConfig;
import com.android.settings.overlay.FeatureFactory; import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.bluetooth.BluetoothUtils; import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.BluetoothUtils.ErrorListener; import com.android.settingslib.bluetooth.BluetoothUtils.ErrorListener;
@@ -153,4 +157,36 @@ public final class Utils {
return Settings.Global.getInt(context.getContentResolver(), return Settings.Global.getInt(context.getContentResolver(),
Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE, 0) == 1; Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE, 0) == 1;
} }
/**
* Check if the Bluetooth device supports advanced details header
*
* @param bluetoothDevice the BluetoothDevice to get metadata
* @return true if it supports advanced details header, false otherwise.
*/
public static boolean isAdvancedDetailsHeader(@NonNull BluetoothDevice bluetoothDevice) {
final boolean advancedEnabled = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SETTINGS_UI,
SettingsUIDeviceConfig.BT_ADVANCED_HEADER_ENABLED, true);
if (!advancedEnabled) {
Log.d(TAG, "isAdvancedDetailsHeader: advancedEnabled is false");
return false;
}
// The metadata is for Android R
final boolean untetheredHeadset = BluetoothUtils.getBooleanMetaData(bluetoothDevice,
BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET);
if (untetheredHeadset) {
Log.d(TAG, "isAdvancedDetailsHeader: untetheredHeadset is true");
return true;
}
// The metadata is for Android S
final String deviceType = BluetoothUtils.getStringMetaData(bluetoothDevice,
BluetoothDevice.METADATA_DEVICE_TYPE);
if (TextUtils.equals(deviceType, BluetoothDevice.DEVICE_TYPE_UNTETHERED_HEADSET)
|| TextUtils.equals(deviceType, BluetoothDevice.DEVICE_TYPE_WATCH)
|| TextUtils.equals(deviceType, BluetoothDevice.DEVICE_TYPE_DEFAULT)) {
Log.d(TAG, "isAdvancedDetailsHeader: deviceType is " + deviceType);
return true;
}
return false;
}
} }

View File

@@ -108,8 +108,61 @@ public class AdvancedBluetoothDetailsHeaderControllerTest {
assertThat(iconDrawable.getCharging()).isTrue(); assertThat(iconDrawable.getCharging()).isTrue();
} }
@Test
public void refresh_connectedWatch_behaveAsExpected() {
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_DEVICE_TYPE)).thenReturn(
BluetoothDevice.DEVICE_TYPE_WATCH.getBytes());
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)).thenReturn(
String.valueOf(false).getBytes());
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_MAIN_BATTERY)).thenReturn(
String.valueOf(BATTERY_LEVEL_MAIN).getBytes());
when(mCachedDevice.isConnected()).thenReturn(true);
mController.refresh();
assertThat(mLayoutPreference.findViewById(R.id.layout_left).getVisibility()).isEqualTo(
View.GONE);
assertThat(mLayoutPreference.findViewById(R.id.layout_right).getVisibility()).isEqualTo(
View.GONE);
assertThat(mLayoutPreference.findViewById(R.id.layout_middle).getVisibility()).isEqualTo(
View.VISIBLE);
assertBatteryLevel(mLayoutPreference.findViewById(R.id.layout_middle), BATTERY_LEVEL_MAIN);
}
@Test
public void refresh_connectedUntetheredHeadset_behaveAsExpected() {
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_DEVICE_TYPE)).thenReturn(
BluetoothDevice.DEVICE_TYPE_UNTETHERED_HEADSET.getBytes());
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)).thenReturn(
String.valueOf(false).getBytes());
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY)).thenReturn(
String.valueOf(BATTERY_LEVEL_LEFT).getBytes());
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY)).thenReturn(
String.valueOf(BATTERY_LEVEL_RIGHT).getBytes());
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_UNTETHERED_CASE_BATTERY)).thenReturn(
String.valueOf(BATTERY_LEVEL_MAIN).getBytes());
when(mCachedDevice.isConnected()).thenReturn(true);
mController.refresh();
assertBatteryLevel(mLayoutPreference.findViewById(R.id.layout_left), BATTERY_LEVEL_LEFT);
assertBatteryLevel(mLayoutPreference.findViewById(R.id.layout_right), BATTERY_LEVEL_RIGHT);
assertBatteryLevel(mLayoutPreference.findViewById(R.id.layout_middle), BATTERY_LEVEL_MAIN);
}
@Test @Test
public void refresh_connected_updateCorrectInfo() { public void refresh_connected_updateCorrectInfo() {
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)).thenReturn(
String.valueOf(true).getBytes());
when(mBluetoothDevice.getMetadata( when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY)).thenReturn( BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY)).thenReturn(
String.valueOf(BATTERY_LEVEL_LEFT).getBytes()); String.valueOf(BATTERY_LEVEL_LEFT).getBytes());
@@ -150,6 +203,9 @@ public class AdvancedBluetoothDetailsHeaderControllerTest {
@Test @Test
public void refresh_withLowBatteryAndUncharged_showAlertIcon() { public void refresh_withLowBatteryAndUncharged_showAlertIcon() {
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)).thenReturn(
String.valueOf(true).getBytes());
when(mBluetoothDevice.getMetadata( when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY)).thenReturn( BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY)).thenReturn(
String.valueOf(LOW_BATTERY_LEVEL).getBytes()); String.valueOf(LOW_BATTERY_LEVEL).getBytes());