AuthenticatorDescription.java revision 15a4d2ffd04dc6c70f2cd17dae12ac6bc14c69ab
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
17package android.accounts;
18
19import android.os.Parcelable;
20import android.os.Parcel;
21
22/**
23 * A {@link Parcelable} value type that contains information about an account authenticator.
24 */
25public class AuthenticatorDescription implements Parcelable {
26    /** The string that uniquely identifies an authenticator */
27    final public String type;
28
29    /** A resource id of a label for the authenticator that is suitable for displaying */
30    final public int labelId;
31
32    /** A resource id of a icon for the authenticator */
33    final public int iconId;
34
35    /** A resource id of a smaller icon for the authenticator */
36    final public int smallIconId;
37
38    /**
39     * A resource id for a hierarchy of PreferenceScreen to be added to the settings page for the
40     * account. See {@link AbstractAccountAuthenticator} for an example.
41     */
42    final public int accountPreferencesId;
43
44    /** The package name that can be used to lookup the resources from above. */
45    final public String packageName;
46
47    /** A constructor for a full AuthenticatorDescription */
48    public AuthenticatorDescription(String type, String packageName, int labelId, int iconId,
49            int smallIconId, int prefId) {
50        if (type == null) throw new IllegalArgumentException("type cannot be null");
51        if (packageName == null) throw new IllegalArgumentException("packageName cannot be null");
52        this.type = type;
53        this.packageName = packageName;
54        this.labelId = labelId;
55        this.iconId = iconId;
56        this.smallIconId = smallIconId;
57        this.accountPreferencesId = prefId;
58    }
59
60    /**
61     * A factory method for creating an AuthenticatorDescription that can be used as a key
62     * to identify the authenticator by its type.
63     */
64
65    public static AuthenticatorDescription newKey(String type) {
66        if (type == null) throw new IllegalArgumentException("type cannot be null");
67        return new AuthenticatorDescription(type);
68    }
69
70    private AuthenticatorDescription(String type) {
71        this.type = type;
72        this.packageName = null;
73        this.labelId = 0;
74        this.iconId = 0;
75        this.smallIconId = 0;
76        this.accountPreferencesId = 0;
77    }
78
79    private AuthenticatorDescription(Parcel source) {
80        this.type = source.readString();
81        this.packageName = source.readString();
82        this.labelId = source.readInt();
83        this.iconId = source.readInt();
84        this.smallIconId = source.readInt();
85        this.accountPreferencesId = source.readInt();
86    }
87
88    /** @inheritDoc */
89    public int describeContents() {
90        return 0;
91    }
92
93    /** Returns the hashcode of the type only. */
94    public int hashCode() {
95        return type.hashCode();
96    }
97
98    /** Compares the type only, suitable for key comparisons. */
99    public boolean equals(Object o) {
100        if (o == this) return true;
101        if (!(o instanceof AuthenticatorDescription)) return false;
102        final AuthenticatorDescription other = (AuthenticatorDescription) o;
103        return type.equals(other.type);
104    }
105
106    public String toString() {
107        return "AuthenticatorDescription {type=" + type + "}";
108    }
109
110    /** @inheritDoc */
111    public void writeToParcel(Parcel dest, int flags) {
112        dest.writeString(type);
113        dest.writeString(packageName);
114        dest.writeInt(labelId);
115        dest.writeInt(iconId);
116        dest.writeInt(smallIconId);
117        dest.writeInt(accountPreferencesId);
118    }
119
120    /** Used to create the object from a parcel. */
121    public static final Creator<AuthenticatorDescription> CREATOR =
122            new Creator<AuthenticatorDescription>() {
123        /** @inheritDoc */
124        public AuthenticatorDescription createFromParcel(Parcel source) {
125            return new AuthenticatorDescription(source);
126        }
127
128        /** @inheritDoc */
129        public AuthenticatorDescription[] newArray(int size) {
130            return new AuthenticatorDescription[size];
131        }
132    };
133}
134