1
+ // Copyright 2017 The Chromium Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style license that can be
3
+ // found in the LICENSE file.
4
+
5
+ package io .flutter .plugins .share ;
6
+
7
+ import android .content .Context ;
8
+ import android .content .Intent ;
9
+
10
+ import io .flutter .app .FlutterActivity ;
11
+ import io .flutter .plugin .common .MethodChannel ;
12
+ import io .flutter .plugin .common .MethodChannel .MethodCallHandler ;
13
+ import io .flutter .plugin .common .MethodChannel .Result ;
14
+ import io .flutter .plugin .common .MethodCall ;
15
+
16
+ /** Plugin method host for presenting a share sheet via Intent */
17
+ public class SharePlugin implements MethodChannel .MethodCallHandler {
18
+
19
+ private static final String PLATFORM_CHANNEL = "plugins.flutter.io/share" ;
20
+
21
+ public static SharePlugin register (FlutterActivity flutterActivity ) {
22
+ return new SharePlugin (flutterActivity );
23
+ }
24
+
25
+ private Context context ;
26
+
27
+ private SharePlugin (FlutterActivity flutterActivity ) {
28
+ context = flutterActivity ;
29
+ new MethodChannel (flutterActivity .getFlutterView (), PLATFORM_CHANNEL )
30
+ .setMethodCallHandler (this );
31
+ }
32
+
33
+ @ Override
34
+ public void onMethodCall (MethodCall call , MethodChannel .Result result ) {
35
+ if (call .method .equals ("share" )) {
36
+ if (!(call .arguments instanceof String )) {
37
+ result .error ("ARGUMENT_ERROR" , "String argument expected" , null );
38
+ return ;
39
+ }
40
+ final String text = (String ) call .arguments ;
41
+ share (text );
42
+ result .success (null );
43
+ } else {
44
+ result .error ("UNKNOWN_METHOD" , "Unknown share method called" , null );
45
+ }
46
+ }
47
+
48
+ private void share (String text ) {
49
+ Intent shareIntent = new Intent ();
50
+ shareIntent .setAction (Intent .ACTION_SEND );
51
+ shareIntent .putExtra (Intent .EXTRA_TEXT , text );
52
+ shareIntent .setType ("text/plain" );
53
+ context .startActivity (Intent .createChooser (shareIntent , null /* dialog title optional */ ));
54
+ }
55
+
56
+ }
0 commit comments