2 * Copyright (C) 2014 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package android.telecom;
19 import android.annotation.SystemApi;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.pm.PackageManager;
23 import android.content.res.Resources.NotFoundException;
24 import android.graphics.Bitmap;
25 import android.graphics.Color;
26 import android.graphics.drawable.BitmapDrawable;
27 import android.graphics.drawable.ColorDrawable;
28 import android.graphics.drawable.Drawable;
29 import android.graphics.drawable.Icon;
30 import android.net.Uri;
31 import android.os.Bundle;
32 import android.os.Parcel;
33 import android.os.Parcelable;
34 import android.text.TextUtils;
36 import java.lang.String;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.MissingResourceException;
43 * Represents a distinct method to place or receive a phone call. Apps which can place calls and
44 * want those calls to be integrated into the dialer and in-call UI should build an instance of
45 * this class and register it with the system using {@link TelecomManager}.
47 * {@link TelecomManager} uses registered {@link PhoneAccount}s to present the user with
48 * alternative options when placing a phone call. When building a {@link PhoneAccount}, the app
49 * should supply a valid {@link PhoneAccountHandle} that references the connection service
50 * implementation Telecom will use to interact with the app.
52 public final class PhoneAccount implements Parcelable {
55 * {@link PhoneAccount} extras key (see {@link PhoneAccount#getExtras()}) which determines the
56 * maximum permitted length of a call subject specified via the
57 * {@link TelecomManager#EXTRA_CALL_SUBJECT} extra on an
58 * {@link android.content.Intent#ACTION_CALL} intent. Ultimately a {@link ConnectionService} is
59 * responsible for enforcing the maximum call subject length when sending the message, however
60 * this extra is provided so that the user interface can proactively limit the length of the
61 * call subject as the user types it.
63 public static final String EXTRA_CALL_SUBJECT_MAX_LENGTH =
64 "android.telecom.extra.CALL_SUBJECT_MAX_LENGTH";
67 * {@link PhoneAccount} extras key (see {@link PhoneAccount#getExtras()}) which determines the
68 * character encoding to be used when determining the length of messages.
69 * The user interface can use this when determining the number of characters the user may type
70 * in a call subject. If empty-string, the call subject message size limit will be enforced on
71 * a 1:1 basis. That is, each character will count towards the messages size limit as a single
72 * character. If a character encoding is specified, the message size limit will be based on the
73 * number of bytes in the message per the specified encoding. See
74 * {@link #EXTRA_CALL_SUBJECT_MAX_LENGTH} for more information on the call subject maximum
77 public static final String EXTRA_CALL_SUBJECT_CHARACTER_ENCODING =
78 "android.telecom.extra.CALL_SUBJECT_CHARACTER_ENCODING";
81 * Flag indicating that this {@code PhoneAccount} can act as a connection manager for
82 * other connections. The {@link ConnectionService} associated with this {@code PhoneAccount}
83 * will be allowed to manage phone calls including using its own proprietary phone-call
84 * implementation (like VoIP calling) to make calls instead of the telephony stack.
86 * When a user opts to place a call using the SIM-based telephony stack, the
87 * {@link ConnectionService} associated with this {@code PhoneAccount} will be attempted first
88 * if the user has explicitly selected it to be used as the default connection manager.
90 * See {@link #getCapabilities}
92 public static final int CAPABILITY_CONNECTION_MANAGER = 0x1;
95 * Flag indicating that this {@code PhoneAccount} can make phone calls in place of
96 * traditional SIM-based telephony calls. This account will be treated as a distinct method
97 * for placing calls alongside the traditional SIM-based telephony stack. This flag is
98 * distinct from {@link #CAPABILITY_CONNECTION_MANAGER} in that it is not allowed to manage
99 * or place calls from the built-in telephony stack.
101 * See {@link #getCapabilities}
104 public static final int CAPABILITY_CALL_PROVIDER = 0x2;
107 * Flag indicating that this {@code PhoneAccount} represents a built-in PSTN SIM
110 * Only the Android framework can register a {@code PhoneAccount} having this capability.
112 * See {@link #getCapabilities}
114 public static final int CAPABILITY_SIM_SUBSCRIPTION = 0x4;
117 * Flag indicating that this {@code PhoneAccount} is capable of placing video calls.
119 * See {@link #getCapabilities}
121 public static final int CAPABILITY_VIDEO_CALLING = 0x8;
124 * Flag indicating that this {@code PhoneAccount} is capable of placing emergency calls.
125 * By default all PSTN {@code PhoneAccount}s are capable of placing emergency calls.
127 * See {@link #getCapabilities}
129 public static final int CAPABILITY_PLACE_EMERGENCY_CALLS = 0x10;
132 * Flag indicating that this {@code PhoneAccount} is capable of being used by all users. This
133 * should only be used by system apps (and will be ignored for all other apps trying to use it).
135 * See {@link #getCapabilities}
139 public static final int CAPABILITY_MULTI_USER = 0x20;
142 * Flag indicating that this {@code PhoneAccount} supports a subject for Calls. This means a
143 * caller is able to specify a short subject line for an outgoing call. A capable receiving
144 * device displays the call subject on the incoming call screen.
146 * See {@link #getCapabilities}
148 public static final int CAPABILITY_CALL_SUBJECT = 0x40;
151 * Flag indicating that this {@code PhoneAccount} should only be used for emergency calls.
153 * See {@link #getCapabilities}
156 public static final int CAPABILITY_EMERGENCY_CALLS_ONLY = 0x80;
159 * Flag indicating that for this {@code PhoneAccount}, the ability to make a video call to a
160 * number relies on presence. Should only be set if the {@code PhoneAccount} also has
161 * {@link #CAPABILITY_VIDEO_CALLING}.
163 * When set, the {@link ConnectionService} is responsible for toggling the
164 * {@link android.provider.ContactsContract.Data#CARRIER_PRESENCE_VT_CAPABLE} bit on the
165 * {@link android.provider.ContactsContract.Data#CARRIER_PRESENCE} column to indicate whether
166 * a contact's phone number supports video calling.
168 * See {@link #getCapabilities}
170 public static final int CAPABILITY_VIDEO_CALLING_RELIES_ON_PRESENCE = 0x100;
173 * Flag indicating that for this {@link PhoneAccount}, emergency video calling is allowed.
175 * When set, Telecom will allow emergency video calls to be placed. When not set, Telecom will
176 * convert all outgoing video calls to emergency numbers to audio-only.
179 public static final int CAPABILITY_EMERGENCY_VIDEO_CALLING = 0x200;
182 * URI scheme for telephone number URIs.
184 public static final String SCHEME_TEL = "tel";
187 * URI scheme for voicemail URIs.
189 public static final String SCHEME_VOICEMAIL = "voicemail";
192 * URI scheme for SIP URIs.
194 public static final String SCHEME_SIP = "sip";
197 * Indicating no icon tint is set.
200 public static final int NO_ICON_TINT = 0;
203 * Indicating no hightlight color is set.
205 public static final int NO_HIGHLIGHT_COLOR = 0;
208 * Indicating no resource ID is set.
210 public static final int NO_RESOURCE_ID = -1;
212 private final PhoneAccountHandle mAccountHandle;
213 private final Uri mAddress;
214 private final Uri mSubscriptionAddress;
215 private final int mCapabilities;
216 private final int mHighlightColor;
217 private final CharSequence mLabel;
218 private final CharSequence mShortDescription;
219 private final List<String> mSupportedUriSchemes;
220 private final Icon mIcon;
221 private final Bundle mExtras;
222 private boolean mIsEnabled;
225 * Helper class for creating a {@link PhoneAccount}.
227 public static class Builder {
228 private PhoneAccountHandle mAccountHandle;
229 private Uri mAddress;
230 private Uri mSubscriptionAddress;
231 private int mCapabilities;
232 private int mHighlightColor = NO_HIGHLIGHT_COLOR;
233 private CharSequence mLabel;
234 private CharSequence mShortDescription;
235 private List<String> mSupportedUriSchemes = new ArrayList<String>();
237 private Bundle mExtras;
238 private boolean mIsEnabled = false;
241 * Creates a builder with the specified {@link PhoneAccountHandle} and label.
243 public Builder(PhoneAccountHandle accountHandle, CharSequence label) {
244 this.mAccountHandle = accountHandle;
249 * Creates an instance of the {@link PhoneAccount.Builder} from an existing
250 * {@link PhoneAccount}.
252 * @param phoneAccount The {@link PhoneAccount} used to initialize the builder.
254 public Builder(PhoneAccount phoneAccount) {
255 mAccountHandle = phoneAccount.getAccountHandle();
256 mAddress = phoneAccount.getAddress();
257 mSubscriptionAddress = phoneAccount.getSubscriptionAddress();
258 mCapabilities = phoneAccount.getCapabilities();
259 mHighlightColor = phoneAccount.getHighlightColor();
260 mLabel = phoneAccount.getLabel();
261 mShortDescription = phoneAccount.getShortDescription();
262 mSupportedUriSchemes.addAll(phoneAccount.getSupportedUriSchemes());
263 mIcon = phoneAccount.getIcon();
264 mIsEnabled = phoneAccount.isEnabled();
265 mExtras = phoneAccount.getExtras();
269 * Sets the address. See {@link PhoneAccount#getAddress}.
271 * @param value The address of the phone account.
272 * @return The builder.
274 public Builder setAddress(Uri value) {
275 this.mAddress = value;
280 * Sets the subscription address. See {@link PhoneAccount#getSubscriptionAddress}.
282 * @param value The subscription address.
283 * @return The builder.
285 public Builder setSubscriptionAddress(Uri value) {
286 this.mSubscriptionAddress = value;
291 * Sets the capabilities. See {@link PhoneAccount#getCapabilities}.
293 * @param value The capabilities to set.
294 * @return The builder.
296 public Builder setCapabilities(int value) {
297 this.mCapabilities = value;
302 * Sets the icon. See {@link PhoneAccount#getIcon}.
304 * @param icon The icon to set.
306 public Builder setIcon(Icon icon) {
312 * Sets the highlight color. See {@link PhoneAccount#getHighlightColor}.
314 * @param value The highlight color.
315 * @return The builder.
317 public Builder setHighlightColor(int value) {
318 this.mHighlightColor = value;
323 * Sets the short description. See {@link PhoneAccount#getShortDescription}.
325 * @param value The short description.
326 * @return The builder.
328 public Builder setShortDescription(CharSequence value) {
329 this.mShortDescription = value;
334 * Specifies an additional URI scheme supported by the {@link PhoneAccount}.
336 * @param uriScheme The URI scheme.
337 * @return The builder.
339 public Builder addSupportedUriScheme(String uriScheme) {
340 if (!TextUtils.isEmpty(uriScheme) && !mSupportedUriSchemes.contains(uriScheme)) {
341 this.mSupportedUriSchemes.add(uriScheme);
347 * Specifies the URI schemes supported by the {@link PhoneAccount}.
349 * @param uriSchemes The URI schemes.
350 * @return The builder.
352 public Builder setSupportedUriSchemes(List<String> uriSchemes) {
353 mSupportedUriSchemes.clear();
355 if (uriSchemes != null && !uriSchemes.isEmpty()) {
356 for (String uriScheme : uriSchemes) {
357 addSupportedUriScheme(uriScheme);
364 * Specifies the extras associated with the {@link PhoneAccount}.
366 * {@code PhoneAccount}s only support extra values of type: {@link String}, {@link Integer},
367 * and {@link Boolean}. Extras which are not of these types are ignored.
372 public Builder setExtras(Bundle extras) {
378 * Sets the enabled state of the phone account.
380 * @param isEnabled The enabled state.
381 * @return The builder.
384 public Builder setIsEnabled(boolean isEnabled) {
385 mIsEnabled = isEnabled;
390 * Creates an instance of a {@link PhoneAccount} based on the current builder settings.
392 * @return The {@link PhoneAccount}.
394 public PhoneAccount build() {
395 // If no supported URI schemes were defined, assume "tel" is supported.
396 if (mSupportedUriSchemes.isEmpty()) {
397 addSupportedUriScheme(SCHEME_TEL);
400 return new PhoneAccount(
403 mSubscriptionAddress,
409 mSupportedUriSchemes,
415 private PhoneAccount(
416 PhoneAccountHandle account,
418 Uri subscriptionAddress,
423 CharSequence shortDescription,
424 List<String> supportedUriSchemes,
427 mAccountHandle = account;
429 mSubscriptionAddress = subscriptionAddress;
430 mCapabilities = capabilities;
432 mHighlightColor = highlightColor;
434 mShortDescription = shortDescription;
435 mSupportedUriSchemes = Collections.unmodifiableList(supportedUriSchemes);
437 mIsEnabled = isEnabled;
440 public static Builder builder(
441 PhoneAccountHandle accountHandle,
442 CharSequence label) {
443 return new Builder(accountHandle, label);
447 * Returns a builder initialized with the current {@link PhoneAccount} instance.
449 * @return The builder.
451 public Builder toBuilder() { return new Builder(this); }
454 * The unique identifier of this {@code PhoneAccount}.
456 * @return A {@code PhoneAccountHandle}.
458 public PhoneAccountHandle getAccountHandle() {
459 return mAccountHandle;
463 * The address (e.g., a phone number) associated with this {@code PhoneAccount}. This
464 * represents the destination from which outgoing calls using this {@code PhoneAccount}
465 * will appear to come, if applicable, and the destination to which incoming calls using this
466 * {@code PhoneAccount} may be addressed.
468 * @return A address expressed as a {@code Uri}, for example, a phone number.
470 public Uri getAddress() {
475 * The raw callback number used for this {@code PhoneAccount}, as distinct from
476 * {@link #getAddress()}. For the majority of {@code PhoneAccount}s this should be registered
477 * as {@code null}. It is used by the system for SIM-based {@code PhoneAccount} registration
478 * where {@link android.telephony.TelephonyManager#setLine1NumberForDisplay(String, String)}
479 * has been used to alter the callback number.
482 * @return The subscription number, suitable for display to the user.
484 public Uri getSubscriptionAddress() {
485 return mSubscriptionAddress;
489 * The capabilities of this {@code PhoneAccount}.
491 * @return A bit field of flags describing this {@code PhoneAccount}'s capabilities.
493 public int getCapabilities() {
494 return mCapabilities;
498 * Determines if this {@code PhoneAccount} has a capabilities specified by the passed in
501 * @param capability The capabilities to check.
502 * @return {@code true} if the phone account has the capability.
504 public boolean hasCapabilities(int capability) {
505 return (mCapabilities & capability) == capability;
509 * A short label describing a {@code PhoneAccount}.
511 * @return A label for this {@code PhoneAccount}.
513 public CharSequence getLabel() {
518 * A short paragraph describing this {@code PhoneAccount}.
520 * @return A description for this {@code PhoneAccount}.
522 public CharSequence getShortDescription() {
523 return mShortDescription;
527 * The URI schemes supported by this {@code PhoneAccount}.
529 * @return The URI schemes.
531 public List<String> getSupportedUriSchemes() {
532 return mSupportedUriSchemes;
536 * The extras associated with this {@code PhoneAccount}.
538 * A {@link ConnectionService} may provide implementation specific information about the
539 * {@link PhoneAccount} via the extras.
541 * @return The extras.
543 public Bundle getExtras() {
548 * The icon to represent this {@code PhoneAccount}.
552 public Icon getIcon() {
557 * Indicates whether the user has enabled this {@code PhoneAccount} or not. This value is only
558 * populated for {@code PhoneAccount}s returned by {@link TelecomManager#getPhoneAccount}.
560 * @return {@code true} if the account is enabled by the user, {@code false} otherwise.
562 public boolean isEnabled() {
567 * Determines if the {@link PhoneAccount} supports calls to/from addresses with a specified URI
570 * @param uriScheme The URI scheme to check.
571 * @return {@code true} if the {@code PhoneAccount} supports calls to/from addresses with the
572 * specified URI scheme.
574 public boolean supportsUriScheme(String uriScheme) {
575 if (mSupportedUriSchemes == null || uriScheme == null) {
579 for (String scheme : mSupportedUriSchemes) {
580 if (scheme != null && scheme.equals(uriScheme)) {
588 * A highlight color to use in displaying information about this {@code PhoneAccount}.
590 * @return A hexadecimal color value.
592 public int getHighlightColor() {
593 return mHighlightColor;
597 * Sets the enabled state of the phone account.
600 public void setIsEnabled(boolean isEnabled) {
601 mIsEnabled = isEnabled;
605 // Parcelable implementation
609 public int describeContents() {
614 public void writeToParcel(Parcel out, int flags) {
615 if (mAccountHandle == null) {
619 mAccountHandle.writeToParcel(out, flags);
621 if (mAddress == null) {
625 mAddress.writeToParcel(out, flags);
627 if (mSubscriptionAddress == null) {
631 mSubscriptionAddress.writeToParcel(out, flags);
633 out.writeInt(mCapabilities);
634 out.writeInt(mHighlightColor);
635 out.writeCharSequence(mLabel);
636 out.writeCharSequence(mShortDescription);
637 out.writeStringList(mSupportedUriSchemes);
643 mIcon.writeToParcel(out, flags);
645 out.writeByte((byte) (mIsEnabled ? 1 : 0));
646 out.writeBundle(mExtras);
649 public static final Creator<PhoneAccount> CREATOR
650 = new Creator<PhoneAccount>() {
652 public PhoneAccount createFromParcel(Parcel in) {
653 return new PhoneAccount(in);
657 public PhoneAccount[] newArray(int size) {
658 return new PhoneAccount[size];
662 private PhoneAccount(Parcel in) {
663 if (in.readInt() > 0) {
664 mAccountHandle = PhoneAccountHandle.CREATOR.createFromParcel(in);
666 mAccountHandle = null;
668 if (in.readInt() > 0) {
669 mAddress = Uri.CREATOR.createFromParcel(in);
673 if (in.readInt() > 0) {
674 mSubscriptionAddress = Uri.CREATOR.createFromParcel(in);
676 mSubscriptionAddress = null;
678 mCapabilities = in.readInt();
679 mHighlightColor = in.readInt();
680 mLabel = in.readCharSequence();
681 mShortDescription = in.readCharSequence();
682 mSupportedUriSchemes = Collections.unmodifiableList(in.createStringArrayList());
683 if (in.readInt() > 0) {
684 mIcon = Icon.CREATOR.createFromParcel(in);
688 mIsEnabled = in.readByte() == 1;
689 mExtras = in.readBundle();
693 public String toString() {
694 StringBuilder sb = new StringBuilder().append("[[")
695 .append(mIsEnabled ? 'X' : ' ')
696 .append("] PhoneAccount: ")
697 .append(mAccountHandle)
698 .append(" Capabilities: ")
699 .append(capabilitiesToString(mCapabilities))
700 .append(" Schemes: ");
701 for (String scheme : mSupportedUriSchemes) {
705 sb.append(" Extras: ");
708 return sb.toString();
712 * Generates a string representation of a capabilities bitmask.
714 * @param capabilities The capabilities bitmask.
715 * @return String representation of the capabilities bitmask.
717 private String capabilitiesToString(int capabilities) {
718 StringBuilder sb = new StringBuilder();
719 if (hasCapabilities(CAPABILITY_VIDEO_CALLING)) {
722 if (hasCapabilities(CAPABILITY_VIDEO_CALLING_RELIES_ON_PRESENCE)) {
723 sb.append("Presence ");
725 if (hasCapabilities(CAPABILITY_CALL_PROVIDER)) {
726 sb.append("CallProvider ");
728 if (hasCapabilities(CAPABILITY_CALL_SUBJECT)) {
729 sb.append("CallSubject ");
731 if (hasCapabilities(CAPABILITY_CONNECTION_MANAGER)) {
732 sb.append("ConnectionMgr ");
734 if (hasCapabilities(CAPABILITY_EMERGENCY_CALLS_ONLY)) {
735 sb.append("EmergOnly ");
737 if (hasCapabilities(CAPABILITY_MULTI_USER)) {
738 sb.append("MultiUser ");
740 if (hasCapabilities(CAPABILITY_PLACE_EMERGENCY_CALLS)) {
741 sb.append("PlaceEmerg ");
743 if (hasCapabilities(CAPABILITY_EMERGENCY_VIDEO_CALLING)) {
744 sb.append("EmergVideo ");
746 if (hasCapabilities(CAPABILITY_SIM_SUBSCRIPTION)) {
747 sb.append("SimSub ");
749 return sb.toString();