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

Commit

Permalink
- Added AddEmailRecipient function (Android-only) (closed #140)
Browse files Browse the repository at this point in the history
- Potential fix for rare "NSCallbackHelper is destroyed!" warnings
- Potential fix for #137
  • Loading branch information
yasirkula committed May 1, 2022
1 parent e57be40 commit 71f2964
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class NativeShare

private static int isXiaomiOrMIUI = 0; // 1: true, -1: false

public static void Share( final Context context, final NativeShareResultReceiver shareResultReceiver, final String[] targetPackages, final String[] targetClasses, final String[] files, final String[] mimes, final String subject, final String text, final String title )
public static void Share( final Context context, final NativeShareResultReceiver shareResultReceiver, final String[] targetPackages, final String[] targetClasses, final String[] files, final String[] mimes, final String[] emailRecipients, final String subject, final String text, final String title )
{
if( files.length > 0 && GetAuthority( context ) == null )
{
Expand All @@ -55,6 +55,7 @@ public static void Share( final Context context, final NativeShareResultReceiver
bundle.putString( NativeShareFragment.TITLE_ID, title );
bundle.putStringArrayList( NativeShareFragment.FILES_ID, ConvertArrayToArrayList( files ) );
bundle.putStringArrayList( NativeShareFragment.MIMES_ID, ConvertArrayToArrayList( mimes ) );
bundle.putStringArrayList( NativeShareFragment.EMAIL_RECIPIENTS_ID, ConvertArrayToArrayList( emailRecipients ) );
bundle.putStringArrayList( NativeShareFragment.TARGET_PACKAGE_ID, ConvertArrayToArrayList( targetPackages ) );
bundle.putStringArrayList( NativeShareFragment.TARGET_CLASS_ID, ConvertArrayToArrayList( targetClasses ) );

Expand Down Expand Up @@ -100,6 +101,7 @@ public static Intent CreateIntentFromBundle( Context context, Bundle bundle, Arr
final String title = bundle.getString( NativeShareFragment.TITLE_ID );
final ArrayList<String> files = bundle.getStringArrayList( NativeShareFragment.FILES_ID );
final ArrayList<String> mimes = bundle.getStringArrayList( NativeShareFragment.MIMES_ID );
final ArrayList<String> emailRecipients = bundle.getStringArrayList( NativeShareFragment.EMAIL_RECIPIENTS_ID );
final ArrayList<String> targetPackages = bundle.getStringArrayList( NativeShareFragment.TARGET_PACKAGE_ID );
final ArrayList<String> targetClasses = bundle.getStringArrayList( NativeShareFragment.TARGET_CLASS_ID );

Expand Down Expand Up @@ -194,6 +196,13 @@ else if( !mimeSubtype.equals( thisMimeSubtype ) )
intent.setAction( Intent.ACTION_SEND );
}

if( emailRecipients.size() > 0 )
{
String[] emailRecipientsArray = new String[emailRecipients.size()];
emailRecipients.toArray( emailRecipientsArray );
intent.putExtra( Intent.EXTRA_EMAIL, emailRecipientsArray );
}

if( title.length() > 0 )
intent.putExtra( Intent.EXTRA_TITLE, title );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class NativeShareFragment extends Fragment
public static final String TARGET_CLASS_ID = "NS_TARGET_CLASS";
public static final String FILES_ID = "NS_FILES";
public static final String MIMES_ID = "NS_MIMES";
public static final String EMAIL_RECIPIENTS_ID = "NS_EMAIL_RECIPIENTS";
public static final String SUBJECT_ID = "NS_SUBJECT";
public static final String TEXT_ID = "NS_TEXT";
public static final String TITLE_ID = "NS_TITLE";
Expand All @@ -45,6 +46,8 @@ public void onCreate( Bundle savedInstanceState )
final Intent shareIntent = NativeShare.CreateIntentFromBundle( getActivity(), getArguments(), fileUris );
final String title = getArguments().getString( NativeShareFragment.TITLE_ID );

shareIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );

try
{
Intent chooserIntent;
Expand Down
1 change: 1 addition & 0 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Simply create a new **NativeShare** object and customize it by chaining the foll
- `SetUrl( string url )`: sets the shared url. On supported iOS apps, this url is used to generate a preview of the target webpage. Other iOS apps may append the url to the text or omit it. While sharing a file on iOS or while sharing anything on Android, the url is appended to the text (unless the text already contains the url)
- `AddFile( string filePath, string mime = null )`: adds the file at path to the share action. You can add multiple files of different types. The MIME of the file is automatically determined if left null; however, if the file doesn't have an extension and/or you already know the MIME of the file, you can enter the MIME manually. MIME has no effect on iOS
- `AddFile( Texture2D texture, string createdFileName = "Image.png" )`: saves the *texture* to *Application.temporaryCachePath* with the specified filename and adds the image file to the share action
- `AddEmailRecipient( string emailAddress )`: auto-populates the *recipients* field of e-mail applications on Android platform. Has no effect on iOS
- `SetTitle( string title )`: sets the title of the share dialog on Android platform. Has no effect on iOS
- `AddTarget( string androidPackageName, string androidClassName = null )`: shares content on a specific application on Android platform. If *androidClassName* is left null, list of activities in the share dialog will be narrowed down to the activities in the specified *androidPackageName* that can handle this share action. Note that androidClassName, if provided, must be the full name of the activity (with its package). You can call this function multiple times. This function has no effect on iOS
- `SetCallback( ShareResultCallback callback )`: invokes the *callback* function after the share action is completed. **ShareResultCallback** has the following signature: `void ShareResultCallback( ShareResult result, string shareTarget )`
Expand Down
4 changes: 3 additions & 1 deletion Plugins/NativeShare/Android/NSCallbackHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if UNITY_EDITOR || UNITY_ANDROID
using System.Collections;
using UnityEngine;

namespace NativeShareNamespace
Expand Down Expand Up @@ -35,11 +36,12 @@ private void Update()
}
}

private void OnApplicationFocus( bool focus )
private IEnumerator OnApplicationFocus( bool focus )
{
if( focus )
{
// Share sheet is closed and now Unity activity is running again. Send Unknown result if OnShareCompleted wasn't called
yield return null;
resultReceived = true;
}
}
Expand Down
Binary file modified Plugins/NativeShare/Android/NativeShare.aar
Binary file not shown.
16 changes: 15 additions & 1 deletion Plugins/NativeShare/NativeShare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ private static AndroidJavaObject Context
private string title = string.Empty;
private string url = string.Empty;

#if UNITY_EDITOR || UNITY_ANDROID
private readonly List<string> emailRecipients = new List<string>( 0 );
#endif

#if UNITY_EDITOR || UNITY_ANDROID
private readonly List<string> targetPackages = new List<string>( 0 );
private readonly List<string> targetClasses = new List<string>( 0 );
Expand Down Expand Up @@ -162,6 +166,16 @@ public NativeShare AddFile( Texture2D texture, string createdFileName = "Image.p
return this;
}

public NativeShare AddEmailRecipient( string emailAddress )
{
#if UNITY_EDITOR || UNITY_ANDROID
if( !string.IsNullOrEmpty( emailAddress ) && !emailRecipients.Contains( emailAddress ) )
emailRecipients.Add( emailAddress );
#endif

return this;
}

public void Share()
{
if( files.Count == 0 && subject.Length == 0 && text.Length == 0 && url.Length == 0 )
Expand All @@ -176,7 +190,7 @@ public void Share()
if( callback != null )
callback( ShareResult.Shared, null );
#elif UNITY_ANDROID
AJC.CallStatic( "Share", Context, new NSShareResultCallbackAndroid( callback ), targetPackages.ToArray(), targetClasses.ToArray(), files.ToArray(), mimes.ToArray(), subject, CombineURLWithText(), title );
AJC.CallStatic( "Share", Context, new NSShareResultCallbackAndroid( callback ), targetPackages.ToArray(), targetClasses.ToArray(), files.ToArray(), mimes.ToArray(), emailRecipients.ToArray(), subject, CombineURLWithText(), title );
#elif UNITY_IOS
NSShareResultCallbackiOS.Initialize( callback );
if( files.Count == 0 )
Expand Down
1 change: 1 addition & 0 deletions Plugins/NativeShare/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Simply create a new NativeShare object and customize it by chaining the followin
- SetUrl( string url ): sets the shared url. On supported iOS apps, this url is used to generate a preview of the target webpage. Other iOS apps may append the url to the text or omit it. While sharing a file on iOS or while sharing anything on Android, the url is appended to the text (unless the text already contains the url)
- AddFile( string filePath, string mime = null ): adds the file at path to the share action. You can add multiple files of different types. The MIME of the file is automatically determined if left null; however, if the file doesn't have an extension and/or you already know the MIME of the file, you can enter the MIME manually. MIME has no effect on iOS
- AddFile( Texture2D texture, string createdFileName = "Image.png" ): saves the texture to Application.temporaryCachePath with the specified filename and adds the image file to the share action
- AddEmailRecipient( string emailAddress ): auto-populates the recipients field of e-mail applications on Android platform. Has no effect on iOS
- SetTitle( string title ): sets the title of the share dialog on Android platform. Has no effect on iOS
- AddTarget( string androidPackageName, string androidClassName = null ): shares content on a specific application on Android platform. If androidClassName is left null, list of activities in the share dialog will be narrowed down to the activities in the specified androidPackageName that can handle this share action. Note that androidClassName, if provided, must be the full name of the activity (with its package). You can call this function multiple times. This function has no effect on iOS
- SetCallback( ShareResultCallback callback ): invokes the callback function after the share action is completed. ShareResultCallback has the following signature: void ShareResultCallback( ShareResult result, string shareTarget )
Expand Down
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.6",
"version": "1.4.7",
"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 71f2964

Please sign in to comment.