blob: d3b2238d8b69b6c6a3f0a7fad9a1c07bf53d5b0c [file] [log] [blame]
Fred Quintana60307342009-03-24 22:48:12 -07001/*
2 * Copyright (C) 2009 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
17package android.accounts;
18
Svet Ganovf6d424f12016-09-20 20:18:53 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
Mathew Inwood1fde99a2018-08-06 16:43:30 +010021import android.annotation.UnsupportedAppUsage;
Svet Ganovc1c0d1c2016-09-23 19:15:47 -070022import android.content.Context;
Fred Quintana60307342009-03-24 22:48:12 -070023import android.os.Parcel;
Hui Yu139c2482018-08-10 15:37:51 -070024import android.os.Parcelable;
Svet Ganovf6d424f12016-09-20 20:18:53 -070025import android.os.RemoteException;
Svet Ganovc1c0d1c2016-09-23 19:15:47 -070026import android.os.ServiceManager;
Fred Quintanaa698f422009-04-08 19:14:54 -070027import android.text.TextUtils;
Svet Ganovf6d424f12016-09-20 20:18:53 -070028import android.util.ArraySet;
29import android.util.Log;
Hui Yu139c2482018-08-10 15:37:51 -070030
Svet Ganovf6d424f12016-09-20 20:18:53 -070031import com.android.internal.annotations.GuardedBy;
32
33import java.util.Set;
Fred Quintana60307342009-03-24 22:48:12 -070034
35/**
36 * Value type that represents an Account in the {@link AccountManager}. This object is
37 * {@link Parcelable} and also overrides {@link #equals} and {@link #hashCode}, making it
38 * suitable for use as the key of a {@link java.util.Map}
39 */
40public class Account implements Parcelable {
Mathew Inwood1fde99a2018-08-06 16:43:30 +010041 @UnsupportedAppUsage
Svet Ganovf6d424f12016-09-20 20:18:53 -070042 private static final String TAG = "Account";
43
44 @GuardedBy("sAccessedAccounts")
45 private static final Set<Account> sAccessedAccounts = new ArraySet<>();
46
Fred Quintana3710f392009-08-13 14:55:02 -070047 public final String name;
48 public final String type;
Hui Yu139c2482018-08-10 15:37:51 -070049 private String mSafeName;
Mathew Inwood1fde99a2018-08-06 16:43:30 +010050 @UnsupportedAppUsage
Svet Ganovc1c0d1c2016-09-23 19:15:47 -070051 private final @Nullable String accessId;
Fred Quintana60307342009-03-24 22:48:12 -070052
53 public boolean equals(Object o) {
54 if (o == this) return true;
55 if (!(o instanceof Account)) return false;
56 final Account other = (Account)o;
Fred Quintana3710f392009-08-13 14:55:02 -070057 return name.equals(other.name) && type.equals(other.type);
Fred Quintana60307342009-03-24 22:48:12 -070058 }
59
60 public int hashCode() {
61 int result = 17;
Fred Quintana3710f392009-08-13 14:55:02 -070062 result = 31 * result + name.hashCode();
63 result = 31 * result + type.hashCode();
Fred Quintana60307342009-03-24 22:48:12 -070064 return result;
65 }
66
67 public Account(String name, String type) {
Svet Ganovf6d424f12016-09-20 20:18:53 -070068 this(name, type, null);
69 }
70
71 /**
72 * @hide
73 */
Svet Ganovc1c0d1c2016-09-23 19:15:47 -070074 public Account(@NonNull Account other, @NonNull String accessId) {
75 this(other.name, other.type, accessId);
Svet Ganovf6d424f12016-09-20 20:18:53 -070076 }
77
78 /**
79 * @hide
80 */
Svet Ganovc1c0d1c2016-09-23 19:15:47 -070081 public Account(String name, String type, String accessId) {
Fred Quintanaa698f422009-04-08 19:14:54 -070082 if (TextUtils.isEmpty(name)) {
83 throw new IllegalArgumentException("the name must not be empty: " + name);
84 }
85 if (TextUtils.isEmpty(type)) {
86 throw new IllegalArgumentException("the type must not be empty: " + type);
87 }
Fred Quintana3710f392009-08-13 14:55:02 -070088 this.name = name;
89 this.type = type;
Svet Ganovc1c0d1c2016-09-23 19:15:47 -070090 this.accessId = accessId;
Fred Quintana60307342009-03-24 22:48:12 -070091 }
92
93 public Account(Parcel in) {
Fred Quintana3710f392009-08-13 14:55:02 -070094 this.name = in.readString();
95 this.type = in.readString();
Svet Ganovc1c0d1c2016-09-23 19:15:47 -070096 this.accessId = in.readString();
97 if (accessId != null) {
Svet Ganovf6d424f12016-09-20 20:18:53 -070098 synchronized (sAccessedAccounts) {
99 if (sAccessedAccounts.add(this)) {
100 try {
Svet Ganovc1c0d1c2016-09-23 19:15:47 -0700101 IAccountManager accountManager = IAccountManager.Stub.asInterface(
102 ServiceManager.getService(Context.ACCOUNT_SERVICE));
103 accountManager.onAccountAccessed(accessId);
Svet Ganovf6d424f12016-09-20 20:18:53 -0700104 } catch (RemoteException e) {
105 Log.e(TAG, "Error noting account access", e);
106 }
107 }
108 }
109 }
110 }
111
112 /** @hide */
Svet Ganovc1c0d1c2016-09-23 19:15:47 -0700113 public String getAccessId() {
114 return accessId;
Fred Quintana60307342009-03-24 22:48:12 -0700115 }
116
117 public int describeContents() {
118 return 0;
119 }
120
121 public void writeToParcel(Parcel dest, int flags) {
Fred Quintana3710f392009-08-13 14:55:02 -0700122 dest.writeString(name);
123 dest.writeString(type);
Svet Ganovc1c0d1c2016-09-23 19:15:47 -0700124 dest.writeString(accessId);
Fred Quintana60307342009-03-24 22:48:12 -0700125 }
126
127 public static final Creator<Account> CREATOR = new Creator<Account>() {
128 public Account createFromParcel(Parcel source) {
129 return new Account(source);
130 }
131
132 public Account[] newArray(int size) {
133 return new Account[size];
134 }
135 };
136
137 public String toString() {
Fred Quintana3710f392009-08-13 14:55:02 -0700138 return "Account {name=" + name + ", type=" + type + "}";
Fred Quintana60307342009-03-24 22:48:12 -0700139 }
Hui Yu139c2482018-08-10 15:37:51 -0700140
141 /**
142 * Return a string representation of the account that is safe to print
143 * to logs and other places where PII should be avoided.
144 * @hide
145 */
146 public String toSafeString() {
147 if (mSafeName == null) {
148 mSafeName = toSafeName(name, 'x');
149 }
150 return "Account {name=" + mSafeName + ", type=" + type + "}";
151 }
152
153 /**
154 * Given a name, replace all letter or digits with the replacement char.
155 * @param name The input name string.
156 * @param replacement the replacement character.
157 * @return the string after replacement.
158 * @hide
159 */
160 public static String toSafeName(String name, char replacement) {
161 final StringBuilder builder = new StringBuilder(64);
162 final int len = name.length();
163 for (int i = 0; i < len; i++) {
164 final char c = name.charAt(i);
165 if (Character.isLetterOrDigit(c)) {
166 builder.append(replacement);
167 } else {
168 builder.append(c);
169 }
170 }
171 return builder.toString();
172 }
Fred Quintana60307342009-03-24 22:48:12 -0700173}