diff options
Diffstat (limited to 'telecomm/java/android/telecom/PhoneAccount.java')
| -rw-r--r-- | telecomm/java/android/telecom/PhoneAccount.java | 356 |
1 files changed, 319 insertions, 37 deletions
diff --git a/telecomm/java/android/telecom/PhoneAccount.java b/telecomm/java/android/telecom/PhoneAccount.java index 1d6d8bc..052a481 100644 --- a/telecomm/java/android/telecom/PhoneAccount.java +++ b/telecomm/java/android/telecom/PhoneAccount.java @@ -17,9 +17,14 @@ package android.telecom; import android.annotation.SystemApi; +import android.content.ComponentName; import android.content.Context; import android.content.pm.PackageManager; import android.content.res.Resources.NotFoundException; +import android.graphics.Bitmap; +import android.graphics.Color; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Parcel; @@ -33,8 +38,14 @@ import java.util.List; import java.util.MissingResourceException; /** - * Describes a distinct account, line of service or call placement method that the system - * can use to place phone calls. + * Represents a distinct method to place or receive a phone call. Apps which can place calls and + * want those calls to be integrated into the dialer and in-call UI should build an instance of + * this class and register it with the system using {@link TelecomManager#registerPhoneAccount}. + * <p> + * {@link TelecomManager} uses registered {@link PhoneAccount}s to present the user with + * alternative options when placing a phone call. When building a {@link PhoneAccount}, the app + * should supply a valid {@link PhoneAccountHandle} that references the {@link ConnectionService} + * implementation Telecom will use to interact with the app. * @hide */ @SystemApi @@ -59,7 +70,7 @@ public class PhoneAccount implements Parcelable { * traditional SIM-based telephony calls. This account will be treated as a distinct method * for placing calls alongside the traditional SIM-based telephony stack. This flag is * distinct from {@link #CAPABILITY_CONNECTION_MANAGER} in that it is not allowed to manage - * calls from or use the built-in telephony stack to place its calls. + * or place calls from the built-in telephony stack. * <p> * See {@link #getCapabilities} * <p> @@ -94,6 +105,15 @@ public class PhoneAccount implements Parcelable { public static final int CAPABILITY_PLACE_EMERGENCY_CALLS = 0x10; /** + * Flag indicating that this {@code PhoneAccount} is capable of being used by all users. This + * should only be used by system apps (and will be ignored for all other apps trying to use it). + * <p> + * See {@link #getCapabilities} + * @hide + */ + public static final int CAPABILITY_MULTI_USER = 0x20; + + /** * URI scheme for telephone number URIs. */ public static final String SCHEME_TEL = "tel"; @@ -108,25 +128,54 @@ public class PhoneAccount implements Parcelable { */ public static final String SCHEME_SIP = "sip"; + /** + * Indicating no icon tint is set. + */ + public static final int NO_ICON_TINT = 0; + + /** + * Indicating no hightlight color is set. + */ + public static final int NO_HIGHLIGHT_COLOR = 0; + + /** + * Indicating no resource ID is set. + */ + public static final int NO_RESOURCE_ID = -1; + private final PhoneAccountHandle mAccountHandle; private final Uri mAddress; private final Uri mSubscriptionAddress; private final int mCapabilities; private final int mIconResId; + private final String mIconPackageName; + private final Bitmap mIconBitmap; + private final int mIconTint; + private final int mHighlightColor; private final CharSequence mLabel; private final CharSequence mShortDescription; private final List<String> mSupportedUriSchemes; + /** + * Helper class for creating a {@link PhoneAccount}. + */ public static class Builder { private PhoneAccountHandle mAccountHandle; private Uri mAddress; private Uri mSubscriptionAddress; private int mCapabilities; private int mIconResId; + private String mIconPackageName; + private Bitmap mIconBitmap; + private int mIconTint = NO_ICON_TINT; + private int mHighlightColor = NO_HIGHLIGHT_COLOR; private CharSequence mLabel; private CharSequence mShortDescription; private List<String> mSupportedUriSchemes = new ArrayList<String>(); + /** + * Creates a builder with the specified {@link PhoneAccountHandle} and label. + */ public Builder(PhoneAccountHandle accountHandle, CharSequence label) { this.mAccountHandle = accountHandle; this.mLabel = label; @@ -144,31 +193,134 @@ public class PhoneAccount implements Parcelable { mSubscriptionAddress = phoneAccount.getSubscriptionAddress(); mCapabilities = phoneAccount.getCapabilities(); mIconResId = phoneAccount.getIconResId(); + mIconPackageName = phoneAccount.getIconPackageName(); + mIconBitmap = phoneAccount.getIconBitmap(); + mIconTint = phoneAccount.getIconTint(); + mHighlightColor = phoneAccount.getHighlightColor(); mLabel = phoneAccount.getLabel(); mShortDescription = phoneAccount.getShortDescription(); mSupportedUriSchemes.addAll(phoneAccount.getSupportedUriSchemes()); } + /** @hide */ + public Builder setAccountHandle(PhoneAccountHandle accountHandle) { + mAccountHandle = accountHandle; + return this; + } + + /** + * Sets the address. See {@link PhoneAccount#getAddress}. + * + * @param value The address of the phone account. + * @return The builder. + */ public Builder setAddress(Uri value) { this.mAddress = value; return this; } + /** + * Sets the subscription address. See {@link PhoneAccount#getSubscriptionAddress}. + * + * @param value The subscription address. + * @return The builder. + */ public Builder setSubscriptionAddress(Uri value) { this.mSubscriptionAddress = value; return this; } + /** + * Sets the capabilities. See {@link PhoneAccount#getCapabilities}. + * + * @param value The capabilities to set. + * @return The builder. + */ public Builder setCapabilities(int value) { this.mCapabilities = value; return this; } - public Builder setIconResId(int value) { - this.mIconResId = value; + /** + * 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. + * @return The builder. + */ + public Builder setIcon(Context packageContext, int iconResId) { + 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_ICON_TINT); + } + + /** + * 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; + } + + /** + * Sets the icon. See {@link PhoneAccount#createIconDrawable}. + * + * @param iconBitmap The icon bitmap. + * @return The builder. + */ + public Builder setIcon(Bitmap iconBitmap) { + this.mIconBitmap = iconBitmap; + this.mIconPackageName = null; + this.mIconResId = NO_RESOURCE_ID; + this.mIconTint = NO_ICON_TINT; + return this; + } + + /** + * Sets the highlight color. See {@link PhoneAccount#getHighlightColor}. + * + * @param value The highlight color. + * @return The builder. + */ + public Builder setHighlightColor(int value) { + this.mHighlightColor = value; return this; } + /** + * Sets the short description. See {@link PhoneAccount#getShortDescription}. + * + * @param value The short description. + * @return The builder. + */ public Builder setShortDescription(CharSequence value) { this.mShortDescription = value; return this; @@ -178,7 +330,7 @@ public class PhoneAccount implements Parcelable { * Specifies an additional URI scheme supported by the {@link PhoneAccount}. * * @param uriScheme The URI scheme. - * @return The Builder. + * @return The builder. * @hide */ public Builder addSupportedUriScheme(String uriScheme) { @@ -192,7 +344,7 @@ public class PhoneAccount implements Parcelable { * Specifies the URI schemes supported by the {@link PhoneAccount}. * * @param uriSchemes The URI schemes. - * @return The Builder. + * @return The builder. */ public Builder setSupportedUriSchemes(List<String> uriSchemes) { mSupportedUriSchemes.clear(); @@ -222,6 +374,10 @@ public class PhoneAccount implements Parcelable { mSubscriptionAddress, mCapabilities, mIconResId, + mIconPackageName, + mIconBitmap, + mIconTint, + mHighlightColor, mLabel, mShortDescription, mSupportedUriSchemes); @@ -234,6 +390,10 @@ public class PhoneAccount implements Parcelable { Uri subscriptionAddress, int capabilities, int iconResId, + String iconPackageName, + Bitmap iconBitmap, + int iconTint, + int highlightColor, CharSequence label, CharSequence shortDescription, List<String> supportedUriSchemes) { @@ -242,6 +402,10 @@ public class PhoneAccount implements Parcelable { mSubscriptionAddress = subscriptionAddress; mCapabilities = capabilities; mIconResId = iconResId; + mIconPackageName = iconPackageName; + mIconBitmap = iconBitmap; + mIconTint = iconTint; + mHighlightColor = highlightColor; mLabel = label; mShortDescription = shortDescription; mSupportedUriSchemes = Collections.unmodifiableList(supportedUriSchemes); @@ -286,6 +450,9 @@ public class PhoneAccount implements Parcelable { * The raw callback number used for this {@code PhoneAccount}, as distinct from * {@link #getAddress()}. For the majority of {@code PhoneAccount}s this should be registered * as {@code null}. It is used by the system for SIM-based {@code PhoneAccount} registration + * where {@link android.telephony.TelephonyManager#setLine1NumberForDisplay(String, String)} + * has been used to alter the callback number. + * <p> * * @return The subscription number, suitable for display to the user. */ @@ -363,6 +530,12 @@ public class PhoneAccount implements Parcelable { /** * The icon resource ID for the icon of this {@code PhoneAccount}. + * <p> + * Creators of a {@code PhoneAccount} who possess the icon in static resources should prefer + * this method of indicating the icon rather than using {@link #getIconBitmap()}, since it + * leads to less resource usage. + * <p> + * Clients wishing to display a {@code PhoneAccount} should use {@link #createIconDrawable(Context)}. * * @return A resource ID. */ @@ -371,30 +544,85 @@ public class PhoneAccount implements Parcelable { } /** - * An icon to represent this {@code PhoneAccount} in a user interface. + * The package name from which to load the icon of this {@code PhoneAccount}. + * <p> + * If this property is {@code null}, the resource {@link #getIconResId()} will be loaded from + * the package in the {@link ComponentName} of the {@link #getAccountHandle()}. + * <p> + * Clients wishing to display a {@code PhoneAccount} should use {@link #createIconDrawable(Context)}. + * + * @return A package name. + */ + public String getIconPackageName() { + return mIconPackageName; + } + + /** + * A tint to apply to the icon of this {@code PhoneAccount}. * - * @return An icon for this {@code PhoneAccount}. + * @return A hexadecimal color value. + */ + public int getIconTint() { + return mIconTint; + } + + /** + * A literal icon bitmap to represent this {@code PhoneAccount} in a user interface. + * <p> + * If this property is specified, it is to be considered the preferred icon. Otherwise, the + * resource specified by {@link #getIconResId()} should be used. + * <p> + * Clients wishing to display a {@code PhoneAccount} should use + * {@link #createIconDrawable(Context)}. + * + * @return A bitmap. */ - public Drawable getIcon(Context context) { - return getIcon(context, mIconResId); + public Bitmap getIconBitmap() { + return mIconBitmap; } - private Drawable getIcon(Context context, int resId) { - Context packageContext; - try { - packageContext = context.createPackageContext( - mAccountHandle.getComponentName().getPackageName(), 0); - } catch (PackageManager.NameNotFoundException e) { - Log.w(this, "Cannot find package %s", mAccountHandle.getComponentName().getPackageName()); - return null; + /** + * 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 + * interface. Uses the properties {@link #getIconResId()}, {@link #getIconPackageName()}, and + * {@link #getIconBitmap()} as necessary. + * + * @param context A {@code Context} to use for loading {@code Drawable}s. + * + * @return An icon for this {@code PhoneAccount}. + */ + public Drawable createIconDrawable(Context context) { + if (mIconBitmap != null) { + return new BitmapDrawable(context.getResources(), mIconBitmap); } - try { - return packageContext.getDrawable(resId); - } catch (NotFoundException|MissingResourceException e) { - Log.e(this, e, "Cannot find icon %d in package %s", - resId, mAccountHandle.getComponentName().getPackageName()); - return null; + + if (mIconResId != 0) { + try { + Context packageContext = context.createPackageContext(mIconPackageName, 0); + try { + Drawable iconDrawable = packageContext.getDrawable(mIconResId); + if (mIconTint != NO_ICON_TINT) { + iconDrawable.setTint(mIconTint); + } + return iconDrawable; + } catch (NotFoundException | MissingResourceException e) { + Log.e(this, e, "Cannot find icon %d in package %s", + mIconResId, mIconPackageName); + } + } catch (PackageManager.NameNotFoundException e) { + Log.w(this, "Cannot find package %s", mIconPackageName); + } } + + return new ColorDrawable(Color.TRANSPARENT); } // @@ -408,14 +636,38 @@ public class PhoneAccount implements Parcelable { @Override public void writeToParcel(Parcel out, int flags) { - out.writeParcelable(mAccountHandle, 0); - out.writeParcelable(mAddress, 0); - out.writeParcelable(mSubscriptionAddress, 0); + if (mAccountHandle == null) { + out.writeInt(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(mIconResId); + out.writeString(mIconPackageName); + if (mIconBitmap == null) { + out.writeInt(0); + } else { + out.writeInt(1); + mIconBitmap.writeToParcel(out, flags); + } + out.writeInt(mIconTint); + out.writeInt(mHighlightColor); out.writeCharSequence(mLabel); out.writeCharSequence(mShortDescription); - out.writeList(mSupportedUriSchemes); + out.writeStringList(mSupportedUriSchemes); } public static final Creator<PhoneAccount> CREATOR @@ -432,18 +684,48 @@ public class PhoneAccount implements Parcelable { }; private PhoneAccount(Parcel in) { - ClassLoader classLoader = PhoneAccount.class.getClassLoader(); - - mAccountHandle = in.readParcelable(getClass().getClassLoader()); - mAddress = in.readParcelable(getClass().getClassLoader()); - mSubscriptionAddress = in.readParcelable(getClass().getClassLoader()); + if (in.readInt() > 0) { + mAccountHandle = PhoneAccountHandle.CREATOR.createFromParcel(in); + } else { + mAccountHandle = null; + } + 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(); mIconResId = in.readInt(); + mIconPackageName = in.readString(); + if (in.readInt() > 0) { + mIconBitmap = Bitmap.CREATOR.createFromParcel(in); + } else { + mIconBitmap = null; + } + mIconTint = in.readInt(); + mHighlightColor = in.readInt(); mLabel = in.readCharSequence(); mShortDescription = in.readCharSequence(); + mSupportedUriSchemes = Collections.unmodifiableList(in.createStringArrayList()); + } - List<String> supportedUriSchemes = new ArrayList<>(); - in.readList(supportedUriSchemes, classLoader); - mSupportedUriSchemes = Collections.unmodifiableList(supportedUriSchemes); + @Override + public String toString() { + StringBuilder sb = new StringBuilder().append("[PhoneAccount: ") + .append(mAccountHandle) + .append(" Capabilities: ") + .append(mCapabilities) + .append(" Schemes: "); + for (String scheme : mSupportedUriSchemes) { + sb.append(scheme) + .append(" "); + } + sb.append("]"); + return sb.toString(); } } |
