Search in sources :

Example 1 with QUEUE_NEW_DOWNLOADS_POSITION_TOP

use of me.devsaki.hentoid.util.Preferences.Constant.QUEUE_NEW_DOWNLOADS_POSITION_TOP in project Hentoid by avluis.

the class BaseWebActivity method processDownload.

/**
 * Add current content (i.e. content of the currently viewed book) to the download queue
 *
 * @param quickDownload True if the action has been triggered by a quick download
 *                      (which means we're not on a book gallery page but on the book list page)
 */
void processDownload(boolean quickDownload, boolean isDownloadPlus) {
    if (null == currentContent)
        return;
    if (currentContent.getId() > 0)
        currentContent = dao.selectContent(currentContent.getId());
    if (null == currentContent)
        return;
    if (!isDownloadPlus && StatusContent.DOWNLOADED == currentContent.getStatus()) {
        ToastHelper.toast(R.string.already_downloaded);
        if (!quickDownload)
            setActionMode(ActionMode.READ);
        return;
    }
    if (isDownloadPlus) {
        // Copy the _current_ content's download params to the extra images
        String downloadParamsStr = currentContent.getDownloadParams();
        if (downloadParamsStr != null && downloadParamsStr.length() > 2) {
            for (ImageFile i : extraImages) i.setDownloadParams(downloadParamsStr);
        }
        // Determine base book : browsed downloaded book or best duplicate ?
        if (!ContentHelper.isInLibrary(currentContent.getStatus()) && duplicateId > 0) {
            currentContent = dao.selectContent(duplicateId);
            if (null == currentContent)
                return;
        }
        // Append additional pages & chapters to the base book's list of pages & chapters
        // Entire image set to update
        List<ImageFile> updatedImgs = new ArrayList<>();
        // URLs of known images
        Set<String> existingImageUrls = new HashSet<>();
        // Positions of known chapters
        Set<Integer> existingChapterOrders = new HashSet<>();
        if (currentContent.getImageFiles() != null) {
            existingImageUrls.addAll(Stream.of(currentContent.getImageFiles()).map(ImageFile::getUrl).toList());
            existingChapterOrders.addAll(Stream.of(currentContent.getImageFiles()).map(i -> {
                if (null == i.getChapter())
                    return -1;
                if (null == i.getChapter().getTarget())
                    return -1;
                return i.getChapter().getTarget().getOrder();
            }).toList());
            updatedImgs.addAll(currentContent.getImageFiles());
        }
        // Save additional pages references to stored book, without duplicate URLs
        List<ImageFile> additionalNonExistingImages = Stream.of(extraImages).filterNot(i -> existingImageUrls.contains(i.getUrl())).toList();
        if (!additionalNonExistingImages.isEmpty()) {
            updatedImgs.addAll(additionalNonExistingImages);
            currentContent.setImageFiles(updatedImgs);
        }
        // Save additional chapters to stored book
        List<Chapter> additionalNonExistingChapters = Stream.of(additionalNonExistingImages).map(ImageFile::getChapter).withoutNulls().map(ToOne::getTarget).withoutNulls().filterNot(c -> existingChapterOrders.contains(c.getOrder())).toList();
        if (!additionalNonExistingChapters.isEmpty()) {
            List<Chapter> updatedChapters;
            if (currentContent.getChapters() != null)
                updatedChapters = new ArrayList<>(currentContent.getChapters());
            else
                updatedChapters = new ArrayList<>();
            updatedChapters.addAll(additionalNonExistingChapters);
            currentContent.setChapters(updatedChapters);
        }
        currentContent.setStatus(StatusContent.SAVED);
        dao.insertContent(currentContent);
    }
    // Check if the tag blocker applies here
    List<String> blockedTagsLocal = ContentHelper.getBlockedTags(currentContent);
    if (!blockedTagsLocal.isEmpty()) {
        if (Preferences.getTagBlockingBehaviour() == Preferences.Constant.DL_TAG_BLOCKING_BEHAVIOUR_DONT_QUEUE) {
            // Stop right here
            ToastHelper.toast(R.string.blocked_tag, blockedTagsLocal.get(0));
        } else {
            // Insert directly as an error
            List<ErrorRecord> errors = new ArrayList<>();
            errors.add(new ErrorRecord(ErrorType.BLOCKED, currentContent.getUrl(), "tags", "blocked tags : " + TextUtils.join(", ", blockedTagsLocal), Instant.now()));
            currentContent.setErrorLog(errors);
            currentContent.setStatus(StatusContent.ERROR);
            dao.insertContent(currentContent);
            ToastHelper.toast(R.string.blocked_tag_queued, blockedTagsLocal.get(0));
            setActionMode(ActionMode.VIEW_QUEUE);
        }
        return;
    }
    // No reason to block or ignore -> actually add to the queue
    if (Preferences.getQueueNewDownloadPosition() == QUEUE_NEW_DOWNLOADS_POSITION_ASK)
        AddQueueMenu.show(this, webView, this, (position, item) -> addToQueue((0 == position) ? QUEUE_NEW_DOWNLOADS_POSITION_TOP : QUEUE_NEW_DOWNLOADS_POSITION_BOTTOM, Preferences.getBrowserDlAction()));
    else
        addToQueue(Preferences.getQueueNewDownloadPosition(), Preferences.getBrowserDlAction());
}
Also used : JavascriptInterface(android.webkit.JavascriptInterface) Bundle(android.os.Bundle) QueueActivity(me.devsaki.hentoid.activities.QueueActivity) ProgressBar(android.widget.ProgressBar) NonNull(androidx.annotation.NonNull) ImageView(android.widget.ImageView) NestedScrollWebView(me.devsaki.hentoid.views.NestedScrollWebView) Chapter(me.devsaki.hentoid.database.domains.Chapter) BadgeDrawable(com.google.android.material.badge.BadgeDrawable) ColorStateList(android.content.res.ColorStateList) Site(me.devsaki.hentoid.enums.Site) CookieManager(android.webkit.CookieManager) Handler(android.os.Handler) Map(java.util.Map) ImageFile(me.devsaki.hentoid.database.domains.ImageFile) ResponseBody(okhttp3.ResponseBody) CoordinatorLayout(androidx.coordinatorlayout.widget.CoordinatorLayout) Set(java.util.Set) ToastHelper(me.devsaki.hentoid.util.ToastHelper) HttpHelper(me.devsaki.hentoid.util.network.HttpHelper) SiteBookmark(me.devsaki.hentoid.database.domains.SiteBookmark) StringRes(androidx.annotation.StringRes) Message(android.os.Message) QUEUE_NEW_DOWNLOADS_POSITION_TOP(me.devsaki.hentoid.util.Preferences.Constant.QUEUE_NEW_DOWNLOADS_POSITION_TOP) QUEUE_NEW_DOWNLOADS_POSITION_BOTTOM(me.devsaki.hentoid.util.Preferences.Constant.QUEUE_NEW_DOWNLOADS_POSITION_BOTTOM) SiteHistory(me.devsaki.hentoid.database.domains.SiteHistory) Stream(com.annimon.stream.Stream) PrefsBundle(me.devsaki.hentoid.activities.bundles.PrefsBundle) BookmarksDialogFragment(me.devsaki.hentoid.fragments.web.BookmarksDialogFragment) AddQueueMenu(me.devsaki.hentoid.widget.AddQueueMenu) ArrayList(java.util.ArrayList) TooltipHelper(me.devsaki.hentoid.util.TooltipHelper) RQST_STORAGE_PERMISSION(me.devsaki.hentoid.util.PermissionHelper.RQST_STORAGE_PERMISSION) WebSettings(android.webkit.WebSettings) DownloadPreparationEvent(me.devsaki.hentoid.events.DownloadPreparationEvent) Helper(me.devsaki.hentoid.util.Helper) Instant(org.threeten.bp.Instant) DuplicateHelper(me.devsaki.hentoid.util.DuplicateHelper) Response(okhttp3.Response) PermissionHelper(me.devsaki.hentoid.util.PermissionHelper) WebChromeClient(android.webkit.WebChromeClient) ContentParserFactory(me.devsaki.hentoid.parsers.ContentParserFactory) ErrorRecord(me.devsaki.hentoid.database.domains.ErrorRecord) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) TextUtils(android.text.TextUtils) IOException(java.io.IOException) ImageListParser(me.devsaki.hentoid.parsers.images.ImageListParser) BiConsumer(com.annimon.stream.function.BiConsumer) WebHistoryItem(android.webkit.WebHistoryItem) ArrowOrientation(com.skydoves.balloon.ArrowOrientation) R(me.devsaki.hentoid.R) SharedPreferences(android.content.SharedPreferences) Content(me.devsaki.hentoid.database.domains.Content) WindowManager(android.view.WindowManager) DrawableRes(androidx.annotation.DrawableRes) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) AlertStatus(me.devsaki.hentoid.enums.AlertStatus) ContentHelper(me.devsaki.hentoid.util.ContentHelper) Optional(com.annimon.stream.Optional) BuildConfig(me.devsaki.hentoid.BuildConfig) StringHelper(me.devsaki.hentoid.util.StringHelper) View(android.view.View) WebBackForwardList(android.webkit.WebBackForwardList) Schedulers(io.reactivex.schedulers.Schedulers) WebView(android.webkit.WebView) BaseWebActivityBundle(me.devsaki.hentoid.activities.bundles.BaseWebActivityBundle) BottomNavigationView(com.google.android.material.bottomnavigation.BottomNavigationView) BaseActivity(me.devsaki.hentoid.activities.BaseActivity) DownloadEvent(me.devsaki.hentoid.events.DownloadEvent) ThreadMode(org.greenrobot.eventbus.ThreadMode) Animatable(android.graphics.drawable.Animatable) ViewGroup(android.view.ViewGroup) Timber(timber.log.Timber) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) TextView(android.widget.TextView) Toolbar(androidx.appcompat.widget.Toolbar) CollectionDAO(me.devsaki.hentoid.database.CollectionDAO) ErrorType(me.devsaki.hentoid.enums.ErrorType) UpdateEvent(me.devsaki.hentoid.events.UpdateEvent) InputDialog(me.devsaki.hentoid.ui.InputDialog) DuplicateDialogFragment(me.devsaki.hentoid.fragments.web.DuplicateDialogFragment) LibraryActivity(me.devsaki.hentoid.activities.LibraryActivity) KeyEvent(android.view.KeyEvent) Preferences(me.devsaki.hentoid.util.Preferences) Pair(android.util.Pair) ToOne(io.objectbox.relation.ToOne) Intent(android.content.Intent) HashMap(java.util.HashMap) PrefsActivity(me.devsaki.hentoid.activities.PrefsActivity) IntDef(androidx.annotation.IntDef) Single(io.reactivex.Single) QueueActivityBundle(me.devsaki.hentoid.activities.bundles.QueueActivityBundle) StatusContent(me.devsaki.hentoid.enums.StatusContent) MenuItem(android.view.MenuItem) Retention(java.lang.annotation.Retention) HashSet(java.util.HashSet) SuppressLint(android.annotation.SuppressLint) QUEUE_NEW_DOWNLOADS_POSITION_ASK(me.devsaki.hentoid.util.Preferences.Constant.QUEUE_NEW_DOWNLOADS_POSITION_ASK) EventBus(org.greenrobot.eventbus.EventBus) UpdateInfo(me.devsaki.hentoid.json.core.UpdateInfo) ContentQueueManager(me.devsaki.hentoid.util.download.ContentQueueManager) FileHelper(me.devsaki.hentoid.util.FileHelper) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) ObjectBoxDAO(me.devsaki.hentoid.database.ObjectBoxDAO) Subscribe(org.greenrobot.eventbus.Subscribe) Bitmap(android.graphics.Bitmap) Collections(java.util.Collections) Resources(android.content.res.Resources) InputStream(java.io.InputStream) RetentionPolicy(java.lang.annotation.RetentionPolicy) ImageFile(me.devsaki.hentoid.database.domains.ImageFile) ArrayList(java.util.ArrayList) Chapter(me.devsaki.hentoid.database.domains.Chapter) HashSet(java.util.HashSet) ErrorRecord(me.devsaki.hentoid.database.domains.ErrorRecord)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 ColorStateList (android.content.res.ColorStateList)1 Resources (android.content.res.Resources)1 Bitmap (android.graphics.Bitmap)1 Animatable (android.graphics.drawable.Animatable)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 Message (android.os.Message)1 TextUtils (android.text.TextUtils)1 Pair (android.util.Pair)1 KeyEvent (android.view.KeyEvent)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 WindowManager (android.view.WindowManager)1 CookieManager (android.webkit.CookieManager)1 JavascriptInterface (android.webkit.JavascriptInterface)1 WebBackForwardList (android.webkit.WebBackForwardList)1