mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-05 02:39:23 +02:00
Loading is interrupted on leaving catalog
git-svn-id: https://only.mawhrin.net/repos/FBReaderJ/trunk@1369 6a642e6f-84f6-412e-ac94-c4a38d5a04b0
This commit is contained in:
parent
377923f68b
commit
70901bcf3c
8 changed files with 203 additions and 81 deletions
|
@ -19,7 +19,8 @@
|
|||
|
||||
package org.geometerplus.android.fbreader.network;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import android.os.Message;
|
||||
import android.os.Handler;
|
||||
|
||||
import org.geometerplus.fbreader.network.NetworkOperationData;
|
||||
import org.geometerplus.fbreader.network.NetworkLibraryItem;
|
||||
|
@ -30,7 +31,6 @@ abstract class ItemsLoadingRunnable implements Runnable {
|
|||
public static final int CATALOG_LOADING = 0;
|
||||
public static final int NETWORK_SEARCH = 1;
|
||||
|
||||
public final AtomicBoolean InterruptFlag = new AtomicBoolean();
|
||||
public final int Type;
|
||||
|
||||
private final ItemsLoadingHandler myHandler;
|
||||
|
@ -38,6 +38,47 @@ abstract class ItemsLoadingRunnable implements Runnable {
|
|||
private final long myUpdateInterval; // in milliseconds
|
||||
private final int myItemsLimit;
|
||||
|
||||
private boolean myInterruptRequested;
|
||||
private boolean myInterruptConfirmed;
|
||||
private Object myInterruptLock = new Object();
|
||||
|
||||
|
||||
private boolean myFinished;
|
||||
private Handler myFinishedHandler;
|
||||
private Object myFinishedLock = new Object();
|
||||
|
||||
|
||||
public void interrupt() {
|
||||
synchronized (myInterruptLock) {
|
||||
myInterruptRequested = true;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean confirmInterrupt() {
|
||||
synchronized (myInterruptLock) {
|
||||
if (myInterruptRequested) {
|
||||
myInterruptConfirmed = true;
|
||||
}
|
||||
return myInterruptConfirmed;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean tryResume() {
|
||||
synchronized (myInterruptLock) {
|
||||
if (!myInterruptConfirmed) {
|
||||
myInterruptRequested = false;
|
||||
}
|
||||
return !myInterruptRequested;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isInterrupted() {
|
||||
synchronized (myInterruptLock) {
|
||||
return myInterruptConfirmed;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public ItemsLoadingRunnable(ItemsLoadingHandler handler, int type) {
|
||||
this(handler, type, 1000, 500);
|
||||
}
|
||||
|
@ -59,7 +100,7 @@ abstract class ItemsLoadingRunnable implements Runnable {
|
|||
public final void run() {
|
||||
String err = doBefore();
|
||||
if (err != null) {
|
||||
myHandler.sendFinish(err);
|
||||
myHandler.sendFinish(err, false);
|
||||
return;
|
||||
}
|
||||
err = doLoading(new NetworkOperationData.OnNewItemListener() {
|
||||
|
@ -67,7 +108,8 @@ abstract class ItemsLoadingRunnable implements Runnable {
|
|||
private int myItemsNumber;
|
||||
public boolean onNewItem(NetworkLibraryItem item) {
|
||||
myHandler.addItem(item);
|
||||
if (InterruptFlag.get() || myItemsNumber++ >= myItemsLimit) {
|
||||
final boolean interrupted = confirmInterrupt();
|
||||
if (interrupted || myItemsNumber++ >= myItemsLimit) {
|
||||
return true;
|
||||
}
|
||||
final long now = System.currentTimeMillis();
|
||||
|
@ -79,7 +121,31 @@ abstract class ItemsLoadingRunnable implements Runnable {
|
|||
}
|
||||
});
|
||||
myHandler.sendUpdateItems();
|
||||
myHandler.ensureFinish();
|
||||
myHandler.sendFinish(err);
|
||||
myHandler.ensureItemsProcessed();
|
||||
myHandler.sendFinish(err, isInterrupted());
|
||||
myHandler.ensureFinishProcessed();
|
||||
synchronized (myFinishedLock) {
|
||||
if (myFinishedHandler != null) {
|
||||
myFinishedHandler.sendEmptyMessage(0);
|
||||
}
|
||||
myFinished = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void runOnFinish(final Runnable runnable) {
|
||||
if (myFinishedHandler != null) {
|
||||
return;
|
||||
}
|
||||
synchronized (myFinishedLock) {
|
||||
if (myFinished) {
|
||||
runnable.run();
|
||||
} else {
|
||||
myFinishedHandler = new Handler() {
|
||||
public void handleMessage(Message message) {
|
||||
runnable.run();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue