Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Prev Previous commit
Next Next commit
Ignore LambdaLast
  • Loading branch information
GaryQian committed May 31, 2022
commit 29c00ddcc17ab205f1b5ce8e918404f654f341ee
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.tracing.Trace;
import io.flutter.Log;
import io.flutter.embedding.android.ExclusiveAppComponent;
import io.flutter.embedding.engine.loader.FlutterLoader;
Expand All @@ -32,7 +33,6 @@
import io.flutter.embedding.engine.plugins.service.ServiceAware;
import io.flutter.embedding.engine.plugins.service.ServiceControlSurface;
import io.flutter.embedding.engine.plugins.service.ServicePluginBinding;
import io.flutter.util.TraceSection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -105,8 +105,8 @@
flutterEngine,
flutterEngine.getDartExecutor(),
flutterEngine.getRenderer(),
new DefaultFlutterAssets(flutterLoader),
flutterEngine.getPlatformViewsController().getRegistry());
flutterEngine.getPlatformViewsController().getRegistry(),
new DefaultFlutterAssets(flutterLoader));
}

public void destroy() {
Expand All @@ -123,7 +123,8 @@ public void destroy() {

@Override
public void add(@NonNull FlutterPlugin plugin) {
TraceSection.begin("FlutterEngineConnectionRegistry#add " + plugin.getClass().getSimpleName());
Trace.beginSection("FlutterEngineConnectionRegistry#add " + plugin.getClass().getSimpleName());

try {
if (has(plugin.getClass())) {
Log.w(
Expand Down Expand Up @@ -191,7 +192,7 @@ public void add(@NonNull FlutterPlugin plugin) {
}
}
} finally {
TraceSection.end();
Trace.endSection();
}
}

Expand Down Expand Up @@ -219,8 +220,10 @@ public void remove(@NonNull Class<? extends FlutterPlugin> pluginClass) {
return;
}

TraceSection.begin("FlutterEngineConnectionRegistry#remove " + pluginClass.getSimpleName());
Trace.beginSection("FlutterEngineConnectionRegistry#remove " + pluginClass.getSimpleName());

try {
Log.v(TAG, "Removing plugin: " + plugin);
// For ActivityAware plugins, notify the plugin that it is detached from
// an Activity if an Activity is currently attached to this engine. Then
// remove the plugin from our set of ActivityAware plugins.
Expand Down Expand Up @@ -270,7 +273,7 @@ public void remove(@NonNull Class<? extends FlutterPlugin> pluginClass) {
plugin.onDetachedFromEngine(pluginBinding);
plugins.remove(pluginClass);
} finally {
TraceSection.end();
Trace.endSection();
}
}

Expand Down Expand Up @@ -313,8 +316,16 @@ private Activity attachedActivity() {
@Override
public void attachToActivity(
@NonNull ExclusiveAppComponent<Activity> exclusiveActivity, @NonNull Lifecycle lifecycle) {
TraceSection.begin("FlutterEngineConnectionRegistry#attachToActivity");
Trace.beginSection("FlutterEngineConnectionRegistry#attachToActivity");

try {
Log.v(
TAG,
"Attaching to an exclusive Activity: "
+ exclusiveActivity.getAppComponent()
+ (isAttachedToActivity() ? " evicting previous activity " + attachedActivity() : "")
+ "."
+ (isWaitingForActivityReattachment ? " This is after a config change." : ""));
if (this.exclusiveActivity != null) {
this.exclusiveActivity.detachFromFlutterEngine();
}
Expand All @@ -323,19 +334,13 @@ public void attachToActivity(
this.exclusiveActivity = exclusiveActivity;
attachToActivityInternal(exclusiveActivity.getAppComponent(), lifecycle);
} finally {
TraceSection.end();
Trace.endSection();
}
}

private void attachToActivityInternal(@NonNull Activity activity, @NonNull Lifecycle lifecycle) {
this.activityPluginBinding = new FlutterEngineActivityPluginBinding(activity, lifecycle);

final boolean useSoftwareRendering =
activity
.getIntent()
.getBooleanExtra(FlutterShellArgs.ARG_KEY_ENABLE_SOFTWARE_RENDERING, false);
flutterEngine.getPlatformViewsController().setSoftwareRendering(useSoftwareRendering);

// Activate the PlatformViewsController. This must happen before any plugins attempt
// to use it, otherwise an error stack trace will appear that says there is no
// flutter/platform_views channel.
Expand All @@ -357,7 +362,9 @@ private void attachToActivityInternal(@NonNull Activity activity, @NonNull Lifec
@Override
public void detachFromActivityForConfigChanges() {
if (isAttachedToActivity()) {
TraceSection.begin("FlutterEngineConnectionRegistry#detachFromActivityForConfigChanges");
Trace.beginSection("FlutterEngineConnectionRegistry#detachFromActivityForConfigChanges");
Log.v(TAG, "Detaching from an Activity for config changes: " + attachedActivity());

try {
isWaitingForActivityReattachment = true;

Expand All @@ -367,7 +374,7 @@ public void detachFromActivityForConfigChanges() {

detachFromActivityInternal();
} finally {
TraceSection.end();
Trace.endSection();
}
} else {
Log.e(TAG, "Attempted to detach plugins from an Activity when no Activity was attached.");
Expand All @@ -377,15 +384,17 @@ public void detachFromActivityForConfigChanges() {
@Override
public void detachFromActivity() {
if (isAttachedToActivity()) {
TraceSection.begin("FlutterEngineConnectionRegistry#detachFromActivity");
Trace.beginSection("FlutterEngineConnectionRegistry#detachFromActivity");

try {
Log.v(TAG, "Detaching from an Activity: " + attachedActivity());
for (ActivityAware activityAware : activityAwarePlugins.values()) {
activityAware.onDetachedFromActivity();
}

detachFromActivityInternal();
} finally {
TraceSection.end();
Trace.endSection();
}
} else {
Log.e(TAG, "Attempted to detach plugins from an Activity when no Activity was attached.");
Expand All @@ -403,13 +412,15 @@ private void detachFromActivityInternal() {
@Override
public boolean onRequestPermissionsResult(
int requestCode, @NonNull String[] permissions, @NonNull int[] grantResult) {
Log.v(TAG, "Forwarding onRequestPermissionsResult() to plugins.");
if (isAttachedToActivity()) {
TraceSection.begin("FlutterEngineConnectionRegistry#onRequestPermissionsResult");
Trace.beginSection("FlutterEngineConnectionRegistry#onRequestPermissionsResult");

try {
return activityPluginBinding.onRequestPermissionsResult(
requestCode, permissions, grantResult);
} finally {
TraceSection.end();
Trace.endSection();
}
} else {
Log.e(
Expand All @@ -422,12 +433,14 @@ public boolean onRequestPermissionsResult(

@Override
public boolean onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
Log.v(TAG, "Forwarding onActivityResult() to plugins.");
if (isAttachedToActivity()) {
TraceSection.begin("FlutterEngineConnectionRegistry#onActivityResult");
Trace.beginSection("FlutterEngineConnectionRegistry#onActivityResult");

try {
return activityPluginBinding.onActivityResult(requestCode, resultCode, data);
} finally {
TraceSection.end();
Trace.endSection();
}
} else {
Log.e(
Expand All @@ -440,12 +453,14 @@ public boolean onActivityResult(int requestCode, int resultCode, @Nullable Inten

@Override
public void onNewIntent(@NonNull Intent intent) {
Log.v(TAG, "Forwarding onNewIntent() to plugins.");
if (isAttachedToActivity()) {
TraceSection.begin("FlutterEngineConnectionRegistry#onNewIntent");
Trace.beginSection("FlutterEngineConnectionRegistry#onNewIntent");

try {
activityPluginBinding.onNewIntent(intent);
} finally {
TraceSection.end();
Trace.endSection();
}
} else {
Log.e(
Expand All @@ -457,12 +472,14 @@ public void onNewIntent(@NonNull Intent intent) {

@Override
public void onUserLeaveHint() {
Log.v(TAG, "Forwarding onUserLeaveHint() to plugins.");
if (isAttachedToActivity()) {
TraceSection.begin("FlutterEngineConnectionRegistry#onUserLeaveHint");
Trace.beginSection("FlutterEngineConnectionRegistry#onUserLeaveHint");

try {
activityPluginBinding.onUserLeaveHint();
} finally {
TraceSection.end();
Trace.endSection();
}
} else {
Log.e(
Expand All @@ -474,12 +491,14 @@ public void onUserLeaveHint() {

@Override
public void onSaveInstanceState(@NonNull Bundle bundle) {
Log.v(TAG, "Forwarding onSaveInstanceState() to plugins.");
if (isAttachedToActivity()) {
TraceSection.begin("FlutterEngineConnectionRegistry#onSaveInstanceState");
Trace.beginSection("FlutterEngineConnectionRegistry#onSaveInstanceState");

try {
activityPluginBinding.onSaveInstanceState(bundle);
} finally {
TraceSection.end();
Trace.endSection();
}
} else {
Log.e(
Expand All @@ -491,12 +510,14 @@ public void onSaveInstanceState(@NonNull Bundle bundle) {

@Override
public void onRestoreInstanceState(@Nullable Bundle bundle) {
Log.v(TAG, "Forwarding onRestoreInstanceState() to plugins.");
if (isAttachedToActivity()) {
TraceSection.begin("FlutterEngineConnectionRegistry#onRestoreInstanceState");
Trace.beginSection("FlutterEngineConnectionRegistry#onRestoreInstanceState");

try {
activityPluginBinding.onRestoreInstanceState(bundle);
} finally {
TraceSection.end();
Trace.endSection();
}
} else {
Log.e(
Expand All @@ -515,7 +536,9 @@ private boolean isAttachedToService() {
@Override
public void attachToService(
@NonNull Service service, @Nullable Lifecycle lifecycle, boolean isForeground) {
TraceSection.begin("FlutterEngineConnectionRegistry#attachToService");
Trace.beginSection("FlutterEngineConnectionRegistry#attachToService");
Log.v(TAG, "Attaching to a Service: " + service);

try {
// If we were already attached to an Android component, detach from it.
detachFromAppComponent();
Expand All @@ -528,14 +551,16 @@ public void attachToService(
serviceAware.onAttachedToService(servicePluginBinding);
}
} finally {
TraceSection.end();
Trace.endSection();
}
}

@Override
public void detachFromService() {
if (isAttachedToService()) {
TraceSection.begin("FlutterEngineConnectionRegistry#detachFromService");
Trace.beginSection("FlutterEngineConnectionRegistry#detachFromService");
Log.v(TAG, "Detaching from a Service: " + service);

try {
// Notify all ServiceAware plugins that they are no longer attached to a Service.
for (ServiceAware serviceAware : serviceAwarePlugins.values()) {
Expand All @@ -545,7 +570,7 @@ public void detachFromService() {
service = null;
servicePluginBinding = null;
} finally {
TraceSection.end();
Trace.endSection();
}
} else {
Log.e(TAG, "Attempted to detach plugins from a Service when no Service was attached.");
Expand All @@ -555,24 +580,27 @@ public void detachFromService() {
@Override
public void onMoveToForeground() {
if (isAttachedToService()) {
TraceSection.begin("FlutterEngineConnectionRegistry#onMoveToForeground");
Trace.beginSection("FlutterEngineConnectionRegistry#onMoveToForeground");

try {
Log.v(TAG, "Attached Service moved to foreground.");
servicePluginBinding.onMoveToForeground();
} finally {
TraceSection.end();
Trace.endSection();
}
}
}

@Override
public void onMoveToBackground() {
if (isAttachedToService()) {
TraceSection.begin("FlutterEngineConnectionRegistry#onMoveToBackground");
;
Trace.beginSection("FlutterEngineConnectionRegistry#onMoveToBackground");
Log.v(TAG, "Attached Service moved to background.");

try {
servicePluginBinding.onMoveToBackground();
} finally {
TraceSection.end();
Trace.endSection();
}
}
}
Expand All @@ -586,7 +614,9 @@ private boolean isAttachedToBroadcastReceiver() {
@Override
public void attachToBroadcastReceiver(
@NonNull BroadcastReceiver broadcastReceiver, @NonNull Lifecycle lifecycle) {
TraceSection.begin("FlutterEngineConnectionRegistry#attachToBroadcastReceiver");
Trace.beginSection("FlutterEngineConnectionRegistry#attachToBroadcastReceiver");
Log.v(TAG, "Attaching to BroadcastReceiver: " + broadcastReceiver);

try {
// If we were already attached to an Android component, detach from it.
detachFromAppComponent();
Expand All @@ -603,14 +633,16 @@ public void attachToBroadcastReceiver(
broadcastReceiverAware.onAttachedToBroadcastReceiver(broadcastReceiverPluginBinding);
}
} finally {
TraceSection.end();
Trace.endSection();
}
}

@Override
public void detachFromBroadcastReceiver() {
if (isAttachedToBroadcastReceiver()) {
TraceSection.begin("FlutterEngineConnectionRegistry#detachFromBroadcastReceiver");
Trace.beginSection("FlutterEngineConnectionRegistry#detachFromBroadcastReceiver");
Log.v(TAG, "Detaching from BroadcastReceiver: " + broadcastReceiver);

try {
// Notify all BroadcastReceiverAware plugins that they are no longer attached to a
// BroadcastReceiver.
Expand All @@ -619,7 +651,7 @@ public void detachFromBroadcastReceiver() {
broadcastReceiverAware.onDetachedFromBroadcastReceiver();
}
} finally {
TraceSection.end();
Trace.endSection();
}
} else {
Log.e(
Expand All @@ -638,8 +670,9 @@ private boolean isAttachedToContentProvider() {
@Override
public void attachToContentProvider(
@NonNull ContentProvider contentProvider, @NonNull Lifecycle lifecycle) {
Trace.beginSection("FlutterEngineConnectionRegistry#attachToContentProvider");
Log.v(TAG, "Attaching to ContentProvider: " + contentProvider);

TraceSection.begin("FlutterEngineConnectionRegistry#attachToContentProvider");
try {
// If we were already attached to an Android component, detach from it.
detachFromAppComponent();
Expand All @@ -656,22 +689,24 @@ public void attachToContentProvider(
contentProviderAware.onAttachedToContentProvider(contentProviderPluginBinding);
}
} finally {
TraceSection.end();
Trace.endSection();
}
}

@Override
public void detachFromContentProvider() {
if (isAttachedToContentProvider()) {
TraceSection.begin("FlutterEngineConnectionRegistry#detachFromContentProvider");
Trace.beginSection("FlutterEngineConnectionRegistry#detachFromContentProvider");
Log.v(TAG, "Detaching from ContentProvider: " + contentProvider);

try {
// Notify all ContentProviderAware plugins that they are no longer attached to a
// ContentProvider.
for (ContentProviderAware contentProviderAware : contentProviderAwarePlugins.values()) {
contentProviderAware.onDetachedFromContentProvider();
}
} finally {
TraceSection.end();
Trace.endSection();
}
} else {
Log.e(
Expand Down
Loading