Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share - Android Updates #1253

Merged
merged 2 commits into from
Mar 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/Fuse.Share/ShareModule.uno
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ namespace Fuse.Share
var description = args.Length>2 ? "" + args[2] : "";
if defined(android)
{
AndroidShareImpl.ShareFile("file://" + path, type, description);
AndroidShareImpl.ShareFile(path, type, description);
return true;
}
else if defined(iOS)
Expand Down
33 changes: 31 additions & 2 deletions Source/Fuse.Share/android/AndroidShareImpl.uno
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ namespace Fuse.Share
"java.io.File",
"java.io.InputStream",
"java.io.FileOutputStream",
"java.util.ArrayList",
"android.os.Build",
"android.net.Uri",
"android.util.Log",
"android.content.Intent",
"android.support.v4.content.FileProvider",
"android.content.Context")]
public extern(Android) class AndroidShareImpl
{
Expand All @@ -20,6 +24,7 @@ namespace Fuse.Share
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, text);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, description);
sendIntent.setType("text/plain");
com.fuse.Activity.getRootActivity().startActivity(Intent.createChooser(sendIntent, description));
@}
Expand All @@ -30,10 +35,34 @@ namespace Fuse.Share
Context context = com.fuse.Activity.getRootActivity();
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
Uri uri = Uri.parse(path);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, description);
//new way for Marshmallow+ (API 23)
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
/*
content:// must be used ~ https://developer.android.com/training/sharing/send
*/
Uri uri = Uri.parse("content://" + path);
File newFile = new File(uri.getPath());
/*
Note: The XML file is the only way you can specify the directories
you want to share; you can't programmatically add a directory.
~ https://developer.android.com/training/secure-file-sharing/setup-sharing
~ https://developer.android.com/reference/android/support/v4/content/FileProvider
*/
Uri contentUri = FileProvider.getUriForFile(context,
"@(Activity.Package).share_file_provider",
newFile);
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
} else {
//for older droids
Uri uri = Uri.parse("file://" + path);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
}
//give temporary read access to file
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType(mimeType);
context.startActivity(Intent.createChooser(shareIntent, description));

@}
}
}
21 changes: 21 additions & 0 deletions Source/Fuse.Share/android/AndroidShareImpl.uxl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Extensions Backend="CPlusPlus">

<ProcessFile Condition="Android" Name="ShareFileProvider.java" TargetName="app/src/main/java/com/fuse/fileprovider/ShareFileProvider.java" />

<Require Condition="Android" AndroidManifest.ApplicationElement>
<![CDATA[
<provider
android:name="com.fuse.fileprovider.ShareFileProvider"
android:authorities="@(Activity.Package).share_file_provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/android_share_paths" />
</provider>
]]>
</Require>

<CopyFile Condition="Android" Name="android_share_paths.xml" TargetName="app/src/main/res/xml/android_share_paths.xml" />

</Extensions>
14 changes: 14 additions & 0 deletions Source/Fuse.Share/android/ShareFileProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.fuse.fileprovider;

import android.support.v4.content.FileProvider;

/**
* Providing a custom {@code FileProvider} prevents manifest {@code <provider>} name collisions.
*
* See https://developer.android.com/guide/topics/manifest/provider-element.html for details.
*/
public class ShareFileProvider extends FileProvider {

// This class intentionally left blank.

}
9 changes: 9 additions & 0 deletions Source/Fuse.Share/android/android_share_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">

<cache-path name="cache" path="." />
<files-path name="files" path="." />
<external-cache-path name="external_cache_images" path="images" />
<cache-path name="cache_images" path="images" />

</paths>