Skip to content
This repository has been archived by the owner on Mar 9, 2024. It is now read-only.

Commit

Permalink
- Added <queries> element to AndroidManifest due to new "Package Visi…
Browse files Browse the repository at this point in the history
…bility change" and added a relevant FAQ entry to README

- Readied the Android native source code for upcoming Android 12 change related to PendingIntents (will recompile the native library once the release comes out of preview)
  • Loading branch information
yasirkula committed May 5, 2021
1 parent 3776f3d commit 29d91ba
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 23 deletions.
12 changes: 12 additions & 0 deletions .github/AAR Source (Android)/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yasirkula.unity">
<application>
<activity android:name=".NativeShareCustomShareDialogActivity" android:theme="@style/Theme.NativeShareTransparent" />
<receiver android:name=".NativeShareBroadcastListener" android:exported="false" />
<provider
android:name="com.yasirkula.unity.NativeShareContentProvider"
android:authorities="${applicationId}.NativeShareContentProvider"
android:exported="false"
android:grantUriPermissions="true" />
</application>

<queries>
<intent>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="*/*" />
</intent>
<intent>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<data android:mimeType="*/*" />
</intent>
</queries>

<uses-sdk android:targetSdkVersion="4" />
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,33 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentSender;
import android.os.Build;
import android.util.Log;

// On Android 22 and newer devices, when standard ACTION_SEND share sheet is used, this BroadcastReceiver receives
// a message when an app from the share sheet is selected and then passes that information to Unity
@TargetApi( 22 )
public class NativeShareBroadcastListener extends BroadcastReceiver
{
private static final String BROADCAST_RECEIVER_FILTER = "com.yasirkula.unity.NATIVESHARE_RESULTRECEIVER";

private static NativeShareBroadcastListener broadcastReceiver;

public static IntentSender Initialize( Context context )
{
if( broadcastReceiver == null )
Intent receiverIntent = new Intent( context, NativeShareBroadcastListener.class );

int pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT;
if( Build.VERSION.SDK_INT >= 31 )
{
broadcastReceiver = new NativeShareBroadcastListener();
( (Activity) context ).getApplication().registerReceiver( broadcastReceiver, new IntentFilter( BROADCAST_RECEIVER_FILTER ) );
// We must mark PendingIntent as either mutable or immutable on Android 12+
// Maybe FLAG_IMMUTABLE is sufficient but the pre-31 default value was implicitly mutable and I don't trust
// all social apps to work correctly on Android 12+ (API 31+) if I set it to FLAG_IMMUTABLE
//pendingIntentFlags |= PendingIntent.FLAG_MUTABLE;
}

Intent receiverIntent = new Intent( BROADCAST_RECEIVER_FILTER );
return PendingIntent.getBroadcast( context, 0, receiverIntent, PendingIntent.FLAG_UPDATE_CURRENT ).getIntentSender();
return PendingIntent.getBroadcast( context, 0, receiverIntent, pendingIntentFlags ).getIntentSender();
}

@Override
public void onReceive( Context context, Intent intent )
{
if( broadcastReceiver != null )
{
context.getApplicationContext().unregisterReceiver( broadcastReceiver );
broadcastReceiver = null;
}
else
Log.e( "Unity", "ShareResultBroadcastReceiver was null!" );

if( NativeShare.shareResultReceiver != null )
{
ComponentName selectedShareTarget = intent.getParcelableExtra( Intent.EXTRA_CHOSEN_COMPONENT );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import android.os.Bundle;
import android.os.PersistableBundle;

// Sole purporse of this activity is to show a NativeShareCsutomShareDialog inside
// Sole purpose of this activity is to show a NativeShareCustomShareDialog inside
// We are not displaying this dialog inside Unity's own Activity for 2 reasons:
// 1: When shown inside Unity'a activity, the dialog doesn't block Unity's main thread (game keeps playing in the background)
// (although this also happens on iOS, this behaviour is inconsistent with the standard ACTION_SEND intent)
Expand Down
4 changes: 2 additions & 2 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ On Android, you can share on a specific app via *AddTarget*. For iOS, you can ch

It is just not possible to share an image/file with text/subject on some apps (e.g. Facebook), they intentionally omit either the image or the text from the shared content. These apps require you to use their own SDKs for complex share actions. For best compatibility, I'd recommend you to share either only image or only text.

- **Can't share, it says "Can't file ContentProvider, share not possible!" in Logcat**
- **I can't build the project to Android, it says "Android resource linking failed: unexpected element <queries> found in <manifest>" in the error message**

After building your project, verify that NativeShare's `<provider ... />` tag is inserted in-between the `<application>...</application>` tags of *PROJECT_PATH/Temp/StagingArea/AndroidManifest.xml*. If not, please create a new **Issue**.
NativeShare adds `<queries>` element to AndroidManifest.xml due to the new [package visibility change](https://developer.android.com/training/package-visibility). The build error can be fixed by following these steps: https://developers.google.com/ar/develop/unity/android-11-build (in my tests, changing "*Gradle installed with Unity*" wasn't necessary). In the worst case, if you are OK with NativeShare not working on some of the affected devices, then you can open *NativeShare.aar* with WinRAR or 7-Zip and then remove the `<queries>...</queries>` element from *AndroidManifest.xml*.

- **Can't share, it says "java.lang.ClassNotFoundException: com.yasirkula.unity.NativeShare" in Logcat**

Expand Down
Binary file modified Plugins/NativeShare/Android/NativeShare.aar
Binary file not shown.
8 changes: 6 additions & 2 deletions Plugins/NativeShare/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ E-mail: yasirkula@gmail.com
1. ABOUT
This plugin helps you natively share files (images, videos, documents, etc.) and/or plain text on Android & iOS. A ContentProvider is used to share the media on Android.


2. HOW TO
2.1. Android Setup
NativeShare no longer requires any manual setup on Android. If you were using an older version of the plugin, you need to remove NativeShare's "<provider ... />" from your AndroidManifest.xml.
Expand All @@ -24,19 +25,21 @@ b. Manual Setup for iOS
- enter a Photo Library Usage Description to Info.plist in Xcode (in case user decides to save the shared media to Photos)
- also enter a Photo Library Additions Usage Description to Info.plist in Xcode, if exists


3. FAQ
- Can I share on a specific app?
On Android, you can share on a specific app via AddTarget. For iOS, you can check out this post and see if it works for you: https://forum.unity.com/threads/native-share-for-android-ios-open-source.519865/page-4#post-4011874

- I can't share image with text on X app
It is just not possible to share an image/file with text/subject on some apps (e.g. Facebook), they intentionally omit either the image or the text from the shared content. These apps require you to use their own SDKs for complex share actions. For best compatibility, I'd recommend you to share either only image or only text.

- Can't share, it says "Can't file ContentProvider, share not possible!" in Logcat
After building your project, verify that NativeShare's "<provider ... />" tag is inserted in-between the "<application>...</application>" tags of PROJECT_PATH/Temp/StagingArea/AndroidManifest.xml. If not, please contact me.
- I can't build the project to Android, it says "Android resource linking failed: unexpected element <queries> found in <manifest>" in the error message
NativeShare adds "<queries>" element to AndroidManifest.xml due to the new package visibility change (https://developer.android.com/training/package-visibility). The build error can be fixed by following these steps: https://developers.google.com/ar/develop/unity/android-11-build (in my tests, changing "Gradle installed with Unity" wasn't necessary). In the worst case, if you are OK with NativeShare not working on some of the affected devices, then you can open NativeShare.aar with WinRAR or 7-Zip and then remove the "<queries>...</queries>" element from AndroidManifest.xml.

- Can't share, it says "java.lang.ClassNotFoundException: com.yasirkula.unity.NativeShare" in Logcat
If you are sure that your plugin is up-to-date, then enable "Custom Proguard File" option from Player Settings and add the following line to that file: -keep class com.yasirkula.unity.* { *; }


4. SCRIPTING API
Simply create a new NativeShare object and customize it by chaining the following functions as you like:

Expand All @@ -56,5 +59,6 @@ Simply create a new NativeShare object and customize it by chaining the followin

Finally, calling the Share() function of the NativeShare object will present the share sheet.


5. KNOWN LIMITATIONS
- Gif files are shared as static images on iOS (to learn more, please see this issue: https://github.com/yasirkula/UnityNativeShare/issues/22)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.yasirkula.nativeshare",
"displayName": "Native Share",
"version": "1.4.0",
"version": "1.4.1",
"documentationUrl": "https://github.com/yasirkula/UnityNativeShare",
"changelogUrl": "https://github.com/yasirkula/UnityNativeShare/releases",
"licensesUrl": "https://github.com/yasirkula/UnityNativeShare/blob/master/LICENSE.txt",
Expand Down

0 comments on commit 29d91ba

Please sign in to comment.