Credential.java revision 3dd85723a1af5537e23e4b05bdc361cce9cd42be
1package com.android.emailcommon.provider; 2 3import android.content.ContentValues; 4import android.content.Context; 5import android.database.Cursor; 6import android.net.Uri; 7import android.os.Parcel; 8import android.os.Parcelable; 9import android.provider.BaseColumns; 10import android.text.TextUtils; 11 12import com.android.emailcommon.utility.Utility; 13import com.android.mail.utils.LogUtils; 14import com.google.common.base.Objects; 15 16public class Credential extends EmailContent implements Parcelable, BaseColumns { 17 18 public static final String TABLE_NAME = "Credential"; 19 public static Uri CONTENT_URI; 20 21 public static final Credential EMPTY = new Credential(-1, "", "", "", 0); 22 23 public static void initCredential() { 24 CONTENT_URI = Uri.parse(EmailContent.CONTENT_URI + "/credential"); 25 } 26 27 // This is the Id of the oauth provider. It can be used to lookup an oauth provider 28 // from oauth.xml. 29 public String mProviderId; 30 public String mAccessToken; 31 public String mRefreshToken; 32 // This is the wall clock time, in milliseconds since Midnight, Jan 1, 1970. 33 public long mExpiration; 34 35 // Name of the authentication provider. 36 public static final String PROVIDER_COLUMN = "provider"; 37 // Access token. 38 public static final String ACCESS_TOKEN_COLUMN = "accessToken"; 39 // Refresh token. 40 public static final String REFRESH_TOKEN_COLUMN = "refreshToken"; 41 // Expiration date for these credentials. 42 public static final String EXPIRATION_COLUMN = "expiration"; 43 44 45 public interface CredentialQuery { 46 public static final int ID_COLUMN_INDEX = 0; 47 public static final int PROVIDER_COLUMN_INDEX = 1; 48 public static final int ACCESS_TOKEN_COLUMN_INDEX = 2; 49 public static final int REFRESH_TOKEN_COLUMN_INDEX = 3; 50 public static final int EXPIRATION_COLUMN_INDEX = 4; 51 52 public static final String[] PROJECTION = new String[] { 53 _ID, 54 PROVIDER_COLUMN, 55 ACCESS_TOKEN_COLUMN, 56 REFRESH_TOKEN_COLUMN, 57 EXPIRATION_COLUMN 58 }; 59 } 60 61 public Credential() { 62 mBaseUri = CONTENT_URI; 63 } 64 65 public Credential(long id, String providerId, String accessToken, String refreshToken, 66 long expiration) { 67 mBaseUri = CONTENT_URI; 68 mId = id; 69 mProviderId = providerId; 70 mAccessToken = accessToken; 71 mRefreshToken = refreshToken; 72 mExpiration = expiration; 73 } 74 75 /** 76 * Restore a Credential from the database, given its unique id 77 * @return the instantiated Credential 78 */ 79 public static Credential restoreCredentialsWithId(Context context, long id) { 80 return EmailContent.restoreContentWithId(context, Credential.class, 81 Credential.CONTENT_URI, CredentialQuery.PROJECTION, id); 82 } 83 84 @Override 85 public void restore(Cursor cursor) { 86 mBaseUri = CONTENT_URI; 87 mId = cursor.getLong(CredentialQuery.ID_COLUMN_INDEX); 88 mProviderId = cursor.getString(CredentialQuery.PROVIDER_COLUMN_INDEX); 89 mAccessToken = cursor.getString(CredentialQuery.ACCESS_TOKEN_COLUMN_INDEX); 90 mRefreshToken = cursor.getString(CredentialQuery.REFRESH_TOKEN_COLUMN_INDEX); 91 mExpiration = cursor.getInt(CredentialQuery.EXPIRATION_COLUMN_INDEX); 92 } 93 94 /** 95 * Supports Parcelable 96 */ 97 @Override 98 public int describeContents() { 99 return 0; 100 } 101 102 /** 103 * Supports Parcelable 104 */ 105 public static final Parcelable.Creator<Credential> CREATOR 106 = new Parcelable.Creator<Credential>() { 107 @Override 108 public Credential createFromParcel(Parcel in) { 109 return new Credential(in); 110 } 111 112 @Override 113 public Credential[] newArray(int size) { 114 return new Credential[size]; 115 } 116 }; 117 118 @Override 119 public void writeToParcel(Parcel dest, int flags) { 120 // mBaseUri is not parceled 121 dest.writeLong(mId); 122 dest.writeString(mProviderId); 123 dest.writeString(mAccessToken); 124 dest.writeString(mRefreshToken); 125 dest.writeLong(mExpiration); 126 } 127 128 /** 129 * Supports Parcelable 130 */ 131 public Credential(Parcel in) { 132 mBaseUri = CONTENT_URI; 133 mId = in.readLong(); 134 mProviderId = in.readString(); 135 mAccessToken = in.readString(); 136 mRefreshToken = in.readString(); 137 mExpiration = in.readLong(); 138 } 139 140 @Override 141 public boolean equals(Object o) { 142 if (!(o instanceof Credential)) { 143 return false; 144 } 145 Credential that = (Credential)o; 146 return Utility.areStringsEqual(mProviderId, that.mProviderId) 147 && Utility.areStringsEqual(mAccessToken, that.mAccessToken) 148 && Utility.areStringsEqual(mRefreshToken, that.mRefreshToken) 149 && mExpiration == that.mExpiration; 150 } 151 152 @Override 153 public int hashCode() { 154 return Objects.hashCode(mAccessToken, mRefreshToken, mExpiration); 155 } 156 157 @Override 158 public ContentValues toContentValues() { 159 ContentValues values = new ContentValues(); 160 if (TextUtils.isEmpty(mProviderId)) { 161 LogUtils.wtf(LogUtils.TAG, "Credential being saved with no provider"); 162 } 163 values.put(PROVIDER_COLUMN, mProviderId); 164 values.put(ACCESS_TOKEN_COLUMN, mAccessToken); 165 values.put(REFRESH_TOKEN_COLUMN, mRefreshToken); 166 values.put(EXPIRATION_COLUMN, mExpiration); 167 return values; 168 } 169} 170