Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

public class Main {
    private static final String PREF_ACTIVE_ACCOUNT = "active_account";
    private static final String PREFS_NAME = "se.leiflandia.lroi.prefs";

    public static void removeActiveAccount(Context context, String accountType) {
        Account account = getActiveAccount(context, accountType);
        if (account != null) {
            AccountManager.get(context).removeAccount(account, null, null);
        }
        setActiveAccountName(context, null);
    }

    public static Account getActiveAccount(final Context context, final String accountType) {
        Account[] accounts = AccountManager.get(context).getAccountsByType(accountType);
        return getActiveAccount(accounts, getActiveAccountName(context));
    }

    private static Account getActiveAccount(final Account[] accounts, final String activeAccountName) {
        for (Account account : accounts) {
            if (TextUtils.equals(account.name, activeAccountName)) {
                return account;
            }
        }
        return null;
    }

    public static void setActiveAccountName(final Context context, final String name) {
        getSharedPreferences(context).edit().putString(PREF_ACTIVE_ACCOUNT, name).commit();
    }

    private static String getActiveAccountName(final Context context) {
        return getSharedPreferences(context).getString(PREF_ACTIVE_ACCOUNT, null);
    }

    private static SharedPreferences getSharedPreferences(final Context context) {
        return context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    }
}