Example usage for android.accounts AccountManager get

List of usage examples for android.accounts AccountManager get

Introduction

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

Prototype

public static AccountManager get(Context context) 

Source Link

Document

Gets an AccountManager instance associated with a Context.

Usage

From source file:at.bitfire.davdroid.ui.AccountActivity.java

private void deleteAccount() {
    AccountManager accountManager = AccountManager.get(this);

    if (Build.VERSION.SDK_INT >= 22)
        accountManager.removeAccount(account, this, new AccountManagerCallback<Bundle>() {
            @Override/*w w  w .j a  v a  2  s  .  com*/
            public void run(AccountManagerFuture<Bundle> future) {
                try {
                    if (future.getResult().getBoolean(AccountManager.KEY_BOOLEAN_RESULT))
                        finish();
                } catch (OperationCanceledException | IOException | AuthenticatorException e) {
                    App.log.log(Level.SEVERE, "Couldn't remove account", e);
                }
            }
        }, null);
    else
        accountManager.removeAccount(account, new AccountManagerCallback<Boolean>() {
            @Override
            public void run(AccountManagerFuture<Boolean> future) {
                try {
                    if (future.getResult())
                        finish();
                } catch (OperationCanceledException | IOException | AuthenticatorException e) {
                    App.log.log(Level.SEVERE, "Couldn't remove account", e);
                }
            }
        }, null);
}

From source file:com.mikecorrigan.bohrium.pubsub.RegistrationClient.java

private Account getAccount() {
    final AccountManager mgr = AccountManager.get(this);
    final Account[] accounts = mgr
            .getAccountsByType(mConfiguration.getString(ACCOUNT_TYPE, DEFAULT_ACCOUNT_TYPE));
    for (final Account account : accounts) {
        if (account.name.equals(mConfiguration.getString(ACCOUNT_NAME))) {
            return account;
        }//  ww  w . j  av  a2  s  .c  o  m
    }

    return null;
}

From source file:com.synox.android.ui.activity.FileActivity.java

/**
 * Launches the account creation activity. To use when no ownCloud account is available
 *///from w  w w. ja  va2  s  .c om
private void createFirstAccount() {
    AccountManager am = AccountManager.get(getApplicationContext());
    am.addAccount(MainApp.getAccountType(), null, null, null, this, new AccountCreationCallback(), null);
}

From source file:com.mikecorrigan.bohrium.pubsub.RegistrationClient.java

private void requestAuthToken() {
    Log.v(TAG, "requestAuthToken");

    final AccountManager mgr = AccountManager.get(this);
    final Account account = getAccount();
    if (account == null) {
        Log.e(TAG,/*from   w w w . ja v a 2s .  c o m*/
                "Failed to find account: accountType="
                        + mConfiguration.getString(ACCOUNT_TYPE, DEFAULT_ACCOUNT_TYPE) + ", accountName="
                        + mConfiguration.getString(ACCOUNT_NAME));
        setStateAndNotify(REGISTRATION_STATE_ERROR, REGISTRATION_SUBSTATE_ERROR_AUTH_TOKEN);
        return;
    }

    mgr.getAuthToken(account, "ah", false, new AuthTokenCallback(), mHandler);
}

From source file:com.android.settings.HWSettings.java

@Override
public void onDestroy() {
    super.onDestroy();
    if (mListeningToAccountUpdates) {
        AccountManager.get(this).removeOnAccountsUpdatedListener(this);
    }/*w ww.j av  a 2  s  .  co  m*/
}

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  a  va2  s. c  o 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.quarterfull.newsAndroid.NewsReaderListActivity.java

public void startSync() {
    SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);

    if (mPrefs.getString(SettingsActivity.EDT_OWNCLOUDROOTPATH_STRING, null) == null)
        StartLoginFragment(this);
    else {//  www . j  a  v  a2 s  . com
        if (!ownCloudSyncService.isSyncRunning()) {
            new PostDelayHandler(this).stopRunningPostDelayHandler();//Stop pending sync handler

            Bundle accBundle = new Bundle();
            accBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
            AccountManager mAccountManager = AccountManager.get(this);
            Account[] accounts = mAccountManager.getAccounts();
            for (Account acc : accounts)
                if (acc.type.equals(AccountGeneral.ACCOUNT_TYPE))
                    ContentResolver.requestSync(acc, AccountGeneral.ACCOUNT_TYPE, accBundle);
            //http://stackoverflow.com/questions/5253858/why-does-contentresolver-requestsync-not-trigger-a-sync
        } else {
            UpdateButtonLayout();
        }

    }
}

From source file:com.mikecorrigan.bohrium.pubsub.RegistrationClient.java

@SafeVarargs
private final void handleAuthToken(AccountManagerFuture<Bundle>... tokens) {
    Log.v(TAG, "handleAuthToken");

    try {/*  w w  w. j a  va  2  s  .com*/
        Bundle result = tokens[0].getResult();

        Intent intent = (Intent) result.get(AccountManager.KEY_INTENT);
        if (intent != null) {
            Log.i(TAG, "Launch activity before getting authToken: intent=" + intent);

            setStateAndNotify(REGISTRATION_STATE_REGISTERING, REGISTRATION_SUBSTATE_PROMPTING_USER);

            intent.setFlags(intent.getFlags() & ~Intent.FLAG_ACTIVITY_NEW_TASK);
            notifyLaunchIntent(intent);
            return;
        }

        String authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
        if (mNeedInvalidate) {
            mNeedInvalidate = false;

            Log.i(TAG, "Invalidating token and starting over.");

            // Invalidate auth token.
            AccountManager mgr = AccountManager.get(this);
            mgr.invalidateAuthToken(mConfiguration.getString(ACCOUNT_TYPE, DEFAULT_ACCOUNT_TYPE), authToken);

            setStateAndNotify(REGISTRATION_STATE_REGISTERING, REGISTRATION_SUBSTATE_INVALIDATED_AUTH_TOKEN);

            // Initiate the request again.
            requestAuthToken();
            return;
        } else {
            Log.i(TAG, "Received authToken=" + authToken);
            mConfiguration.putString(AUTH_TOKEN, authToken);
            setStateAndNotify(REGISTRATION_STATE_REGISTERING, REGISTRATION_SUBSTATE_HAVE_AUTH_TOKEN);

            // Move on to the next step, request auth cookie.
            requestAuthCookie();
            return;
        }
    } catch (Exception e) {
        Log.e(TAG, "Exception " + e);
        Log.e(TAG, Log.getStackTraceString(e));
    }

    setStateAndNotify(REGISTRATION_STATE_ERROR, REGISTRATION_SUBSTATE_ERROR_AUTH_TOKEN);
}

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.
 *///from w  w w  .j a v a  2 s  . com
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);
}