Skip to content

Commit

Permalink
Android Embedding Refactor PR32: Clean up logs in new embedding (flut…
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-carroll committed Jun 18, 2019
1 parent 36f18b0 commit f2e76a8
Show file tree
Hide file tree
Showing 21 changed files with 237 additions and 46 deletions.
Expand Up @@ -18,14 +18,14 @@
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import io.flutter.Log;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterShellArgs;
import io.flutter.embedding.engine.renderer.OnFirstFrameRenderedListener;
Expand Down Expand Up @@ -179,7 +179,6 @@ public Intent build(@NonNull Context context) {

@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate()");
super.onCreate(savedInstanceState);
configureWindowForTransparency();
setContentView(createFragmentContainer());
Expand Down Expand Up @@ -221,6 +220,8 @@ private void showCoverView() {
return;
}

Log.v(TAG, "Showing cover view until first frame is rendered.");

// Create the coverView.
if (coverView == null) {
coverView = new View(this);
Expand Down Expand Up @@ -322,6 +323,13 @@ private void ensureFlutterFragmentCreated() {
protected FlutterFragment createFlutterFragment() {
BackgroundMode backgroundMode = getBackgroundMode();

Log.d(TAG, "Creating FlutterFragment:\n"
+ "Background transparency mode: " + backgroundMode + "\n"
+ "Dart entrypoint: " + getDartEntrypoint() + "\n"
+ "Initial route: " + getInitialRoute() + "\n"
+ "App bundle path: " + getAppBundlePath() + "\n"
+ "Will attach FlutterEngine to Activity: " + shouldAttachEngineToActivity());

return new FlutterFragment.Builder()
.dartEntrypoint(getDartEntrypoint())
.initialRoute(getInitialRoute())
Expand Down Expand Up @@ -515,6 +523,7 @@ private boolean isDebuggable() {

@Override
public void onFirstFrameRendered() {
Log.v(TAG, "First frame has been rendered. Hiding cover view.");
hideCoverView();
}

Expand Down
Expand Up @@ -17,11 +17,13 @@
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.Arrays;

import io.flutter.Log;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterShellArgs;
import io.flutter.embedding.engine.dart.DartExecutor;
Expand Down Expand Up @@ -358,6 +360,7 @@ public void onAttach(Context context) {
// which means there shouldn't be any possibility for the Fragment Lifecycle to get out of
// sync with the Activity. We use the Fragment's Lifecycle because it is possible that the
// attached Activity is not a LifecycleOwner.
Log.d(TAG, "Attaching FlutterEngine to the Activity that owns this Fragment.");
flutterEngine.getActivityControlSurface().attachToActivity(getActivity(), getLifecycle());
}
}
Expand Down Expand Up @@ -385,6 +388,8 @@ private void initializeFlutter(@NonNull Context context) {
* {@link FlutterEngine} is instantiated.
*/
private void setupFlutterEngine() {
Log.d(TAG, "Setting up FlutterEngine.");

// First, defer to subclasses for a custom FlutterEngine.
flutterEngine = createFlutterEngine(getContextCompat());
if (flutterEngine != null) {
Expand All @@ -407,8 +412,8 @@ private void setupFlutterEngine() {

// Neither our subclass, nor our owning Activity wanted to provide a custom FlutterEngine.
// Create a FlutterEngine to back our FlutterView.
Log.d(TAG, "No subclass or our attached Activity provided a custom FlutterEngine. Creating a "
+ "new FlutterEngine for this FlutterFragment.");
Log.d(TAG, "No preferred FlutterEngine was provided. Creating a new FlutterEngine for"
+ " this FlutterFragment.");
flutterEngine = new FlutterEngine(getContext());
isFlutterEngineFromActivity = false;
}
Expand All @@ -434,6 +439,7 @@ protected FlutterEngine createFlutterEngine(@NonNull Context context) {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.v(TAG, "Creating FlutterView.");
flutterView = new FlutterView(getContext(), getRenderMode(), getTransparencyMode());
flutterView.addOnFirstFrameRenderedListener(onFirstFrameRenderedListener);
return flutterView;
Expand All @@ -455,6 +461,9 @@ private void doInitialFlutterViewRun() {
return;
}

Log.d(TAG, "Executing Dart entrypoint: " + getDartEntrypointFunctionName()
+ ", and sending initial route: " + getInitialRoute());

// The engine needs to receive the Flutter app's initial route before executing any
// Dart code to ensure that the initial route arrives in time to be applied.
if (getInitialRoute() != null) {
Expand Down Expand Up @@ -528,7 +537,7 @@ protected FlutterView.TransparencyMode getTransparencyMode() {
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart()");
Log.v(TAG, "onStart()");

// We post() the code that attaches the FlutterEngine to our FlutterView because there is
// some kind of blocking logic on the native side when the surface is connected. That lag
Expand All @@ -538,6 +547,7 @@ public void onStart() {
new Handler().post(new Runnable() {
@Override
public void run() {
Log.v(TAG, "Attaching FlutterEngine to FlutterView.");
flutterView.attachToFlutterEngine(flutterEngine);

// TODO(mattcarroll): the following call should exist here, but the plugin system needs to be revamped.
Expand All @@ -552,13 +562,13 @@ public void run() {
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume()");
Log.v(TAG, "onResume()");
flutterEngine.getLifecycleChannel().appIsResumed();
}

// TODO(mattcarroll): determine why this can't be in onResume(). Comment reason, or move if possible.
public void onPostResume() {
Log.d(TAG, "onPostResume()");
Log.v(TAG, "onPostResume()");
if (flutterEngine != null) {
// TODO(mattcarroll): find a better way to handle the update of UI overlays than calling through
// to platformPlugin. We're implicitly entangling the Window, Activity, Fragment,
Expand All @@ -572,32 +582,33 @@ public void onPostResume() {
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "onPause()");
Log.v(TAG, "onPause()");
flutterEngine.getLifecycleChannel().appIsInactive();
}

@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop()");
Log.v(TAG, "onStop()");
flutterEngine.getLifecycleChannel().appIsPaused();
flutterView.detachFromFlutterEngine();
}

@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG, "onDestroyView()");
Log.v(TAG, "onDestroyView()");
flutterView.removeOnFirstFrameRenderedListener(onFirstFrameRenderedListener);
}

@Override
public void onDetach() {
super.onDetach();
Log.d(TAG, "onDetach()");
Log.v(TAG, "onDetach()");

if (shouldAttachEngineToActivity()) {
// Notify plugins that they are no longer attached to an Activity.
Log.d(TAG, "Detaching FlutterEngine from the Activity that owns this Fragment.");
if (getActivity().isChangingConfigurations()) {
flutterEngine.getActivityControlSurface().detachFromActivityForConfigChanges();
} else {
Expand Down Expand Up @@ -641,8 +652,8 @@ protected boolean shouldAttachEngineToActivity() {
* See {@link android.app.Activity#onBackPressed()}
*/
public void onBackPressed() {
Log.d(TAG, "onBackPressed()");
if (flutterEngine != null) {
Log.v(TAG, "Forwarding onBackPressed() to FlutterEngine.");
flutterEngine.getNavigationChannel().popRoute();
} else {
Log.w(TAG, "Invoked onBackPressed() before FlutterFragment was attached to an Activity.");
Expand All @@ -660,6 +671,10 @@ public void onBackPressed() {
*/
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (flutterEngine != null) {
Log.v(TAG, "Forwarding onRequestPermissionsResult() to FlutterEngine:\n"
+ "requestCode: " + requestCode + "\n"
+ "permissions: " + Arrays.toString(permissions) + "\n"
+ "grantResults: " + Arrays.toString(grantResults));
flutterEngine.getActivityControlSurface().onRequestPermissionsResult(requestCode, permissions, grantResults);
} else {
Log.w(TAG, "onRequestPermissionResult() invoked before FlutterFragment was attached to an Activity.");
Expand All @@ -676,6 +691,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
*/
public void onNewIntent(@NonNull Intent intent) {
if (flutterEngine != null) {
Log.v(TAG, "Forwarding onNewIntent() to FlutterEngine.");
flutterEngine.getActivityControlSurface().onNewIntent(intent);
} else {
Log.w(TAG, "onNewIntent() invoked before FlutterFragment was attached to an Activity.");
Expand All @@ -692,6 +708,10 @@ public void onNewIntent(@NonNull Intent intent) {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (flutterEngine != null) {
Log.v(TAG, "Forwarding onActivityResult() to FlutterEngine:\n"
+ "requestCode: " + requestCode + "\n"
+ "resultCode: " + resultCode + "\n"
+ "data: " + data);
flutterEngine.getActivityControlSurface().onActivityResult(requestCode, resultCode, data);
} else {
Log.w(TAG, "onActivityResult() invoked before FlutterFragment was attached to an Activity.");
Expand All @@ -706,6 +726,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
*/
public void onUserLeaveHint() {
if (flutterEngine != null) {
Log.v(TAG, "Forwarding onUserLeaveHint() to FlutterEngine.");
flutterEngine.getActivityControlSurface().onUserLeaveHint();
} else {
Log.w(TAG, "onUserLeaveHint() invoked before FlutterFragment was attached to an Activity.");
Expand All @@ -724,6 +745,7 @@ public void onTrimMemory(int level) {
// Use a trim level delivered while the application is running so the
// framework has a chance to react to the notification.
if (level == TRIM_MEMORY_RUNNING_LOW) {
Log.v(TAG, "Forwarding onTrimMemory() to FlutterEngine. Level: " + level);
flutterEngine.getSystemChannel().sendMemoryPressureWarning();
}
} else {
Expand All @@ -739,6 +761,7 @@ public void onTrimMemory(int level) {
@Override
public void onLowMemory() {
super.onLowMemory();
Log.v(TAG, "Forwarding onLowMemory() to FlutterEngine.");
flutterEngine.getSystemChannel().sendMemoryPressureWarning();
}

Expand Down
Expand Up @@ -9,13 +9,13 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.HashSet;
import java.util.Set;

import io.flutter.Log;
import io.flutter.embedding.engine.renderer.FlutterRenderer;
import io.flutter.embedding.engine.renderer.OnFirstFrameRenderedListener;

Expand Down Expand Up @@ -51,26 +51,25 @@ public class FlutterSurfaceView extends SurfaceView implements FlutterRenderer.R
private final SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.d(TAG, "SurfaceHolder.Callback.surfaceCreated()");
Log.v(TAG, "SurfaceHolder.Callback.surfaceCreated()");
isSurfaceAvailableForRendering = true;

if (isAttachedToFlutterRenderer) {
Log.d(TAG, "Already attached to renderer. Notifying of surface creation.");
connectSurfaceToRenderer();
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.d(TAG, "SurfaceHolder.Callback.surfaceChanged()");
Log.v(TAG, "SurfaceHolder.Callback.surfaceChanged()");
if (isAttachedToFlutterRenderer) {
changeSurfaceSize(width, height);
}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d(TAG, "SurfaceHolder.Callback.surfaceDestroyed()");
Log.v(TAG, "SurfaceHolder.Callback.surfaceDestroyed()");
isSurfaceAvailableForRendering = false;

if (isAttachedToFlutterRenderer) {
Expand Down Expand Up @@ -138,8 +137,9 @@ private void init() {
* Flutter's UI to this {@code FlutterSurfaceView}.
*/
public void attachToRenderer(@NonNull FlutterRenderer flutterRenderer) {
Log.d(TAG, "attachToRenderer");
Log.v(TAG, "Attaching to FlutterRenderer.");
if (this.flutterRenderer != null) {
Log.v(TAG, "Already connected to a FlutterRenderer. Detaching from old one and attaching to new one.");
this.flutterRenderer.detachFromRenderSurface();
}

Expand All @@ -149,7 +149,7 @@ public void attachToRenderer(@NonNull FlutterRenderer flutterRenderer) {
// If we're already attached to an Android window then we're now attached to both a renderer
// and the Android window. We can begin rendering now.
if (isSurfaceAvailableForRendering) {
Log.d(TAG, "Surface is available for rendering. Connecting.");
Log.v(TAG, "Surface is available for rendering. Connecting FlutterRenderer to Android surface.");
connectSurfaceToRenderer();
}
}
Expand All @@ -164,7 +164,9 @@ public void detachFromRenderer() {
if (flutterRenderer != null) {
// If we're attached to an Android window then we were rendering a Flutter UI. Now that
// this FlutterSurfaceView is detached from the FlutterRenderer, we need to stop rendering.
// TODO(mattcarroll): introduce a isRendererConnectedToSurface() to wrap "getWindowToken() != null"
if (getWindowToken() != null) {
Log.v(TAG, "Disconnecting FlutterRenderer from Android surface.");
disconnectSurfaceFromRenderer();
}

Expand Down Expand Up @@ -193,6 +195,7 @@ private void changeSurfaceSize(int width, int height) {
throw new IllegalStateException("changeSurfaceSize() should only be called when flutterRenderer is non-null.");
}

Log.v(TAG, "Notifying FlutterRenderer that Android surface size has changed to " + width + " x " + height);
flutterRenderer.surfaceChanged(width, height);
}

Expand Down Expand Up @@ -226,7 +229,7 @@ public void removeOnFirstFrameRenderedListener(@NonNull OnFirstFrameRenderedList
@Override
public void onFirstFrameRendered() {
// TODO(mattcarroll): decide where this method should live and what it needs to do.
Log.d(TAG, "onFirstFrameRendered()");
Log.v(TAG, "onFirstFrameRendered()");
// Now that a frame is ready to display, take this SurfaceView from transparent to opaque.
setAlpha(1.0f);

Expand Down

0 comments on commit f2e76a8

Please sign in to comment.