Skip to content

Commit

Permalink
Fix: Change account update by removing existing account
Browse files Browse the repository at this point in the history
  • Loading branch information
fmariqueo-kunder committed Aug 26, 2021
1 parent 17f11de commit 500138f
Showing 1 changed file with 20 additions and 26 deletions.
46 changes: 20 additions & 26 deletions src/android/cl/kunder/accountmanager/AccountManagerPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ else if (action.equals("addAccount")) {
String accountType = args.getString(2);
Bundle userData = new Bundle();

try{
password = AESCrypt.encrypt(ENCRYPTION_KEY, password);
}catch (GeneralSecurityException e){
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR));
return false;
}

try {
JSONObject jsonObject = args.getJSONObject(4);
Iterator<?> iterator = jsonObject.keys();
Expand All @@ -86,10 +79,29 @@ else if (action.equals("addAccount")) {

Account [] accounts = accountManager.getAccountsByType(accountType);

if(accounts.length == 1) {
// Remove existing account
if(Build.VERSION.SDK_INT >= 22){
accountManager.removeAccountExplicitly(accounts[0]);
}
else{
//Deprecated on API 22
accountManager.removeAccount(accounts[0], null, null);
}
// Refresh accounts array
accounts = accountManager.getAccountsByType(accountType);
}

if(accounts.length == 0){
//No hay cuentas, entonces es posible añadir una

Account account = new Account(userAccount, accountType);

try{
password = AESCrypt.encrypt(ENCRYPTION_KEY, password);
}catch (GeneralSecurityException e){
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR));
return false;
}

if(accountManager.addAccountExplicitly(account, password, userData)){
// Toast.makeText(getApplicationContext(), "Registro de cuenta exitoso", Toast.LENGTH_LONG).show();
Expand All @@ -102,24 +114,6 @@ else if (action.equals("addAccount")) {
// Toast.makeText(getApplicationContext(), "Error al registrar una cuenta!!!", Toast.LENGTH_LONG).show();
}

} else if(accounts.length == 1) {
// Actualizar todos los datos con la nueva cuenta
Iterator<?> iterator = userData.keys();
while (iterator.hasNext()){
String key = (String)iterator.next();
String value = userData.get(key).toString();
try{
key = AESCrypt.encrypt(ENCRYPTION_KEY, key);
value = AESCrypt.encrypt(ENCRYPTION_KEY, value);
}catch (GeneralSecurityException e){
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR));
return false;
}

accountManager.setUserData(accounts[0],key,value);
}
accountManager.setPassword(accounts[0], password);
accountManager.renameAccount(accounts[0], userAccount);
}
else {
//Caso contrario, no se debe realizar el proceso
Expand Down

0 comments on commit 500138f

Please sign in to comment.