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.Context;
21 import android.content.pm.PackageManager;
22 import android.content.res.Resources.NotFoundException;
23 import android.graphics.drawable.Drawable;
24 import android.net.Uri;
25 import android.os.Parcel;
26 import android.os.Parcelable;
27 import android.text.TextUtils;
29 import java.lang.String;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.MissingResourceException;
36 * Describes a distinct account, line of service or call placement method that the system
37 * can use to place phone calls.
41 public class PhoneAccount implements Parcelable {
44 * Flag indicating that this {@code PhoneAccount} can act as a connection manager for
45 * other connections. The {@link ConnectionService} associated with this {@code PhoneAccount}
46 * will be allowed to manage phone calls including using its own proprietary phone-call
47 * implementation (like VoIP calling) to make calls instead of the telephony stack.
49 * When a user opts to place a call using the SIM-based telephony stack, the
50 * {@link ConnectionService} associated with this {@code PhoneAccount} will be attempted first
51 * if the user has explicitly selected it to be used as the default connection manager.
53 * See {@link #getCapabilities}
55 public static final int CAPABILITY_CONNECTION_MANAGER = 0x1;
58 * Flag indicating that this {@code PhoneAccount} can make phone calls in place of
59 * traditional SIM-based telephony calls. This account will be treated as a distinct method
60 * for placing calls alongside the traditional SIM-based telephony stack. This flag is
61 * distinct from {@link #CAPABILITY_CONNECTION_MANAGER} in that it is not allowed to manage
62 * calls from or use the built-in telephony stack to place its calls.
64 * See {@link #getCapabilities}
68 public static final int CAPABILITY_CALL_PROVIDER = 0x2;
71 * Flag indicating that this {@code PhoneAccount} represents a built-in PSTN SIM
74 * Only the Android framework can register a {@code PhoneAccount} having this capability.
76 * See {@link #getCapabilities}
78 public static final int CAPABILITY_SIM_SUBSCRIPTION = 0x4;
81 * Flag indicating that this {@code PhoneAccount} is capable of placing video calls.
83 * See {@link #getCapabilities}
86 public static final int CAPABILITY_VIDEO_CALLING = 0x8;
89 * Flag indicating that this {@code PhoneAccount} is capable of placing emergency calls.
90 * By default all PSTN {@code PhoneAccount}s are capable of placing emergency calls.
92 * See {@link #getCapabilities}
94 public static final int CAPABILITY_PLACE_EMERGENCY_CALLS = 0x10;
97 * URI scheme for telephone number URIs.
99 public static final String SCHEME_TEL = "tel";
102 * URI scheme for voicemail URIs.
104 public static final String SCHEME_VOICEMAIL = "voicemail";
107 * URI scheme for SIP URIs.
109 public static final String SCHEME_SIP = "sip";
112 * Indicating no color is set.
114 public static final int NO_COLOR = -1;
116 private final PhoneAccountHandle mAccountHandle;
117 private final Uri mAddress;
118 private final Uri mSubscriptionAddress;
119 private final int mCapabilities;
120 private final int mIconResId;
121 private final int mColor;
122 private final CharSequence mLabel;
123 private final CharSequence mShortDescription;
124 private final List<String> mSupportedUriSchemes;
126 public static class Builder {
127 private PhoneAccountHandle mAccountHandle;
128 private Uri mAddress;
129 private Uri mSubscriptionAddress;
130 private int mCapabilities;
131 private int mIconResId;
132 private int mColor = NO_COLOR;
133 private CharSequence mLabel;
134 private CharSequence mShortDescription;
135 private List<String> mSupportedUriSchemes = new ArrayList<String>();
137 public Builder(PhoneAccountHandle accountHandle, CharSequence label) {
138 this.mAccountHandle = accountHandle;
143 * Creates an instance of the {@link PhoneAccount.Builder} from an existing
144 * {@link PhoneAccount}.
146 * @param phoneAccount The {@link PhoneAccount} used to initialize the builder.
148 public Builder(PhoneAccount phoneAccount) {
149 mAccountHandle = phoneAccount.getAccountHandle();
150 mAddress = phoneAccount.getAddress();
151 mSubscriptionAddress = phoneAccount.getSubscriptionAddress();
152 mCapabilities = phoneAccount.getCapabilities();
153 mIconResId = phoneAccount.getIconResId();
154 mColor = phoneAccount.getColor();
155 mLabel = phoneAccount.getLabel();
156 mShortDescription = phoneAccount.getShortDescription();
157 mSupportedUriSchemes.addAll(phoneAccount.getSupportedUriSchemes());
160 public Builder setAddress(Uri value) {
161 this.mAddress = value;
165 public Builder setSubscriptionAddress(Uri value) {
166 this.mSubscriptionAddress = value;
170 public Builder setCapabilities(int value) {
171 this.mCapabilities = value;
175 public Builder setIconResId(int value) {
176 this.mIconResId = value;
180 public Builder setColor(int value) {
185 public Builder setShortDescription(CharSequence value) {
186 this.mShortDescription = value;
191 * Specifies an additional URI scheme supported by the {@link PhoneAccount}.
193 * @param uriScheme The URI scheme.
194 * @return The Builder.
197 public Builder addSupportedUriScheme(String uriScheme) {
198 if (!TextUtils.isEmpty(uriScheme) && !mSupportedUriSchemes.contains(uriScheme)) {
199 this.mSupportedUriSchemes.add(uriScheme);
205 * Specifies the URI schemes supported by the {@link PhoneAccount}.
207 * @param uriSchemes The URI schemes.
208 * @return The Builder.
210 public Builder setSupportedUriSchemes(List<String> uriSchemes) {
211 mSupportedUriSchemes.clear();
213 if (uriSchemes != null && !uriSchemes.isEmpty()) {
214 for (String uriScheme : uriSchemes) {
215 addSupportedUriScheme(uriScheme);
222 * Creates an instance of a {@link PhoneAccount} based on the current builder settings.
224 * @return The {@link PhoneAccount}.
226 public PhoneAccount build() {
227 // If no supported URI schemes were defined, assume "tel" is supported.
228 if (mSupportedUriSchemes.isEmpty()) {
229 addSupportedUriScheme(SCHEME_TEL);
232 return new PhoneAccount(
235 mSubscriptionAddress,
241 mSupportedUriSchemes);
245 private PhoneAccount(
246 PhoneAccountHandle account,
248 Uri subscriptionAddress,
253 CharSequence shortDescription,
254 List<String> supportedUriSchemes) {
255 mAccountHandle = account;
257 mSubscriptionAddress = subscriptionAddress;
258 mCapabilities = capabilities;
259 mIconResId = iconResId;
262 mShortDescription = shortDescription;
263 mSupportedUriSchemes = Collections.unmodifiableList(supportedUriSchemes);
266 public static Builder builder(
267 PhoneAccountHandle accountHandle,
268 CharSequence label) {
269 return new Builder(accountHandle, label);
273 * Returns a builder initialized with the current {@link PhoneAccount} instance.
275 * @return The builder.
278 public Builder toBuilder() { return new Builder(this); }
281 * The unique identifier of this {@code PhoneAccount}.
283 * @return A {@code PhoneAccountHandle}.
285 public PhoneAccountHandle getAccountHandle() {
286 return mAccountHandle;
290 * The address (e.g., a phone number) associated with this {@code PhoneAccount}. This
291 * represents the destination from which outgoing calls using this {@code PhoneAccount}
292 * will appear to come, if applicable, and the destination to which incoming calls using this
293 * {@code PhoneAccount} may be addressed.
295 * @return A address expressed as a {@code Uri}, for example, a phone number.
297 public Uri getAddress() {
302 * The raw callback number used for this {@code PhoneAccount}, as distinct from
303 * {@link #getAddress()}. For the majority of {@code PhoneAccount}s this should be registered
304 * as {@code null}. It is used by the system for SIM-based {@code PhoneAccount} registration
306 * @return The subscription number, suitable for display to the user.
308 public Uri getSubscriptionAddress() {
309 return mSubscriptionAddress;
313 * The capabilities of this {@code PhoneAccount}.
315 * @return A bit field of flags describing this {@code PhoneAccount}'s capabilities.
317 public int getCapabilities() {
318 return mCapabilities;
322 * Determines if this {@code PhoneAccount} has a capabilities specified by the passed in
325 * @param capability The capabilities to check.
326 * @return {@code True} if the phone account has the capability.
328 public boolean hasCapabilities(int capability) {
329 return (mCapabilities & capability) == capability;
333 * A short label describing a {@code PhoneAccount}.
335 * @return A label for this {@code PhoneAccount}.
337 public CharSequence getLabel() {
342 * A short paragraph describing this {@code PhoneAccount}.
344 * @return A description for this {@code PhoneAccount}.
346 public CharSequence getShortDescription() {
347 return mShortDescription;
351 * The URI schemes supported by this {@code PhoneAccount}.
353 * @return The URI schemes.
355 public List<String> getSupportedUriSchemes() {
356 return mSupportedUriSchemes;
360 * Determines if the {@link PhoneAccount} supports calls to/from addresses with a specified URI
363 * @param uriScheme The URI scheme to check.
364 * @return {@code True} if the {@code PhoneAccount} supports calls to/from addresses with the
365 * specified URI scheme.
367 public boolean supportsUriScheme(String uriScheme) {
368 if (mSupportedUriSchemes == null || uriScheme == null) {
372 for (String scheme : mSupportedUriSchemes) {
373 if (scheme != null && scheme.equals(uriScheme)) {
381 * The icon resource ID for the icon of this {@code PhoneAccount}.
383 * @return A resource ID.
385 public int getIconResId() {
390 * A highlight color to use in displaying information about this {@code PhoneAccount}.
392 * @return A hexadecimal color value.
394 public int getColor() {
399 * An icon to represent this {@code PhoneAccount} in a user interface.
401 * @return An icon for this {@code PhoneAccount}.
403 public Drawable getIcon(Context context) {
404 return getIcon(context, mIconResId);
407 private Drawable getIcon(Context context, int resId) {
408 Context packageContext;
410 packageContext = context.createPackageContext(
411 mAccountHandle.getComponentName().getPackageName(), 0);
412 } catch (PackageManager.NameNotFoundException e) {
413 Log.w(this, "Cannot find package %s", mAccountHandle.getComponentName().getPackageName());
417 return packageContext.getDrawable(resId);
418 } catch (NotFoundException|MissingResourceException e) {
419 Log.e(this, e, "Cannot find icon %d in package %s",
420 resId, mAccountHandle.getComponentName().getPackageName());
426 // Parcelable implementation
430 public int describeContents() {
435 public void writeToParcel(Parcel out, int flags) {
436 out.writeParcelable(mAccountHandle, 0);
437 out.writeParcelable(mAddress, 0);
438 out.writeParcelable(mSubscriptionAddress, 0);
439 out.writeInt(mCapabilities);
440 out.writeInt(mIconResId);
441 out.writeInt(mColor);
442 out.writeCharSequence(mLabel);
443 out.writeCharSequence(mShortDescription);
444 out.writeList(mSupportedUriSchemes);
447 public static final Creator<PhoneAccount> CREATOR
448 = new Creator<PhoneAccount>() {
450 public PhoneAccount createFromParcel(Parcel in) {
451 return new PhoneAccount(in);
455 public PhoneAccount[] newArray(int size) {
456 return new PhoneAccount[size];
460 private PhoneAccount(Parcel in) {
461 ClassLoader classLoader = PhoneAccount.class.getClassLoader();
463 mAccountHandle = in.readParcelable(getClass().getClassLoader());
464 mAddress = in.readParcelable(getClass().getClassLoader());
465 mSubscriptionAddress = in.readParcelable(getClass().getClassLoader());
466 mCapabilities = in.readInt();
467 mIconResId = in.readInt();
468 mColor = in.readInt();
469 mLabel = in.readCharSequence();
470 mShortDescription = in.readCharSequence();
472 List<String> supportedUriSchemes = new ArrayList<>();
473 in.readList(supportedUriSchemes, classLoader);
474 mSupportedUriSchemes = Collections.unmodifiableList(supportedUriSchemes);