Android Open Source - Navigation-drawer-a-la-Google Account Utils






From Project

Back to project page Navigation-drawer-a-la-Google.

License

The source code is released under:

Apache License

If you think the Android project Navigation-drawer-a-la-Google 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

/*
 * Copyright 2014 Google Inc. All rights reserved.
 */*  w  ww .ja v a  2  s .  c o m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.samples.apps.iosched.util;

import android.accounts.Account;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.google.android.gms.auth.GoogleAuthUtil;

/**
 * Account and login utilities. This class manages a local shared preferences object
 * that stores which account is currently active, and can store associated information
 * such as Google+ profile info (name, image URL, cover URL) and also the auth token
 * associated with the account.
 */
public class AccountUtils {
    private static final String PREF_ACTIVE_ACCOUNT = "chosen_account";

    private static final String PREFIX_PREF_AUTH_TOKEN = "auth_token_";
    private static final String PREFIX_PREF_ACCOUNT_NAME = "account_name_";


    public static boolean setActiveAccount(final Context context, final String accountName) {
        SharedPreferences sp = getSharedPreferences(context);
        sp.edit().putString(PREF_ACTIVE_ACCOUNT, accountName).commit();
        return true;
    }

    private static SharedPreferences getSharedPreferences(final Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    public static Account getActiveAccount(final Context context) {
        String account = getActiveAccountName(context);
        if (account != null) {
            return new Account(account, GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
        } else {
            return null;
        }
    }

    public static String getActiveAccountName(final Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getString(PREF_ACTIVE_ACCOUNT, null);
    }

    public static String getName(final Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return hasActiveAccount(context) ? sp.getString(makeAccountSpecificPrefKey(context,
                PREFIX_PREF_ACCOUNT_NAME), null) : null;
    }

    public static void setName(final Context context, final String accountName, final String name) {
        SharedPreferences sp = getSharedPreferences(context);
        sp.edit().putString(makeAccountSpecificPrefKey(accountName, PREFIX_PREF_ACCOUNT_NAME),
                name).commit();
    }

    private static String makeAccountSpecificPrefKey(String accountName, String prefix) {
        return prefix + accountName;
    }

    public static boolean hasActiveAccount(final Context context) {
        return !TextUtils.isEmpty(getActiveAccountName(context));
    }

    public static boolean hasToken(final Context context, final String accountName) {
        SharedPreferences sp = getSharedPreferences(context);
        return !TextUtils.isEmpty(sp.getString(makeAccountSpecificPrefKey(accountName,
                PREFIX_PREF_AUTH_TOKEN), null));
    }

    public static void setAuthToken(final Context context, final String accountName, final String authToken) {
        SharedPreferences sp = getSharedPreferences(context);
        sp.edit().putString(makeAccountSpecificPrefKey(accountName, PREFIX_PREF_AUTH_TOKEN),
                authToken).commit();
    }

    private static String makeAccountSpecificPrefKey(Context ctx, String prefix) {
        return hasActiveAccount(ctx) ? makeAccountSpecificPrefKey(getActiveAccountName(ctx),
                prefix) : null;
    }
}




Java Source Code List

com.google.samples.apps.iosched.ui.widget.BezelImageView.java
com.google.samples.apps.iosched.ui.widget.ScrimInsetsScrollView.java
com.google.samples.apps.iosched.util.AccountUtils.java
com.google.samples.apps.iosched.util.LoginAndAuthHelper.java
com.google.samples.apps.iosched.util.PrefUtils.java
com.google.samples.apps.iosched.util.UIUtils.java
info.korzeniowski.activitydrawersample.BaseDrawerActivity.java
info.korzeniowski.activitydrawersample.FirstDrawerActivity.java
info.korzeniowski.activitydrawersample.SecondDrawerActivity.java
info.korzeniowski.activitydrawersample.SettingsActivity.java
info.korzeniowski.activitydrawersample.SimpleFragment.java
info.korzeniowski.activitydrawersample.ThirdDrawerActivity.java