Howto let a user change account data in 'Accounts and sync' - android

There is an existing account on the phone that is used for a sync service. The account has some settings that the user entered when he created the account. Theses settings are stored as user data (--> mAccountManager.addAccountExplicitly(account, mPassword, userData)).
The user should be able to change these settings. How can this be achieved? Do I need a standalone app to change existing account data?
I guess the user would go to 'Settings'/'Accounts and sync'/'myAccount' and should find a menu entry like 'modify account data'. This menu entry should open the same activity that the user has already used to enter the data initially.
Any hints to push me in the right direction?

This fooled me for a while too - I expected to find getUserData()/setUserData() methods on the Account class, but they are on the AccountManager instead:
AccountManager am = AccountManager.get(context);
String myData = am.getUserData(account, SomeClass.MY_DATA_KEY);
myData = "Some New Value";
am.setUserData(account, SomeClass.MY_DATA_KEY, myData);
Check out the AccountManager setUserData method docs for more information.
Cheers, Andrew.

Related

Disable user account for FRP, allow only work managed accounts

Prelude:
The client wants to install EMM for his owner devices. Sometimes the user wants to use the device as work device (only taxi and couriers apps) and sometimes the user wants to use the device as his own (install games, social apps, and his own Google accounts).
Situation:
The client wants to store his gsuite accounts in FRP storage (to have the ability to unlock a phone in case employee leave organization) but doesn't want an employee to unlock the phone after FR entering his personal account credentials.
Example:
I added two work account programmatically (like described here). But after the user gets the phone he entered his personal Gmail account to use Gmail, other apps. How can I programmatically or maybe from DPC app prevents user recover access to the phone using his personal account after Factory Reset?
I found the answer, finally. The link to the documentation.
I need just to create a bundle with google plus ids of accounts that will have the opportunity to recover device after factory reset.
And after that need to send a broadcast to notify the system that these values were changed.
val bundle = Bundle()
// list of recovery accounts
val recoveryAccounts = arrayOf(
"115273111154663031432",
"110369192556268846321",
)
bundle.putStringArray("factoryResetProtectionAdmin", recoveryAccounts)
mAdminComponentName = DeviceAdminReceiver.getComponentName(context)
// set restrictions
mDevicePolicyManager.setApplicationRestrictions(mAdminComponentName, "com.google.android.gms", bundle)
// send broadcast
val broadcastIntent = Intent("com.google.android.gms.auth.FRP_CONFIG_CHANGED")
broadcastIntent.setPackage("com.google.android.gms")
broadcastIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND)
applicationContext.sendBroadcast(broadcastIntent)
Off topic.
To get the id of recovery accounts try this: https://developers.google.com/people/api/rest/v1/people/get?apix=true&apix_params={"resourceName":"people/me","personFields":"metadata"}
Push "Execute" and login with needed account. It'll be shown in "id" field of response.

how to add google account programmatically in android with one click in background?

i am trying to add google account programmatically to android using a app that takes username and password and logins the user in background. But i m having problem as it is showing error...
Error: Caller uid 10024 is different than authenticator's uid .
I had already included all permissions and authenticator xml but it is not responding...
plz help i want to add google account in backgroundwith one click ..
Thanks in advance..
Account acc = new Account("mygmailid#gmail.com", "com.google");
AccountManager am = AccountManager.get(this);
Bundle userdata = new Bundle();
userdata.putString("SERVER", "extra");
if (am.addAccountExplicitly(acc, "mypass", userdata)) {
// success message..
}
Basically this would be impossible to do because adding an new google account without the prior information of the user itself would definetely be a kind of malware or theft app.
Therefore according to Google this would lead to violate the user freedom and its trust towards Android operating system.
But you can add new google account and can ask user to enter the password for that account. So there will be no permission violation.

Change username and password of android custom account

I have created sync adapter for android that syncs data with my server. I works fine, but now I want to be able to change my username and password without removing and adding the account again. How can I do this?
I have a login screen that has edit texts for entering username and password, but how to apply these changes to the account?
EDIT:
Found a way how to change the password:
AccountManager.get(mContext).setPassword(account, password );
where account is my account, and password is the new password.
So now my question is: HOW TO CHANGE THE USERNAME?
I can change the password with no problem, but if I want to change the username of the account I must delete and recreate the account with the new username. This deletes all data from that account and resyncs the account again from the beginning.
AccountManager.renameAccount(Account account, String newName, AccountManagerCallback<Account> callback, Handler handler)
This has been unfortunately added only in API 21 (documentation).
In previous versions, deleting and recreating the account with the new username is the only way, as mentioned in another answer.
Use an AccountAuthenticatorActivity, which you can either open from the Settings -> Accounts & Sync page inside your account, or when you try to fetch from the server and get an error indicating wrong login. Look here for details of how to set it up. Writing an Android Sync Provider: Part 1

receive on clear data

Is it possible to receive an event, if the user execute the "Clear Data" on the global app screen?
I create an account in my Application and save the name of the account as preference:
AccountManager accountManager = AccountManager.get(this.context);
Account account = new Account("my account name", "com.mchammer.cantouchthis");
accountManager.addAccountExplicitly(account, "", null);
now, if the user clears the data, the account on the system (used for synchronisation) have to removed too...
thanks for your help
You can't. Your app gets FCed on Clear Data, so you can't run any code.
BUT. You can use SharedPreferences to store a boolean pref, say isAccountInitialized.
You set it to true and store after your AccountManager logic has done creating the account. Then, every time you use the account for sync (or every time you launch your main activity, or your service is up do do something - this you'll have to figure out based on your app specifics) you pull the isAccountInitialized from shared prefs and if it's false and your account is present in AccountManager - that's your sign that user has executed Clear Data and it's time to remove the account from AccountManager. If it's false and there's no account - that's first launch.
// OFFTOP Nice package name :)
It is not possible as As soon as you press clear data. Your application process will be stopped. You can check this link for answer.

Synching accountmanager accounts

I am writing an android application that will start sync for all accounts added under "Account & sync" settings.I am fetching all the added accounts using the following code
AccountManager am = (AccountManager) getSystemService(Context.ACCOUNT_SERVICE);
Account[]acs = am.getAccounts();
After fetching the account I want to start sync for each account
for(Account ac:acs){
ContentResolver.requestSync(ac,authority,extras);
}
My question is how do i fetch the authority for the retrieved account ?
A far simpler answer is simply to turn off the global sync enabled flag and then turn it back on. Android starts a full sync by defualt in this case.
See ContentResolver.setMasterSyncAutomatically().
Your app will need appropriate permissions in the manifest to be allowed to change that flag.

Categories

Resources