Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 678f4fc1 authored by hughchen's avatar hughchen
Browse files

Add method to get rainbow bt icon

- Add method to get rainbow bt icon
- Add fast pair icon with background shape

Bug: 128570540
Test: RunSettingsLibRoboTests
Change-Id: Idf97d3c1fd892d9b3188b61be47d190ac2075af9
parent ed6a4d28
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -603,4 +603,26 @@
        <item>3</item><item>3</item>
    </array>

    <!-- Bluetooth icon foreground colors -->
    <integer-array name="bt_icon_fg_colors">
        <item>@color/bt_color_icon_1</item>
        <item>@color/bt_color_icon_2</item>
        <item>@color/bt_color_icon_3</item>
        <item>@color/bt_color_icon_4</item>
        <item>@color/bt_color_icon_5</item>
        <item>@color/bt_color_icon_6</item>
        <item>@color/bt_color_icon_7</item>
    </integer-array>

    <!-- Bluetooth icon background colors -->
    <integer-array name="bt_icon_bg_colors">
        <item>@color/bt_color_bg_1</item>
        <item>@color/bt_color_bg_2</item>
        <item>@color/bt_color_bg_3</item>
        <item>@color/bt_color_bg_4</item>
        <item>@color/bt_color_bg_5</item>
        <item>@color/bt_color_bg_6</item>
        <item>@color/bt_color_bg_7</item>
    </integer-array>

</resources>
+16 −0
Original line number Diff line number Diff line
@@ -19,4 +19,20 @@

    <color name="usage_graph_dots">@*android:color/tertiary_device_default_settings</color>
    <color name="list_divider_color">#64000000</color>

    <color name="bt_color_icon_1">#48a50e0e</color> <!-- 72% Material Red 900 -->
    <color name="bt_color_icon_2">#480d652d</color> <!-- 72% Material Green 900 -->
    <color name="bt_color_icon_3">#48e37400</color> <!-- 72% Material Yellow 900 -->
    <color name="bt_color_icon_4">#48b06000</color> <!-- 72% Material Orange 900 -->
    <color name="bt_color_icon_5">#489c166b</color> <!-- 72% Material Pink 900 -->
    <color name="bt_color_icon_6">#48681da8</color> <!-- 72% Material Purple 900 -->
    <color name="bt_color_icon_7">#48007b83</color> <!-- 72% Material Cyan 900 -->

    <color name="bt_color_bg_1">#fad2cf</color> <!-- Material Red 100 -->
    <color name="bt_color_bg_2">#ceead6</color> <!-- Material Green 100 -->
    <color name="bt_color_bg_3">#feefc3</color> <!-- Material Yellow 100 -->
    <color name="bt_color_bg_4">#fedfc8</color> <!-- Material Orange 100 -->
    <color name="bt_color_bg_5">#fdcfe8</color> <!-- Material Pink 100 -->
    <color name="bt_color_bg_6">#e9d2fd</color> <!-- Material Purple 100 -->
    <color name="bt_color_bg_7">#cbf0f8</color> <!-- Material Cyan 100 -->
</resources>
+2 −1
Original line number Diff line number Diff line
@@ -91,6 +91,7 @@
    <!-- How far to inset the rounded edges -->
    <dimen name="stat_sys_mobile_signal_circle_inset">0.9dp</dimen>


    <!-- Size of nearby icon -->
    <dimen name="bt_nearby_icon_size">24dp</dimen>

</resources>
+65 −0
Original line number Diff line number Diff line
package com.android.settingslib.bluetooth;

import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.util.Pair;

import androidx.annotation.DrawableRes;

import com.android.settingslib.R;
import com.android.settingslib.widget.AdaptiveIcon;
import com.android.settingslib.widget.AdaptiveOutlineDrawable;

import java.io.IOException;
import java.util.List;

public class BluetoothUtils {
    private static final String TAG = "BluetoothUtils";

    public static final boolean V = false; // verbose logging
    public static final boolean D = true;  // regular logging

@@ -112,4 +124,57 @@ public class BluetoothUtils {
    public static Drawable getBluetoothDrawable(Context context, @DrawableRes int resId) {
        return context.getDrawable(resId);
    }

    /**
     * Get colorful bluetooth icon with description
     */
    public static Pair<Drawable, String> getBtRainbowDrawableWithDescription(Context context,
            CachedBluetoothDevice cachedDevice) {
        final Pair<Drawable, String> pair = BluetoothUtils.getBtClassDrawableWithDescription(
                context, cachedDevice);
        final BluetoothDevice bluetoothDevice = cachedDevice.getDevice();
        final boolean untetheredHeadset = bluetoothDevice != null
                ? Boolean.parseBoolean(bluetoothDevice.getMetadata(
                        BluetoothDevice.METADATA_IS_UNTHETHERED_HEADSET))
                : false;
        final int iconSize = context.getResources().getDimensionPixelSize(
                R.dimen.bt_nearby_icon_size);
        final Resources resources = context.getResources();

        // Deal with untethered headset
        if (untetheredHeadset) {
            final String uriString = bluetoothDevice != null
                    ? bluetoothDevice.getMetadata(BluetoothDevice.METADATA_MAIN_ICON)
                    : null;
            final Uri iconUri = uriString != null ? Uri.parse(uriString) : null;
            if (iconUri != null) {
                try {
                    final Bitmap bitmap = MediaStore.Images.Media.getBitmap(
                            context.getContentResolver(), iconUri);
                    if (bitmap != null) {
                        final Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, iconSize,
                                iconSize, false);
                        bitmap.recycle();
                        final AdaptiveOutlineDrawable drawable = new AdaptiveOutlineDrawable(
                                resources, resizedBitmap);
                        return new Pair<>(drawable, pair.second);
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Failed to get drawable for: " + iconUri, e);
                }
            }
        }

        // Deal with normal headset
        final int[] iconFgColors = resources.getIntArray(R.array.bt_icon_fg_colors);
        final int[] iconBgColors = resources.getIntArray(R.array.bt_icon_bg_colors);

        // get color index based on mac address
        final int index =  Math.abs(cachedDevice.getAddress().hashCode()) % iconBgColors.length;
        pair.first.setColorFilter(iconFgColors[index], PorterDuff.Mode.SRC_ATOP);
        final Drawable adaptiveIcon = new AdaptiveIcon(context, pair.first);
        ((AdaptiveIcon) adaptiveIcon).setBackgroundColor(iconBgColors[index]);

        return new Pair<>(adaptiveIcon, pair.second);
    }
}
+20 −0
Original line number Diff line number Diff line
@@ -15,15 +15,20 @@
 */
package com.android.settingslib.bluetooth;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Pair;

import com.android.settingslib.widget.AdaptiveIcon;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -38,6 +43,9 @@ public class BluetoothUtilsTest {

    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    private CachedBluetoothDevice mCachedBluetoothDevice;
    @Mock
    private BluetoothDevice mBluetoothDevice;

    private Context mContext;

    @Before
@@ -66,4 +74,16 @@ public class BluetoothUtilsTest {

        verify(mContext).getDrawable(com.android.internal.R.drawable.ic_bt_laptop);
    }

    @Test
    public void getBtRainbowDrawableWithDescription_normalHeadset_returnAdaptiveIcon() {
        when(mBluetoothDevice.getMetadata(
                BluetoothDevice.METADATA_IS_UNTHETHERED_HEADSET)).thenReturn("false");
        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
        when(mCachedBluetoothDevice.getAddress()).thenReturn("1f:aa:bb");

        assertThat(BluetoothUtils.getBtRainbowDrawableWithDescription(
                RuntimeEnvironment.application,
                mCachedBluetoothDevice).first).isInstanceOf(AdaptiveIcon.class);
    }
}
 No newline at end of file