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

Commit 4a0a867d authored by Schneider Victor-tulias's avatar Schneider Victor-tulias Committed by Android (Google) Code Review
Browse files

Merge "Fix NullPointerException in RotationHelper" into main

parents 0f9d9a82 127ce4a3
Loading
Loading
Loading
Loading
+35 −40
Original line number 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.window.WindowManagerProxy.MIN_TABLET_WIDTH;

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

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

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_LOCK = 2;

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

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

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

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

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

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

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

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

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

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

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

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

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

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