Java tutorial
//package com.java2s; /* * Copyright (c) 2016. by Hoang Hiep (hoanghiep8899@gmail.com) * This file AccountUtils.java is part of File Manager * Create at 3/6/16 2:19 PM * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.text.TextUtils; import android.util.Log; import java.util.UUID; public class Main { private static final String PREF_ACTIVE_ACCOUNT = "chosen_account"; private static final String PREFIX_PREF_GCM_KEY = "gcm_key_"; public static String getGcmKey(final Context context, final String accountName) { SharedPreferences sp = getSharedPreferences(context); String gcmKey = sp.getString(makeAccountSpecificPrefKey(accountName, PREFIX_PREF_GCM_KEY), null); // if there is no current GCM key, generate a new random one if (TextUtils.isEmpty(gcmKey)) { gcmKey = UUID.randomUUID().toString(); Log.d("GoogleAcount", "No GCM key on account " + accountName + ". Generating random one: " + sanitizeGcmKey(gcmKey)); setGcmKey(context, accountName, gcmKey); } return gcmKey; } private static SharedPreferences getSharedPreferences(final Context context) { return PreferenceManager.getDefaultSharedPreferences(context); } private static String makeAccountSpecificPrefKey(Context ctx, String prefix) { return hasActiveAccount(ctx) ? makeAccountSpecificPrefKey(getActiveAccountName(ctx), prefix) : null; } private static String makeAccountSpecificPrefKey(String accountName, String prefix) { return prefix + accountName; } public static String sanitizeGcmKey(String key) { if (key == null) { return "(null)"; } else if (key.length() > 8) { return key.substring(0, 4) + "........" + key.substring(key.length() - 4); } else { return "........"; } } public static void setGcmKey(final Context context, final String accountName, final String gcmKey) { SharedPreferences sp = getSharedPreferences(context); sp.edit().putString(makeAccountSpecificPrefKey(accountName, PREFIX_PREF_GCM_KEY), gcmKey).apply(); Log.d("GoogleAcount", "GCM key of account " + accountName + " set to: " + sanitizeGcmKey(gcmKey)); } /** * Specify whether the app has an active account set. * * @param context Context used to lookup {@link SharedPreferences} the value is stored with. */ public static boolean hasActiveAccount(final Context context) { return !TextUtils.isEmpty(getActiveAccountName(context)); } /** * Return the accountName the app is using as the active Google Account. * * @param context Context used to lookup {@link SharedPreferences} the value is stored with. */ public static String getActiveAccountName(final Context context) { SharedPreferences sp = getSharedPreferences(context); return sp.getString(PREF_ACTIVE_ACCOUNT, null); } }