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