Clean up ContentProviderBackupComponent

This commit is contained in:
Steve Soltys 2017-10-23 20:00:25 -04:00
parent 765b8b2540
commit b9239143e0

View file

@ -3,12 +3,12 @@ package com.stevesoltys.backup.transport.component.provider.backup;
import android.app.backup.BackupDataInput; import android.app.backup.BackupDataInput;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.net.Uri;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.util.Base64; import android.util.Base64;
import android.util.Log; import android.util.Log;
import com.stevesoltys.backup.transport.component.BackupComponent; import com.stevesoltys.backup.transport.component.BackupComponent;
import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfiguration; import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfiguration;
import libcore.io.IoUtils;
import sun.misc.IOUtils; import sun.misc.IOUtils;
import java.io.*; import java.io.*;
@ -20,8 +20,6 @@ import java.util.zip.ZipOutputStream;
import static android.app.backup.BackupTransport.*; import static android.app.backup.BackupTransport.*;
/** /**
* TODO: Clean this up. Much of it was taken from the LocalTransport implementation.
*
* @author Steve Soltys * @author Steve Soltys
*/ */
public class ContentProviderBackupComponent implements BackupComponent { public class ContentProviderBackupComponent implements BackupComponent {
@ -62,6 +60,21 @@ public class ContentProviderBackupComponent implements BackupComponent {
return TRANSPORT_OK; return TRANSPORT_OK;
} }
@Override
public int clearBackupData(PackageInfo packageInfo) {
return TRANSPORT_OK;
}
@Override
public long getBackupQuota(String packageName, boolean fullBackup) {
return configuration.getBackupSizeQuota();
}
@Override
public long requestFullBackupTime() {
return 0;
}
private void initializeBackupState() throws IOException { private void initializeBackupState() throws IOException {
if (backupState == null) { if (backupState == null) {
backupState = new ContentProviderBackupState(); backupState = new ContentProviderBackupState();
@ -73,10 +86,8 @@ public class ContentProviderBackupComponent implements BackupComponent {
} }
private void initializeOutputStream() throws FileNotFoundException { private void initializeOutputStream() throws FileNotFoundException {
Uri outputUri = configuration.getUri();
ContentResolver contentResolver = configuration.getContext().getContentResolver(); ContentResolver contentResolver = configuration.getContext().getContentResolver();
ParcelFileDescriptor outputFileDescriptor = contentResolver.openFileDescriptor(outputUri, "w"); ParcelFileDescriptor outputFileDescriptor = contentResolver.openFileDescriptor(configuration.getUri(), "w");
backupState.setOutputFileDescriptor(outputFileDescriptor); backupState.setOutputFileDescriptor(outputFileDescriptor);
FileOutputStream fileOutputStream = new FileOutputStream(outputFileDescriptor.getFileDescriptor()); FileOutputStream fileOutputStream = new FileOutputStream(outputFileDescriptor.getFileDescriptor());
@ -84,11 +95,6 @@ public class ContentProviderBackupComponent implements BackupComponent {
backupState.setOutputStream(zipOutputStream); backupState.setOutputStream(zipOutputStream);
} }
@Override
public int clearBackupData(PackageInfo packageInfo) {
return TRANSPORT_OK;
}
@Override @Override
public int performIncrementalBackup(PackageInfo packageInfo, ParcelFileDescriptor data) { public int performIncrementalBackup(PackageInfo packageInfo, ParcelFileDescriptor data) {
BackupDataInput backupDataInput = new BackupDataInput(data.getFileDescriptor()); BackupDataInput backupDataInput = new BackupDataInput(data.getFileDescriptor());
@ -119,8 +125,8 @@ public class ContentProviderBackupComponent implements BackupComponent {
int dataSize = backupDataInput.getDataSize(); int dataSize = backupDataInput.getDataSize();
if (dataSize >= 0) { if (dataSize >= 0) {
ZipEntry zipEntry = new ZipEntry(configuration.getIncrementalBackupDirectory() + backupState.getPackageName() + ZipEntry zipEntry = new ZipEntry(configuration.getIncrementalBackupDirectory() +
"/" + chunkFileName); backupState.getPackageName() + "/" + chunkFileName);
outputStream.putNextEntry(zipEntry); outputStream.putNextEntry(zipEntry);
if (dataSize > bufferSize) { if (dataSize > bufferSize) {
@ -136,7 +142,6 @@ public class ContentProviderBackupComponent implements BackupComponent {
} catch (Exception ex) { } catch (Exception ex) {
Log.e(TAG, "Error performing incremental backup for " + backupState.getPackageName() + ": ", ex); Log.e(TAG, "Error performing incremental backup for " + backupState.getPackageName() + ": ", ex);
clearBackupState(true); clearBackupState(true);
return TRANSPORT_ERROR; return TRANSPORT_ERROR;
} }
} }
@ -168,7 +173,6 @@ public class ContentProviderBackupComponent implements BackupComponent {
} catch (Exception ex) { } catch (Exception ex) {
Log.e(TAG, "Error creating backup file for " + targetPackage.packageName + ": ", ex); Log.e(TAG, "Error creating backup file for " + targetPackage.packageName + ": ", ex);
clearBackupState(true); clearBackupState(true);
return TRANSPORT_ERROR; return TRANSPORT_ERROR;
} }
@ -193,7 +197,7 @@ public class ContentProviderBackupComponent implements BackupComponent {
public int sendBackupData(int numBytes) { public int sendBackupData(int numBytes) {
if (backupState == null) { if (backupState == null) {
Log.e(TAG, "Attempted sendBackupData before performFullBackup"); Log.e(TAG, "Attempted sendBackupData() before performFullBackup()");
return TRANSPORT_ERROR; return TRANSPORT_ERROR;
} }
@ -234,10 +238,8 @@ public class ContentProviderBackupComponent implements BackupComponent {
} }
try { try {
if (backupState.getInputFileDescriptor() != null) { IoUtils.closeQuietly(backupState.getInputFileDescriptor());
backupState.getInputFileDescriptor().close();
backupState.setInputFileDescriptor(null); backupState.setInputFileDescriptor(null);
}
ZipOutputStream outputStream = backupState.getOutputStream(); ZipOutputStream outputStream = backupState.getOutputStream();
@ -246,16 +248,12 @@ public class ContentProviderBackupComponent implements BackupComponent {
} }
if (backupState.getPackageIndex() == configuration.getPackageCount() || closeFile) { if (backupState.getPackageIndex() == configuration.getPackageCount() || closeFile) {
if (outputStream != null) { if (outputStream != null) {
outputStream.finish(); outputStream.finish();
outputStream.close(); outputStream.close();
} }
if (backupState.getOutputFileDescriptor() != null) { IoUtils.closeQuietly(backupState.getOutputFileDescriptor());
backupState.getOutputFileDescriptor().close();
}
backupState = null; backupState = null;
} }
@ -266,14 +264,4 @@ public class ContentProviderBackupComponent implements BackupComponent {
return TRANSPORT_OK; return TRANSPORT_OK;
} }
@Override
public long getBackupQuota(String packageName, boolean fullBackup) {
return fullBackup ? configuration.getBackupSizeQuota() : Long.MAX_VALUE;
}
@Override
public long requestFullBackupTime() {
return 0;
}
} }