android:layout_below="@+id/logo"
android:paddingTop="40dip" />
+ <Button
+ android:id="@+id/activity_splash_retry_button"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_below="@+id/activity_splash_progress_bar"
+ android:layout_centerHorizontal="true"
+ android:layout_marginTop="60dp"
+ android:text="@string/retry_label"
+ android:visibility="gone"
+ />
+
</RelativeLayout>
\ No newline at end of file
<string name="title_activity_junk">JunkActivity</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
+ <string name="retry_label">Retry</string>
</resources>
private String httpAddress = "http://ulysses.calvin.edu";
/** The TAG. */
- public String TAG = "AccountAccess";
+ private String TAG = "AccountAccess";
/**
* The auth token. Sent with every request that needs authentication
* */
- public String authToken = null;
+ private String authToken = null;
/** The cm. */
private ConnectivityManager cm = null;
return accountAccess;
}
- public static void setAccountName(String username) {
- userName = username;
- }
-
public Integer getHomeLibraryID() {
return homeLibraryID;
}
/**
* Retrieve session.
+ * @throws NoAccessToServer
+ * @throws NoNetworkAccessException
+ * @throws SessionNotFoundException
*/
- public boolean initSession() {
-
- if (this.haveSession)
- return true;
+ public boolean initSession(String auth_token) throws NoNetworkAccessException, NoAccessToServer, SessionNotFoundException {
+ if (this.haveSession && this.authToken.equals(auth_token))
+ return true;
+ this.haveSession = false;
+ this.authToken = auth_token;
+
+ Object resp = Utils.doRequest(conn, SERVICE_AUTH, METHOD_AUTH_SESSION_RETRV, authToken, null, new Object[] {authToken});
+ if (resp != null) {
+ OSRFObject au = (OSRFObject) resp;
+ userID = au.getInt("id");
+ homeLibraryID = au.getInt("home_ou");
+ userName = au.getString("usrname");
+ //email = au.getString("email");
+ this.haveSession = true;
+ }
+ return this.haveSession;
+ /*
Method method = new Method(METHOD_AUTH_SESSION_RETRV);
method.addParam(authToken);
userID = au.getInt("id");
homeLibraryID = au.getInt("home_ou");
String s = au.getString("usrname");
- //may be interesting: usrname=coxken, email=kenstir@gmail.com
System.out.println("User Id " + userID);
this.haveSession = true;
}
return this.haveSession;
+ */
}
// ------------------------Checked Out Items Section
} catch (Exception e) {
//System.err.println(e.getMessage());
}
-
if (textcode != null) {
if (textcode.equals("NO_SESSION")) {
Log.d(TAG, textcode);
*/
package org.evergreen.android.views.splashscreen;
+import java.security.PublicKey;
+
import org.evergreen.android.accountAccess.AccountAccess;
import org.evergreen.android.accountAccess.SessionNotFoundException;
import org.evergreen.android.globals.GlobalConfigs;
import org.evergreen.android.globals.NoNetworkAccessException;
import org.evergreen_ils.auth.Const;
+import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.widget.ProgressBar;
import android.widget.TextView;
-public class LoadingTask extends AsyncTask<String, String, String> {
+/** This is basically the same as an AsyncTask<String,String,String>, except that it uses
+ * a Thread. Starting with HONEYCOMB, tasks are executed on a single thread and the 2nd
+ * AsyncTask doesn't start until the first finishes.
+ *
+ * @author kenstir
+ *
+ */
+public class LoadingTask {
private String TAG = "LoadingTask";
public static final String TASK_OK = "OK";
this.mCallingActivity = callingActivity;
mAccountManager = AccountManager.get(callingActivity);
}
-
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- mListener.onPreExecute();
+
+ public void execute() {
+ Log.d(TAG, "execute>");
+ final Thread t = new Thread(new Runnable() {
+ @Override
+ public void run() {
+ final String result = doInBackground();
+ mCallingActivity.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ onPostExecute(result);
+ }
+ });
+ }
+ }, TAG);
+ onPreExecute();
+ t.start();
}
- @Override
- protected String doInBackground(String... params) {
+ protected void publishProgress(final String progress) {
+ mCallingActivity.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ onProgressUpdate(progress);
+ }
+ });
+ }
+
+ protected String doInBackground() {
+ final String tag ="doInBackground> ";
+ Log.d(TAG, tag);
try {
- Log.d(TAG, "Loading resources");
+ Log.d(TAG, tag+"Loading resources");
publishProgress("Loading resources");
- GlobalConfigs gl = GlobalConfigs.getGlobalConfigs(mCallingActivity);
+ GlobalConfigs.getGlobalConfigs(mCallingActivity); // loads IDL
- Log.d(TAG, "Signing in");
+ Log.d(TAG, tag+"Signing in");
publishProgress("Signing in");
- final AccountManagerFuture<Bundle> future = mAccountManager.getAuthTokenByFeatures(Const.ACCOUNT_TYPE, Const.AUTHTOKEN_TYPE, null, mCallingActivity, null, null, null, null);
+ AccountManagerFuture<Bundle> future = mAccountManager.getAuthTokenByFeatures(Const.ACCOUNT_TYPE, Const.AUTHTOKEN_TYPE, null, mCallingActivity, null, null, null, null);
Bundle bnd = future.getResult();
- Log.d(TAG, "bnd="+bnd);
- final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
- final String account_name = bnd.getString(AccountManager.KEY_ACCOUNT_NAME);
- Log.d(TAG, "account_name="+account_name+" token="+authtoken);
- //onSuccessfulLogin(account_name, authtoken);
+ Log.d(TAG, tag+"bnd="+bnd);
+ String auth_token = bnd.getString(AccountManager.KEY_AUTHTOKEN);
+ String account_name = bnd.getString(AccountManager.KEY_ACCOUNT_NAME);
+ Log.d(TAG, tag+"account_name="+account_name+" token="+auth_token);
if (account_name == null)
return "no account";
- Log.d(TAG, "Starting session");
+ Log.d(TAG, tag+"Starting session");
publishProgress("Starting session");
AccountAccess ac = AccountAccess.getAccountAccess(GlobalConfigs.httpAddress);
- if (!ac.initSession())
+
+ // auth token zen: try once and if it fails, invalidate the token and try again
+ boolean haveSession = false;
+ boolean retry = false;
+ try {
+ haveSession = ac.initSession(auth_token);
+ } catch (SessionNotFoundException e) {
+ mAccountManager.invalidateAuthToken(Const.ACCOUNT_TYPE, auth_token);
+ retry = true;
+ }
+ if (retry) {
+ final Account account = new Account(account_name, Const.ACCOUNT_TYPE);
+ future = mAccountManager.getAuthToken(account, Const.AUTHTOKEN_TYPE, null, mCallingActivity, null, null);
+ bnd = future.getResult();
+ Log.d(TAG, tag+"bnd="+bnd);
+ auth_token = bnd.getString(AccountManager.KEY_AUTHTOKEN);
+ account_name = bnd.getString(AccountManager.KEY_ACCOUNT_NAME);
+ Log.d(TAG, tag+"account_name="+account_name+" token="+auth_token);
+ if (account_name == null)
+ return "no account";
+ haveSession = ac.initSession(auth_token);
+ }
+ if (!haveSession)
return "no session";
- Log.d(TAG, "Retrieving bookbags");
+ Log.d(TAG, tag+"Retrieving bookbags");
publishProgress("Retrieving bookbags");
ac.retrieveBookbags();
return TASK_OK;
} catch (Exception e) {
- Log.d(TAG, "Caught exception", e);
- return e.getMessage();
+ Log.d(TAG, tag+"Caught exception", e);
+ String s = e.getMessage();
+ if (s == null) s = "Cancelled";
+ Log.d(TAG, tag+"returning "+s);
+ return s;
}
}
- @Override
- protected void onProgressUpdate(String... values) {
- super.onProgressUpdate(values);
- mListener.onProgressUpdate(values[0]);
+ protected void onPreExecute() {
+ Log.d(TAG, "onPreExecute> ");
+ mListener.onPreExecute();
}
- @Override
+ protected void onProgressUpdate(String s) {
+ Log.d(TAG, "onProgressUpdate> "+s);
+ mListener.onProgressUpdate(s);
+ }
+
protected void onPostExecute(String result) {
- super.onPostExecute(result);
+ Log.d(TAG, "onPostExecute> "+result);
mListener.onPostExecute(result);
}
-}
\ No newline at end of file
+}
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class SplashActivity extends Activity implements LoadingTaskListener {
- private TextView progressText;
- private Context context;
- private ProgressBar progressBar;
+ private TextView mProgressText;
+ private Context mContext;
+ private ProgressBar mProgressBar;
private String TAG = "SplashActivity";
- private AlertDialog alertDialog;
- private SharedPreferences prefs;
- private LoadingTask task;
+ private AlertDialog mAlertDialog;
+ private Button mRetryButton;
+ //private SharedPreferences prefs;
+ private LoadingTask mTask;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
- this.context = this;
+ this.mContext = this;
- progressText = (TextView) findViewById(R.id.action_in_progress);
- progressBar = (ProgressBar) findViewById(R.id.activity_splash_progress_bar);
+ mProgressText = (TextView) findViewById(R.id.action_in_progress);
+ mProgressBar = (ProgressBar) findViewById(R.id.activity_splash_progress_bar);
+ mRetryButton = (Button) findViewById(R.id.activity_splash_retry_button);
+ Log.d(TAG, "onCreate>");
+ mRetryButton.setOnClickListener(new OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ startTask();
+ }
+ });
- prefs= PreferenceManager.getDefaultSharedPreferences(context);
- GlobalConfigs.httpAddress = context.getString(R.string.ou_library_url);
- String username = prefs.getString("username", "");
- AccountAccess.setAccountName(username);
-
- task = new LoadingTask(this, this);
- task.execute(new String());
+ GlobalConfigs.httpAddress = mContext.getString(R.string.ou_library_url);
+ //prefs= PreferenceManager.getDefaultSharedPreferences(context);
+ //String username = prefs.getString("username", "");
+ startTask();
+ }
+
+ protected void startTask() {
+ Log.d(TAG, "startTask> task="+mTask);
+ if (mTask != null)
+ return;
+ mTask = new LoadingTask(this, this);
+ mTask.execute();
}
@Override
protected void onStop() {
- // TODO Auto-generated method stub
super.onStop();
- if(alertDialog != null) {
- alertDialog.dismiss();
+ if(mAlertDialog != null) {
+ mAlertDialog.dismiss();
}
}
@Override
public void onPreExecute() {
- progressBar.setVisibility(View.VISIBLE);
+ mRetryButton.setVisibility(View.GONE);
+ mProgressBar.setVisibility(View.VISIBLE);
}
@Override
public void onProgressUpdate(String value) {
- progressText.setText(value);
+ Log.d(TAG, "onProgressUpdate> "+value);
+ mProgressText.setText(value);
}
@Override
public void onPostExecute(String result) {
Log.d(TAG, "onPostExecute> "+result);
- progressBar.setVisibility(View.GONE);
- if (result.equals(LoadingTask.TASK_OK)) {
+ mTask = null;
+ Log.d(TAG, "progressbar...gone");
+ mProgressBar.setVisibility(View.GONE);
+ if (TextUtils.equals(result, LoadingTask.TASK_OK)) {
+ Log.d(TAG, "startApp");
startApp();
} else {
- String extra_text = "...Failed";
- if (!TextUtils.isEmpty(result)) extra_text = extra_text + ": " + result;
- progressText.setText(progressText.getText() + extra_text);
- //retryButton.setVisibility(View.VISIBLE);
+ String extra_text;
+ if (!TextUtils.isEmpty(result))
+ extra_text = "...Failed:\n" + result;
+ else
+ extra_text = "...Cancelled";
+ Log.d(TAG, "progresstext += "+extra_text);
+ mProgressText.setText(mProgressText.getText() + extra_text);
+ Log.d(TAG, "retrybutton...visible");
+ mRetryButton.setVisibility(View.VISIBLE);
}
}
-}
\ No newline at end of file
+}
* startActivityForResult(signup, REQ_SIGNUP); } });
*/
if (savedInstanceState != null) {
- Log.d(TAG, "onCreate> should I create a dialog here? alertDialog="+alertDialog+" alertMessage="+alertMessage);
+ Log.d(TAG, "onCreate> savedInstanceState="+savedInstanceState);
if (savedInstanceState.getString(STATE_ALERT_MESSAGE) != null) {
showAlert(savedInstanceState.getString(STATE_ALERT_MESSAGE));
}
}
public void submit() {
+ Log.d(TAG, "submit>");
final String username = ((TextView) findViewById(R.id.accountName)).getText().toString();
final String password = ((TextView) findViewById(R.id.accountPassword)).getText().toString();
@Override
protected void onPostExecute(Intent intent) {
task = null;
+ Log.d(TAG, "task.onPostExecute> intent="+intent);
if (intent.hasExtra(KEY_ERROR_MESSAGE)) {
Log.d(TAG, "task.onPostExecute> error msg: "+intent.getStringExtra(KEY_ERROR_MESSAGE));
onAuthFailure(intent.getStringExtra(KEY_ERROR_MESSAGE));
} else {
- Log.d(TAG, "task.onPostExecute> no error msg, finishing");
+ Log.d(TAG, "task.onPostExecute> no error msg");
onAuthSuccess(intent);
}
}
String accountType = intent.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
String accountPassword = intent.getStringExtra(PARAM_USER_PASS);
final Account account = new Account(accountName, accountType);
- Log.d(TAG, "finishLogin> accountName="+accountName);
+ Log.d(TAG, "onAuthSuccess> accountName="+accountName);
//if (getIntent().getBooleanExtra(ARG_IS_ADDING_NEW_ACCOUNT, false))
- Log.d(TAG, "finishLogin > addAccountExplicitly "+accountName);
+ Log.d(TAG, "onAuthSuccess> addAccountExplicitly "+accountName);
String authtoken = intent.getStringExtra(AccountManager.KEY_AUTHTOKEN);
String authtokenType = authTokenType;
// Create the account on the device
if (accountManager.addAccountExplicitly(account, accountPassword, null)) {
+ Log.d(TAG, "onAuthSuccess> true, setAuthToken "+authtoken);
// Not setting the auth token will cause another call to the server
// to authenticate the user
accountManager.setAuthToken(account, authtokenType, authtoken);
} else {
// Probably the account already existed, in which case update the password
- Log.d(TAG, "finishLogin > setPassword");
+ Log.d(TAG, "onAuthSuccess> false, setPassword");
accountManager.setPassword(account, accountPassword);
}