From 040a5a20069b12c093e2b70a19e37f017461608e Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Tue, 23 May 2017 14:27:01 +0200 Subject: [PATCH] use autovalue for BackupKeyringParcel --- .../keychain/operations/BackupOperation.java | 29 ++++---- .../keychain/remote/OpenPgpService.java | 3 +- .../keychain/service/BackupKeyringParcel.java | 69 +++++-------------- .../keychain/ui/BackupCodeFragment.java | 3 +- .../operations/BackupOperationTest.java | 4 +- 5 files changed, 39 insertions(+), 69 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/BackupOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/BackupOperation.java index 924514cef..56c688b8f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/BackupOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/BackupOperation.java @@ -71,7 +71,7 @@ import org.sufficientlysecure.keychain.util.Log; * This class receives a source and/or destination of keys as input and performs * all steps for this backup. * - * @see org.sufficientlysecure.keychain.ui.adapter.ImportKeysAdapter#getSelectedEntries() + * see org.sufficientlysecure.keychain.ui.adapter.ImportKeysAdapter#getSelectedEntries() * For the backup operation, the input consists of a set of key ids and * either the name of a file or an output uri to write to. */ @@ -104,8 +104,8 @@ public class BackupOperation extends BaseOperation { OutputStream outputStream) { OperationLog log = new OperationLog(); - if (backupInput.mMasterKeyIds != null) { - log.add(LogType.MSG_BACKUP, 0, backupInput.mMasterKeyIds.length); + if (backupInput.getMasterKeyIds() != null) { + log.add(LogType.MSG_BACKUP, 0, backupInput.getMasterKeyIds().length); } else { log.add(LogType.MSG_BACKUP_ALL, 0); } @@ -113,7 +113,7 @@ public class BackupOperation extends BaseOperation { try { Uri plainUri = null; OutputStream plainOut; - if (backupInput.mIsEncrypted) { + if (backupInput.getIsEncrypted()) { if (cryptoInput == null) { throw new IllegalStateException("Encrypted backup must supply cryptoInput parameter"); } @@ -121,23 +121,23 @@ public class BackupOperation extends BaseOperation { plainUri = TemporaryFileProvider.createFile(mContext); plainOut = mContext.getContentResolver().openOutputStream(plainUri); } else { - if (backupInput.mOutputUri == null || outputStream != null) { + if (backupInput.getOutputUri() == null || outputStream != null) { throw new IllegalArgumentException("Unencrypted export to output stream is not supported!"); } else { - plainOut = mContext.getContentResolver().openOutputStream(backupInput.mOutputUri); + plainOut = mContext.getContentResolver().openOutputStream(backupInput.getOutputUri()); } } CountingOutputStream outStream = new CountingOutputStream(new BufferedOutputStream(plainOut)); boolean backupSuccess = exportKeysToStream( - log, backupInput.mMasterKeyIds, backupInput.mExportSecret, outStream); + log, backupInput.getMasterKeyIds(), backupInput.getExportSecret(), outStream); if (!backupSuccess) { // if there was an error, it will be in the log so we just have to return return new ExportResult(ExportResult.RESULT_ERROR, log); } - if (!backupInput.mIsEncrypted) { + if (!backupInput.getIsEncrypted()) { // log.add(LogType.MSG_EXPORT_NO_ENCRYPT, 1); log.add(LogType.MSG_BACKUP_SUCCESS, 1); return new ExportResult(ExportResult.RESULT_OK, log); @@ -172,25 +172,26 @@ public class BackupOperation extends BaseOperation { PgpSignEncryptData.Builder data = PgpSignEncryptData.builder(); data.setSymmetricPassphrase(cryptoInput.getPassphrase()); - data.setEnableAsciiArmorOutput(backupInput.mEnableAsciiArmorOutput); + data.setEnableAsciiArmorOutput(backupInput.getEnableAsciiArmorOutput()); data.setAddBackupHeader(true); PgpSignEncryptInputParcel inputParcel = new PgpSignEncryptInputParcel(data.build()); InputStream inStream = mContext.getContentResolver().openInputStream(plainUri); String filename; - if (backupInput.mMasterKeyIds != null && backupInput.mMasterKeyIds.length == 1) { - filename = Constants.FILE_BACKUP_PREFIX + KeyFormattingUtils.convertKeyIdToHex(backupInput.mMasterKeyIds[0]); + long[] masterKeyIds = backupInput.getMasterKeyIds(); + if (masterKeyIds != null && masterKeyIds.length == 1) { + filename = Constants.FILE_BACKUP_PREFIX + KeyFormattingUtils.convertKeyIdToHex(masterKeyIds[0]); } else { filename = Constants.FILE_BACKUP_PREFIX + new SimpleDateFormat("yyyy-MM-dd", Locale .getDefault()).format(new Date()); } - filename += backupInput.mExportSecret ? Constants.FILE_EXTENSION_BACKUP_SECRET : Constants.FILE_EXTENSION_BACKUP_PUBLIC; + filename += backupInput.getExportSecret() ? Constants.FILE_EXTENSION_BACKUP_SECRET : Constants.FILE_EXTENSION_BACKUP_PUBLIC; InputData inputData = new InputData(inStream, exportedDataSize, filename); OutputStream outStream; - if (backupInput.mOutputUri == null) { + if (backupInput.getOutputUri() == null) { if (outputStream == null) { throw new IllegalArgumentException("If output uri is not set, outputStream must not be null!"); } @@ -199,7 +200,7 @@ public class BackupOperation extends BaseOperation { if (outputStream != null) { throw new IllegalArgumentException("If output uri is set, outputStream must null!"); } - outStream = mContext.getContentResolver().openOutputStream(backupInput.mOutputUri); + outStream = mContext.getContentResolver().openOutputStream(backupInput.getOutputUri()); } return signEncryptOperation.execute(inputParcel, CryptoInputParcel.createCryptoInputParcel(), inputData, outStream); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java index be3e76a32..e0518856e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java @@ -658,7 +658,8 @@ public class OpenPgpService extends Service { // after user interaction with RemoteBackupActivity, // the backup code is cached in CryptoInputParcelCacheService, now we can proceed - BackupKeyringParcel input = new BackupKeyringParcel(masterKeyIds, backupSecret, true, enableAsciiArmorOutput, null); + BackupKeyringParcel input = BackupKeyringParcel + .createBackupKeyringParcel(masterKeyIds, backupSecret, true, enableAsciiArmorOutput, null); BackupOperation op = new BackupOperation(this, mKeyRepository, null); ExportResult pgpResult = op.execute(input, inputParcel, outputStream); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/BackupKeyringParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/BackupKeyringParcel.java index b0d98ee33..a5e139d59 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/BackupKeyringParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/BackupKeyringParcel.java @@ -19,61 +19,28 @@ package org.sufficientlysecure.keychain.service; + import android.net.Uri; -import android.os.Parcel; import android.os.Parcelable; +import android.support.annotation.Nullable; + +import com.google.auto.value.AutoValue; -public class BackupKeyringParcel implements Parcelable { - public Uri mCanonicalizedPublicKeyringUri; +@AutoValue +public abstract class BackupKeyringParcel implements Parcelable { + @Nullable + @SuppressWarnings("mutable") + public abstract long[] getMasterKeyIds(); + public abstract boolean getExportSecret(); + public abstract boolean getIsEncrypted(); + public abstract boolean getEnableAsciiArmorOutput(); + @Nullable + public abstract Uri getOutputUri(); - public final boolean mExportSecret; - public final boolean mIsEncrypted; - public final boolean mEnableAsciiArmorOutput; - public final long mMasterKeyIds[]; - public final Uri mOutputUri; - - public BackupKeyringParcel(long[] masterKeyIds, boolean exportSecret, boolean isEncrypted, boolean enableAsciiArmorOutput, Uri outputUri) { - mMasterKeyIds = masterKeyIds; - mExportSecret = exportSecret; - mOutputUri = outputUri; - mIsEncrypted = isEncrypted; - mEnableAsciiArmorOutput = enableAsciiArmorOutput; + public static BackupKeyringParcel createBackupKeyringParcel(long[] masterKeyIds, boolean exportSecret, + boolean isEncrypted, boolean enableAsciiArmorOutput, Uri outputUri) { + return new AutoValue_BackupKeyringParcel( + masterKeyIds, exportSecret, isEncrypted, enableAsciiArmorOutput, outputUri); } - - protected BackupKeyringParcel(Parcel in) { - mCanonicalizedPublicKeyringUri = (Uri) in.readValue(Uri.class.getClassLoader()); - mExportSecret = in.readByte() != 0x00; - mOutputUri = (Uri) in.readValue(Uri.class.getClassLoader()); - mMasterKeyIds = in.createLongArray(); - mIsEncrypted = in.readInt() != 0; - mEnableAsciiArmorOutput = in.readInt() != 0; - } - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(Parcel dest, int flags) { - dest.writeValue(mCanonicalizedPublicKeyringUri); - dest.writeByte((byte) (mExportSecret ? 0x01 : 0x00)); - dest.writeValue(mOutputUri); - dest.writeLongArray(mMasterKeyIds); - dest.writeInt(mIsEncrypted ? 1 : 0); - dest.writeInt(mEnableAsciiArmorOutput ? 1 : 0); - } - - public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { - @Override - public BackupKeyringParcel createFromParcel(Parcel in) { - return new BackupKeyringParcel(in); - } - - @Override - public BackupKeyringParcel[] newArray(int size) { - return new BackupKeyringParcel[size]; - } - }; } \ No newline at end of file diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/BackupCodeFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/BackupCodeFragment.java index 3aee1542d..c06e66c8a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/BackupCodeFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/BackupCodeFragment.java @@ -606,7 +606,8 @@ public class BackupCodeFragment extends CryptoOperationFragment