Skip to content

Commit

Permalink
fix(messaging): Revert "feat(messaging): use FlutterEngineGroup to im…
Browse files Browse the repository at this point in the history
…prove performance of background handlers"

This reverts commit 2e9deac.
  • Loading branch information
Lyokone committed Nov 16, 2022
1 parent 4c72b54 commit 8cd90b1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import androidx.annotation.NonNull;
import com.google.firebase.messaging.RemoteMessage;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineGroup;
import io.flutter.embedding.engine.FlutterShellArgs;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.dart.DartExecutor.DartCallback;
import io.flutter.embedding.engine.loader.FlutterLoader;
Expand All @@ -24,6 +24,7 @@
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.view.FlutterCallbackInformation;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -114,7 +115,7 @@ public void startBackgroundIsolate() {
if (isNotRunning()) {
long callbackHandle = getPluginCallbackHandle();
if (callbackHandle != 0) {
startBackgroundIsolate(callbackHandle);
startBackgroundIsolate(callbackHandle, null);
}
}
}
Expand All @@ -137,7 +138,7 @@ public void startBackgroundIsolate() {
* handle does not resolve to a Dart callback then this method does nothing.
* </ul>
*/
public void startBackgroundIsolate(long callbackHandle) {
public void startBackgroundIsolate(long callbackHandle, FlutterShellArgs shellArgs) {
if (backgroundFlutterEngine != null) {
Log.e(TAG, "Background isolate already started.");
return;
Expand All @@ -156,10 +157,19 @@ public void startBackgroundIsolate(long callbackHandle) {
String appBundlePath = loader.findAppBundlePath();
AssetManager assets = ContextHolder.getApplicationContext().getAssets();
if (isNotRunning()) {
Log.i(TAG, "Creating background FlutterEngine instance.");
backgroundFlutterEngine =
new FlutterEngineGroup(ContextHolder.getApplicationContext())
.createAndRunDefaultEngine(ContextHolder.getApplicationContext());
if (shellArgs != null) {
Log.i(
TAG,
"Creating background FlutterEngine instance, with args: "
+ Arrays.toString(shellArgs.toArray()));
backgroundFlutterEngine =
new FlutterEngine(
ContextHolder.getApplicationContext(), shellArgs.toArray());
} else {
Log.i(TAG, "Creating background FlutterEngine instance.");
backgroundFlutterEngine =
new FlutterEngine(ContextHolder.getApplicationContext());
}
// We need to create an instance of `FlutterEngine` before looking up the
// callback. If we don't, the callback cache won't be initialized and the
// lookup will fail.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.firebase.messaging.RemoteMessage;
import io.flutter.embedding.engine.FlutterShellArgs;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -51,13 +52,13 @@ public static void enqueueMessageProcessing(Context context, Intent messageInten
* </ul>
*/
@SuppressWarnings("JavadocReference")
public static void startBackgroundIsolate(long callbackHandle) {
public static void startBackgroundIsolate(long callbackHandle, FlutterShellArgs shellArgs) {
if (flutterBackgroundExecutor != null) {
Log.w(TAG, "Attempted to start a duplicate background isolate. Returning...");
return;
}
flutterBackgroundExecutor = new FlutterFirebaseMessagingBackgroundExecutor();
flutterBackgroundExecutor.startBackgroundIsolate(callbackHandle);
flutterBackgroundExecutor.startBackgroundIsolate(callbackHandle, shellArgs);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.firebase.FirebaseApp;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.RemoteMessage;
import io.flutter.embedding.engine.FlutterShellArgs;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
Expand Down Expand Up @@ -429,9 +430,19 @@ public void onMethodCall(final MethodCall call, @NonNull final Result result) {
userCallbackHandle = Long.valueOf((Integer) arg2);
}

FlutterShellArgs shellArgs = null;
if (mainActivity != null) {
// Supports both Flutter Activity types:
// io.flutter.embedding.android.FlutterFragmentActivity
// io.flutter.embedding.android.FlutterActivity
// We could use `getFlutterShellArgs()` but this is only available on `FlutterActivity`.
shellArgs = FlutterShellArgs.fromIntent(mainActivity.getIntent());
}

FlutterFirebaseMessagingBackgroundService.setCallbackDispatcher(pluginCallbackHandle);
FlutterFirebaseMessagingBackgroundService.setUserCallbackHandle(userCallbackHandle);
FlutterFirebaseMessagingBackgroundService.startBackgroundIsolate(pluginCallbackHandle);
FlutterFirebaseMessagingBackgroundService.startBackgroundIsolate(
pluginCallbackHandle, shellArgs);
methodCallTask = Tasks.forResult(null);
break;
case "Messaging#getInitialMessage":
Expand Down

0 comments on commit 8cd90b1

Please sign in to comment.