Last active
August 29, 2015 13:56
-
-
Save mbarcia/8796426 to your computer and use it in GitHub Desktop.
RESTFul session - Part of http://develop-for-android.blogspot.com.es/2014/02/managing-user-sessions-for-restful-api.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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