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 boolean hasActiveAccount(final Context context, final String accountType) {
        return getActiveAccount(context, accountType) != 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;
    }

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