Add a util method of creating Icon with Drawable
- Add a util method to help create Icon with Drawable so that users don't need to handle bitmaps - Add support for creating with ColorDrawable Test: robotest Fixes: 124407373 Change-Id: I1897256821cc804a5e599d967feb84a3bcd689a9
This commit is contained in:
@@ -89,6 +89,7 @@ import android.widget.ListView;
|
||||
import android.widget.TabWidget;
|
||||
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.core.graphics.drawable.IconCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceGroup;
|
||||
@@ -950,24 +951,30 @@ public final class Utils extends com.android.settingslib.Utils {
|
||||
bitmap = Bitmap.createScaledBitmap(((BitmapDrawable) original).getBitmap(), width,
|
||||
height, false);
|
||||
} else {
|
||||
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||
final Canvas canvas = new Canvas(bitmap);
|
||||
original.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
|
||||
original.draw(canvas);
|
||||
bitmap = createBitmap(original, width, height);
|
||||
}
|
||||
return new BitmapDrawable(null, bitmap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the {@link Drawable} to a {@link Bitmap}.
|
||||
* Create an Icon pointing to a drawable.
|
||||
*/
|
||||
public static Bitmap drawableToBitmap(Drawable drawable) {
|
||||
public static IconCompat createIconWithDrawable(Drawable drawable) {
|
||||
Bitmap bitmap;
|
||||
if (drawable instanceof BitmapDrawable) {
|
||||
return ((BitmapDrawable)drawable).getBitmap();
|
||||
bitmap = ((BitmapDrawable)drawable).getBitmap();
|
||||
} else {
|
||||
final int width = drawable.getIntrinsicWidth();
|
||||
final int height = drawable.getIntrinsicHeight();
|
||||
bitmap = createBitmap(drawable,
|
||||
width > 0 ? width : 1,
|
||||
height > 0 ? height : 1);
|
||||
}
|
||||
return IconCompat.createWithBitmap(bitmap);
|
||||
}
|
||||
|
||||
final Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
|
||||
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
|
||||
private static Bitmap createBitmap(Drawable drawable, int width, int height) {
|
||||
final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||
final Canvas canvas = new Canvas(bitmap);
|
||||
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
|
||||
drawable.draw(canvas);
|
||||
|
@@ -220,7 +220,7 @@ public class BluetoothDevicesSlice implements CustomSliceable {
|
||||
.getBtClassDrawableWithDescription(mContext, device);
|
||||
|
||||
if (pair.first != null) {
|
||||
return IconCompat.createWithBitmap(Utils.drawableToBitmap(pair.first));
|
||||
return Utils.createIconWithDrawable(pair.first);
|
||||
} else {
|
||||
return IconCompat.createWithResource(mContext,
|
||||
com.android.internal.R.drawable.ic_settings_bluetooth);
|
||||
|
@@ -30,9 +30,6 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
@@ -243,7 +240,7 @@ public class NotificationChannelSlice implements CustomSliceable {
|
||||
return null;
|
||||
}
|
||||
|
||||
return IconCompat.createWithBitmap(Utils.drawableToBitmap(drawable));
|
||||
return Utils.createIconWithDrawable(drawable);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
|
@@ -23,8 +23,6 @@ import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.UserHandle;
|
||||
@@ -87,7 +85,7 @@ public class MediaOutputSlice implements CustomSliceable {
|
||||
|
||||
final Drawable drawable =
|
||||
Utils.getBadgedIcon(mIconDrawableFactory, pm, mPackageName, UserHandle.myUserId());
|
||||
final IconCompat icon = IconCompat.createWithBitmap(getBitmapFromDrawable(drawable));
|
||||
final IconCompat icon = Utils.createIconWithDrawable(drawable);
|
||||
|
||||
@ColorInt final int color = Utils.getColorAccentDefaultColor(mContext);
|
||||
final SliceAction primarySliceAction = SliceAction.createDeeplink(getPrimaryAction(), icon,
|
||||
@@ -134,17 +132,6 @@ public class MediaOutputSlice implements CustomSliceable {
|
||||
return PendingIntent.getActivity(mContext, 0 /* requestCode */, intent, 0 /* flags */);
|
||||
}
|
||||
|
||||
private Bitmap getBitmapFromDrawable(Drawable 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);
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
private ListBuilder.RowBuilder getMediaDeviceRow(MediaDevice device) {
|
||||
final String title = device.getName();
|
||||
final PendingIntent broadcastAction =
|
||||
|
@@ -90,7 +90,7 @@ public class ContextualWifiSlice extends WifiSlice {
|
||||
}
|
||||
|
||||
d.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
|
||||
return IconCompat.createWithBitmap(Utils.drawableToBitmap(d));
|
||||
return Utils.createIconWithDrawable(d);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -34,6 +34,11 @@ import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.UserInfo;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.graphics.drawable.VectorDrawable;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.LinkAddress;
|
||||
import android.net.LinkProperties;
|
||||
@@ -48,6 +53,8 @@ import android.util.IconDrawableFactory;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.core.graphics.drawable.IconCompat;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -96,7 +103,7 @@ public class UtilsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetWifiIpAddresses_succeeds() throws Exception {
|
||||
public void getWifiIpAddresses_succeeds() throws Exception {
|
||||
when(wifiManager.getCurrentNetwork()).thenReturn(network);
|
||||
LinkAddress address = new LinkAddress(InetAddress.getByName("127.0.0.1"), 0);
|
||||
LinkProperties lp = new LinkProperties();
|
||||
@@ -107,7 +114,7 @@ public class UtilsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetWifiIpAddresses_nullLinkProperties() {
|
||||
public void getWifiIpAddresses_nullLinkProperties() {
|
||||
when(wifiManager.getCurrentNetwork()).thenReturn(network);
|
||||
// Explicitly set the return value to null for readability sake.
|
||||
when(connectivityManager.getLinkProperties(network)).thenReturn(null);
|
||||
@@ -116,7 +123,7 @@ public class UtilsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetWifiIpAddresses_nullNetwork() {
|
||||
public void getWifiIpAddresses_nullNetwork() {
|
||||
// Explicitly set the return value to null for readability sake.
|
||||
when(wifiManager.getCurrentNetwork()).thenReturn(null);
|
||||
|
||||
@@ -124,7 +131,7 @@ public class UtilsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitializeVolumeDoesntBreakOnNullVolume() {
|
||||
public void initializeVolumeDoesntBreakOnNullVolume() {
|
||||
VolumeInfo info = new VolumeInfo("id", 0, new DiskInfo("id", 0), "");
|
||||
StorageManager storageManager = mock(StorageManager.class, RETURNS_DEEP_STUBS);
|
||||
when(storageManager.findVolumeById(anyString())).thenReturn(info);
|
||||
@@ -133,13 +140,13 @@ public class UtilsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetInstallationStatus_notInstalled_shouldReturnUninstalled() {
|
||||
public void getInstallationStatus_notInstalled_shouldReturnUninstalled() {
|
||||
assertThat(Utils.getInstallationStatus(new ApplicationInfo()))
|
||||
.isEqualTo(R.string.not_installed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetInstallationStatus_enabled_shouldReturnInstalled() {
|
||||
public void getInstallationStatus_enabled_shouldReturnInstalled() {
|
||||
final ApplicationInfo info = new ApplicationInfo();
|
||||
info.flags = ApplicationInfo.FLAG_INSTALLED;
|
||||
info.enabled = true;
|
||||
@@ -148,7 +155,7 @@ public class UtilsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetInstallationStatus_disabled_shouldReturnDisabled() {
|
||||
public void getInstallationStatus_disabled_shouldReturnDisabled() {
|
||||
final ApplicationInfo info = new ApplicationInfo();
|
||||
info.flags = ApplicationInfo.FLAG_INSTALLED;
|
||||
info.enabled = false;
|
||||
@@ -157,7 +164,7 @@ public class UtilsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsProfileOrDeviceOwner_deviceOwnerApp_returnTrue() {
|
||||
public void isProfileOrDeviceOwner_deviceOwnerApp_returnTrue() {
|
||||
when(mDevicePolicyManager.isDeviceOwnerAppOnAnyUser(PACKAGE_NAME)).thenReturn(true);
|
||||
|
||||
assertThat(Utils.isProfileOrDeviceOwner(mUserManager, mDevicePolicyManager, PACKAGE_NAME))
|
||||
@@ -165,7 +172,7 @@ public class UtilsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsProfileOrDeviceOwner_profileOwnerApp_returnTrue() {
|
||||
public void isProfileOrDeviceOwner_profileOwnerApp_returnTrue() {
|
||||
final List<UserInfo> userInfos = new ArrayList<>();
|
||||
userInfos.add(new UserInfo());
|
||||
|
||||
@@ -178,7 +185,7 @@ public class UtilsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetEditTextCursorPosition_shouldGetExpectedEditTextLenght() {
|
||||
public void setEditTextCursorPosition_shouldGetExpectedEditTextLenght() {
|
||||
final EditText editText = new EditText(mContext);
|
||||
final CharSequence text = "test";
|
||||
editText.setText(text, TextView.BufferType.EDITABLE);
|
||||
@@ -189,7 +196,36 @@ public class UtilsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBadgedIcon_usePackageNameAndUserId()
|
||||
public void createIconWithDrawable_BitmapDrawable() {
|
||||
final Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
|
||||
final BitmapDrawable drawable = new BitmapDrawable(mContext.getResources(), bitmap);
|
||||
|
||||
final IconCompat icon = Utils.createIconWithDrawable(drawable);
|
||||
|
||||
assertThat(icon.getBitmap()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createIconWithDrawable_ColorDrawable() {
|
||||
final ColorDrawable drawable = new ColorDrawable(Color.BLACK);
|
||||
|
||||
final IconCompat icon = Utils.createIconWithDrawable(drawable);
|
||||
|
||||
assertThat(icon.getBitmap()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createIconWithDrawable_VectorDrawable() {
|
||||
final VectorDrawable drawable = VectorDrawable.create(mContext.getResources(),
|
||||
R.drawable.ic_settings_accent);
|
||||
|
||||
final IconCompat icon = Utils.createIconWithDrawable(drawable);
|
||||
|
||||
assertThat(icon.getBitmap()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getBadgedIcon_usePackageNameAndUserId()
|
||||
throws PackageManager.NameNotFoundException {
|
||||
doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfoAsUser(
|
||||
PACKAGE_NAME, PackageManager.GET_META_DATA, USER_ID);
|
||||
|
@@ -22,14 +22,11 @@ import static org.mockito.Mockito.mock;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Matrix;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.SurfaceTexture;
|
||||
import android.util.Size;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import com.google.zxing.BinaryBitmap;
|
||||
import com.google.zxing.LuminanceSource;
|
||||
import com.google.zxing.RGBLuminanceSource;
|
||||
|
Reference in New Issue
Block a user