/**
         * Called when the PDF is loaded
         */
        public void LoadComplete(PdfFile pdfFile)
        {
            state        = State.Loaded;
            this.PdfFile = pdfFile;

            if (!renderingHandlerThread.IsAlive)
            {
                renderingHandlerThread.Start();
            }

            RenderingHandler = new RenderingHandler(renderingHandlerThread.Looper,
                                                    this);
            RenderingHandler.Start();

            if (ScrollHandle != null)
            {
                ScrollHandle.SetupLayout(this);
                isScrollHandleInit = true;
            }

            dragPinchManager.Enable();

            Callbacks.CallOnLoadComplete(this, new LoadCompletedEventArgs(pdfFile.PagesCount));

            JumpTo(DefaultPage, false);
        }
        private void ShowPage(int pageNb)
        {
            if (IsRecycled)
            {
                return;
            }

            pageNb      = PdfFile.DetermineValidPageNumberFrom(pageNb);
            CurrentPage = pageNb;

            LoadPages();

            if (ScrollHandle != null && !DocumentFitsView)
            {
                ScrollHandle.SetPageNum(CurrentPage + 1);
            }

            Callbacks.CallOnPageChange(this, new PageChangeEventArgs(CurrentPage, PdfFile.PagesCount));
        }
        public void Recycle()
        {
            waitingDocumentConfigurator = null;
            animationManager.StopAll();
            dragPinchManager.Disable();
            // Stop tasks
            if (RenderingHandler != null)
            {
                RenderingHandler.Stop();
                RenderingHandler.RemoveMessages(RenderingHandler.MsgRenderTask);
            }

            decodingAsyncTask?.Cancel();

            // Clear caches
            CacheManager.Recycle();

            if (ScrollHandle != null && isScrollHandleInit)
            {
                ScrollHandle.DestroyLayout();
            }

            if (PdfFile != null)
            {
                PdfFile.Dispose();
                PdfFile = null;
            }

            RenderingHandler   = null;
            ScrollHandle       = null;
            isScrollHandleInit = false;
            CurrentXOffset     = CurrentYOffset = 0;
            Zoom       = 1f;
            IsRecycled = true;
            Callbacks  = new Callbacks();
            state      = State.Default;
        }
        /**
         * Move to the given X and Y offsets, but check them ahead of time
         * to be sure not to go outside the the big strip.
         *
         * @param offsetX    The big strip X offset to use as the left border of the screen.
         * @param offsetY    The big strip Y offset to use as the right border of the screen.
         * @param moveHandle whether to move scroll handle or not
         */
        public void MoveTo(float offsetX, float offsetY, bool moveHandle)
        {
            if (IsSwipeVertical)
            {
                // Check X offset
                var scaledPageWidth = ToCurrentScale(PdfFile.MaxPageWidth);
                if (scaledPageWidth < Width)
                {
                    offsetX = Width / 2 - scaledPageWidth / 2;
                }
                else
                {
                    if (offsetX > 0)
                    {
                        offsetX = 0;
                    }
                    else if (offsetX + scaledPageWidth < Width)
                    {
                        offsetX = Width - scaledPageWidth;
                    }
                }

                // Check Y offset
                var contentHeight = PdfFile.GetDocLen(Zoom);
                if (contentHeight < Height)
                {
                    // whole document height visible on screen
                    offsetY = (Height - contentHeight) / 2;
                }
                else
                {
                    if (offsetY > 0)
                    {
                        // top visible
                        offsetY = 0;
                    }
                    else if (offsetY + contentHeight < Height)
                    {
                        // bottom visible
                        offsetY = -contentHeight + Height;
                    }
                }

                if (offsetY < CurrentYOffset)
                {
                    scrollDir = ScrollDir.End;
                }
                else if (offsetY > CurrentYOffset)
                {
                    scrollDir = ScrollDir.Start;
                }
                else
                {
                    scrollDir = ScrollDir.None;
                }
            }
            else
            {
                // Check Y offset
                var scaledPageHeight = ToCurrentScale(PdfFile.MaxPageHeight);
                if (scaledPageHeight < Height)
                {
                    offsetY = Height / 2 - scaledPageHeight / 2;
                }
                else
                {
                    if (offsetY > 0)
                    {
                        offsetY = 0;
                    }
                    else if (offsetY + scaledPageHeight < Height)
                    {
                        offsetY = Height - scaledPageHeight;
                    }
                }

                // Check X offset
                var contentWidth = PdfFile.GetDocLen(Zoom);
                if (contentWidth < Width)
                {
                    // whole document width visible on screen
                    offsetX = (Width - contentWidth) / 2;
                }
                else
                {
                    if (offsetX > 0)
                    {
                        // left visible
                        offsetX = 0;
                    }
                    else if (offsetX + contentWidth < Width)
                    {
                        // right visible
                        offsetX = -contentWidth + Width;
                    }
                }

                if (offsetX < CurrentXOffset)
                {
                    scrollDir = ScrollDir.End;
                }
                else if (offsetX > CurrentXOffset)
                {
                    scrollDir = ScrollDir.Start;
                }
                else
                {
                    scrollDir = ScrollDir.None;
                }
            }

            CurrentXOffset = offsetX;
            CurrentYOffset = offsetY;
            var positionOffset = PositionOffset;

            if (moveHandle && ScrollHandle != null && !DocumentFitsView)
            {
                ScrollHandle.SetScroll(positionOffset);
            }

            Callbacks.CallOnPageScroll(this, new PageScrolledEventArgs(CurrentPage, positionOffset));

            Redraw();
        }