diff --git a/packages/shared_preferences/shared_preferences/android/src/main/java/io/flutter/plugins/sharedpreferences/AsyncHandler.java b/packages/shared_preferences/shared_preferences/android/src/main/java/io/flutter/plugins/sharedpreferences/AsyncHandler.java new file mode 100644 index 000000000000..21df8197da37 --- /dev/null +++ b/packages/shared_preferences/shared_preferences/android/src/main/java/io/flutter/plugins/sharedpreferences/AsyncHandler.java @@ -0,0 +1,75 @@ +package io.flutter.plugins.sharedpreferences; + +import android.os.Handler; +import android.os.Looper; +import java.util.Iterator; +import java.util.ServiceLoader; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadFactory; + +/** Helper class to handle async tasks */ +class AsyncHandler { + private ExecutorService executorService; + + private final Handler handler; + + public AsyncHandler() { + Iterator executorServiceIterator = + ServiceLoader.load(ExecutorService.class).iterator(); + if (executorServiceIterator.hasNext()) { + executorService = executorServiceIterator.next(); + } else { + executorService = + Executors.newSingleThreadExecutor( + new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread thread = new Thread(r, "SharedPreferencesAsync"); + thread.setDaemon(true); + return thread; + } + }); + } + handler = new Handler(Looper.getMainLooper()); + } + + public void executeAsync( + final Callable executeInBackground, final Callback resultCallback) { + executorService.execute( + new Runnable() { + @Override + public void run() { + try { + final R result = executeInBackground.call(); + handler.post( + new Runnable() { + @Override + public void run() { + resultCallback.onComplete(result); + } + }); + } catch (final Exception ex) { + handler.post( + new Runnable() { + @Override + public void run() { + resultCallback.onError(ex); + } + }); + } + } + }); + } + + public interface Callback { + void onError(Exception e); + + void onComplete(R result); + } + + public interface Callable { + + V call() throws Exception; + } +} diff --git a/packages/shared_preferences/shared_preferences/android/src/main/java/io/flutter/plugins/sharedpreferences/MethodCallHandlerImpl.java b/packages/shared_preferences/shared_preferences/android/src/main/java/io/flutter/plugins/sharedpreferences/MethodCallHandlerImpl.java index 33f2474592fa..b3ae51fda223 100644 --- a/packages/shared_preferences/shared_preferences/android/src/main/java/io/flutter/plugins/sharedpreferences/MethodCallHandlerImpl.java +++ b/packages/shared_preferences/shared_preferences/android/src/main/java/io/flutter/plugins/sharedpreferences/MethodCallHandlerImpl.java @@ -6,7 +6,6 @@ import android.content.Context; import android.content.SharedPreferences; -import android.os.AsyncTask; import android.util.Base64; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; @@ -38,12 +37,15 @@ class MethodCallHandlerImpl implements MethodChannel.MethodCallHandler { private final android.content.SharedPreferences preferences; + private final AsyncHandler asyncHandler; + /** * Constructs a {@link MethodCallHandlerImpl} instance. Creates a {@link * android.content.SharedPreferences} based on the {@code context}. */ MethodCallHandlerImpl(Context context) { preferences = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); + asyncHandler = new AsyncHandler(); } @Override @@ -118,17 +120,24 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) { private void commitAsync( final SharedPreferences.Editor editor, final MethodChannel.Result result) { - new AsyncTask() { - @Override - protected Boolean doInBackground(Void... voids) { - return editor.commit(); - } + asyncHandler.executeAsync( + new AsyncHandler.Callable() { + @Override + public Boolean call() { + return editor.commit(); + } + }, + new AsyncHandler.Callback() { + @Override + public void onError(Exception e) { + result.error(e.getClass().getName(), e.getLocalizedMessage(), e.getMessage()); + } - @Override - protected void onPostExecute(Boolean value) { - result.success(value); - } - }.execute(); + @Override + public void onComplete(Boolean resultValue) { + result.success(resultValue); + } + }); } private List decodeList(String encodedList) throws IOException {