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.google.samples.apps.sergio.ui.BaseActivity.java

private void signInOrCreateAnAccount() {
    //Get list of accounts on device.
    AccountManager am = AccountManager.get(BaseActivity.this);
    Account[] accountArray = am.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
    if (accountArray.length == 0) {
        //Send the user to the "Add Account" page.
        Intent intent = new Intent(Settings.ACTION_ADD_ACCOUNT);
        intent.putExtra(Settings.EXTRA_ACCOUNT_TYPES, new String[] { "com.google" });
        startActivity(intent);/*from  www . java2 s  .  c  om*/
    } else {
        //Try to log the user in with the first account on the device.
        startLoginProcess();
        mDrawerLayout.closeDrawer(Gravity.START);
    }
}

From source file:com.saarang.samples.apps.iosched.ui.BaseActivity.java

/**
 * Returns the default account on the device. We use the rule that the first account
 * should be the default. It's arbitrary, but the alternative would be showing an account
 * chooser popup which wouldn't be a smooth first experience with the app. Since the user
 * can easily switch the account with the nav drawer, we opted for this implementation.
 *//*from w  w  w . j av a 2 s  .co  m*/
private String getDefaultAccount() {
    // Choose first account on device.
    LogUtils.LOGD(TAG, "Choosing default account (first account on device)");
    AccountManager am = AccountManager.get(this);
    Account[] accounts = am.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
    if (accounts.length == 0) {
        // No Google accounts on device.
        LogUtils.LOGW(TAG, "No Google accounts on device; not setting default account.");
        return null;
    }

    LogUtils.LOGD(TAG, "Default account is: " + accounts[0].name);
    return accounts[0].name;
}

From source file:com.google.samples.apps.sergio.ui.BaseActivity.java

/**
 * Sets up the account box. The account box is the area at the top of the nav drawer that
 * shows which account the user is logged in as, and lets them switch accounts. It also
 * shows the user's Google+ cover photo as background.
 *//*from w  w  w .j  av a 2 s . co m*/
private void setupAccountBox() {
    mAccountListContainer = (LinearLayout) findViewById(R.id.account_list);

    if (mAccountListContainer == null) {
        //This activity does not have an account box
        return;
    }

    final View chosenAccountView = findViewById(R.id.chosen_account_view);
    Account chosenAccount = AccountUtils.getActiveAccount(this);
    if (chosenAccount == null) {
        // No account logged in; hide account box
        chosenAccountView.setVisibility(View.GONE);
        mAccountListContainer.setVisibility(View.GONE);
        return;
    } else {
        chosenAccountView.setVisibility(View.VISIBLE);
        mAccountListContainer.setVisibility(View.INVISIBLE);
    }

    AccountManager am = AccountManager.get(this);
    Account[] accountArray = am.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
    List<Account> accounts = new ArrayList<Account>(Arrays.asList(accountArray));
    accounts.remove(chosenAccount);

    ImageView coverImageView = (ImageView) chosenAccountView.findViewById(R.id.profile_cover_image);
    ImageView profileImageView = (ImageView) chosenAccountView.findViewById(R.id.profile_image);
    TextView nameTextView = (TextView) chosenAccountView.findViewById(R.id.profile_name_text);
    TextView email = (TextView) chosenAccountView.findViewById(R.id.profile_email_text);
    mExpandAccountBoxIndicator = (ImageView) findViewById(R.id.expand_account_box_indicator);

    String name = AccountUtils.getPlusName(this);
    if (name == null) {
        nameTextView.setVisibility(View.GONE);
    } else {
        nameTextView.setVisibility(View.VISIBLE);
        nameTextView.setText(name);
    }

    String imageUrl = AccountUtils.getPlusImageUrl(this);
    if (imageUrl != null) {
        mImageLoader.loadImage(imageUrl, profileImageView);
    }

    String coverImageUrl = AccountUtils.getPlusCoverUrl(this);
    if (coverImageUrl != null) {
        mImageLoader.loadImage(coverImageUrl, coverImageView);
    } else {
        coverImageView.setImageResource(R.drawable.default_cover);
    }

    email.setText(chosenAccount.name);

    if (accounts.isEmpty()) {
        // There's only one account on the device, so no need for a switcher.
        mExpandAccountBoxIndicator.setVisibility(View.GONE);
        mAccountListContainer.setVisibility(View.GONE);
        chosenAccountView.setEnabled(false);
        return;
    }

    chosenAccountView.setEnabled(true);

    mExpandAccountBoxIndicator.setVisibility(View.VISIBLE);
    chosenAccountView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mAccountBoxExpanded = !mAccountBoxExpanded;
            setupAccountBoxToggle();
        }
    });
    setupAccountBoxToggle();

    populateAccountList(accounts);
}

From source file:com.google.samples.apps.iosched.ui.BaseActivity.java

private void signInOrCreateAnAccount() {
    //Get list of accounts on device.
    AccountManager am = AccountManager.get(BaseActivity.this);
    Account[] accountArray = am.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
    if (accountArray.length == 0) {
        //Send the user to the "Add Account" page.
        Intent intent = new Intent(Settings.ACTION_ADD_ACCOUNT);
        intent.putExtra(Settings.EXTRA_ACCOUNT_TYPES, new String[] { GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE });
        startActivity(intent);/* w w w .j av  a2s .c om*/
    } else {
        //Try to log the user in with the first account on the device.
        startLoginProcess();
        mDrawerLayout.closeDrawer(GravityCompat.START);
    }
}

From source file:com.saarang.samples.apps.iosched.ui.BaseActivity.java

/**
 * Sets up the account box. The account box is the area at the top of the nav drawer that
 * shows which account the user is logged in as, and lets them switch accounts. It also
 * shows the user's Google+ cover photo as background.
 *//*from w  w  w. ja v a  2s . c  o m*/
private void setupAccountBox() {
    mAccountListContainer = (LinearLayout) findViewById(com.saarang.samples.apps.iosched.R.id.account_list);

    if (mAccountListContainer == null) {
        //This activity does not have an account box
        return;
    }

    final View chosenAccountView = findViewById(com.saarang.samples.apps.iosched.R.id.chosen_account_view);
    Account chosenAccount = AccountUtils.getActiveAccount(this);
    if (chosenAccount == null) {
        // No account logged in; hide account box
        chosenAccountView.setVisibility(View.GONE);
        mAccountListContainer.setVisibility(View.GONE);
        return;
    } else {
        chosenAccountView.setVisibility(View.VISIBLE);
        mAccountListContainer.setVisibility(View.INVISIBLE);
    }

    AccountManager am = AccountManager.get(this);
    Account[] accountArray = am.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
    List<Account> accounts = new ArrayList<Account>(Arrays.asList(accountArray));
    accounts.remove(chosenAccount);

    ImageView coverImageView = (ImageView) chosenAccountView
            .findViewById(com.saarang.samples.apps.iosched.R.id.profile_cover_image);
    ImageView profileImageView = (ImageView) chosenAccountView
            .findViewById(com.saarang.samples.apps.iosched.R.id.profile_image);
    TextView nameTextView = (TextView) chosenAccountView
            .findViewById(com.saarang.samples.apps.iosched.R.id.profile_name_text);
    TextView email = (TextView) chosenAccountView
            .findViewById(com.saarang.samples.apps.iosched.R.id.profile_email_text);
    mExpandAccountBoxIndicator = (ImageView) findViewById(
            com.saarang.samples.apps.iosched.R.id.expand_account_box_indicator);

    String name = AccountUtils.getPlusName(this);
    if (name == null) {
        nameTextView.setVisibility(View.GONE);
    } else {
        nameTextView.setVisibility(View.VISIBLE);
        nameTextView.setText(name);
    }

    String imageUrl = AccountUtils.getPlusImageUrl(this);
    if (imageUrl != null) {
        mImageLoader.loadImage(imageUrl, profileImageView);
    }

    String coverImageUrl = AccountUtils.getPlusCoverUrl(this);
    if (coverImageUrl != null) {
        mImageLoader.loadImage(coverImageUrl, coverImageView);
    } else {
        coverImageView.setImageResource(R.drawable.bg_cover_2016);
    }

    email.setText(chosenAccount.name);

    if (accounts.isEmpty()) {
        // There's only one account on the device, so no need for a switcher.
        mExpandAccountBoxIndicator.setVisibility(View.GONE);
        mAccountListContainer.setVisibility(View.GONE);
        chosenAccountView.setEnabled(false);
        return;
    }

    chosenAccountView.setEnabled(true);

    mExpandAccountBoxIndicator.setVisibility(View.VISIBLE);
    chosenAccountView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mAccountBoxExpanded = !mAccountBoxExpanded;
            setupAccountBoxToggle();
        }
    });
    setupAccountBoxToggle();

    populateAccountList(accounts);
}

From source file:com.microsoft.windowsazure.mobileservices.MobileServiceClient.java

/**
 * Invokes Microsoft Azure Mobile Service authentication using a the Google
 * account registered in the device/*  www .  j  a  v a2s .  c  om*/
 *
 * @param activity The activity that triggered the authentication
 * @param scopes   The scopes used as authentication token type for login
 */
public ListenableFuture<MobileServiceUser> loginWithGoogleAccount(Activity activity, String scopes) {
    AccountManager acMgr = AccountManager.get(activity.getApplicationContext());
    Account[] accounts = acMgr.getAccountsByType(GOOGLE_ACCOUNT_TYPE);

    Account account;
    if (accounts.length == 0) {
        account = null;
    } else {
        account = accounts[0];
    }

    return loginWithGoogleAccount(activity, account, scopes);
}

From source file:com.google.samples.apps.iosched.ui.BaseActivity.java

/**
 * Sets up the account box. The account box is the area at the top of the nav drawer that
 * shows which account the user is logged in as, and lets them switch accounts. It also
 * shows the user's Google+ cover photo as background.
 *///  w w w .  ja  va  2  s  .  co  m
private void setupAccountBox() {
    mAccountListContainer = (LinearLayout) findViewById(R.id.account_list);

    if (mAccountListContainer == null) {
        //This activity does not have an account box
        return;
    }

    final View chosenAccountView = findViewById(R.id.chosen_account_view);
    Account chosenAccount = AccountUtils.getActiveAccount(this);
    if (chosenAccount == null) {
        // No account logged in; hide account box
        chosenAccountView.setVisibility(View.GONE);
        mAccountListContainer.setVisibility(View.GONE);
        return;
    } else {
        chosenAccountView.setVisibility(View.VISIBLE);
        mAccountListContainer.setVisibility(View.INVISIBLE);
    }

    AccountManager am = AccountManager.get(this);
    Account[] accountArray = am.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
    List<Account> accounts = new ArrayList<Account>(Arrays.asList(accountArray));
    accounts.remove(chosenAccount);

    ImageView coverImageView = (ImageView) chosenAccountView.findViewById(R.id.profile_cover_image);
    ImageView profileImageView = (ImageView) chosenAccountView.findViewById(R.id.profile_image);
    TextView nameTextView = (TextView) chosenAccountView.findViewById(R.id.profile_name_text);
    TextView email = (TextView) chosenAccountView.findViewById(R.id.profile_email_text);
    mExpandAccountBoxIndicator = (ImageView) findViewById(R.id.expand_account_box_indicator);

    String name = AccountUtils.getPlusName(this);
    if (name == null) {
        nameTextView.setVisibility(View.GONE);
    } else {
        nameTextView.setVisibility(View.VISIBLE);
        nameTextView.setText(name);
    }

    String imageUrl = AccountUtils.getPlusImageUrl(this);
    if (imageUrl != null) {
        mImageLoader.loadImage(imageUrl, profileImageView);
    }

    String coverImageUrl = AccountUtils.getPlusCoverUrl(this);
    if (coverImageUrl != null) {
        findViewById(R.id.profile_cover_image_placeholder).setVisibility(View.GONE);
        coverImageView.setVisibility(View.VISIBLE);
        coverImageView.setContentDescription(
                getResources().getString(R.string.navview_header_user_image_content_description));
        mImageLoader.loadImage(coverImageUrl, coverImageView);
        coverImageView.setColorFilter(getResources().getColor(R.color.light_content_scrim));
    }

    email.setText(chosenAccount.name);

    if (accounts.isEmpty()) {
        // There's only one account on the device, so no need for a switcher.
        mExpandAccountBoxIndicator.setVisibility(View.GONE);
        mAccountListContainer.setVisibility(View.GONE);
        chosenAccountView.setEnabled(false);
        return;
    }

    chosenAccountView.setEnabled(true);

    mExpandAccountBoxIndicator.setVisibility(View.VISIBLE);
    chosenAccountView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mAccountBoxExpanded = !mAccountBoxExpanded;
            setupAccountBoxToggle();
        }
    });
    setupAccountBoxToggle();

    populateAccountList(accounts);
}

From source file:co.beem.project.beem.FbTextService.java

/**
 * Get the specified Android account./*from   www . j  a va 2  s.  c  o m*/
 * 
 * @param accountName
 *            the account name
 * @param accountType
 *            the account type
 * 
 * @return the account or null if it does not exist
 */
private Account getAccount(String accountName, String accountType) {
    AccountManager am = AccountManager.get(this);
    for (Account a : am.getAccountsByType(accountType)) {
        if (a.name.equals(accountName)) {
            return a;
        }
    }
    return null;
}

From source file:org.linphone.ContactsManager.java

public void initializeSyncAccount(Context context, ContentResolver contentResolver) {
    initializeContactManager(context, contentResolver);
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

    Account[] accounts = accountManager.getAccountsByType(context.getPackageName());

    if (accounts != null && accounts.length == 0) {
        Account newAccount = new Account(context.getString(R.string.sync_account_name),
                context.getPackageName());
        try {//from w  ww  .j ava 2 s  . com
            accountManager.addAccountExplicitly(newAccount, null, null);
            mAccount = newAccount;
        } catch (Exception e) {
            Log.e(e);
            mAccount = null;
        }
    } else {
        mAccount = accounts[0];
    }
    initializeContactManager(context, contentResolver);
}

From source file:org.tigase.messenger.phone.pro.service.XMPPService.java

private final void updateJaxmppInstances() {
    final HashSet<BareJID> accountsJids = new HashSet<BareJID>();
    for (JaxmppCore jaxmpp : multiJaxmpp.get()) {
        accountsJids.add(jaxmpp.getSessionObject().getUserBareJid());
    }//  ww w  .java  2  s  .  c  o m

    final AccountManager am = AccountManager.get(this);
    for (Account account : am.getAccountsByType(Authenticator.ACCOUNT_TYPE)) {
        BareJID accountJid = BareJID.bareJIDInstance(account.name);
        Jaxmpp jaxmpp = multiJaxmpp.get(accountJid);
        if (jaxmpp == null) {
            jaxmpp = createJaxmpp(accountJid, account.hashCode());
            multiJaxmpp.add(jaxmpp);
        }

        // workaround for unknown certificate error
        jaxmpp.getSessionObject().setProperty("jaxmpp#ThrowedException", null);

        String password = am.getPassword(account);
        String nickname = am.getUserData(account, AccountsConstants.FIELD_NICKNAME);
        String hostname = am.getUserData(account, AccountsConstants.FIELD_HOSTNAME);
        String resource = am.getUserData(account, AccountsConstants.FIELD_RESOURCE);
        hostname = hostname == null ? null : hostname.trim();

        jaxmpp.getSessionObject().setUserProperty(SessionObject.PASSWORD, password);
        jaxmpp.getSessionObject().setUserProperty(SessionObject.NICKNAME, nickname);
        if (hostname != null && TextUtils.isEmpty(hostname))
            hostname = null;
        // sessionObject.setUserProperty(SessionObject.DOMAIN_NAME,
        // hostname);
        if (TextUtils.isEmpty(resource))
            resource = null;
        jaxmpp.getSessionObject().setUserProperty(SessionObject.RESOURCE, resource);

        MobileModeFeature.updateSettings(account, jaxmpp, this);

        boolean disabled = !Boolean.parseBoolean(am.getUserData(account, AccountsConstants.FIELD_ACTIVE));
        jaxmpp.getSessionObject().setUserProperty("CC:DISABLED", disabled);

        if (disabled) {
            if (jaxmpp.isConnected()) {
                this.disconnectJaxmpp(jaxmpp, true);
            }
        } else {
            if (!jaxmpp.isConnected()) {
                this.connectJaxmpp(jaxmpp, 1L);
            }
        }

        accountsJids.remove(accountJid);
    }

    for (BareJID accountJid : accountsJids) {
        final Jaxmpp jaxmpp = multiJaxmpp.get(accountJid);
        if (jaxmpp != null) {
            multiJaxmpp.remove(jaxmpp);
            (new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {
                    try {
                        jaxmpp.disconnect();
                        // clear presences for account?
                        // app.clearPresences(jaxmpp.getSessionObject(),
                        // false);
                        // is this needed any more??
                        // JaxmppService.this.rosterProvider.resetStatus(jaxmpp.getSessionObject());
                    } catch (Exception ex) {
                        Log.e(TAG, "Can't disconnect", ex);
                    }

                    return null;
                }
            }).execute();
        }
    }

    dataRemover.removeUnusedData(this);
}