Example usage for android.accounts Account Account

List of usage examples for android.accounts Account Account

Introduction

In this page you can find the example usage for android.accounts Account Account.

Prototype

public Account(@NonNull Account other, @NonNull String accessId) 

Source Link

Usage

From source file:Main.java

public static Account getSyncAccount() {
    return new Account(ACCOUNT_NAME, ACCOUNT_TYPE);
}

From source file:Main.java

public static void ensureUserIsLoggedIn(Context context, String accountType) {
    AccountManager manager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    Account account = new Account("username", accountType);
    String password = "testpassword";
    manager.addAccountExplicitly(account, password, null);
}

From source file:Main.java

/**
 * Creates an Account object for the given name.
 *//*from w  w  w  . j a v  a2  s.co m*/
public static Account createAccountFromName(String name) {
    return new Account(name, GOOGLE_ACCOUNT_TYPE);
}

From source file:Main.java

/**
 * Create a new dummy account for the sync adapter
 *
 * @param context The application context
 *//*from w  ww. j a va2  s.  co  m*/
public static Account CreateSyncAccount(Context context) {

    // Create the account type and default account
    Account newAccount = new Account(ACCOUNT_NAME, ACCOUNT_TYPE);
    // Get an instance of the Android account manager
    AccountManager accountManager = (AccountManager) context.getSystemService(context.ACCOUNT_SERVICE);

    /*
     * Add the account and account type, no password or user data
     * If successful, return the Account object, otherwise report an error.
     */
    if (accountManager.addAccountExplicitly(newAccount, null, null)) {
        Log.d(TAG, "Account created: " + newAccount.name);

        return newAccount;
    } else {
        /*
         * The account exists or some other error occurred.
         * Try and get account first. Then log or report the error and return null.
         */
        Account[] accounts = accountManager.getAccounts();
        for (Account account : accounts) {
            if (account.name.equals(ACCOUNT_NAME)) {
                Log.d(TAG, "Account exists: " + account.name);
                return account;
            }
        }
        Log.d(TAG, "Error occured. The account is null");
        return null;
    }

}

From source file:Main.java

/**
 * Sets password of account, creates a new account if necessary.
 *//*  w  w  w. j ava  2s  . c  o m*/
private static Account findOrCreateAccount(AccountManager accountManager, String username, String refreshToken,
        String accountType) {

    for (Account account : accountManager.getAccountsByType(accountType)) {
        if (account.name.equals(username)) {
            accountManager.setPassword(account, refreshToken);
            return account;
        }
    }

    Account account = new Account(username, accountType);
    accountManager.addAccountExplicitly(account, refreshToken, null);
    return account;
}

From source file:Main.java

/**
 * Create a account for the sync adapter.
 *
 * @param _context The application context.
 * @return the new created Account./*from   ww w. j a v a2  s .c om*/
 */
public static Account createSyncAccount(Context _context, String _username) {
    // create a new a account.
    Account newAccount = new Account(_username, ACCOUNT_TYPE);

    AccountManager accountManager = (AccountManager) _context.getSystemService(Context.ACCOUNT_SERVICE);
    Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
    for (Account account : accounts) {
        if (account.equals(newAccount)) {
            // account already exists
            return null;
        }
    }

    // try to login and retrieve the token

    Bundle bundle = new Bundle();
    bundle.putString("Token", "some auth token please");

    if (accountManager.addAccountExplicitly(newAccount, null, bundle)) {

    } else {
        Log.i(TAG, "createSyncAccount: account already exists or an error happened");
    }

    return newAccount;
}

From source file:Main.java

public static String refreshAuthToken(Activity activity, String token, String name, String googleApi) {
    String authToken = null;//from w  w w  . ja  va2 s.  co  m
    final Account account;
    AccountManagerFuture<Bundle> accountFuture;

    account = new Account(name, GOOGLE_ACCOUNT_TYPE);
    try {
        // invalidate the retrieved token and get a fresh one
        AccountManager.get(activity).invalidateAuthToken(GOOGLE_ACCOUNT_TYPE, token);
        accountFuture = AccountManager.get(activity).getAuthToken(account, googleApi, null, activity, null,
                null);
        authToken = accountFuture.getResult().get(AccountManager.KEY_AUTHTOKEN).toString();
    } catch (OperationCanceledException e) {
        Log.e(TAG, e.toString());
    } catch (AuthenticatorException e) {
        Log.e(TAG, e.toString());
    } catch (IOException e) {
        Log.e(TAG, e.toString());
    }
    return authToken;
}

From source file:saschpe.birthdays.helper.AccountHelper.java

public static Bundle addAccount(Context context) {
    Log.d(TAG, "AccountHelper.addAccount: Adding account...");

    final Account account = new Account(context.getString(R.string.app_name),
            context.getString(R.string.account_type));
    AccountManager manager = AccountManager.get(context);

    if (manager.addAccountExplicitly(account, null, null)) {
        // Enable automatic sync once per day
        ContentResolver.setSyncAutomatically(account, context.getString(R.string.content_authority), true);
        ContentResolver.setIsSyncable(account, context.getString(R.string.content_authority), 1);

        // Add periodic sync interval based on user preference
        final long freq = PreferencesHelper.getPeriodicSyncFrequency(context);
        ContentResolver.addPeriodicSync(account, context.getString(R.string.content_authority), new Bundle(),
                freq);/*from  w w  w.ja v a 2  s.  c o  m*/

        Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        Log.i(TAG, "Account added: " + account.name);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            manager.notifyAccountAuthenticated(account);
        }
        return result;
    } else {
        Log.e(TAG, "Adding account explicitly failed!");
        return null;
    }
}

From source file:Main.java

public static String getAuthToken(Activity activity, String name, String googleApi) {
    String authToken = null;/*ww w .  ja  v a2 s  . c  om*/
    final Account account;
    AccountManagerFuture<Bundle> accountFuture;

    account = new Account(name, GOOGLE_ACCOUNT_TYPE);
    accountFuture = AccountManager.get(activity).getAuthToken(account, googleApi, null, activity, null, null);

    try {
        authToken = accountFuture.getResult().get(AccountManager.KEY_AUTHTOKEN).toString();
        // invalidate the retrieved token and get a fresh one
        AccountManager.get(activity).invalidateAuthToken(GOOGLE_ACCOUNT_TYPE, authToken);
        accountFuture = AccountManager.get(activity).getAuthToken(account, googleApi, null, activity, null,
                null);
        authToken = accountFuture.getResult().get(AccountManager.KEY_AUTHTOKEN).toString();
    } catch (OperationCanceledException e) {
        Log.e(TAG, e.toString());
    } catch (AuthenticatorException e) {
        Log.e(TAG, e.toString());
    } catch (IOException e) {
        Log.e(TAG, e.toString());
    }
    return authToken;
}

From source file:com.android.volley.toolbox.AndroidAuthenticatorTest.java

@Before
public void setUp() {
    mAccountManager = mock(AccountManager.class);
    mFuture = mock(AccountManagerFuture.class);
    mAccount = new Account("coolperson", "cooltype");
    mAuthenticator = new AndroidAuthenticator(mAccountManager, mAccount, "cooltype", false);
}