|
1 | 1 | package org.grapheneos.pdfviewer.viewmodel
|
2 | 2 |
|
3 | 3 | import android.content.Context
|
4 |
| -import android.database.Cursor |
5 |
| -import android.graphics.Typeface |
6 | 4 | import android.net.Uri
|
7 |
| -import android.provider.OpenableColumns |
8 |
| -import android.text.SpannableStringBuilder |
9 |
| -import android.text.Spanned |
10 |
| -import android.text.style.StyleSpan |
11 | 5 | import android.util.Log
|
12 | 6 | import androidx.annotation.NonNull
|
13 | 7 | import androidx.lifecycle.*
|
14 | 8 | import kotlinx.coroutines.Dispatchers
|
15 | 9 | import kotlinx.coroutines.launch
|
16 |
| -import org.grapheneos.pdfviewer.R |
17 |
| -import org.grapheneos.pdfviewer.Utils |
18 |
| -import org.json.JSONException |
19 |
| -import org.json.JSONObject |
20 |
| -import java.text.ParseException |
21 | 10 |
|
22 | 11 | private const val TAG = "PdfViewerViewModel"
|
23 | 12 |
|
24 |
| -private const val MISSING_STRING = "-" |
25 |
| - |
26 | 13 | private const val STATE_URI = "uri"
|
27 | 14 | private const val STATE_PAGE = "page"
|
28 | 15 | private const val STATE_ZOOM_RATIO = "zoomRatio"
|
@@ -87,75 +74,4 @@ class PdfViewerViewModel(private val state: SavedStateHandle) : ViewModel() {
|
87 | 74 | }
|
88 | 75 | }
|
89 | 76 | }
|
90 |
| - |
91 |
| - companion object DocumentPropertiesParser { |
92 |
| - fun parsePropertiesString( |
93 |
| - propertiesString: String, |
94 |
| - context: Context, |
95 |
| - numPages: Int, |
96 |
| - uri: Uri? |
97 |
| - ): ArrayList<CharSequence>? { |
98 |
| - val properties = ArrayList<CharSequence>() |
99 |
| - val names = context.resources.getStringArray(R.array.property_names); |
100 |
| - val cursor: Cursor? = context.contentResolver.query(uri!!, null, null, null, null); |
101 |
| - cursor?.let { |
102 |
| - it.moveToFirst(); |
103 |
| - |
104 |
| - val indexOfName = it.getColumnIndex(OpenableColumns.DISPLAY_NAME) |
105 |
| - if (indexOfName >= 0) { |
106 |
| - properties.add( |
107 |
| - getProperty(null, names[0], it.getString(indexOfName), context) |
108 |
| - ) |
109 |
| - } |
110 |
| - |
111 |
| - val indexOfSize = it.getColumnIndex(OpenableColumns.SIZE) |
112 |
| - if (indexOfSize >= 0) { |
113 |
| - val fileSize = it.getString(indexOfSize).toLong() |
114 |
| - properties.add( |
115 |
| - getProperty(null, names[1], Utils.parseFileSize(fileSize), context) |
116 |
| - ) |
117 |
| - } |
118 |
| - } |
119 |
| - cursor?.close() |
120 |
| - |
121 |
| - val specNames = arrayOf("Title", "Author", "Subject", "Keywords", "CreationDate", |
122 |
| - "ModDate", "Producer", "Creator", "PDFFormatVersion", numPages.toString()) |
123 |
| - try { |
124 |
| - val json = JSONObject(propertiesString) |
125 |
| - for (i in 2 until names.size) { |
126 |
| - properties.add(getProperty(json, names[i], specNames[i - 2], context)) |
127 |
| - } |
128 |
| - |
129 |
| - Log.d(TAG, "Successfully parsed properties") |
130 |
| - return properties |
131 |
| - } catch (e: JSONException) { |
132 |
| - Log.e(TAG, "Failed to parse properties: ${e.message}", e) |
133 |
| - } |
134 |
| - return null |
135 |
| - } |
136 |
| - |
137 |
| - private fun getProperty( |
138 |
| - json: JSONObject?, |
139 |
| - name: String, |
140 |
| - specName: String, |
141 |
| - context: Context |
142 |
| - ): CharSequence { |
143 |
| - val property = SpannableStringBuilder(name).append(":\n") |
144 |
| - val value = json?.optString(specName, MISSING_STRING) ?: specName |
145 |
| - |
146 |
| - val valueToAppend = if (specName.endsWith("Date") && value != MISSING_STRING) { |
147 |
| - try { |
148 |
| - Utils.parseDate(value) |
149 |
| - } catch (e: ParseException) { |
150 |
| - Log.w(TAG, "${e.message} for $value at offset: ${e.errorOffset}") |
151 |
| - context.getString(R.string.document_properties_invalid_date) |
152 |
| - } |
153 |
| - } else { |
154 |
| - value |
155 |
| - } |
156 |
| - property.append(valueToAppend) |
157 |
| - property.setSpan(StyleSpan(Typeface.BOLD), 0, name.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) |
158 |
| - return property |
159 |
| - } |
160 |
| - } |
161 | 77 | }
|
0 commit comments