Skip to content

Commit

Permalink
Android fixes (#134)
Browse files Browse the repository at this point in the history
* ensure to free UploadObserver objects quickly
* Ensure to call back on UI thread
  • Loading branch information
ened authored Dec 26, 2020
1 parent c955965 commit ddb7c3b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LiveData;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;
import com.bluechilli.flutteruploader.plugin.CachingStreamHandler;
import com.bluechilli.flutteruploader.plugin.StatusListener;
Expand All @@ -18,6 +20,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** FlutterUploaderPlugin */
Expand All @@ -38,13 +41,14 @@ public class FlutterUploaderPlugin implements FlutterPlugin, StatusListener {
private EventChannel resultEventChannel;
private final CachingStreamHandler<Map<String, Object>> resultStreamHandler =
new CachingStreamHandler<>();
private LiveData<List<WorkInfo>> workInfoLiveData;

public static void registerWith(Registrar registrar) {
final FlutterUploaderPlugin plugin = new FlutterUploaderPlugin();
plugin.startListening(registrar.context(), registrar.messenger());
registrar.addViewDestroyListener(
view -> {
plugin.stopListening(registrar.context());
plugin.stopListening();
return false;
});
}
Expand All @@ -56,7 +60,7 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
stopListening(binding.getApplicationContext());
stopListening();
}

private void startListening(Context context, BinaryMessenger messenger) {
Expand All @@ -66,9 +70,9 @@ private void startListening(Context context, BinaryMessenger messenger) {
methodCallHandler = new MethodCallHandlerImpl(context, timeout, this);

uploadObserver = new UploadObserver(this);
WorkManager.getInstance(context)
.getWorkInfosByTagLiveData(FLUTTER_UPLOAD_WORK_TAG)
.observeForever(uploadObserver);
workInfoLiveData =
WorkManager.getInstance(context).getWorkInfosByTagLiveData(FLUTTER_UPLOAD_WORK_TAG);
workInfoLiveData.observeForever(uploadObserver);

channel.setMethodCallHandler(methodCallHandler);

Expand All @@ -79,14 +83,13 @@ private void startListening(Context context, BinaryMessenger messenger) {
resultEventChannel.setStreamHandler(resultStreamHandler);
}

private void stopListening(Context context) {
private void stopListening() {
channel.setMethodCallHandler(null);
channel = null;

if (uploadObserver != null) {
WorkManager.getInstance(context)
.getWorkInfosByTagLiveData(FLUTTER_UPLOAD_WORK_TAG)
.removeObserver(uploadObserver);
workInfoLiveData.removeObserver(uploadObserver);
workInfoLiveData = null;
uploadObserver = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ private void enqueue(MethodCall call, MethodChannel.Result result) {
mainExecutor.execute(
() -> {
result.success(taskId);
statusListener.onUpdateProgress(taskId, UploadStatus.ENQUEUED, 0);
});
statusListener.onUpdateProgress(taskId, UploadStatus.ENQUEUED, 0);
},
workManagerExecutor);
}
Expand Down

0 comments on commit ddb7c3b

Please sign in to comment.