Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ec514d05 authored by Saumya Prakash's avatar Saumya Prakash Committed by Schneider Victor-tulias
Browse files

Check that onAttach() is called before onResume() to prevent NPE.

Sometimes, the context is null in TutorialController which leads to an
NPE when relying on context specific functions. This change checks that
context is null and if so, defers the context specific functions that are
called until after onAttach() is called.

Fix: 307781152
Test: Set context to null in onResume() and check that the tutorial
continues like normal
Flag: None

Change-Id: I66e345f8361315787534bb88b9c1d12278708d78
parent c7157814
Loading
Loading
Loading
Loading
+24 −1
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import static com.android.quickstep.interaction.GestureSandboxActivity.KEY_USE_T
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Insets;
@@ -86,6 +87,8 @@ abstract class TutorialFragment extends GestureSandboxFragment implements OnTouc
    private boolean mIsFoldable;
    private boolean mOnAttachedToWindowPendingCreate;

    @Nullable private Runnable mOnAttachedOnGlobalLayoutCallback = null;

    public static TutorialFragment newInstance(
            TutorialType tutorialType, boolean gestureComplete, boolean fromTutorialMenu) {
        TutorialFragment fragment = getFragmentForTutorialType(tutorialType, fromTutorialMenu);
@@ -349,13 +352,27 @@ abstract class TutorialFragment extends GestureSandboxFragment implements OnTouc
                    new ViewTreeObserver.OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {
                            changeController(mTutorialType);
                            runOnAttached(() -> changeController(mTutorialType));
                            mRootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        }
                    });
        }
    }

    private void runOnAttached(Runnable callback) {
        mOnAttachedOnGlobalLayoutCallback = callback;
        if (getContext() != null) {
            onAttached();
        }
    }

    private void onAttached() {
        if (mOnAttachedOnGlobalLayoutCallback != null) {
            mOnAttachedOnGlobalLayoutCallback.run();
            mOnAttachedOnGlobalLayoutCallback = null;
        }
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (mTutorialController != null && !isGestureComplete()) {
@@ -377,6 +394,12 @@ abstract class TutorialFragment extends GestureSandboxFragment implements OnTouc
                | mNavBarGestureHandler.onInterceptTouch(motionEvent);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        onAttached();
    }

    @Override
    void onAttachedToWindow() {
        if (mEdgeBackGestureHandler == null) {