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.content.Context;
22 import android.os.Parcelable;
23 import android.os.Parcel;
24 import android.os.RemoteException;
25 import android.os.ServiceManager;
26 import android.text.TextUtils;
27 import android.util.ArraySet;
28 import android.util.Log;
29 import com.android.internal.annotations.GuardedBy;
30 
31 import java.util.Set;
32 
33 /**
34  * Value type that represents an Account in the {@link AccountManager}. This object is
35  * {@link Parcelable} and also overrides {@link #equals} and {@link #hashCode}, making it
36  * suitable for use as the key of a {@link java.util.Map}
37  */
38 public class Account implements Parcelable {
39     private static final String TAG = "Account";
40 
41     @GuardedBy("sAccessedAccounts")
42     private static final Set<Account> sAccessedAccounts = new ArraySet<>();
43 
44     public final String name;
45     public final String type;
46     private final @Nullable String accessId;
47 
equals(Object o)48     public boolean equals(Object o) {
49         if (o == this) return true;
50         if (!(o instanceof Account)) return false;
51         final Account other = (Account)o;
52         return name.equals(other.name) && type.equals(other.type);
53     }
54 
hashCode()55     public int hashCode() {
56         int result = 17;
57         result = 31 * result + name.hashCode();
58         result = 31 * result + type.hashCode();
59         return result;
60     }
61 
Account(String name, String type)62     public Account(String name, String type) {
63         this(name, type, null);
64     }
65 
66     /**
67      * @hide
68      */
Account(@onNull Account other, @NonNull String accessId)69     public Account(@NonNull Account other, @NonNull String accessId) {
70         this(other.name, other.type, accessId);
71     }
72 
73     /**
74      * @hide
75      */
Account(String name, String type, String accessId)76     public Account(String name, String type, String accessId) {
77         if (TextUtils.isEmpty(name)) {
78             throw new IllegalArgumentException("the name must not be empty: " + name);
79         }
80         if (TextUtils.isEmpty(type)) {
81             throw new IllegalArgumentException("the type must not be empty: " + type);
82         }
83         this.name = name;
84         this.type = type;
85         this.accessId = accessId;
86     }
87 
Account(Parcel in)88     public Account(Parcel in) {
89         this.name = in.readString();
90         this.type = in.readString();
91         this.accessId = in.readString();
92         if (accessId != null) {
93             synchronized (sAccessedAccounts) {
94                 if (sAccessedAccounts.add(this)) {
95                     try {
96                         IAccountManager accountManager = IAccountManager.Stub.asInterface(
97                                 ServiceManager.getService(Context.ACCOUNT_SERVICE));
98                         accountManager.onAccountAccessed(accessId);
99                     } catch (RemoteException e) {
100                         Log.e(TAG, "Error noting account access", e);
101                     }
102                 }
103             }
104         }
105     }
106 
107     /** @hide */
getAccessId()108     public String getAccessId() {
109         return accessId;
110     }
111 
describeContents()112     public int describeContents() {
113         return 0;
114     }
115 
writeToParcel(Parcel dest, int flags)116     public void writeToParcel(Parcel dest, int flags) {
117         dest.writeString(name);
118         dest.writeString(type);
119         dest.writeString(accessId);
120     }
121 
122     public static final Creator<Account> CREATOR = new Creator<Account>() {
123         public Account createFromParcel(Parcel source) {
124             return new Account(source);
125         }
126 
127         public Account[] newArray(int size) {
128             return new Account[size];
129         }
130     };
131 
toString()132     public String toString() {
133         return "Account {name=" + name + ", type=" + type + "}";
134     }
135 }
136