From 3b1aec65c4e5f91aa50e3f8d7f59eb21cf1ce69d Mon Sep 17 00:00:00 2001 From: Shin-NiL Date: Tue, 8 May 2018 18:15:18 -0300 Subject: [PATCH] Use of FileProvider for better security & compatibility --- README.md | 8 ++++--- demo/godot-2/share_btn.gd | 7 +++--- share/android/AndroidManifestChunk.xml | 11 +++++++++ share/android/GodotShare.java | 24 ++++++++++++------- share/android/res/xml/file_provider_paths.xml | 3 +++ share/config.py | 2 ++ 6 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 share/android/AndroidManifestChunk.xml create mode 100644 share/android/res/xml/file_provider_paths.xml diff --git a/README.md b/README.md index 2cfde06..30240c2 100644 --- a/README.md +++ b/README.md @@ -50,15 +50,17 @@ The following methods are available: shareText(title, subject, text) # Share image -# @param String absolute_path The image location full path +# @param String image_abs_path The image location absolute path # @param String title # @param String subject # @param String text -void sharePic(absolute_path, title, subject, text) +void sharePic(image_abs_path, title, subject, text) ``` -Demonstration +Usage ------------- +An important note is that the image you want to share must be saved on the ```"user://"``` virtual directory root to be accessible, you can use ```OS.get_user_data_dir()``` to get its absolute path (required by ```sharePic```). + In the demo directory you'll find a working sample project for Godot 2 and 3 where a screen capture is shared. ### Known Issues diff --git a/demo/godot-2/share_btn.gd b/demo/godot-2/share_btn.gd index 90e5108..a214183 100644 --- a/demo/godot-2/share_btn.gd +++ b/demo/godot-2/share_btn.gd @@ -4,7 +4,7 @@ var share = null # our share singleton instance func _ready(): # initialize the share singleton if it exists - if (Globals.has_singleton("GodotShare")): + if Globals.has_singleton("GodotShare"): share = Globals.get_singleton("GodotShare") @@ -17,11 +17,12 @@ func _on_share_btn_pressed(): yield(get_tree(), "idle_frame") yield(get_tree(), "idle_frame") - # user://tmp.png + # The file must be saved at user:// root var image_save_path = OS.get_data_dir() + "/tmp.png" - # actually get takes the and saves caputure + # actually takes the caputure var capture = view_port.get_screen_capture() + # saves the capture as user://tmp.png capture.save_png(image_save_path) # if share was found, use it diff --git a/share/android/AndroidManifestChunk.xml b/share/android/AndroidManifestChunk.xml new file mode 100644 index 0000000..1e274ed --- /dev/null +++ b/share/android/AndroidManifestChunk.xml @@ -0,0 +1,11 @@ + + + + + \ No newline at end of file diff --git a/share/android/GodotShare.java b/share/android/GodotShare.java index cab2b19..ddc7121 100644 --- a/share/android/GodotShare.java +++ b/share/android/GodotShare.java @@ -6,12 +6,14 @@ import android.app.*; import android.net.Uri; import android.content.ContextWrapper; +import android.support.v4.content.FileProvider; import android.util.Log; public class GodotShare extends Godot.SingletonBase { + private static final String TAG = "godot"; private Activity activity; static public Godot.SingletonBase initialize(Activity p_activity) @@ -31,28 +33,34 @@ public GodotShare(Activity p_activity) public void shareText(String title, String subject, String text) { + Log.d(TAG, "shareText called"); Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); sharingIntent.setType("text/plain"); sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, text); activity.startActivity(Intent.createChooser(sharingIntent, title)); - - Log.d("godot", "shareText"); } public void sharePic(String path, String title, String subject, String text) { + Log.d(TAG, "sharePic called"); + + File f = new File(path); + + Uri uri; + try { + uri = FileProvider.getUriForFile(activity, activity.getPackageName(), f); + } catch (IllegalArgumentException e) { + Log.e(TAG, "The selected file can't be shared: " + path); + return; + } + Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("image/*"); - File f = new File(path); - f.setReadable(true, false); - Uri uri = Uri.fromFile(f); - shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject); shareIntent.putExtra(Intent.EXTRA_TEXT, text); shareIntent.putExtra(Intent.EXTRA_STREAM, uri); + shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); activity.startActivity(Intent.createChooser(shareIntent, title)); - - Log.d("godot", "sharePic"); } } diff --git a/share/android/res/xml/file_provider_paths.xml b/share/android/res/xml/file_provider_paths.xml new file mode 100644 index 0000000..406bff7 --- /dev/null +++ b/share/android/res/xml/file_provider_paths.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/share/config.py b/share/config.py index 55c8f25..f468cab 100644 --- a/share/config.py +++ b/share/config.py @@ -4,6 +4,8 @@ def can_build(plat): def configure(env): if (env['platform'] == 'android'): env.android_add_java_dir("android") + env.android_add_to_manifest("android/AndroidManifestChunk.xml") + env.android_add_res_dir("android/res") env.disable_module() if env['platform'] == "iphone":