Commit 4e42bd9e authored by zhouteng's avatar zhouteng

add share panel title for android and subject for both android and ios

parent c339e7be
...@@ -30,6 +30,8 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi ...@@ -30,6 +30,8 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi
private final Registrar mRegistrar; private final Registrar mRegistrar;
private List<String> list; private List<String> list;
private String type; private String type;
private String sharePanelTitle;
private String subject;
public static void registerWith(Registrar registrar) { public static void registerWith(Registrar registrar) {
MethodChannel channel = new MethodChannel(registrar.messenger(), CHANNEL); MethodChannel channel = new MethodChannel(registrar.messenger(), CHANNEL);
...@@ -50,22 +52,24 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi ...@@ -50,22 +52,24 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi
throw new IllegalArgumentException("Map argument expected"); throw new IllegalArgumentException("Map argument expected");
} }
// Android does not support showing the share sheet at a particular point on screen. // Android does not support showing the share sheet at a particular point on screen.
share((List) call.argument("list"), (String) call.argument("type")); list = call.argument("list");
type = call.argument("type");
sharePanelTitle = call.argument("sharePanelTitle");
subject = call.argument("subject");
share(list, type, sharePanelTitle, subject);
result.success(null); result.success(null);
} else { } else {
result.notImplemented(); result.notImplemented();
} }
} }
private void share(List<String> list, String type) { private void share(List<String> list, String type, String sharePanelTitle, String subject) {
if (list == null || list.isEmpty()) { if (list == null || list.isEmpty()) {
throw new IllegalArgumentException("Non-empty list expected"); throw new IllegalArgumentException("Non-empty list expected");
} }
this.list = list;
this.type = type;
Intent shareIntent = new Intent(); Intent shareIntent = new Intent();
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
if ("text".equals(type)) { if ("text".equals(type)) {
shareIntent.setAction(Intent.ACTION_SEND); shareIntent.setAction(Intent.ACTION_SEND);
...@@ -101,11 +105,11 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi ...@@ -101,11 +105,11 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList); shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
} }
} }
startChooserActivity(shareIntent); startChooserActivity(shareIntent, sharePanelTitle);
} }
private void startChooserActivity(Intent shareIntent) { private void startChooserActivity(Intent shareIntent,String sharePanelTitle) {
Intent chooserIntent = Intent.createChooser(shareIntent, null /* dialog title optional */); Intent chooserIntent = Intent.createChooser(shareIntent, sharePanelTitle /* dialog subject optional */);
if (mRegistrar.activity() != null) { if (mRegistrar.activity() != null) {
mRegistrar.activity().startActivity(chooserIntent); mRegistrar.activity().startActivity(chooserIntent);
} else { } else {
...@@ -126,7 +130,7 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi ...@@ -126,7 +130,7 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi
@Override @Override
public boolean onRequestPermissionsResult(int requestCode, String[] perms, int[] grantResults) { public boolean onRequestPermissionsResult(int requestCode, String[] perms, int[] grantResults) {
if (requestCode == CODE_ASK_PERMISSION && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (requestCode == CODE_ASK_PERMISSION && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
share(list, type); share(list, type, sharePanelTitle, subject);
} }
return false; return false;
} }
......
...@@ -35,7 +35,9 @@ class _MyAppState extends State<MyApp> { ...@@ -35,7 +35,9 @@ class _MyAppState extends State<MyApp> {
children: <Widget>[ children: <Widget>[
RaisedButton( RaisedButton(
onPressed: () { onPressed: () {
ShareExtend.share("share text", "text"); ShareExtend.share("share text", "text",
sharePanelTitle: "share text title",
subject: "share text subject");
}, },
child: Text("share text"), child: Text("share text"),
), ),
...@@ -44,7 +46,9 @@ class _MyAppState extends State<MyApp> { ...@@ -44,7 +46,9 @@ class _MyAppState extends State<MyApp> {
File f = await ImagePicker.pickImage( File f = await ImagePicker.pickImage(
source: ImageSource.gallery); source: ImageSource.gallery);
if (f != null) { if (f != null) {
ShareExtend.share(f.path, "image"); ShareExtend.share(f.path, "image",
sharePanelTitle: "share image title",
subject: "share image subject");
} }
}, },
child: Text("share image"), child: Text("share image"),
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
NSDictionary *arguments = [call arguments]; NSDictionary *arguments = [call arguments];
NSArray *array = arguments[@"list"]; NSArray *array = arguments[@"list"];
NSString *shareType = arguments[@"type"]; NSString *shareType = arguments[@"type"];
NSString *subject = arguments[@"subject"];
if (array.count == 0) { if (array.count == 0) {
result( result(
...@@ -31,7 +32,7 @@ ...@@ -31,7 +32,7 @@
} }
if ([shareType isEqualToString:@"text"]) { if ([shareType isEqualToString:@"text"]) {
[self share:array atSource:originRect]; [self share:array atSource:originRect withSubject:subject];
result(nil); result(nil);
} else if ([shareType isEqualToString:@"image"]) { } else if ([shareType isEqualToString:@"image"]) {
NSMutableArray * imageArray = [[NSMutableArray alloc] init]; NSMutableArray * imageArray = [[NSMutableArray alloc] init];
...@@ -39,14 +40,14 @@ ...@@ -39,14 +40,14 @@
UIImage *image = [UIImage imageWithContentsOfFile:path]; UIImage *image = [UIImage imageWithContentsOfFile:path];
[imageArray addObject:image]; [imageArray addObject:image];
} }
[self share:imageArray atSource:originRect]; [self share:imageArray atSource:originRect withSubject:subject];
} else { } else {
NSMutableArray * urlArray = [[NSMutableArray alloc] init]; NSMutableArray * urlArray = [[NSMutableArray alloc] init];
for (NSString * path in array) { for (NSString * path in array) {
NSURL *url = [NSURL fileURLWithPath:path]; NSURL *url = [NSURL fileURLWithPath:path];
[urlArray addObject:url]; [urlArray addObject:url];
} }
[self share:urlArray atSource:originRect]; [self share:urlArray atSource:originRect withSubject:subject];
result(nil); result(nil);
} }
} else { } else {
...@@ -55,7 +56,7 @@ ...@@ -55,7 +56,7 @@
}]; }];
} }
+ (void)share:(NSArray *)sharedItems atSource:(CGRect)origin { + (void)share:(NSArray *)sharedItems atSource:(CGRect)origin withSubject:(NSString *) subject {
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:sharedItems applicationActivities:nil]; UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:sharedItems applicationActivities:nil];
UIViewController *controller =[UIApplication sharedApplication].keyWindow.rootViewController; UIViewController *controller =[UIApplication sharedApplication].keyWindow.rootViewController;
...@@ -64,6 +65,7 @@ ...@@ -64,6 +65,7 @@
if (!CGRectIsEmpty(origin)) { if (!CGRectIsEmpty(origin)) {
activityViewController.popoverPresentationController.sourceRect = origin; activityViewController.popoverPresentationController.sourceRect = origin;
} }
[activityViewController setValue:subject forKey:@"subject"];
[controller presentViewController:activityViewController animated:YES completion:nil]; [controller presentViewController:activityViewController animated:YES completion:nil];
} }
......
...@@ -21,11 +21,14 @@ class ShareExtend { ...@@ -21,11 +21,14 @@ class ShareExtend {
} }
static Future<void> share(String text, String type, static Future<void> share(String text, String type,
{Rect sharePositionOrigin}) { {Rect sharePositionOrigin, String sharePanelTitle, String subject = ""}) {
assert(text != null); assert(text != null);
assert(text.isNotEmpty); assert(text.isNotEmpty);
List<String> list = [text]; List<String> list = [text];
return _shareInner(list, type, sharePositionOrigin: sharePositionOrigin); return _shareInner(list, type,
sharePositionOrigin: sharePositionOrigin,
sharePanelTitle: sharePanelTitle,
subject: subject);
} }
/// method to share with system ui /// method to share with system ui
...@@ -36,11 +39,13 @@ class ShareExtend { ...@@ -36,11 +39,13 @@ class ShareExtend {
/// [sharePositionOrigin] only supports ios /// [sharePositionOrigin] only supports ios
/// ///
static Future<void> _shareInner(List<String> list, String type, static Future<void> _shareInner(List<String> list, String type,
{Rect sharePositionOrigin}) { {Rect sharePositionOrigin, String sharePanelTitle, String subject}) {
assert(list != null && list.isNotEmpty); assert(list != null && list.isNotEmpty);
final Map<String, dynamic> params = <String, dynamic>{ final Map<String, dynamic> params = <String, dynamic>{
'list': list, 'list': list,
'type': type 'type': type,
'sharePanelTitle': sharePanelTitle,
'subject': subject
}; };
if (sharePositionOrigin != null) { if (sharePositionOrigin != null) {
params['originX'] = sharePositionOrigin.left; params['originX'] = sharePositionOrigin.left;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment