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