From 98b7fdbd5c11af30afdf147d0c4cc44fe4ef240a Mon Sep 17 00:00:00 2001 From: jackqdyulei Date: Wed, 6 Sep 2017 16:38:23 -0700 Subject: [PATCH] Change battery icon to the side of bt icon Also add a new parameter to control the size of battery icon at the side. In spec the height of battery icon should be 55% of the bt icon. Since bt icon has different natural margin in its vector drawable. This cl adds a padding and then change the ratio to 75%. Bug: 65397557 Test: RunSettingsLibRoboTests Change-Id: I442149392edcec2ee23f651232ffe3e204badc38 --- packages/SettingsLib/res/values/dimens.xml | 7 +- .../graph/BatteryMeterDrawableBase.java | 5 +- .../graph/BluetoothDeviceLayerDrawable.java | 74 ++++++++++------------ .../graph/BluetoothDeviceLayerDrawableTest.java | 25 ++++++-- .../res/drawable/ic_qs_bluetooth_connected.xml | 4 +- packages/SystemUI/res/layout/qs_detail_item.xml | 2 +- packages/SystemUI/res/values/dimens.xml | 1 + .../android/systemui/qs/tiles/BluetoothTile.java | 4 +- 8 files changed, 70 insertions(+), 52 deletions(-) diff --git a/packages/SettingsLib/res/values/dimens.xml b/packages/SettingsLib/res/values/dimens.xml index 1e35cbc78343..e2615709cfb7 100644 --- a/packages/SettingsLib/res/values/dimens.xml +++ b/packages/SettingsLib/res/values/dimens.xml @@ -61,10 +61,13 @@ 10.5% - 7.5% + 7.8% - 45% + 35% + + + 75% diff --git a/packages/SettingsLib/src/com/android/settingslib/graph/BatteryMeterDrawableBase.java b/packages/SettingsLib/src/com/android/settingslib/graph/BatteryMeterDrawableBase.java index d1f91d917586..ec45b7e91700 100755 --- a/packages/SettingsLib/src/com/android/settingslib/graph/BatteryMeterDrawableBase.java +++ b/packages/SettingsLib/src/com/android/settingslib/graph/BatteryMeterDrawableBase.java @@ -293,6 +293,7 @@ public class BatteryMeterDrawableBase extends Drawable { @Override public void draw(Canvas c) { final int level = mLevel; + final Rect bounds = getBounds(); if (level == -1) return; @@ -301,8 +302,10 @@ public class BatteryMeterDrawableBase extends Drawable { final int width = (int) (getAspectRatio() * mHeight); final int px = (mWidth - width) / 2; final int buttonHeight = Math.round(height * mButtonHeightFraction); + final int left = mPadding.left + bounds.left; + final int top = bounds.bottom - mPadding.bottom - height; - mFrame.set(mPadding.left, mPadding.top, width + mPadding.left, height + mPadding.top); + mFrame.set(left, top, width + left, height + top); mFrame.offset(px, 0); // button-frame: area above the battery body diff --git a/packages/SettingsLib/src/com/android/settingslib/graph/BluetoothDeviceLayerDrawable.java b/packages/SettingsLib/src/com/android/settingslib/graph/BluetoothDeviceLayerDrawable.java index 61790b96099f..b7fd40485e4d 100644 --- a/packages/SettingsLib/src/com/android/settingslib/graph/BluetoothDeviceLayerDrawable.java +++ b/packages/SettingsLib/src/com/android/settingslib/graph/BluetoothDeviceLayerDrawable.java @@ -24,6 +24,7 @@ import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; +import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.LayerDrawable; @@ -47,7 +48,7 @@ public class BluetoothDeviceLayerDrawable extends LayerDrawable { /** * Create the {@link LayerDrawable} that contains bluetooth device icon and battery icon. - * This is a vertical layout drawable while bluetooth icon at top and battery icon at bottom. + * This is a horizontal layout drawable while bluetooth icon at start and battery icon at end. * * @param context used to get the spec for icon * @param resId represents the bluetooth device drawable @@ -55,55 +56,44 @@ public class BluetoothDeviceLayerDrawable extends LayerDrawable { */ public static BluetoothDeviceLayerDrawable createLayerDrawable(Context context, int resId, int batteryLevel) { + return createLayerDrawable(context, resId, batteryLevel, 1 /*iconScale*/); + } + + /** + * Create the {@link LayerDrawable} that contains bluetooth device icon and battery icon. + * This is a horizontal layout drawable while bluetooth icon at start and battery icon at end. + * + * @param context used to get the spec for icon + * @param resId represents the bluetooth device drawable + * @param batteryLevel the battery level for bluetooth device + * @param iconScale the ratio of height between battery icon and bluetooth icon + */ + public static BluetoothDeviceLayerDrawable createLayerDrawable(Context context, int resId, + int batteryLevel, float iconScale) { final Drawable deviceDrawable = context.getDrawable(resId); final BatteryMeterDrawable batteryDrawable = new BatteryMeterDrawable(context, R.color.meter_background_color, batteryLevel); - final int pad = context.getResources() - .getDimensionPixelSize(R.dimen.bt_battery_padding); - batteryDrawable.setPadding(0, pad, 0, pad); + final int pad = context.getResources().getDimensionPixelSize(R.dimen.bt_battery_padding); + batteryDrawable.setPadding(pad, pad, pad, pad); final BluetoothDeviceLayerDrawable drawable = new BluetoothDeviceLayerDrawable( - new Drawable[]{deviceDrawable, - rotateDrawable(context.getResources(), batteryDrawable)}); - // Set the bluetooth icon at the top - drawable.setLayerGravity(0 /* index of deviceDrawable */, Gravity.TOP); - // Set battery icon right below the bluetooth icon - drawable.setLayerInset(1 /* index of batteryDrawable */, 0, - deviceDrawable.getIntrinsicHeight(), 0, 0); + new Drawable[]{deviceDrawable, batteryDrawable}); + // Set the bluetooth icon at the left + drawable.setLayerGravity(0 /* index of deviceDrawable */, Gravity.START); + // Set battery icon to the right of the bluetooth icon + drawable.setLayerInsetStart(1 /* index of batteryDrawable */, + deviceDrawable.getIntrinsicWidth()); + drawable.setLayerInsetTop(1 /* index of batteryDrawable */, + (int) (deviceDrawable.getIntrinsicHeight() * (1 - iconScale))); - drawable.setConstantState(context, resId, batteryLevel); + drawable.setConstantState(context, resId, batteryLevel, iconScale); return drawable; } - /** - * Rotate the {@code drawable} by 90 degree clockwise and return rotated {@link Drawable} - */ - private static Drawable rotateDrawable(Resources res, Drawable drawable) { - // Get the bitmap from drawable - final Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), - drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); - final Canvas canvas = new Canvas(bitmap); - drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); - drawable.draw(canvas); - - // Create rotate matrix - final Matrix matrix = new Matrix(); - matrix.postRotate( - res.getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_LTR - ? 90 : 270); - - // Create new bitmap with rotate matrix - final Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), - bitmap.getHeight(), matrix, true); - bitmap.recycle(); - - return new BitmapDrawable(res, rotateBitmap); - } - - public void setConstantState(Context context, int resId, int batteryLevel) { - mState = new BluetoothDeviceLayerDrawableState(context, resId, batteryLevel); + public void setConstantState(Context context, int resId, int batteryLevel, float iconScale) { + mState = new BluetoothDeviceLayerDrawableState(context, resId, batteryLevel, iconScale); } @Override @@ -149,17 +139,19 @@ public class BluetoothDeviceLayerDrawable extends LayerDrawable { Context context; int resId; int batteryLevel; + float iconScale; public BluetoothDeviceLayerDrawableState(Context context, int resId, - int batteryLevel) { + int batteryLevel, float iconScale) { this.context = context; this.resId = resId; this.batteryLevel = batteryLevel; + this.iconScale = iconScale; } @Override public Drawable newDrawable() { - return createLayerDrawable(context, resId, batteryLevel); + return createLayerDrawable(context, resId, batteryLevel, iconScale); } @Override diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/graph/BluetoothDeviceLayerDrawableTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/graph/BluetoothDeviceLayerDrawableTest.java index 89855bec5782..76760a966667 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/graph/BluetoothDeviceLayerDrawableTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/graph/BluetoothDeviceLayerDrawableTest.java @@ -39,6 +39,8 @@ import org.robolectric.annotation.Config; public class BluetoothDeviceLayerDrawableTest { private static final int RES_ID = R.drawable.ic_bt_cellphone; private static final int BATTERY_LEVEL = 15; + private static final float BATTERY_ICON_SCALE = 0.75f; + private static final int BATTERY_ICON_PADDING_TOP = 6; private static final float TOLERANCE = 0.001f; private Context mContext; @@ -54,9 +56,24 @@ public class BluetoothDeviceLayerDrawableTest { mContext, RES_ID, BATTERY_LEVEL); assertThat(drawable.getDrawable(0)).isInstanceOf(VectorDrawable.class); - assertThat(drawable.getDrawable(1)).isInstanceOf(BitmapDrawable.class); - assertThat(drawable.getLayerInsetTop(1)).isEqualTo( - drawable.getDrawable(0).getIntrinsicHeight()); + assertThat(drawable.getDrawable(1)).isInstanceOf( + BluetoothDeviceLayerDrawable.BatteryMeterDrawable.class); + assertThat(drawable.getLayerInsetStart(1)).isEqualTo( + drawable.getDrawable(0).getIntrinsicWidth()); + assertThat(drawable.getLayerInsetTop(1)).isEqualTo(0); + } + + @Test + public void testCreateLayerDrawable_withIconScale_configCorrect() { + BluetoothDeviceLayerDrawable drawable = BluetoothDeviceLayerDrawable.createLayerDrawable( + mContext, RES_ID, BATTERY_LEVEL, BATTERY_ICON_SCALE); + + assertThat(drawable.getDrawable(0)).isInstanceOf(VectorDrawable.class); + assertThat(drawable.getDrawable(1)).isInstanceOf( + BluetoothDeviceLayerDrawable.BatteryMeterDrawable.class); + assertThat(drawable.getLayerInsetStart(1)).isEqualTo( + drawable.getDrawable(0).getIntrinsicWidth()); + assertThat(drawable.getLayerInsetTop(1)).isEqualTo(BATTERY_ICON_PADDING_TOP); } @Test @@ -65,7 +82,7 @@ public class BluetoothDeviceLayerDrawableTest { new BluetoothDeviceLayerDrawable.BatteryMeterDrawable(mContext, R.color.meter_background_color, BATTERY_LEVEL); - assertThat(batteryDrawable.getAspectRatio()).isWithin(TOLERANCE).of(0.45f); + assertThat(batteryDrawable.getAspectRatio()).isWithin(TOLERANCE).of(0.35f); assertThat(batteryDrawable.getRadiusRatio()).isWithin(TOLERANCE).of(0f); assertThat(batteryDrawable.getBatteryLevel()).isEqualTo(BATTERY_LEVEL); } diff --git a/packages/SystemUI/res/drawable/ic_qs_bluetooth_connected.xml b/packages/SystemUI/res/drawable/ic_qs_bluetooth_connected.xml index 3eb81f84dc2a..b673e4f3b081 100644 --- a/packages/SystemUI/res/drawable/ic_qs_bluetooth_connected.xml +++ b/packages/SystemUI/res/drawable/ic_qs_bluetooth_connected.xml @@ -14,8 +14,8 @@ Copyright (C) 2017 The Android Open Source Project limitations under the License. --> diff --git a/packages/SystemUI/res/layout/qs_detail_item.xml b/packages/SystemUI/res/layout/qs_detail_item.xml index 97e82ffb92f4..0844bb46d53f 100644 --- a/packages/SystemUI/res/layout/qs_detail_item.xml +++ b/packages/SystemUI/res/layout/qs_detail_item.xml @@ -26,7 +26,7 @@ 16dp 4dp 24dp + 32dp 0dp 20dp 16dp diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/BluetoothTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/BluetoothTile.java index bc6233d45a67..a0eb6aed4dfc 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/tiles/BluetoothTile.java +++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/BluetoothTile.java @@ -136,7 +136,9 @@ public class BluetoothTile extends QSTileImpl { int batteryLevel = lastDevice.getBatteryLevel(); if (batteryLevel != BluetoothDevice.BATTERY_LEVEL_UNKNOWN) { BluetoothDeviceLayerDrawable drawable = createLayerDrawable(mContext, - R.drawable.ic_qs_bluetooth_connected, batteryLevel); + R.drawable.ic_qs_bluetooth_connected, batteryLevel, + mContext.getResources().getFraction( + R.fraction.bt_battery_scale_fraction, 1, 1)); state.icon = new DrawableIcon(drawable); } } -- 2.11.0