Skip to content

Commit

Permalink
#108: com.github.k3b.ToGoZip:libK3b(Android)Zip:L2.1.21.2 => L2.1.22.…
Browse files Browse the repository at this point in the history
…3 to allow async cancel
  • Loading branch information
k3b committed Aug 5, 2019
1 parent a2d094e commit 7312fab
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 33 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ dependencies {
// https://jitpack.io/com/github/k3b/ToGoZip/L2.0.15/build.log
// https://jitpack.io/com/github/k3b/ToGoZip/libK3bZip/L2.0.15/
// implementation('com.github.k3b.ToGoZip:libK3bZip:L2.0.15') { transitive = false; }
implementation('com.github.k3b.ToGoZip:libK3bZip:L2.1.21.2') { transitive = false }
implementation('com.github.k3b.ToGoZip:libK3bAndroidZip:L2.1.21.2') { transitive = false }
implementation('com.github.k3b.ToGoZip:libK3bZip:L2.1.22.3') { transitive = false }
implementation('com.github.k3b.ToGoZip:libK3bAndroidZip:L2.1.22.3') { transitive = false }

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ public class Backup2ZipService implements IProgessListener, ZipLog {
private final Context context;
private final IZipConfig zipConfig;
private final ZipStorage zipStorage;
private final IProgessListener progessListener;
private IProgessListener progessListener = null;
private final ZipLog zipLog;

// set to false (in gui thread) if operation is canceled
protected boolean continueProcessing = true;

// used to translate ZipLog.traceMessage() to become IProgessListener
private int lastZipItemNumber = 0;

Expand All @@ -91,9 +94,14 @@ public Backup2ZipService(Context context, IZipConfig zipConfig, ZipStorage zipSt
this.context = context;
this.zipConfig = zipConfig;
this.zipStorage = zipStorage;
this.progessListener = progessListener;
this.zipLog = (LibZipGlobal.debugEnabled) ? new ZipLogImpl(true) : null;
}

public Backup2ZipService setProgessListener(IProgessListener progessListener) {
this.progessListener = progessListener;
return this;
}

/** Executes add2zip for all found items of found query-result-item of zipConfig */
public IZipConfig execute() {
ZipConfigRepository repo = new ZipConfigRepository(zipConfig);
Expand All @@ -105,35 +113,40 @@ public IZipConfig execute() {
// pipline for (IPhotoProperties item: query(filter)) : csv+=toCsv(item)
final PhotoPropertiesCsvStringSaver csvFromQuery = new PhotoPropertiesCsvStringSaver();

onProgress(0,0,
onProgress(0, 0,
"query images " + ((Global.debugEnabledSql) ? filter : ""));

final CompressJob job = new ApmZipCompressJob(context, this,"history.log");
job.setZipStorage(zipStorage);
if (this.continueProcessing) {
final CompressJob job = new ApmZipCompressJob(context, this, "history.log");
job.setZipStorage(zipStorage);

// pipline for (IPhotoProperties item: query(filter)) : Zip+=File(item)
/// !!! todo go on here
final IItemSaver<File> file2ZipSaver = new IItemSaver<File>() {
@Override
public boolean save(File item) {
job.addToCompressQue("", item);
return true;
}
};
// pipline for (IPhotoProperties item: query(filter)) : Zip+=File(item)
/// !!! todo go on here
final IItemSaver<File> file2ZipSaver = new IItemSaver<File>() {
@Override
public boolean save(File item) {
job.addToCompressQue("", item);
return true;
}
};

final PhotoProperties2ExistingFileSaver media2fileZipSaver = new PhotoProperties2ExistingFileSaver(file2ZipSaver);
final PhotoProperties2ExistingFileSaver media2fileZipSaver = new PhotoProperties2ExistingFileSaver(file2ZipSaver);

execQuery(filter, csvFromQuery, media2fileZipSaver);
execQuery(filter, csvFromQuery, media2fileZipSaver);

job.addTextToCompressQue("changes.csv", csvFromQuery.toString());
job.addTextToCompressQue("changes.csv", csvFromQuery.toString());

job.compress(false);
if (this.continueProcessing) {
// not canceled yet in gui thread
job.compress(false);
}

if (repo.save()) {
if (LibZipGlobal.debugEnabled) {
Log.d(LibZipGlobal.LOG_TAG, mDebugPrefix + " Saved as " + repo);
if (repo.save()) {
if (LibZipGlobal.debugEnabled) {
Log.d(LibZipGlobal.LOG_TAG, mDebugPrefix + " Saved as " + repo);
}
return repo;
}
return repo;
}
}
return null;
Expand Down Expand Up @@ -190,7 +203,11 @@ private void execQuery(QueryParameter query,
}

if ((progress % 100) == 0) {
onProgress(0, itemCount, context.getString(R.string.selection_status_format, progress));
if (!onProgress(0, itemCount, context.getString(R.string.selection_status_format, progress)))
{
// canceled in gui thread
return;
}
}
progress++;

Expand All @@ -214,16 +231,18 @@ private void execQuery(QueryParameter query,
@Override
public boolean onProgress(int itemcount, int size, String message) {
if (progessListener != null) {
return progessListener.onProgress(itemcount, size, message);
if (!progessListener.onProgress(itemcount, size, message)) {
this.continueProcessing = false;
}
}
return false;
return this.continueProcessing;
}

/**
* 10 `interface ZipLog-traceMessage` become one `interface onProgress()` message
*/
@Override
public String traceMessage(ZipJobState state, int itemNumber, int itemTotal, String format, Object... params) {
public String traceMessage(int state, int itemNumber, int itemTotal, String format, Object... params) {
if ((itemNumber != 0) && (itemNumber > this.lastZipItemNumber)) {
lastZipItemNumber = itemNumber;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TimePicker;
import android.widget.Toast;

Expand Down Expand Up @@ -97,6 +98,9 @@ public class BackupActivity extends ActivityWithAutoCloseDialogs implements Comm
private static final String STATE_ZIP_CONFIG = "zip_config";
private static String mDebugPrefix = "BackupActivity: ";

// != null while async backup is running
private static BackupAsyncTask backupAsyncTask = null;

private Gui gui = null;
private SelectedFiles mSelectedFiles;

Expand Down Expand Up @@ -317,6 +321,12 @@ protected void onCreate(Bundle savedInstanceState) {
loadGuiFromData();
}

@Override
protected void onDestroy() {
enableBackupAsyncTask(false);
super.onDestroy();
}

/**
* load layout and bind layout members
*/
Expand Down Expand Up @@ -629,10 +639,33 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
}

@Override
protected void onPause() {
enableBackupAsyncTask(false);
super.onPause();
}

@Override
protected void onResume() {
enableBackupAsyncTask(true);
Global.debugMemory(mDebugPrefix, "onResume");
super.onResume();

}

private void enableBackupAsyncTask(boolean enable) {
if (backupAsyncTask != null) {
final boolean isActive = BackupAsyncTask.isActive(backupAsyncTask);
if (enable && isActive) {
backupAsyncTask.setContext(
(ProgressBar) this.findViewById(R.id.progressBar));
} else {
backupAsyncTask.setContext(null);
if (!isActive) {
backupAsyncTask = null;
}
}
}
}

private void clearFilter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,49 @@

import android.content.Context;
import android.os.AsyncTask;
import android.widget.ProgressBar;

import java.util.concurrent.atomic.AtomicBoolean;

import de.k3b.io.IProgessListener;
import de.k3b.zip.IZipConfig;
import de.k3b.zip.ZipConfigDto;
import de.k3b.zip.ZipStorage;

public class BackupAsyncTask extends AsyncTask<Object, String, Object> {
public class BackupAsyncTask extends AsyncTask<Object, String, IZipConfig> implements IProgessListener {
private final Backup2ZipService service;
private ProgressBar mProgressBar = null;
private AtomicBoolean isActive = new AtomicBoolean(true);

public BackupAsyncTask(Context context, ZipConfigDto mZipConfigData, ZipStorage zipStorage) {
this.service = new Backup2ZipService(context, mZipConfigData, zipStorage, null);
this.service = new Backup2ZipService(context.getApplicationContext(),
mZipConfigData, zipStorage, null);
}

public void setContext(ProgressBar progressBar) {
mProgressBar = progressBar;
service.setProgessListener((progressBar != null) ? this : null);
}

@Override
protected IZipConfig doInBackground(Object... voids) {
try {
return this.service.execute();
} finally {
this.isActive.set(false);
}
}

public static boolean isActive(BackupAsyncTask backupAsyncTask) {
return (backupAsyncTask != null) && (backupAsyncTask.isActive.get());
}

/**
* called every time when command makes some little progress.
* return true to continue
*/
@Override
protected Object doInBackground(Object... voids) {
return null;
public boolean onProgress(int itemcount, int size, String message) {
return !this.isCancelled();
}
}
2 changes: 1 addition & 1 deletion fotolib2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies {
// https://jitpack.io/com/github/k3b/ToGoZip/L2.0.15/build.log
// https://jitpack.io/com/github/k3b/ToGoZip/libK3bZip/L2.0.15/
// implementation('com.github.k3b.ToGoZip:libK3bZip:L2.0.15') { transitive = false; }
implementation('com.github.k3b.ToGoZip:libK3bZip:L2.1.21.2') { transitive = false }
implementation('com.github.k3b.ToGoZip:libK3bZip:L2.1.22.3') { transitive = false }
// implementation('com.github.k3b.ToGoZip:libK3bAndroidZip:v2.0.18.181224') { transitive = false }
}

Expand Down

0 comments on commit 7312fab

Please sign in to comment.