emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 9500fbe231e 1/2: Facilitate opening content:// files without a li


From: Po Lu
Subject: master 9500fbe231e 1/2: Facilitate opening content:// files without a linked file name
Date: Tue, 12 Sep 2023 21:30:32 -0400 (EDT)

branch: master
commit 9500fbe231ec32c66e619b7fc8b221d163d5acca
Author: Po Lu <luangruo@yahoo.com>
Commit: Po Lu <luangruo@yahoo.com>

    Facilitate opening content:// files without a linked file name
    
    * java/org/gnu/emacs/EmacsOpenActivity.java
    (checkReadableOrCopy): If FILE is NULL, return a matching
    content URI if possible, else generate a file name with the URI
    as a reference.
    (onCreate): Catch SecurityException around calls to
    openFileDescriptor.
---
 java/org/gnu/emacs/EmacsOpenActivity.java | 40 ++++++++++++++++++++++++-------
 1 file changed, 32 insertions(+), 8 deletions(-)

diff --git a/java/org/gnu/emacs/EmacsOpenActivity.java 
b/java/org/gnu/emacs/EmacsOpenActivity.java
index ca6d99e20b7..00503b6cbcc 100644
--- a/java/org/gnu/emacs/EmacsOpenActivity.java
+++ b/java/org/gnu/emacs/EmacsOpenActivity.java
@@ -213,12 +213,13 @@ public final class EmacsOpenActivity extends Activity
     dialog.show ();
   }
 
-  /* Check that the specified FILE is readable.  If Android 4.4 or
-     later is being used, return URI formatted into a `/content/' file
-     name.
+  /* Check that the specified FILE is non-NULL and readable.
 
      If it is not, then copy the file in FD to a location in the
-     system cache directory and return the name of that file.  */
+     system cache directory and return the name of that file.
+
+     Alternatively, return URI formatted into a `/content/' file name
+     if the system runs Android 4.4 or later.  */
 
   private String
   checkReadableOrCopy (String file, ParcelFileDescriptor fd,
@@ -232,10 +233,19 @@ public final class EmacsOpenActivity extends Activity
     int read;
     String content;
 
-    inFile = new File (file);
+    if (file != null)
+      {
+       inFile = new File (file);
+
+       if (inFile.canRead ())
+         return file;
 
-    if (inFile.canRead ())
-      return file;
+       content = inFile.getName ();
+      }
+    else
+      /* content is the name of this content file if the next branch
+        is not taken.  */
+      content = uri.getLastPathSegment ();
 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
       {
@@ -243,8 +253,14 @@ public final class EmacsOpenActivity extends Activity
        return content;
       }
 
+    /* The file is unnamed if content is NULL.  Generate a unique
+       name with the current time as a reference.  */
+
+    if (content == null)
+      content = "content." + System.currentTimeMillis () / 1000;
+
     /* inFile is now the file being written to.  */
-    inFile = new File (getCacheDir (), inFile.getName ());
+    inFile = new File (getCacheDir (), content);
     buffer = new byte[4098];
 
     /* Initialize both streams to NULL.  */
@@ -464,6 +480,14 @@ public final class EmacsOpenActivity extends Activity
                  {
                    /* Do nothing.  */
                  }
+               catch (SecurityException exception)
+                 {
+                   /* This means Emacs lacks the rights to open this
+                      file.  Display the error message and exit.  */
+                   displayFailureDialog ("Error openining file",
+                                         exception.toString ());
+                   return;
+                 }
 
                if (fd != null)
                  {



reply via email to

[Prev in Thread] Current Thread [Next in Thread]