Skip to content

Instantly share code, notes, and snippets.

@mbarcia
Last active August 29, 2015 13:56
Show Gist options
  • Save mbarcia/8796426 to your computer and use it in GitHub Desktop.
Save mbarcia/8796426 to your computer and use it in GitHub Desktop.
package net.colaborativa.exampleapp;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.text.TextUtils;
import android.util.Log;
// This is the login activity of your app, only used
// here to retrieve the value of ACCOUNT_TYPE
import net.colaborativa.exampleapp.account.AuthenticatorActivity;
/**
* RESTFul session
*
* @author Mariano E. Barcia Calderón <[email protected]>
*/
public class MyApiSession {
// 10min time-to-live
private static final long TOKEN_TTL_AFTER_LAST_REQUEST = 600000;
// global instance
private static MyApiSession instance = null;
// timestamp of last revalidation
private long mTime = 0;
// account to hold the session for
private Account mAccount;
// session token
private String mToken;
/**
* @param account to hold the session for. Protected to prevent
* creation without token
*/
protected MyApiSession(Account account) {
mAccount = account;
}
/**
* @param account AM object
* @param token string
* @return session instance
*/
public static MyApiSession newSessionFor(
Account account,
String token) {
instance = new MyApiSession(account);
instance.setToken(token);
instance.touch();
return instance;
}
/**
* @return session instance. Note this is a singleton-or-null
*/
public static MyApiSession getInstance() {
return instance;
}
/**
* @param token to be set
*/
public void setToken(String token) {
this.mToken = token;
}
/**
* @return the session token
*/
public String getToken() {
return mToken;
}
/**
* @return true if this session object is valid,
* false otherwise
*/
public boolean isValid() {
long millis = elapsedTimeMillis();
return (mAccount != null &&
!TextUtils.isEmpty(mToken) &&
millis < TOKEN_TTL_AFTER_LAST_REQUEST);
}
/**
* @param token used to revalidate this session object
* @return this (revalidated state)
*/
public GlowSession revalidate(String token) {
if (!TextUtils.equals(mToken, token)) {
mToken = token;
if (!TextUtils.isEmpty(token)) {
Log.i(ExpendituresApplication.TAG, "Revalidating " +
"with a different token, assuming newly created " +
"account or confirmed credentials.");
}
else {
Log.i(ExpendituresApplication.TAG, "Revalidating with " +
"an empty token, assuming existing account but app just " +
"started.");
}
}
return revalidate();
}
/**
* @return this (revalidated state)
*/
public MyApiSession revalidate() {
if (isValid()) {
Log.w("exampleapp", "No need to revalidate twice!");
}
touch();
Log.i("exampleapp", "Revalidated session for account " +
mAccount.name + ".");
return this;
}
/**
* Touches this session to keep it alive
*/
public void touch() {
mTime = System.currentTimeMillis();
Log.d("exampleapp", "Session for " + mAccount.name +
" has been touched.");
}
/**
* Invalidates AM's token for this session's account
*/
public void invalidate() {
AccountManager am =
AccountManager.get(ExampleApplication.getInstance());
am.invalidateAuthToken(AuthenticatorActivity.ACCOUNT_TYPE,
mToken);
mToken = "";
Log.i("exampleapp", "Invalidated session for account " +
mAccount.name);
}
public long elapsedTimeMillis() {
return System.currentTimeMillis() - mTime;
}
public Account getAccount() {
return mAccount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment