Merge pull request #716 from esensar/feature/bring-back-pdfium

Return `AndroidPdfViewer` based on `PDFium`
This commit is contained in:
Tibor Kaputa
2023-07-06 09:32:20 +02:00
committed by GitHub
4 changed files with 32 additions and 74 deletions

View File

@ -64,8 +64,8 @@ android {
} }
dependencies { dependencies {
implementation 'com.github.SimpleMobileTools:Simple-Commons:30e6321592' implementation 'com.github.SimpleMobileTools:Simple-Commons:84c71fdcc1'
implementation 'com.github.tibbi:PdfViewPager:d2af24208d' implementation 'com.github.tibbi:AndroidPdfViewer:e6a533125b'
implementation 'com.github.Stericson:RootTools:df729dcb13' implementation 'com.github.Stericson:RootTools:df729dcb13'
implementation 'com.github.Stericson:RootShell:1.6' implementation 'com.github.Stericson:RootShell:1.6'
implementation 'com.alexvasilkov:gesture-views:2.5.2' implementation 'com.alexvasilkov:gesture-views:2.5.2'

View File

@ -632,7 +632,7 @@ class MainActivity : SimpleActivity() {
} }
private fun launchAbout() { private fun launchAbout() {
val licenses = LICENSE_GLIDE or LICENSE_PATTERN or LICENSE_REPRINT or LICENSE_GESTURE_VIEWS or LICENSE_PDF_VIEW_PAGER or LICENSE_AUTOFITTEXTVIEW val licenses = LICENSE_GLIDE or LICENSE_PATTERN or LICENSE_REPRINT or LICENSE_GESTURE_VIEWS or LICENSE_PDF_VIEWER or LICENSE_AUTOFITTEXTVIEW
val faqItems = arrayListOf( val faqItems = arrayListOf(
FAQItem(R.string.faq_3_title_commons, R.string.faq_3_text_commons), FAQItem(R.string.faq_3_title_commons, R.string.faq_3_text_commons),

View File

@ -6,12 +6,9 @@ import android.graphics.Color
import android.os.Bundle import android.os.Bundle
import android.print.PrintAttributes import android.print.PrintAttributes
import android.print.PrintManager import android.print.PrintManager
import android.view.View
import android.view.WindowManager import android.view.WindowManager
import androidx.constraintlayout.widget.ConstraintLayout import android.widget.RelativeLayout
import androidx.viewpager2.widget.MarginPageTransformer import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.REAL_FILE_PATH import com.simplemobiletools.commons.helpers.REAL_FILE_PATH
import com.simplemobiletools.commons.helpers.isPiePlus import com.simplemobiletools.commons.helpers.isPiePlus
@ -19,8 +16,6 @@ import com.simplemobiletools.filemanager.pro.R
import com.simplemobiletools.filemanager.pro.extensions.hideSystemUI import com.simplemobiletools.filemanager.pro.extensions.hideSystemUI
import com.simplemobiletools.filemanager.pro.extensions.showSystemUI import com.simplemobiletools.filemanager.pro.extensions.showSystemUI
import com.simplemobiletools.filemanager.pro.helpers.PdfDocumentAdapter import com.simplemobiletools.filemanager.pro.helpers.PdfDocumentAdapter
import es.voghdev.pdfviewpager.library.adapter.PDFPagerAdapter
import es.voghdev.pdfviewpager.library.adapter.PdfErrorHandler
import kotlinx.android.synthetic.main.activity_pdf_viewer.* import kotlinx.android.synthetic.main.activity_pdf_viewer.*
class PDFViewerActivity : SimpleActivity() { class PDFViewerActivity : SimpleActivity() {
@ -59,6 +54,7 @@ class PDFViewerActivity : SimpleActivity() {
} }
private fun setupMenu() { private fun setupMenu() {
(pdf_viewer_appbar.layoutParams as RelativeLayout.LayoutParams).topMargin = statusBarHeight
pdf_viewer_toolbar.menu.apply { pdf_viewer_toolbar.menu.apply {
findItem(R.id.menu_print).isVisible = realFilePath.isNotEmpty() findItem(R.id.menu_print).isVisible = realFilePath.isNotEmpty()
findItem(R.id.menu_print).setOnMenuItemClickListener { findItem(R.id.menu_print).setOnMenuItemClickListener {
@ -71,29 +67,20 @@ class PDFViewerActivity : SimpleActivity() {
finish() finish()
} }
setupViewOffsets()
val primaryColor = getProperPrimaryColor()
page_counter.background?.applyColorFilter(primaryColor)
page_counter.setTextColor(primaryColor.getContrastColor())
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
setupViewOffsets()
}
private fun setupViewOffsets() {
val pageCounterMargin = resources.getDimension(R.dimen.normal_margin).toInt()
(pdf_viewer_appbar.layoutParams as ConstraintLayout.LayoutParams).topMargin = statusBarHeight
if (!portrait && navigationBarOnSide && navigationBarWidth > 0) { if (!portrait && navigationBarOnSide && navigationBarWidth > 0) {
pdf_viewer_appbar.setPadding(0, 0, navigationBarWidth, 0) pdf_viewer_appbar.setPadding(0, 0, navigationBarWidth, 0)
} else { } else {
pdf_viewer_appbar.setPadding(0, 0, 0, 0) pdf_viewer_appbar.setPadding(0, 0, 0, 0)
} }
}
(page_counter.layoutParams as ConstraintLayout.LayoutParams).apply { override fun onConfigurationChanged(newConfig: Configuration) {
rightMargin = navigationBarWidth + pageCounterMargin super.onConfigurationChanged(newConfig)
bottomMargin = navigationBarHeight + pageCounterMargin (pdf_viewer_appbar.layoutParams as RelativeLayout.LayoutParams).topMargin = statusBarHeight
if (!portrait && navigationBarOnSide && navigationBarWidth > 0) {
pdf_viewer_appbar.setPadding(0, 0, navigationBarWidth, 0)
} else {
pdf_viewer_appbar.setPadding(0, 0, 0, 0)
} }
} }
@ -104,30 +91,17 @@ class PDFViewerActivity : SimpleActivity() {
return return
} }
val clickListener = View.OnClickListener { val primaryColor = getProperPrimaryColor()
toggleFullScreen() pdf_viewer.setBackgroundColor(getProperBackgroundColor())
} pdf_viewer.fromUri(uri)
.scrollHandle(DefaultScrollHandle(this, primaryColor.getContrastColor(), primaryColor))
val errorHandler = PdfErrorHandler { throwable -> showErrorToast(throwable.toString()) } .spacing(15)
.onTap { toggleFullScreen() }
pdf_viewer.setPageTransformer(MarginPageTransformer(resources.getDimension(R.dimen.activity_margin).toInt())) .onError {
pdf_viewer.orientation = ViewPager2.ORIENTATION_VERTICAL showErrorToast(it.localizedMessage?.toString() ?: getString(R.string.unknown_error_occurred))
try {
pdf_viewer.adapter = PDFPagerAdapter(this, clickListener, errorHandler, uri.toString(), getProperBackgroundColor())
} catch (e: Exception) {
showErrorToast(e)
finish() finish()
return
} }
.load()
pdf_viewer.registerOnPageChangeCallback(object : OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
updatePageCounter(position)
}
})
updatePageCounter(0)
page_counter.beVisible()
showSystemUI(true) showSystemUI(true)
@ -137,10 +111,6 @@ class PDFViewerActivity : SimpleActivity() {
} }
} }
private fun updatePageCounter(position: Int) {
page_counter.text = "${position + 1} / ${pdf_viewer.adapter?.itemCount}"
}
private fun printText() { private fun printText() {
val adapter = PdfDocumentAdapter(this, realFilePath) val adapter = PdfDocumentAdapter(this, realFilePath)
@ -160,7 +130,6 @@ class PDFViewerActivity : SimpleActivity() {
showSystemUI(true) showSystemUI(true)
} }
page_counter.animate().alpha(newAlpha).start()
top_shadow.animate().alpha(newAlpha).start() top_shadow.animate().alpha(newAlpha).start()
pdf_viewer_appbar.animate().alpha(newAlpha).withStartAction { pdf_viewer_appbar.animate().alpha(newAlpha).withStartAction {
if (newAlpha == 1f) { if (newAlpha == 1f) {

View File

@ -1,13 +1,12 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pdf_viewer_wrapper" android:id="@+id/pdf_viewer_wrapper"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"> app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.viewpager2.widget.ViewPager2 <com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdf_viewer" android:id="@+id/pdf_viewer"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />
@ -16,28 +15,18 @@
android:id="@+id/top_shadow" android:id="@+id/top_shadow"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/top_shadow_height" android:layout_height="@dimen/top_shadow_height"
android:layout_alignParentTop="true"
android:background="@drawable/gradient_background_flipped" android:background="@drawable/gradient_background_flipped"
android:contentDescription="@null" android:contentDescription="@null" />
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/page_counter"
style="@style/ColoredButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:text="1/3" />
<com.google.android.material.appbar.AppBarLayout <com.google.android.material.appbar.AppBarLayout
android:id="@+id/pdf_viewer_appbar" android:id="@+id/pdf_viewer_appbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@android:color/transparent" android:background="@android:color/transparent"
android:elevation="0dp" android:elevation="0dp"
app:elevation="0dp" app:elevation="0dp">
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.appbar.MaterialToolbar <com.google.android.material.appbar.MaterialToolbar
android:id="@+id/pdf_viewer_toolbar" android:id="@+id/pdf_viewer_toolbar"
@ -47,4 +36,4 @@
app:navigationIcon="@drawable/ic_arrow_left_vector" /> app:navigationIcon="@drawable/ic_arrow_left_vector" />
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </RelativeLayout>