use autovalue for BackupKeyringParcel

This commit is contained in:
Vincent Breitmoser 2017-05-23 14:27:01 +02:00
parent c4a4fdadff
commit 040a5a2006
5 changed files with 39 additions and 69 deletions

View file

@ -71,7 +71,7 @@ import org.sufficientlysecure.keychain.util.Log;
* This class receives a source and/or destination of keys as input and performs * This class receives a source and/or destination of keys as input and performs
* all steps for this backup. * 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 * 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. * either the name of a file or an output uri to write to.
*/ */
@ -104,8 +104,8 @@ public class BackupOperation extends BaseOperation<BackupKeyringParcel> {
OutputStream outputStream) { OutputStream outputStream) {
OperationLog log = new OperationLog(); OperationLog log = new OperationLog();
if (backupInput.mMasterKeyIds != null) { if (backupInput.getMasterKeyIds() != null) {
log.add(LogType.MSG_BACKUP, 0, backupInput.mMasterKeyIds.length); log.add(LogType.MSG_BACKUP, 0, backupInput.getMasterKeyIds().length);
} else { } else {
log.add(LogType.MSG_BACKUP_ALL, 0); log.add(LogType.MSG_BACKUP_ALL, 0);
} }
@ -113,7 +113,7 @@ public class BackupOperation extends BaseOperation<BackupKeyringParcel> {
try { try {
Uri plainUri = null; Uri plainUri = null;
OutputStream plainOut; OutputStream plainOut;
if (backupInput.mIsEncrypted) { if (backupInput.getIsEncrypted()) {
if (cryptoInput == null) { if (cryptoInput == null) {
throw new IllegalStateException("Encrypted backup must supply cryptoInput parameter"); throw new IllegalStateException("Encrypted backup must supply cryptoInput parameter");
} }
@ -121,23 +121,23 @@ public class BackupOperation extends BaseOperation<BackupKeyringParcel> {
plainUri = TemporaryFileProvider.createFile(mContext); plainUri = TemporaryFileProvider.createFile(mContext);
plainOut = mContext.getContentResolver().openOutputStream(plainUri); plainOut = mContext.getContentResolver().openOutputStream(plainUri);
} else { } else {
if (backupInput.mOutputUri == null || outputStream != null) { if (backupInput.getOutputUri() == null || outputStream != null) {
throw new IllegalArgumentException("Unencrypted export to output stream is not supported!"); throw new IllegalArgumentException("Unencrypted export to output stream is not supported!");
} else { } else {
plainOut = mContext.getContentResolver().openOutputStream(backupInput.mOutputUri); plainOut = mContext.getContentResolver().openOutputStream(backupInput.getOutputUri());
} }
} }
CountingOutputStream outStream = new CountingOutputStream(new BufferedOutputStream(plainOut)); CountingOutputStream outStream = new CountingOutputStream(new BufferedOutputStream(plainOut));
boolean backupSuccess = exportKeysToStream( boolean backupSuccess = exportKeysToStream(
log, backupInput.mMasterKeyIds, backupInput.mExportSecret, outStream); log, backupInput.getMasterKeyIds(), backupInput.getExportSecret(), outStream);
if (!backupSuccess) { if (!backupSuccess) {
// if there was an error, it will be in the log so we just have to return // if there was an error, it will be in the log so we just have to return
return new ExportResult(ExportResult.RESULT_ERROR, log); 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_EXPORT_NO_ENCRYPT, 1);
log.add(LogType.MSG_BACKUP_SUCCESS, 1); log.add(LogType.MSG_BACKUP_SUCCESS, 1);
return new ExportResult(ExportResult.RESULT_OK, log); return new ExportResult(ExportResult.RESULT_OK, log);
@ -172,25 +172,26 @@ public class BackupOperation extends BaseOperation<BackupKeyringParcel> {
PgpSignEncryptData.Builder data = PgpSignEncryptData.builder(); PgpSignEncryptData.Builder data = PgpSignEncryptData.builder();
data.setSymmetricPassphrase(cryptoInput.getPassphrase()); data.setSymmetricPassphrase(cryptoInput.getPassphrase());
data.setEnableAsciiArmorOutput(backupInput.mEnableAsciiArmorOutput); data.setEnableAsciiArmorOutput(backupInput.getEnableAsciiArmorOutput());
data.setAddBackupHeader(true); data.setAddBackupHeader(true);
PgpSignEncryptInputParcel inputParcel = new PgpSignEncryptInputParcel(data.build()); PgpSignEncryptInputParcel inputParcel = new PgpSignEncryptInputParcel(data.build());
InputStream inStream = mContext.getContentResolver().openInputStream(plainUri); InputStream inStream = mContext.getContentResolver().openInputStream(plainUri);
String filename; String filename;
if (backupInput.mMasterKeyIds != null && backupInput.mMasterKeyIds.length == 1) { long[] masterKeyIds = backupInput.getMasterKeyIds();
filename = Constants.FILE_BACKUP_PREFIX + KeyFormattingUtils.convertKeyIdToHex(backupInput.mMasterKeyIds[0]); if (masterKeyIds != null && masterKeyIds.length == 1) {
filename = Constants.FILE_BACKUP_PREFIX + KeyFormattingUtils.convertKeyIdToHex(masterKeyIds[0]);
} else { } else {
filename = Constants.FILE_BACKUP_PREFIX + new SimpleDateFormat("yyyy-MM-dd", Locale filename = Constants.FILE_BACKUP_PREFIX + new SimpleDateFormat("yyyy-MM-dd", Locale
.getDefault()).format(new Date()); .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); InputData inputData = new InputData(inStream, exportedDataSize, filename);
OutputStream outStream; OutputStream outStream;
if (backupInput.mOutputUri == null) { if (backupInput.getOutputUri() == null) {
if (outputStream == null) { if (outputStream == null) {
throw new IllegalArgumentException("If output uri is not set, outputStream must not be null!"); throw new IllegalArgumentException("If output uri is not set, outputStream must not be null!");
} }
@ -199,7 +200,7 @@ public class BackupOperation extends BaseOperation<BackupKeyringParcel> {
if (outputStream != null) { if (outputStream != null) {
throw new IllegalArgumentException("If output uri is set, outputStream must 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); return signEncryptOperation.execute(inputParcel, CryptoInputParcel.createCryptoInputParcel(), inputData, outStream);

View file

@ -658,7 +658,8 @@ public class OpenPgpService extends Service {
// after user interaction with RemoteBackupActivity, // after user interaction with RemoteBackupActivity,
// the backup code is cached in CryptoInputParcelCacheService, now we can proceed // 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); BackupOperation op = new BackupOperation(this, mKeyRepository, null);
ExportResult pgpResult = op.execute(input, inputParcel, outputStream); ExportResult pgpResult = op.execute(input, inputParcel, outputStream);

View file

@ -19,61 +19,28 @@
package org.sufficientlysecure.keychain.service; package org.sufficientlysecure.keychain.service;
import android.net.Uri; import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.support.annotation.Nullable;
import com.google.auto.value.AutoValue;
public class BackupKeyringParcel implements Parcelable { @AutoValue
public Uri mCanonicalizedPublicKeyringUri; 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 static BackupKeyringParcel createBackupKeyringParcel(long[] masterKeyIds, boolean exportSecret,
public final boolean mIsEncrypted; boolean isEncrypted, boolean enableAsciiArmorOutput, Uri outputUri) {
public final boolean mEnableAsciiArmorOutput; return new AutoValue_BackupKeyringParcel(
public final long mMasterKeyIds[]; masterKeyIds, exportSecret, isEncrypted, enableAsciiArmorOutput, outputUri);
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;
} }
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<BackupKeyringParcel> CREATOR = new Parcelable.Creator<BackupKeyringParcel>() {
@Override
public BackupKeyringParcel createFromParcel(Parcel in) {
return new BackupKeyringParcel(in);
}
@Override
public BackupKeyringParcel[] newArray(int size) {
return new BackupKeyringParcel[size];
}
};
} }

View file

@ -606,7 +606,8 @@ public class BackupCodeFragment extends CryptoOperationFragment<BackupKeyringPar
@Nullable @Nullable
@Override @Override
public BackupKeyringParcel createOperationInput() { public BackupKeyringParcel createOperationInput() {
return new BackupKeyringParcel(mMasterKeyIds, mExportSecret, true, true, mCachedBackupUri); return BackupKeyringParcel
.createBackupKeyringParcel(mMasterKeyIds, mExportSecret, true, true, mCachedBackupUri);
} }
@Override @Override

View file

@ -252,7 +252,7 @@ public class BackupOperationTest {
BackupOperation op = new BackupOperation(spyApplication, BackupOperation op = new BackupOperation(spyApplication,
KeyWritableRepository.createDatabaseReadWriteInteractor(RuntimeEnvironment.application), null); KeyWritableRepository.createDatabaseReadWriteInteractor(RuntimeEnvironment.application), null);
BackupKeyringParcel parcel = new BackupKeyringParcel( BackupKeyringParcel parcel = BackupKeyringParcel.createBackupKeyringParcel(
new long[] { mStaticRing1.getMasterKeyId() }, false, false, true, fakeOutputUri); new long[] { mStaticRing1.getMasterKeyId() }, false, false, true, fakeOutputUri);
ExportResult result = op.execute(parcel, null); ExportResult result = op.execute(parcel, null);
@ -309,7 +309,7 @@ public class BackupOperationTest {
BackupOperation op = new BackupOperation(spyApplication, BackupOperation op = new BackupOperation(spyApplication,
KeyWritableRepository.createDatabaseReadWriteInteractor(RuntimeEnvironment.application), null); KeyWritableRepository.createDatabaseReadWriteInteractor(RuntimeEnvironment.application), null);
BackupKeyringParcel parcel = new BackupKeyringParcel( BackupKeyringParcel parcel = BackupKeyringParcel.createBackupKeyringParcel(
new long[] { mStaticRing1.getMasterKeyId() }, false, true, true, fakeOutputUri); new long[] { mStaticRing1.getMasterKeyId() }, false, true, true, fakeOutputUri);
CryptoInputParcel inputParcel = CryptoInputParcel.createCryptoInputParcel(passphrase); CryptoInputParcel inputParcel = CryptoInputParcel.createCryptoInputParcel(passphrase);
ExportResult result = op.execute(parcel, inputParcel); ExportResult result = op.execute(parcel, inputParcel);