Merge "Update PhoneAccount icon API (1/6)" into lmp-mr1-dev

This commit is contained in:
Ihab Awad
2014-11-11 22:56:07 +00:00
committed by Android (Google) Code Review

View File

@@ -124,6 +124,11 @@ public class PhoneAccount implements Parcelable {
*/ */
public static final int NO_COLOR = -1; public static final int NO_COLOR = -1;
/**
* Indicating no resource ID is set.
*/
public static final int NO_RESOURCE_ID = -1;
private final PhoneAccountHandle mAccountHandle; private final PhoneAccountHandle mAccountHandle;
private final Uri mAddress; private final Uri mAddress;
private final Uri mSubscriptionAddress; private final Uri mSubscriptionAddress;
@@ -131,7 +136,8 @@ public class PhoneAccount implements Parcelable {
private final int mIconResId; private final int mIconResId;
private final String mIconPackageName; private final String mIconPackageName;
private final Bitmap mIconBitmap; private final Bitmap mIconBitmap;
private final int mColor; private final int mIconTint;
private final int mHighlightColor;
private final CharSequence mLabel; private final CharSequence mLabel;
private final CharSequence mShortDescription; private final CharSequence mShortDescription;
private final List<String> mSupportedUriSchemes; private final List<String> mSupportedUriSchemes;
@@ -147,7 +153,8 @@ public class PhoneAccount implements Parcelable {
private int mIconResId; private int mIconResId;
private String mIconPackageName; private String mIconPackageName;
private Bitmap mIconBitmap; private Bitmap mIconBitmap;
private int mColor = NO_COLOR; private int mIconTint = NO_COLOR;
private int mHighlightColor = NO_COLOR;
private CharSequence mLabel; private CharSequence mLabel;
private CharSequence mShortDescription; private CharSequence mShortDescription;
private List<String> mSupportedUriSchemes = new ArrayList<String>(); private List<String> mSupportedUriSchemes = new ArrayList<String>();
@@ -174,7 +181,8 @@ public class PhoneAccount implements Parcelable {
mIconResId = phoneAccount.getIconResId(); mIconResId = phoneAccount.getIconResId();
mIconPackageName = phoneAccount.getIconPackageName(); mIconPackageName = phoneAccount.getIconPackageName();
mIconBitmap = phoneAccount.getIconBitmap(); mIconBitmap = phoneAccount.getIconBitmap();
mColor = phoneAccount.getColor(); mIconTint = phoneAccount.getIconTint();
mHighlightColor = phoneAccount.getHighlightColor();
mLabel = phoneAccount.getLabel(); mLabel = phoneAccount.getLabel();
mShortDescription = phoneAccount.getShortDescription(); mShortDescription = phoneAccount.getShortDescription();
mSupportedUriSchemes.addAll(phoneAccount.getSupportedUriSchemes()); mSupportedUriSchemes.addAll(phoneAccount.getSupportedUriSchemes());
@@ -214,46 +222,76 @@ public class PhoneAccount implements Parcelable {
} }
/** /**
* Sets the icon resource ID. See {@link PhoneAccount#getIconResId}. * Sets the icon. See {@link PhoneAccount#createIconDrawable}.
* *
* @param value The resource ID of the icon. * @param packageContext The package from which to load an icon.
* @param iconResId The resource in {@code iconPackageName} representing the icon.
* @return The builder. * @return The builder.
*/ */
public Builder setIconResId(int value) { public Builder setIcon(Context packageContext, int iconResId) {
this.mIconResId = value; return setIcon(packageContext.getPackageName(), iconResId);
}
/**
* Sets the icon. See {@link PhoneAccount#createIconDrawable}.
*
* @param iconPackageName The package from which to load an icon.
* @param iconResId The resource in {@code iconPackageName} representing the icon.
* @return The builder.
*/
public Builder setIcon(String iconPackageName, int iconResId) {
return setIcon(iconPackageName, iconResId, NO_COLOR);
}
/**
* Sets the icon. See {@link PhoneAccount#createIconDrawable}.
*
* @param packageContext The package from which to load an icon.
* @param iconResId The resource in {@code iconPackageName} representing the icon.
* @param iconTint A color with which to tint this icon.
* @return The builder.
*/
public Builder setIcon(Context packageContext, int iconResId, int iconTint) {
return setIcon(packageContext.getPackageName(), iconResId, iconTint);
}
/**
* Sets the icon. See {@link PhoneAccount#createIconDrawable}.
*
* @param iconPackageName The package from which to load an icon.
* @param iconResId The resource in {@code iconPackageName} representing the icon.
* @param iconTint A color with which to tint this icon.
* @return The builder.
*/
public Builder setIcon(String iconPackageName, int iconResId, int iconTint) {
this.mIconPackageName = iconPackageName;
this.mIconResId = iconResId;
this.mIconTint = iconTint;
return this; return this;
} }
/** /**
* Sets the icon package name. See {@link PhoneAccount#getIconPackageName}. * Sets the icon. See {@link PhoneAccount#createIconDrawable}.
* *
* @param value The name of the package from which to load the icon. * @param iconBitmap The icon bitmap.
* @return The builder. * @return The builder.
*/ */
public Builder setIconPackageName(String value) { public Builder setIcon(Bitmap iconBitmap) {
this.mIconPackageName = value; this.mIconBitmap = iconBitmap;
this.mIconPackageName = null;
this.mIconResId = NO_RESOURCE_ID;
this.mIconTint = NO_COLOR;
return this; return this;
} }
/** /**
* Sets the icon bitmap. See {@link PhoneAccount#getIconBitmap}. * Sets the highlight color. See {@link PhoneAccount#getHighlightColor}.
* *
* @param value The icon bitmap. * @param value The highlight color.
* @return The builder. * @return The builder.
*/ */
public Builder setIconBitmap(Bitmap value) { public Builder setHighlightColor(int value) {
this.mIconBitmap = value; this.mHighlightColor = value;
return this;
}
/**
* Sets the color. See {@link PhoneAccount#getColor}.
*
* @param value The resource ID of the icon.
* @return The builder.
*/
public Builder setColor(int value) {
this.mColor = value;
return this; return this;
} }
@@ -318,7 +356,8 @@ public class PhoneAccount implements Parcelable {
mIconResId, mIconResId,
mIconPackageName, mIconPackageName,
mIconBitmap, mIconBitmap,
mColor, mIconTint,
mHighlightColor,
mLabel, mLabel,
mShortDescription, mShortDescription,
mSupportedUriSchemes); mSupportedUriSchemes);
@@ -333,7 +372,8 @@ public class PhoneAccount implements Parcelable {
int iconResId, int iconResId,
String iconPackageName, String iconPackageName,
Bitmap iconBitmap, Bitmap iconBitmap,
int color, int iconTint,
int highlightColor,
CharSequence label, CharSequence label,
CharSequence shortDescription, CharSequence shortDescription,
List<String> supportedUriSchemes) { List<String> supportedUriSchemes) {
@@ -344,7 +384,8 @@ public class PhoneAccount implements Parcelable {
mIconResId = iconResId; mIconResId = iconResId;
mIconPackageName = iconPackageName; mIconPackageName = iconPackageName;
mIconBitmap = iconBitmap; mIconBitmap = iconBitmap;
mColor = color; mIconTint = iconTint;
mHighlightColor = highlightColor;
mLabel = label; mLabel = label;
mShortDescription = shortDescription; mShortDescription = shortDescription;
mSupportedUriSchemes = Collections.unmodifiableList(supportedUriSchemes); mSupportedUriSchemes = Collections.unmodifiableList(supportedUriSchemes);
@@ -474,7 +515,7 @@ public class PhoneAccount implements Parcelable {
* this method of indicating the icon rather than using {@link #getIconBitmap()}, since it * this method of indicating the icon rather than using {@link #getIconBitmap()}, since it
* leads to less resource usage. * leads to less resource usage.
* <p> * <p>
* Clients wishing to display a {@code PhoneAccount} should use {@link #getIcon(Context)}. * Clients wishing to display a {@code PhoneAccount} should use {@link #createIconDrawable(Context)}.
* *
* @return A resource ID. * @return A resource ID.
*/ */
@@ -488,7 +529,7 @@ public class PhoneAccount implements Parcelable {
* If this property is {@code null}, the resource {@link #getIconResId()} will be loaded from * If this property is {@code null}, the resource {@link #getIconResId()} will be loaded from
* the package in the {@link ComponentName} of the {@link #getAccountHandle()}. * the package in the {@link ComponentName} of the {@link #getAccountHandle()}.
* <p> * <p>
* Clients wishing to display a {@code PhoneAccount} should use {@link #getIcon(Context)}. * Clients wishing to display a {@code PhoneAccount} should use {@link #createIconDrawable(Context)}.
* *
* @return A package name. * @return A package name.
*/ */
@@ -497,12 +538,12 @@ public class PhoneAccount implements Parcelable {
} }
/** /**
* A highlight color to use in displaying information about this {@code PhoneAccount}. * A tint to apply to the icon of this {@code PhoneAccount}.
* *
* @return A hexadecimal color value. * @return A hexadecimal color value.
*/ */
public int getColor() { public int getIconTint() {
return mColor; return mIconTint;
} }
/** /**
@@ -511,7 +552,8 @@ public class PhoneAccount implements Parcelable {
* If this property is specified, it is to be considered the preferred icon. Otherwise, the * If this property is specified, it is to be considered the preferred icon. Otherwise, the
* resource specified by {@link #getIconResId()} should be used. * resource specified by {@link #getIconResId()} should be used.
* <p> * <p>
* Clients wishing to display a {@code PhoneAccount} should use {@link #getIcon(Context)}. * Clients wishing to display a {@code PhoneAccount} should use
* {@link #createIconDrawable(Context)}.
* *
* @return A bitmap. * @return A bitmap.
*/ */
@@ -519,6 +561,15 @@ public class PhoneAccount implements Parcelable {
return mIconBitmap; return mIconBitmap;
} }
/**
* A highlight color to use in displaying information about this {@code PhoneAccount}.
*
* @return A hexadecimal color value.
*/
public int getHighlightColor() {
return mHighlightColor;
}
/** /**
* Builds and returns an icon {@code Drawable} to represent this {@code PhoneAccount} in a user * Builds and returns an icon {@code Drawable} to represent this {@code PhoneAccount} in a user
* interface. Uses the properties {@link #getIconResId()}, {@link #getIconPackageName()}, and * interface. Uses the properties {@link #getIconResId()}, {@link #getIconPackageName()}, and
@@ -528,25 +579,26 @@ public class PhoneAccount implements Parcelable {
* *
* @return An icon for this {@code PhoneAccount}. * @return An icon for this {@code PhoneAccount}.
*/ */
public Drawable getIcon(Context context) { public Drawable createIconDrawable(Context context) {
if (mIconBitmap != null) { if (mIconBitmap != null) {
return new BitmapDrawable(context.getResources(), mIconBitmap); return new BitmapDrawable(context.getResources(), mIconBitmap);
} }
if (mIconResId != 0) { if (mIconResId != 0) {
String packageName = mIconPackageName == null
? mAccountHandle.getComponentName().getPackageName()
: mIconPackageName;
try { try {
Context packageContext = context.createPackageContext(packageName, 0); Context packageContext = context.createPackageContext(mIconPackageName, 0);
try { try {
return packageContext.getDrawable(mIconResId); Drawable iconDrawable = packageContext.getDrawable(mIconResId);
if (mIconTint != NO_COLOR) {
iconDrawable.setTint(mIconTint);
}
return iconDrawable;
} catch (NotFoundException | MissingResourceException e) { } catch (NotFoundException | MissingResourceException e) {
Log.e(this, e, "Cannot find icon %d in package %s", mIconResId, packageName); Log.e(this, e, "Cannot find icon %d in package %s",
mIconResId, mIconPackageName);
} }
} catch (PackageManager.NameNotFoundException e) { } catch (PackageManager.NameNotFoundException e) {
Log.w(this, "Cannot find package %s", packageName); Log.w(this, "Cannot find package %s", mIconPackageName);
} }
} }
@@ -564,17 +616,37 @@ public class PhoneAccount implements Parcelable {
@Override @Override
public void writeToParcel(Parcel out, int flags) { public void writeToParcel(Parcel out, int flags) {
out.writeParcelable(mAccountHandle, 0); if (mAccountHandle == null) {
out.writeParcelable(mAddress, 0); out.writeInt(0);
out.writeParcelable(mSubscriptionAddress, 0); } else {
out.writeInt(1);
mAccountHandle.writeToParcel(out, flags);
}
if (mAddress == null) {
out.writeInt(0);
} else {
out.writeInt(1);
mAddress.writeToParcel(out, flags);
}
if (mSubscriptionAddress == null) {
out.writeInt(0);
} else {
out.writeInt(1);
mSubscriptionAddress.writeToParcel(out, flags);
}
out.writeInt(mCapabilities); out.writeInt(mCapabilities);
out.writeInt(mIconResId); out.writeInt(mIconResId);
out.writeString(mIconPackageName); out.writeString(mIconPackageName);
out.writeParcelable(mIconBitmap, 0); if (mIconBitmap == null) {
out.writeInt(mColor); out.writeInt(0);
} else {
mIconBitmap.writeToParcel(out, flags);
}
out.writeInt(mIconTint);
out.writeInt(mHighlightColor);
out.writeCharSequence(mLabel); out.writeCharSequence(mLabel);
out.writeCharSequence(mShortDescription); out.writeCharSequence(mShortDescription);
out.writeList(mSupportedUriSchemes); out.writeStringList(mSupportedUriSchemes);
} }
public static final Creator<PhoneAccount> CREATOR public static final Creator<PhoneAccount> CREATOR
@@ -591,22 +663,34 @@ public class PhoneAccount implements Parcelable {
}; };
private PhoneAccount(Parcel in) { private PhoneAccount(Parcel in) {
ClassLoader classLoader = PhoneAccount.class.getClassLoader(); if (in.readInt() > 0) {
mAccountHandle = PhoneAccountHandle.CREATOR.createFromParcel(in);
mAccountHandle = in.readParcelable(getClass().getClassLoader()); } else {
mAddress = in.readParcelable(getClass().getClassLoader()); mAccountHandle = null;
mSubscriptionAddress = in.readParcelable(getClass().getClassLoader()); }
if (in.readInt() > 0) {
mAddress = Uri.CREATOR.createFromParcel(in);
} else {
mAddress = null;
}
if (in.readInt() > 0) {
mSubscriptionAddress = Uri.CREATOR.createFromParcel(in);
} else {
mSubscriptionAddress = null;
}
mCapabilities = in.readInt(); mCapabilities = in.readInt();
mIconResId = in.readInt(); mIconResId = in.readInt();
mIconPackageName = in.readString(); mIconPackageName = in.readString();
mIconBitmap = in.readParcelable(getClass().getClassLoader()); if (in.readInt() > 0) {
mColor = in.readInt(); mIconBitmap = Bitmap.CREATOR.createFromParcel(in);
} else {
mIconBitmap = null;
}
mIconTint = in.readInt();
mHighlightColor = in.readInt();
mLabel = in.readCharSequence(); mLabel = in.readCharSequence();
mShortDescription = in.readCharSequence(); mShortDescription = in.readCharSequence();
mSupportedUriSchemes = Collections.unmodifiableList(in.createStringArrayList());
List<String> supportedUriSchemes = new ArrayList<>();
in.readList(supportedUriSchemes, classLoader);
mSupportedUriSchemes = Collections.unmodifiableList(supportedUriSchemes);
} }
@Override @Override