Generate SSD PDF load path from typed Jerboa

ober

79d947a5e040c2cb4551610513a150a6ab021527

diff --git a/templates/ssd-review.ss b/templates/ssd-review.ss
index 5f266b4..f22cff2 100644
--- a/templates/ssd-review.ss
+++ b/templates/ssd-review.ss
@@ -4782,6 +4782,7 @@
                       (android os Environment)
                       (android os ParcelFileDescriptor)
                       (android provider DocumentsContract)
+                      (android provider OpenableColumns)
                       (android provider Settings)
                       (android text Editable)
                       (android text InputType)
@@ -4926,6 +4927,7 @@
                 mainActivityAdvanceLoadGeneration
                 mainActivityRestoreLastPdf mainActivityPersistCurrentPdfState
                 mainActivityRestoredViewportFor
+                mainActivityDisplayName mainActivityLoadPdf
                 mainActivityInitialPickerUri mainActivityOpenPdf
                 mainActivityOpenAnyPdf mainActivityChooseSsdPdfFolder
                 mainActivityRequestDownloadAccess
@@ -5124,6 +5126,8 @@
           (kotlin-member-get path))
         (extern (uriFromFileRaw (file : File)) : Uri
           (kotlin-call Uri fromFile))
+        (extern (uriLastPathSegmentRaw (uri : Uri)) : (Nullable String)
+          (kotlin-member-get lastPathSegment))
         (extern (documentFileIsDirectoryRaw (file : DocumentFile)) : Bool
           (kotlin-member-get isDirectory))
         (extern (documentFileIsFile (file : DocumentFile)) : Bool
@@ -5132,17 +5136,33 @@
           (kotlin-member-call delete))
         (extern (cursorMoveToNextRaw (cursor : Cursor)) : Bool
           (kotlin-member-call moveToNext))
+        (extern (cursorMoveToFirstRaw (cursor : Cursor)) : Bool
+          (kotlin-member-call moveToFirst))
         (extern (cursorCountRaw (cursor : Cursor)) : Int32
           (kotlin-member-get count))
+        (extern (cursorGetColumnIndexRaw
+                  (cursor : Cursor)
+                  (name : String)) : Int32
+          (kotlin-member-call getColumnIndex))
         (extern (cursorGetStringRaw (cursor : Cursor)
                                     (column : Int32)) : (Nullable String)
           (kotlin-member-call getString))
         (extern (cursorGetLongRaw (cursor : Cursor)
                                   (column : Int32)) : Int
           (kotlin-member-call getLong))
+        (extern (cursorCloseRaw (cursor : Cursor)) : Unit
+          (kotlin-member-call close))
         (extern (contentUrisWithAppendedIdRaw (uri : Uri)
                                              (id : Int)) : Uri
           (kotlin-call ContentUris withAppendedId))
+        (extern (contentResolverQueryRaw
+                  (resolver : ContentResolver)
+                  (uri : Uri)
+                  (projection : (Nullable (Array String)))
+                  (selection : (Nullable String))
+                  (selectionArgs : (Nullable (Array String)))
+                  (sortOrder : (Nullable String))) : (Nullable Cursor)
+          (kotlin-member-call query))
         (extern (contentResolverOpenInputStreamRaw
                   (resolver : ContentResolver)
                   (uri : Uri)) : (Nullable InputStream)
@@ -5891,18 +5911,6 @@
                   (activity : MainActivity)
                   (error : Exception)) : JSONObject
           (kotlin-member-call throwableLog))
-        (extern (mainActivityLoadPdfRaw
-                  (activity : MainActivity)
-                  (uri : Uri)
-                  (flags : Int32)) : Unit
-          (kotlin-member-call loadPdf))
-        (extern (mainActivityLoadPdfForRestoreRaw
-                  (activity : MainActivity)
-                  (uri : Uri)
-                  (flags : Int32)
-                  (initialPageIndex : Int32)
-                  (restoring : Bool)) : Unit
-          (kotlin-member-call loadPdf))
         (extern (mainActivityReviewViewSet
                   (activity : MainActivity)
                   (view : SsdReviewView)) : Unit
@@ -8011,6 +8019,8 @@
           (kotlin-value PREF_LAST_VIEWPORT_IMAGE_WIDTH))
         (extern (mainActivityPrefLastViewportImageHeightValue) : String
           (kotlin-value PREF_LAST_VIEWPORT_IMAGE_HEIGHT))
+        (extern (openableColumnsDisplayNameValue) : String
+          (kotlin-value OpenableColumns DISPLAY_NAME))
         (extern (environmentIsExternalStorageManagerRaw) : Bool
           (kotlin-call Environment isExternalStorageManager))
         (extern (environmentDirectoryDownloads) : String
@@ -8556,6 +8566,72 @@
                 (mainActivityGetPreferencesRaw activity (contextModePrivate)))
               key
               value)))
+        (def (uriLastPathSegmentOrDefault
+               (uri : Uri)
+               (fallback : String)) : String
+          (nullableTextOrDefault
+            (uriLastPathSegmentRaw uri)
+            fallback))
+        (def (mainActivityDisplayNameFromCursor
+               (cursor : Cursor)
+               (fallback : String)) : String
+          (let ((index
+                  (cursorGetColumnIndexRaw
+                    cursor
+                    (openableColumnsDisplayNameValue))))
+            (if (displayNameColumnPresent index)
+              (if (displayNameCursorHasFirstRow
+                    (cursorMoveToFirstRaw cursor))
+                (nullableTextOrDefault
+                  (cursorGetStringRaw cursor index)
+                  fallback)
+                fallback)
+              fallback)))
+        (def (mainActivityDisplayName
+               (activity : MainActivity)
+               (uri : Uri)) : String
+          (let ((fallback
+                  (uriLastPathSegmentOrDefault uri "ssd.pdf"))
+                (nameCursor
+                  (contentResolverQueryRaw
+                    (mainActivityContentResolverRaw activity)
+                    uri
+                    (nullable-none (Array String))
+                    (nullable-none String)
+                    (nullable-none (Array String))
+                    (nullable-none String))))
+            (if (cursorPresent nameCursor)
+              (let ((cursor (nullable-get nameCursor)))
+                (try-finally
+                  (mainActivityDisplayNameFromCursor cursor fallback)
+                  (cursorCloseRaw cursor)))
+              fallback)))
+        (def (mainActivityLoadPdf
+               (activity : MainActivity)
+               (uri : Uri)
+               (flags : Int32)
+               (initialPageIndex : Int32)
+               (restoring : Bool)) : Unit
+          (begin
+            (takePersistableUriPermissionIgnoringError
+              (mainActivityContentResolverRaw activity)
+              uri
+              (bitwise-and
+                flags
+                (intentFlagGrantReadUriPermission)))
+            (mainActivitySetPdfIdentity
+              activity
+              uri
+              (mainActivityDisplayName activity uri))
+            (mainActivityStorePreferenceString
+              activity
+              (mainActivityPrefLastPdfUriValue)
+              (uriToStringRaw uri))
+            (mainActivityLoadPdfAsync
+              activity
+              uri
+              initialPageIndex
+              restoring)))
         (def (mainActivityRestoreLastPdf
                (activity : MainActivity)) : Bool
           (let ((preferences
@@ -8576,7 +8652,7 @@
                           (int32 0))))
                   (try
                     (begin
-                      (mainActivityLoadPdfForRestoreRaw
+                      (mainActivityLoadPdf
                         activity
                         (uriParse (nullableTextOrEmpty raw))
                         (intentFlagGrantReadUriPermission)
@@ -8835,7 +8911,12 @@
               (let ((uri (intentDataUriOrEmpty data))
                     (flags (intentFlagsOrZero data)))
                 (if (requestIsOpenPdf requestCode requestOpenPdfCode)
-                  (mainActivityLoadPdfRaw activity uri flags)
+                  (mainActivityLoadPdf
+                    activity
+                    uri
+                    flags
+                    (int32 0)
+                    #f)
                   (if (requestIsOpenSsdPdfTree requestCode requestOpenTreeCode)
                     (mainActivityRememberAndBrowseSsdFolder
                       activity
@@ -9238,10 +9319,12 @@
             builder
             (stringListToTypedArray (ssdPdfChoiceLabels choices))
             (lambda ((unusedDialog : DialogInterface) (which : Int32))
-              (mainActivityLoadPdfRaw
+              (mainActivityLoadPdf
                 activity
                 (ssdPdfChoiceUriAt choices which)
-                readFlag))))
+                readFlag
+                (int32 0)
+                #f))))
         (def (mainActivityShowPdfChoiceDialog
                (activity : MainActivity)
                (choices : (List SsdPdfChoice))
@@ -12245,21 +12328,8 @@
        "            }"
        "            }"
        "    }"
-       ""
-       "    internal fun loadPdf(uri: Uri, flags: Int, initialPageIndex: Int = 0, restoring: Boolean = false) {"
-       "        try {"
-       "            contentResolver.takePersistableUriPermission(uri, flags and Intent.FLAG_GRANT_READ_URI_PERMISSION)"
-       "        } catch (_: Exception) {"
-       "        }"
-       "        mainActivitySetPdfIdentity(this, uri, displayName(uri))"
-       "        getPreferences(MODE_PRIVATE)"
-       "            .edit()"
-       "            .putString(PREF_LAST_PDF_URI, uri.toString())"
-       "            .apply()"
-       "        mainActivityLoadPdfAsync(this, uri, initialPageIndex, restoring)"
-       "    }"
-       ""
-       "    internal fun pdfPageCount(uri: Uri): Int {"
+	       ""
+	       "    internal fun pdfPageCount(uri: Uri): Int {"
        "        openPdfRenderer(uri).use { renderer -> return renderer.pageCount }"
        "    }"
        ""
@@ -12286,19 +12356,6 @@
        "        return PdfRenderer(pfd)"
        "    }"
        ""
-       "    private fun displayName(uri: Uri): String {"
-       "        val nameCursor = contentResolver.query(uri, null, null, null, null)"
-       "        if (cursorPresent(nameCursor)) checkNotNull(nameCursor).use { cursor ->"
-       "            val index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)"
-       "            if (displayNameColumnPresent(index)) {"
-       "                if (displayNameCursorHasFirstRow(cursor.moveToFirst())) {"
-       "                    return cursor.getString(index)"
-       "                }"
-       "            }"
-       "        }"
-       "        return nullableTextOrDefault(uri.lastPathSegment, \"ssd.pdf\")"
-       "    }"
-       ""
        "    private fun installClientCrashLogger() {"
        "        if (clientCrashLoggerInstalled) return"
        "        clientCrashLoggerInstalled = true"