Example usage for android.accounts AccountManager KEY_ACCOUNT_NAME

List of usage examples for android.accounts AccountManager KEY_ACCOUNT_NAME

Introduction

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

Prototype

String KEY_ACCOUNT_NAME

To view the source code for android.accounts AccountManager KEY_ACCOUNT_NAME.

Click Source Link

Document

Bundle key used for the String account name in results from methods which return information about a particular account.

Usage

From source file:io.v.android.apps.account_manager.AccountActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_PICK_ACCOUNTS) {
        if (resultCode != RESULT_OK) {
            replyWithError("User didn't pick account.");
            return;
        }//from   www .  j a v  a  2  s.  co  m
        mAccountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
        getIdentity();
    } else if (requestCode == REQUEST_CODE_USER_APPROVAL) {
        if (resultCode != RESULT_OK) {
            replyWithError("User didn't give proposed permissions.");
            return;
        }
        getIdentity();
    }
    super.onActivityResult(requestCode, resultCode, data);
}

From source file:com.github.riotopsys.shoppinglist.activity.ShoppingListPreview.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shopping_list_preview);

    AccountUtils accountUtils = new AccountUtils();
    String account = accountUtils.getAccountName(this);

    if (account == null) {
        AccountManager am = AccountManager.get(this);
        am.getAuthTokenByFeatures(AppKeys.ACCOUNT_TYPE, AppKeys.OAUTH2_SCOPE, null, this, null, null,
                new AccountManagerCallback<Bundle>() {
                    @Override/*from w w w. j a v a 2s .c  o m*/
                    public void run(AccountManagerFuture<Bundle> future) {
                        try {
                            Bundle bundle = future.getResult();
                            Log.i(TAG, "Got Bundle:\n" + " act name: "
                                    + bundle.getString(AccountManager.KEY_ACCOUNT_NAME) + "\n act type: "
                                    + bundle.getString(AccountManager.KEY_ACCOUNT_TYPE) + "\n auth token: "
                                    + bundle.getString(AccountManager.KEY_AUTHTOKEN));
                            AccountUtils accountUtils = new AccountUtils();
                            accountUtils.setAccountName(getBaseContext(),
                                    bundle.getString(AccountManager.KEY_ACCOUNT_NAME));
                            accountUtils.setToken(getBaseContext(),
                                    bundle.getString(AccountManager.KEY_AUTHTOKEN));
                        } catch (Exception e) {
                            Log.i(TAG, "getAuthTokenByFeatures() cancelled or failed:", e);
                            Toast.makeText(getBaseContext(), R.string.no_account, Toast.LENGTH_LONG).show();
                            finish();
                        }
                    }
                }, null);
    }

    mShoppingListCollection = new ShoppingListCollection();

    mShoppingListCollectionAdapter = new ShoppingListCollectionAdapter(this, getSupportFragmentManager(),
            mShoppingListCollection);

    //      mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mShoppingListCollectionAdapter);

    Intent startingIntent = getIntent();
    if (startingIntent != null) {
        Uri data = startingIntent.getData();
        if (data != null) {
            UUID guid = UUID.fromString(data.getLastPathSegment());
            Intent i = new Intent(this, ServerInterfaceService.class);
            i.putExtra(AppKeys.SERVER_TASK_KEY, ServerTask.SUBSCRIBE);
            i.putExtra(AppKeys.GUID_KEY, guid);
            startService(i);
        }
    }

    IConfigurations config = new Configurations();

    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);
    final String regId = GCMRegistrar.getRegistrationId(this);
    if (regId.equals("")) {
        GCMRegistrar.register(this, config.getGCMSenderID());
    } else {
        Log.v(TAG, "Already registered");
    }

}

From source file:com.jefftharris.passwdsafe.sync.owncloud.OwncloudProvider.java

@Override
public NewAccountTask finishAccountLink(int activityResult, Intent activityData, Uri providerAcctUri) {
    String accountName = null;//from ww w. j  av  a 2  s . c  o m
    do {
        if ((activityResult != Activity.RESULT_OK) || (activityData == null)) {
            break;
        }

        Bundle b = activityData.getExtras();
        accountName = b.getString(AccountManager.KEY_ACCOUNT_NAME);
        Log.i(TAG, "Selected account: " + accountName);
        if (TextUtils.isEmpty(accountName)) {
            accountName = null;
            break;
        }
    } while (false);

    saveAuthData(accountName, createUrlFromAccount(accountName, true));
    updateOwncloudAcct();

    if (accountName == null) {
        return null;
    }
    return new NewAccountTask(providerAcctUri, accountName, ProviderType.OWNCLOUD, true, getContext()) {
        @Override
        protected void doAccountUpdate(ContentResolver cr) {
            Activity act = getActivity();
            String authToken = getAuthToken(getAccount(itsNewAcct), act, act);
            if (authToken != null) {
                super.doAccountUpdate(cr);
            }
        }
    };
}

From source file:org.klnusbaum.udj.auth.Authenticator.java

private Bundle bundleUpAuthToken(Account account, String authToken) {
    final Bundle result = new Bundle();
    result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
    result.putString(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
    result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
    return result;
}

From source file:com.clearcenter.mobile_demo.mdAuthenticator.java

public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
        Bundle loginOptions) throws NetworkErrorException {
    Log.v(TAG, "getAuthToken()");

    // If the caller requested an authToken type we don't support, then
    // return an error
    if (!authTokenType.equals(mdConstants.AUTHTOKEN_TYPE)) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
        return result;
    }/*from w w w  .ja va2 s.com*/

    // Extract the username and password from the Account Manager, and ask
    // the server for an appropriate AuthToken.
    final AccountManager am = AccountManager.get(ctx);
    final String password = am.getPassword(account);
    final String hostname = am.getUserData(account, "hostname");
    final String username = am.getUserData(account, "username");

    if (password != null) {
        try {
            mdSSLUtil.DisableSecurity();

            final String authToken = mdRest.Login(hostname, username, null, password);
            if (!TextUtils.isEmpty(authToken)) {
                final Bundle result = new Bundle();
                result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
                result.putString(AccountManager.KEY_ACCOUNT_TYPE, mdConstants.ACCOUNT_TYPE);
                result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
                return result;
            }
        } catch (JSONException e) {
            Log.e(TAG, "JSONException", e);
        } catch (GeneralSecurityException e) {
            Log.e(TAG, "GeneralSecurityException", e);
        }
    }

    Log.v(TAG, "Asking for password again...");

    // If we get here, then we couldn't access the user's password - so we
    // need to re-prompt them for their credentials. We do that by creating
    // an intent to display our mdAuthenticatorActivity panel.
    final Intent intent = new Intent(ctx, mdAuthenticatorActivity.class);
    intent.putExtra(mdAuthenticatorActivity.PARAM_NICKNAME, account.name);
    intent.putExtra(mdAuthenticatorActivity.PARAM_USERNAME, username);
    intent.putExtra(mdAuthenticatorActivity.PARAM_HOSTNAME, hostname);
    intent.putExtra(mdAuthenticatorActivity.PARAM_AUTHTOKEN_TYPE, authTokenType);
    intent.putExtra(mdAuthenticatorActivity.PARAM_CONFIRM_CREDENTIALS, true);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);

    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);

    return bundle;
}

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

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
        Bundle options) throws NetworkErrorException {

    if (!authTokenType.equals(AuthenticatorActivity.PARAM_AUTHTOKEN_TYPE)) {

        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");

        return result;
    }/*from   w  w  w  . j a  va  2 s  .c  o  m*/

    final AccountManager am = AccountManager.get(mContext);
    final String password = am.getPassword(account);

    if (password != null) {
        boolean verified = false;

        String loginResponse = null;
        try {
            loginResponse = LoginServiceImpl.sendCredentials(account.name, password);
            verified = LoginServiceImpl.hasLoggedIn(loginResponse);
        } catch (AndroidHacksException e) {
            verified = false;
        }

        if (verified) {
            final Bundle result = new Bundle();
            result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, AuthenticatorActivity.PARAM_ACCOUNT_TYPE);

            return result;
        }
    }
    // Password is missing or incorrect
    final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
    intent.putExtra(AuthenticatorActivity.PARAM_USER, account.name);
    intent.putExtra(AuthenticatorActivity.PARAM_AUTHTOKEN_TYPE, authTokenType);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);
    return bundle;
}

From source file:monakhv.android.samlib.SamlibPreferencesActivity.java

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent it) {
    super.onActivityResult(requestCode, resultCode, it);
    if (it == null) {
        return;/*from www  .jav  a  2  s  .  c  o m*/
    }
    switch (requestCode) {
    case REQ_AUTH:
        helper.setGoogleAccount(it.getStringExtra(AccountManager.KEY_ACCOUNT_NAME));
        googlePrefs.setSummary(helper.getGoogleAccount());
        break;
    }

}

From source file:com.jefftharris.passwdsafe.sync.gdrive.GDriveProvider.java

@Override
public NewAccountTask finishAccountLink(int activityResult, Intent activityData, Uri acctProviderUri) {
    if (activityData == null) {
        return null;
    }/*from   w  w w . j  a va 2  s. c o  m*/

    Bundle b = activityData.getExtras();
    String accountName = b.getString(AccountManager.KEY_ACCOUNT_NAME);
    Log.i(TAG, "Selected account: " + accountName);
    if (TextUtils.isEmpty(accountName)) {
        return null;
    }

    setAcctName(accountName);
    updateAcct();
    return new NewAccountTask(acctProviderUri, accountName, ProviderType.GDRIVE, false, itsContext);
}

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

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
        Bundle loginOptions) throws NetworkErrorException {
    Log.v(TAG, "getAuthToken()");

    // If the caller requested an authToken type we don't support, then
    // return an error
    if (!authTokenType.equals(Constants.AUTHTOKEN_TYPE)) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
        return result;
    }//from   w  w w.  j a v a2 s . c  o  m
    try {
        final AccountManager am = AccountManager.get(mContext);
        final String peek = am.peekAuthToken(account, authTokenType);
        if (peek != null) {
            final Bundle result = new Bundle();
            result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
            result.putString(AccountManager.KEY_AUTHTOKEN, peek);
            return result;
        }
        // Extract the username and password from the Account Manager, and
        // ask
        // the server for an appropriate AuthToken.
        final String password = am.getPassword(account);
        if (password != null) {
            String[] reply;

            reply = SifeupAPI.authenticate(account.name, password, mContext);
            final String authToken = reply[1];
            if (!TextUtils.isEmpty(authToken)) {
                final Bundle result = new Bundle();
                result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
                result.putString(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
                result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
                return result;
            }
        }

    } catch (AuthenticationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        throw new NetworkErrorException();
    }
    // If we get here, then we couldn't access the user's password - so we
    // need to re-prompt them for their credentials. We do that by creating
    // an intent to display our AuthenticatorActivity panel.
    final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
    intent.putExtra(AuthenticatorActivity.PARAM_CONFIRM_CREDENTIALS, true);
    intent.putExtra(AuthenticatorActivity.PARAM_USERNAME, account.name);
    intent.putExtra(AuthenticatorActivity.PARAM_AUTHTOKEN_TYPE, authTokenType);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);
    return bundle;
}

From source file:net.xisberto.phonetodesktop.MainActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case REQUEST_ACCOUNT_PICKER:
        Utils.log("Result from Account Picker");
        if (resultCode == RESULT_OK && data != null && data.hasExtra(AccountManager.KEY_ACCOUNT_NAME)) {
            String accountName = data.getExtras().getString(AccountManager.KEY_ACCOUNT_NAME);
            Utils.log("Saving account " + accountName);
            if (accountName != null) {
                if (currentFragment instanceof WelcomeFragment) {
                    mainFragment = (MainFragment) getSupportFragmentManager().findFragmentByTag(TAG_MAIN);
                    if (mainFragment == null) {
                        mainFragment = MainFragment.newInstance();
                    }//  w w  w  .ja va2  s .com
                    updateMainLayout(true);
                    getSupportFragmentManager().beginTransaction().replace(R.id.main_frame, mainFragment)
                            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
                }
                credential.setSelectedAccountName(accountName);
                preferences.saveAccountName(accountName);
                // If PhoneToDesktop has'nt been authorized by the user
                // this will lead to an UserRecoverableAuthIOException
                // that will generate an onActivityResult for
                // REQUEST_AUTHENTICATION
                asyncRequestLists();
            }
        } else {
            // User cancelled, or any other error during authorization
            // updateMainLayout(false);
        }
        break;

    case REQUEST_GOOGLE_PLAY_SERVICES:
        Utils.log("Result from Play Services error");
        startAuthorization();
        break;
    case REQUEST_AUTHORIZATION:
        Utils.log("Result from Authorization");
        if (resultCode == RESULT_OK) {
            asyncRequestLists();
        } else {
            updateMainLayout(false);
        }
        break;

    default:
        break;
    }
}