summaryrefslogtreecommitdiff
path: root/java/org/gnu/emacs/EmacsOpenActivity.java
blob: 327a53bc417bf668b17eaed0b00f79c7579ab3e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/* Communication module for Android terminals.  -*- c-file-style: "GNU" -*-

Copyright (C) 2023-2024 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */

package org.gnu.emacs;

/* This class makes the Emacs server work reasonably on Android.

   There is no way to make the Unix socket publicly available on
   Android.

   Instead, this activity tries to connect to the Emacs server, to
   make it open files the system asks Emacs to open, and to emulate
   some reasonable behavior when Emacs has not yet started.

   First, Emacs registers itself as an application that can open text
   and image files.

   Then, when the user is asked to open a file and selects ``Emacs''
   as the application that will open the file, the system pops up a
   window, this activity, and calls the `onCreate' function.

   `onCreate' then tries very to find the file name of the file that
   was selected, and give it to emacsclient.

   If emacsclient successfully opens the file, then this activity
   starts EmacsActivity (to bring it on to the screen); otherwise, it
   displays the output of emacsclient or any error message that occurs
   and exits.  */

import android.app.AlertDialog;
import android.app.Activity;

import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;

import android.net.Uri;

import android.os.Build;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.os.Parcelable;

import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import java.util.List;

public final class EmacsOpenActivity extends Activity
  implements DialogInterface.OnClickListener,
  DialogInterface.OnCancelListener
{
  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.  */
  public static EmacsOpenActivity currentActivity;

  private class EmacsClientThread extends Thread
  {
    private ProcessBuilder builder;

    public
    EmacsClientThread (ProcessBuilder processBuilder)
    {
      builder = processBuilder;
    }

    @Override
    public void
    run ()
    {
      Process process;
      InputStream error;
      String errorText;

      try
	{
	  /* Start emacsclient.  */
	  process = builder.start ();
	  process.waitFor ();

	  /* Now figure out whether or not starting the process was
	     successful.  */
	  if (process.exitValue () == 0)
	    finishSuccess ();
	  else
	    finishFailure ("Error opening file", null);
	}
      catch (IOException exception)
	{
	  finishFailure ("Internal error", exception.toString ());
	}
      catch (InterruptedException exception)
	{
	  finishFailure ("Internal error", exception.toString ());
	}
    }
  }

  @Override
  public void
  onClick (DialogInterface dialog, int which)
  {
    finish ();
  }

  @Override
  public void
  onCancel (DialogInterface dialog)
  {
    finish ();
  }

  public String
  readEmacsClientLog ()
  {
    File file, cache;
    FileReader reader;
    char[] buffer;
    int rc;
    StringBuilder builder;

    /* Because the ProcessBuilder functions necessary to redirect
       process output are not implemented on Android 7 and earlier,
       print a generic error message.  */

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
      return ("This is likely because the Emacs server"
	      + " is not running, or because you did"
	      + " not grant Emacs permission to access"
	      + " external storage.");

    cache = getCacheDir ();
    file = new File (cache, "emacsclient.log");
    builder = new StringBuilder ();
    reader = null;

    try
      {
	reader = new FileReader (file);
	buffer = new char[2048];

	while ((rc = reader.read (buffer, 0, 2048)) != -1)
	  builder.append (buffer, 0, rc);

	reader.close ();
	return builder.toString ();
      }
    catch (IOException exception)
      {
	/* Close the reader if it's already been opened.  */

	try
	  {
	    if (reader != null)
	      reader.close ();
	  }
	catch (IOException e)
	  {
	    /* Not sure what to do here.  */
	  }

	return ("Couldn't read emacsclient.log: "
		+ exception.toString ());
      }
  }

  private void
  displayFailureDialog (String title, String text)
  {
    AlertDialog.Builder builder;
    AlertDialog dialog;

    builder = new AlertDialog.Builder (this);
    dialog = builder.create ();
    dialog.setTitle (title);

    if (text == null)
      /* Read in emacsclient.log instead.  */
      text = readEmacsClientLog ();

    dialog.setMessage (text);
    dialog.setButton (DialogInterface.BUTTON_POSITIVE, "OK", this);
    dialog.setOnCancelListener (this);
    dialog.show ();
  }

  /* 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.

     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,
		       Uri uri)
    throws IOException, FileNotFoundException
  {
    File inFile;
    FileOutputStream outStream;
    InputStream stream;
    byte buffer[];
    int read;
    String content;

    if (file != null)
      {
	inFile = new File (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)
      {
	content = EmacsService.buildContentName (uri, getContentResolver ());
	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 (), content);
    buffer = new byte[4098];

    /* Initialize both streams to NULL.  */
    outStream = null;
    stream = null;

    try
      {
	outStream = new FileOutputStream (inFile);
	stream = new FileInputStream (fd.getFileDescriptor ());

	while ((read = stream.read (buffer)) >= 0)
	  outStream.write (buffer, 0, read);
      }
    finally
      {
	/* Note that this does not close FD.

	   Keep in mind that execution is transferred to ``finally''
	   even if an exception happens inside the while loop
	   above.  */

	if (stream != null)
	  stream.close ();

	if (outStream != null)
	  outStream.close ();
      }

    return inFile.getCanonicalPath ();
  }

  /* Finish this activity in response to emacsclient having
     successfully opened a file.

     In the main thread, close this window, and open a window
     belonging to an Emacs frame.  */

  public void
  finishSuccess ()
  {
    runOnUiThread (new Runnable () {
	@Override
	public void
	run ()
	{
	  Intent intent;

	  intent = new Intent (EmacsOpenActivity.this,
			       EmacsActivity.class);

	  /* This means only an existing frame will be displayed.  */
	  intent.addFlags (Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
	  startActivity (intent);

	  EmacsOpenActivity.this.finish ();
	}
      });
  }

  /* Finish this activity after displaying a dialog associated with
     failure to open a file.

     Use TITLE as the title of the dialog.  If TEXT is non-NULL,
     display that text in the dialog.  Otherwise, use the contents of
     emacsclient.log in the cache directory instead, or describe why
     that file cannot be read.  */

  public void
  finishFailure (final String title, final String text)
  {
    runOnUiThread (new Runnable () {
	@Override
	public void
	run ()
	{
	  displayFailureDialog (title, text);
	}
      });
  }

  /* Start `emacsclient' with the provided list of ARGUMENTS, after
     ARGUMENTS[0] is replaced with the name of the emacsclient binary.

     Create a new thread to await its completion, subsequently
     reporting any errors that arise to the user.  */

  public void
  startEmacsClient (String[] arguments)
  {
    String libDir;
    ProcessBuilder builder;
    Process process;
    EmacsClientThread thread;
    File file;
    Intent intent;

    libDir = EmacsService.getLibraryDirectory (this);
    arguments[0] = libDir + "/libemacsclient.so";

    builder = new ProcessBuilder (arguments);

    /* Redirection is unfortunately not possible in Android 7 and
       earlier.  */

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
      {
	file = new File (getCacheDir (), "emacsclient.log");

	/* Redirect standard error to a file so that errors can be
	   meaningfully reported.  */

	if (file.exists ())
	  file.delete ();

	builder.redirectError (file);
      }

    /* Track process output in a new thread, since this is the UI
       thread and doing so here can cause deadlocks when EmacsService
       decides to wait for something.  */

    thread = new EmacsClientThread (builder);
    thread.start ();
  }

  /* Run emacsclient to open the file specified in the Intent that
     caused this activity to start.

     Determine the name of the file corresponding to the URI specified
     in that intent; then, run emacsclient and wait for it to finish.

     Finally, display any error message, transfer the focus to an
     Emacs frame, and finish the activity.  */

  @SuppressWarnings ("deprecation") /* getParcelableExtra */
  @Override
  public void
  onCreate (Bundle savedInstanceState)
  {
    String action, fileName;
    Intent intent;
    Uri uri;
    ContentResolver resolver;
    ParcelFileDescriptor fd;
    byte[] names;
    String errorBlurb, scheme;
    String subjectString, textString, attachmentString;
    CharSequence tem;
    String tem1;
    String[] emails;
    StringBuilder builder;
    List<Parcelable> list;

    super.onCreate (savedInstanceState);

    /* Obtain the intent that started Emacs.  */
    intent = getIntent ();
    action = intent.getAction ();
    resolver = getContentResolver ();

    if (action == null)
      {
	finish ();
	return;
      }

    /* Now see if the action specified is supported by Emacs.  */

    if (action.equals ("android.intent.action.VIEW")
	|| action.equals ("android.intent.action.EDIT")
	|| action.equals ("android.intent.action.PICK")
	|| action.equals ("android.intent.action.SEND")
	|| action.equals ("android.intent.action.SENDTO"))
      {
	/* Obtain the URI of the action.  */
	uri = intent.getData ();

	if (uri == null)
	  {
	    finish ();
	    return;
	  }

	scheme = uri.getScheme ();

	/* It is possible for scheme to be NULL, under Android 2.3 at
	   least.  */

	if (scheme == null)
	  return;

	/* If URL is a mailto URI, call `message-mailto' much the same
	   way emacsclient-mail.desktop does.  */

	if (scheme.equals ("mailto"))
	  {
	    /* Escape the special characters $ and " before enclosing
	       the string within the `message-mailto' wrapper.  */
	    fileName = uri.toString ();

	    /* If fileName is merely mailto: (absent either an email
	       address or content), then the program launching Emacs
	       conceivably provided such an URI to exclude non-email
	       programs from the Share dialog.  Intents created thus
	       might hold the recipient email as a string array, which
	       is non-standard behavior.  */

	    if (fileName.equals ("mailto:") || fileName.equals ("mailto://"))
	      {
	        emails = intent.getStringArrayExtra (Intent.EXTRA_EMAIL);

		if (emails[0] != null && emails.length > 0)
		  fileName = "mailto:" + emails[0];
	      }

	    /* Subsequently, escape fileName such that it is rendered
	       safe to append to the command line.  */

	    fileName = (fileName
			.replace ("\\", "\\\\")
			.replace ("\"", "\\\"")
			.replace ("$", "\\$"));

	    fileName = "(message-mailto \"" + fileName + "\" ";

	    /* Parse the intent itself to ascertain if any
	       non-standard subject, body, or something else of the
	       like is set.  Such fields, non-standard as they are,
	       yield to fields provided within the URL itself; refer
	       to message-mailto.  */

	    textString = attachmentString = subjectString = "()";

	    tem = intent.getCharSequenceExtra (Intent.EXTRA_SUBJECT);

	    if (tem != null)
	      subjectString = ("\"" + (tem.toString ()
				       .replace ("\\", "\\\\")
				       .replace ("\"", "\\\"")
				       .replace ("$", "\\$"))
			       + "\" ");

	    tem = intent.getCharSequenceExtra (Intent.EXTRA_TEXT);

	    if (tem != null)
	      textString = ("\"" + (tem.toString ()
				    .replace ("\\", "\\\\")
				    .replace ("\"", "\\\"")
				    .replace ("$", "\\$"))
			    + "\" ");

	    /* Producing a list of attachments is prey to two
	       mannerisms of the system: in the first instance, these
	       attachments are content URIs which don't allude to
	       their content types; and in the second instance, they
	       are either a list of such URIs or one individual URI,
	       subject to the type of the intent itself.  */

	    if (Intent.ACTION_SEND.equals (action))
	      {
		/* The attachment in this case is a single content
		   URI.  */

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
		  uri = intent.getParcelableExtra (Intent.EXTRA_STREAM,
						   Uri.class);
		else
		  uri = intent.getParcelableExtra (Intent.EXTRA_STREAM);

		if ((scheme = uri.getScheme ()) != null
		    && scheme.equals ("content")
		    && (Build.VERSION.SDK_INT
			>= Build.VERSION_CODES.KITKAT))
		  {
		    tem1 = EmacsService.buildContentName (uri, resolver);
		    attachmentString = ("'(\"" + (tem1.replace ("\\", "\\\\")
						  .replace ("\"", "\\\"")
						  .replace ("$", "\\$"))
					+ "\")");
		  }
		else if (scheme != null && scheme.equals ("file"))
		  {
		    tem1 = uri.getPath ();
		    attachmentString = ("'(\"" + (tem1.replace ("\\", "\\\\")
						  .replace ("\"", "\\\"")
						  .replace ("$", "\\$"))
					+ "\")");
		  }
	      }
	    else
	      {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
		  list
		    = intent.getParcelableArrayListExtra (Intent.EXTRA_STREAM,
							  Parcelable.class);
		else
		  list
		    = intent.getParcelableArrayListExtra (Intent.EXTRA_STREAM);

		if (list != null)
		  {
		    builder = new StringBuilder ("'(");

		    for (Parcelable parcelable : list)
		      {
			if (!(parcelable instanceof Uri))
			  continue;

			uri = (Uri) parcelable;

			if (uri != null
			    && (scheme = uri.getScheme ()) != null
			    && scheme.equals ("content")
			    && (Build.VERSION.SDK_INT
				>= Build.VERSION_CODES.KITKAT))
			  {
			    tem1
			      = EmacsService.buildContentName (uri, resolver);
			    builder.append ("\"");
			    builder.append (tem1.replace ("\\", "\\\\")
					    .replace ("\"", "\\\"")
					    .replace ("$", "\\$"));
			    builder.append ("\"");
			  }
			else if (scheme != null
				 && scheme.equals ("file"))
			  {
			    tem1 = uri.getPath ();
			    builder.append ("\"");
			    builder.append (tem1.replace ("\\", "\\\\")
					    .replace ("\"", "\\\"")
					    .replace ("$", "\\$"));
			    builder.append ("\"");
			  }
		      }

		    builder.append (")");
		    attachmentString = builder.toString ();
		  }
	      }

	    fileName += subjectString;
	    fileName += textString;
	    fileName += attachmentString;
	    fileName += ")";

	    /* Execute emacsclient in order to execute this code.  */
	    currentActivity = this;
	    startEmacsClient (new String[] { "--timeout=10", "--no-wait",
					     "--eval", fileName, });
	    return;
	  }

	/* Now, try to get the file name.  */

	if (scheme.equals ("file"))
	  fileName = uri.getPath ();
	else
	  {
	    fileName = null;

	    if (scheme.equals ("content")
		/* Retrieving the native file descriptor of a
		   ParcelFileDescriptor requires Honeycomb, and
		   proceeding without this capability is pointless on
		   systems before KitKat, since Emacs doesn't support
		   opening content files on those.  */
		&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
	      {
		/* This is one of the annoying Android ``content''
		   URIs.  Most of the time, there is actually an
		   underlying file, but it cannot be found without
		   opening the file and doing readlink on its file
		   descriptor in /proc/self/fd.  */
		fd = null;

		try
		  {
		    fd = resolver.openFileDescriptor (uri, "r");
		    names = EmacsNative.getProcName (fd.getFd ());

		    /* What is the right encoding here? */

		    if (names != null)
		      fileName = new String (names, "UTF-8");

		    fileName = checkReadableOrCopy (fileName, fd, uri);
		  }
		catch (FileNotFoundException exception)
		  {
		    /* Do nothing.  */
		  }
		catch (IOException exception)
		  {
		    /* Do nothing.  */
		  }
		catch (SecurityException exception)
		  {
		    /* This means Emacs lacks the rights to open this
		       file.  Display the error message and exit.  */
		    displayFailureDialog ("Error opening file",
					  exception.toString ());
		    return;
		  }

		if (fd != null)
		  {
		    try
		      {
			fd.close ();
		      }
		    catch (IOException exception)
		      {
			/* Do nothing.  */
		      }
		  }
	      }
	    else if (scheme.equals ("org-protocol"))
	      /* URL is an org-protocol:// link, which is meant to be
		 directly relayed to emacsclient.  */
	      fileName = uri.toString ();

	    if (fileName == null)
	      {
		errorBlurb = ("The URI: " + uri + " could not be opened"
			      + ", as it does not encode file name inform"
			      + "ation.");
		displayFailureDialog ("Error opening file", errorBlurb);
		return;
	      }
	  }

	/* If the Emacs service is not running, then start Emacs and make
	   it open this file.  */

	if (EmacsService.SERVICE == null)
	  {
	    fileToOpen = fileName;
	    intent = new Intent (EmacsOpenActivity.this,
				 EmacsActivity.class);
	    finish ();
	    startActivity (intent);
	    return;
	  }

	/* And start emacsclient.  Set `currentActivity' to this now.
	   Presumably, it will shortly become capable of displaying
	   dialogs.  */
	currentActivity = this;
	startEmacsClient (new String[] { "--timeout=10", "--no-wait",
					 "--reuse-frame", fileName, });
      }
    else
      finish ();
  }



  @Override
  public void
  onDestroy ()
  {
    /* Clear `currentActivity' if it refers to the activity being
       destroyed.  */

    if (currentActivity == this)
      this.currentActivity = null;

    super.onDestroy ();
  }

  @Override
  public void
  onWindowFocusChanged (boolean isFocused)
  {
    if (isFocused)
      currentActivity = this;
    else if (currentActivity == this)
      currentActivity = null;

    super.onWindowFocusChanged (isFocused);
  }

  @Override
  public void
  onPause ()
  {
    /* XXX: clear currentActivity here as well; I don't know whether
       or not onWindowFocusChanged is always called prior to this.  */

    if (currentActivity == this)
      currentActivity = null;

    super.onPause ();
  }
}