Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[shared_preferences] Replace AsyncTask class with AsyncHandler to handle async task for Android #3294

Closed
wants to merge 4 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -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<ExecutorService> 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 <R> void executeAsync(
final Callable<R> executeInBackground, final Callback<R> 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<R> {
void onError(Exception e);

void onComplete(R result);
}

public interface Callable<V> {

V call() throws Exception;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... voids) {
return editor.commit();
}
asyncHandler.executeAsync(
new AsyncHandler.Callable<Boolean>() {
@Override
public Boolean call() {
return editor.commit();
}
},
new AsyncHandler.Callback<Boolean>() {
@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<String> decodeList(String encodedList) throws IOException {
Expand Down