Skip to content

Commit

Permalink
Use of FileProvider for better security & compatibility
Browse files Browse the repository at this point in the history
Shin-NiL committed May 8, 2018
1 parent 8ab40b6 commit 3b1aec6
Showing 6 changed files with 41 additions and 14 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions demo/godot-2/share_btn.gd
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions share/android/AndroidManifestChunk.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<provider
android:name="android.support.v4.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false"
android:authorities="${applicationId}">

<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths"/>

</provider>
24 changes: 16 additions & 8 deletions share/android/GodotShare.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
3 changes: 3 additions & 0 deletions share/android/res/xml/file_provider_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<paths>
<files-path path="/" name="filesRoot" />
</paths>
2 changes: 2 additions & 0 deletions share/config.py
Original file line number Diff line number Diff line change
@@ -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":

0 comments on commit 3b1aec6

Please sign in to comment.