7
7
import android .content .Context ;
8
8
import android .os .Build .VERSION ;
9
9
import android .os .Build .VERSION_CODES ;
10
+ import android .os .Handler ;
11
+ import android .os .Looper ;
10
12
import androidx .annotation .NonNull ;
13
+ import com .google .common .util .concurrent .FutureCallback ;
14
+ import com .google .common .util .concurrent .Futures ;
15
+ import com .google .common .util .concurrent .SettableFuture ;
16
+ import com .google .common .util .concurrent .ThreadFactoryBuilder ;
11
17
import io .flutter .embedding .engine .plugins .FlutterPlugin ;
12
18
import io .flutter .plugin .common .MethodCall ;
13
19
import io .flutter .plugin .common .MethodChannel ;
17
23
import java .io .File ;
18
24
import java .util .ArrayList ;
19
25
import java .util .List ;
26
+ import java .util .concurrent .Callable ;
27
+ import java .util .concurrent .Executor ;
28
+ import java .util .concurrent .Executors ;
20
29
21
30
public class PathProviderPlugin implements FlutterPlugin , MethodCallHandler {
22
31
23
32
private Context context ;
24
33
private MethodChannel channel ;
34
+ private final Executor uiThreadExecutor = new UiThreadExecutor ();
35
+ private final Executor executor =
36
+ Executors .newSingleThreadExecutor (
37
+ new ThreadFactoryBuilder ()
38
+ .setNameFormat ("path-provider-background-%d" )
39
+ .setPriority (Thread .NORM_PRIORITY )
40
+ .build ());
25
41
26
42
public PathProviderPlugin () {}
27
43
@@ -46,28 +62,52 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
46
62
channel = null ;
47
63
}
48
64
65
+ private <T > void executeInBackground (Callable <T > task , Result result ) {
66
+ final SettableFuture <T > future = SettableFuture .create ();
67
+ Futures .addCallback (
68
+ future ,
69
+ new FutureCallback <T >() {
70
+ public void onSuccess (T answer ) {
71
+ result .success (answer );
72
+ }
73
+
74
+ public void onFailure (Throwable t ) {
75
+ result .error (t .getClass ().getName (), t .getMessage (), null );
76
+ }
77
+ },
78
+ uiThreadExecutor );
79
+ executor .execute (
80
+ () -> {
81
+ try {
82
+ future .set (task .call ());
83
+ } catch (Throwable t ) {
84
+ future .setException (t );
85
+ }
86
+ });
87
+ }
88
+
49
89
@ Override
50
90
public void onMethodCall (MethodCall call , @ NonNull Result result ) {
51
91
switch (call .method ) {
52
92
case "getTemporaryDirectory" :
53
- result . success ( getPathProviderTemporaryDirectory ());
93
+ executeInBackground (() -> getPathProviderTemporaryDirectory (), result );
54
94
break ;
55
95
case "getApplicationDocumentsDirectory" :
56
- result . success ( getPathProviderApplicationDocumentsDirectory ());
96
+ executeInBackground (() -> getPathProviderApplicationDocumentsDirectory (), result );
57
97
break ;
58
98
case "getStorageDirectory" :
59
- result . success ( getPathProviderStorageDirectory ());
99
+ executeInBackground (() -> getPathProviderStorageDirectory (), result );
60
100
break ;
61
101
case "getExternalCacheDirectories" :
62
- result . success ( getPathProviderExternalCacheDirectories ());
102
+ executeInBackground (() -> getPathProviderExternalCacheDirectories (), result );
63
103
break ;
64
104
case "getExternalStorageDirectories" :
65
105
final Integer type = call .argument ("type" );
66
106
final String directoryName = StorageDirectoryMapper .androidType (type );
67
- result . success ( getPathProviderExternalStorageDirectories (directoryName ));
107
+ executeInBackground (() -> getPathProviderExternalStorageDirectories (directoryName ), result );
68
108
break ;
69
109
case "getApplicationSupportDirectory" :
70
- result . success ( getApplicationSupportDirectory ());
110
+ executeInBackground (() -> getApplicationSupportDirectory (), result );
71
111
break ;
72
112
default :
73
113
result .notImplemented ();
@@ -131,4 +171,13 @@ private List<String> getPathProviderExternalStorageDirectories(String type) {
131
171
132
172
return paths ;
133
173
}
174
+
175
+ private static class UiThreadExecutor implements Executor {
176
+ private final Handler handler = new Handler (Looper .getMainLooper ());
177
+
178
+ @ Override
179
+ public void execute (Runnable command ) {
180
+ handler .post (command );
181
+ }
182
+ }
134
183
}
0 commit comments