Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.it_nomads.fluttersecurestorage;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;

import androidx.annotation.NonNull;
Expand All @@ -10,6 +9,8 @@
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
Expand All @@ -21,30 +22,30 @@ public class FlutterSecureStoragePlugin implements MethodCallHandler, FlutterPlu

private MethodChannel channel;
private FlutterSecureStorage secureStorage;
private HandlerThread workerThread;
private Handler workerThreadHandler;
private ExecutorService executor;
private FlutterPluginBinding binding;

@Override
public void onAttachedToEngine(FlutterPluginBinding binding) {
this.binding = binding;
workerThread = new HandlerThread("fluttersecurestorage.worker");
workerThread.start();
workerThreadHandler = new Handler(workerThread.getLooper());
int availableCores = Runtime.getRuntime().availableProcessors();
int threadCount = Math.min(4, Math.max(2, availableCores));
this.executor = Executors.newFixedThreadPool(threadCount);

channel = new MethodChannel(binding.getBinaryMessenger(), "plugins.it_nomads.com/flutter_secure_storage");
channel.setMethodCallHandler(this);
}

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
if (channel != null) {
if (workerThread != null) {
workerThread.quitSafely();
workerThread = null;
}
channel.setMethodCallHandler(null);
channel = null;
}
if (executor != null) {
executor.shutdown();
executor = null;
}
secureStorage = null;
}

Expand All @@ -69,7 +70,7 @@ private boolean initSecureStorage(Result result, Map<String, Object> options) {
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result rawResult) {
MethodResultWrapper result = new MethodResultWrapper(rawResult);
workerThreadHandler.post(new MethodRunner(call, result));
executor.execute(new MethodRunner(call, result));
}

class MethodRunner implements Runnable {
Expand Down