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