77import android .content .Context ;
88import android .os .Build .VERSION ;
99import android .os .Build .VERSION_CODES ;
10+ import android .os .Handler ;
11+ import android .os .Looper ;
1012import 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 ;
1117import io .flutter .embedding .engine .plugins .FlutterPlugin ;
1218import io .flutter .plugin .common .MethodCall ;
1319import io .flutter .plugin .common .MethodChannel ;
1723import java .io .File ;
1824import java .util .ArrayList ;
1925import java .util .List ;
26+ import java .util .concurrent .Callable ;
27+ import java .util .concurrent .Executor ;
28+ import java .util .concurrent .Executors ;
2029
2130public class PathProviderPlugin implements FlutterPlugin , MethodCallHandler {
2231
2332 private Context context ;
2433 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 ());
2541
2642 public PathProviderPlugin () {}
2743
@@ -46,28 +62,52 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
4662 channel = null ;
4763 }
4864
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+
4989 @ Override
5090 public void onMethodCall (MethodCall call , @ NonNull Result result ) {
5191 switch (call .method ) {
5292 case "getTemporaryDirectory" :
53- result . success ( getPathProviderTemporaryDirectory ());
93+ executeInBackground (() -> getPathProviderTemporaryDirectory (), result );
5494 break ;
5595 case "getApplicationDocumentsDirectory" :
56- result . success ( getPathProviderApplicationDocumentsDirectory ());
96+ executeInBackground (() -> getPathProviderApplicationDocumentsDirectory (), result );
5797 break ;
5898 case "getStorageDirectory" :
59- result . success ( getPathProviderStorageDirectory ());
99+ executeInBackground (() -> getPathProviderStorageDirectory (), result );
60100 break ;
61101 case "getExternalCacheDirectories" :
62- result . success ( getPathProviderExternalCacheDirectories ());
102+ executeInBackground (() -> getPathProviderExternalCacheDirectories (), result );
63103 break ;
64104 case "getExternalStorageDirectories" :
65105 final Integer type = call .argument ("type" );
66106 final String directoryName = StorageDirectoryMapper .androidType (type );
67- result . success ( getPathProviderExternalStorageDirectories (directoryName ));
107+ executeInBackground (() -> getPathProviderExternalStorageDirectories (directoryName ), result );
68108 break ;
69109 case "getApplicationSupportDirectory" :
70- result . success ( getApplicationSupportDirectory ());
110+ executeInBackground (() -> getApplicationSupportDirectory (), result );
71111 break ;
72112 default :
73113 result .notImplemented ();
@@ -131,4 +171,13 @@ private List<String> getPathProviderExternalStorageDirectories(String type) {
131171
132172 return paths ;
133173 }
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+ }
134183}
0 commit comments