If you think the Android project Mamytas listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package mn.aug.restfulandroid.security;
//www.java2s.comimport android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import java.util.ArrayList;
import java.util.List;
import mn.aug.restfulandroid.rest.Request;
/**
* OAuthManager handles OAuth authentication with the Twitter API.
*
* @author jeremy
*
*/publicclass AuthorizationManager implements RequestSigner {
// Singleton instance of the OAuthManager
privatestatic AuthorizationManager mInstance;
// Preferences in which to store the request and access tokens
privatefinal SharedPreferences prefs;
publicstaticfinal String WUNDERLIST_TOKEN="WUNDERLIST_TOKEN";
publicstaticfinal String WUNDERLIST_NAME="WUNDERLIST_NAME";
private String mToken;
private String mUser;
/**
* Returns the singleton instance of the OAuthManager
*
* @return singleton instance of the OAuthManager
*/publicstatic AuthorizationManager getInstance(Context context) {
if (mInstance == null) {
mInstance = new AuthorizationManager(context);
}
return mInstance;
}
/**
* Private constructor for the OAuthManager. Initializes the persistent
* storate and OAuthService
*/private AuthorizationManager(Context context) {
prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
}
/**
* Persists the token. Pass in <code>null</code> to clear the saved
* token.
*
* @param token
* the token to persist, or <code>null</code> to clear it
* @return <code>true</code> if the save was successful
*/publicboolean saveToken(String token, String user) {
SharedPreferences.Editor editor = prefs.edit();
if (token == null) {
editor.remove(WUNDERLIST_TOKEN);
editor.remove(WUNDERLIST_NAME);
} else {
editor.putString(WUNDERLIST_TOKEN, token);
editor.putString(WUNDERLIST_NAME, user);
}
return editor.commit();
}
/**
* Retrieves the request token for authorizing the app
*/privatevoid retrieveToken() {
//Appel au service pour rcuprer le token
String token = prefs.getString(WUNDERLIST_TOKEN,null);
this.mToken=token;
}
/**
* Retrieves the name of the usere
*/privatevoid retrieveName() {
//Appel au service pour rcuprer le token
String name = prefs.getString(WUNDERLIST_NAME,null);
this.mUser=name;
}
/**
* Returns the token (may be null)
*
* @return saved token (or null if it does not exist)
*/public String getToken() {
retrieveToken();
return mToken;
}
/**
* Returns the user (may be null)
*
* @return saved user (or null if it does not exist)
*/public String getUser() {
retrieveName();
return mUser;
}
/**
* Determines whether a user is currently logged in
*
* @return <code>true</code> if user is logged in, <code>false</code>
* otherwise
*/publicboolean loggedIn() {
return getToken() != null;
}
/**
* Log out of the application
*/publicvoid logout() {
mToken = null;
mUser=null;
saveToken(mToken,mUser);
}
/**
* Authorizes aWunderlist request.
*
* Authorizing a Request</a> for authorization requirements and methods.
*/
@Override
publicvoid authorize(Request request) {
retrieveToken();
List<String> values = new ArrayList<String>();
values.add("Bearer " + mToken);
request.addHeader("Authorization", values);
}
}