trying to avoid wierd lost auth/ssl error cases
[WhereAmI.git] / src / org / average / whereami / CredentialStore.java
1 package org.average.whereami;
2
3 import android.content.Context;
4 import android.content.SharedPreferences;
5 import android.content.SharedPreferences.Editor;
6
7 import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
8
9 public class CredentialStore {
10
11     private static final String ACCESS_TOKEN = "access_token";
12     private static final String EXPIRES_IN = "expires_in";
13     private static final String REFRESH_TOKEN = "refresh_token";
14     private static final String SCOPE = "scope";
15
16     private SharedPreferences prefs;
17
18     public CredentialStore(SharedPreferences prefs) {
19         this.prefs = prefs;
20     }
21
22     public AccessTokenResponse read() {
23         AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
24         accessTokenResponse.accessToken = prefs.getString(ACCESS_TOKEN, "");
25         accessTokenResponse.expiresIn = prefs.getLong(EXPIRES_IN, 0);
26         accessTokenResponse.refreshToken = prefs.getString(REFRESH_TOKEN, "");
27         accessTokenResponse.scope = prefs.getString(SCOPE, "");
28         return accessTokenResponse;
29     }
30
31     public void write(AccessTokenResponse accessTokenResponse) {
32         Editor editor = prefs.edit();
33         editor.putString(ACCESS_TOKEN,accessTokenResponse.accessToken);
34         editor.putLong(EXPIRES_IN,accessTokenResponse.expiresIn);
35         editor.putString(REFRESH_TOKEN,accessTokenResponse.refreshToken);
36         editor.putString(SCOPE,accessTokenResponse.scope);
37         editor.commit();
38     }
39
40     public void clear() {
41         Editor editor = prefs.edit();
42         editor.remove(ACCESS_TOKEN);
43         editor.remove(EXPIRES_IN);
44         editor.remove(REFRESH_TOKEN);
45         editor.remove(SCOPE);
46         editor.commit();
47     }
48
49     public void updateAccessToken(String accessToken) {
50         Editor editor = prefs.edit();
51         editor.putString(ACCESS_TOKEN,accessToken);
52         editor.commit();
53     }
54 }