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 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.UnsupportedAppUsage; 22 import android.content.Context; 23 import android.os.Parcel; 24 import android.os.Parcelable; 25 import android.os.RemoteException; 26 import android.os.ServiceManager; 27 import android.text.TextUtils; 28 import android.util.ArraySet; 29 import android.util.Log; 30 31 import com.android.internal.annotations.GuardedBy; 32 33 import java.util.Set; 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 { 41 @UnsupportedAppUsage 42 private static final String TAG = "Account"; 43 44 @GuardedBy("sAccessedAccounts") 45 private static final Set<Account> sAccessedAccounts = new ArraySet<>(); 46 47 public final String name; 48 public final String type; 49 private String mSafeName; 50 @UnsupportedAppUsage 51 private final @Nullable String accessId; 52 equals(Object o)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; 57 return name.equals(other.name) && type.equals(other.type); 58 } 59 hashCode()60 public int hashCode() { 61 int result = 17; 62 result = 31 * result + name.hashCode(); 63 result = 31 * result + type.hashCode(); 64 return result; 65 } 66 Account(String name, String type)67 public Account(String name, String type) { 68 this(name, type, null); 69 } 70 71 /** 72 * @hide 73 */ Account(@onNull Account other, @NonNull String accessId)74 public Account(@NonNull Account other, @NonNull String accessId) { 75 this(other.name, other.type, accessId); 76 } 77 78 /** 79 * @hide 80 */ Account(String name, String type, String accessId)81 public Account(String name, String type, String accessId) { 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 } 88 this.name = name; 89 this.type = type; 90 this.accessId = accessId; 91 } 92 Account(Parcel in)93 public Account(Parcel in) { 94 this.name = in.readString(); 95 this.type = in.readString(); 96 if (TextUtils.isEmpty(name)) { 97 throw new android.os.BadParcelableException("the name must not be empty: " + name); 98 } 99 if (TextUtils.isEmpty(type)) { 100 throw new android.os.BadParcelableException("the type must not be empty: " + type); 101 } 102 this.accessId = in.readString(); 103 if (accessId != null) { 104 synchronized (sAccessedAccounts) { 105 if (sAccessedAccounts.add(this)) { 106 try { 107 IAccountManager accountManager = IAccountManager.Stub.asInterface( 108 ServiceManager.getService(Context.ACCOUNT_SERVICE)); 109 accountManager.onAccountAccessed(accessId); 110 } catch (RemoteException e) { 111 Log.e(TAG, "Error noting account access", e); 112 } 113 } 114 } 115 } 116 } 117 118 /** @hide */ getAccessId()119 public String getAccessId() { 120 return accessId; 121 } 122 describeContents()123 public int describeContents() { 124 return 0; 125 } 126 writeToParcel(Parcel dest, int flags)127 public void writeToParcel(Parcel dest, int flags) { 128 dest.writeString(name); 129 dest.writeString(type); 130 dest.writeString(accessId); 131 } 132 133 public static final @android.annotation.NonNull Creator<Account> CREATOR = new Creator<Account>() { 134 public Account createFromParcel(Parcel source) { 135 return new Account(source); 136 } 137 138 public Account[] newArray(int size) { 139 return new Account[size]; 140 } 141 }; 142 toString()143 public String toString() { 144 return "Account {name=" + name + ", type=" + type + "}"; 145 } 146 147 /** 148 * Return a string representation of the account that is safe to print 149 * to logs and other places where PII should be avoided. 150 * @hide 151 */ toSafeString()152 public String toSafeString() { 153 if (mSafeName == null) { 154 mSafeName = toSafeName(name, 'x'); 155 } 156 return "Account {name=" + mSafeName + ", type=" + type + "}"; 157 } 158 159 /** 160 * Given a name, replace all letter or digits with the replacement char. 161 * @param name The input name string. 162 * @param replacement the replacement character. 163 * @return the string after replacement. 164 * @hide 165 */ toSafeName(String name, char replacement)166 public static String toSafeName(String name, char replacement) { 167 final StringBuilder builder = new StringBuilder(64); 168 final int len = name.length(); 169 for (int i = 0; i < len; i++) { 170 final char c = name.charAt(i); 171 if (Character.isLetterOrDigit(c)) { 172 builder.append(replacement); 173 } else { 174 builder.append(c); 175 } 176 } 177 return builder.toString(); 178 } 179 } 180