Add fullscreen toggle in PDF Viewer

This commit is contained in:
Naveen
2022-06-09 11:57:29 +05:30
parent a4b581fecd
commit 6798a1b3ba
7 changed files with 76 additions and 60 deletions

View File

@ -1,38 +1,36 @@
package com.simplemobiletools.filemanager.pro.activities package com.simplemobiletools.filemanager.pro.activities
import android.content.Context import android.content.Context
import android.content.res.Configuration.UI_MODE_NIGHT_NO import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.print.PrintAttributes import android.print.PrintAttributes
import android.print.PrintManager import android.print.PrintManager
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.WindowInsetsController
import android.view.WindowManager import android.view.WindowManager
import android.widget.RelativeLayout
import androidx.core.view.updateLayoutParams
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle
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
import com.simplemobiletools.commons.helpers.isSPlus import com.simplemobiletools.commons.helpers.isRPlus
import com.simplemobiletools.filemanager.pro.R import com.simplemobiletools.filemanager.pro.R
import com.simplemobiletools.filemanager.pro.extensions.config
import com.simplemobiletools.filemanager.pro.extensions.getUiMode
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.HIDE_SYSTEM_UI_DELAY
import com.simplemobiletools.filemanager.pro.helpers.PdfDocumentAdapter import com.simplemobiletools.filemanager.pro.helpers.PdfDocumentAdapter
import kotlinx.android.synthetic.main.activity_pdf_viewer.* import kotlinx.android.synthetic.main.activity_pdf_viewer.*
class PDFViewerActivity : SimpleActivity() { class PDFViewerActivity : SimpleActivity() {
private var realFilePath = "" private var realFilePath = ""
private var isFullScreen = false private var isFullScreen = false
private var pdfViewerHeight = -1
private var positionOffset = 0f
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
useDynamicTheme = false
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_pdf_viewer) setContentView(R.layout.activity_pdf_viewer)
@ -40,12 +38,25 @@ class PDFViewerActivity : SimpleActivity() {
return return
} }
window.decorView.setBackgroundColor(getProperBackgroundColor())
top_shadow.layoutParams.height = statusBarHeight + actionBarHeight
checkNotchSupport()
if (intent.extras?.containsKey(REAL_FILE_PATH) == true) { if (intent.extras?.containsKey(REAL_FILE_PATH) == true) {
realFilePath = intent.extras?.get(REAL_FILE_PATH)?.toString() ?: "" realFilePath = intent.extras?.get(REAL_FILE_PATH)?.toString() ?: ""
} }
checkIntent() checkIntent()
setupNotch() if (isRPlus()) {
window.insetsController?.setSystemBarsAppearance(0, WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS)
}
}
override fun onResume() {
super.onResume()
supportActionBar?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
window.statusBarColor = Color.TRANSPARENT
setTranslucentNavigation()
} }
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
@ -54,7 +65,7 @@ class PDFViewerActivity : SimpleActivity() {
findItem(R.id.menu_print).isVisible = realFilePath.isNotEmpty() findItem(R.id.menu_print).isVisible = realFilePath.isNotEmpty()
} }
updateMenuItemColors(menu) updateMenuItemColors(menu, forceWhiteIcons = true)
return true return true
} }
@ -73,11 +84,6 @@ class PDFViewerActivity : SimpleActivity() {
return return
} }
val filename = getFilenameFromUri(uri)
if (filename.isNotEmpty()) {
title = filename
}
val primaryColor = getProperPrimaryColor() val primaryColor = getProperPrimaryColor()
pdf_viewer.setBackgroundColor(getProperBackgroundColor()) pdf_viewer.setBackgroundColor(getProperBackgroundColor())
pdf_viewer.fromUri(uri) pdf_viewer.fromUri(uri)
@ -85,6 +91,19 @@ class PDFViewerActivity : SimpleActivity() {
.spacing(15) .spacing(15)
.onTap { toggleFullScreen() } .onTap { toggleFullScreen() }
.load() .load()
showSystemUI(true)
pdf_viewer_wrapper.onGlobalLayout {
Handler(mainLooper).postDelayed({
toggleFullScreen()
}, HIDE_SYSTEM_UI_DELAY)
val filename = getFilenameFromUri(uri)
if (filename.isNotEmpty()) {
supportActionBar?.title = filename
}
}
} }
private fun printText() { private fun printText() {
@ -96,43 +115,23 @@ class PDFViewerActivity : SimpleActivity() {
} }
private fun toggleFullScreen(): Boolean { private fun toggleFullScreen(): Boolean {
if (isFullScreen) exitFullScreen() else enterFullScreen()
isFullScreen = !isFullScreen isFullScreen = !isFullScreen
val newAlpha: Float
if (isFullScreen) {
newAlpha = 0f
hideSystemUI(true)
} else {
newAlpha = 1f
showSystemUI(true)
}
top_shadow.animate().alpha(newAlpha).start()
// return false to also toggle scroll handle
return true return true
} }
private fun enterFullScreen() { private fun checkNotchSupport() {
if (pdfViewerHeight == -1) {
pdfViewerHeight = pdf_viewer.height
}
positionOffset = pdf_viewer.positionOffset
hideSystemUI(true)
pdf_viewer.updateLayoutParams<RelativeLayout.LayoutParams> {
// hack to workaround pdf viewer height glitch
this.height = pdf_viewer_wrapper.height + statusBarHeight + actionBarHeight
}
}
private fun exitFullScreen() {
positionOffset = pdf_viewer.positionOffset
showSystemUI(true)
pdf_viewer.updateLayoutParams<RelativeLayout.LayoutParams> {
this.height = pdfViewerHeight
}
pdf_viewer.post { pdf_viewer.positionOffset = positionOffset }
@Suppress("DEPRECATION")
// use light status bar on material you
if (isSPlus() && config.isUsingSystemTheme && getUiMode() == UI_MODE_NIGHT_NO) {
val flags = window.decorView.systemUiVisibility
window.decorView.systemUiVisibility = flags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}
}
private fun setupNotch() {
if (isPiePlus()) { if (isPiePlus()) {
window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)

View File

@ -80,21 +80,22 @@ fun BaseSimpleActivity.toggleItemVisibility(oldPath: String, hide: Boolean, call
} }
} }
@Suppress("DEPRECATION")
fun AppCompatActivity.showSystemUI(toggleActionBarVisibility: Boolean) { fun AppCompatActivity.showSystemUI(toggleActionBarVisibility: Boolean) {
if (toggleActionBarVisibility) { if (toggleActionBarVisibility) {
supportActionBar?.show() supportActionBar?.show()
} }
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
} }
@Suppress("DEPRECATION")
fun AppCompatActivity.hideSystemUI(toggleActionBarVisibility: Boolean) { fun AppCompatActivity.hideSystemUI(toggleActionBarVisibility: Boolean) {
if (toggleActionBarVisibility) { if (toggleActionBarVisibility) {
supportActionBar?.hide() supportActionBar?.hide()
} }
window.decorView.systemUiVisibility = window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or

View File

@ -6,6 +6,7 @@ import com.simplemobiletools.commons.helpers.TAB_STORAGE_ANALYSIS
const val PATH = "path" const val PATH = "path"
const val MAX_COLUMN_COUNT = 20 const val MAX_COLUMN_COUNT = 20
const val HIDE_SYSTEM_UI_DELAY = 800L
// shared preferences // shared preferences
const val SHOW_HIDDEN = "show_hidden" const val SHOW_HIDDEN = "show_hidden"

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="270"
android:endColor="@android:color/transparent"
android:startColor="@color/gradient_grey_start"/>
</shape>

View File

@ -9,4 +9,11 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />
<ImageView
android:id="@+id/top_shadow"
android:layout_width="match_parent"
android:layout_height="@dimen/default_status_action_height"
android:background="@drawable/gradient_background_flipped"
android:contentDescription="@null" />
</RelativeLayout> </RelativeLayout>

View File

@ -1,4 +1,5 @@
<resources> <resources>
<dimen name="grid_view_icon_size">50dp</dimen> <dimen name="grid_view_icon_size">50dp</dimen>
<dimen name="storage_free_space_text_size">46sp</dimen> <dimen name="storage_free_space_text_size">46sp</dimen>
<dimen name="default_status_action_height">86dp</dimen>
</resources> </resources>

View File

@ -1,5 +1,5 @@
<resources> <resources>
<style name="AppTheme" parent="AppTheme.Base"/> <style name="AppTheme" parent="AppTheme.Base" />
</resources> </resources>