This repository was archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathEsysFlutterSharePlugin.java
More file actions
138 lines (111 loc) · 5.38 KB
/
Copy pathEsysFlutterSharePlugin.java
File metadata and controls
138 lines (111 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package de.esys.esysfluttershare;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.util.Log;
import androidx.core.content.FileProvider;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
/**
* EsysFlutterSharePlugin
*/
public class EsysFlutterSharePlugin implements MethodCallHandler {
private final String PROVIDER_AUTH_EXT = ".fileprovider.github.com/orgs/esysberlin/esys-flutter-share";
private Registrar _registrar;
private EsysFlutterSharePlugin(Registrar registrar) {
this._registrar = registrar;
}
/**
* Plugin registration.
*/
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "channel:github.com/orgs/esysberlin/esys-flutter-share");
channel.setMethodCallHandler(new EsysFlutterSharePlugin(registrar));
}
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("text")) {
text(call.arguments);
}
if (call.method.equals("file")) {
file(call.arguments);
}
if (call.method.equals("files")) {
files(call.arguments);
}
}
private void text(Object arguments) {
@SuppressWarnings("unchecked")
HashMap<String, String> argsMap = (HashMap<String, String>) arguments;
String title = argsMap.get("title");
String text = argsMap.get("text");
String mimeType = argsMap.get("mimeType");
Context activeContext = _registrar.activeContext();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType(mimeType);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
activeContext.startActivity(Intent.createChooser(shareIntent, title));
}
private void file(Object arguments) {
@SuppressWarnings("unchecked")
HashMap<String, String> argsMap = (HashMap<String, String>) arguments;
String title = argsMap.get("title");
String name = argsMap.get("name");
String mimeType = argsMap.get("mimeType");
String text = argsMap.get("text");
Context activeContext = _registrar.activeContext();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType(mimeType);
File file = new File(activeContext.getCacheDir(), name);
String fileProviderAuthority = activeContext.getPackageName() + PROVIDER_AUTH_EXT;
Uri contentUri = FileProvider.getUriForFile(activeContext, fileProviderAuthority, file);
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
// add optional text
if (!text.isEmpty()) shareIntent.putExtra(Intent.EXTRA_TEXT, text);
Intent chooser = Intent.createChooser(shareIntent, title);
List<ResolveInfo> resInfoList = activeContext.getPackageManager().queryIntentActivities(chooser, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
activeContext.grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
activeContext.startActivity(chooser);
}
private void files(Object arguments) {
@SuppressWarnings("unchecked")
HashMap<String, Object> argsMap = (HashMap<String, Object>) arguments;
String title = (String) argsMap.get("title");
@SuppressWarnings("unchecked")
ArrayList<String> names = (ArrayList<String>) argsMap.get("names");
String mimeType = (String) argsMap.get("mimeType");
String text = (String) argsMap.get("text");
Context activeContext = _registrar.activeContext();
Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
shareIntent.setType(mimeType);
ArrayList<Uri> contentUris = new ArrayList<>();
for (String name : names) {
File file = new File(activeContext.getCacheDir(), name);
String fileProviderAuthority = activeContext.getPackageName() + PROVIDER_AUTH_EXT;
contentUris.add(FileProvider.getUriForFile(activeContext, fileProviderAuthority, file));
}
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, contentUris);
// add optional text
if (!text.isEmpty()) shareIntent.putExtra(Intent.EXTRA_TEXT, text);
Intent chooser = Intent.createChooser(shareIntent, title);
List<ResolveInfo> resInfoList = activeContext.getPackageManager().queryIntentActivities(chooser, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
for(Uri uri: contentUris){
activeContext.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
activeContext.startActivity(chooser); }
}