-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #489 from ably/more-safe-channel-wrapper
feat: protect from multiple `MethodChannel.Result` invocation
- Loading branch information
Showing
2 changed files
with
58 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
android/src/main/java/io/ably/flutter/plugin/MainThreadMethodResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.ably.flutter.plugin; | ||
|
||
import android.os.Handler; | ||
import android.os.Looper; | ||
|
||
import io.ably.lib.util.Log; | ||
import io.flutter.plugin.common.MethodChannel; | ||
|
||
// MethodChannel.Result wrapper that responds on the platform thread. | ||
// | ||
// Plugins crash with "Methods marked with @UiThread must be executed on the main thread." | ||
// This happens while making network calls in thread other than main thread | ||
// | ||
// https://github.com/flutter/flutter/issues/34993#issue-459900986 | ||
// https://github.com/aloisdeniel/flutter_geocoder/commit/bc34cfe473bfd1934fe098bb7053248b75200241 | ||
public class MainThreadMethodResult implements MethodChannel.Result { | ||
private static final String TAG = MainThreadMethodResult.class.getName(); | ||
private final String methodName; | ||
|
||
private final MethodChannel.Result methodResult; | ||
private final Handler handler; | ||
|
||
MainThreadMethodResult(String methodName, MethodChannel.Result result) { | ||
this.methodName = methodName; | ||
methodResult = result; | ||
handler = new Handler(Looper.getMainLooper()); | ||
} | ||
|
||
@Override | ||
public void success(final Object result) { | ||
handler.post(() -> { | ||
try { | ||
methodResult.success(result); | ||
} catch (Exception e) { | ||
Log.e(TAG, String.format("\"%s\" platform method received error during invocation, caused by: %s", methodName, e.getMessage()), e); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void error( | ||
final String errorCode, final String errorMessage, final Object errorDetails) { | ||
handler.post(() -> { | ||
try { | ||
methodResult.error(errorCode, errorMessage, errorDetails); | ||
} catch (Exception e) { | ||
Log.e(TAG, String.format("\"%s\" platform method received error during invocation, caused by: %s", methodName, e.getMessage()), e); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void notImplemented() { | ||
handler.post(methodResult::notImplemented); | ||
} | ||
} |