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

Commit 8c2cfb9e authored by Shivangi Dubey's avatar Shivangi Dubey Committed by Android (Google) Code Review
Browse files

Merge "Integrate refactored device-state auto-rotate setting manager" into main

parents ca5470de 04d8e057
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -24,11 +24,11 @@ import android.provider.Settings;
 * Implementation of {@link SecureSettings} that uses Android's {@link Settings.Secure}
 * implementation.
 */
class AndroidSecureSettings implements SecureSettings {
public class AndroidSecureSettings implements SecureSettings {

    private final ContentResolver mContentResolver;

    AndroidSecureSettings(ContentResolver contentResolver) {
    public AndroidSecureSettings(ContentResolver contentResolver) {
        mContentResolver = contentResolver;
    }

+2 −3
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.settingslib.devicestate

import android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK
import android.util.Dumpable

/**
 * Interface for managing [DEVICE_STATE_ROTATION_LOCK] setting.
@@ -25,7 +26,7 @@ import android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK
 * specific device states, retrieve the setting value, and check if rotation is locked for specific
 * or all device states.
 */
interface DeviceStateAutoRotateSettingManager {
interface DeviceStateAutoRotateSettingManager : Dumpable {
    // TODO: b/397928958 - Rename all terms from rotationLock to autoRotate in all apis.

    /** Listener for changes in device-state based auto rotate setting. */
@@ -65,5 +66,3 @@ data class SettableDeviceState(
    /** Returns whether there is an auto-rotation setting for this device state. */
    val isSettable: Boolean
)

+11 −0
Original line number Diff line number Diff line
@@ -22,11 +22,13 @@ import android.os.UserHandle
import android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK
import android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK_IGNORED
import android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK_LOCKED
import android.util.IndentingPrintWriter
import android.util.Log
import android.util.SparseIntArray
import com.android.internal.R
import com.android.settingslib.devicestate.DeviceStateAutoRotateSettingManager.DeviceStateAutoRotateSettingListener
import com.android.window.flags.Flags
import java.io.PrintWriter
import java.util.concurrent.Executor

/**
@@ -104,6 +106,15 @@ class DeviceStateAutoRotateSettingManagerImpl(
        throw UnsupportedOperationException("API updateSetting is not implemented yet")
    }

    override fun dump(writer: PrintWriter, args: Array<out String>?) {
        val indentingWriter = IndentingPrintWriter(writer)
        indentingWriter.println("DeviceStateAutoRotateSettingManagerImpl")
        indentingWriter.increaseIndent()
        indentingWriter.println("fallbackPostureMap: $fallbackPostureMap")
        indentingWriter.println("settableDeviceState: $settableDeviceState")
        indentingWriter.decreaseIndent()
    }

    private fun notifyListeners() =
        settingListeners.forEach { listener -> listener.onSettingsChanged() }

+53 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.settingslib.devicestate

import android.content.Context
import android.os.Handler
import com.android.window.flags.Flags
import java.util.concurrent.Executor

/**
 * Provides appropriate instance of [DeviceStateAutoRotateSettingManager], based on the value of
 * [Flags.FLAG_ENABLE_DEVICE_STATE_AUTO_ROTATE_SETTING_REFACTOR].
 */
object DeviceStateAutoRotateSettingManagerProvider {
    /**
     * Provides an instance of [DeviceStateAutoRotateSettingManager], based on the value of
     * [Flags.FLAG_ENABLE_DEVICE_STATE_AUTO_ROTATE_SETTING_REFACTOR]. It is supposed to be used
     * by apps that supports dagger.
     */
    @JvmStatic
    fun createInstance(
        context: Context,
        backgroundExecutor: Executor,
        secureSettings: SecureSettings,
        mainHandler: Handler,
        posturesHelper: PosturesHelper,
    ): DeviceStateAutoRotateSettingManager =
        if (Flags.enableDeviceStateAutoRotateSettingRefactor()) {
            DeviceStateAutoRotateSettingManagerImpl(
                context,
                backgroundExecutor,
                secureSettings,
                mainHandler,
                posturesHelper,
            )
        } else {
            DeviceStateRotationLockSettingsManager(context, secureSettings)
        }
}
+29 −42
Original line number Diff line number Diff line
@@ -20,10 +20,8 @@ import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK_IGNORE
import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK_LOCKED;
import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK_UNLOCKED;

import static com.android.settingslib.devicestate.DeviceStateAutoRotateSettingManager.DeviceStateAutoRotateSettingListener;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.database.ContentObserver;
@@ -41,6 +39,7 @@ import android.util.SparseIntArray;
import com.android.internal.R;
import com.android.internal.annotations.VisibleForTesting;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
@@ -51,7 +50,8 @@ import java.util.Set;
 * Manages device-state based rotation lock settings. Handles reading, writing, and listening for
 * changes.
 */
public final class DeviceStateRotationLockSettingsManager {
public final class DeviceStateRotationLockSettingsManager implements
        DeviceStateAutoRotateSettingManager {

    private static final String TAG = "DSRotLockSettingsMngr";
    private static final String SEPARATOR_REGEX = ":";
@@ -68,8 +68,7 @@ public final class DeviceStateRotationLockSettingsManager {
    private SparseIntArray mPostureRotationLockFallbackSettings;
    private List<SettableDeviceState> mSettableDeviceStates;

    @VisibleForTesting
    DeviceStateRotationLockSettingsManager(Context context, SecureSettings secureSettings) {
    public DeviceStateRotationLockSettingsManager(Context context, SecureSettings secureSettings) {
        mSecureSettings = secureSettings;

        mPosturesHelper = new PosturesHelper(context, getDeviceStateManager(context));
@@ -89,30 +88,6 @@ public final class DeviceStateRotationLockSettingsManager {
        return null;
    }

    /** Returns a singleton instance of this class */
    public static synchronized DeviceStateRotationLockSettingsManager getInstance(Context context) {
        if (sSingleton == null) {
            Context applicationContext = context.getApplicationContext();
            ContentResolver contentResolver = applicationContext.getContentResolver();
            SecureSettings secureSettings = new AndroidSecureSettings(contentResolver);
            sSingleton =
                    new DeviceStateRotationLockSettingsManager(applicationContext, secureSettings);
        }
        return sSingleton;
    }

    /** Resets the singleton instance of this class. Only used for testing. */
    @VisibleForTesting
    public static synchronized void resetInstance() {
        sSingleton = null;
    }

    /** Returns true if device-state based rotation lock settings are enabled. */
    public static boolean isDeviceStateRotationLockEnabled(Context context) {
        return context.getResources()
                .getStringArray(R.array.config_perDeviceStateRotationLockDefaults).length > 0;
    }

    private void listenForSettingsChange() {
        mSecureSettings
                .registerContentObserver(
@@ -131,7 +106,8 @@ public final class DeviceStateRotationLockSettingsManager {
     * Registers a {@link DeviceStateAutoRotateSettingListener} to be notified when the settings
     * change. Can be called multiple times with different listeners.
     */
    public void registerListener(DeviceStateAutoRotateSettingListener runnable) {
    @Override
    public void registerListener(@NonNull DeviceStateAutoRotateSettingListener runnable) {
        mListeners.add(runnable);
    }

@@ -139,14 +115,16 @@ public final class DeviceStateRotationLockSettingsManager {
     * Unregisters a {@link DeviceStateAutoRotateSettingListener}. No-op if the given instance
     * was never registered.
     */
    @Override
    public void unregisterListener(
            DeviceStateAutoRotateSettingListener deviceStateAutoRotateSettingListener) {
            @NonNull DeviceStateAutoRotateSettingListener deviceStateAutoRotateSettingListener) {
        if (!mListeners.remove(deviceStateAutoRotateSettingListener)) {
            Log.w(TAG, "Attempting to unregister a listener hadn't been registered");
        }
    }

    /** Updates the rotation lock setting for a specified device state. */
    @Override
    public void updateSetting(int deviceState, boolean rotationLocked) {
        int posture = mPosturesHelper.deviceStateToPosture(deviceState);
        if (mPostureRotationLockFallbackSettings.indexOfKey(posture) >= 0) {
@@ -173,6 +151,7 @@ public final class DeviceStateRotationLockSettingsManager {
     * DEVICE_STATE_ROTATION_LOCK_IGNORED}.
     */
    @Settings.Secure.DeviceStateRotationLockSetting
    @Override
    public int getRotationLockSetting(int deviceState) {
        int devicePosture = mPosturesHelper.deviceStateToPosture(deviceState);
        int rotationLockSetting = mPostureRotationLockSettings.get(
@@ -196,6 +175,7 @@ public final class DeviceStateRotationLockSettingsManager {


    /** Returns true if the rotation is locked for the current device state */
    @Override
    public boolean isRotationLocked(int deviceState) {
        return getRotationLockSetting(deviceState) == DEVICE_STATE_ROTATION_LOCK_LOCKED;
    }
@@ -204,6 +184,7 @@ public final class DeviceStateRotationLockSettingsManager {
     * Returns true if there is no device state for which the current setting is {@link
     * DEVICE_STATE_ROTATION_LOCK_UNLOCKED}.
     */
    @Override
    public boolean isRotationLockedForAllStates() {
        for (int i = 0; i < mPostureRotationLockSettings.size(); i++) {
            if (mPostureRotationLockSettings.valueAt(i)
@@ -215,6 +196,8 @@ public final class DeviceStateRotationLockSettingsManager {
    }

    /** Returns a list of device states and their respective auto-rotation setting availability. */
    @Override
    @NonNull
    public List<SettableDeviceState> getSettableDeviceStates() {
        // Returning a copy to make sure that nothing outside can mutate our internal list.
        return new ArrayList<>(mSettableDeviceStates);
@@ -356,17 +339,21 @@ public final class DeviceStateRotationLockSettingsManager {
        }
    }

    /** Dumps internal state. */
    public void dump(IndentingPrintWriter pw) {
        pw.println("DeviceStateRotationLockSettingsManager");
        pw.increaseIndent();
        pw.println("mPostureRotationLockDefaults: "
    @Override
    public void dump(@NonNull PrintWriter writer, String[] args) {
        IndentingPrintWriter indentingWriter = new IndentingPrintWriter(writer);
        indentingWriter.println("DeviceStateRotationLockSettingsManager");
        indentingWriter.increaseIndent();
        indentingWriter.println("mPostureRotationLockDefaults: "
                + Arrays.toString(mPostureRotationLockDefaults));
        pw.println("mPostureDefaultRotationLockSettings: " + mPostureDefaultRotationLockSettings);
        pw.println("mDeviceStateRotationLockSettings: " + mPostureRotationLockSettings);
        pw.println("mPostureRotationLockFallbackSettings: " + mPostureRotationLockFallbackSettings);
        pw.println("mSettableDeviceStates: " + mSettableDeviceStates);
        pw.decreaseIndent();
        indentingWriter.println(
                "mPostureDefaultRotationLockSettings: " + mPostureDefaultRotationLockSettings);
        indentingWriter.println(
                "mDeviceStateRotationLockSettings: " + mPostureRotationLockSettings);
        indentingWriter.println(
                "mPostureRotationLockFallbackSettings: " + mPostureRotationLockFallbackSettings);
        indentingWriter.println("mSettableDeviceStates: " + mSettableDeviceStates);
        indentingWriter.decreaseIndent();
    }

    /**
Loading