Example usage for android.accounts AccountManager getAccountsByType

List of usage examples for android.accounts AccountManager getAccountsByType

Introduction

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

Prototype

@NonNull
public Account[] getAccountsByType(String type) 

Source Link

Document

Lists all accounts of particular type visible to the caller.

Usage

From source file:Main.java

/**
 * Get account by type and name/*w ww .j av  a2  s. c  o  m*/
 * @param context context
 * @param accountType account type
 * @param accountName account name
 * @return Account object
 */
public static Account getAccount(Context context, String accountType, String accountName) {
    final AccountManager accountManager = AccountManager.get(context);
    Account[] accounts = accountManager.getAccountsByType(accountType);

    if (accounts == null || accounts.length == 0) {
        return null;
    } else {
        for (Account account : accounts) {
            if (account.name.equals(accountName)) {
                return account;
            }
        }
        return null;
    }
}

From source file:Main.java

/**
 * Gets the synchronized Accounts in device of user.
 * @param context Android app Context/*w ww.j a v a2s  .co m*/
 * @param type String with kind of accounts we want to obtain (Ex. com.google) 
 * @return Account[] with  synchronized user accounts into the device
 */
public static Account[] getAccountsByType(Context context, String type) {
    final AccountManager manager = AccountManager.get(context);
    final Account[] accounts = manager.getAccountsByType(type);
    return accounts;
}

From source file:Main.java

/**
 * Sets password of account, creates a new account if necessary.
 *//*from w ww  . j ava2s . c  om*/
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

/**
 * Check if the specified account exist/*from   w  w  w. j  a  v a 2s  . c o m*/
 * @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;
    }
}

From source file:Main.java

static List<String> getEmailAddresses(Context context, int limit) {
    ArrayList<String> emails = new ArrayList<String>();
    AccountManager am = AccountManager.get(context);
    if (am == null) {
        return emails;
    }/*from   ww  w  . java2  s. c  o m*/
    Account[] accounts = am.getAccountsByType("com.google");
    if (accounts == null || accounts.length == 0) {
        return emails;
    }
    for (Account a : accounts) {
        if (a.name == null || a.name.length() == 0) {
            continue;
        }
        emails.add(a.name.trim().toLowerCase());
    }
    while (emails.size() > limit) {
        emails.remove(emails.size() - 1);
    }
    return emails;
}

From source file:Main.java

/**
 * Create a account for the sync adapter.
 *
 * @param _context The application context.
 * @return the new created Account.//ww w .  java 2 s .c  o m
 */
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:net.heroicefforts.viable.android.rep.it.auth.Authenticate.java

/**
 * Attempts to authenticate the user using a pre-existing stored authentication token.  If an account exists, but no such token 
 * exists, then the user will be prompted by the account authenticator to re-enter their Google credentials to generate the new token.
 * //ww  w  .  ja  v a2  s.  c  o m
 * @param act the calling activity
 * @return the authentication token for the requested service or null if there is no Google Account.
 * @throws AuthenticatorException if an error occurs during authentication.
 * @throws OperationCanceledException
 * @throws IOException
 */
public static String authenticate(Activity act, String serviceCode)
        throws AuthenticatorException, OperationCanceledException, IOException {
    AccountManager mgr = AccountManager.get(act);
    Account[] accts = mgr.getAccountsByType(GCLAccountAuthenticator.ACCT_TYPE);
    if (accts.length > 0) {
        Account acct = accts[0];
        AccountManagerFuture<Bundle> accountManagerFuture = mgr.getAuthToken(acct, serviceCode, null, act, null,
                null);
        Bundle authTokenBundle = accountManagerFuture.getResult();
        String authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();

        return authToken;
    } else {
        Log.e(TAG, "No google accounts registered for this device.");
        return null;
    }
}

From source file:com.flowzr.budget.holo.export.flowzr.FlowzrSyncTask.java

public static android.accounts.Account getAndroidAccount(Context context) {
    String accountName = MyPreferences.getFlowzrAccount(context);
    AccountManager accountManager = AccountManager.get(context);
    android.accounts.Account[] accounts = accountManager.getAccountsByType("com.google");
    Account useCredential = null;//w w  w .  j  a v a  2s  .  c om
    for (int i = 0; i < accounts.length; i++) {
        if (accountName.equals(((android.accounts.Account) accounts[i]).name)) {
            return accounts[i];
        }
    }
    return null;
}

From source file:com.android.projectz.teamrocket.thebusapp.util.Device.java

/**
 * permette di prendere il probabile indirizzo email del telefono
 *
 * @param context//from w  w w.  ja  v  a 2 s.c om
 * @return
 */
public static String getDeviceEMAIL(Context context) {
    AccountManager manager = AccountManager.get(context);
    if (ActivityCompat.checkSelfPermission(context,
            Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
        return "ERR_PERMISSION";
    }
    Account[] accounts = manager.getAccountsByType("com.google");
    List<String> possibleEmails = new LinkedList<String>();

    for (Account account : accounts) {
        possibleEmails.add(account.name);
    }
    if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {
        String email = possibleEmails.get(0);
        String[] parts = email.split("@");

        if (parts.length > 1)
            return parts[0];
    }
    return null;
}

From source file:com.ntsync.android.sync.activities.PaymentVerificationService.java

private static void runVerifier(final Context context, final ScheduledExecutorService sched) {
    SystemHelper.initSystem(context);//from  ww  w  . j a  va 2  s  .c  o  m
    // Check if PaymentData available -> when no cancel
    AccountManager acm = AccountManager.get(context);
    Account[] accounts = acm.getAccountsByType(Constants.ACCOUNT_TYPE);
    boolean foundPaymentData = false;
    for (Account account : accounts) {
        PaymentData paymentData = SyncUtils.getPayment(account, acm);
        if (paymentData != null) {
            if (SyncUtils.isPaymentVerificationStarted()) {
                return;
            }

            if (System.currentTimeMillis() > paymentData.paymentSaveDate + TIMEOUT_PENDING_PAYMENT) {
                sendNotification(context, account, R.string.shop_activity_pendingverification,
                        ShopActivity.class, false);
            }
            foundPaymentData = true;
            break;
        }
    }

    if (foundPaymentData) {
        // Check if user has to be notified

        // Start Service
        Intent verifService = new Intent(context, PaymentVerificationService.class);
        context.startService(verifService);
        LogHelper.logD(TAG, "Start PaymentVerificationService", null);
    } else {
        sched.shutdownNow();
    }
}