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:org.brussels.gtug.attendance.AccountsActivity.java

/**
 * Registers for C2DM messaging with the given account name.
 * /*from   ww  w . j  a  v  a 2 s .com*/
 * @param accountName a String containing a Google account name
 */
private void register(final String accountName) {
    // Store the account name in shared preferences
    final SharedPreferences prefs = Util.getSharedPreferences(mContext);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(Util.ACCOUNT_NAME, accountName);
    editor.remove(Util.AUTH_COOKIE);
    editor.remove(Util.DEVICE_REGISTRATION_ID);
    editor.commit();

    // Obtain an auth token and register<
    final AccountManager mgr = AccountManager.get(mContext);
    Account[] accts = mgr.getAccountsByType("com.google");
    for (Account acct : accts) {
        final Account account = acct;
        if (account.name.equals(accountName)) {
            // Get the auth token from the AccountManager and convert
            // it into a cookie for the appengine server
            final Activity activity = this;
            mgr.getAuthToken(account, "ah", null, activity, new AccountManagerCallback<Bundle>() {
                public void run(AccountManagerFuture<Bundle> future) {
                    String authToken = getAuthToken(future);
                    // Ensure the token is not expired by invalidating it and
                    // obtaining a new one
                    mgr.invalidateAuthToken(account.type, authToken);
                    mgr.getAuthToken(account, "ah", null, activity, new AccountManagerCallback<Bundle>() {
                        public void run(AccountManagerFuture<Bundle> future) {
                            String authToken = getAuthToken(future);
                            // Convert the token into a cookie for future use
                            String authCookie = getAuthCookie(authToken);
                            Editor editor = prefs.edit();
                            editor.putString(Util.AUTH_COOKIE, authCookie);
                            editor.commit();
                            C2DMessaging.register(mContext, Constants.SENDER_ID);
                        }
                    }, null);
                }
            }, null);
            break;
        }
    }
}

From source file:io.v.syncslides.SignInActivity.java

private void fetchUserProfile() {
    AccountManager manager = (AccountManager) getSystemService(Context.ACCOUNT_SERVICE);
    Account[] accounts = manager.getAccountsByType("com.google");
    Account account = null;/*from  w  ww  .j  a  va2 s  .  c o  m*/
    for (int i = 0; i < accounts.length; i++) {
        if (accounts[i].name.equals(mAccountName)) {
            account = accounts[i];
            break;
        }
    }
    if (account == null) {
        Log.e(TAG, "Couldn't find Google account with name: " + mAccountName);
        pickAccount();
        return;
    }
    manager.getAuthToken(account, OAUTH_SCOPE, new Bundle(), false, new OnTokenAcquired(), new Handler(msg -> {
        Log.e(TAG, "Error getting auth token: " + msg.toString());
        fetchUserProfileDone(null);
        return true;
    }));
}

From source file:com.cloudtask1.AccountsActivity.java

/**
 * Registers for C2DM messaging with the given account name.
 * /*from   www .j a v  a2 s  .  c  om*/
 * @param accountName a String containing a Google account name
 */
private void register(final String accountName) {
    // Store the account name in shared preferences
    final SharedPreferences prefs = Util.getSharedPreferences(mContext);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(Util.ACCOUNT_NAME, accountName);
    editor.remove(Util.AUTH_COOKIE);
    editor.remove(Util.DEVICE_REGISTRATION_ID);
    editor.commit();

    // Obtain an auth token and register
    final AccountManager mgr = AccountManager.get(mContext);
    Account[] accts = mgr.getAccountsByType("com.google");
    for (Account acct : accts) {
        final Account account = acct;
        if (account.name.equals(accountName)) {
            if (Util.isDebug(mContext)) {
                // Use a fake cookie for the dev mode app engine server
                // The cookie has the form email:isAdmin:userId
                // We set the userId to be the same as the email
                String authCookie = "dev_appserver_login=" + accountName + ":false:" + accountName;
                prefs.edit().putString(Util.AUTH_COOKIE, authCookie).commit();
                C2DMessaging.register(mContext, Setup.SENDER_ID);
            } else {
                // Get the auth token from the AccountManager and convert
                // it into a cookie for the appengine server
                final Activity activity = this;
                mgr.getAuthToken(account, "ah", null, activity, new AccountManagerCallback<Bundle>() {
                    public void run(AccountManagerFuture<Bundle> future) {
                        String authToken = getAuthToken(future);
                        // Ensure the token is not expired by invalidating it and
                        // obtaining a new one
                        mgr.invalidateAuthToken(account.type, authToken);
                        mgr.getAuthToken(account, "ah", null, activity, new AccountManagerCallback<Bundle>() {
                            public void run(AccountManagerFuture<Bundle> future) {
                                String authToken = getAuthToken(future);
                                // Convert the token into a cookie for future use
                                String authCookie = getAuthCookie(authToken);
                                Editor editor = prefs.edit();
                                editor.putString(Util.AUTH_COOKIE, authCookie);
                                editor.commit();
                                C2DMessaging.register(mContext, Setup.SENDER_ID);
                            }
                        }, null);
                    }
                }, null);
            }
            break;
        }
    }
}

From source file:com.masteriti.manager.AccountsActivity.java

/**
 * Registers for C2DM messaging with the given account name.
 * //from  w  w  w . j a  va  2s .c om
 * @param accountName a String containing a Google account name
 */
private void register(final String accountName) {
    // Store the account name in shared preferences
    final SharedPreferences prefs = Util.getSharedPreferences(mContext);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(Util.ACCOUNT_NAME, accountName);
    editor.remove(Util.AUTH_COOKIE);
    editor.remove(Util.DEVICE_REGISTRATION_ID);
    editor.commit();

    // Obtain an auth token and register
    final AccountManager mgr = AccountManager.get(mContext);
    Account[] accts = mgr.getAccountsByType("com.google");
    for (Account acct : accts) {
        final Account account = acct;
        if (account.name.equals(accountName)) {
            if (Util.isDebug(mContext)) {
                // Use a fake cookie for the dev mode app engine server
                // The cookie has the form email:isAdmin:userId
                // We set the userId to be the same as the email
                String authCookie = "dev_appserver_login=" + accountName + ":false:" + accountName;
                prefs.edit().putString(Util.AUTH_COOKIE, authCookie).commit();
                C2DMessaging.register(mContext, Setup.SENDER_ID);
            } else {
                // Get the auth token from the AccountManager and convert
                // it into a cookie for the appengine server
                final Activity activity = this;
                mgr.getAuthToken(account, "ah", null, activity, new AccountManagerCallback<Bundle>() {
                    @Override
                    public void run(AccountManagerFuture<Bundle> future) {
                        String authToken = getAuthToken(future);
                        // Ensure the token is not expired by invalidating it and
                        // obtaining a new one
                        mgr.invalidateAuthToken(account.type, authToken);
                        mgr.getAuthToken(account, "ah", null, activity, new AccountManagerCallback<Bundle>() {
                            @Override
                            public void run(AccountManagerFuture<Bundle> future) {
                                String authToken = getAuthToken(future);
                                // Convert the token into a cookie for future use
                                String authCookie = getAuthCookie(authToken);
                                Editor editor = prefs.edit();
                                editor.putString(Util.AUTH_COOKIE, authCookie);
                                editor.commit();
                                C2DMessaging.register(mContext, Setup.SENDER_ID);
                            }
                        }, null);
                    }
                }, null);
            }
            break;
        }
    }
}

From source file:org.tigase.messenger.phone.pro.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);// w  ww. ja v a 2  s  .c om

    /*
     * FloatingActionButton fab = (FloatingActionButton)
     * findViewById(R.id.fab); fab.setOnClickListener(new
     * View.OnClickListener() {
     *
     * @Override public void onClick(View view) { Snackbar.make(view,
     * "Replace with your own action", Snackbar.LENGTH_LONG)
     * .setAction("Action", null).show(); } });
     */

    final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    this.navigationMenu = navigationView.getMenu();

    View headerLayout = navigationView.getHeaderView(0);
    this.statusSelector = (Spinner) headerLayout.findViewById(R.id.status_selector);

    final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    switch (sharedPref.getString("menu", "roster")) {
    case "roster":
        switchMainFragment(R.id.nav_roster);
        break;
    case "connectionstatus":
        switchMainFragment(R.id.nav_connectionstatus);
        break;
    default:
        switchMainFragment(R.id.nav_chats);
        break;
    }

    StatusSelectorAdapter statusAdapter = StatusSelectorAdapter.instance(this);
    statusSelector.setAdapter(statusAdapter);
    statusSelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            doPresenceChange(id);
            drawer.closeDrawers();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
    statusSelector.setSelection(statusAdapter.getPosition(sharedPref.getLong("presence", CPresence.OFFLINE)));

    final AccountManager am = AccountManager.get(this);
    Account[] accounts = am.getAccountsByType(Authenticator.ACCOUNT_TYPE);
    if (accounts == null || accounts.length == 0) {
        Intent intent = new Intent(this, NewAccountActivity.class);
        startActivity(intent);
    }
}

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

private void updateDestinationAccountSelector() {
    AccountManager accountManager = AccountManager.get(this);
    Account[] accounts = accountManager.getAccountsByType(Constants.ACCOUNT_TYPE);
    accountAvailable = false;// w w  w  .  j  a va2  s.c o m
    boolean clearAdapter = true;
    accountName = null;
    if (accounts.length > 0) {
        accountAvailable = true;
        if (accounts.length > 1) {
            // Account-Name Auswahl anzeigen
            clearAdapter = false;
            String[] names = new String[accounts.length];
            boolean changed = false;
            int oldCount = destAccountAdapter.getCount();
            for (int i = 0; i < accounts.length; i++) {
                names[i] = accounts[i].name;
                if (i >= oldCount || !destAccountAdapter.getItem(i).equals(names[i])) {
                    changed = true;
                }
            }
            if (changed) {
                destAccountAdapter.setNotifyOnChange(false);
                destAccountAdapter.clear();
                for (String name : names) {
                    destAccountAdapter.add(name);
                }
                destAccountAdapter.setNotifyOnChange(true);
                destAccountAdapter.notifyDataSetChanged();
            }
        } else {
            accountName = accounts[0].name;
        }
    }
    if (clearAdapter) {
        destAccountAdapter.clear();
    }
}

From source file:io.v.android.apps.syncslides.SignInActivity.java

private void fetchUserProfile() {
    if (!FETCH_PROFILE) {
        fetchUserProfileDone(null);/*  www.  j a va  2 s . c  o  m*/
        return;
    }
    AccountManager manager = (AccountManager) getSystemService(Context.ACCOUNT_SERVICE);
    Account[] accounts = manager.getAccountsByType("com.google");
    Account account = null;
    for (int i = 0; i < accounts.length; i++) {
        if (accounts[i].name.equals(mAccountName)) {
            account = accounts[i];
            break;
        }
    }
    if (account == null) {
        Log.e(TAG, "Couldn't find Google account with name: " + mAccountName);
        pickAccount();
        return;
    }
    manager.getAuthToken(account, OAUTH_SCOPE, new Bundle(), false, new OnTokenAcquired(),
            new Handler(new Handler.Callback() {
                @Override
                public boolean handleMessage(Message msg) {
                    Log.e(TAG, "Error getting auth token: " + msg.toString());
                    fetchUserProfileDone(null);
                    return true;
                }
            }));
}

From source file:com.listomate.activities.AccountsActivity.java

/**
 * Registers for C2DM messaging with the given account name.
 * //from ww w.j a v a2s  . co  m
 * @param accountName a String containing a Google account name
 */
private void register(final String accountName) {
    // Store the account name in shared preferences
    final SharedPreferences prefs = Util.getSharedPreferences(mContext);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(Util.ACCOUNT_NAME, accountName);
    editor.putString(Util.AUTH_COOKIE, null);
    editor.commit();

    // Obtain an auth token and register
    AccountManager mgr = AccountManager.get(mContext);
    Account[] accts = mgr.getAccountsByType("com.google");
    for (Account acct : accts) {
        if (acct.name.equals(accountName)) {
            if (Util.isDebug(mContext)) {
                // Use a fake cookie for the dev mode app engine server
                // The cookie has the form email:isAdmin:userId
                // We set the userId to be the same as the account name
                String authCookie = "dev_appserver_login=" + accountName + ":false:" + accountName;
                prefs.edit().putString(Util.AUTH_COOKIE, authCookie).commit();
                C2DMessaging.register(mContext, Setup.SENDER_ID);
            } else {
                // Get the auth token from the AccountManager and convert
                // it into a cookie for the appengine server
                mgr.getAuthToken(acct, "ah", null, this, new AccountManagerCallback<Bundle>() {
                    public void run(AccountManagerFuture<Bundle> future) {
                        try {
                            Bundle authTokenBundle = future.getResult();
                            String authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
                            String authCookie = getAuthCookie(authToken);
                            prefs.edit().putString(Util.AUTH_COOKIE, authCookie).commit();

                            C2DMessaging.register(mContext, Setup.SENDER_ID);
                        } catch (AuthenticatorException e) {
                            Log.w(TAG, "Got AuthenticatorException " + e);
                            Log.w(TAG, Log.getStackTraceString(e));
                        } catch (IOException e) {
                            Log.w(TAG, "Got IOException " + Log.getStackTraceString(e));
                            Log.w(TAG, Log.getStackTraceString(e));
                        } catch (OperationCanceledException e) {
                            Log.w(TAG, "Got OperationCanceledException " + e);
                            Log.w(TAG, Log.getStackTraceString(e));
                        }
                    }
                }, null);
            }
            break;
        }
    }
}

From source file:com.capstonecontrol.AccountsActivity.java

/**
 * Registers for C2DM messaging with the given account name.
 * //from  w w w  .j  a  v  a  2  s.  co  m
 * @param accountName
 *            a String containing a Google account name
 */
private void register(final String accountName) {
    // Store the account name in shared preferences
    final SharedPreferences prefs = Util.getSharedPreferences(mContext);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(Util.ACCOUNT_NAME, accountName);
    editor.remove(Util.AUTH_COOKIE);
    editor.remove(Util.DEVICE_REGISTRATION_ID);
    editor.commit();

    // Obtain an auth token and register
    final AccountManager mgr = AccountManager.get(mContext);
    Account[] accts = mgr.getAccountsByType("com.google");
    for (Account acct : accts) {
        final Account account = acct;
        if (account.name.equals(accountName)) {
            if (Util.isDebug(mContext)) {
                // Use a fake cookie for the dev mode app engine server
                // The cookie has the form email:isAdmin:userId
                // We set the userId to be the same as the email
                String authCookie = "dev_appserver_login=" + accountName + ":false:" + accountName;
                prefs.edit().putString(Util.AUTH_COOKIE, authCookie).commit();
                C2DMessaging.register(mContext, Setup.SENDER_ID);
            } else {
                // Get the auth token from the AccountManager and convert
                // it into a cookie for the appengine server
                final Activity activity = this;
                mgr.getAuthToken(account, "ah", null, activity, new AccountManagerCallback<Bundle>() {
                    public void run(AccountManagerFuture<Bundle> future) {
                        String authToken = getAuthToken(future);
                        // Ensure the token is not expired by
                        // invalidating it and
                        // obtaining a new one
                        mgr.invalidateAuthToken(account.type, authToken);
                        mgr.getAuthToken(account, "ah", null, activity, new AccountManagerCallback<Bundle>() {
                            public void run(AccountManagerFuture<Bundle> future) {
                                String authToken = getAuthToken(future);
                                // Convert the token into a
                                // cookie for future use
                                String authCookie = getAuthCookie(authToken);
                                Editor editor = prefs.edit();
                                editor.putString(Util.AUTH_COOKIE, authCookie);
                                editor.commit();
                                C2DMessaging.register(mContext, Setup.SENDER_ID);
                            }
                        }, null);
                    }
                }, null);
            }
            break;
        }
    }
}

From source file:com.nextgis.maplibui.SelectNGWResourceDialog.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ADDACCOUNT_CODE) {
        if (resultCode != Activity.RESULT_CANCELED) {
            //search new account and add it
            final AccountManager accountManager = AccountManager.get(getActivity());
            Connections connections = mListAdapter.getConnections();
            for (Account account : accountManager.getAccountsByType(NGW_ACCOUNT_TYPE)) {
                boolean find = false;
                for (int i = 0; i < connections.getChildrenCount(); i++) {
                    Connection connection = (Connection) connections.getChild(i);
                    if (null != connection && connection.getName().equals(account.name)) {
                        find = true;//  w w w . j a  v  a 2 s. co m
                        break;
                    }
                }

                if (!find) {
                    String url = accountManager.getUserData(account, "url");
                    String password = accountManager.getPassword(account);
                    String login = accountManager.getUserData(account, "login");
                    connections.add(new Connection(account.name, login, password, url));
                    mListAdapter.notifyDataSetChanged();
                    break;
                }
            }
        }
    } else
        super.onActivityResult(requestCode, resultCode, data);
}