Debian Codegraph @

telecomm/java/android/telecom/PhoneAccountHandle.java

raw symbols SCIP
1 /*
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
17 package android.telecom;
18
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.compat.annotation.UnsupportedAppUsage;
22 import android.content.ComponentName;
23 import android.os.Build;
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 import android.os.Process;
27 import android.os.UserHandle;
28
29 import java.util.Objects;
30
31 /**
32 * The unique identifier for a {@link PhoneAccount}. A {@code PhoneAccountHandle} is made of two
33 * parts:
34 * <ul>
35 * <li>The component name of the associated connection service.</li>
36 * <li>A string identifier that is unique across {@code PhoneAccountHandle}s with the same
37 * 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>
41 * </ul>
42 *
43 * 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 *
46 * See {@link PhoneAccount}, {@link TelecomManager}.
47 */
48 public final class PhoneAccountHandle implements Parcelable {
49 /**
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
57 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 127403196)
58 private final ComponentName mComponentName;
59 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
60 private final String mId;
61 private final UserHandle mUserHandle;
62
63 /**
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 */
74 public PhoneAccountHandle(
75 @NonNull ComponentName componentName,
76 @NonNull String id) {
77 this(componentName, id, Process.myUserHandle());
78 }
79
80 /**
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 */
92 public PhoneAccountHandle(
93 @NonNull ComponentName componentName,
94 @NonNull String id,
95 @NonNull UserHandle userHandle) {
96 checkParameters(componentName, userHandle);
97 mComponentName = componentName;
98 mId = id;
99 mUserHandle = userHandle;
100 }
101
102 /**
103 * The {@code ComponentName} of the connection service which is responsible for making phone
104 * calls using this {@code PhoneAccountHandle}.
105 *
106 * @return A suitable {@code ComponentName}.
107 */
108 public ComponentName getComponentName() {
109 return mComponentName;
110 }
111
112 /**
113 * A string that uniquely distinguishes this particular {@code PhoneAccountHandle} from all the
114 * others supported by the connection service that created it.
115 * <p>
116 * A connection service must select identifiers that are stable for the lifetime of
117 * 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>
124 * Important: A non-unique identifier could cause non-deterministic call-log backup/restore
125 * behavior.
126 *
127 * @return A service-specific unique opaque identifier for this {@code PhoneAccountHandle}.
128 */
129 public String getId() {
130 return mId;
131 }
132
133 /**
134 * @return the {@link UserHandle} to use when connecting to this PhoneAccount.
135 */
136 public UserHandle getUserHandle() {
137 return mUserHandle;
138 }
139
140 @Override
141 public int hashCode() {
142 return Objects.hash(mComponentName, mId, mUserHandle);
143 }
144
145 @Override
146 public String toString() {
147 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();
164 }
165
166 @Override
167 public boolean equals(Object other) {
168 return other != null &&
169 other instanceof PhoneAccountHandle &&
170 Objects.equals(((PhoneAccountHandle) other).getComponentName(),
171 getComponentName()) &&
172 Objects.equals(((PhoneAccountHandle) other).getId(), getId()) &&
173 Objects.equals(((PhoneAccountHandle) other).getUserHandle(), getUserHandle());
174 }
175
176 //
177 // Parcelable implementation.
178 //
179
180 @Override
181 public int describeContents() {
182 return 0;
183 }
184
185 @Override
186 public void writeToParcel(Parcel out, int flags) {
187 mComponentName.writeToParcel(out, flags);
188 out.writeString(mId);
189 mUserHandle.writeToParcel(out, flags);
190 }
191
192 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
203 public static final @android.annotation.NonNull Creator<PhoneAccountHandle> CREATOR =
204 new Creator<PhoneAccountHandle>() {
205 @Override
206 public PhoneAccountHandle createFromParcel(Parcel in) {
207 return new PhoneAccountHandle(in);
208 }
209
210 @Override
211 public PhoneAccountHandle[] newArray(int size) {
212 return new PhoneAccountHandle[size];
213 }
214 };
215
216 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
217 private PhoneAccountHandle(Parcel in) {
218 this(ComponentName.CREATOR.createFromParcel(in),
219 in.readString(),
220 UserHandle.CREATOR.createFromParcel(in));
221 }
222
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 }
239 }