Skip to content

Commit

Permalink
The main thread is not meant to be slept on
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbWatershed committed Jan 7, 2022
1 parent 79f32b8 commit cabb05c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import me.devsaki.hentoid.enums.StatusContent;
import me.devsaki.hentoid.parsers.ParseHelper;
import me.devsaki.hentoid.util.FileHelper;
import me.devsaki.hentoid.util.Helper;
import me.devsaki.hentoid.util.JsonHelper;
import me.devsaki.hentoid.util.Preferences;
import me.devsaki.hentoid.util.network.HttpHelper;
Expand Down Expand Up @@ -140,23 +141,9 @@ protected Content processContent(@NonNull Content content, @NonNull String url,
// Wait until the page's resources are all loaded
if (!quickDownload) {
Timber.i(">> not loading");
while (!isLoading()) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
Timber.w(e);
Thread.currentThread().interrupt();
}
}
while (!isLoading()) Helper.pause(20);
Timber.i(">> loading");
while (isLoading()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Timber.w(e);
Thread.currentThread().interrupt();
}
}
while (isLoading()) Helper.pause(100);
Timber.i(">> done");
}
final AtomicReference<String> imagesStr = new AtomicReference<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
import me.devsaki.hentoid.json.sources.EHentaiImageQuery;
import me.devsaki.hentoid.json.sources.EHentaiImageResponse;
import me.devsaki.hentoid.parsers.ParseHelper;
import me.devsaki.hentoid.util.Helper;
import me.devsaki.hentoid.util.JsonHelper;
import me.devsaki.hentoid.util.exception.EmptyResultException;
import me.devsaki.hentoid.util.exception.LimitReachedException;
import me.devsaki.hentoid.util.exception.PreparationInterruptedException;
import me.devsaki.hentoid.util.network.HttpHelper;
import okhttp3.Response;
import okhttp3.ResponseBody;
import timber.log.Timber;

public class EHentaiParser implements ImageListParser {

Expand Down Expand Up @@ -158,14 +158,7 @@ private List<ImageFile> loadMpv(
result.add(ParseHelper.urlToImageFile(imageMetadata.getUrl(), pageNum, pageCount, StatusContent.SAVED));
progress.advance();
// Emulate JS loader
if (0 == pageNum % 10) {
try {
Thread.sleep(750);
} catch (InterruptedException e) {
Timber.w(e);
Thread.currentThread().interrupt();
}
}
if (0 == pageNum % 10) Helper.pause(750);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
import me.devsaki.hentoid.json.sources.EHentaiImageQuery;
import me.devsaki.hentoid.json.sources.EHentaiImageResponse;
import me.devsaki.hentoid.parsers.ParseHelper;
import me.devsaki.hentoid.util.Helper;
import me.devsaki.hentoid.util.JsonHelper;
import me.devsaki.hentoid.util.exception.EmptyResultException;
import me.devsaki.hentoid.util.exception.LimitReachedException;
import me.devsaki.hentoid.util.exception.PreparationInterruptedException;
import me.devsaki.hentoid.util.network.HttpHelper;
import okhttp3.Response;
import okhttp3.ResponseBody;
import timber.log.Timber;

public class ExHentaiParser implements ImageListParser {

Expand Down Expand Up @@ -143,14 +143,7 @@ private List<ImageFile> loadMpv(
result.add(ParseHelper.urlToImageFile(imageMetadata.getUrl(), pageNum, pageCount, StatusContent.SAVED));
progress.advance();
// Emulate JS loader
if (0 == pageNum % 10) {
try {
Thread.sleep(750);
} catch (InterruptedException e) {
Timber.w(e);
Thread.currentThread().interrupt();
}
}
if (0 == pageNum % 10) Helper.pause(750);
}

return result;
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/me/devsaki/hentoid/util/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,17 @@ public static void tryShowMenuIcons(@NonNull Context context, @NonNull Menu menu
}
}

// TODO doc
public static void pause(int millis) {
assertNonUiThread();
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
Timber.w(e);
Thread.currentThread().interrupt();
}
}

/**
* Update the JSON file that stores bookmarks with the current bookmarks
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
import java.util.Random;
import java.util.concurrent.TimeUnit;

import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import me.devsaki.hentoid.util.Helper;
import me.devsaki.hentoid.util.Preferences;
import me.devsaki.hentoid.util.network.VolleyOkHttp3Stack;
import timber.log.Timber;
Expand Down Expand Up @@ -178,12 +179,7 @@ private void refillRequestQueue() {
long now = Instant.now().toEpochMilli();
int allowedNewRequests = getAllowedNewRequests(now);
while (0 == allowedNewRequests && 0 == nbActiveRequests) { // Dry queue
try {
Thread.sleep(250);
} catch (InterruptedException e) {
Timber.w(e);
Thread.currentThread().interrupt();
}
Helper.pause(250);
now = Instant.now().toEpochMilli();
allowedNewRequests = getAllowedNewRequests(now);
}
Expand Down Expand Up @@ -220,19 +216,30 @@ public void onRequestFinished(Request<T> request) {
Timber.d("Waiting requests queue ::: waiting %d ms", delayMs);
waitDisposable = Observable.timer(delayMs, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.observeOn(Schedulers.computation())
.map(v -> {
// Add the next request to the queue
Timber.d("Waiting requests queue ::: request added for host %s - current total %s", Uri.parse(request.getUrl()).getHost(), waitingRequestQueue.size());
Request<T> req = waitingRequestQueue.removeFirst();
addToRequestQueue(req);
return true;
})
.observeOn(Schedulers.computation())
.subscribe(
v -> {
// Add the next request to the queue
Timber.d("Waiting requests queue ::: request added for host %s - current total %s", Uri.parse(request.getUrl()).getHost(), waitingRequestQueue.size());
Request<T> req = waitingRequestQueue.removeFirst();
addToRequestQueue(req);
waitDisposable.dispose();
},
v -> waitDisposable.dispose(),
Timber::e
);
}
if (nbRequestsPerSecond > -1) refillRequestQueue();
if (nbRequestsPerSecond > -1) {
waitDisposable = Completable.fromRunnable(this::refillRequestQueue)
.subscribeOn(Schedulers.computation())
.observeOn(Schedulers.computation())
.subscribe(
() -> waitDisposable.dispose(),
Timber::e
);

}
}
}

Expand All @@ -248,10 +255,6 @@ public void setNbRequestsPerSecond(int value) {
nbRequestsPerSecond = value;
}

public int getNbRequestsPerSecond() {
return nbRequestsPerSecond;
}

// This will cancel any current download
public void setDownloadThreadCount(@NonNull Context ctx, int value) {
manualDlThreadCount = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import me.devsaki.hentoid.util.ArchiveHelper;
import me.devsaki.hentoid.util.ContentHelper;
import me.devsaki.hentoid.util.FileHelper;
import me.devsaki.hentoid.util.Helper;
import me.devsaki.hentoid.util.ImageHelper;
import me.devsaki.hentoid.util.JsonHelper;
import me.devsaki.hentoid.util.Preferences;
Expand Down Expand Up @@ -416,12 +417,7 @@ private ImmutablePair<QueuingResult, Content> downloadFirstInQueue() {
// Wait a delay corresponding to book browsing if we're between two sources with "simulate human reading"
if (content.getSite().isSimulateHumanReading() && requestQueueManager.isSimulateHumanReading()) {
int delayMs = 3000 + new Random().nextInt(2000);
try {
Thread.sleep(delayMs);
} catch (InterruptedException e) {
Timber.d(e);
Thread.currentThread().interrupt();
}
Helper.pause(delayMs);
}

if (content.getSite().getParallelDownloadCap() > 0 &&
Expand Down Expand Up @@ -577,13 +573,7 @@ private void watchProgress(@NonNull Content content) {
}

// We're polling the DB because we can't observe LiveData from a background service
try {
//noinspection BusyWait
Thread.sleep(1000);
} catch (InterruptedException e) {
Timber.i(e);
Thread.currentThread().interrupt();
}
Helper.pause(1000);
}
while (!isDone && !downloadInterrupted.get() && !contentQueueManager.isQueuePaused());

Expand Down

0 comments on commit cabb05c

Please sign in to comment.