Android Open Source - Wardrobe_app Account Utils






From Project

Back to project page Wardrobe_app.

License

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.

Java Source Code

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);
        }
    }
}




Java Source Code List

com.android.busolo.apps.wardrobe.Config.java
com.android.busolo.apps.wardrobe.engine.BaseActivity.java
com.android.busolo.apps.wardrobe.engine.ColorPickerFragment.java
com.android.busolo.apps.wardrobe.engine.CommentsFragment.java
com.android.busolo.apps.wardrobe.engine.FollowActivity.java
com.android.busolo.apps.wardrobe.engine.HomeActivity.java
com.android.busolo.apps.wardrobe.engine.ItemDetailsActivity.java
com.android.busolo.apps.wardrobe.engine.LoginFragment.java
com.android.busolo.apps.wardrobe.engine.MatchFragment.java
com.android.busolo.apps.wardrobe.engine.NewPostActivity.java
com.android.busolo.apps.wardrobe.engine.PrivateStreamFragment.java
com.android.busolo.apps.wardrobe.engine.ProfileFragment.java
com.android.busolo.apps.wardrobe.engine.PublicStreamActivity.java
com.android.busolo.apps.wardrobe.engine.PublicStreamFragment.java
com.android.busolo.apps.wardrobe.engine.SignupFragment.java
com.android.busolo.apps.wardrobe.engine.StepOneFragment.java
com.android.busolo.apps.wardrobe.engine.StepTwoFragment.java
com.android.busolo.apps.wardrobe.engine.UserAccountActivity.java
com.android.busolo.apps.wardrobe.engine.adapter.ColorListAdapter.java
com.android.busolo.apps.wardrobe.engine.adapter.ColorSpinnerAdapter.java
com.android.busolo.apps.wardrobe.engine.adapter.FeedsListAdapter.java
com.android.busolo.apps.wardrobe.engine.adapter.GridViewPhotoAdapter.java
com.android.busolo.apps.wardrobe.engine.adapter.ViewInflaterBaseAdapter.java
com.android.busolo.apps.wardrobe.engine.model.ColorPicker.java
com.android.busolo.apps.wardrobe.engine.model.FilterParam.java
com.android.busolo.apps.wardrobe.engine.model.Follow.java
com.android.busolo.apps.wardrobe.engine.model.Stream.java
com.android.busolo.apps.wardrobe.sync.SyncHelper.java
com.android.busolo.apps.wardrobe.sync.SyncService.java
com.android.busolo.apps.wardrobe.utils.AccountUtils.java
com.android.busolo.apps.wardrobe.utils.BitmapCache.java
com.android.busolo.apps.wardrobe.utils.ImageLoader.java
com.android.busolo.apps.wardrobe.utils.LogUtils.java
com.android.busolo.apps.wardrobe.utils.LruBitmapCache.java
com.android.busolo.apps.wardrobe.utils.NetUtils.java
com.android.busolo.apps.wardrobe.utils.ParserUtils.java
com.android.busolo.apps.wardrobe.utils.PrefUtils.java
com.android.busolo.apps.wardrobe.utils.ServerResponse.java
com.android.busolo.apps.wardrobe.utils.UIUtils.java
com.android.busolo.apps.wardrobe.utils.VolleyAppController.java
com.android.busolo.apps.wardrobe.utils.model.FeedResult.java
com.android.busolo.apps.wardrobe.widget.BezelImageView.java
com.android.busolo.apps.wardrobe.widget.CheckableFrameLayout.java
com.android.busolo.apps.wardrobe.widget.EllipsizedTextView.java
com.android.busolo.apps.wardrobe.widget.ObservableScrollView.java
com.android.busolo.apps.wardrobe.widget.SquareImageView.java