Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.accounts; |
| 18 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Mathew Inwood | 1fde99a | 2018-08-06 16:43:30 +0100 | [diff] [blame] | 21 | import android.annotation.UnsupportedAppUsage; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 23 | import android.os.Parcel; |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 24 | import android.os.Parcelable; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 25 | import android.os.RemoteException; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 26 | import android.os.ServiceManager; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 27 | import android.text.TextUtils; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 28 | import android.util.ArraySet; |
| 29 | import android.util.Log; |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 30 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 31 | import com.android.internal.annotations.GuardedBy; |
| 32 | |
| 33 | import java.util.Set; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 34 | |
| 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 | */ |
| 40 | public class Account implements Parcelable { |
Mathew Inwood | 1fde99a | 2018-08-06 16:43:30 +0100 | [diff] [blame] | 41 | @UnsupportedAppUsage |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 42 | private static final String TAG = "Account"; |
| 43 | |
| 44 | @GuardedBy("sAccessedAccounts") |
| 45 | private static final Set<Account> sAccessedAccounts = new ArraySet<>(); |
| 46 | |
Fred Quintana | 3710f39 | 2009-08-13 14:55:02 -0700 | [diff] [blame] | 47 | public final String name; |
| 48 | public final String type; |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 49 | private String mSafeName; |
Mathew Inwood | 1fde99a | 2018-08-06 16:43:30 +0100 | [diff] [blame] | 50 | @UnsupportedAppUsage |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 51 | private final @Nullable String accessId; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 52 | |
| 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 Quintana | 3710f39 | 2009-08-13 14:55:02 -0700 | [diff] [blame] | 57 | return name.equals(other.name) && type.equals(other.type); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | public int hashCode() { |
| 61 | int result = 17; |
Fred Quintana | 3710f39 | 2009-08-13 14:55:02 -0700 | [diff] [blame] | 62 | result = 31 * result + name.hashCode(); |
| 63 | result = 31 * result + type.hashCode(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 64 | return result; |
| 65 | } |
| 66 | |
| 67 | public Account(String name, String type) { |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 68 | this(name, type, null); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @hide |
| 73 | */ |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 74 | public Account(@NonNull Account other, @NonNull String accessId) { |
| 75 | this(other.name, other.type, accessId); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @hide |
| 80 | */ |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 81 | public Account(String name, String type, String accessId) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 82 | 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 Quintana | 3710f39 | 2009-08-13 14:55:02 -0700 | [diff] [blame] | 88 | this.name = name; |
| 89 | this.type = type; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 90 | this.accessId = accessId; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | public Account(Parcel in) { |
Fred Quintana | 3710f39 | 2009-08-13 14:55:02 -0700 | [diff] [blame] | 94 | this.name = in.readString(); |
| 95 | this.type = in.readString(); |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 96 | this.accessId = in.readString(); |
| 97 | if (accessId != null) { |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 98 | synchronized (sAccessedAccounts) { |
| 99 | if (sAccessedAccounts.add(this)) { |
| 100 | try { |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 101 | IAccountManager accountManager = IAccountManager.Stub.asInterface( |
| 102 | ServiceManager.getService(Context.ACCOUNT_SERVICE)); |
| 103 | accountManager.onAccountAccessed(accessId); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 104 | } catch (RemoteException e) { |
| 105 | Log.e(TAG, "Error noting account access", e); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** @hide */ |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 113 | public String getAccessId() { |
| 114 | return accessId; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | public int describeContents() { |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | public void writeToParcel(Parcel dest, int flags) { |
Fred Quintana | 3710f39 | 2009-08-13 14:55:02 -0700 | [diff] [blame] | 122 | dest.writeString(name); |
| 123 | dest.writeString(type); |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 124 | dest.writeString(accessId); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 125 | } |
| 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 Quintana | 3710f39 | 2009-08-13 14:55:02 -0700 | [diff] [blame] | 138 | return "Account {name=" + name + ", type=" + type + "}"; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 139 | } |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 140 | |
| 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 Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 173 | } |