| Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 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 |  | 
| Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 17 | package android.telecom; | 
| Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 18 |  | 
| Brad Ebinger | 428cec9 | 2016-03-15 14:23:44 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; | 
| Tyler Gunn | c7d1078 | 2019-04-11 11:41:15 -0700 | [diff] [blame] | 20 | import android.annotation.Nullable; | 
| Artur Satayev | 53ada2a | 2019-12-10 17:47:56 +0000 | [diff] [blame] | 21 | import android.compat.annotation.UnsupportedAppUsage; | 
| Ihab Awad | c35ad02 | 2014-06-12 16:29:42 -0700 | [diff] [blame] | 22 | import android.content.ComponentName; | 
| Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 23 | import android.os.Build; | 
| Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 24 | import android.os.Parcel; | 
|  | 25 | import android.os.Parcelable; | 
| Evan Charlton | 134dd68 | 2014-11-25 14:12:57 -0800 | [diff] [blame] | 26 | import android.os.Process; | 
|  | 27 | import android.os.UserHandle; | 
| Ihab Awad | c35ad02 | 2014-06-12 16:29:42 -0700 | [diff] [blame] | 28 |  | 
| Ihab Awad | dcaa5d6 | 2014-07-08 10:33:46 -0700 | [diff] [blame] | 29 | import java.util.Objects; | 
| Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 30 |  | 
|  | 31 | /** | 
| Santos Cordon | d9e614f | 2014-10-28 13:10:36 -0700 | [diff] [blame] | 32 | * The unique identifier for a {@link PhoneAccount}. A {@code PhoneAccountHandle} is made of two | 
|  | 33 | * parts: | 
|  | 34 | * <ul> | 
| Brian Attwell | ad147f4 | 2014-12-19 11:37:16 -0800 | [diff] [blame] | 35 | *  <li>The component name of the associated connection service.</li> | 
| Santos Cordon | d9e614f | 2014-10-28 13:10:36 -0700 | [diff] [blame] | 36 | *  <li>A string identifier that is unique across {@code PhoneAccountHandle}s with the same | 
| Tyler Gunn | c9bdcb4 | 2021-07-02 11:28:23 -0700 | [diff] [blame] | 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> | 
| Santos Cordon | d9e614f | 2014-10-28 13:10:36 -0700 | [diff] [blame] | 41 | * </ul> | 
|  | 42 | * | 
| Brad Ebinger | 428cec9 | 2016-03-15 14:23:44 -0700 | [diff] [blame] | 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 | * | 
| Brian Attwell | ad147f4 | 2014-12-19 11:37:16 -0800 | [diff] [blame] | 46 | * See {@link PhoneAccount}, {@link TelecomManager}. | 
| Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 47 | */ | 
| Yorke Lee | 400470f | 2015-05-12 13:31:25 -0700 | [diff] [blame] | 48 | public final class PhoneAccountHandle implements Parcelable { | 
| Tyler Gunn | 8a97933 | 2022-05-06 12:31:17 -0700 | [diff] [blame] | 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 |  | 
| Tyler Gunn | 17933eb | 2019-03-05 13:58:45 -0800 | [diff] [blame] | 57 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 127403196) | 
| Evan Charlton | 134dd68 | 2014-11-25 14:12:57 -0800 | [diff] [blame] | 58 | private final ComponentName mComponentName; | 
| Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 59 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) | 
| Evan Charlton | 134dd68 | 2014-11-25 14:12:57 -0800 | [diff] [blame] | 60 | private final String mId; | 
|  | 61 | private final UserHandle mUserHandle; | 
| Ihab Awad | c35ad02 | 2014-06-12 16:29:42 -0700 | [diff] [blame] | 62 |  | 
| Tyler Gunn | c9bdcb4 | 2021-07-02 11:28:23 -0700 | [diff] [blame] | 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. | 
| Thomas Stuart | e2b0dc5 | 2022-12-28 11:10:50 -0800 | [diff] [blame] | 73 | * <p> | 
|  | 74 | * Note: Each String field is limited to 256 characters. This check is enforced when | 
|  | 75 | *           registering the PhoneAccount via | 
|  | 76 | *           {@link TelecomManager#registerPhoneAccount(PhoneAccount)} and will cause an | 
|  | 77 | *           {@link IllegalArgumentException} to be thrown if the character field limit is | 
|  | 78 | *           over 256. | 
| Tyler Gunn | c9bdcb4 | 2021-07-02 11:28:23 -0700 | [diff] [blame] | 79 | */ | 
| Evan Charlton | 6eb262c | 2014-07-19 18:18:19 -0700 | [diff] [blame] | 80 | public PhoneAccountHandle( | 
| Brad Ebinger | 428cec9 | 2016-03-15 14:23:44 -0700 | [diff] [blame] | 81 | @NonNull ComponentName componentName, | 
|  | 82 | @NonNull String id) { | 
| Evan Charlton | 134dd68 | 2014-11-25 14:12:57 -0800 | [diff] [blame] | 83 | this(componentName, id, Process.myUserHandle()); | 
|  | 84 | } | 
|  | 85 |  | 
| Tyler Gunn | c9bdcb4 | 2021-07-02 11:28:23 -0700 | [diff] [blame] | 86 | /** | 
|  | 87 | * Creates a new {@link PhoneAccountHandle}. | 
|  | 88 | * | 
|  | 89 | * @param componentName The {@link ComponentName} of the {@link ConnectionService} which | 
|  | 90 | *                      services this {@link PhoneAccountHandle}. | 
|  | 91 | * @param id A string identifier that is unique across {@code PhoneAccountHandle}s with the same | 
|  | 92 | *           component name. Apps registering {@link PhoneAccountHandle}s should ensure that the | 
|  | 93 | *           ID provided does not expose personally identifying information.  A | 
|  | 94 | *           {@link ConnectionService} should use an opaque token as the | 
|  | 95 | *           {@link PhoneAccountHandle} identifier. | 
|  | 96 | * @param userHandle The {@link UserHandle} associated with this {@link PhoneAccountHandle}. | 
| Thomas Stuart | e2b0dc5 | 2022-12-28 11:10:50 -0800 | [diff] [blame] | 97 | * | 
|  | 98 | * <p> | 
|  | 99 | * Note: Each String field is limited to 256 characters. This check is enforced when | 
|  | 100 | *           registering the PhoneAccount via | 
|  | 101 | *           {@link TelecomManager#registerPhoneAccount(PhoneAccount)} and will cause an | 
|  | 102 | *           {@link IllegalArgumentException} to be thrown if the character field limit is | 
|  | 103 | *           over 256. | 
| Tyler Gunn | c9bdcb4 | 2021-07-02 11:28:23 -0700 | [diff] [blame] | 104 | */ | 
| Evan Charlton | 134dd68 | 2014-11-25 14:12:57 -0800 | [diff] [blame] | 105 | public PhoneAccountHandle( | 
| Brad Ebinger | 428cec9 | 2016-03-15 14:23:44 -0700 | [diff] [blame] | 106 | @NonNull ComponentName componentName, | 
|  | 107 | @NonNull String id, | 
|  | 108 | @NonNull UserHandle userHandle) { | 
|  | 109 | checkParameters(componentName, userHandle); | 
| Ihab Awad | c35ad02 | 2014-06-12 16:29:42 -0700 | [diff] [blame] | 110 | mComponentName = componentName; | 
|  | 111 | mId = id; | 
| Evan Charlton | 134dd68 | 2014-11-25 14:12:57 -0800 | [diff] [blame] | 112 | mUserHandle = userHandle; | 
| Ihab Awad | c35ad02 | 2014-06-12 16:29:42 -0700 | [diff] [blame] | 113 | } | 
|  | 114 |  | 
|  | 115 | /** | 
| Brian Attwell | ad147f4 | 2014-12-19 11:37:16 -0800 | [diff] [blame] | 116 | * The {@code ComponentName} of the connection service which is responsible for making phone | 
|  | 117 | * calls using this {@code PhoneAccountHandle}. | 
| Ihab Awad | c35ad02 | 2014-06-12 16:29:42 -0700 | [diff] [blame] | 118 | * | 
|  | 119 | * @return A suitable {@code ComponentName}. | 
|  | 120 | */ | 
|  | 121 | public ComponentName getComponentName() { | 
|  | 122 | return mComponentName; | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | /** | 
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 126 | * A string that uniquely distinguishes this particular {@code PhoneAccountHandle} from all the | 
| Brian Attwell | ad147f4 | 2014-12-19 11:37:16 -0800 | [diff] [blame] | 127 | * others supported by the connection service that created it. | 
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 128 | * <p> | 
| Brian Attwell | ad147f4 | 2014-12-19 11:37:16 -0800 | [diff] [blame] | 129 | * A connection service must select identifiers that are stable for the lifetime of | 
| Tyler Gunn | c9bdcb4 | 2021-07-02 11:28:23 -0700 | [diff] [blame] | 130 | * their users' relationship with their service, across many Android devices.  The identifier | 
|  | 131 | * should be a stable opaque token which uniquely identifies the user within the service. | 
|  | 132 | * Depending on how a service chooses to operate, a bad set of identifiers might be an | 
|  | 133 | * increasing series of integers ({@code 0}, {@code 1}, {@code 2}, ...) that are generated | 
|  | 134 | * locally on each phone and could collide with values generated on other phones or after a data | 
|  | 135 | * wipe of a given phone. | 
|  | 136 | * <p> | 
| Santos Cordon | 8b338d4 | 2015-02-10 03:38:31 -0800 | [diff] [blame] | 137 | * Important: A non-unique identifier could cause non-deterministic call-log backup/restore | 
|  | 138 | * behavior. | 
|  | 139 | * | 
| Tyler Gunn | c9bdcb4 | 2021-07-02 11:28:23 -0700 | [diff] [blame] | 140 | * @return A service-specific unique opaque identifier for this {@code PhoneAccountHandle}. | 
| Ihab Awad | c35ad02 | 2014-06-12 16:29:42 -0700 | [diff] [blame] | 141 | */ | 
|  | 142 | public String getId() { | 
|  | 143 | return mId; | 
|  | 144 | } | 
|  | 145 |  | 
| Evan Charlton | 134dd68 | 2014-11-25 14:12:57 -0800 | [diff] [blame] | 146 | /** | 
|  | 147 | * @return the {@link UserHandle} to use when connecting to this PhoneAccount. | 
| Evan Charlton | 134dd68 | 2014-11-25 14:12:57 -0800 | [diff] [blame] | 148 | */ | 
|  | 149 | public UserHandle getUserHandle() { | 
|  | 150 | return mUserHandle; | 
|  | 151 | } | 
|  | 152 |  | 
| Ihab Awad | 807fe0a | 2014-07-09 12:30:52 -0700 | [diff] [blame] | 153 | @Override | 
|  | 154 | public int hashCode() { | 
| Evan Charlton | 134dd68 | 2014-11-25 14:12:57 -0800 | [diff] [blame] | 155 | return Objects.hash(mComponentName, mId, mUserHandle); | 
| Ihab Awad | c35ad02 | 2014-06-12 16:29:42 -0700 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
| Santos Cordon | 98b2703 | 2014-07-14 03:32:56 -0700 | [diff] [blame] | 158 | @Override | 
|  | 159 | public String toString() { | 
| Tyler Gunn | 8a97933 | 2022-05-06 12:31:17 -0700 | [diff] [blame] | 160 | StringBuilder sb = new StringBuilder() | 
|  | 161 | .append(mComponentName) | 
|  | 162 | .append(", "); | 
|  | 163 |  | 
|  | 164 | if (TELEPHONY_COMPONENT_NAME.equals(mComponentName)) { | 
|  | 165 | // Telephony phone account handles are now keyed by subscription id which is not | 
|  | 166 | // sensitive. | 
|  | 167 | sb.append(mId); | 
|  | 168 | } else { | 
|  | 169 | // Note: Log.pii called for mId as it can contain personally identifying phone account | 
|  | 170 | // information such as SIP account IDs. | 
|  | 171 | sb.append(Log.pii(mId)); | 
|  | 172 | } | 
|  | 173 | sb.append(", "); | 
|  | 174 | sb.append(mUserHandle); | 
|  | 175 |  | 
|  | 176 | return sb.toString(); | 
| Santos Cordon | 98b2703 | 2014-07-14 03:32:56 -0700 | [diff] [blame] | 177 | } | 
|  | 178 |  | 
| Ihab Awad | 94cf4bf | 2014-07-17 11:21:19 -0700 | [diff] [blame] | 179 | @Override | 
|  | 180 | public boolean equals(Object other) { | 
| Santos Cordon | 98b2703 | 2014-07-14 03:32:56 -0700 | [diff] [blame] | 181 | return other != null && | 
| Evan Charlton | 6eb262c | 2014-07-19 18:18:19 -0700 | [diff] [blame] | 182 | other instanceof PhoneAccountHandle && | 
|  | 183 | Objects.equals(((PhoneAccountHandle) other).getComponentName(), | 
|  | 184 | getComponentName()) && | 
| Evan Charlton | 134dd68 | 2014-11-25 14:12:57 -0800 | [diff] [blame] | 185 | Objects.equals(((PhoneAccountHandle) other).getId(), getId()) && | 
|  | 186 | Objects.equals(((PhoneAccountHandle) other).getUserHandle(), getUserHandle()); | 
| Santos Cordon | 98b2703 | 2014-07-14 03:32:56 -0700 | [diff] [blame] | 187 | } | 
|  | 188 |  | 
| Ihab Awad | dcaa5d6 | 2014-07-08 10:33:46 -0700 | [diff] [blame] | 189 | // | 
| Ihab Awad | 807fe0a | 2014-07-09 12:30:52 -0700 | [diff] [blame] | 190 | // Parcelable implementation. | 
|  | 191 | // | 
| Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 192 |  | 
| Sailesh Nepal | e7ef59a | 2014-07-08 21:48:22 -0700 | [diff] [blame] | 193 | @Override | 
| Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 194 | public int describeContents() { | 
|  | 195 | return 0; | 
|  | 196 | } | 
|  | 197 |  | 
| Sailesh Nepal | e7ef59a | 2014-07-08 21:48:22 -0700 | [diff] [blame] | 198 | @Override | 
| Ihab Awad | c35ad02 | 2014-06-12 16:29:42 -0700 | [diff] [blame] | 199 | public void writeToParcel(Parcel out, int flags) { | 
| Evan Charlton | 134dd68 | 2014-11-25 14:12:57 -0800 | [diff] [blame] | 200 | mComponentName.writeToParcel(out, flags); | 
| Ihab Awad | c35ad02 | 2014-06-12 16:29:42 -0700 | [diff] [blame] | 201 | out.writeString(mId); | 
| Evan Charlton | 134dd68 | 2014-11-25 14:12:57 -0800 | [diff] [blame] | 202 | mUserHandle.writeToParcel(out, flags); | 
| Ihab Awad | c35ad02 | 2014-06-12 16:29:42 -0700 | [diff] [blame] | 203 | } | 
| Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 204 |  | 
| Brad Ebinger | 428cec9 | 2016-03-15 14:23:44 -0700 | [diff] [blame] | 205 | private void checkParameters(ComponentName componentName, UserHandle userHandle) { | 
|  | 206 | if(componentName == null) { | 
|  | 207 | android.util.Log.w("PhoneAccountHandle", new Exception("PhoneAccountHandle has " + | 
|  | 208 | "been created with null ComponentName!")); | 
|  | 209 | } | 
|  | 210 | if(userHandle == null) { | 
|  | 211 | android.util.Log.w("PhoneAccountHandle", new Exception("PhoneAccountHandle has " + | 
|  | 212 | "been created with null UserHandle!")); | 
|  | 213 | } | 
|  | 214 | } | 
|  | 215 |  | 
| Tyler Gunn | c9bdcb4 | 2021-07-02 11:28:23 -0700 | [diff] [blame] | 216 | public static final @android.annotation.NonNull Creator<PhoneAccountHandle> CREATOR = | 
|  | 217 | new Creator<PhoneAccountHandle>() { | 
| Ihab Awad | 807fe0a | 2014-07-09 12:30:52 -0700 | [diff] [blame] | 218 | @Override | 
| Evan Charlton | 6eb262c | 2014-07-19 18:18:19 -0700 | [diff] [blame] | 219 | public PhoneAccountHandle createFromParcel(Parcel in) { | 
|  | 220 | return new PhoneAccountHandle(in); | 
| Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 221 | } | 
|  | 222 |  | 
| Ihab Awad | 807fe0a | 2014-07-09 12:30:52 -0700 | [diff] [blame] | 223 | @Override | 
| Evan Charlton | 6eb262c | 2014-07-19 18:18:19 -0700 | [diff] [blame] | 224 | public PhoneAccountHandle[] newArray(int size) { | 
|  | 225 | return new PhoneAccountHandle[size]; | 
| Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 226 | } | 
|  | 227 | }; | 
|  | 228 |  | 
| Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 229 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) | 
| Evan Charlton | 6eb262c | 2014-07-19 18:18:19 -0700 | [diff] [blame] | 230 | private PhoneAccountHandle(Parcel in) { | 
| Evan Charlton | 134dd68 | 2014-11-25 14:12:57 -0800 | [diff] [blame] | 231 | this(ComponentName.CREATOR.createFromParcel(in), | 
|  | 232 | in.readString(), | 
|  | 233 | UserHandle.CREATOR.createFromParcel(in)); | 
| Ihab Awad | c35ad02 | 2014-06-12 16:29:42 -0700 | [diff] [blame] | 234 | } | 
| Tyler Gunn | c7d1078 | 2019-04-11 11:41:15 -0700 | [diff] [blame] | 235 |  | 
|  | 236 | /** | 
|  | 237 | * Determines if two {@link PhoneAccountHandle}s are from the same package. | 
|  | 238 | * | 
|  | 239 | * @param a Phone account handle to check for same {@link ConnectionService} package. | 
|  | 240 | * @param b Other phone account handle to check for same {@link ConnectionService} package. | 
|  | 241 | * @return {@code true} if the two {@link PhoneAccountHandle}s passed in belong to the same | 
|  | 242 | * {@link ConnectionService} / package, {@code false} otherwise.  Note: {@code null} phone | 
|  | 243 | * account handles are considered equivalent to other {@code null} phone account handles. | 
|  | 244 | * @hide | 
|  | 245 | */ | 
|  | 246 | public static boolean areFromSamePackage(@Nullable PhoneAccountHandle a, | 
|  | 247 | @Nullable PhoneAccountHandle b) { | 
|  | 248 | String aPackageName = a != null ? a.getComponentName().getPackageName() : null; | 
|  | 249 | String bPackageName = b != null ? b.getComponentName().getPackageName() : null; | 
|  | 250 | return Objects.equals(aPackageName, bPackageName); | 
|  | 251 | } | 
| Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 252 | } |