Example usage for android.accounts Account Account

List of usage examples for android.accounts Account Account

Introduction

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

Prototype

public Account(@NonNull Account other, @NonNull String accessId) 

Source Link

Usage

From source file:com.manning.androidhacks.hack023.authenticator.AuthenticatorActivity.java

private void finishLogin() {
    final Account account = new Account(mUsername, PARAM_ACCOUNT_TYPE);

    if (mRequestNewAccount) {
        mAccountManager.addAccountExplicitly(account, mPassword, null);

        Bundle bundle = new Bundle();
        ContentResolver.addPeriodicSync(account, TodoContentProvider.AUTHORITY, bundle, 300);

    } else {/*from   ww  w  .ja v a 2 s .  com*/
        mAccountManager.setPassword(account, mPassword);
    }

    final Intent intent = new Intent();
    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mUsername);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, PARAM_ACCOUNT_TYPE);

    if (mAuthTokenType != null && mAuthTokenType.equals(PARAM_AUTHTOKEN_TYPE)) {
        intent.putExtra(AccountManager.KEY_AUTHTOKEN, mAuthToken);
    }

    setAccountAuthenticatorResult(intent.getExtras());
    setResult(RESULT_OK, intent);
    finish();
}

From source file:com.jefftharris.passwdsafe.sync.onedrive.OnedriveProvider.java

/**
 * Get the account for the named provider
 *//* w w  w.j ava  2s  .co m*/
@Override
public Account getAccount(String acctName) {
    return new Account(acctName, SyncDb.ONEDRIVE_ACCOUNT_TYPE);
}

From source file:moose.com.ac.LoginActivity.java

private void addAccount(String username, String password) {
    Account account = new Account(username, BuildConfig.APPLICATION_ID);
    accountManager.addAccountExplicitly(account, password, null);
    accountManager.setPassword(account, password); // for re-login with updated password
    Bundle bundle = new Bundle();
    bundle.putString(AccountManager.KEY_ACCOUNT_NAME, username);
    bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, BuildConfig.APPLICATION_ID);
    SettingPreferences.setUsername(this, username);
    finish();/*  w  ww  .  j  a  va2 s  . co  m*/
}

From source file:info.semanticsoftware.semassist.android.activity.AuthenticationActivity.java

/** Sends an authentication request when the save button is pushed.
 * @param v view /*from  w  w w  .java  2  s . c o  m*/
 */
public void onSaveClick(View v) {
    TextView tvUsername = (TextView) this.findViewById(R.id.uc_txt_username);
    TextView tvPassword = (TextView) this.findViewById(R.id.uc_txt_password);

    //Qualified username, i.e, user@semanticassistants.com
    String qUsername = tvUsername.getText().toString();
    String username = null;
    if (qUsername.indexOf("@") > 0) {
        username = qUsername.substring(0, qUsername.indexOf("@"));
    } else {
        username = qUsername;
    }
    String password = tvPassword.getText().toString();

    //TODO do client-side validation like password length etc.
    String response = authenicate(username, password);
    if (!response.equals(Constants.AUTHENTICATION_FAIL)) {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        Editor editor = settings.edit();
        editor.putString("username", username);
        editor.putString("password", password);
        //FIXME replace this with a descent XML parser
        String acctype = "";
        int start = response.indexOf("<accType>");
        int end = response.indexOf("</accType>");
        if (start > -1 && end > -1) {
            acctype = response.substring(start + "<accType>".length(), end);
        }
        editor.putString("acctype", acctype);
        String sessionId = "";
        start = response.indexOf("<sessionId>");
        end = response.indexOf("</sessionId>");
        if (start > -1 && end > -1) {
            sessionId = response.substring(start + "<sessionId>".length(), end);
        }
        editor.putString("sessionId", sessionId);
        String reqNum = "";
        start = response.indexOf("<reqNum>");
        end = response.indexOf("</reqNum>");
        if (start > -1 && end > -1) {
            reqNum = response.substring(start + "<reqNum>".length(), end);
        }
        editor.putString("reqNum", reqNum);
        boolean result = editor.commit();
        if (result) {
            Toast.makeText(this, R.string.authenticationSuccess, Toast.LENGTH_LONG).show();
            String accountType = this.getIntent().getStringExtra("auth.token");
            if (accountType == null) {
                accountType = SemAssistAuthenticator.ACCOUNT_TYPE;
            }

            AccountManager accMgr = AccountManager.get(this);

            // Add the account to the Android Account Manager
            String accountName = username + "@semanticassistants.com";
            final Account account = new Account(accountName, accountType);
            accMgr.addAccountExplicitly(account, password, null);

            // Inform the caller (could be the Android Account Manager or the SA app) that the process was successful
            final Intent intent = new Intent();
            intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, accountName);
            intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, accountType);
            intent.putExtra(AccountManager.KEY_AUTHTOKEN, accountType);
            this.setAccountAuthenticatorResult(intent.getExtras());
            this.setResult(RESULT_OK, intent);
            this.finish();
        } else {
            Toast.makeText(this, "Could not write the preferences.", Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(this, R.string.authenticationFail, Toast.LENGTH_LONG).show();
    }
}

From source file:com.owncloud.android.lib.common.SingleSessionManager.java

@Override
public synchronized void saveAllClients(Context context, String accountType)
        throws AccountNotFoundException, AuthenticatorException, IOException, OperationCanceledException {

    Iterator<String> accountNames = mClientsWithKnownUsername.keySet().iterator();
    String accountName = null;/*from   w w  w .ja v a  2  s.c o  m*/
    Account account = null;
    while (accountNames.hasNext()) {
        accountName = accountNames.next();
        account = new Account(accountName, accountType);
        AccountUtils.saveClient(mClientsWithKnownUsername.get(accountName), account, context);
    }
}

From source file:com.pindroid.authenticator.AuthenticatorActivity.java

/**
 * //from  w ww . jav a2 s  . co m
 * Called when response is received from the server for authentication
 * request. See onAuthenticationResult(). Sets the
 * AccountAuthenticatorResult which is sent back to the caller. Also sets
 * the authToken in AccountManager for this account.
 * 
 * @param the confirmCredentials result.
 */
protected void finishLogin(String authToken) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    final int synctime = Integer.parseInt(settings.getString("pref_synctime", "0"));

    final Account account = new Account(mUsername, Constants.ACCOUNT_TYPE);

    if (mRequestNewAccount) {
        mAccountManager.addAccountExplicitly(account, null, null);

        ContentResolver.setSyncAutomatically(account, BookmarkContentProvider.AUTHORITY, true);
        if (synctime != 0) {
            SyncUtils.addPeriodicSync(BookmarkContentProvider.AUTHORITY, Bundle.EMPTY, synctime, this);
        }
    }

    mAccountManager.setAuthToken(account, Constants.AUTHTOKEN_TYPE, authToken);

    final Intent intent = new Intent();

    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mUsername);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
    intent.putExtra(AccountManager.KEY_AUTHTOKEN, authToken);
    setAccountAuthenticatorResult(intent.getExtras());
    setResult(RESULT_OK, intent);
    finish();
}

From source file:pt.up.mobile.authenticator.AuthenticatorActivity.java

/**
 * Called when response is received from the server for confirm credentials
 * request. See onAuthenticationResult(). Sets the
 * AccountAuthenticatorResult which is sent back to the caller.
 * //from  ww w.  j a  va 2s  . c  o  m
 * @param result
 *            the confirmCredentials result.
 */
private void finishConfirmCredentials(boolean result) {
    Log.i(TAG, "finishConfirmCredentials()");
    final Account account = new Account(mUsername, Constants.ACCOUNT_TYPE);
    mAccountManager.setPassword(account, mPassword);
    final Intent intent = new Intent();
    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mUsername);
    intent.putExtra(AccountManager.KEY_BOOLEAN_RESULT, result);
    setAccountAuthenticatorResult(intent.getExtras());
    setResult(RESULT_OK, intent);
    finish();
}

From source file:org.level28.android.moca.ui.schedule.ScheduleActivity.java

/**
 * Perform a manual synchronization//  w w  w . ja va2s  .com
 */
private void triggerRefresh() {
    Bundle extras = new Bundle();
    extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
    ContentResolver.requestSync(
            new Account(MocaAuthenticator.HARDCODED_USERNAME, MocaAuthenticator.ACCOUNT_TYPE),
            ScheduleContract.CONTENT_AUTHORITY, extras);
}

From source file:com.mobilyzer.AccountSelector.java

/** Starts an authentication request  */
public void authenticate() throws OperationCanceledException, AuthenticatorException, IOException {
    Logger.i("AccountSelector.authenticate() running");
    /* We only need to authenticate every AUTHENTICATE_PERIOD_MILLI milliseconds, during
     * which we can reuse the cookie. If authentication fails due to expired
     * authToken, the client of AccountSelector can call authImmedately() to request
     * authenticate() upon the next checkin
     *///ww  w  .j  av  a2s  .c  o m
    long authTimeLast = this.getLastAuthTime();
    long timeSinceLastAuth = System.currentTimeMillis() - authTimeLast;
    if (!this.shouldAuthImmediately() && authTimeLast != 0 && (timeSinceLastAuth < AUTHENTICATE_PERIOD_MSEC)) {
        return;
    }

    Logger.i("Authenticating. Last authentication is " + timeSinceLastAuth / 1000 / 60 + " minutes ago. ");

    AccountManager accountManager = AccountManager.get(context.getApplicationContext());
    if (this.authToken != null) {
        // There will be no effect on the token if it is still valid
        Logger.i("Invalidating token");
        accountManager.invalidateAuthToken(ACCOUNT_TYPE, this.authToken);
    }
    if (!hasGetAccountsPermission(context)) {
        isAnonymous = true;
        return;
    }

    Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
    Logger.i("Got " + accounts.length + " accounts");

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
    String selectedAccount = prefs.getString(Config.PREF_KEY_SELECTED_ACCOUNT, null);
    Logger.i("Selected account = " + selectedAccount);

    final String defaultUserName = Config.DEFAULT_USER;
    isAnonymous = true;
    if (selectedAccount != null && selectedAccount.equals(defaultUserName)) {
        return;
    }

    if (accounts != null && accounts.length > 0) {
        // Default account should be the Anonymous account
        Account accountToUse = new Account(Config.DEFAULT_USER, ACCOUNT_TYPE);
        if (!accounts[accounts.length - 1].name.equals(defaultUserName)) {
            for (Account account : accounts) {
                if (account.name.equals(defaultUserName)) {
                    accountToUse = account;
                    break;
                }
            }
        }
        if (selectedAccount != null) {
            for (Account account : accounts) {
                if (account.name.equals(selectedAccount)) {
                    accountToUse = account;
                    break;
                }
            }
        }

        isAnonymous = accountToUse.name.equals(defaultUserName);

        if (isAnonymous) {
            Logger.d("Skipping authentication as account is " + defaultUserName);
            return;
        }

        Logger.i("Trying to get auth token for " + accountToUse);
        AccountManagerFuture<Bundle> future = accountManager.getAuthToken(accountToUse, "ah", false,
                new AccountManagerCallback<Bundle>() {
                    @Override
                    public void run(AccountManagerFuture<Bundle> result) {
                        Logger.i("AccountManagerCallback invoked");
                        try {
                            getAuthToken(result);
                        } catch (RuntimeException e) {
                            Logger.e("Failed to get authToken", e);
                            /* TODO(Wenjie): May ask the user whether to quit the app nicely here if a number
                             * of trials have been made and failed. Since Speedometer is basically useless 
                             * without checkin
                             */
                        }
                    }
                }, null);
        Logger.i("AccountManager.getAuthToken returned " + future);
    } else {
        throw new RuntimeException("No google account found");
    }
}