aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPo Lu2023-07-10 13:31:57 +0800
committerPo Lu2023-07-10 13:31:57 +0800
commitcf2dde4261a311406203a38d6bf1be72b4f9e8a7 (patch)
tree904aa8c47417f9c16018ed04bf7ef05bfd446268
parentfaca007b61422969f5c8888c67a1e356a8c5b64a (diff)
downloademacs-cf2dde4261a311406203a38d6bf1be72b4f9e8a7.tar.gz
emacs-cf2dde4261a311406203a38d6bf1be72b4f9e8a7.zip
Update Android port
* java/org/gnu/emacs/EmacsService.java (browseUrl): New argument SEND. Choose from a list of applications that want to share the URL if true. * lisp/net/browse-url.el (browse-url-android-share): New user option. (browse-url-default-android-browser): Respect said user option. * src/android.c (android_init_emacs_service) (android_browse_url): Expose new option. * src/android.h: Update prototypes. * src/androidselect.c (Fandroid_browse_url): Likewise.
-rw-r--r--java/org/gnu/emacs/EmacsService.java65
-rw-r--r--lisp/net/browse-url.el19
-rw-r--r--src/android.c16
-rw-r--r--src/android.h2
-rw-r--r--src/androidselect.c16
5 files changed, 82 insertions, 36 deletions
diff --git a/java/org/gnu/emacs/EmacsService.java b/java/org/gnu/emacs/EmacsService.java
index 0543c3a1bdd..22649167f8a 100644
--- a/java/org/gnu/emacs/EmacsService.java
+++ b/java/org/gnu/emacs/EmacsService.java
@@ -583,12 +583,18 @@ public final class EmacsService extends Service
583 } 583 }
584 } 584 }
585 585
586 /* Ask the system to open the specified URL. 586 /* Ask the system to open the specified URL in an application that
587 understands how to open it.
588
589 If SEND, tell the system to also open applications that can
590 ``send'' the URL (through mail, for example), instead of only
591 those that can view the URL.
592
587 Value is NULL upon success, or a string describing the error 593 Value is NULL upon success, or a string describing the error
588 upon failure. */ 594 upon failure. */
589 595
590 public String 596 public String
591 browseUrl (String url) 597 browseUrl (String url, boolean send)
592 { 598 {
593 Intent intent; 599 Intent intent;
594 Uri uri; 600 Uri uri;
@@ -596,28 +602,47 @@ public final class EmacsService extends Service
596 try 602 try
597 { 603 {
598 /* Parse the URI. */ 604 /* Parse the URI. */
599 uri = Uri.parse (url); 605 if (!send)
600
601 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
602 { 606 {
603 /* On Android 4.4 and later, check if URI is actually a 607 uri = Uri.parse (url);
604 file name. If so, rewrite it into a content provider 608
605 URI, so that it can be accessed by other programs. */ 609 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
606 610 {
607 if (uri.getScheme ().equals ("file") 611 /* On Android 4.4 and later, check if URI is actually
608 && uri.getPath () != null) 612 a file name. If so, rewrite it into a content
609 uri 613 provider URI, so that it can be accessed by other
610 = DocumentsContract.buildDocumentUri ("org.gnu.emacs", 614 programs. */
611 uri.getPath ()); 615
616 if (uri.getScheme ().equals ("file")
617 && uri.getPath () != null)
618 uri
619 = DocumentsContract.buildDocumentUri ("org.gnu.emacs",
620 uri.getPath ());
621 }
622
623 Log.d (TAG, ("browseUri: browsing " + url
624 + " --> " + uri.getPath ()
625 + " --> " + uri));
626
627 intent = new Intent (Intent.ACTION_VIEW, uri);
628 intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK
629 | Intent.FLAG_GRANT_READ_URI_PERMISSION);
612 } 630 }
631 else
632 {
633 intent = new Intent (Intent.ACTION_SEND);
634 intent.setType ("text/plain");
635 intent.putExtra (Intent.EXTRA_SUBJECT, "Sharing link");
636 intent.putExtra (Intent.EXTRA_TEXT, url);
637
638 /* Display a list of programs able to send this URL. */
639 intent = Intent.createChooser (intent, "Send");
613 640
614 Log.d (TAG, ("browseUri: browsing " + url 641 /* Apparently flags need to be set after a choser is
615 + " --> " + uri.getPath () 642 created. */
616 + " --> " + uri)); 643 intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
644 }
617 645
618 intent = new Intent (Intent.ACTION_VIEW, uri);
619 intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK
620 | Intent.FLAG_GRANT_READ_URI_PERMISSION);
621 startActivity (intent); 646 startActivity (intent);
622 } 647 }
623 catch (Exception e) 648 catch (Exception e)
diff --git a/lisp/net/browse-url.el b/lisp/net/browse-url.el
index 8036bbc8acc..daa46baf8f2 100644
--- a/lisp/net/browse-url.el
+++ b/lisp/net/browse-url.el
@@ -1307,18 +1307,31 @@ Default to the URL around or before point."
1307(function-put 'browse-url-default-haiku-browser 1307(function-put 'browse-url-default-haiku-browser
1308 'browse-url-browser-kind 'external) 1308 'browse-url-browser-kind 'external)
1309 1309
1310(defcustom browse-url-android-share nil
1311 "If non-nil, share URLs instead of opening them.
1312When non-nil, `browse-url-default-android-browser' will try to
1313share the URL being browsed through programs such as mail clients
1314and instant messengers instead of opening it in a web browser."
1315 :type 'boolean
1316 :version "30.1")
1317
1310(declare-function android-browse-url "androidselect.c") 1318(declare-function android-browse-url "androidselect.c")
1311 1319
1312;;;###autoload 1320;;;###autoload
1313(defun browse-url-default-android-browser (url &optional _new-window) 1321(defun browse-url-default-android-browser (url &optional _new-window)
1314 "Browse URL with the system default browser. 1322 "Browse URL with the system default browser.
1315Default to the URL around or before point." 1323If `browse-url-android-share' is non-nil, try to share URL using
1324an external program instead. Default to the URL around or before
1325point."
1316 (interactive (browse-url-interactive-arg "URL: ")) 1326 (interactive (browse-url-interactive-arg "URL: "))
1317 (setq url (browse-url-encode-url url)) 1327 (unless browse-url-android-share
1328 ;; The URL shouldn't be encoded if it's being shared through
1329 ;; another program.
1330 (setq url (browse-url-encode-url url)))
1318 ;; Make sure the URL starts with an appropriate scheme. 1331 ;; Make sure the URL starts with an appropriate scheme.
1319 (unless (string-match "\\(.+\\):/" url) 1332 (unless (string-match "\\(.+\\):/" url)
1320 (setq url (concat "http://" url))) 1333 (setq url (concat "http://" url)))
1321 (android-browse-url url)) 1334 (android-browse-url url browse-url-android-share))
1322 1335
1323(function-put 'browse-url-default-android-browser 1336(function-put 'browse-url-default-android-browser
1324 'browse-url-browser-kind 'external) 1337 'browse-url-browser-kind 'external)
diff --git a/src/android.c b/src/android.c
index 5850b6079c9..4e9897f4648 100644
--- a/src/android.c
+++ b/src/android.c
@@ -2286,7 +2286,7 @@ android_init_emacs_service (void)
2286 FIND_METHOD (get_screen_height, "getScreenHeight", "(Z)I"); 2286 FIND_METHOD (get_screen_height, "getScreenHeight", "(Z)I");
2287 FIND_METHOD (detect_mouse, "detectMouse", "()Z"); 2287 FIND_METHOD (detect_mouse, "detectMouse", "()Z");
2288 FIND_METHOD (name_keysym, "nameKeysym", "(I)Ljava/lang/String;"); 2288 FIND_METHOD (name_keysym, "nameKeysym", "(I)Ljava/lang/String;");
2289 FIND_METHOD (browse_url, "browseUrl", "(Ljava/lang/String;)" 2289 FIND_METHOD (browse_url, "browseUrl", "(Ljava/lang/String;Z)"
2290 "Ljava/lang/String;"); 2290 "Ljava/lang/String;");
2291 FIND_METHOD (restart_emacs, "restartEmacs", "()V"); 2291 FIND_METHOD (restart_emacs, "restartEmacs", "()V");
2292 FIND_METHOD (update_ic, "updateIC", 2292 FIND_METHOD (update_ic, "updateIC",
@@ -6959,12 +6959,15 @@ android_project_image_nearest (struct android_image *image,
6959 6959
6960/* Other miscellaneous functions. */ 6960/* Other miscellaneous functions. */
6961 6961
6962/* Ask the system to start browsing the specified encoded URL. Upon 6962/* Ask the system to start browsing the specified URL. Upon failure,
6963 failure, return a string describing the error. Else, value is 6963 return a string describing the error. Else, value is nil. URL
6964 nil. */ 6964 should be encoded unless SEND.
6965
6966 If SEND, open the URL with applications that can ``send'' or
6967 ``share'' the URL (through mail, for example.) */
6965 6968
6966Lisp_Object 6969Lisp_Object
6967android_browse_url (Lisp_Object url) 6970android_browse_url (Lisp_Object url, Lisp_Object send)
6968{ 6971{
6969 jobject value, string; 6972 jobject value, string;
6970 Lisp_Object tem; 6973 Lisp_Object tem;
@@ -6974,7 +6977,8 @@ android_browse_url (Lisp_Object url)
6974 value = (*android_java_env)->CallObjectMethod (android_java_env, 6977 value = (*android_java_env)->CallObjectMethod (android_java_env,
6975 emacs_service, 6978 emacs_service,
6976 service_class.browse_url, 6979 service_class.browse_url,
6977 string); 6980 string,
6981 (jboolean) !NILP (send));
6978 android_exception_check (); 6982 android_exception_check ();
6979 6983
6980 ANDROID_DELETE_LOCAL_REF (string); 6984 ANDROID_DELETE_LOCAL_REF (string);
diff --git a/src/android.h b/src/android.h
index 33ca379e6ba..2323690fa25 100644
--- a/src/android.h
+++ b/src/android.h
@@ -178,7 +178,7 @@ struct android_battery_state
178 int temperature; 178 int temperature;
179}; 179};
180 180
181extern Lisp_Object android_browse_url (Lisp_Object); 181extern Lisp_Object android_browse_url (Lisp_Object, Lisp_Object);
182extern int android_query_battery (struct android_battery_state *); 182extern int android_query_battery (struct android_battery_state *);
183extern void android_display_toast (const char *); 183extern void android_display_toast (const char *);
184 184
diff --git a/src/androidselect.c b/src/androidselect.c
index d1f2ebb52f9..f5371280457 100644
--- a/src/androidselect.c
+++ b/src/androidselect.c
@@ -232,11 +232,15 @@ DEFUN ("android-clipboard-exists-p", Fandroid_clipboard_exists_p,
232} 232}
233 233
234DEFUN ("android-browse-url", Fandroid_browse_url, 234DEFUN ("android-browse-url", Fandroid_browse_url,
235 Sandroid_browse_url, 1, 1, 0, 235 Sandroid_browse_url, 1, 2, 0,
236 doc: /* Start the system web browser. 236 doc: /* Open URL in an external application. URL should be a
237Then, point the web browser to URL, which should be a URL-encoded 237URL-encoded URL with a scheme specified unless SEND is non-nil.
238URL with a scheme specified. Signal an error upon failure. */) 238Signal an error upon failure.
239 (Lisp_Object url) 239
240If SEND is nil, start a program that is able to display the URL, such
241as a web browser. Otherwise, try to share URL using programs such as
242email clients. */)
243 (Lisp_Object url, Lisp_Object send)
240{ 244{
241 Lisp_Object value; 245 Lisp_Object value;
242 246
@@ -244,7 +248,7 @@ URL with a scheme specified. Signal an error upon failure. */)
244 error ("No Android display connection!"); 248 error ("No Android display connection!");
245 249
246 CHECK_STRING (url); 250 CHECK_STRING (url);
247 value = android_browse_url (url); 251 value = android_browse_url (url, send);
248 252
249 /* Signal an error upon failure. */ 253 /* Signal an error upon failure. */
250 if (!NILP (value)) 254 if (!NILP (value))