Java Code Examples for android.accounts.AccountManager#getPassword()

The following examples show how to use android.accounts.AccountManager#getPassword() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppAccountAuthenticator.java    From account-authenticator with Apache License 2.0 6 votes vote down vote up
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
    AuthenticatorManager authenticatorManager = AuthenticatorManager.authenticatorManager;
    Bundle result;
    AccountManager accountManager = AccountManager.get(context);
    // case 1: access token is available
    result = authenticatorManager.getAccessTokenFromCache(account, authTokenType, accountManager);
    if (result != null) {
        return result;
    }
    final String refreshToken = accountManager.getPassword(account);
    // case 2: access token is not available but refresh token is
    if (refreshToken != null) {
        result = authenticatorManager.makeResultBundle(account, refreshToken, null);
        return result;
    }
    // case 3: neither tokens is available but the account exists
    if (isAccountAvailable(account, accountManager)) {
        result = authenticatorManager.makeResultBundle(account, null, null);
        return result;
    }
    // case 4: account does not exist
    return new Bundle();
}
 
Example 2
Source File: SyncAdapter.java    From Paperwork-Android with MIT License 6 votes vote down vote up
private static Account getSyncAccount(Context context)
{
    // Get an instance of the Android account manager
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

    // Create the account type and default account
    Account newAccount = new Account("Jon Doe", "paperwork.rocks");

    // If the password doesn't exist, the account doesn't exist
    if (null == accountManager.getPassword(newAccount))
    {
    /*
     * Add the account and account type, no password or user data
     * If successful, return the Account object, otherwise report an error.
     */
        if (!accountManager.addAccountExplicitly(newAccount, "", null))
        {
            return null;
        }
    }
    return newAccount;
}
 
Example 3
Source File: SunshineSyncAdapter.java    From Krishi-Seva with MIT License 5 votes vote down vote up
/**
 * Helper method to get the fake account to be used with SyncAdapter, or make a new one
 * if the fake account doesn't exist yet.  If we make a new account, we call the
 * onAccountCreated method so we can initialize things.
 *
 * @param context The context used to access the account service
 * @return a fake account.
 */
public static Account getSyncAccount(Context context) {
    // Get an instance of the Android account manager
    AccountManager accountManager =
            (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

    // Create the account type and default account
    Account newAccount = new Account(
            context.getString(R.string.app_name), context.getString(R.string.sync_account_type));

    // If the password doesn't exist, the account doesn't exist
    if ( null == accountManager.getPassword(newAccount) ) {

    /*
     * Add the account and account type, no password or user data
     * If successful, return the Account object, otherwise report an error.
     */
        if (!accountManager.addAccountExplicitly(newAccount, "", null)) {
            return null;
        }
        /*
         * If you don't set android:syncable="true" in
         * in your <provider> element in the manifest,
         * then call ContentResolver.setIsSyncable(account, AUTHORITY, 1)
         * here.
         */

        onAccountCreated(newAccount, context);
    }
    return newAccount;
}
 
Example 4
Source File: Authenticator.java    From cordova-android-accountmanager with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle loginOptions) throws NetworkErrorException
{
    // If the caller requested an authToken type we don't support, then
    // return an error
    /*if(authTokenType isn't a supported auth token type)
    {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
        return result;
    }*/

    // 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);
    if (password != null)
    {
        final String authToken = ""; /* TODO: Login with account.name and passwod */
        if (!TextUtils.isEmpty(authToken))
        {
            final Bundle result = new Bundle();
            result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
            result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
            return result;
        }
    }

    //final Intent intent = null; // TODO: Login intent
    //final Bundle bundle = new Bundle();
    //bundle.putParcelable(AccountManager.KEY_INTENT, intent);
    //return bundle;
    return null;
}
 
Example 5
Source File: AuthHelper.java    From android-atleap with Apache License 2.0 5 votes vote down vote up
/**
 * Get password of specified account
 * @param context context
 * @param account account
 * @return account password
 */
public static String getAccountPassword(Context context, Account account) {
    if(account == null)
        return null;
    AccountManager accountManager = AccountManager.get(context);
    return accountManager.getPassword(account);
}
 
Example 6
Source File: BaseAuthenticator.java    From android-atleap with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {

    Log.v(TAG, "getAuthToken for account=" + account + " and tokenType=" + authTokenType);

    final AccountManager am = AccountManager.get(mContext);

    String authToken = am.peekAuthToken(account, authTokenType);

    if (!TextUtils.isEmpty(authToken)) {
        Log.v(TAG, "Auth token is in the cache. Retuning.");
        return createAccountManagerResult(account, authToken);
    }

    Log.v(TAG, "Trying to get password from cache");

    final String password = am.getPassword(account);
    if (TextUtils.isEmpty(authToken)) {
        if (password != null) {
            Log.v(TAG, "Password is in cache. Trying to authenticate again.");
            try {
                authToken = authenticateOnServer(account, password, authTokenType, options, response);
            } catch (Exception e) {
                Log.w(TAG, "Cannot authenticate on the server", e);
            }
        }
    }


    if (!TextUtils.isEmpty(authToken)) {
        Log.v(TAG, "Auth token was received from the server");
        return createAccountManagerResult(account, authToken);
    }


    Log.v(TAG, "Auth token was not received. Starting auth activity.");
    return createAuthActivityIntentBundle(response, account.name, account.type, authTokenType, password, options);
}
 
Example 7
Source File: WoodminSyncAdapter.java    From Woodmin with Apache License 2.0 5 votes vote down vote up
public static void removeAccount(Context context){
    String user = Utility.getPreferredUser(context);
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    Account account = new Account("Woodmin", context.getString(R.string.sync_account_type));
    if ( accountManager.getPassword(account) != null  ) {
        String secret = Utility.getPreferredSecret(context);
        accountManager.removeAccount(account,null,null);
    }
}
 
Example 8
Source File: WoodminSyncAdapter.java    From Woodmin with Apache License 2.0 5 votes vote down vote up
public static Account getSyncAccount(Context context) {

        String user = Utility.getPreferredUser(context);
        AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
        Account account = new Account("Woodmin", context.getString(R.string.sync_account_type));
        if ( accountManager.getPassword(account) == null  ) {
            String secret = Utility.getPreferredSecret(context);
            if (!accountManager.addAccountExplicitly(account, secret, null)) {
                return null;
            }
            onAccountCreated(account, context);
        }
        return account;

    }
 
Example 9
Source File: AccountUtils.java    From cnode-android with MIT License 5 votes vote down vote up
public static String getAccessToken(AccountManager accountManager, Account account) {
    if (account == null) {
        return null;
    }

    return accountManager.getPassword(account);
}
 
Example 10
Source File: AccountAuthenticator.java    From Pioneer with Apache License 2.0 5 votes vote down vote up
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
    Timber.d("[getAuthToken] - account: %s, authTokenType: %s", account, authTokenType);

    Bundle result = new Bundle();

    if (!authTokenType.equals(AUTH_TOKEN_TYPE_PIONEER)) {
        return result;
    }

    AccountManager am = AccountManager.get(context);
    String password = am.getPassword(account);
    if (TextUtils.isEmpty(password)) {
        result.putParcelable(KEY_INTENT, createActivityIntent(response, authTokenType));
        return result;
    }

    String authToken = getOrCreateAuthorization(account.name, password);

    if (TextUtils.isEmpty(authToken)) {
        result.putParcelable(KEY_INTENT, createActivityIntent(response, authTokenType));
    } else {
        result.putString(KEY_ACCOUNT_NAME, account.name);
        result.putString(KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
        result.putString(KEY_AUTHTOKEN, authToken);
        am.clearPassword(account);
    }
    return result;
}
 
Example 11
Source File: MoverAuthenticator.java    From Mover with Apache License 2.0 5 votes vote down vote up
@Override
public Bundle getAuthToken(AccountManager manager, Account account) {
    if(!mSigning){
        mPassword = manager.getPassword(account);
    }

    Bundle result = new Bundle();
    try {
        Response response = mService.signIn(account.name, mPassword);

        if (isResponseSuccess(response)) {
            JSONObject json = asJson(asString(response));

            if (json == null || json.has("errors") || json.has("error")) {
                result.putInt(KEY_ERROR_CODE, ERROR_CODE_INVALID_USER_DATA);
                return result;
            }

            for (Header header : response.getHeaders()) {
                if (isCookiesHeader(header)) {
                    String cookies = header.getValue();
                    String token = findToken(HttpCookie.parse(cookies));

                    if (token != null) {
                        result.putString(KEY_AUTHTOKEN, token);
                        result.putString(USER_PICTURE_URL, findUserImage(mService.channel(account.name)));
                    }
                }
            }
        } else {
            result.putInt(KEY_ERROR_CODE, ERROR_CODE_NETWORK_ERROR);
        }
    }catch (RetrofitError error){
        result.putInt(KEY_ERROR_CODE, ERROR_CODE_NETWORK_ERROR);
    }

    return result;
}
 
Example 12
Source File: WoodminSyncAdapter.java    From Woodmin with Apache License 2.0 5 votes vote down vote up
public static void removeAccount(Context context){
    String user = Utility.getPreferredUser(context);
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    Account account = new Account("Woodmin", context.getString(R.string.sync_account_type));
    if ( accountManager.getPassword(account) != null  ) {
        String secret = Utility.getPreferredSecret(context);
        accountManager.removeAccount(account,null,null);
    }
}
 
Example 13
Source File: WoodminSyncAdapter.java    From Woodmin with Apache License 2.0 5 votes vote down vote up
public static Account getSyncAccount(Context context) {

        String user = Utility.getPreferredUser(context);
        AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
        Account account = new Account("Woodmin", context.getString(R.string.sync_account_type));
        if ( accountManager.getPassword(account) == null  ) {
            String secret = Utility.getPreferredSecret(context);
            if (!accountManager.addAccountExplicitly(account, secret, null)) {
                return null;
            }
            onAccountCreated(account, context);
        }
        return account;

    }
 
Example 14
Source File: ApplicationManager.java    From ETSMobile-Android2 with Apache License 2.0 5 votes vote down vote up
@Override
    public void onCreate() {
        super.onCreate();
        createDatabaseTables();

//        Fabric.with(this, new Crashlytics());
        FirebaseApp.initializeApp(this);

        AccountManager accountManager = AccountManager.get(this);
        Account[] accounts = accountManager.getAccountsByType(Constants.ACCOUNT_TYPE);
        String username = "", password = "";

        if (accounts.length > 0) {
            username = accounts[0].name;
            password = accountManager.getPassword(accounts[0]);
        }

        if (username.length() > 0 && password.length() > 0) {
            userCredentials = new UserCredentials(username, password);
        }

        SecurePreferences securePreferences = new SecurePreferences(this);
        int typeUsagerId = securePreferences.getInt(Constants.TYPE_USAGER_ID, -1);
        String domaine = securePreferences.getString(Constants.DOMAINE, "");

        if(typeUsagerId != -1 && !TextUtils.isEmpty(domaine)) {
            ApplicationManager.typeUsagerId = typeUsagerId;
            ApplicationManager.domaine = domaine;
        }

        appComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build();
    }
 
Example 15
Source File: SunshineSyncAdapter.java    From Advanced_Android_Development with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to get the fake account to be used with SyncAdapter, or make a new one
 * if the fake account doesn't exist yet.  If we make a new account, we call the
 * onAccountCreated method so we can initialize things.
 *
 * @param context The context used to access the account service
 * @return a fake account.
 */
public static Account getSyncAccount(Context context) {
    // Get an instance of the Android account manager
    AccountManager accountManager =
            (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

    // Create the account type and default account
    Account newAccount = new Account(
            context.getString(R.string.app_name), context.getString(R.string.sync_account_type));

    // If the password doesn't exist, the account doesn't exist
    if ( null == accountManager.getPassword(newAccount) ) {

    /*
     * Add the account and account type, no password or user data
     * If successful, return the Account object, otherwise report an error.
     */
        if (!accountManager.addAccountExplicitly(newAccount, "", null)) {
            return null;
        }
        /*
         * If you don't set android:syncable="true" in
         * in your <provider> element in the manifest,
         * then call ContentResolver.setIsSyncable(account, AUTHORITY, 1)
         * here.
         */

        onAccountCreated(newAccount, context);
    }
    return newAccount;
}
 
Example 16
Source File: AccountAuthenticator.java    From Pioneer with Apache License 2.0 5 votes vote down vote up
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
    Timber.d("[getAuthToken] - account: %s, authTokenType: %s", account, authTokenType);

    Bundle result = new Bundle();

    if (!authTokenType.equals(AUTH_TOKEN_TYPE_PIONEER)) {
        return result;
    }

    AccountManager am = AccountManager.get(context);
    String password = am.getPassword(account);
    if (TextUtils.isEmpty(password)) {
        result.putParcelable(KEY_INTENT, createActivityIntent(response, authTokenType));
        return result;
    }

    String authToken = getOrCreateAuthorization(account.name, password);

    if (TextUtils.isEmpty(authToken)) {
        result.putParcelable(KEY_INTENT, createActivityIntent(response, authTokenType));
    } else {
        result.putString(KEY_ACCOUNT_NAME, account.name);
        result.putString(KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
        result.putString(KEY_AUTHTOKEN, authToken);
        am.clearPassword(account);
    }
    return result;
}
 
Example 17
Source File: AuthenticatorManager.java    From account-authenticator with Apache License 2.0 5 votes vote down vote up
public Bundle getAccessTokenFromCache(Account account, String authTokenType, AccountManager accountManager) {
    Bundle result;
    String cachedAuthToken = accountManager.peekAuthToken(account, authTokenType);
    String refreshToken = accountManager.getPassword(account);
    if (cachedAuthToken != null) {
        result = makeResultBundle(account, refreshToken, cachedAuthToken);
        return result;
    }
    return null;
}
 
Example 18
Source File: Authenticator.java    From react-native-account-manager with MIT License 5 votes vote down vote up
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle loginOptions) throws NetworkErrorException
{
    // If the caller requested an authToken type we don't support, then
    // return an error
    /*if(authTokenType isn't a supported auth token type)
    {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
        return result;
    }*/

    // 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);
    if (password != null)
    {
        final String authToken = ""; /* TODO: Login with account.name and passwod */
        if (!TextUtils.isEmpty(authToken))
        {
            final Bundle result = new Bundle();
            result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
            result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
            return result;
        }
    }

    //final Intent intent = null; // TODO: Login intent
    //final Bundle bundle = new Bundle();
    //bundle.putParcelable(AccountManager.KEY_INTENT, intent);
    //return bundle;
    return null;
}
 
Example 19
Source File: SyncAdapter.java    From react-native-sync-adapter with MIT License 5 votes vote down vote up
static Account getSyncAccount(Context context, int syncInterval, int syncFlexTime) {
    // Get an instance of the Android account manager
    AccountManager accountManager =
            (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

    // Create the account type and default account
    Account newAccount = new Account(context.getString(R.string.app_name),
            context.getString(R.string.rnsb_sync_account_type));

    // If the password doesn't exist, the account doesn't exist
    if (null == accountManager.getPassword(newAccount)) {

    /*
     * Add the account and account type, no password or user data
     * If successful, return the Account object, otherwise report an error.
     */
        if (!accountManager.addAccountExplicitly(newAccount, "", null)) {
            return null;
        }
        /*
         * If you don't set android:syncable="true" in
         * in your <provider> element in the manifest,
         * then call ContentResolver.setIsSyncable(account, AUTHORITY, 1)
         * here.
         */
        onAccountCreated(newAccount, context, syncInterval, syncFlexTime);
    }
    return newAccount;
}
 
Example 20
Source File: PalmAccount.java    From palmsuda with Apache License 2.0 4 votes vote down vote up
public PalmAccount(Account account, AccountManager mAccountManager) {
	accountName = account.name;
	password = mAccountManager.getPassword(account);
}