summaryrefslogtreecommitdiffstats
path: root/emailcommon/src/com/android/emailcommon/provider/Credential.java
blob: d7961a29fc8a75381352fb58866766e4a27fa67e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.android.emailcommon.provider;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.BaseColumns;
import android.text.TextUtils;

import com.android.emailcommon.utility.Utility;
import com.android.mail.utils.LogUtils;
import com.google.common.base.Objects;

public class Credential extends EmailContent implements Parcelable, BaseColumns {

    public static final String TABLE_NAME = "Credential";
    public static Uri CONTENT_URI;

    public static final Credential EMPTY = new Credential(-1, "", "", "", 0);

    public static void initCredential() {
        CONTENT_URI = Uri.parse(EmailContent.CONTENT_URI + "/credential");
    }

    // This is the Id of the oauth provider. It can be used to lookup an oauth provider
    // from oauth.xml.
    public String mProviderId;
    public String mAccessToken;
    public String mRefreshToken;
    // This is the wall clock time, in milliseconds since Midnight, Jan 1, 1970.
    public long mExpiration;

    // Name of the authentication provider.
    public static final String PROVIDER_COLUMN = "provider";
    // Access token.
    public static final String ACCESS_TOKEN_COLUMN = "accessToken";
    // Refresh token.
    public static final String REFRESH_TOKEN_COLUMN = "refreshToken";
    // Expiration date for these credentials.
    public static final String EXPIRATION_COLUMN = "expiration";


    public interface CredentialQuery {
        public static final int ID_COLUMN_INDEX = 0;
        public static final int PROVIDER_COLUMN_INDEX = 1;
        public static final int ACCESS_TOKEN_COLUMN_INDEX = 2;
        public static final int REFRESH_TOKEN_COLUMN_INDEX = 3;
        public static final int EXPIRATION_COLUMN_INDEX = 4;

        public static final String[] PROJECTION = new String[] {
            _ID,
            PROVIDER_COLUMN,
            ACCESS_TOKEN_COLUMN,
            REFRESH_TOKEN_COLUMN,
            EXPIRATION_COLUMN
        };
    }

    public Credential() {
        mBaseUri = CONTENT_URI;
    }

    public Credential(long id, String providerId, String accessToken, String refreshToken,
            long expiration) {
        mBaseUri = CONTENT_URI;
        mId = id;
        mProviderId = providerId;
        mAccessToken = accessToken;
        mRefreshToken = refreshToken;
        mExpiration = expiration;
    }

    /**
     * Restore a Credential from the database, given its unique id
     * @return the instantiated Credential
     */
   public static Credential restoreCredentialsWithId(Context context, long id) {
       return EmailContent.restoreContentWithId(context, Credential.class,
               Credential.CONTENT_URI, CredentialQuery.PROJECTION, id);
   }

   @Override
   public void restore(Cursor cursor) {
       mBaseUri = CONTENT_URI;
       mId = cursor.getLong(CredentialQuery.ID_COLUMN_INDEX);
       mProviderId = cursor.getString(CredentialQuery.PROVIDER_COLUMN_INDEX);
       mAccessToken = cursor.getString(CredentialQuery.ACCESS_TOKEN_COLUMN_INDEX);
       mRefreshToken = cursor.getString(CredentialQuery.REFRESH_TOKEN_COLUMN_INDEX);
       mExpiration = cursor.getInt(CredentialQuery.EXPIRATION_COLUMN_INDEX);
   }

   /**
    * Supports Parcelable
    */
   @Override
   public int describeContents() {
       return 0;
   }

   /**
    * Supports Parcelable
    */
   public static final Parcelable.Creator<Credential> CREATOR
           = new Parcelable.Creator<Credential>() {
       @Override
       public Credential createFromParcel(Parcel in) {
           return new Credential(in);
       }

       @Override
       public Credential[] newArray(int size) {
           return new Credential[size];
       }
   };

   @Override
   public void writeToParcel(Parcel dest, int flags) {
       // mBaseUri is not parceled
       dest.writeLong(mId);
       dest.writeString(mProviderId);
       dest.writeString(mAccessToken);
       dest.writeString(mRefreshToken);
       dest.writeLong(mExpiration);
   }

   /**
    * Supports Parcelable
    */
   public Credential(Parcel in) {
       mBaseUri = CONTENT_URI;
       mId = in.readLong();
       mProviderId = in.readString();
       mAccessToken = in.readString();
       mRefreshToken = in.readString();
       mExpiration = in.readLong();
   }

   @Override
   public boolean equals(Object o) {
       if (!(o instanceof Credential)) {
           return false;
       }
       Credential that = (Credential)o;
       return Utility.areStringsEqual(mProviderId, that.mProviderId)
               && Utility.areStringsEqual(mAccessToken, that.mAccessToken)
               && Utility.areStringsEqual(mRefreshToken, that.mRefreshToken)
               && mExpiration == that.mExpiration;
   }

   @Override
   public int hashCode() {
       return Objects.hashCode(mAccessToken, mRefreshToken, mExpiration);
   }

   @Override
   public ContentValues toContentValues() {
       ContentValues values = new ContentValues();
       if (TextUtils.isEmpty(mProviderId)) {
           LogUtils.wtf(LogUtils.TAG, "Credential being saved with no provider");
       }
       values.put(PROVIDER_COLUMN, mProviderId);
       values.put(ACCESS_TOKEN_COLUMN, mAccessToken);
       values.put(REFRESH_TOKEN_COLUMN, mRefreshToken);
       values.put(EXPIRATION_COLUMN, mExpiration);
       return values;
   }
}