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.
This commit is contained in:
Po Lu 2023-07-10 13:31:57 +08:00
parent faca007b61
commit cf2dde4261
5 changed files with 82 additions and 36 deletions

View file

@ -583,12 +583,18 @@ invocation of app_process (through android-emacs) can
}
}
/* Ask the system to open the specified URL.
/* Ask the system to open the specified URL in an application that
understands how to open it.
If SEND, tell the system to also open applications that can
``send'' the URL (through mail, for example), instead of only
those that can view the URL.
Value is NULL upon success, or a string describing the error
upon failure. */
public String
browseUrl (String url)
browseUrl (String url, boolean send)
{
Intent intent;
Uri uri;
@ -596,28 +602,47 @@ invocation of app_process (through android-emacs) can
try
{
/* Parse the URI. */
uri = Uri.parse (url);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
if (!send)
{
/* On Android 4.4 and later, check if URI is actually a
file name. If so, rewrite it into a content provider
URI, so that it can be accessed by other programs. */
uri = Uri.parse (url);
if (uri.getScheme ().equals ("file")
&& uri.getPath () != null)
uri
= DocumentsContract.buildDocumentUri ("org.gnu.emacs",
uri.getPath ());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
/* On Android 4.4 and later, check if URI is actually
a file name. If so, rewrite it into a content
provider URI, so that it can be accessed by other
programs. */
if (uri.getScheme ().equals ("file")
&& uri.getPath () != null)
uri
= DocumentsContract.buildDocumentUri ("org.gnu.emacs",
uri.getPath ());
}
Log.d (TAG, ("browseUri: browsing " + url
+ " --> " + uri.getPath ()
+ " --> " + uri));
intent = new Intent (Intent.ACTION_VIEW, uri);
intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
else
{
intent = new Intent (Intent.ACTION_SEND);
intent.setType ("text/plain");
intent.putExtra (Intent.EXTRA_SUBJECT, "Sharing link");
intent.putExtra (Intent.EXTRA_TEXT, url);
/* Display a list of programs able to send this URL. */
intent = Intent.createChooser (intent, "Send");
/* Apparently flags need to be set after a choser is
created. */
intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
}
Log.d (TAG, ("browseUri: browsing " + url
+ " --> " + uri.getPath ()
+ " --> " + uri));
intent = new Intent (Intent.ACTION_VIEW, uri);
intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity (intent);
}
catch (Exception e)

View file

@ -1307,18 +1307,31 @@ Default to the URL around or before point."
(function-put 'browse-url-default-haiku-browser
'browse-url-browser-kind 'external)
(defcustom browse-url-android-share nil
"If non-nil, share URLs instead of opening them.
When non-nil, `browse-url-default-android-browser' will try to
share the URL being browsed through programs such as mail clients
and instant messengers instead of opening it in a web browser."
:type 'boolean
:version "30.1")
(declare-function android-browse-url "androidselect.c")
;;;###autoload
(defun browse-url-default-android-browser (url &optional _new-window)
"Browse URL with the system default browser.
Default to the URL around or before point."
If `browse-url-android-share' is non-nil, try to share URL using
an external program instead. Default to the URL around or before
point."
(interactive (browse-url-interactive-arg "URL: "))
(setq url (browse-url-encode-url url))
(unless browse-url-android-share
;; The URL shouldn't be encoded if it's being shared through
;; another program.
(setq url (browse-url-encode-url url)))
;; Make sure the URL starts with an appropriate scheme.
(unless (string-match "\\(.+\\):/" url)
(setq url (concat "http://" url)))
(android-browse-url url))
(android-browse-url url browse-url-android-share))
(function-put 'browse-url-default-android-browser
'browse-url-browser-kind 'external)

View file

@ -2286,7 +2286,7 @@ android_init_emacs_service (void)
FIND_METHOD (get_screen_height, "getScreenHeight", "(Z)I");
FIND_METHOD (detect_mouse, "detectMouse", "()Z");
FIND_METHOD (name_keysym, "nameKeysym", "(I)Ljava/lang/String;");
FIND_METHOD (browse_url, "browseUrl", "(Ljava/lang/String;)"
FIND_METHOD (browse_url, "browseUrl", "(Ljava/lang/String;Z)"
"Ljava/lang/String;");
FIND_METHOD (restart_emacs, "restartEmacs", "()V");
FIND_METHOD (update_ic, "updateIC",
@ -6959,12 +6959,15 @@ android_project_image_nearest (struct android_image *image,
/* Other miscellaneous functions. */
/* Ask the system to start browsing the specified encoded URL. Upon
failure, return a string describing the error. Else, value is
nil. */
/* Ask the system to start browsing the specified URL. Upon failure,
return a string describing the error. Else, value is nil. URL
should be encoded unless SEND.
If SEND, open the URL with applications that can ``send'' or
``share'' the URL (through mail, for example.) */
Lisp_Object
android_browse_url (Lisp_Object url)
android_browse_url (Lisp_Object url, Lisp_Object send)
{
jobject value, string;
Lisp_Object tem;
@ -6974,7 +6977,8 @@ android_browse_url (Lisp_Object url)
value = (*android_java_env)->CallObjectMethod (android_java_env,
emacs_service,
service_class.browse_url,
string);
string,
(jboolean) !NILP (send));
android_exception_check ();
ANDROID_DELETE_LOCAL_REF (string);

View file

@ -178,7 +178,7 @@ struct android_battery_state
int temperature;
};
extern Lisp_Object android_browse_url (Lisp_Object);
extern Lisp_Object android_browse_url (Lisp_Object, Lisp_Object);
extern int android_query_battery (struct android_battery_state *);
extern void android_display_toast (const char *);

View file

@ -232,11 +232,15 @@ DEFUN ("android-clipboard-exists-p", Fandroid_clipboard_exists_p,
}
DEFUN ("android-browse-url", Fandroid_browse_url,
Sandroid_browse_url, 1, 1, 0,
doc: /* Start the system web browser.
Then, point the web browser to URL, which should be a URL-encoded
URL with a scheme specified. Signal an error upon failure. */)
(Lisp_Object url)
Sandroid_browse_url, 1, 2, 0,
doc: /* Open URL in an external application. URL should be a
URL-encoded URL with a scheme specified unless SEND is non-nil.
Signal an error upon failure.
If SEND is nil, start a program that is able to display the URL, such
as a web browser. Otherwise, try to share URL using programs such as
email clients. */)
(Lisp_Object url, Lisp_Object send)
{
Lisp_Object value;
@ -244,7 +248,7 @@ URL with a scheme specified. Signal an error upon failure. */)
error ("No Android display connection!");
CHECK_STRING (url);
value = android_browse_url (url);
value = android_browse_url (url, send);
/* Signal an error upon failure. */
if (!NILP (value))