Use background tasks for encryption change

This commit is contained in:
Jakob Nixdorf 2021-03-24 07:46:37 +01:00
parent 58f4826f42
commit d8a1e03806
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
2 changed files with 73 additions and 8 deletions

View file

@ -49,6 +49,7 @@ import org.openintents.openpgp.util.OpenPgpAppPreference;
import org.openintents.openpgp.util.OpenPgpKeyPreference;
import org.shadowice.flocke.andotp.Preferences.CredentialsPreference;
import org.shadowice.flocke.andotp.R;
import org.shadowice.flocke.andotp.Tasks.ChangeEncryptionTask;
import org.shadowice.flocke.andotp.Utilities.BackupHelper;
import org.shadowice.flocke.andotp.Utilities.Constants;
import org.shadowice.flocke.andotp.Utilities.DatabaseHelper;
@ -73,6 +74,8 @@ public class SettingsActivity extends BaseActivity
private SecretKey encryptionKey = null;
private boolean encryptionChanged = false;
private Toast inProgress = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -207,16 +210,12 @@ public class SettingsActivity extends BaseActivity
fragment.setEncryptionKey(newEncryptionKey);
}
private void tryEncryptionChange(EncryptionType newEnc, byte[] newKey) {
Toast upgrading = Toast.makeText(this, R.string.settings_toast_encryption_changing, Toast.LENGTH_LONG);
upgrading.show();
private void handleTaskResult(ChangeEncryptionTask.Result result) {
inProgress.cancel();
EncryptionHelper.EncryptionChangeResult result = EncryptionHelper.tryEncryptionChange(this, encryptionKey, newEnc, newKey, this);
upgrading.cancel();
switch (result) {
switch (result.result) {
case SUCCESS:
onSuccessfulEncryptionChange(result.encryptionType, result.newEncryptionKey);
Toast.makeText(this, R.string.settings_toast_encryption_change_success, Toast.LENGTH_LONG).show();
break;
case BACKUP_FAILURE:
@ -228,6 +227,17 @@ public class SettingsActivity extends BaseActivity
}
}
private void tryEncryptionChange(EncryptionType newEnc, byte[] newKey) {
ChangeEncryptionTask task = new ChangeEncryptionTask(this, encryptionKey, newEnc, newKey);
task.setCallback(this::handleTaskResult);
// TODO: Better in-progress notification
inProgress = Toast.makeText(this, R.string.settings_toast_encryption_changing, Toast.LENGTH_LONG);
inProgress.show();
task.execute();
}
private void requestBackupAccess() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION

View file

@ -0,0 +1,55 @@
package org.shadowice.flocke.andotp.Tasks;
import android.content.Context;
import androidx.annotation.NonNull;
import org.shadowice.flocke.andotp.Utilities.Constants;
import org.shadowice.flocke.andotp.Utilities.EncryptionHelper;
import javax.crypto.SecretKey;
public class ChangeEncryptionTask extends UiBasedBackgroundTask<ChangeEncryptionTask.Result>
implements EncryptionHelper.EncryptionChangeCallback {
private final Context context;
private final SecretKey oldEncryptionKey;
private final Constants.EncryptionType newEncryptionType;
private final byte[] newKeyMaterial;
private SecretKey newEncryptionKey = null;
public ChangeEncryptionTask(Context context, SecretKey oldEncryptionKey, Constants.EncryptionType newEncryptionType, byte[] newKey) {
super(new Result(EncryptionHelper.EncryptionChangeResult.TASK_CREATION_FAILED, null, null));
this.context = context;
this.oldEncryptionKey = oldEncryptionKey;
this.newEncryptionType = newEncryptionType;
this. newKeyMaterial = newKey;
}
@Override
public void onSuccessfulEncryptionChange(Constants.EncryptionType newEncryptionType, SecretKey newEncryptionKey) {
this.newEncryptionKey = newEncryptionKey;
}
@NonNull
@Override
protected Result doInBackground() {
EncryptionHelper.EncryptionChangeResult result = EncryptionHelper.tryEncryptionChange(context, oldEncryptionKey, newEncryptionType, newKeyMaterial, this);
return new Result(result, this.newEncryptionKey, this.newEncryptionType);
}
public static class Result {
public final EncryptionHelper.EncryptionChangeResult result;
public final SecretKey newEncryptionKey;
public final Constants.EncryptionType encryptionType;
public Result(EncryptionHelper.EncryptionChangeResult result, SecretKey newKey, Constants.EncryptionType encryptionType) {
this.result = result;
this.newEncryptionKey = newKey;
this.encryptionType = encryptionType;
}
}
}