Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/path_provider/path_provider/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.6.19

* Android implementation does path queries in the background thread rather than UI thread.

## 1.6.18

* Keep handling deprecated Android v1 classes for backward compatibility.
Expand Down
7 changes: 7 additions & 0 deletions packages/path_provider/path_provider/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ android {
lintOptions {
disable 'InvalidPackage'
}
android {
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}
}

dependencies {
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'com.google.guava:guava:20.0'
testImplementation 'junit:junit:4.12'
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
import android.content.Context;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.SettableFuture;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
Expand All @@ -17,11 +23,21 @@
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class PathProviderPlugin implements FlutterPlugin, MethodCallHandler {

private Context context;
private MethodChannel channel;
private final Executor uiThreadExecutor = new UiThreadExecutor();
private final Executor executor =
Executors.newSingleThreadExecutor(
new ThreadFactoryBuilder()
.setNameFormat("path-provider-background-%d")
.setPriority(Thread.NORM_PRIORITY)
.build());

public PathProviderPlugin() {}

Expand All @@ -46,28 +62,52 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel = null;
}

private <T> void executeInBackground(Callable<T> task, Result result) {
final SettableFuture<T> future = SettableFuture.create();
Futures.addCallback(
future,
new FutureCallback<T>() {
public void onSuccess(T answer) {
result.success(answer);
}

public void onFailure(Throwable t) {
result.error(t.getClass().getName(), t.getMessage(), null);
}
},
uiThreadExecutor);
executor.execute(
() -> {
try {
future.set(task.call());
} catch (Throwable t) {
future.setException(t);
}
});
}

@Override
public void onMethodCall(MethodCall call, @NonNull Result result) {
switch (call.method) {
case "getTemporaryDirectory":
result.success(getPathProviderTemporaryDirectory());
executeInBackground(() -> getPathProviderTemporaryDirectory(), result);
break;
case "getApplicationDocumentsDirectory":
result.success(getPathProviderApplicationDocumentsDirectory());
executeInBackground(() -> getPathProviderApplicationDocumentsDirectory(), result);
break;
case "getStorageDirectory":
result.success(getPathProviderStorageDirectory());
executeInBackground(() -> getPathProviderStorageDirectory(), result);
break;
case "getExternalCacheDirectories":
result.success(getPathProviderExternalCacheDirectories());
executeInBackground(() -> getPathProviderExternalCacheDirectories(), result);
break;
case "getExternalStorageDirectories":
final Integer type = call.argument("type");
final String directoryName = StorageDirectoryMapper.androidType(type);
result.success(getPathProviderExternalStorageDirectories(directoryName));
executeInBackground(() -> getPathProviderExternalStorageDirectories(directoryName), result);
break;
case "getApplicationSupportDirectory":
result.success(getApplicationSupportDirectory());
executeInBackground(() -> getApplicationSupportDirectory(), result);
break;
default:
result.notImplemented();
Expand Down Expand Up @@ -131,4 +171,13 @@ private List<String> getPathProviderExternalStorageDirectories(String type) {

return paths;
}

private static class UiThreadExecutor implements Executor {
private final Handler handler = new Handler(Looper.getMainLooper());

@Override
public void execute(Runnable command) {
handler.post(command);
}
}
}
2 changes: 1 addition & 1 deletion packages/path_provider/path_provider/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: path_provider
description: Flutter plugin for getting commonly used locations on host platform file systems, such as the temp and app data directories.
homepage: https://github.com/flutter/plugins/tree/master/packages/path_provider/path_provider
version: 1.6.18
version: 1.6.19

flutter:
plugin:
Expand Down