Merge "Add api to get drawable of MediaDevice" into rvc-dev am: 205a1d5b5b

Change-Id: I62356e374ddc81fe401c7e7c111943b44e21754e
This commit is contained in:
TreeHugger Robot
2020-05-26 09:16:39 +00:00
committed by Automerger Merge Worker
5 changed files with 64 additions and 25 deletions

View File

@@ -7,6 +7,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.provider.MediaStore; import android.provider.MediaStore;
@@ -132,6 +133,44 @@ public class BluetoothUtils {
*/ */
public static Pair<Drawable, String> getBtRainbowDrawableWithDescription(Context context, public static Pair<Drawable, String> getBtRainbowDrawableWithDescription(Context context,
CachedBluetoothDevice cachedDevice) { CachedBluetoothDevice cachedDevice) {
final Resources resources = context.getResources();
final Pair<Drawable, String> pair = BluetoothUtils.getBtDrawableWithDescription(context,
cachedDevice);
if (pair.first instanceof BitmapDrawable) {
return new Pair<>(new AdaptiveOutlineDrawable(
resources, ((BitmapDrawable) pair.first).getBitmap()), pair.second);
}
return new Pair<>(buildBtRainbowDrawable(context,
pair.first, cachedDevice.getAddress().hashCode()), pair.second);
}
/**
* Build Bluetooth device icon with rainbow
*/
public static Drawable buildBtRainbowDrawable(Context context, Drawable drawable,
int hashCode) {
final Resources resources = context.getResources();
// 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(hashCode % iconBgColors.length);
drawable.setTint(iconFgColors[index]);
final Drawable adaptiveIcon = new AdaptiveIcon(context, drawable);
((AdaptiveIcon) adaptiveIcon).setBackgroundColor(iconBgColors[index]);
return adaptiveIcon;
}
/**
* Get bluetooth icon with description
*/
public static Pair<Drawable, String> getBtDrawableWithDescription(Context context,
CachedBluetoothDevice cachedDevice) {
final Pair<Drawable, String> pair = BluetoothUtils.getBtClassDrawableWithDescription( final Pair<Drawable, String> pair = BluetoothUtils.getBtClassDrawableWithDescription(
context, cachedDevice); context, cachedDevice);
final BluetoothDevice bluetoothDevice = cachedDevice.getDevice(); final BluetoothDevice bluetoothDevice = cachedDevice.getDevice();
@@ -159,9 +198,8 @@ public class BluetoothUtils {
final Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, iconSize, final Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, iconSize,
iconSize, false); iconSize, false);
bitmap.recycle(); bitmap.recycle();
final AdaptiveOutlineDrawable drawable = new AdaptiveOutlineDrawable( return new Pair<>(new BitmapDrawable(resources,
resources, resizedBitmap); resizedBitmap), pair.second);
return new Pair<>(drawable, pair.second);
} }
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "Failed to get drawable for: " + iconUri, e); Log.e(TAG, "Failed to get drawable for: " + iconUri, e);
@@ -171,28 +209,7 @@ public class BluetoothUtils {
} }
} }
return new Pair<>(buildBtRainbowDrawable(context, return new Pair<>(pair.first, pair.second);
pair.first, cachedDevice.getAddress().hashCode()), pair.second);
}
/**
* Build Bluetooth device icon with rainbow
*/
public static Drawable buildBtRainbowDrawable(Context context, Drawable drawable,
int hashCode) {
final Resources resources = context.getResources();
// 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(hashCode % iconBgColors.length);
drawable.setTint(iconFgColors[index]);
final Drawable adaptiveIcon = new AdaptiveIcon(context, drawable);
((AdaptiveIcon) adaptiveIcon).setBackgroundColor(iconBgColors[index]);
return adaptiveIcon;
} }
/** /**

View File

@@ -62,6 +62,11 @@ public class BluetoothMediaDevice extends MediaDevice {
return pair.first; return pair.first;
} }
@Override
public Drawable getIconWithoutBackground() {
return BluetoothUtils.getBtDrawableWithDescription(mContext, mCachedDevice).first;
}
@Override @Override
public String getId() { public String getId() {
return MediaDeviceUtils.getId(mCachedDevice); return MediaDeviceUtils.getId(mCachedDevice);

View File

@@ -60,6 +60,11 @@ public class InfoMediaDevice extends MediaDevice {
mContext.getDrawable(getDrawableResId()), getId().hashCode()); mContext.getDrawable(getDrawableResId()), getId().hashCode());
} }
@Override
public Drawable getIconWithoutBackground() {
return mContext.getDrawable(getDrawableResId());
}
@VisibleForTesting @VisibleForTesting
int getDrawableResId() { int getDrawableResId() {
int resId; int resId;

View File

@@ -152,6 +152,13 @@ public abstract class MediaDevice implements Comparable<MediaDevice> {
*/ */
public abstract Drawable getIcon(); public abstract Drawable getIcon();
/**
* Get icon of MediaDevice without background.
*
* @return drawable of icon
*/
public abstract Drawable getIconWithoutBackground();
/** /**
* Get unique ID that represent MediaDevice * Get unique ID that represent MediaDevice
* @return unique id of MediaDevice * @return unique id of MediaDevice

View File

@@ -89,6 +89,11 @@ public class PhoneMediaDevice extends MediaDevice {
mContext.getDrawable(getDrawableResId()), getId().hashCode()); mContext.getDrawable(getDrawableResId()), getId().hashCode());
} }
@Override
public Drawable getIconWithoutBackground() {
return mContext.getDrawable(getDrawableResId());
}
@VisibleForTesting @VisibleForTesting
int getDrawableResId() { int getDrawableResId() {
int resId; int resId;