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

The following examples show how to use android.accounts.AccountManager#setUserData() . 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: TestUtils.java    From indigenous-android with GNU General Public License v3.0 6 votes vote down vote up
static void createAccount(Context context, boolean microsub) {
    String domain = "http://example.com";
    String accessToken = "awesome";

    AccountManager am = AccountManager.get(context);
    Account account = new Account(domain, ACCOUNT_TYPE);
    am.addAccountExplicitly(account, null, null);
    am.setAuthToken(account, TOKEN_TYPE, accessToken);
    am.setUserData(account, "micropub_endpoint", domain + "/micropub");
    am.setUserData(account, "authorization_endpoint", domain + "/auth");
    am.setUserData(account, "micropub_media_endpoint", domain + "/media");
    am.setUserData(account, "token_endpoint", domain + "/token");
    am.setUserData(account, "author_name", "Indigenous");

    // Set first account.
    SharedPreferences.Editor editor = context.getSharedPreferences("indigenous", MODE_PRIVATE).edit();
    editor.putString("account", domain);
    editor.apply();
    editor.commit();
}
 
Example 2
Source File: OdooAccountManager.java    From hr with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Login to user account. changes active state for user.
 * Other users will be automatically logged out
 *
 * @param context
 * @param username
 * @return new user object
 */
public static OUser login(Context context, String username) {

    // Setting odoo instance to null
    App app = (App) context.getApplicationContext();
    app.setOdoo(null, null);
    // Clearing models registry
    OModelRegistry registry = new OModelRegistry();
    registry.clearAll();
    OUser activeUser = getActiveUser(context);
    // Logging out user if any
    if (activeUser != null) {
        logout(context, activeUser.getAndroidName());
    }

    OUser newUser = getDetails(context, username);
    if (newUser != null) {
        AccountManager accountManager = AccountManager.get(context);
        accountManager.setUserData(newUser.getAccount(), "isactive", "true");
        Log.i(TAG, newUser.getName() + " Logged in successfully");
        return newUser;
    }
    // Clearing old cache of the system
    OCacheUtils.clearSystemCache(context);
    return null;
}
 
Example 3
Source File: OdooAccountManager.java    From framework with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Login to user account. changes active state for user.
 * Other users will be automatically logged out
 *
 * @param context
 * @param username
 * @return new user object
 */
public static OUser login(Context context, String username) {

    // Setting odoo instance to null
    App app = (App) context.getApplicationContext();
    app.setOdoo(null, null);
    OUser activeUser = getActiveUser(context);
    // Logging out user if any
    if (activeUser != null) {
        logout(context, activeUser.getAndroidName());
    }

    OUser newUser = getDetails(context, username);
    if (newUser != null) {
        AccountManager accountManager = AccountManager.get(context);
        accountManager.setUserData(newUser.getAccount(), "isactive", "true");
        Log.i(TAG, newUser.getName() + " Logged in successfully");
        return newUser;
    }
    // Clearing old cache of the system
    OCacheUtils.clearSystemCache(context);
    return null;
}
 
Example 4
Source File: MicropubAction.java    From indigenous-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Parse tags response.
 *
 * @param response
 *   Parse tags response.
 * @param tags
 *   The autocomplete field
 * @param saveInAccount
 *   Whether to set the tags list or not.
 * @param user
 *   The user.
 */
private void parseTagsResponse(String response, MultiAutoCompleteTextView tags, boolean saveInAccount, User user) {
    ArrayList<String> items = new ArrayList<>();

    try {
        JSONObject categoryResponse = new JSONObject(response);
        if (categoryResponse.has("categories")) {
            JSONArray tagsList = categoryResponse.getJSONArray("categories");
            if (tagsList.length() > 0) {
                for (int i = 0; i < tagsList.length(); i++) {
                    items.add(tagsList.getString(i));
                }
            }
        }
    }
    catch (JSONException ignored) {}

    if (items.size() > 0) {
        setTagsAutocomplete(tags, items);

        if (saveInAccount) {
            AccountManager am = AccountManager.get(context);
            am.setUserData(user.getAccount(), "tags_list", response);
        }
    }

}
 
Example 5
Source File: UiUtils.java    From tindroid with Apache License 2.0 5 votes vote down vote up
static void updateAndroidAccount(final Context context, final String uid, final String secret,
                                 final String token, final Date tokenExpires) {
    final AccountManager am = AccountManager.get(context);
    final Account acc = Utils.createAccount(uid);
    // It's OK to call even if the account already exists.
    am.addAccountExplicitly(acc, secret, null);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        am.notifyAccountAuthenticated(acc);
    }
    if (!TextUtils.isEmpty(token)) {
        am.setAuthToken(acc, Utils.TOKEN_TYPE, token);
        am.setUserData(acc, Utils.TOKEN_EXPIRATION_TIME, String.valueOf(tokenExpires.getTime()));
    }
}
 
Example 6
Source File: AuthenticationUtil.java    From palmsuda with Apache License 2.0 5 votes vote down vote up
public synchronized static void saveEditAccount(Context context,
		Account account) {
	if (mAccount == null) {
		Log.e(TAG, "saveEditAccount: mAccount is empty!");
		return;
	}
	AccountManager mAccountManager = AccountManager.get(context);
	mAccountManager.setPassword(account, mAccount.getAccountName());

	mAccountManager.setUserData(account, Params.PARAMS_LOGIN_PW,
			mAccount.getPassword());
	mAccountManager.setUserData(account, Params.PARAMS_LOGIN_ID,
			mAccount.getAccountName());
	mAccountManager.setUserData(account, Params.PARAMS_LOGIN_IMSI,
			mAccount.getImsi());
	mAccountManager.setUserData(account, Params.PARAMS_USER_BIRTHDAY,
			mAccount.getBirthDate());
	mAccountManager.setUserData(account, Params.PARAMS_USER_CITY,
			mAccount.getCityName());
	mAccountManager.setUserData(account, Params.PARAMS_USER_EMAIL,
			mAccount.getEmail());
	mAccountManager.setUserData(account, Params.PARAMS_USER_NICK_NAME,
			mAccount.getNickname());
	mAccountManager.setUserData(account, Params.PARAMS_USER_DESC,
			mAccount.getMkeywords());
	mAccountManager.setUserData(account, Params.PARAMS_USER_SEX,
			String.valueOf(mAccount.getSex()));
	mAccountManager.setUserData(account, Params.PARAMS_NEW_MOBILE_NUMBER,
			mAccount.getNewNum());

}
 
Example 7
Source File: AuthenticationUtil.java    From palmsuda with Apache License 2.0 5 votes vote down vote up
public synchronized static void updateAccountParameter(Context context,
		String key, String value) {
	AccountManager mAccountManager = AccountManager.get(context);
	Account[] mAccounts = mAccountManager
			.getAccountsByType(Constants.ACCOUNT_TYPE);

	mAccountManager.setUserData(mAccounts[0], key, value);
}
 
Example 8
Source File: OdooAccountManager.java    From hr with GNU Affero General Public License v3.0 5 votes vote down vote up
public static OUser updateUserData(Context context, OUser user, OUser newData) {
    if (user != null) {
        AccountManager accountManager = AccountManager.get(context);
        for (String key : newData.getAsBundle().keySet()) {
            accountManager.setUserData(user.getAccount(), key, newData.getAsBundle().get(key) + "");
        }
        OPreferenceManager pref = new OPreferenceManager(context);
        if (pref.getInt(userObjectKEY(user), 0) != OUser.USER_ACCOUNT_VERSION) {
            pref.putInt(userObjectKEY(user), OUser.USER_ACCOUNT_VERSION);
        }
    }
    return getDetails(context, newData.getAndroidName());
}
 
Example 9
Source File: OdooAccountManager.java    From hr with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Logout user
 *
 * @param context
 * @param username
 * @return true, if successfully logged out
 */
public static boolean logout(Context context, String username) {
    OUser user = getDetails(context, username);
    if (user != null) {
        if (cancelUserSync(user.getAccount())) {
            AccountManager accountManager = AccountManager.get(context);
            accountManager.setUserData(user.getAccount(), "isactive", "false");
            Log.i(TAG, user.getName() + " Logged out successfully");
            return true;
        }
    }
    return false;
}
 
Example 10
Source File: OdooAccountManager.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
public static OUser updateUserData(Context context, OUser user, OUser newData) {
    if (user != null) {
        AccountManager accountManager = AccountManager.get(context);
        for (String key : newData.getAsBundle().keySet()) {
            accountManager.setUserData(user.getAccount(), key, newData.getAsBundle().get(key) + "");
        }
        OPreferenceManager pref = new OPreferenceManager(context);
        if (pref.getInt(userObjectKEY(user), 0) != OUser.USER_ACCOUNT_VERSION) {
            pref.putInt(userObjectKEY(user), OUser.USER_ACCOUNT_VERSION);
        }
    }
    return getDetails(context, newData.getAndroidName());
}
 
Example 11
Source File: OdooAccountManager.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Logout user
 *
 * @param context
 * @param username
 * @return true, if successfully logged out
 */
public static boolean logout(Context context, String username) {
    OUser user = getDetails(context, username);
    if (user != null) {
        if (cancelUserSync(user.getAccount())) {
            AccountManager accountManager = AccountManager.get(context);
            accountManager.setUserData(user.getAccount(), "isactive", "false");
            Log.i(TAG, user.getName() + " Logged out successfully");
            return true;
        }
    }
    return false;
}
 
Example 12
Source File: BasicAccountAuthenticator.java    From Mover with Apache License 2.0 4 votes vote down vote up
@Override
@DebugLog
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
    mExtras.clear();

    Source source = getSource(authTokenType);

    if(source == null){
        mExtras.putInt(KEY_ERROR_CODE, INVALID_AUTH_TOKEN_TYPE);
        return mExtras;
    }

    AccountManager manager = mAccountManager;
    String authToken = mAccountManager.peekAuthToken(account, authTokenType);

    if(!source.isValidAuthToken(authToken)){
        Bundle bundle = source.getAuthenticator().getAuthToken(manager, account);

        authToken = bundle.getString(KEY_AUTHTOKEN);

        if(bundle.containsKey(USER_PICTURE_URL)){
            manager.setUserData(account, USER_PICTURE_URL, bundle.getString(USER_PICTURE_URL));
        }
    }

    if(TextUtils.isEmpty(authToken)){
        Intent intent = new Intent(ACTION_AUTHENTICATE)
                .putExtra(EXTRA_ACCOUNT, account)
                .putExtra(EXTRA_AUTH_TOKEN_TYPE, authTokenType)
                .putExtra(KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response)
                .putExtras(options);

        mExtras.putParcelable(KEY_INTENT, intent);
    }else{
        mExtras.putString(KEY_AUTHTOKEN, authToken);
        mExtras.putString(KEY_ACCOUNT_NAME, account.name);
        mExtras.putString(KEY_ACCOUNT_TYPE, account.type);
        mExtras.putString(EXTRA_AUTH_TOKEN_TYPE, authTokenType);
    }

    return mExtras;
}
 
Example 13
Source File: LoginUtils.java    From android-galaxyzoo with GNU General Public License v3.0 4 votes vote down vote up
private static void copyPrefToAccount(final AccountManager mgr, final Account account, final String key, final String value) {
    //Note that this requires the AUTHENTICATE_ACCOUNTS permission on
    //SDK <=22.
    mgr.setUserData(account, key, value);
}
 
Example 14
Source File: AuthHelper.java    From android-atleap with Apache License 2.0 3 votes vote down vote up
/**
 * Set user data to account
 * @param context context
 * @param account account
 * @param key key
 * @param value value
 */
public static void setUserData(Context context, Account account, String key, String value) {
    if(account == null)
        throw new IllegalArgumentException("account cannot be null");
    AccountManager accountManager = AccountManager.get(context);
    accountManager.setUserData(account, key, value);
}