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