Skip to content

Commit

Permalink
[shared_preferences] Removed deprecated AsyncTask API (flutter#3481)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirusCodes authored Feb 23, 2021
1 parent 7413abf commit 73aefe6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
4 changes: 4 additions & 0 deletions packages/shared_preferences/shared_preferences/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.1

* Removed deprecated [AsyncTask](https://developer.android.com/reference/android/os/AsyncTask) was deprecated in API level 30 ([#3481](https://github.com/flutter/plugins/pull/3481))

## 2.0.0

* Migrate to null-safety.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.util.Base64;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
Expand All @@ -21,6 +22,10 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* Implementation of the {@link MethodChannel.MethodCallHandler} for the plugin. It is also
Expand Down Expand Up @@ -118,17 +123,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();
}
final ExecutorService executor =
new ThreadPoolExecutor(0, 1, 30L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
final Handler handler = new Handler(Looper.getMainLooper());

@Override
protected void onPostExecute(Boolean value) {
result.success(value);
}
}.execute();
executor.execute(
new Runnable() {
@Override
public void run() {
final boolean response = editor.commit();
handler.post(
new Runnable() {
@Override
public void run() {
result.success(response);
}
});
}
});
}

private List<String> decodeList(String encodedList) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: shared_preferences
description: Flutter plugin for reading and writing simple key-value pairs.
Wraps NSUserDefaults on iOS and SharedPreferences on Android.
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences
version: 2.0.0
version: 2.0.1

flutter:
plugin:
Expand Down

0 comments on commit 73aefe6

Please sign in to comment.