Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
440 changes: 180 additions & 260 deletions app/src/main/java/app/grapheneos/pdfviewer/PdfViewer.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,29 +1,53 @@
package app.grapheneos.pdfviewer.fragment

import android.app.Dialog
import android.graphics.Typeface
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.StyleSpan
import android.widget.ArrayAdapter
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.activityViewModels
import app.grapheneos.pdfviewer.R
import app.grapheneos.pdfviewer.properties.DocumentProperty
import app.grapheneos.pdfviewer.viewModel.PdfViewModel
import com.google.android.material.dialog.MaterialAlertDialogBuilder

class DocumentPropertiesFragment : DialogFragment() {

// TODO replace with nav args once the `PdfViewer` activity is converted to kotlin
private val mDocumentProperties: List<String> by lazy {
requireArguments().getStringArrayList(KEY_DOCUMENT_PROPERTIES)?.toList() ?: emptyList()
private val viewModel by activityViewModels<PdfViewModel>()

private fun formatProperties(properties: Map<DocumentProperty, String>): List<CharSequence> {
return properties.map { (property, value) ->
val name = getString(property.nameResource)
SpannableStringBuilder()
.append(name)
.append(":\n")
.append(value)
.apply {
setSpan(
StyleSpan(Typeface.BOLD),
0,
name.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
}
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val properties = viewModel.documentProperties.value

return MaterialAlertDialogBuilder(requireActivity())
.setPositiveButton(android.R.string.ok, null).apply {
if (mDocumentProperties.isNotEmpty()) {
if (!properties.isNullOrEmpty()) {
setTitle(getString(R.string.action_view_document_properties))
setAdapter(
ArrayAdapter(
requireActivity(),
android.R.layout.simple_list_item_1,
mDocumentProperties
formatProperties(properties)
), null
)
} else {
Expand All @@ -36,18 +60,8 @@ class DocumentPropertiesFragment : DialogFragment() {
companion object {

const val TAG = "DocumentPropertiesFragment"
private const val KEY_DOCUMENT_PROPERTIES = "document_properties"

@JvmStatic
fun newInstance(metaData: List<CharSequence>): DocumentPropertiesFragment {
val fragment = DocumentPropertiesFragment()
val args = Bundle()
args.putCharSequenceArrayList(
KEY_DOCUMENT_PROPERTIES,
metaData as ArrayList<CharSequence>
)
fragment.arguments = args
return fragment
}
fun newInstance() = DocumentPropertiesFragment()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ class JumpToPageFragment : DialogFragment() {
private const val STATE_PICKER_MAX = "picker_max"
}

private val mPicker: NumberPicker by lazy { NumberPicker(requireActivity()) }
private val picker: NumberPicker by lazy { NumberPicker(requireActivity()) }

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

val viewerActivity: PdfViewer = (requireActivity() as PdfViewer)

if (savedInstanceState != null) {
mPicker.minValue = savedInstanceState.getInt(STATE_PICKER_MIN)
mPicker.maxValue = savedInstanceState.getInt(STATE_PICKER_MAX)
mPicker.value = savedInstanceState.getInt(STATE_PICKER_CUR)
picker.minValue = savedInstanceState.getInt(STATE_PICKER_MIN)
picker.maxValue = savedInstanceState.getInt(STATE_PICKER_MAX)
picker.value = savedInstanceState.getInt(STATE_PICKER_CUR)
} else {
mPicker.minValue = 1
mPicker.maxValue = viewerActivity.mNumPages
mPicker.value = viewerActivity.mPage
picker.minValue = 1
picker.maxValue = viewerActivity.viewModel.numPages
picker.value = viewerActivity.viewModel.page
}
val layout = FrameLayout(requireActivity())
layout.addView(
mPicker, FrameLayout.LayoutParams(
picker, FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER
Expand All @@ -45,16 +45,16 @@ class JumpToPageFragment : DialogFragment() {
return MaterialAlertDialogBuilder(requireActivity())
.setView(layout)
.setPositiveButton(android.R.string.ok) { _: DialogInterface?, _: Int ->
mPicker.clearFocus()
viewerActivity.onJumpToPageInDocument(mPicker.value)
picker.clearFocus()
viewerActivity.onJumpToPageInDocument(picker.value)
}
.setNegativeButton(android.R.string.cancel, null)
.create()
}

override fun onSaveInstanceState(outState: Bundle) {
outState.putInt(STATE_PICKER_MIN, mPicker.minValue)
outState.putInt(STATE_PICKER_MAX, mPicker.maxValue)
outState.putInt(STATE_PICKER_CUR, mPicker.value)
outState.putInt(STATE_PICKER_MIN, picker.minValue)
outState.putInt(STATE_PICKER_MAX, picker.maxValue)
outState.putInt(STATE_PICKER_CUR, picker.value)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,46 +1,26 @@
package app.grapheneos.pdfviewer.loader
package app.grapheneos.pdfviewer.properties

import android.content.Context
import android.graphics.Typeface
import android.net.Uri
import android.provider.OpenableColumns
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.format.Formatter
import android.text.style.StyleSpan
import android.util.Log
import androidx.core.database.getLongOrNull
import app.grapheneos.pdfviewer.R
import org.json.JSONException

class DocumentPropertiesLoader(
class DocumentPropertiesRetriever(
private val context: Context,
private val properties: String,
private val numPages: Int,
private val mUri: Uri
private val uri: Uri
) {

fun loadAsList(): List<CharSequence> {
return load().map { item ->
val name = context.getString(item.key.nameResource)
val value = item.value

SpannableStringBuilder()
.append(name)
.append(":\n")
.append(value)
.apply {
setSpan(
StyleSpan(Typeface.BOLD),
0,
name.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
}
companion object {
const val TAG = "DocumentPropertiesRetriever"
}

private fun load(): Map<DocumentProperty, String> {
fun retrieve(): Map<DocumentProperty, String> {
val result = mutableMapOf<DocumentProperty, String>()
result.addFileProperties()
result.addPageSizeProperty()
Expand All @@ -67,16 +47,13 @@ class DocumentPropertiesLoader(
context.getString(R.string.document_properties_invalid_date),
parseExceptionListener = { parseException, value ->
Log.w(
DocumentPropertiesAsyncTaskLoader.TAG,
TAG,
"${parseException.message} for $value at offset: ${parseException.errorOffset}"
)
}
).convert()
} catch (e: JSONException) {
Log.w(
DocumentPropertiesAsyncTaskLoader.TAG,
"invalid properties"
)
} catch (_: JSONException) {
Log.w(TAG, "invalid properties")
emptyMap()
}
}
Expand All @@ -89,7 +66,7 @@ class DocumentPropertiesLoader(
)

context.contentResolver.query(
mUri,
uri,
proj,
null,
null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app.grapheneos.pdfviewer.loader
package app.grapheneos.pdfviewer.properties

import androidx.annotation.StringRes
import app.grapheneos.pdfviewer.R
Expand All @@ -17,7 +17,7 @@ const val DEFAULT_VALUE = "-"

enum class DocumentProperty(
val key: String = "",
@StringRes val nameResource: Int,
@param:StringRes val nameResource: Int,
val isDate: Boolean = false
) {
FileName(key = "", nameResource = R.string.file_name),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app.grapheneos.pdfviewer.loader
package app.grapheneos.pdfviewer.properties

import app.grapheneos.pdfviewer.Utils
import org.json.JSONException
Expand Down
Loading