public class

ZoomScrollValueObserver

extends java.lang.Object

implements ObservableValue.ValueObserver<ZoomView.ZoomScroll>

 java.lang.Object

↳androidx.pdf.viewer.ZoomScrollValueObserver

Gradle dependencies

compile group: 'androidx.pdf', name: 'pdf-viewer', version: '1.0.0-alpha02'

  • groupId: androidx.pdf
  • artifactId: pdf-viewer
  • version: 1.0.0-alpha02

Artifact androidx.pdf:pdf-viewer:1.0.0-alpha02 it located at Google repository (https://maven.google.com/)

Summary

Constructors
publicZoomScrollValueObserver(ZoomView zoomView, PaginatedView paginatedView, LayoutHandler layoutHandler, FloatingActionButton annotationButton, FindInFileView findInFileView, boolean isAnnotationIntentResolvable, SelectionActionMode selectionActionMode, ObservableValue<ViewState> viewState)

Methods
public voidclearAnnotationHandler()

Exposing a function to clear the handler when PDFViewer Fragment is destroyed.

public voidonChange(ZoomView.ZoomScroll oldPosition, ZoomView.ZoomScroll position)

public voidsetAnnotationIntentResolvable(boolean annotationIntentResolvable)

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public ZoomScrollValueObserver(ZoomView zoomView, PaginatedView paginatedView, LayoutHandler layoutHandler, FloatingActionButton annotationButton, FindInFileView findInFileView, boolean isAnnotationIntentResolvable, SelectionActionMode selectionActionMode, ObservableValue<ViewState> viewState)

Methods

public void onChange(ZoomView.ZoomScroll oldPosition, ZoomView.ZoomScroll position)

public void clearAnnotationHandler()

Exposing a function to clear the handler when PDFViewer Fragment is destroyed.

public void setAnnotationIntentResolvable(boolean annotationIntentResolvable)

Source

/*
 * Copyright 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.pdf.viewer;

import android.graphics.Rect;
import android.os.Handler;
import android.os.Looper;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.pdf.ViewState;
import androidx.pdf.find.FindInFileView;
import androidx.pdf.select.SelectionActionMode;
import androidx.pdf.util.ObservableValue;
import androidx.pdf.widget.ZoomView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

@RestrictTo(RestrictTo.Scope.LIBRARY)
public class ZoomScrollValueObserver implements ObservableValue.ValueObserver<ZoomView.ZoomScroll> {
    private final PaginatedView mPaginatedView;
    private final ZoomView mZoomView;
    private final LayoutHandler mLayoutHandler;
    private final FloatingActionButton mAnnotationButton;
    private final Handler mAnnotationButtonHandler;
    private final FindInFileView mFindInFileView;
    private boolean mIsAnnotationIntentResolvable;
    private final SelectionActionMode mSelectionActionMode;
    private final ObservableValue<ViewState> mViewState;

    private boolean mIsPageScrollingUp;

    public ZoomScrollValueObserver(@Nullable ZoomView zoomView,
            @Nullable PaginatedView paginatedView,
            @NonNull LayoutHandler layoutHandler, @NonNull FloatingActionButton annotationButton,
            @NonNull FindInFileView findInFileView, boolean isAnnotationIntentResolvable,
            @NonNull SelectionActionMode selectionActionMode,
            @NonNull ObservableValue<ViewState> viewState) {
        mZoomView = zoomView;
        mPaginatedView = paginatedView;
        mLayoutHandler = layoutHandler;
        mAnnotationButton = annotationButton;
        mFindInFileView = findInFileView;
        mIsAnnotationIntentResolvable = isAnnotationIntentResolvable;
        mSelectionActionMode = selectionActionMode;
        mViewState = viewState;
        mAnnotationButtonHandler = new Handler(Looper.getMainLooper());
        mIsPageScrollingUp = false;
    }

    @Override
    public void onChange(@Nullable ZoomView.ZoomScroll oldPosition,
            @Nullable ZoomView.ZoomScroll position) {
        if (mPaginatedView == null || !mPaginatedView.getModel().isInitialized()
                || position == null || mPaginatedView.getModel().getSize() == 0) {
            return;
        }

        mZoomView.loadPageAssets(mLayoutHandler, mViewState);

        if (oldPosition.scrollY > position.scrollY) {
            mIsPageScrollingUp = true;
        } else if (oldPosition.scrollY < position.scrollY) {
            mIsPageScrollingUp = false;
        }

        // Stop showing context menu if there is any change in zoom or scroll, resume only when
        // the new position is stable
        if (mPaginatedView.getSelectionModel().selection().get() != null) {
            mSelectionActionMode.stopActionMode();
            if (position.stable) {
                setUpContextMenu();
            }
        }

        if (mIsAnnotationIntentResolvable && !mPaginatedView.isConfigurationChanged()) {

            if (!isAnnotationButtonVisible() && position.scrollY == 0
                    && mFindInFileView.getVisibility() == View.GONE) {
                mAnnotationButton.show();
            } else if (isAnnotationButtonVisible() && mIsPageScrollingUp) {
                clearAnnotationHandler();
                return;
            }
            if (position.scrollY == oldPosition.scrollY) {
                mAnnotationButtonHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (position.scrollY != 0) {
                            mAnnotationButton.hide();
                        }
                    }
                });
            }
        } else if (mPaginatedView.isConfigurationChanged()
                && position.scrollY != oldPosition.scrollY) {
            mPaginatedView.setConfigurationChanged(false);
        }
    }

    private boolean isAnnotationButtonVisible() {
        return mAnnotationButton.getVisibility() == View.VISIBLE;
    }

    /** Exposing a function to clear the handler when PDFViewer Fragment is destroyed. */
    public void clearAnnotationHandler() {
        mAnnotationButtonHandler.removeCallbacksAndMessages(null);
    }

    public void setAnnotationIntentResolvable(boolean annotationIntentResolvable) {
        mIsAnnotationIntentResolvable = annotationIntentResolvable;
    }

    private void setUpContextMenu() {
        // Resume the context menu if selected area is on the current viewing screen
        if (mPaginatedView.getSelectionModel().getPage() != -1) {
            int selectionPage = mPaginatedView.getSelectionModel().getPage();
            int firstPageInVisibleRange =
                    mPaginatedView.getPageRangeHandler().getVisiblePages().getFirst();
            int lastPageInVisisbleRange =
                    mPaginatedView.getPageRangeHandler().getVisiblePages().getLast();

            // If selection is within the range of visible pages
            if (selectionPage >= firstPageInVisibleRange
                    && selectionPage <= lastPageInVisisbleRange) {
                // Start and stop coordinates in a page wrt pagination model
                int startX = mPaginatedView.getModel().getLookAtX(selectionPage,
                        mPaginatedView.getSelectionModel().selection().get().getStart().getX());
                int startY = mPaginatedView.getModel().getLookAtY(selectionPage,
                        mPaginatedView.getSelectionModel().selection().get().getStart().getY());
                int stopX = mPaginatedView.getModel().getLookAtX(selectionPage,
                        mPaginatedView.getSelectionModel().selection().get().getStop().getX());
                int stopY = mPaginatedView.getModel().getLookAtY(selectionPage,
                        mPaginatedView.getSelectionModel().selection().get().getStop().getY());

                Rect currentViewArea = mPaginatedView.getViewArea();

                if (currentViewArea.intersect(startX, startY, stopX, stopY)) {
                    mSelectionActionMode.resume();
                }
            }
        }
    }

}