blob: 77b510df8ff86f1cdab451eb928040cf40ae97b6 [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;
Ihab Awadc35ad022014-06-12 16:29:42 -070020import android.content.ComponentName;
Ihab Awad542e0ea2014-05-16 10:22:16 -070021import android.os.Parcel;
22import android.os.Parcelable;
Evan Charlton134dd682014-11-25 14:12:57 -080023import android.os.Process;
24import android.os.UserHandle;
Ihab Awadc35ad022014-06-12 16:29:42 -070025
Ihab Awaddcaa5d62014-07-08 10:33:46 -070026import java.util.Objects;
Ihab Awad542e0ea2014-05-16 10:22:16 -070027
28/**
Santos Cordond9e614f2014-10-28 13:10:36 -070029 * The unique identifier for a {@link PhoneAccount}. A {@code PhoneAccountHandle} is made of two
30 * parts:
31 * <ul>
Brian Attwellad147f42014-12-19 11:37:16 -080032 * <li>The component name of the associated connection service.</li>
Santos Cordond9e614f2014-10-28 13:10:36 -070033 * <li>A string identifier that is unique across {@code PhoneAccountHandle}s with the same
34 * component name.</li>
35 * </ul>
36 *
Brad Ebinger428cec92016-03-15 14:23:44 -070037 * Note: This Class requires a non-null {@link ComponentName} and {@link UserHandle} to operate
38 * properly. Passing in invalid parameters will generate a log warning.
39 *
Brian Attwellad147f42014-12-19 11:37:16 -080040 * See {@link PhoneAccount}, {@link TelecomManager}.
Ihab Awad542e0ea2014-05-16 10:22:16 -070041 */
Yorke Lee400470f2015-05-12 13:31:25 -070042public final class PhoneAccountHandle implements Parcelable {
Evan Charlton134dd682014-11-25 14:12:57 -080043 private final ComponentName mComponentName;
44 private final String mId;
45 private final UserHandle mUserHandle;
Ihab Awadc35ad022014-06-12 16:29:42 -070046
Evan Charlton6eb262c2014-07-19 18:18:19 -070047 public PhoneAccountHandle(
Brad Ebinger428cec92016-03-15 14:23:44 -070048 @NonNull ComponentName componentName,
49 @NonNull String id) {
Evan Charlton134dd682014-11-25 14:12:57 -080050 this(componentName, id, Process.myUserHandle());
51 }
52
Evan Charlton134dd682014-11-25 14:12:57 -080053 public PhoneAccountHandle(
Brad Ebinger428cec92016-03-15 14:23:44 -070054 @NonNull ComponentName componentName,
55 @NonNull String id,
56 @NonNull UserHandle userHandle) {
57 checkParameters(componentName, userHandle);
Ihab Awadc35ad022014-06-12 16:29:42 -070058 mComponentName = componentName;
59 mId = id;
Evan Charlton134dd682014-11-25 14:12:57 -080060 mUserHandle = userHandle;
Ihab Awadc35ad022014-06-12 16:29:42 -070061 }
62
63 /**
Brian Attwellad147f42014-12-19 11:37:16 -080064 * The {@code ComponentName} of the connection service which is responsible for making phone
65 * calls using this {@code PhoneAccountHandle}.
Ihab Awadc35ad022014-06-12 16:29:42 -070066 *
67 * @return A suitable {@code ComponentName}.
68 */
69 public ComponentName getComponentName() {
70 return mComponentName;
71 }
72
73 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -070074 * A string that uniquely distinguishes this particular {@code PhoneAccountHandle} from all the
Brian Attwellad147f42014-12-19 11:37:16 -080075 * others supported by the connection service that created it.
Ihab Awadb19a0bc2014-08-07 19:46:01 -070076 * <p>
Brian Attwellad147f42014-12-19 11:37:16 -080077 * A connection service must select identifiers that are stable for the lifetime of
Ihab Awadb19a0bc2014-08-07 19:46:01 -070078 * their users' relationship with their service, across many Android devices. For example, a
79 * good set of identifiers might be the email addresses with which with users registered for
80 * their accounts with a particular service. Depending on how a service chooses to operate,
81 * a bad set of identifiers might be an increasing series of integers
82 * ({@code 0}, {@code 1}, {@code 2}, ...) that are generated locally on each phone and could
83 * collide with values generated on other phones or after a data wipe of a given phone.
Ihab Awadc35ad022014-06-12 16:29:42 -070084 *
Santos Cordon8b338d42015-02-10 03:38:31 -080085 * Important: A non-unique identifier could cause non-deterministic call-log backup/restore
86 * behavior.
87 *
Ihab Awadb19a0bc2014-08-07 19:46:01 -070088 * @return A service-specific unique identifier for this {@code PhoneAccountHandle}.
Ihab Awadc35ad022014-06-12 16:29:42 -070089 */
90 public String getId() {
91 return mId;
92 }
93
Evan Charlton134dd682014-11-25 14:12:57 -080094 /**
95 * @return the {@link UserHandle} to use when connecting to this PhoneAccount.
Evan Charlton134dd682014-11-25 14:12:57 -080096 */
97 public UserHandle getUserHandle() {
98 return mUserHandle;
99 }
100
Ihab Awad807fe0a2014-07-09 12:30:52 -0700101 @Override
102 public int hashCode() {
Evan Charlton134dd682014-11-25 14:12:57 -0800103 return Objects.hash(mComponentName, mId, mUserHandle);
Ihab Awadc35ad022014-06-12 16:29:42 -0700104 }
105
Santos Cordon98b27032014-07-14 03:32:56 -0700106 @Override
107 public String toString() {
Tyler Gunn76c01a52014-09-30 14:47:51 -0700108 // Note: Log.pii called for mId as it can contain personally identifying phone account
109 // information such as SIP account IDs.
Santos Cordon98b27032014-07-14 03:32:56 -0700110 return new StringBuilder().append(mComponentName)
111 .append(", ")
Tyler Gunn76c01a52014-09-30 14:47:51 -0700112 .append(Log.pii(mId))
Evan Charlton134dd682014-11-25 14:12:57 -0800113 .append(", ")
114 .append(mUserHandle)
Santos Cordon98b27032014-07-14 03:32:56 -0700115 .toString();
116 }
117
Ihab Awad94cf4bf2014-07-17 11:21:19 -0700118 @Override
119 public boolean equals(Object other) {
Santos Cordon98b27032014-07-14 03:32:56 -0700120 return other != null &&
Evan Charlton6eb262c2014-07-19 18:18:19 -0700121 other instanceof PhoneAccountHandle &&
122 Objects.equals(((PhoneAccountHandle) other).getComponentName(),
123 getComponentName()) &&
Evan Charlton134dd682014-11-25 14:12:57 -0800124 Objects.equals(((PhoneAccountHandle) other).getId(), getId()) &&
125 Objects.equals(((PhoneAccountHandle) other).getUserHandle(), getUserHandle());
Santos Cordon98b27032014-07-14 03:32:56 -0700126 }
127
Ihab Awaddcaa5d62014-07-08 10:33:46 -0700128 //
Ihab Awad807fe0a2014-07-09 12:30:52 -0700129 // Parcelable implementation.
130 //
Ihab Awad542e0ea2014-05-16 10:22:16 -0700131
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700132 @Override
Ihab Awad542e0ea2014-05-16 10:22:16 -0700133 public int describeContents() {
134 return 0;
135 }
136
Sailesh Nepale7ef59a2014-07-08 21:48:22 -0700137 @Override
Ihab Awadc35ad022014-06-12 16:29:42 -0700138 public void writeToParcel(Parcel out, int flags) {
Evan Charlton134dd682014-11-25 14:12:57 -0800139 mComponentName.writeToParcel(out, flags);
Ihab Awadc35ad022014-06-12 16:29:42 -0700140 out.writeString(mId);
Evan Charlton134dd682014-11-25 14:12:57 -0800141 mUserHandle.writeToParcel(out, flags);
Ihab Awadc35ad022014-06-12 16:29:42 -0700142 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700143
Brad Ebinger428cec92016-03-15 14:23:44 -0700144 private void checkParameters(ComponentName componentName, UserHandle userHandle) {
145 if(componentName == null) {
146 android.util.Log.w("PhoneAccountHandle", new Exception("PhoneAccountHandle has " +
147 "been created with null ComponentName!"));
148 }
149 if(userHandle == null) {
150 android.util.Log.w("PhoneAccountHandle", new Exception("PhoneAccountHandle has " +
151 "been created with null UserHandle!"));
152 }
153 }
154
Evan Charlton6eb262c2014-07-19 18:18:19 -0700155 public static final Creator<PhoneAccountHandle> CREATOR = new Creator<PhoneAccountHandle>() {
Ihab Awad807fe0a2014-07-09 12:30:52 -0700156 @Override
Evan Charlton6eb262c2014-07-19 18:18:19 -0700157 public PhoneAccountHandle createFromParcel(Parcel in) {
158 return new PhoneAccountHandle(in);
Ihab Awad542e0ea2014-05-16 10:22:16 -0700159 }
160
Ihab Awad807fe0a2014-07-09 12:30:52 -0700161 @Override
Evan Charlton6eb262c2014-07-19 18:18:19 -0700162 public PhoneAccountHandle[] newArray(int size) {
163 return new PhoneAccountHandle[size];
Ihab Awad542e0ea2014-05-16 10:22:16 -0700164 }
165 };
166
Evan Charlton6eb262c2014-07-19 18:18:19 -0700167 private PhoneAccountHandle(Parcel in) {
Evan Charlton134dd682014-11-25 14:12:57 -0800168 this(ComponentName.CREATOR.createFromParcel(in),
169 in.readString(),
170 UserHandle.CREATOR.createFromParcel(in));
Ihab Awadc35ad022014-06-12 16:29:42 -0700171 }
Ihab Awad542e0ea2014-05-16 10:22:16 -0700172}