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:com.hemou.android.account.AccountUtils.java

/**
 * Get default account where password can be retrieved
 * //from   ww w.  j  a v  a2 s. c o  m
 * @param context
 * @return password accessible account or null if none
 */
public static Account getPasswordAccessibleAccount(final Context context) {
    AccountManager manager = AccountManager.get(context);
    Account[] accounts = manager.getAccountsByType(ACCOUNT_TYPE);
    if (accounts == null || accounts.length == 0)
        return null;

    try {
        accounts = getPasswordAccessibleAccounts(manager, accounts);
    } catch (AuthenticatorConflictException e) {
        return null;
    }
    return accounts != null && accounts.length > 0 ? accounts[0] : null;
}

From source file:net.vleu.par.android.rpc.Transceiver.java

/**
 * Lists all Google accounts on this device
 * /*  w ww.  ja v a 2s. c o m*/
 * @param context
 *            Won't be kept anywhere
 * @return A possibly empty array of Google Accounts
 */
public static Account[] listGoogleAccounts(final Context context) {
    final AccountManager am = AccountManager.get(context);
    return am.getAccountsByType(GOOGLE_ACCOUNT_TYPE);
}

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

/**
 * Checks whether the account is enabled or not
 *///from   w  w w .j a v  a 2 s .c  o m
public static boolean isAccountActivated(Context context) {
    AccountManager manager = AccountManager.get(context);
    if (ActivityCompat.checkSelfPermission(context,
            Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
        Log.e(TAG, "Lacking permission GET_ACCOUNTS to query existing accounts!");
        return false;
    }
    Account[] availableAccounts = manager.getAccountsByType(context.getString(R.string.account_type));
    for (Account currentAccount : availableAccounts) {
        if (currentAccount.name.equals(context.getString(R.string.app_name))) {
            Log.i(TAG, "Account already present");
            return true;
        }
    }
    return false;
}

From source file:com.mrcaps.taskswidget.TasksHelper.java

public static void refreshAuthTokenImpl5(final Context context, final boolean invalidate,
        final Runnable continuation) {
    final Handler handl = getHandler();

    new Thread(new Runnable() {
        public void run() {
            AccountManager mgr = AccountManager.get(context);
            Account[] accts = mgr.getAccountsByType(ACCOUNT_TYPE);
            if (accts.length < 1) {
                handl.post(new Runnable() {
                    public void run() {
                        Toast.makeText(context, "Error: could not find a Google account on this phone",
                                Toast.LENGTH_SHORT).show();
                    }//from   w w w .j av a2  s .  c  o m
                });
                return;
            }

            Account acct = accts[0];

            if (invalidate) {
                long now = System.currentTimeMillis();
                Log.v(TAG, "I5 Auth Token Invalidation Requested");
                if (now - lastInvalidation > invalidationDelta) {
                    Log.v(TAG, "I5 Invalidating Auth Token");
                    try {
                        String token = mgr.blockingGetAuthToken(acct, SERVICE_NAME, true);
                        mgr.invalidateAuthToken(ACCOUNT_TYPE, token);
                    } catch (Exception e) {
                        Log.e(TAG, "I5 couldn't invalidate token", e);
                    }
                }
            }

            try {
                String token = mgr.blockingGetAuthToken(acct, SERVICE_NAME, true);
                Log.v(TAG, "I5 got auth token: " + token);
                authToken = token;
            } catch (Exception e) {
                Log.e(TAG, "I5 couldn't authenticate", e);
                handl.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, "Error: I5 could not authenticate with Google account",
                                Toast.LENGTH_SHORT).show();
                    }
                });
            }

            continuation.run();
        }
    }).start();
}

From source file:com.alphabetbloc.accessmrs.utilities.NetworkUtils.java

private static void getServerCredentials() {
    final AccountManager am = AccountManager.get(App.getApp());
    Account[] accounts = am.getAccountsByType(App.getApp().getString(R.string.app_account_type));

    if (App.DEBUG)
        Log.v(TAG, "accounts.length =" + accounts.length);
    if (accounts.length <= 0) {
        if (App.DEBUG)
            Log.v(TAG, "no accounts have been set up");

    } else {//from   w w w  . j ava  2 s . co  m

        mUsername = accounts[0].name;
        String encPwd = am.getPassword(accounts[0]);
        mPassword = EncryptionUtil.decryptString(encPwd);
    }

}

From source file:com.bjorsond.android.timeline.utilities.Utilities.java

public static Account getUserAccount(Context c) {
    AccountManager manager = AccountManager.get(c);
    Account[] accounts = manager.getAccountsByType("com.google");

    for (Account account : accounts) {
        return account;
    }/*from  ww  w .  j  a  v a2s .  c o  m*/

    Account nonRegisteredAccount = new Account("test@timelineapp.no", "com.google");
    return nonRegisteredAccount;
}

From source file:io.github.hidroh.materialistic.data.SyncDelegate.java

@UiThread
public static void scheduleSync(Context context, Job job) {
    if (!Preferences.Offline.isEnabled(context)) {
        return;/*  ww w .java2 s .  c om*/
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !TextUtils.isEmpty(job.id)) {
        JobInfo.Builder builder = new JobInfo.Builder(Long.valueOf(job.id).intValue(),
                new ComponentName(context.getPackageName(), ItemSyncJobService.class.getName()))
                        .setRequiredNetworkType(
                                Preferences.Offline.isWifiOnly(context) ? JobInfo.NETWORK_TYPE_UNMETERED
                                        : JobInfo.NETWORK_TYPE_ANY)
                        .setExtras(job.toPersistableBundle());
        if (Preferences.Offline.currentConnectionEnabled(context)) {
            builder.setOverrideDeadline(0);
        }
        ((JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(builder.build());
    } else {
        Bundle extras = new Bundle(job.toBundle());
        extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        Account syncAccount;
        AccountManager accountManager = AccountManager.get(context);
        Account[] accounts = accountManager.getAccountsByType(BuildConfig.APPLICATION_ID);
        if (accounts.length == 0) {
            syncAccount = new Account(SYNC_ACCOUNT_NAME, BuildConfig.APPLICATION_ID);
            accountManager.addAccountExplicitly(syncAccount, null, null);
        } else {
            syncAccount = accounts[0];
        }
        ContentResolver.requestSync(syncAccount, MaterialisticProvider.PROVIDER_AUTHORITY, extras);
    }
}

From source file:com.meiste.greg.ptw.GAE.java

public static boolean isAccountSetupNeeded(final Context context) {
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    final String account = prefs.getString(EditPreferences.KEY_ACCOUNT_EMAIL, "");

    if (account.length() == 0) {
        // Account not setup at all
        return true;
    }/*from  w  w  w .  j a v a2  s.  c o m*/

    final AccountManager mgr = AccountManager.get(context);
    final Account[] accts = mgr.getAccountsByType(ACCOUNT_TYPE);
    for (final Account acct : accts) {
        if (acct.name.equals(account)) {
            // Account setup and found on system
            return false;
        }
    }

    // Account setup, but no longer present on system
    final SharedPreferences.Editor editor = prefs.edit();
    editor.putString(EditPreferences.KEY_ACCOUNT_EMAIL, null);
    editor.putString(EditPreferences.KEY_ACCOUNT_COOKIE, null);
    editor.apply();

    return true;
}

From source file:io.github.hidroh.materialistic.AppUtils.java

@SuppressLint("MissingPermission")
public static Pair<String, String> getCredentials(Context context) {
    String username = Preferences.getUsername(context);
    if (TextUtils.isEmpty(username)) {
        return null;
    }//from   w w  w  . j a va2 s.c  o  m
    AccountManager accountManager = AccountManager.get(context);
    Account[] accounts = accountManager.getAccountsByType(BuildConfig.APPLICATION_ID);
    for (Account account : accounts) {
        if (TextUtils.equals(username, account.name)) {
            return Pair.create(username, accountManager.getPassword(account));
        }
    }
    return null;
}

From source file:com.alphabetbloc.accessmrs.services.SyncManager.java

public static void syncData() {
    if (App.DEBUG)
        Log.v(TAG, "SyncData is Requested");
    AccountManager accountManager = AccountManager.get(App.getApp());
    Account[] accounts = accountManager.getAccountsByType(App.getApp().getString(R.string.app_account_type));
    if (accounts.length > 0) {

        sStartSync.set(true);/*from   w ww. j  a v a 2s.  c  om*/

        Bundle bundle = new Bundle();
        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_FORCE, true);
        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);

        // //this resets the scheduled sync
        ContentResolver.requestSync(accounts[0], App.getApp().getString(R.string.app_provider_authority),
                bundle);
    } else
        UiUtils.toastAlert(App.getApp().getString(R.string.sync_error),
                App.getApp().getString(R.string.no_account_setup));
}