Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

public class Main {
    /**
     * Recreate account
     * @param account account
     * @param authTokenType authTokenType
     * @param requiredFeatures requiredFeatures, could be <code>null</code>
     * @param options options, could be <code>null</code>
     * @param activity activity (cannot be <code>null</code>)
     */
    public static void reCreateAccount(Account account, String accountType, String authTokenType,
            String[] requiredFeatures, Bundle options, Activity activity) {
        if (activity == null) {
            throw new IllegalArgumentException("activity cannot be null");
        }

        final AccountManager accountManager = AccountManager.get(activity.getApplicationContext());
        if (isAccountExist(activity, account)) {
            accountManager.removeAccount(account, null, null);
        }

        accountManager.addAccount(accountType, authTokenType, requiredFeatures, options, activity, null, null);
    }

    /**
     * Check if the specified account exist
     * @param context context
     * @param account account to check
     * @return <code>true</code> if account exist
     */
    public static boolean isAccountExist(Context context, Account account) {
        if (account == null)
            return false;
        final AccountManager accountManager = AccountManager.get(context);
        Account[] accounts = accountManager.getAccountsByType(account.type);
        if (accounts == null || accounts.length == 0) {
            return false;
        } else {
            for (Account a : accounts) {
                if (a.equals(account))
                    return true;
            }
            return false;
        }
    }

    /**
     * Remove specified account
     * @param context context
     * @param account account to remove
     */
    public static void removeAccount(Context context, Account account) {
        final AccountManager accountManager = AccountManager.get(context);
        accountManager.removeAccount(account, null, null);
    }

    /**
     * Add account
     * @param accountType accountType
     * @param authTokenType authTokenType
     * @param requiredFeatures requiredFeatures, could be <code>null</code>
     * @param options options, could be <code>null</code>
     * @param activity activity (cannot be <code>null</code>)
     */
    public static void addAccount(String accountType, String authTokenType, String[] requiredFeatures,
            Bundle options, Activity activity) {
        if (activity == null) {
            throw new IllegalArgumentException("activity cannot be null");
        }
        final AccountManager accountManager = AccountManager.get(activity.getApplicationContext());
        accountManager.addAccount(accountType, authTokenType, requiredFeatures, options, activity, null, null);
    }
}