]> git.eshelyaron.com Git - emacs.git/commitdiff
Simplify handling of command-line arguments on Android
authorPo Lu <luangruo@yahoo.com>
Sat, 4 May 2024 08:06:00 +0000 (16:06 +0800)
committerEshel Yaron <me@eshelyaron.com>
Mon, 6 May 2024 16:38:23 +0000 (18:38 +0200)
* java/org/gnu/emacs/EmacsActivity.java
(EXTRA_STARTUP_ARGUMENTS): New constant.
(onCreate): Read a string array, not a string extra from the
intent with this key.

* java/org/gnu/emacs/EmacsOpenActivity.java (EmacsOpenActivity)
<fileToOpen>: Delete field.
(onCreate): Provide file name as a command line argument when
starting the Emacs service.

* java/org/gnu/emacs/EmacsPreferencesActivity.java (startEmacsQ)
(startEmacsDebugInit): In like manner, replace ad-hoc
command-line argument extra with a proper array.

* java/org/gnu/emacs/EmacsService.java (EmacsService): Rename
extraStartupArgument to extraStartupArguments, and change its
type to a string array.
(onCreate): Adjust to match.

* java/org/gnu/emacs/EmacsThread.java (EmacsThread)
<extraStartupArguments>: Ditto.
<fileToOpen>: Delete field.
(run): Adjust correspondingly.

(cherry picked from commit 41dd78cd362a80f1becc006a37f163119b93df10)

java/org/gnu/emacs/EmacsActivity.java
java/org/gnu/emacs/EmacsOpenActivity.java
java/org/gnu/emacs/EmacsPreferencesActivity.java
java/org/gnu/emacs/EmacsService.java
java/org/gnu/emacs/EmacsThread.java

index 90be9a385cfe2c7a4b3ce092bbca71f243ebfcdc..118c3375ad57922029b9bd544aa8322d9dd675ed 100644 (file)
@@ -55,6 +55,9 @@ public class EmacsActivity extends Activity
 {
   public static final String TAG = "EmacsActivity";
 
+  /* Key of intent value providing extra startup argument.  */
+  public static final String EXTRA_STARTUP_ARGUMENTS;
+
   /* ID for URIs from a granted document tree.  */
   public static final int ACCEPT_DOCUMENT_TREE = 1;
 
@@ -88,6 +91,7 @@ public class EmacsActivity extends Activity
   static
   {
     focusedActivities = new ArrayList<EmacsActivity> ();
+    EXTRA_STARTUP_ARGUMENTS = "org.gnu.emacs.STARTUP_ARGUMENTS";
   };
 
   public static void
@@ -242,8 +246,8 @@ public class EmacsActivity extends Activity
     /* See if Emacs should be started with any extra arguments, such
        as `--quick'.  */
     intent = getIntent ();
-    EmacsService.extraStartupArgument
-      = intent.getStringExtra ("org.gnu.emacs.STARTUP_ARGUMENT");
+    EmacsService.extraStartupArguments
+      = intent.getStringArrayExtra (EXTRA_STARTUP_ARGUMENTS);
 
     matchParent = FrameLayout.LayoutParams.MATCH_PARENT;
     params
index cdc68aea2bf6088b84af035f46246942251a8045..28e1e2618214d9a6ceefc3ac39ab0872df89fc32 100644 (file)
@@ -70,11 +70,6 @@ public final class EmacsOpenActivity extends Activity
 {
   private static final String TAG = "EmacsOpenActivity";
 
-  /* The name of any file that should be opened as EmacsThread starts
-     Emacs.  This is never cleared, even if EmacsOpenActivity is
-     started a second time, as EmacsThread only starts once.  */
-  public static String fileToOpen;
-
   /* Any currently focused EmacsOpenActivity.  Used to show pop ups
      while the activity is active and Emacs doesn't have permission to
      display over other programs.  */
@@ -697,9 +692,10 @@ public final class EmacsOpenActivity extends Activity
 
        if (EmacsService.SERVICE == null)
          {
-           fileToOpen = fileName;
            intent = new Intent (EmacsOpenActivity.this,
                                 EmacsActivity.class);
+           intent.putExtra (EmacsActivity.EXTRA_STARTUP_ARGUMENTS,
+                            new String [] { fileName, });
            finish ();
            startActivity (intent);
            return;
index 766e2e11d464b837dea8afc63cd5c2abd6ef4ea7..a3edd6388b4916346fe056bb4fd093cc230c8674 100644 (file)
@@ -57,7 +57,8 @@ public class EmacsPreferencesActivity extends PreferenceActivity
     intent = new Intent (this, EmacsActivity.class);
     intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK
                     | Intent.FLAG_ACTIVITY_CLEAR_TASK);
-    intent.putExtra ("org.gnu.emacs.STARTUP_ARGUMENT", "--quick");
+    intent.putExtra (EmacsActivity.EXTRA_STARTUP_ARGUMENTS,
+                    new String[] {"--quick", });
     startActivity (intent);
     System.exit (0);
   }
@@ -74,7 +75,8 @@ public class EmacsPreferencesActivity extends PreferenceActivity
     intent = new Intent (this, EmacsActivity.class);
     intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK
                     | Intent.FLAG_ACTIVITY_CLEAR_TASK);
-    intent.putExtra ("org.gnu.emacs.STARTUP_ARGUMENT", "--debug-init");
+    intent.putExtra (EmacsActivity.EXTRA_STARTUP_ARGUMENTS,
+                    new String[] {"--debug-init", });
     startActivity (intent);
     System.exit (0);
   }
index ced9f59996074f687d0f31b2f7a4fe204b0281c5..5548748ddfa528c58a9f54b6b76e690d8303a961 100644 (file)
@@ -25,6 +25,7 @@ import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
 
@@ -102,9 +103,9 @@ public final class EmacsService extends Service
   /* The started Emacs service object.  */
   public static EmacsService SERVICE;
 
-  /* If non-NULL, an extra argument to pass to
+  /* If non-NULL, an array of extra arguments to pass to
      `android_emacs_init'.  */
-  public static String extraStartupArgument;
+  public static String[] extraStartupArguments;
 
   /* The thread running Emacs C code.  */
   private EmacsThread thread;
@@ -289,7 +290,9 @@ public final class EmacsService extends Service
 
        Log.d (TAG, "Initializing Emacs, where filesDir = " + filesDir
               + ", libDir = " + libDir + ", and classPath = " + classPath
-              + "; fileToOpen = " + EmacsOpenActivity.fileToOpen
+              + "; args = " + (extraStartupArguments != null
+                               ? Arrays.toString (extraStartupArguments)
+                               : "(none)")
               + "; display density: " + pixelDensityX + " by "
               + pixelDensityY + " scaled to " + scaledDensity);
 
@@ -306,9 +309,7 @@ public final class EmacsService extends Service
                                          classPath, EmacsService.this,
                                          Build.VERSION.SDK_INT);
            }
-         }, extraStartupArgument,
-         /* If any file needs to be opened, open it now.  */
-         EmacsOpenActivity.fileToOpen);
+         }, extraStartupArguments);
        thread.start ();
       }
     catch (IOException exception)
index 4adcb98b2f70b96728b677d555580d75d35791e8..a90eb73b1efb8befd89264566d6bc21688efd5cd 100644 (file)
@@ -28,24 +28,20 @@ public final class EmacsThread extends Thread
 {
   private static final String TAG = "EmacsThread";
 
-  /* Whether or not Emacs should be started with an additional
-     argument, and that additional argument if non-NULL.  */
-  private String extraStartupArgument;
+  /* Whether or not Emacs should be started with additional arguments,
+     and those additional arguments if non-NULL.  */
+  private final String[] extraStartupArguments;
 
   /* Runnable run to initialize Emacs.  */
-  private Runnable paramsClosure;
-
-  /* Whether or not to open a file after starting Emacs.  */
-  private String fileToOpen;
+  private final Runnable paramsClosure;
 
   public
   EmacsThread (EmacsService service, Runnable paramsClosure,
-              String extraStartupArgument, String fileToOpen)
+              String[] extraStartupArguments)
   {
     super ("Emacs main thread");
-    this.extraStartupArgument = extraStartupArgument;
+    this.extraStartupArguments = extraStartupArguments;
     this.paramsClosure = paramsClosure;
-    this.fileToOpen = fileToOpen;
   }
 
   @Override
@@ -54,23 +50,15 @@ public final class EmacsThread extends Thread
   {
     String args[];
 
-    if (fileToOpen == null)
-      {
-       if (extraStartupArgument == null)
-         args = new String[] { "libandroid-emacs.so", };
-       else
-         args = new String[] { "libandroid-emacs.so",
-                               extraStartupArgument, };
-      }
+    if (extraStartupArguments == null)
+      args = new String[] { "libandroid-emacs.so", };
     else
       {
-       if (extraStartupArgument == null)
-         args = new String[] { "libandroid-emacs.so",
-                               fileToOpen, };
-       else
-         args = new String[] { "libandroid-emacs.so",
-                               extraStartupArgument,
-                               fileToOpen, };
+       /* Prepend "libandroid-emacs.so" to the list of arguments.  */
+       args = new String[extraStartupArguments.length + 1];
+       args[0] = "libandroid-emacs.so";
+       System.arraycopy (extraStartupArguments, 0, args,
+                         1, extraStartupArguments.length);
       }
 
     paramsClosure.run ();