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

Commit 127ce4a3 authored by Schneider Victor-tulias's avatar Schneider Victor-tulias
Browse files

Fix NullPointerException in RotationHelper

A possible race condition between RotationHelper and DisplayController is causing RotationHelper.onDisplayInfoChanged to be called after it has already been destroyed. Added checks to make Rotationhelper a no-op after being destroyed.

Flag: not needed
Fixes: 303506682
Test: AllAppsImageTest
Change-Id: Ief1946a356698eda9c2b806f9d49e08b1d66dae1
parent 5bec1164
Loading
Loading
Loading
Loading
+35 −40
Original line number Original line Diff line number Diff line
@@ -25,14 +25,13 @@ import static com.android.launcher3.Utilities.dpiFromPx;
import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
import static com.android.launcher3.util.window.WindowManagerProxy.MIN_TABLET_WIDTH;
import static com.android.launcher3.util.window.WindowManagerProxy.MIN_TABLET_WIDTH;


import android.app.Activity;
import android.content.Context;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Handler;
import android.os.Handler;
import android.os.Message;
import android.os.Message;


import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.annotation.WorkerThread;
import androidx.annotation.WorkerThread;


import com.android.launcher3.BaseActivity;
import com.android.launcher3.BaseActivity;
@@ -62,8 +61,8 @@ public class RotationHelper implements OnSharedPreferenceChangeListener,
    public static final int REQUEST_ROTATE = 1;
    public static final int REQUEST_ROTATE = 1;
    public static final int REQUEST_LOCK = 2;
    public static final int REQUEST_LOCK = 2;


    @Nullable
    @NonNull
    private BaseActivity mActivity;
    private final BaseActivity mActivity;
    private final Handler mRequestOrientationHandler;
    private final Handler mRequestOrientationHandler;


    private boolean mIgnoreAutoRotateSettings;
    private boolean mIgnoreAutoRotateSettings;
@@ -92,14 +91,14 @@ public class RotationHelper implements OnSharedPreferenceChangeListener,
    // Initialize mLastActivityFlags to a value not used by SCREEN_ORIENTATION flags
    // Initialize mLastActivityFlags to a value not used by SCREEN_ORIENTATION flags
    private int mLastActivityFlags = -999;
    private int mLastActivityFlags = -999;


    public RotationHelper(BaseActivity activity) {
    public RotationHelper(@NonNull BaseActivity activity) {
        mActivity = activity;
        mActivity = activity;
        mRequestOrientationHandler =
        mRequestOrientationHandler =
                new Handler(UI_HELPER_EXECUTOR.getLooper(), this::setOrientationAsync);
                new Handler(UI_HELPER_EXECUTOR.getLooper(), this::setOrientationAsync);
    }
    }


    private void setIgnoreAutoRotateSettings(boolean ignoreAutoRotateSettings,
    private void setIgnoreAutoRotateSettings(boolean ignoreAutoRotateSettings) {
            DisplayController.Info info) {
        if (mDestroyed) return;
        // On large devices we do not handle auto-rotate differently.
        // On large devices we do not handle auto-rotate differently.
        mIgnoreAutoRotateSettings = ignoreAutoRotateSettings;
        mIgnoreAutoRotateSettings = ignoreAutoRotateSettings;
        if (!mIgnoreAutoRotateSettings) {
        if (!mIgnoreAutoRotateSettings) {
@@ -122,58 +121,54 @@ public class RotationHelper implements OnSharedPreferenceChangeListener,


    @Override
    @Override
    public void onDisplayInfoChanged(Context context, DisplayController.Info info, int flags) {
    public void onDisplayInfoChanged(Context context, DisplayController.Info info, int flags) {
        if (mDestroyed) return;
        boolean ignoreAutoRotateSettings = info.isTablet(info.realBounds);
        boolean ignoreAutoRotateSettings = info.isTablet(info.realBounds);
        if (mIgnoreAutoRotateSettings != ignoreAutoRotateSettings) {
        if (mIgnoreAutoRotateSettings != ignoreAutoRotateSettings) {
            setIgnoreAutoRotateSettings(ignoreAutoRotateSettings, info);
            setIgnoreAutoRotateSettings(ignoreAutoRotateSettings);
            notifyChange();
            notifyChange();
        }
        }
    }
    }


    public void setStateHandlerRequest(int request) {
    public void setStateHandlerRequest(int request) {
        if (mStateHandlerRequest != request) {
        if (mDestroyed || mStateHandlerRequest == request) return;
        mStateHandlerRequest = request;
        mStateHandlerRequest = request;
        notifyChange();
        notifyChange();
    }
    }
    }


    public void setCurrentTransitionRequest(int request) {
    public void setCurrentTransitionRequest(int request) {
        if (mCurrentTransitionRequest != request) {
        if (mDestroyed || mCurrentTransitionRequest == request) return;
        mCurrentTransitionRequest = request;
        mCurrentTransitionRequest = request;
        notifyChange();
        notifyChange();
    }
    }
    }


    public void setCurrentStateRequest(int request) {
    public void setCurrentStateRequest(int request) {
        if (mCurrentStateRequest != request) {
        if (mDestroyed || mCurrentStateRequest == request) return;
        mCurrentStateRequest = request;
        mCurrentStateRequest = request;
        notifyChange();
        notifyChange();
    }
    }
    }


    // Used by tests only.
    // Used by tests only.
    public void forceAllowRotationForTesting(boolean allowRotation) {
    public void forceAllowRotationForTesting(boolean allowRotation) {
        if (mDestroyed) return;
        mForceAllowRotationForTesting = allowRotation;
        mForceAllowRotationForTesting = allowRotation;
        notifyChange();
        notifyChange();
    }
    }


    public void initialize() {
    public void initialize() {
        if (!mInitialized) {
        if (mInitialized) return;
        mInitialized = true;
        mInitialized = true;
        DisplayController displayController = DisplayController.INSTANCE.get(mActivity);
        DisplayController displayController = DisplayController.INSTANCE.get(mActivity);
        DisplayController.Info info = displayController.getInfo();
        DisplayController.Info info = displayController.getInfo();
            setIgnoreAutoRotateSettings(info.isTablet(info.realBounds), info);
        setIgnoreAutoRotateSettings(info.isTablet(info.realBounds));
        displayController.addChangeListener(this);
        displayController.addChangeListener(this);
        notifyChange();
        notifyChange();
    }
    }
    }


    public void destroy() {
    public void destroy() {
        if (!mDestroyed) {
        if (mDestroyed) return;
        mDestroyed = true;
        mDestroyed = true;
        DisplayController.INSTANCE.get(mActivity).removeChangeListener(this);
        DisplayController.INSTANCE.get(mActivity).removeChangeListener(this);
        LauncherPrefs.get(mActivity).removeListener(this, ALLOW_ROTATION);
        LauncherPrefs.get(mActivity).removeListener(this, ALLOW_ROTATION);
            mActivity = null;
        }
    }
    }


    private void notifyChange() {
    private void notifyChange() {
@@ -206,10 +201,8 @@ public class RotationHelper implements OnSharedPreferenceChangeListener,


    @WorkerThread
    @WorkerThread
    private boolean setOrientationAsync(Message msg) {
    private boolean setOrientationAsync(Message msg) {
        Activity activity = mActivity;
        if (mDestroyed) return true;
        if (activity != null) {
        mActivity.setRequestedOrientation(msg.what);
            activity.setRequestedOrientation(msg.what);
        }
        return true;
        return true;
    }
    }


@@ -228,8 +221,10 @@ public class RotationHelper implements OnSharedPreferenceChangeListener,
    public String toString() {
    public String toString() {
        return String.format("[mStateHandlerRequest=%d, mCurrentStateRequest=%d, "
        return String.format("[mStateHandlerRequest=%d, mCurrentStateRequest=%d, "
                        + "mLastActivityFlags=%d, mIgnoreAutoRotateSettings=%b, "
                        + "mLastActivityFlags=%d, mIgnoreAutoRotateSettings=%b, "
                        + "mHomeRotationEnabled=%b, mForceAllowRotationForTesting=%b]",
                        + "mHomeRotationEnabled=%b, mForceAllowRotationForTesting=%b,"
                        + " mDestroyed=%b]",
                mStateHandlerRequest, mCurrentStateRequest, mLastActivityFlags,
                mStateHandlerRequest, mCurrentStateRequest, mLastActivityFlags,
                mIgnoreAutoRotateSettings, mHomeRotationEnabled, mForceAllowRotationForTesting);
                mIgnoreAutoRotateSettings, mHomeRotationEnabled, mForceAllowRotationForTesting,
                mDestroyed);
    }
    }
}
}