Back to project page Wardrobe_app.
The source code is released under:
Apache License
If you think the Android project Wardrobe_app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.android.busolo.apps.wardrobe.utils; /*w ww .jav a 2s .co m*/ import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.AsyncTask; import android.preference.PreferenceManager; import android.text.TextUtils; import com.android.busolo.apps.wardrobe.engine.UserAccountActivity; import static com.android.busolo.apps.wardrobe.utils.LogUtils.LOGE; public class AccountUtils { private static final String TAG = "makeLogTag(AccountUtils.class)"; private static final String PREF_LOGGED_ACCOUNT = "logged_account"; private static final String PREF_AUTH_TOKEN = "auth_token"; public static boolean isAuthenticated(final Context context) { return !TextUtils.isEmpty(getChosenAccountName(context)); } public static String getChosenAccountName(final Context context) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); return sp.getString(PREF_LOGGED_ACCOUNT, null); } public static void startAuthenticationFlow(final Context context, final Intent finishIntent) { Intent loginFlowIntent = new Intent(context, UserAccountActivity.class); // loginFlowIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // loginFlowIntent.putExtra(UserAccountActivity.EXTRA_FINISH_INTENT, finishIntent); context.startActivity(loginFlowIntent); } public static void tryAuthenticate(final Activity activity, final String userName, final String password) { (new GetTokenTask(activity, userName, password)).execute(); } private static void setAuthToken(final Context context, final String authToken) { /*LOGI(TAG, "Auth token of length " + (TextUtils.isEmpty(authToken) ? 0 : authToken.length()));*/ SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); sp.edit().putString(PREF_AUTH_TOKEN, authToken).commit(); //LOGD(TAG, "Auth Token: " + authToken); } public static void setChosenAccountName(final Context context, final String accountName) { LOGE(TAG, "Chose account " + accountName); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); sp.edit().putString(PREF_LOGGED_ACCOUNT, accountName).commit(); } public static void signOut(final Context context) { // Destroy auth tokens invalidateAuthToken(context); // Remove remaining application state SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); sp.edit().clear().commit(); } static void invalidateAuthToken(final Context context) { setAuthToken(context, null); } public static interface AuthenticateCallback { public boolean shouldCancelAuthentication(); public void onAuthTokenAvailable(); public void onRecoverableException(final int code); public void onUnRecoverableException(final String errorMessage); } private static class GetTokenTask extends AsyncTask<Void, Void, String> { private String mUserName; private String mPassword; private Activity mActivity; public GetTokenTask(Activity activity, String userName, String password) { mUserName = userName; mPassword = password; mActivity = activity; } @Override protected String doInBackground(Void... params) { try { //Call login function from here to get auth token final String token = "TOKEN"; // Persists auth token. setAuthToken(mActivity, token); setChosenAccountName(mActivity, mUserName); return token; } catch (RuntimeException e) { LOGE(TAG, "Error encountered: " + e.getMessage()); } return null; } @Override protected void onPostExecute(String token) { super.onPostExecute(token); } } }