manage tokens in a class
authorEugene Crosser <Eugene.Crosser@ru.ibm.com>
Wed, 1 Feb 2012 14:16:37 +0000 (18:16 +0400)
committerEugene Crosser <Eugene.Crosser@ru.ibm.com>
Wed, 1 Feb 2012 14:16:37 +0000 (18:16 +0400)
src/org/average/whereami/Authorize.java
src/org/average/whereami/CredentialStore.java [new file with mode: 0644]

index 1bf5fad0f6bdbf1e74cff00a127007e691c099a9..44a88a9b470d11943ed2c772d4311b91161a58e0 100644 (file)
@@ -6,6 +6,7 @@ package org.average.whereami;
 import java.io.IOException;
 
 import org.average.whereami.ClientCredentials;
+import org.average.whereami.CredentialStore;
 
 import android.app.Activity;
 import android.os.Bundle;
@@ -72,13 +73,15 @@ public class Authorize extends Activity {
                                 ClientCredentials.CLIENT_SECRET,
                                 code,
                                 ClientCredentials.REDIRECT_URI).execute();
-                            storeTokens(accessTokenResponse);
+                            CredentialStore credentialStore =
+                                               new CredentialStore(prefs);
+                            credentialStore.write(accessTokenResponse);
                             finish();
                             //view.setVisibility(View.INVISIBLE);
                             //startActivity(new Intent(Authorize.this,
                             //                         WhereAmI.class));
                         } else if (url.indexOf("error=")!=-1) {
-                            clearTokens();
+                            new CredentialStore(prefs).clear();
                             finish();
                             //view.setVisibility(View.INVISIBLE);
                             //startActivity(new Intent(Authorize.this,
@@ -96,30 +99,6 @@ public class Authorize extends Activity {
                             ClientCredentials.REDIRECT_URI.length()+7,
                             url.length());
             }
-
-            private void storeTokens(AccessTokenResponse accessTokenResponse) {
-                Log.w(TAG, "Storing tokens: " + accessTokenResponse);
-                Editor editor = prefs.edit();
-                editor.putString(ACCESS_TOKEN,
-                                 accessTokenResponse.accessToken);
-                editor.putLong(  EXPIRES_IN,
-                                 accessTokenResponse.expiresIn);
-                editor.putString(REFRESH_TOKEN,
-                                 accessTokenResponse.refreshToken);
-                editor.putString(SCOPE,
-                                 accessTokenResponse.scope);
-                editor.commit();
-            }
-
-            private void clearTokens() {
-                Log.w(TAG, "Clear tokens");
-                Editor editor = prefs.edit();
-                editor.remove(ACCESS_TOKEN);
-                editor.remove(EXPIRES_IN);
-                editor.remove(REFRESH_TOKEN);
-                editor.remove(SCOPE);
-                editor.commit();
-            }
         });
 
         webview.loadUrl(authorizationUrl);
diff --git a/src/org/average/whereami/CredentialStore.java b/src/org/average/whereami/CredentialStore.java
new file mode 100644 (file)
index 0000000..2dcbffb
--- /dev/null
@@ -0,0 +1,47 @@
+package org.average.whereami;
+
+import android.content.SharedPreferences;
+import android.content.SharedPreferences.Editor;
+
+import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
+
+public class CredentialStore {
+
+    private static final String ACCESS_TOKEN = "access_token";
+    private static final String EXPIRES_IN = "expires_in";
+    private static final String REFRESH_TOKEN = "refresh_token";
+    private static final String SCOPE = "scope";
+
+    private SharedPreferences prefs;
+
+    public CredentialStore(SharedPreferences prefs) {
+        this.prefs = prefs;
+    }
+
+    public AccessTokenResponse read() {
+        AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
+        accessTokenResponse.accessToken = prefs.getString(ACCESS_TOKEN, "");
+        accessTokenResponse.expiresIn = prefs.getLong(EXPIRES_IN, 0);
+        accessTokenResponse.refreshToken = prefs.getString(REFRESH_TOKEN, "");
+        accessTokenResponse.scope = prefs.getString(SCOPE, "");
+        return accessTokenResponse;
+    }
+
+    public void write(AccessTokenResponse accessTokenResponse) {
+        Editor editor = prefs.edit();
+        editor.putString(ACCESS_TOKEN,accessTokenResponse.accessToken);
+        editor.putLong(EXPIRES_IN,accessTokenResponse.expiresIn);
+        editor.putString(REFRESH_TOKEN,accessTokenResponse.refreshToken);
+        editor.putString(SCOPE,accessTokenResponse.scope);
+        editor.commit();
+    }
+
+    public void clear() {
+        Editor editor = prefs.edit();
+        editor.remove(ACCESS_TOKEN);
+        editor.remove(EXPIRES_IN);
+        editor.remove(REFRESH_TOKEN);
+        editor.remove(SCOPE);
+        editor.commit();
+    }
+}