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

Commit 4735da06 authored by Abel Tesfaye's avatar Abel Tesfaye
Browse files

Add toggle switch for auto rotate preference in display settings.

Bug: 181299673

Test: locally with flame & crosshatch
Change-Id: Iea4dbdd6df8a53f0fb79b82c97892256ad072c1d
parent 0ef977bc
Loading
Loading
Loading
Loading
+59 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.display;

import android.content.Context;
import android.util.AttributeSet;

import com.android.internal.view.RotationPolicy;
import com.android.settings.widget.PrimarySwitchPreference;

/**
 * component for the display settings auto rotate toggle
 */
public class SmartAutoRotatePreference extends PrimarySwitchPreference {

    private RotationPolicy.RotationPolicyListener mRotationPolicyListener;

    public SmartAutoRotatePreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onAttached() {
        super.onAttached();
        if (mRotationPolicyListener == null) {
            mRotationPolicyListener = new RotationPolicy.RotationPolicyListener() {
                @Override
                public void onChange() {
                    setChecked(!RotationPolicy.isRotationLocked(getContext()));
                }
            };
        }
        RotationPolicy.registerRotationPolicyListener(getContext(),
                mRotationPolicyListener);
    }

    @Override
    public void onDetached() {
        super.onDetached();
        if (mRotationPolicyListener != null) {
            RotationPolicy.unregisterRotationPolicyListener(getContext(),
                    mRotationPolicyListener);
        }
    }
}
+30 −5
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import static android.provider.Settings.Secure.CAMERA_AUTOROTATE;
import static com.android.settings.display.SmartAutoRotateController.hasSufficientPermission;
import static com.android.settings.display.SmartAutoRotateController.isRotationResolverServiceAvailable;

import android.app.settings.SettingsEnums;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -37,7 +38,9 @@ import androidx.preference.PreferenceScreen;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.view.RotationPolicy;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
@@ -45,12 +48,10 @@ import com.android.settingslib.core.lifecycle.events.OnStop;
/**
 * SmartAutoRotatePreferenceController provides auto rotate summary in display settings
 */
public class SmartAutoRotatePreferenceController extends BasePreferenceController
public class SmartAutoRotatePreferenceController extends TogglePreferenceController
        implements LifecycleObserver, OnStart, OnStop {

    private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
    private Preference mPreference;

    private final MetricsFeatureProvider mMetricsFeatureProvider;
    private final SensorPrivacyManager mPrivacyManager;
    private final PowerManager mPowerManager;
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@@ -60,12 +61,16 @@ public class SmartAutoRotatePreferenceController extends BasePreferenceControlle
        }
    };

    private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
    private Preference mPreference;

    public SmartAutoRotatePreferenceController(Context context, String preferenceKey) {
        super(context, preferenceKey);
        mPrivacyManager = SensorPrivacyManager.getInstance(context);
        mPrivacyManager
                .addSensorPrivacyListener(CAMERA, (sensor, enabled) -> refreshSummary(mPreference));
        mPowerManager = context.getSystemService(PowerManager.class);
        mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
    }

    @Override
@@ -80,6 +85,12 @@ public class SmartAutoRotatePreferenceController extends BasePreferenceControlle
        mPreference = screen.findPreference(getPreferenceKey());
    }

    @Override
    public void updateState(Preference preference) {
        super.updateState(preference);
        refreshSummary(mPreference);
    }

    @Override
    public void onStart() {
        mContext.registerReceiver(mReceiver,
@@ -121,6 +132,20 @@ public class SmartAutoRotatePreferenceController extends BasePreferenceControlle
        return mPowerManager.isPowerSaveMode();
    }

    @Override
    public boolean isChecked() {
        return !RotationPolicy.isRotationLocked(mContext);
    }

    @Override
    public boolean setChecked(boolean isChecked) {
        final boolean isLocked = !isChecked;
        mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_ROTATION_LOCK,
                isLocked);
        RotationPolicy.setRotationLock(mContext, isLocked);
        return true;
    }

    @Override
    public CharSequence getSummary() {
        int activeStringId = R.string.auto_rotate_option_off;