Example usage for android.accounts AccountManager KEY_ACCOUNT_TYPE

List of usage examples for android.accounts AccountManager KEY_ACCOUNT_TYPE

Introduction

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

Prototype

String KEY_ACCOUNT_TYPE

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

Click Source Link

Document

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

Usage

From source file:saschpe.birthdays.helper.AccountHelper.java

public static Bundle addAccount(Context context) {
    Log.d(TAG, "AccountHelper.addAccount: Adding account...");

    final Account account = new Account(context.getString(R.string.app_name),
            context.getString(R.string.account_type));
    AccountManager manager = AccountManager.get(context);

    if (manager.addAccountExplicitly(account, null, null)) {
        // Enable automatic sync once per day
        ContentResolver.setSyncAutomatically(account, context.getString(R.string.content_authority), true);
        ContentResolver.setIsSyncable(account, context.getString(R.string.content_authority), 1);

        // Add periodic sync interval based on user preference
        final long freq = PreferencesHelper.getPeriodicSyncFrequency(context);
        ContentResolver.addPeriodicSync(account, context.getString(R.string.content_authority), new Bundle(),
                freq);/*from  w w w .  j  a v  a2 s  . c o m*/

        Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        Log.i(TAG, "Account added: " + account.name);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            manager.notifyAccountAuthenticated(account);
        }
        return result;
    } else {
        Log.e(TAG, "Adding account explicitly failed!");
        return null;
    }
}

From source file:org.birthdayadapter.util.AccountHelper.java

/**
 * Add account for Birthday Adapter to Android system
 *///from  ww w .  j a v  a2s  .c  o  m
public Bundle addAccountAndSync() {
    Log.d(Constants.TAG, "Adding account...");

    // enable automatic sync once per day
    ContentResolver.setSyncAutomatically(Constants.ACCOUNT, Constants.CONTENT_AUTHORITY, true);
    ContentResolver.setIsSyncable(Constants.ACCOUNT, Constants.ACCOUNT_TYPE, 1);

    // add periodic sync interval once per day
    long freq = AlarmManager.INTERVAL_DAY;
    ContentResolver.addPeriodicSync(Constants.ACCOUNT, Constants.ACCOUNT_TYPE, new Bundle(), freq);

    AccountManager am = AccountManager.get(mContext);
    if (am.addAccountExplicitly(Constants.ACCOUNT, null, null)) {
        Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, Constants.ACCOUNT.name);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT.type);

        // Force a sync! Even when background sync is disabled, this will force one sync!
        manualSync();

        return result;
    } else {
        return null;
    }
}

From source file:org.ohmage.auth.Authenticator.java

@Override
public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType,
        String[] requiredFeatures, Bundle options) {
    // TODO: decide if we should allow more than one account, and if not, make it clear to the
    // user that they need to logout and login with a new account

    final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, accountType);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);
    return bundle;
}

From source file:drupalfit.sample.ToolBarActivity.java

/** {@inheritDoc} */
@Override/*from ww  w  .ja va  2s . co  m*/
public void addAccount(String username, String password, String authToken) {
    String accountType = "drualfit";
    String authTokenType = "drualfit";
    Account account = new Account(username, accountType);
    Bundle userdata = null;

    Intent intent = new Intent();

    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, username);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, accountType);
    intent.putExtra(AccountManager.KEY_AUTHTOKEN, authToken);

    if (TextUtils.isEmpty(password)) {
        boolean added = getAccountManager().addAccountExplicitly(account, password, userdata);
    } else {
        getAccountManager().setPassword(account, password);
    }

    getAccountManager().setAuthToken(account, authTokenType, authToken);

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

From source file:com.rukman.emde.smsgroups.authenticator.GMSAuthenticator.java

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

    Log.d(TAG, "Get Auth Token");
    // If the caller requested an authToken type we don't support, then
    // return an error
    if (!authTokenType.equals(GMSApplication.AUTHTOKEN_TYPE)) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
        return result;
    }// w  ww.  java2  s. c  om

    // Extract the username and password from the Account Manager, and ask
    // the server for an appropriate AuthToken.
    final String password = AccountManager.get(mContext).getPassword(account);
    if (!TextUtils.isEmpty(password)) {
        try {
            final String authToken = NetworkUtilities.authenticate(account.name, password);
            if (!TextUtils.isEmpty(authToken)) {
                final Bundle result = new Bundle();
                result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
                result.putString(AccountManager.KEY_ACCOUNT_TYPE, GMSApplication.ACCOUNT_TYPE);
                result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
                return result;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    // 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, GMSAuthenticatorActivity.class);
    intent.putExtra(GMSAuthenticatorActivity.PARAM_USERNAME, account.name);
    intent.putExtra(GMSAuthenticatorActivity.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: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  2  s. 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: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;
    }//ww w. j a v a  2 s  .  c om

    // 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  av a  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: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 www  .  j  av a2 s .  co 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;
}