Example usage for android.accounts AccountManager addAccount

List of usage examples for android.accounts AccountManager addAccount

Introduction

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

Prototype

public AccountManagerFuture<Bundle> addAccount(final String accountType, final String authTokenType,
        final String[] requiredFeatures, final Bundle addAccountOptions, final Activity activity,
        AccountManagerCallback<Bundle> callback, Handler handler) 

Source Link

Document

Asks the user to add an account of a specified type.

Usage

From source file:Main.java

/**
 * Add account//from ww w . j a  v a  2s. c o  m
 * @param accountType accountType
 * @param authTokenType authTokenType
 * @param requiredFeatures requiredFeatures, could be <code>null</code>
 * @param options options, could be <code>null</code>
 * @param activity activity (cannot be <code>null</code>)
 */
public static void addAccount(String accountType, String authTokenType, String[] requiredFeatures,
        Bundle options, Activity activity) {
    if (activity == null) {
        throw new IllegalArgumentException("activity cannot be null");
    }
    final AccountManager accountManager = AccountManager.get(activity.getApplicationContext());
    accountManager.addAccount(accountType, authTokenType, requiredFeatures, options, activity, null, null);
}

From source file:Main.java

/**
 * Invalidate auth token for specified account
 * @param account account to invalidate auth token
 * @param authTokenType auth token type/*from   w w  w  .j ava  2s .  c  o  m*/
 * @param requiredFeatures requiredFeatures, could be <code>null</code>
 * @param options options, could be <code>null</code>
 * @param activity activity (cannot be <code>null</code>)
 */
public static void invalidateAuthToken(Account account, String authTokenType, String[] requiredFeatures,
        Bundle options, Activity activity) {
    if (activity == null) {
        throw new IllegalArgumentException("activity cannot be null");
    }
    if (account == null) {
        throw new IllegalArgumentException("account cannot be null");
    }
    Context context = activity.getApplicationContext();
    final AccountManager am = AccountManager.get(context);
    String authToken = am.peekAuthToken(account, authTokenType);
    if (!TextUtils.isEmpty(authToken)) {
        am.invalidateAuthToken(account.type, authToken);
    }
    am.addAccount(account.type, authTokenType, requiredFeatures, options, activity, null, null);
}

From source file:Main.java

/**
 * Recreate account/*from ww w  . j a  v a  2 s.co  m*/
 * @param account account
 * @param authTokenType authTokenType
 * @param requiredFeatures requiredFeatures, could be <code>null</code>
 * @param options options, could be <code>null</code>
 * @param activity activity (cannot be <code>null</code>)
 */
public static void reCreateAccount(Account account, String accountType, String authTokenType,
        String[] requiredFeatures, Bundle options, Activity activity) {
    if (activity == null) {
        throw new IllegalArgumentException("activity cannot be null");
    }

    final AccountManager accountManager = AccountManager.get(activity.getApplicationContext());
    if (isAccountExist(activity, account)) {
        accountManager.removeAccount(account, null, null);
    }

    accountManager.addAccount(accountType, authTokenType, requiredFeatures, options, activity, null, null);
}

From source file:com.afwsamples.testdpc.AddAccountActivity.java

private void addAccount(String accountName) {
    AccountManager accountManager = AccountManager.get(this);
    Bundle bundle = new Bundle();
    if (!TextUtils.isEmpty(accountName)) {
        bundle.putString(AccountManager.KEY_ACCOUNT_NAME, accountName);
    }//w w  w .  j a  v a  2  s. c o m

    accountManager.addAccount(GOOGLE_ACCOUNT_TYPE, null, null, bundle, this, accountManagerFuture -> {
        try {
            Bundle result = accountManagerFuture.getResult();
            String accountNameAdded = result.getString(AccountManager.KEY_ACCOUNT_NAME);
            Log.d(TAG, "addAccount - accountNameAdded: " + accountNameAdded);
            if (mNextActivityIntent != null) {
                startActivity(mNextActivityIntent);
            }
            finish();
        } catch (OperationCanceledException | AuthenticatorException | IOException e) {
            Log.e(TAG, "addAccount - failed", e);
            Toast.makeText(AddAccountActivity.this, R.string.fail_to_add_account, Toast.LENGTH_LONG).show();
        }
    }, null);
}

From source file:com.hemou.android.account.AccountUtils.java

/**
 * Get account used for authentication/*from  w w  w.j a v a 2s . c om*/
 * 
 * @param manager
 * @param act
 * @return account
 * @throws IOException
 * @throws AccountsException
 */
public static Account getAccount(final AccountManager manager, final Activity act)
        throws IOException, AccountsException {
    final String SUB_TAG = "acnt_get";
    final boolean loggable = Log.isLoggable(SUB_TAG, DEBUG);
    if (loggable)
        Log.d(SUB_TAG, "Getting account");

    if (act == null)
        throw new IllegalArgumentException("Activity cannot be null");

    if (act.isFinishing()) {
        Log.v(SUB_TAG, act.getClass().getName()
                + "--->?finish()OperationCanceledException...");
        throw new OperationCanceledException();
    }

    Account[] accounts;
    try {
        if (!hasAuthenticator(manager)) {
            Log.e(SUB_TAG, "Current user is not under the authenticated environment....");
            throw new AuthenticatorConflictException();
        }

        while ((accounts = getAccounts(manager)).length == 0) {
            Bundle result = manager.addAccount(ACCOUNT_TYPE, AUTHTOKEN_TYPE, null, null, act, null, null)
                    .getResult();
        }
    } catch (OperationCanceledException e) {
        Log.d(SUB_TAG, "Excepting retrieving account", e);
        act.finish();
        throw e;
    } catch (AccountsException e) {
        Log.d(SUB_TAG, "Excepting retrieving account", e);
        throw e;
    } catch (AuthenticatorConflictException e) {
        act.runOnUiThread(new Runnable() {

            public void run() {
                showConflictMessage(act);
            }
        });
        throw e;
    } catch (IOException e) {
        Log.d(SUB_TAG, "Excepting retrieving account", e);
        throw e;
    }

    // if (loggable)
    Log.d(SUB_TAG, "Returning account " + accounts[0].name);

    return accounts[0];
}

From source file:com.owncloud.android.ui.activity.ManageAccountsActivity.java

@Override
public void createAccount() {
    AccountManager am = AccountManager.get(getApplicationContext());
    am.addAccount(MainApp.getAccountType(), null, null, null, this, new AccountManagerCallback<Bundle>() {
        @Override/*from  www  .  j a  va  2s .  com*/
        public void run(AccountManagerFuture<Bundle> future) {
            if (future != null) {
                try {
                    Bundle result = future.getResult();
                    String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
                    AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name);
                    mAccountListAdapter = new AccountListAdapter(ManageAccountsActivity.this,
                            getAccountListItems(), mTintedCheck);
                    mListView.setAdapter(mAccountListAdapter);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mAccountListAdapter.notifyDataSetChanged();
                        }
                    });
                } catch (OperationCanceledException e) {
                    Log_OC.d(TAG, "Account creation canceled");
                } catch (Exception e) {
                    Log_OC.e(TAG, "Account creation finished in exception: ", e);
                }
            }
        }
    }, mHandler);
}

From source file:org.jnrain.mobile.ui.MainActivity.java

@Override
public void run(AccountManagerFuture<Account[]> response) {
    Account[] accounts;/*  w w  w  .  j a  va  2  s. co  m*/

    try {
        accounts = response.getResult();

        if (accounts.length > 0) {
            onAccountAcquired(accounts[0]);
            return;
        }

        // no account
        // try to create one, but don't recurse infinitely
        if (GlobalState.getAccountInitLevel() > 2) {
            // finish self
            finish();
            return;
        }

        // create account
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... arg0) {
                AccountManager am = AccountManager.get(getThisActivity());

                try {
                    am.addAccount(AccountConstants.ACCOUNT_TYPE_KBS, null, null, null, getThisActivity(),
                            new AccountManagerCallback<Bundle>() {
                                @Override
                                public void run(AccountManagerFuture<Bundle> response) {
                                    try {
                                        response.getResult();
                                    } catch (OperationCanceledException e) {
                                        // TODO Auto-generated catch
                                        // block
                                        e.printStackTrace();
                                    } catch (AuthenticatorException e) {
                                        // TODO Auto-generated catch
                                        // block
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch
                                        // block
                                        e.printStackTrace();
                                    }

                                    MainActivity.this.initAccount();
                                }
                            }, null).getResult();
                } catch (OperationCanceledException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (AuthenticatorException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                return null;
            }
        }.execute((Void) null);
    } catch (OperationCanceledException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        finish();
    } catch (AuthenticatorException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.owncloud.android.ui.activity.ManageAccountsActivity.java

/**
 * Callback executed after the {@link AccountManager} removed an account
 *
 * @param future    Result of the removal; future.getResult() is true if account was removed correctly.
 *///from   ww  w  .  j ava  2  s  .  co  m
@Override
public void run(AccountManagerFuture<Boolean> future) {
    if (future != null && future.isDone()) {
        Account account = new Account(mAccountBeingRemoved, MainApp.getAccountType());
        if (!AccountUtils.exists(account.name, MainApp.getAppContext())) {
            // Cancel transfers of the removed account
            if (mUploaderBinder != null) {
                mUploaderBinder.cancel(account);
            }
            if (mDownloaderBinder != null) {
                mDownloaderBinder.cancel(account);
            }
        }

        mAccountListAdapter = new AccountListAdapter(this, getAccountListItems(), mTintedCheck);
        mListView.setAdapter(mAccountListAdapter);

        AccountManager am = AccountManager.get(this);
        if (am.getAccountsByType(MainApp.getAccountType()).length == 0) {
            // Show create account screen if there isn't any account
            am.addAccount(MainApp.getAccountType(), null, null, null, this, null, null);
        } else { // at least one account left
            if (AccountUtils.getCurrentOwnCloudAccount(this) == null) {
                // current account was removed - set another as current
                String accountName = "";
                Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
                if (accounts.length != 0) {
                    accountName = accounts[0].name;
                }
                AccountUtils.setCurrentOwnCloudAccount(this, accountName);
            }
        }
    }
}

From source file:net.heroicefforts.viable.android.rep.it.GIssueTrackerRepository.java

public GIssueTrackerRepository(String appName, Activity act, Bundle metaData) throws CreateException {
    this.appName = appName;
    this.act = act;
    this.projectName = metaData.getString(PARAM_PROJECT_NAME);
    if (projectName == null)
        throw new CreateException("No '" + "viable-project-name"
                + "' meta-data field defined for application.  Google Isusue Tracker Repository cannot be constructed.");
    this.description = metaData.getString(PARAM_PROJECT_DESC);
    this.lead = metaData.getString(PARAM_PROJECT_ADMIN);
    String versionStr = metaData.getString(PARAM_VERSIONS);
    if (versionStr != null) {
        String[] versions = versionStr.split("[ ]*,[ ]*");
        for (String version : versions)
            this.versions.add(new VersionDetail(version));
    }/*from   w  ww  .  j  ava 2 s  . c  o m*/

    try {
        host = new ProjectHostingService();
        String token = Authenticate.authenticate(act, GCLAccountAuthenticator.TOKEN_TYPE_ISSUE_TRACKER);
        if (token != null)
            host.setAuthSubToken(token);
        else {
            //TODO add popup.
            AccountManager acct = (AccountManager) act.getSystemService(Context.ACCOUNT_SERVICE);
            if (Config.LOGD)
                Log.d(TAG, "Requesting account creation.");
            acct.addAccount(GCLAccountAuthenticator.ACCT_TYPE, "code", null, null, act, callback, null);
        }
    } catch (AuthenticatorException e) {
        throw new CreateException(
                "Exception authenticating Google account for Issue Tracker repository access.");
    } catch (Exception e) {
        throw new CreateException("Exception creating Google Issue Tracker repository.", e);
    }
}

From source file:eu.trentorise.smartcampus.ac.authenticator.AMSCAccessProvider.java

@Override
public String getAuthToken(final Activity activity, String inAuthority)
        throws OperationCanceledException, AuthenticatorException, IOException {
    final String authority = inAuthority == null ? Constants.AUTHORITY_DEFAULT : inAuthority;
    final AccountManager am = AccountManager.get(activity);
    String token = am.peekAuthToken(
            new Account(Constants.getAccountName(activity), Constants.getAccountType(activity)), authority);
    if (token == null) {
        final Account a = new Account(Constants.getAccountName(activity), Constants.getAccountType(activity));
        Account[] accounts = am.getAccountsByType(Constants.getAccountType(activity));
        if (accounts == null || accounts.length == 0) {
            am.addAccount(Constants.getAccountType(activity), authority, null, null, null,
                    new Callback(authority, activity), null);
        } else {// ww  w.  ja  v  a 2s.  c  om
            am.getAuthToken(a, authority, null, null, new Callback(authority, activity), null);
        }
        return null;
    }
    return token;

}