Skip to content

Commit

Permalink
Return image picker method call results on the platform thread
Browse files Browse the repository at this point in the history
The MediaScanConnection.scanFile callback is invoked on a background thread.
However, MethodChannel results must be provided on the platform thread.

Fixes flutter/flutter#32315
  • Loading branch information
jason-simmons committed May 9, 2019
1 parent a7c074c commit 70b9bfc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import android.app.Application;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.VisibleForTesting;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
Expand Down Expand Up @@ -94,12 +96,58 @@ public void onActivityStopped(Activity activity) {}
}
}

// MethodChannel.Result wrapper that responds on the platform thread.
private static class MethodResultWrapper implements MethodChannel.Result {
private MethodChannel.Result methodResult;
private Handler handler;

MethodResultWrapper(MethodChannel.Result result) {
methodResult = result;
handler = new Handler(Looper.getMainLooper());
}

@Override
public void success(final Object result) {
handler.post(
new Runnable() {
@Override
public void run() {
methodResult.success(result);
}
});
}

@Override
public void error(
final String errorCode, final String errorMessage, final Object errorDetails) {
handler.post(
new Runnable() {
@Override
public void run() {
methodResult.error(errorCode, errorMessage, errorDetails);
}
});
}

@Override
public void notImplemented() {
handler.post(
new Runnable() {
@Override
public void run() {
methodResult.notImplemented();
}
});
}
}

@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
public void onMethodCall(MethodCall call, MethodChannel.Result rawResult) {
if (registrar.activity() == null) {
result.error("no_activity", "image_picker plugin requires a foreground activity.", null);
rawResult.error("no_activity", "image_picker plugin requires a foreground activity.", null);
return;
}
MethodChannel.Result result = new MethodResultWrapper(rawResult);
int imageSource;
switch (call.method) {
case METHOD_CALL_IMAGE:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.flutter.plugins.imagepicker;

import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -81,7 +83,7 @@ public void onMethodCall_WhenSourceIsGallery_InvokesChooseImageFromGallery() {

plugin.onMethodCall(call, mockResult);

verify(mockImagePickerDelegate).chooseImageFromGallery(call, mockResult);
verify(mockImagePickerDelegate).chooseImageFromGallery(eq(call), any());
verifyZeroInteractions(mockResult);
}

Expand All @@ -92,7 +94,7 @@ public void onMethodCall_WhenSourceIsCamera_InvokesTakeImageWithCamera() {

plugin.onMethodCall(call, mockResult);

verify(mockImagePickerDelegate).takeImageWithCamera(call, mockResult);
verify(mockImagePickerDelegate).takeImageWithCamera(eq(call), any());
verifyZeroInteractions(mockResult);
}

Expand Down

0 comments on commit 70b9bfc

Please sign in to comment.