blob: eb568e04ebf3d06c3d2532ed983c3c0a848121e5 [file] [log] [blame]
Ihab Awad542e0ea2014-05-16 10:22:16 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
Tyler Gunnef9f6f92014-09-12 22:16:17 -070017package android.telecom;
Ihab Awad542e0ea2014-05-16 10:22:16 -070018
Brad Ebinger428cec92016-03-15 14:23:44 -070019import android.annotation.NonNull;
Tyler Gunnc7d10782019-04-11 11:41:15 -070020import android.annotation.Nullable;
Mathew Inwoode5bfa3e2018-08-01 11:26:20 +010021import android.annotation.UnsupportedAppUsage;
Ihab Awadc35ad022014-06-12 16:29:42 -070022import android.content.ComponentName;
Mathew Inwood8c854f82018-09-14 12:35:36 +010023import android.os.Build;
Ihab Awad542e0ea2014-05-16 10:22:16 -070024import android.os.Parcel;
25import android.os.Parcelable;
Evan Charlton134dd682014-11-25 14:12:57 -080026import android.os.Process;
27import android.os.UserHandle;
Ihab Awadc35ad022014-06-12 16:29:42 -070028
Ihab Awaddcaa5d62014-07-08 10:33:46 -070029import java.util.Objects;
Ihab Awad542e0ea2014-05-16 10:22:16 -070030
31/**
Santos Cordond9e614f2014-10-28 13:10:36 -070032 * The unique identifier for a {@link PhoneAccount}. A {@code PhoneAccountHandle} is made of two
33 * parts:
34 * <ul>
Brian Attwellad147f42014-12-19 11:37:16 -080035 * <li>The component name of the associated connection service.</li>
Santos Cordond9e614f2014-10-28 13:10:36 -070036 * <li>A string identifier that is unique across {@code PhoneAccountHandle}s with the same
37 * component name.</li>
38 * </ul>
39 *
Brad Ebinger428cec92016-03-15 14:23:44 -070040 * Note: This Class requires a non-null {@link ComponentName} and {@link UserHandle} to operate
41 * properly. Passing in invalid parameters will generate a log warning.
42 *
Brian Attwellad147f42014-12-19 11:37:16 -080043 * See {@link PhoneAccount}, {@link TelecomManager}.
Ihab Awad542e0ea2014-05-16 10:22:16 -070044 */
Yorke Lee400470f2015-05-12 13:31:25 -070045public final class PhoneAccountHandle implements Parcelable {
Tyler Gunn17933eb2019-03-05 13:58:45 -080046 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 127403196)
Evan Charlton134dd682014-11-25 14:12:57 -080047 private final ComponentName mComponentName;
Mathew Inwood8c854f82018-09-14 12:35:36 +010048 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Evan Charlton134dd682014-11-25 14:12:57 -080049 private final String mId;
50 private final UserHandle mUserHandle;
Ihab Awadc35ad022014-06-12 16:29:42 -070051
Evan Charlton6eb262c2014-07-19 18:18:19 -070052 public PhoneAccountHandle(
Brad Ebinger428cec92016-03-15 14:23:44 -070053 @NonNull ComponentName componentName,
54 @NonNull String id) {
Evan Charlton134dd682014-11-25 14:12:57 -080055 this(componentName, id, Process.myUserHandle());
56 }
57
Evan Charlton134dd682014-11-25 14:12:57 -080058 public PhoneAccountHandle(
Brad Ebinger428cec92016-03-15 14:23:44 -070059 @NonNull ComponentName componentName,
60 @NonNull String id,
61 @NonNull UserHandle userHandle) {
62 checkParameters(componentName, userHandle);
Ihab Awadc35ad022014-06-12 16:29:42 -070063 mComponentName = componentName;
64 mId = id;
Evan Charlton134dd682014-11-25 14:12:57 -080065 mUserHandle = userHandle;
Ihab Awadc35ad022014-06-12 16:29:42 -070066 }
67
68 /**
Brian Attwellad147f42014-12-19 11:37:16 -080069 * The {@code ComponentName} of the connection service which is responsible for making phone
70 * calls using this {@code PhoneAccountHandle}.
Ihab Awadc35ad022014-06-12 16:29:42 -070071 *
72 * @return A suitable {@code ComponentName}.
73 */
74 public ComponentName getComponentName() {
75 return mComponentName;
76 }
77
78 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -070079 * A string that uniquely distinguishes this particular {@code PhoneAccountHandle} from all the
Brian Attwellad147f42014-12-19 11:37:16 -080080 * others supported by the connection service that created it.
Ihab Awadb19a0bc2014-08-07 19:46:01 -070081 * <p>
Brian Attwellad147f42014-12-19 11:37:16 -080082 * A connection service must select identifiers that are stable for the lifetime of
Ihab Awadb19a0bc2014-08-07 19:46:01 -070083 * their users' relationship with their service, across many Android devices. For example, a
84 * good set of identifiers might be the email addresses with which with users registered for
85 * their accounts with a particular service. Depending on how a service chooses to operate,
86 * a bad set of identifiers might be an increasing series of integers
87 * ({@code 0}, {@code 1}, {@code 2}, ...) that are generated locally on each phone and could
88 * collide with values generated on other phones or after a data wipe of a given phone.
Ihab Awadc35ad022014-06-12 16:29:42 -070089 *
Santos Cordon8b338d42015-02-10 03:38:31 -080090 * Important: A non-unique identifier could cause non-deterministic call-log backup/restore
91 * behavior.
92 *
Ihab Awadb19a0bc2014-08-07 19:46:01 -070093 * @return A service-specific unique identifier for this {@code PhoneAccountHandle}.
Ihab Awadc35ad022014-06-12 16:29:42 -070094 */
95 public String getId() {
96 return mId;
97 }
98
Evan Charlton134dd682014-11-25 14:12:57 -080099 /**
100 * @return the {@link UserHandle} to use when connecting to this PhoneAccount.
Evan Charlton134dd682014-11-25 14:12:57 -0800101 */
102 public UserHandle getUserHandle() {
103 return mUserHandle;
104 }
105
Ihab Awad807fe0a2014-07-09 12:30:52 -0700106 @Override
107 public int hashCode() {
Evan Charlton134dd682014-11-25 14:12:57 -0800108 return Objects.hash(mComponentName, mId, mUserHandle);
Ihab Awadc35ad022014-06-12 16:29:42 -0700109 }
110
Santos Cordon98b27032014-07-14 03:32:56 -0700111 @Override
112 public String toString() {
Tyler Gunn76c01a52014-09-30 14:47:51 -0700113 // Note: Log.pii called for mId as it can contain personally identifying phone account
114 // information such as SIP account IDs.
Santos Cordon98b27032014-07-14 03:32:56 -0700115 return new StringBuilder().append(mComponentName)
116 .append(", ")
Tyler Gunn76c01a52014-09-30 14:47:51 -0700117 .append(Log.pii(mId))
Evan Charlton134dd682014-11-25 14:12:57 -0800118 .append(", ")
119 .append(mUserHandle)
Santos Cordon98b27032014-07-14 03:32:56 -0700120 .toString();
121 }
122
Ihab Awad94cf4bf2014-07-17 11:21:19 -0700123 @Override
124 public boolean equals(Object other) {
Santos Cordon98b27032014-07-14 03:32:56 -0700125 return other != null &&
Evan Charlton6eb262c2014-07-19 18:18:19 -0700126 other instanceof PhoneAccountHandle &&
127 Objects.equals(((PhoneAccountHandle) other).getComponentName(),
128 getComponentName()) &&
Evan Charlton134dd682014-11-25 14:12:57 -0800129 Objects.equals(((PhoneAccountHandle) other).getId(), getId()) &&
130 Objects.equals(((PhoneAccountHandle) other).getUserHandle(), getUserHandle());
Santos Cordon98b27032014-07-14 03:32:56 -0700131 }
132
Ihab Awaddcaa5d62014-07-08 10:33:46 -0700133 //
Ihab Awad807fe0a2014-07-09 12:30:52 -0700134 // Parcelable implementation.
135 //
Ihab Awad542e0ea2014-05-16 10:22:16 -0700136
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700137 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700138 public int describeContents() {
139 return 0;
140 }
141
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700142 @Override
Ihab Awadc35ad022014-06-12 16:29:42 -0700143 public void writeToParcel(Parcel out, int flags) {
Evan Charlton134dd682014-11-25 14:12:57 -0800144 mComponentName.writeToParcel(out, flags);
Ihab Awadc35ad022014-06-12 16:29:42 -0700145 out.writeString(mId);
Evan Charlton134dd682014-11-25 14:12:57 -0800146 mUserHandle.writeToParcel(out, flags);
Ihab Awadc35ad022014-06-12 16:29:42 -0700147 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700148
Brad Ebinger428cec92016-03-15 14:23:44 -0700149 private void checkParameters(ComponentName componentName, UserHandle userHandle) {
150 if(componentName == null) {
151 android.util.Log.w("PhoneAccountHandle", new Exception("PhoneAccountHandle has " +
152 "been created with null ComponentName!"));
153 }
154 if(userHandle == null) {
155 android.util.Log.w("PhoneAccountHandle", new Exception("PhoneAccountHandle has " +
156 "been created with null UserHandle!"));
157 }
158 }
159
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700160 public static final @android.annotation.NonNull Creator<PhoneAccountHandle> CREATOR = new Creator<PhoneAccountHandle>() {
Ihab Awad807fe0a2014-07-09 12:30:52 -0700161 @Override
Evan Charlton6eb262c2014-07-19 18:18:19 -0700162 public PhoneAccountHandle createFromParcel(Parcel in) {
163 return new PhoneAccountHandle(in);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700164 }
165
Ihab Awad807fe0a2014-07-09 12:30:52 -0700166 @Override
Evan Charlton6eb262c2014-07-19 18:18:19 -0700167 public PhoneAccountHandle[] newArray(int size) {
168 return new PhoneAccountHandle[size];
Ihab Awad542e0ea2014-05-16 10:22:16 -0700169 }
170 };
171
Mathew Inwood31755f92018-12-20 13:53:36 +0000172 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Evan Charlton6eb262c2014-07-19 18:18:19 -0700173 private PhoneAccountHandle(Parcel in) {
Evan Charlton134dd682014-11-25 14:12:57 -0800174 this(ComponentName.CREATOR.createFromParcel(in),
175 in.readString(),
176 UserHandle.CREATOR.createFromParcel(in));
Ihab Awadc35ad022014-06-12 16:29:42 -0700177 }
Tyler Gunnc7d10782019-04-11 11:41:15 -0700178
179 /**
180 * Determines if two {@link PhoneAccountHandle}s are from the same package.
181 *
182 * @param a Phone account handle to check for same {@link ConnectionService} package.
183 * @param b Other phone account handle to check for same {@link ConnectionService} package.
184 * @return {@code true} if the two {@link PhoneAccountHandle}s passed in belong to the same
185 * {@link ConnectionService} / package, {@code false} otherwise. Note: {@code null} phone
186 * account handles are considered equivalent to other {@code null} phone account handles.
187 * @hide
188 */
189 public static boolean areFromSamePackage(@Nullable PhoneAccountHandle a,
190 @Nullable PhoneAccountHandle b) {
191 String aPackageName = a != null ? a.getComponentName().getPackageName() : null;
192 String bPackageName = b != null ? b.getComponentName().getPackageName() : null;
193 return Objects.equals(aPackageName, bPackageName);
194 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700195}