Factored out authtoken retrieval to LoginController. Still forwarding to JunkActivit...
authorkenstir <kenstir@gmail.com>
Thu, 21 Nov 2013 03:33:02 +0000 (22:33 -0500)
committerkenstir <kenstir@gmail.com>
Thu, 21 Nov 2013 03:33:02 +0000 (22:33 -0500)
Open-ILS/src/Android/AndroidManifest.xml
Open-ILS/src/Android/res/values/ou.xml
Open-ILS/src/Android/res/xml/authenticator.xml
Open-ILS/src/Android/src/org/evergreen/android/JunkActivity.java
Open-ILS/src/Android/src/org/evergreen/android/accountAccess/AccountAccess.java
Open-ILS/src/Android/src/org/evergreen/android/accountAccess/CurrentLogin.java [deleted file]
Open-ILS/src/Android/src/org/evergreen/android/accountAccess/LoginController.java [new file with mode: 0644]
Open-ILS/src/Android/src/org/evergreen/android/globals/GlobalConfigs.java
Open-ILS/src/Android/src/org/evergreen/android/searchCatalog/SearchCatalog.java
Open-ILS/src/Android/src/org/evergreen/android/views/StartupActivity.java
Open-ILS/src/Android/src/org/evergreen_ils/auth/Const.java

index 15cadea..76ed1d4 100644 (file)
         </activity>\r
         <activity\r
             android:name="org.evergreen.android.views.StartupActivity"\r
-            android:label="@string/ou_app_label"\r
+            android:label="@string/ou_app_name"\r
             android:windowSoftInputMode="adjustResize|stateVisible" >\r
             <intent-filter>\r
                 <action android:name="android.intent.action.MAIN" />\r
index 268c2be..d1e759c 100644 (file)
@@ -3,8 +3,9 @@
 <resources>
     <!--  TODO: make this HTTPS -->
     <string name="ou_gateway_url">http://bark.cwmars.org/osrf-gateway-v1</string>
-    <string name="ou_app_name">C/W Mars Library Rover</string>
+    <string name="ou_app_name">C/W Mars Library</string>
     <string name="ou_app_label">C/W Mars</string>
+    <string name="ou_app_welcome_label">Welcome</string>
     <string name="ou_account_label">C/W Mars Library</string>
     <string name="ou_account_sign_in_message">Sign in to your\nC/W Mars Library Account</string>
 </resources>
index f724c41..ff954f3 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
-                       android:accountType="org.evergreen_ils.opac"
+                       android:accountType="org.evergreen_ils"
                        android:icon="@drawable/evergreen_launcher_icon"
                        android:smallIcon="@drawable/evergreen_launcher_icon"
                        android:label="@string/ou_account_label"/>
index b45c2cb..e218eee 100644 (file)
@@ -1,6 +1,6 @@
 package org.evergreen.android;
 
-import org.evergreen.android.accountAccess.CurrentLogin;
+import org.evergreen.android.accountAccess.LoginController;
 
 import android.app.Activity;
 import android.os.Bundle;
@@ -12,8 +12,8 @@ public class JunkActivity extends Activity {
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         TextView tv = new TextView(this);
-        String account_name = CurrentLogin.getUsername();
-        String auth_token = CurrentLogin.getAuthToken();
+        String account_name = LoginController.getInstance(this).getAccountName();
+        String auth_token = LoginController.getInstance(this).getAuthToken();
         tv.setText("account.name="+((account_name==null)?"null":account_name)
                 + "\nauth_token="+((auth_token==null)?"null":auth_token));
         setContentView(tv);
index 232fe03..8bba018 100644 (file)
@@ -49,9 +49,6 @@ import android.util.Log;
  */
 public class AccountAccess {
 
-    // Used for authentication purpose
-
-    /** The SERVICE. */
     public static String SERVICE_AUTH = "open-ils.auth";
 
     /** The METHOD Auth init. */
@@ -63,21 +60,10 @@ public class AccountAccess {
     /** The METHOD Auth session retrieve. */
     public static String METHOD_AUTH_SESSION_RETRV = "open-ils.auth.session.retrieve";
 
-    // Used for the Checked out Items Tab
-
-    /** The SERVIC e_ actor. */
     public static String SERVICE_ACTOR = "open-ils.actor";
-
-    /** The SERVIC e_ circ. */
     public static String SERVICE_CIRC = "open-ils.circ";
-
-    /** The SERVIC e_ search. */
     public static String SERVICE_SEARCH = "open-ils.search";
-
-    /** The SERVIC e_ serial. */
     public static String SERVICE_SERIAL = "open-ils.serial";
-
-    /** The SERVIC e_ fielder. */
     public static String SERVICE_FIELDER = "open-ils.fielder";
 
     /** The METHOD_FETCH_CHECKED_OUT_SUM description : for a given user returns a a structure of circulation objects sorted by out, overdue, lost, claims_returned, long_overdue; A list of ID's returned for each type : "out":[id1,id2,...] @returns: { "out":[id 's],"claims_returned":[],"long_overdue":[],"overdue":[],"lost":[] } */
diff --git a/Open-ILS/src/Android/src/org/evergreen/android/accountAccess/CurrentLogin.java b/Open-ILS/src/Android/src/org/evergreen/android/accountAccess/CurrentLogin.java
deleted file mode 100644 (file)
index 37d65ec..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-package org.evergreen.android.accountAccess;
-
-/**
- * data representing the current logged-in user 
- * @author kenstir
- */
-public class CurrentLogin {
-    
-    protected static CurrentLogin mInstance = null;
-    
-    protected String mUsername = null;
-    protected String mAuthToken = null; 
-
-    protected CurrentLogin() {
-    }
-
-    protected static CurrentLogin getInstance() {
-        if (mInstance == null)
-            mInstance = new CurrentLogin();
-        return mInstance;
-    }
-    
-    public static void clear() {
-        mInstance = null;
-    }
-
-    public static void setAccountInfo(String username, String auth_token) {
-        getInstance().mUsername = username;
-        getInstance().mAuthToken = auth_token;
-    }
-
-    public static String getUsername() {
-        return getInstance().mUsername;
-    }
-
-    public static String getAuthToken() {
-        return getInstance().mAuthToken;
-    }
-}
diff --git a/Open-ILS/src/Android/src/org/evergreen/android/accountAccess/LoginController.java b/Open-ILS/src/Android/src/org/evergreen/android/accountAccess/LoginController.java
new file mode 100644 (file)
index 0000000..9e1c18c
--- /dev/null
@@ -0,0 +1,335 @@
+package org.evergreen.android.accountAccess;
+
+import org.evergreen.android.JunkActivity;
+import org.evergreen.android.R;
+import org.evergreen.android.globals.AppPreferences;
+import org.evergreen.android.views.StartupActivity;
+import org.evergreen_ils.auth.Const;
+
+import android.accounts.Account;
+import android.accounts.AccountManager;
+import android.accounts.AccountManagerCallback;
+import android.accounts.AccountManagerFuture;
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.os.AsyncTask;
+import android.os.Build;
+import android.os.Bundle;
+import android.text.TextUtils;
+import android.util.Log;
+import android.view.ContextThemeWrapper;
+import android.widget.Toast;
+
+/**
+ * Handle common Activity startup: making sure we have an account, and an auth_token.
+ * 
+ * @author kenstir
+ *
+ */
+public class LoginController {
+    
+    protected static LoginController mInstance = null;
+
+    private String TAG = LoginController.class.getSimpleName();
+    protected String mAccountName = null;
+    protected String mAuthToken = null; 
+    protected StartupTask mStartupTask = null;
+    private Activity mActivity = null;
+    private Class<?> mNextActivityClass = null;
+    private AppPreferences mAppPrefs = null;
+    private AccountManager mAccountManager;
+
+    protected LoginController() {
+    }
+    
+    protected void setActivity(Activity a) {
+        mActivity = a;
+        if (mAppPrefs == null) mAppPrefs = new AppPreferences(a);
+        if (mAccountManager == null) mAccountManager = AccountManager.get(a);
+    }
+    
+    public static LoginController getInstance(Activity a) {
+        if (mInstance == null)
+            mInstance = new LoginController();
+        mInstance.setActivity(a);
+        return mInstance;
+    }
+    
+    public static LoginController getInstance() {
+        return mInstance;
+    }
+
+    public static String getAccountName() {
+        return getInstance().mAccountName;
+    }
+
+    public static String getAuthToken() {
+        return getInstance().mAuthToken;
+    }
+
+    /** login and stay on the current activity, for use by non-startup activity */
+    public void login() {
+        loginForActivity(null);
+    }
+
+    /** login and forward to another activity, for use by startup/splash activity */
+    public void loginForActivity(Class nextActivity) {
+        mNextActivityClass = nextActivity;
+
+        final String auth_token = getAuthToken();
+        if (TextUtils.isEmpty(auth_token)) {
+            getTokenForLastActiveAccount();
+        } else {
+            Log.d(TAG, "signIn> already have auth_token");
+            startNextActivity();
+        }
+    }
+    
+    private void getTokenForLastActiveAccount() {
+        final String username = mAppPrefs.getLastAccountName();
+        Log.d(TAG, "getToken> username="+username);
+        
+        // first try to get an auth token for the last account used
+        if (!TextUtils.isEmpty(username)) {
+            if (reuseExistingAccountAuthToken(username)) {
+                Log.d(TAG, "getToken> reuseExisting returned true");
+            }
+        } else {
+            getTokenForAccountCreateIfNeeded();
+        }
+    }
+
+    /**
+     * Add new account to the account manager
+     */
+    private void addNewAccount() {
+        //final AccountManagerFuture<Bundle> future = 
+        mAccountManager.addAccount(Const.ACCOUNT_TYPE, Const.AUTHTOKEN_TYPE, null, null, mActivity, new AccountManagerCallback<Bundle>() {
+            @Override
+            public void run(AccountManagerFuture<Bundle> future) {
+                try {
+                    Bundle bnd = future.getResult();
+                    final String account_name = bnd.getString(AccountManager.KEY_ACCOUNT_NAME);
+                    showMessage("Account "+account_name+" was created");
+                    mAppPrefs.putLastAccountName(account_name);
+                    Log.d(TAG, "addNewAccount bnd=" + bnd);
+                } catch (Exception e) {
+                    mAppPrefs.clearLastAccountName();
+                    e.printStackTrace();
+                    showMessage(e.getMessage());
+                }
+            }
+        }, null);
+    }
+
+    /**
+     * Show all the accounts registered on the account manager. Request an auth token upon user select.
+     * @param authTokenType
+     */
+    private void showAccountPicker() {
+        final Account availableAccounts[] = mAccountManager.getAccountsByType(Const.ACCOUNT_TYPE);
+
+        if (availableAccounts.length == 0) {
+            Toast.makeText(mActivity, "No accounts", Toast.LENGTH_SHORT).show();
+            addNewAccount();
+        } else {
+            String name[] = new String[availableAccounts.length];
+            for (int i = 0; i < availableAccounts.length; i++) {
+                name[i] = availableAccounts[i].name;
+            }
+
+            // Account picker
+            AlertDialog.Builder builder;
+            if (Build.VERSION.SDK_INT >= 1/*Build.VERSION_CODES.HONEYCOMB*/) {
+                builder = new AlertDialog.Builder(mActivity);
+            } else {
+                ContextThemeWrapper wrapper = new ContextThemeWrapper(mActivity, R.style.EvergreenTheme);
+                builder = new AlertDialog.Builder(wrapper);
+            }
+            AlertDialog aDialog = builder.setTitle("Pick Account").setItems(name, new DialogInterface.OnClickListener() {
+                @Override
+                public void onClick(DialogInterface dialog, int which) {
+                    getExistingAccountAuthToken(availableAccounts[which]);
+                }
+            }).create();
+            aDialog.show();
+        }
+    }
+
+    private void getExistingAccountAuthToken(final Account account) {
+        final AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(account, Const.AUTHTOKEN_TYPE, null, mActivity, 
+                new AccountManagerCallback<Bundle>() {
+            @Override
+            public void run(AccountManagerFuture<Bundle> future) {
+                Log.d(TAG, "getExistingAccountAuthToken> callback run> got future "+future);
+            }
+        }, null);
+
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    Bundle bnd = future.getResult();
+                    Log.d(TAG, "getExistingAccountAuthToken> thread run> got future "+future);
+                    final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
+                    final String account_name = bnd.getString(AccountManager.KEY_ACCOUNT_NAME);
+                    onSuccessfulLogin(account_name, authtoken);
+                } catch (Exception e) {
+                    Log.d(TAG, "getExistingAccountAuthToken caught "+e.getMessage());
+                    onFailedLogin(e.getMessage());
+                }
+            }
+        }).start();
+    }
+
+    protected void onSuccessfulLogin(String account_name, String auth_token) {
+        Log.d(TAG,"onSuccessfulLogin> account_name "+account_name+" token "+auth_token);
+        showMessage((auth_token != null) ? "SUCCESS with "+account_name+"\ntoken: " + auth_token : "FAIL");
+        if (auth_token != null) {
+            mAppPrefs.putLastAccountName(account_name);
+            mAccountName = account_name;
+            mAuthToken = auth_token;
+            startNextActivity();
+        }
+    }
+    
+    protected void onFailedLogin(String msg) {
+        mAppPrefs.clearLastAccountName();
+        mAccountName = null;
+        mAuthToken = null;
+        showMessage(msg);
+    }
+
+    private boolean reuseExistingAccountAuthToken(final String account_name) {
+        final Account availableAccounts[] = mAccountManager.getAccountsByType(Const.ACCOUNT_TYPE);
+        for (int i = 0; i < availableAccounts.length; i++) {
+            Log.d(TAG, "reuseExistingAccountAuthToken> looking for "+account_name+", found "+availableAccounts[i].name);
+            if (account_name.equals(availableAccounts[i].name)) {
+                Log.d(TAG, "reuseExistingAccountAuthToken> found it");
+                getExistingAccountAuthToken(availableAccounts[i]);
+                return true;
+            }
+        }
+        return false;
+    }
+    
+    /**
+     * Invalidates the auth token for the account
+     * @param account
+     * @param authTokenType
+     */
+    private void invalidateAuthToken(final Account account) {
+        final AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(account, Const.AUTHTOKEN_TYPE, null, mActivity, null, null);
+
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    Bundle bnd = future.getResult();
+
+                    final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
+                    mAccountManager.invalidateAuthToken(account.type, authtoken);
+                    showMessage(account.name + " invalidated");
+                    mAppPrefs.putLastAccountName(null);
+                } catch (Exception e) {
+                    mAppPrefs.putLastAccountName(null);
+                    e.printStackTrace();
+                    showMessage(e.getMessage());
+                }
+            }
+        }).start();
+    }
+
+    /**
+     * Get an auth token for the account.
+     * If not exist - add it and then return its auth token.
+     * If one exist - return its auth token.
+     * If more than one exists - show a picker and return the select account's auth token.
+     * @param accountType
+     * @param authTokenType
+     */
+    private void getTokenForAccountCreateIfNeeded() {
+        Log.d(TAG, "getTokenForAccountCreateIfNeeded> ");
+        final AccountManagerFuture<Bundle> future = mAccountManager.getAuthTokenByFeatures(Const.ACCOUNT_TYPE, Const.AUTHTOKEN_TYPE, null, mActivity, null, null,
+                new AccountManagerCallback<Bundle>() {
+                    @Override
+                    public void run(AccountManagerFuture<Bundle> future) {
+                        Bundle bnd = null;
+                        try {
+                            bnd = future.getResult();
+                            Log.d(TAG, "getTokenForAccountCreateIfNeeded> bnd="+bnd);
+                            final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
+                            final String account_name = bnd.getString(AccountManager.KEY_ACCOUNT_NAME);
+                            onSuccessfulLogin(account_name, authtoken);
+                        } catch (Exception e) {
+                            Log.d(TAG, "getTokenForAccountCreateIfNeeded> caught "+e.getMessage());
+                            onFailedLogin(e.getMessage());
+                        }
+                    }
+                }
+        , null);
+    }
+
+    private void showMessage(final String msg) {
+        if (TextUtils.isEmpty(msg))
+            return;
+
+        mActivity.runOnUiThread(new Runnable() {
+            @Override
+            public void run() {
+                Log.d(TAG, "showMessage> "+msg);
+                Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show();
+            }
+        });
+    }
+
+    private void startNextActivity() {
+        //Intent intent = new Intent(this, SearchCatalogListView.class);
+        Intent intent = new Intent(mActivity, mNextActivityClass);
+        mActivity.startActivity(intent);
+        mActivity.finish();
+    }
+
+    public void downloadResources() {
+        if (mStartupTask != null) {
+            return;
+        }
+        
+        // blah blah blah
+        // mDownloadTask = new LoginTask();
+    }
+
+    private class StartupTask extends AsyncTask<Void, Void, Boolean> {
+        @Override
+        protected Boolean doInBackground(Void... params) {
+            try {
+                // TODO
+                Thread.sleep(2000);
+            } catch (InterruptedException e) {
+                return false;
+            }
+
+            return true;
+        }
+        
+        @Override
+        protected void onProgressUpdate(Void... params) {
+        }
+
+        @Override
+        protected void onPostExecute(final Boolean success) {
+            mStartupTask = null;
+            if (success) {
+                startNextActivity();
+            }
+        }
+
+        @Override
+        protected void onCancelled() {
+            mStartupTask = null;
+        }
+    }
+}
index 1e15946..f5d4cb1 100644 (file)
@@ -100,46 +100,14 @@ public class GlobalConfigs {
      * Initialize function that retrieves IDL file and Orgs file
      */
     private boolean initialize(Context context) {
-
         if (init == false) {
-
-            init = true;
-            SharedPreferences preferences = PreferenceManager
-                    .getDefaultSharedPreferences(context);
-            GlobalConfigs.httpAddress = preferences
-                    .getString("library_url", "");
-            boolean noNetworkAccess = false;
-            System.out.println("Check for network conenctivity");
-            try {
-                Utils.checkNetworkStatus((ConnectivityManager) context
+            // TODO do this in AsyncTask
+            loadIDLFile(context);
+            getOrganisations();
+            getCopyStatusesAvailable((ConnectivityManager) context
                         .getSystemService(Context.CONNECTIVITY_SERVICE));
-
-            } catch (NoNetworkAccessException e) {
-                noNetworkAccess = true;
-            } catch (NoAccessToServer e) {
-
-                System.out.println("No access to network");
-                Intent preferencesAnctivity = new Intent(context,
-                        ApplicationPreferences.class);
-                context.startActivity(preferencesAnctivity);
-
-                noNetworkAccess = true;
-
-            }
-            if (!noNetworkAccess) {
-                loadIDLFile(context);
-                getOrganisations();
-
-                getCopyStatusesAvailable((ConnectivityManager) context
-                        .getSystemService(Context.CONNECTIVITY_SERVICE));
-
-                AccountAccess.setAccountInfo(
-                        preferences.getString("username", ""),
-                        preferences.getString("password", ""));
-
-                return true;
-            }
-            return false;
+            init = true;
+            return true;
         }
         return false;
     }
index e50b2a9..8c8b5b5 100644 (file)
@@ -328,7 +328,6 @@ public class SearchCatalog {
             for (int i = 0; i < ccs_list.size(); i++) {
                 OSRFObject ccs_obj = ccs_list.get(i);
                 if (ccs_obj.getString("opac_visible").equals("t")) {
-
                     CopyInformation.availableOrgStatuses.put(
                             ccs_obj.getInt("id") + "",
                             ccs_obj.getString("name"));
index 78a91bd..47f41df 100644 (file)
@@ -1,7 +1,7 @@
 package org.evergreen.android.views;
 
 import org.evergreen.android.R;
-import org.evergreen.android.accountAccess.CurrentLogin;
+import org.evergreen.android.accountAccess.LoginController;
 import org.evergreen.android.globals.AppPreferences;
 import org.evergreen.android.searchCatalog.SearchCatalogListView;
 import org.evergreen.android.JunkActivity;
@@ -32,46 +32,16 @@ public class StartupActivity extends Activity {
 
     private String TAG = StartupActivity.class.getSimpleName();
     private TextView mLoginStatusMessageView;
-    private StartupTask mStartupTask = null;
     private String mAlertMessage = null;
-    private AppPreferences mAppPrefs;
-    private AccountManager mAccountManager;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_startup);
         
-        mAppPrefs = new AppPreferences(this);
-        mAccountManager = AccountManager.get(this);
-        
         mLoginStatusMessageView = (TextView) findViewById(R.id.login_status_message);
         
-        signIn();
-    }
-    
-    public void signIn() {
-        final String auth_token = CurrentLogin.getAuthToken();
-        if (TextUtils.isEmpty(auth_token)) {
-            getTokenForLastActiveAccount();
-        } else {
-            Log.d(TAG, "signIn> already have auth_token");
-            initializeAccountInfo();
-        }
-    }
-
-    private void getTokenForLastActiveAccount() {
-        final String username = mAppPrefs.getLastAccountName();
-        Log.d(TAG, "getToken> username="+username);
-        
-        // first try to get an auth token for the last account used
-        if (!TextUtils.isEmpty(username)) {
-            if (reuseExistingAccountAuthToken(username)) {
-                Log.d(TAG, "getToken> reuseExisting returned true");
-            }
-        } else {
-            getTokenForAccountCreateIfNeeded();
-        }
+        LoginController.getInstance(this).loginForActivity(JunkActivity.class);
     }
 
     @Override
@@ -87,233 +57,4 @@ public class StartupActivity extends Activity {
         return true;
     }
     */
-
-    /**
-     * Add new account to the account manager
-     */
-    private void addNewAccount() {
-        //final AccountManagerFuture<Bundle> future = 
-        mAccountManager.addAccount(Const.ACCOUNT_TYPE, Const.AUTHTOKEN_TYPE, null, null, this, new AccountManagerCallback<Bundle>() {
-            @Override
-            public void run(AccountManagerFuture<Bundle> future) {
-                try {
-                    Bundle bnd = future.getResult();
-                    final String account_name = bnd.getString(AccountManager.KEY_ACCOUNT_NAME);
-                    showMessage("Account "+account_name+" was created");
-                    mAppPrefs.putLastAccountName(account_name);
-                    Log.d(TAG, "addNewAccount bnd=" + bnd);
-                } catch (Exception e) {
-                    mAppPrefs.clearLastAccountName();
-                    e.printStackTrace();
-                    showMessage(e.getMessage());
-                }
-            }
-        }, null);
-    }
-
-    /**
-     * Show all the accounts registered on the account manager. Request an auth token upon user select.
-     * @param authTokenType
-     */
-    private void showAccountPicker() {
-        final Account availableAccounts[] = mAccountManager.getAccountsByType(Const.ACCOUNT_TYPE);
-
-        if (availableAccounts.length == 0) {
-            Toast.makeText(this, "No accounts", Toast.LENGTH_SHORT).show();
-            addNewAccount();
-        } else {
-            String name[] = new String[availableAccounts.length];
-            for (int i = 0; i < availableAccounts.length; i++) {
-                name[i] = availableAccounts[i].name;
-            }
-
-            // Account picker
-            AlertDialog.Builder builder;
-            if (Build.VERSION.SDK_INT >= 1/*Build.VERSION_CODES.HONEYCOMB*/) {
-                builder = new AlertDialog.Builder(this);
-            } else {
-                ContextThemeWrapper wrapper = new ContextThemeWrapper(this, R.style.EvergreenTheme);
-                builder = new AlertDialog.Builder(wrapper);
-            }
-            AlertDialog aDialog = builder.setTitle("Pick Account").setItems(name, new DialogInterface.OnClickListener() {
-                @Override
-                public void onClick(DialogInterface dialog, int which) {
-                    getExistingAccountAuthToken(availableAccounts[which]);
-                }
-            }).create();
-            aDialog.show();
-        }
-    }
-
-    private void getExistingAccountAuthToken(final Account account) {
-        final AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(account, Const.AUTHTOKEN_TYPE, null, this, 
-                new AccountManagerCallback<Bundle>() {
-            @Override
-            public void run(AccountManagerFuture<Bundle> future) {
-                Log.d(TAG, "getExistingAccountAuthToken> callback run> got future "+future);
-            }
-        }, null);
-
-        new Thread(new Runnable() {
-            @Override
-            public void run() {
-                try {
-                    Bundle bnd = future.getResult();
-                    Log.d(TAG, "getExistingAccountAuthToken> thread run> got future "+future);
-                    final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
-                    final String account_name = bnd.getString(AccountManager.KEY_ACCOUNT_NAME);
-                    onSuccessfulLogin(account_name, authtoken);
-                } catch (Exception e) {
-                    Log.d(TAG, "getExistingAccountAuthToken caught "+e.getMessage());
-                    onFailedLogin(e.getMessage());
-                }
-            }
-        }).start();
-    }
-
-    protected void onSuccessfulLogin(String account_name, String auth_token) {
-        Log.d(TAG,"onSuccessfulLogin> account_name "+account_name+" token "+auth_token);
-        showMessage((auth_token != null) ? "SUCCESS with "+account_name+"\ntoken: " + auth_token : "FAIL");
-        if (auth_token != null) {
-            mAppPrefs.putLastAccountName(account_name);
-            CurrentLogin.setAccountInfo(account_name, auth_token);
-            startNextActivity();
-        }
-    }
-    
-    protected void onFailedLogin(String msg) {
-        mAppPrefs.clearLastAccountName();
-        CurrentLogin.clear();
-        showMessage(msg);
-    }
-
-    private boolean reuseExistingAccountAuthToken(final String account_name) {
-        final Account availableAccounts[] = mAccountManager.getAccountsByType(Const.ACCOUNT_TYPE);
-        for (int i = 0; i < availableAccounts.length; i++) {
-            Log.d(TAG, "reuseExistingAccountAuthToken> looking for "+account_name+", found "+availableAccounts[i].name);
-            if (account_name.equals(availableAccounts[i].name)) {
-                Log.d(TAG, "reuseExistingAccountAuthToken> found it");
-                getExistingAccountAuthToken(availableAccounts[i]);
-                return true;
-            }
-        }
-        return false;
-    }
-    
-    /**
-     * Invalidates the auth token for the account
-     * @param account
-     * @param authTokenType
-     */
-    private void invalidateAuthToken(final Account account) {
-        final AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(account, Const.AUTHTOKEN_TYPE, null, this, null,null);
-
-        new Thread(new Runnable() {
-            @Override
-            public void run() {
-                try {
-                    Bundle bnd = future.getResult();
-
-                    final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
-                    mAccountManager.invalidateAuthToken(account.type, authtoken);
-                    showMessage(account.name + " invalidated");
-                    mAppPrefs.putLastAccountName(null);
-                } catch (Exception e) {
-                    mAppPrefs.putLastAccountName(null);
-                    e.printStackTrace();
-                    showMessage(e.getMessage());
-                }
-            }
-        }).start();
-    }
-
-    /**
-     * Get an auth token for the account.
-     * If not exist - add it and then return its auth token.
-     * If one exist - return its auth token.
-     * If more than one exists - show a picker and return the select account's auth token.
-     * @param accountType
-     * @param authTokenType
-     */
-    private void getTokenForAccountCreateIfNeeded() {
-        Log.d(TAG, "getTokenForAccountCreateIfNeeded> ");
-        final AccountManagerFuture<Bundle> future = mAccountManager.getAuthTokenByFeatures(Const.ACCOUNT_TYPE, Const.AUTHTOKEN_TYPE, null, this, null, null,
-                new AccountManagerCallback<Bundle>() {
-                    @Override
-                    public void run(AccountManagerFuture<Bundle> future) {
-                        Bundle bnd = null;
-                        try {
-                            bnd = future.getResult();
-                            Log.d(TAG, "getTokenForAccountCreateIfNeeded> bnd="+bnd);
-                            final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
-                            final String account_name = bnd.getString(AccountManager.KEY_ACCOUNT_NAME);
-                            onSuccessfulLogin(account_name, authtoken);
-                        } catch (Exception e) {
-                            Log.d(TAG, "getTokenForAccountCreateIfNeeded> caught "+e.getMessage());
-                            onFailedLogin(e.getMessage());
-                        }
-                    }
-                }
-        , null);
-    }
-
-    private void showMessage(final String msg) {
-        if (TextUtils.isEmpty(msg))
-            return;
-
-        runOnUiThread(new Runnable() {
-            @Override
-            public void run() {
-                Log.d(TAG, "showMessage> "+msg);
-                Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
-            }
-        });
-    }
-
-    private void startNextActivity() {
-        //Intent intent = new Intent(this, SearchCatalogListView.class);
-        Intent intent = new Intent(this, JunkActivity.class);
-        startActivity(intent);
-        finish();
-    }
-
-    public void downloadResources() {
-        if (mStartupTask != null) {
-            return;
-        }
-        
-        // blah blah blah
-        // mDownloadTask = new LoginTask();
-    }
-
-    private class StartupTask extends AsyncTask<Void, Void, Boolean> {
-        @Override
-        protected Boolean doInBackground(Void... params) {
-            try {
-                // TODO
-                Thread.sleep(2000);
-            } catch (InterruptedException e) {
-                return false;
-            }
-
-            return true;
-        }
-        
-        @Override
-        protected void onProgressUpdate(Void... params) {
-        }
-
-        @Override
-        protected void onPostExecute(final Boolean success) {
-            mStartupTask = null;
-            if (success) {
-                startNextActivity();
-            }
-        }
-
-        @Override
-        protected void onCancelled() {
-            mStartupTask = null;
-        }
-    }
 }
index 6df2e20..9b9882e 100644 (file)
@@ -1,7 +1,7 @@
 package org.evergreen_ils.auth;
 
 public class Const {
-    public static final String ACCOUNT_TYPE = "org.evergreen_ils.opac";
+    public static final String ACCOUNT_TYPE = "org.evergreen_ils";
     public static final String AUTHTOKEN_TYPE = "opac";
     public static final String AUTHTOKEN_TYPE_LABEL = "Online Public Access Catalog";
 }