blob: ec94f8a1829fd09ff14267c94ec2fbb86c39693d [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;
Artur Satayev53ada2a2019-12-10 17:47:56 +000021import android.compat.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
Tyler Gunnc9bdcb42021-07-02 11:28:23 -070037 * component name. Apps registering {@link PhoneAccountHandle}s should ensure that the
38 * {@link #getId()} provided does not expose personally identifying information. A
39 * {@link ConnectionService} should use an opaque token as the {@link PhoneAccountHandle}
40 * identifier.</li>
Santos Cordond9e614f2014-10-28 13:10:36 -070041 * </ul>
42 *
Brad Ebinger428cec92016-03-15 14:23:44 -070043 * Note: This Class requires a non-null {@link ComponentName} and {@link UserHandle} to operate
44 * properly. Passing in invalid parameters will generate a log warning.
45 *
Brian Attwellad147f42014-12-19 11:37:16 -080046 * See {@link PhoneAccount}, {@link TelecomManager}.
Ihab Awad542e0ea2014-05-16 10:22:16 -070047 */
Yorke Lee400470f2015-05-12 13:31:25 -070048public final class PhoneAccountHandle implements Parcelable {
Tyler Gunn8a979332022-05-06 12:31:17 -070049 /**
50 * Expected component name of Telephony phone accounts; ONLY used to determine if we should log
51 * the phone account handle ID.
52 */
53 private static final ComponentName TELEPHONY_COMPONENT_NAME =
54 new ComponentName("com.android.phone",
55 "com.android.services.telephony.TelephonyConnectionService");
56
Tyler Gunn17933eb2019-03-05 13:58:45 -080057 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 127403196)
Evan Charlton134dd682014-11-25 14:12:57 -080058 private final ComponentName mComponentName;
Mathew Inwood8c854f82018-09-14 12:35:36 +010059 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Evan Charlton134dd682014-11-25 14:12:57 -080060 private final String mId;
61 private final UserHandle mUserHandle;
Ihab Awadc35ad022014-06-12 16:29:42 -070062
Tyler Gunnc9bdcb42021-07-02 11:28:23 -070063 /**
64 * Creates a new {@link PhoneAccountHandle}.
65 *
66 * @param componentName The {@link ComponentName} of the {@link ConnectionService} which
67 * services this {@link PhoneAccountHandle}.
68 * @param id A string identifier that is unique across {@code PhoneAccountHandle}s with the same
69 * component name. Apps registering {@link PhoneAccountHandle}s should ensure that the
70 * ID provided does not expose personally identifying information. A
71 * {@link ConnectionService} should use an opaque token as the
72 * {@link PhoneAccountHandle} identifier.
73 */
Evan Charlton6eb262c2014-07-19 18:18:19 -070074 public PhoneAccountHandle(
Brad Ebinger428cec92016-03-15 14:23:44 -070075 @NonNull ComponentName componentName,
76 @NonNull String id) {
Evan Charlton134dd682014-11-25 14:12:57 -080077 this(componentName, id, Process.myUserHandle());
78 }
79
Tyler Gunnc9bdcb42021-07-02 11:28:23 -070080 /**
81 * Creates a new {@link PhoneAccountHandle}.
82 *
83 * @param componentName The {@link ComponentName} of the {@link ConnectionService} which
84 * services this {@link PhoneAccountHandle}.
85 * @param id A string identifier that is unique across {@code PhoneAccountHandle}s with the same
86 * component name. Apps registering {@link PhoneAccountHandle}s should ensure that the
87 * ID provided does not expose personally identifying information. A
88 * {@link ConnectionService} should use an opaque token as the
89 * {@link PhoneAccountHandle} identifier.
90 * @param userHandle The {@link UserHandle} associated with this {@link PhoneAccountHandle}.
91 */
Evan Charlton134dd682014-11-25 14:12:57 -080092 public PhoneAccountHandle(
Brad Ebinger428cec92016-03-15 14:23:44 -070093 @NonNull ComponentName componentName,
94 @NonNull String id,
95 @NonNull UserHandle userHandle) {
96 checkParameters(componentName, userHandle);
Ihab Awadc35ad022014-06-12 16:29:42 -070097 mComponentName = componentName;
98 mId = id;
Evan Charlton134dd682014-11-25 14:12:57 -080099 mUserHandle = userHandle;
Ihab Awadc35ad022014-06-12 16:29:42 -0700100 }
101
102 /**
Brian Attwellad147f42014-12-19 11:37:16 -0800103 * The {@code ComponentName} of the connection service which is responsible for making phone
104 * calls using this {@code PhoneAccountHandle}.
Ihab Awadc35ad022014-06-12 16:29:42 -0700105 *
106 * @return A suitable {@code ComponentName}.
107 */
108 public ComponentName getComponentName() {
109 return mComponentName;
110 }
111
112 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700113 * A string that uniquely distinguishes this particular {@code PhoneAccountHandle} from all the
Brian Attwellad147f42014-12-19 11:37:16 -0800114 * others supported by the connection service that created it.
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700115 * <p>
Brian Attwellad147f42014-12-19 11:37:16 -0800116 * A connection service must select identifiers that are stable for the lifetime of
Tyler Gunnc9bdcb42021-07-02 11:28:23 -0700117 * their users' relationship with their service, across many Android devices. The identifier
118 * should be a stable opaque token which uniquely identifies the user within the service.
119 * Depending on how a service chooses to operate, a bad set of identifiers might be an
120 * increasing series of integers ({@code 0}, {@code 1}, {@code 2}, ...) that are generated
121 * locally on each phone and could collide with values generated on other phones or after a data
122 * wipe of a given phone.
123 * <p>
Santos Cordon8b338d42015-02-10 03:38:31 -0800124 * Important: A non-unique identifier could cause non-deterministic call-log backup/restore
125 * behavior.
126 *
Tyler Gunnc9bdcb42021-07-02 11:28:23 -0700127 * @return A service-specific unique opaque identifier for this {@code PhoneAccountHandle}.
Ihab Awadc35ad022014-06-12 16:29:42 -0700128 */
129 public String getId() {
130 return mId;
131 }
132
Evan Charlton134dd682014-11-25 14:12:57 -0800133 /**
134 * @return the {@link UserHandle} to use when connecting to this PhoneAccount.
Evan Charlton134dd682014-11-25 14:12:57 -0800135 */
136 public UserHandle getUserHandle() {
137 return mUserHandle;
138 }
139
Ihab Awad807fe0a2014-07-09 12:30:52 -0700140 @Override
141 public int hashCode() {
Evan Charlton134dd682014-11-25 14:12:57 -0800142 return Objects.hash(mComponentName, mId, mUserHandle);
Ihab Awadc35ad022014-06-12 16:29:42 -0700143 }
144
Santos Cordon98b27032014-07-14 03:32:56 -0700145 @Override
146 public String toString() {
Tyler Gunn8a979332022-05-06 12:31:17 -0700147 StringBuilder sb = new StringBuilder()
148 .append(mComponentName)
149 .append(", ");
150
151 if (TELEPHONY_COMPONENT_NAME.equals(mComponentName)) {
152 // Telephony phone account handles are now keyed by subscription id which is not
153 // sensitive.
154 sb.append(mId);
155 } else {
156 // Note: Log.pii called for mId as it can contain personally identifying phone account
157 // information such as SIP account IDs.
158 sb.append(Log.pii(mId));
159 }
160 sb.append(", ");
161 sb.append(mUserHandle);
162
163 return sb.toString();
Santos Cordon98b27032014-07-14 03:32:56 -0700164 }
165
Ihab Awad94cf4bf2014-07-17 11:21:19 -0700166 @Override
167 public boolean equals(Object other) {
Santos Cordon98b27032014-07-14 03:32:56 -0700168 return other != null &&
Evan Charlton6eb262c2014-07-19 18:18:19 -0700169 other instanceof PhoneAccountHandle &&
170 Objects.equals(((PhoneAccountHandle) other).getComponentName(),
171 getComponentName()) &&
Evan Charlton134dd682014-11-25 14:12:57 -0800172 Objects.equals(((PhoneAccountHandle) other).getId(), getId()) &&
173 Objects.equals(((PhoneAccountHandle) other).getUserHandle(), getUserHandle());
Santos Cordon98b27032014-07-14 03:32:56 -0700174 }
175
Ihab Awaddcaa5d62014-07-08 10:33:46 -0700176 //
Ihab Awad807fe0a2014-07-09 12:30:52 -0700177 // Parcelable implementation.
178 //
Ihab Awad542e0ea2014-05-16 10:22:16 -0700179
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700180 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700181 public int describeContents() {
182 return 0;
183 }
184
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700185 @Override
Ihab Awadc35ad022014-06-12 16:29:42 -0700186 public void writeToParcel(Parcel out, int flags) {
Evan Charlton134dd682014-11-25 14:12:57 -0800187 mComponentName.writeToParcel(out, flags);
Ihab Awadc35ad022014-06-12 16:29:42 -0700188 out.writeString(mId);
Evan Charlton134dd682014-11-25 14:12:57 -0800189 mUserHandle.writeToParcel(out, flags);
Ihab Awadc35ad022014-06-12 16:29:42 -0700190 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700191
Brad Ebinger428cec92016-03-15 14:23:44 -0700192 private void checkParameters(ComponentName componentName, UserHandle userHandle) {
193 if(componentName == null) {
194 android.util.Log.w("PhoneAccountHandle", new Exception("PhoneAccountHandle has " +
195 "been created with null ComponentName!"));
196 }
197 if(userHandle == null) {
198 android.util.Log.w("PhoneAccountHandle", new Exception("PhoneAccountHandle has " +
199 "been created with null UserHandle!"));
200 }
201 }
202
Tyler Gunnc9bdcb42021-07-02 11:28:23 -0700203 public static final @android.annotation.NonNull Creator<PhoneAccountHandle> CREATOR =
204 new Creator<PhoneAccountHandle>() {
Ihab Awad807fe0a2014-07-09 12:30:52 -0700205 @Override
Evan Charlton6eb262c2014-07-19 18:18:19 -0700206 public PhoneAccountHandle createFromParcel(Parcel in) {
207 return new PhoneAccountHandle(in);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700208 }
209
Ihab Awad807fe0a2014-07-09 12:30:52 -0700210 @Override
Evan Charlton6eb262c2014-07-19 18:18:19 -0700211 public PhoneAccountHandle[] newArray(int size) {
212 return new PhoneAccountHandle[size];
Ihab Awad542e0ea2014-05-16 10:22:16 -0700213 }
214 };
215
Mathew Inwood31755f92018-12-20 13:53:36 +0000216 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Evan Charlton6eb262c2014-07-19 18:18:19 -0700217 private PhoneAccountHandle(Parcel in) {
Evan Charlton134dd682014-11-25 14:12:57 -0800218 this(ComponentName.CREATOR.createFromParcel(in),
219 in.readString(),
220 UserHandle.CREATOR.createFromParcel(in));
Ihab Awadc35ad022014-06-12 16:29:42 -0700221 }
Tyler Gunnc7d10782019-04-11 11:41:15 -0700222
223 /**
224 * Determines if two {@link PhoneAccountHandle}s are from the same package.
225 *
226 * @param a Phone account handle to check for same {@link ConnectionService} package.
227 * @param b Other phone account handle to check for same {@link ConnectionService} package.
228 * @return {@code true} if the two {@link PhoneAccountHandle}s passed in belong to the same
229 * {@link ConnectionService} / package, {@code false} otherwise. Note: {@code null} phone
230 * account handles are considered equivalent to other {@code null} phone account handles.
231 * @hide
232 */
233 public static boolean areFromSamePackage(@Nullable PhoneAccountHandle a,
234 @Nullable PhoneAccountHandle b) {
235 String aPackageName = a != null ? a.getComponentName().getPackageName() : null;
236 String bPackageName = b != null ? b.getComponentName().getPackageName() : null;
237 return Objects.equals(aPackageName, bPackageName);
238 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700239}