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
private final Registrar mRegistrar;
private List<String> list;
private String type;
private String sharePanelTitle;
private String subject;
public static void registerWith(Registrar registrar) {
MethodChannel channel = new MethodChannel(registrar.messenger(), CHANNEL);
......@@ -50,22 +52,24 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi
throw new IllegalArgumentException("Map argument expected");
}
// 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);
} else {
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()) {
throw new IllegalArgumentException("Non-empty list expected");
}
this.list = list;
this.type = type;
Intent shareIntent = new Intent();
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
if ("text".equals(type)) {
shareIntent.setAction(Intent.ACTION_SEND);
......@@ -101,11 +105,11 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
}
}
startChooserActivity(shareIntent);
startChooserActivity(shareIntent, sharePanelTitle);
}
private void startChooserActivity(Intent shareIntent) {
Intent chooserIntent = Intent.createChooser(shareIntent, null /* dialog title optional */);
private void startChooserActivity(Intent shareIntent,String sharePanelTitle) {
Intent chooserIntent = Intent.createChooser(shareIntent, sharePanelTitle /* dialog subject optional */);
if (mRegistrar.activity() != null) {
mRegistrar.activity().startActivity(chooserIntent);
} else {
......@@ -126,7 +130,7 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi
@Override
public boolean onRequestPermissionsResult(int requestCode, String[] perms, int[] grantResults) {
if (requestCode == CODE_ASK_PERMISSION && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
share(list, type);
share(list, type, sharePanelTitle, subject);
}
return false;
}
......
......@@ -35,7 +35,9 @@ class _MyAppState extends State<MyApp> {
children: <Widget>[
RaisedButton(
onPressed: () {
ShareExtend.share("share text", "text");
ShareExtend.share("share text", "text",
sharePanelTitle: "share text title",
subject: "share text subject");
},
child: Text("share text"),
),
......@@ -44,7 +46,9 @@ class _MyAppState extends State<MyApp> {
File f = await ImagePicker.pickImage(
source: ImageSource.gallery);
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"),
......
......@@ -12,6 +12,7 @@
NSDictionary *arguments = [call arguments];
NSArray *array = arguments[@"list"];
NSString *shareType = arguments[@"type"];
NSString *subject = arguments[@"subject"];
if (array.count == 0) {
result(
......@@ -31,7 +32,7 @@
}
if ([shareType isEqualToString:@"text"]) {
[self share:array atSource:originRect];
[self share:array atSource:originRect withSubject:subject];
result(nil);
} else if ([shareType isEqualToString:@"image"]) {
NSMutableArray * imageArray = [[NSMutableArray alloc] init];
......@@ -39,14 +40,14 @@
UIImage *image = [UIImage imageWithContentsOfFile:path];
[imageArray addObject:image];
}
[self share:imageArray atSource:originRect];
[self share:imageArray atSource:originRect withSubject:subject];
} else {
NSMutableArray * urlArray = [[NSMutableArray alloc] init];
for (NSString * path in array) {
NSURL *url = [NSURL fileURLWithPath:path];
[urlArray addObject:url];
}
[self share:urlArray atSource:originRect];
[self share:urlArray atSource:originRect withSubject:subject];
result(nil);
}
} else {
......@@ -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];
UIViewController *controller =[UIApplication sharedApplication].keyWindow.rootViewController;
......@@ -64,6 +65,7 @@
if (!CGRectIsEmpty(origin)) {
activityViewController.popoverPresentationController.sourceRect = origin;
}
[activityViewController setValue:subject forKey:@"subject"];
[controller presentViewController:activityViewController animated:YES completion:nil];
}
......
......@@ -21,11 +21,14 @@ class ShareExtend {
}
static Future<void> share(String text, String type,
{Rect sharePositionOrigin}) {
{Rect sharePositionOrigin, String sharePanelTitle, String subject = ""}) {
assert(text != null);
assert(text.isNotEmpty);
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
......@@ -36,11 +39,13 @@ class ShareExtend {
/// [sharePositionOrigin] only supports ios
///
static Future<void> _shareInner(List<String> list, String type,
{Rect sharePositionOrigin}) {
{Rect sharePositionOrigin, String sharePanelTitle, String subject}) {
assert(list != null && list.isNotEmpty);
final Map<String, dynamic> params = <String, dynamic>{
'list': list,
'type': type
'type': type,
'sharePanelTitle': sharePanelTitle,
'subject': subject
};
if (sharePositionOrigin != null) {
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