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

Commit acb9620f authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Rename PostureHelper to PostureDeviceStateConverter" into main

parents 0bedc3c3 160d03b4
Loading
Loading
Loading
Loading
+143 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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 static android.hardware.devicestate.DeviceState.PROPERTY_FEATURE_REAR_DISPLAY;
import static android.hardware.devicestate.DeviceState.PROPERTY_FOLDABLE_DISPLAY_CONFIGURATION_INNER_PRIMARY;
import static android.hardware.devicestate.DeviceState.PROPERTY_FOLDABLE_DISPLAY_CONFIGURATION_OUTER_PRIMARY;
import static android.hardware.devicestate.DeviceState.PROPERTY_FOLDABLE_HARDWARE_CONFIGURATION_FOLD_IN_HALF_OPEN;
import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_KEY_FOLDED;
import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_KEY_HALF_FOLDED;
import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_KEY_REAR_DISPLAY;
import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_KEY_UNFOLDED;
import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_KEY_UNKNOWN;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toSet;

import android.content.Context;
import android.hardware.devicestate.DeviceState;
import android.hardware.devicestate.DeviceStateManager;
import android.hardware.devicestate.feature.flags.Flags;
import android.provider.Settings.Secure.DeviceStateRotationLockKey;

import com.android.internal.R;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/** Helps to convert between device state and posture. */
public class PostureDeviceStateConverter {

    private final Map<Integer, Set<Integer>> mPostures;

    public PostureDeviceStateConverter(Context context, DeviceStateManager deviceStateManager) {
        if (deviceStateManager != null && Flags.deviceStatePropertyMigration()) {
            mPostures = deviceStateManager.getSupportedDeviceStates().stream()
                    .filter(state -> this.toPosture(state) != DEVICE_STATE_ROTATION_KEY_UNKNOWN)
                    .collect(groupingBy(this::toPosture,
                            mapping(DeviceState::getIdentifier, toSet())));
            return;
        }

        mPostures = new HashMap<>();
        final int[] foldedDeviceStatesConfig = context.getResources().getIntArray(
                R.array.config_foldedDeviceStates);
        final Set<Integer> foldedDeviceStates = new HashSet<>();
        for (int state : foldedDeviceStatesConfig) {
            foldedDeviceStates.add(state);
        }
        final int[] halfFoldedDeviceStatesConfig = context.getResources().getIntArray(
                R.array.config_foldedDeviceStates);
        final Set<Integer> halfFoldedDeviceStates = new HashSet<>();
        for (int state : halfFoldedDeviceStatesConfig) {
            halfFoldedDeviceStates.add(state);
        }
        final int[] unfoldedDeviceStatesConfig = context.getResources().getIntArray(
                R.array.config_openDeviceStates);
        final Set<Integer> unfoldedDeviceStates = new HashSet<>();
        for (int state : unfoldedDeviceStatesConfig) {
            unfoldedDeviceStates.add(state);
        }
        final int[] rearDisplayDeviceStatesConfig = context.getResources().getIntArray(
                R.array.config_rearDisplayDeviceStates);
        final Set<Integer> rearDisplayDeviceStates = new HashSet<>();
        for (int state : rearDisplayDeviceStatesConfig) {
            rearDisplayDeviceStates.add(state);
        }

        mPostures.put(DEVICE_STATE_ROTATION_KEY_FOLDED, foldedDeviceStates);
        mPostures.put(DEVICE_STATE_ROTATION_KEY_HALF_FOLDED,
                halfFoldedDeviceStates);
        mPostures.put(DEVICE_STATE_ROTATION_KEY_UNFOLDED, unfoldedDeviceStates);
        mPostures.put(DEVICE_STATE_ROTATION_KEY_REAR_DISPLAY,
                rearDisplayDeviceStates);

    }

    /**
     * Returns posture mapped to the given {@link deviceState}, returns
     * {@link DEVICE_STATE_ROTATION_KEY_UNKNOWN} if no posture found.
     */
    @DeviceStateRotationLockKey
    public int deviceStateToPosture(int deviceState) {
        for (Map.Entry<Integer, Set<Integer>> entry : mPostures.entrySet()) {
            if (entry.getValue().contains(deviceState)) {
                return entry.getKey();
            }
        }
        return DEVICE_STATE_ROTATION_KEY_UNKNOWN;
    }

    /**
     * Returns device state mapped to the given {@link posture}, returns null if no device state
     * found.
     */
    public Integer postureToDeviceState(@DeviceStateRotationLockKey int posture) {
        Set<Integer> deviceStates = mPostures.get(posture);
        if (deviceStates != null && !deviceStates.isEmpty()) {
            return deviceStates.stream().findFirst().orElse(null);
        }
        return null;
    }

    /**
     * Maps a {@link DeviceState} to the corresponding {@link DeviceStateRotationLockKey} value
     * based on the properties of the state.
     */
    @DeviceStateRotationLockKey
    private int toPosture(DeviceState deviceState) {
        if (deviceState.hasProperty(PROPERTY_FEATURE_REAR_DISPLAY)) {
            return DEVICE_STATE_ROTATION_KEY_REAR_DISPLAY;
        }
        if (deviceState.hasProperty(PROPERTY_FOLDABLE_DISPLAY_CONFIGURATION_OUTER_PRIMARY)) {
            return DEVICE_STATE_ROTATION_KEY_FOLDED;
        }
        if (deviceState.hasProperties(PROPERTY_FOLDABLE_DISPLAY_CONFIGURATION_INNER_PRIMARY,
                PROPERTY_FOLDABLE_HARDWARE_CONFIGURATION_FOLD_IN_HALF_OPEN)) {
            return DEVICE_STATE_ROTATION_KEY_HALF_FOLDED;
        }
        if (deviceState.hasProperty(PROPERTY_FOLDABLE_DISPLAY_CONFIGURATION_INNER_PRIMARY)) {
            return DEVICE_STATE_ROTATION_KEY_UNFOLDED;
        }
        return DEVICE_STATE_ROTATION_KEY_UNKNOWN;

    }
}
+5 −5
Original line number Diff line number Diff line
@@ -58,18 +58,18 @@ public class DeviceStateAutoRotateSettingManagerImpl implements
    private final List<SettableDeviceState> mSettableDeviceState = new ArrayList<>();
    private final SecureSettings mSecureSettings;
    private final Handler mMainHandler;
    private final PosturesHelper mPosturesHelper;
    private final PostureDeviceStateConverter mPostureDeviceStateConverter;

    public DeviceStateAutoRotateSettingManagerImpl(
            Context context,
            Executor backgroundExecutor,
            SecureSettings secureSettings,
            Handler mainHandler,
            PosturesHelper posturesHelper
            PostureDeviceStateConverter postureDeviceStateConverter
    ) {
        mSecureSettings = secureSettings;
        mMainHandler = mainHandler;
        mPosturesHelper = posturesHelper;
        mPostureDeviceStateConverter = postureDeviceStateConverter;

        loadAutoRotateDeviceStates(context);
        final ContentObserver contentObserver = new ContentObserver(mMainHandler) {
@@ -103,7 +103,7 @@ public class DeviceStateAutoRotateSettingManagerImpl implements
    @Override
    @Settings.Secure.DeviceStateRotationLockSetting
    public Integer getRotationLockSetting(int deviceState) {
        final int devicePosture = mPosturesHelper.deviceStateToPosture(deviceState);
        final int devicePosture = mPostureDeviceStateConverter.deviceStateToPosture(deviceState);
        final SparseIntArray deviceStateAutoRotateSetting = getRotationLockSetting();
        final Integer autoRotateSettingValue = extractSettingForDevicePosture(devicePosture,
                deviceStateAutoRotateSetting);
@@ -218,7 +218,7 @@ public class DeviceStateAutoRotateSettingManagerImpl implements
            final int posture = parsedEntry.posture;
            final int autoRotateValue = parsedEntry.autoRotateValue;
            final Integer fallbackPosture = parsedEntry.fallbackPosture;
            final Integer deviceState = mPosturesHelper.postureToDeviceState(posture);
            final Integer deviceState = mPostureDeviceStateConverter.postureToDeviceState(posture);

            if (deviceState == null) {
                Log.wtf(TAG, "No matching device state for posture: " + posture);
+2 −2
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ public class DeviceStateAutoRotateSettingManagerProvider {
            @NonNull Executor backgroundExecutor,
            @NonNull SecureSettings secureSettings,
            @NonNull Handler mainHandler,
            @NonNull PosturesHelper posturesHelper
            @NonNull PostureDeviceStateConverter postureDeviceStateConverter
    ) {
        if (Flags.enableDeviceStateAutoRotateSettingRefactor()) {
            return new DeviceStateAutoRotateSettingManagerImpl(
@@ -48,7 +48,7 @@ public class DeviceStateAutoRotateSettingManagerProvider {
                    backgroundExecutor,
                    secureSettings,
                    mainHandler,
                    posturesHelper
                    postureDeviceStateConverter
            );
        } else {
            return new DeviceStateRotationLockSettingsManager(context, secureSettings);
+6 −5
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ public final class DeviceStateRotationLockSettingsManager implements
    private final Handler mMainHandler = new Handler(Looper.getMainLooper());
    private final Set<DeviceStateAutoRotateSettingListener> mListeners = new HashSet<>();
    private final SecureSettings mSecureSettings;
    private final PosturesHelper mPosturesHelper;
    private final PostureDeviceStateConverter mPostureDeviceStateConverter;
    private String[] mPostureRotationLockDefaults;
    private SparseIntArray mPostureRotationLockSettings;
    private SparseIntArray mPostureDefaultRotationLockSettings;
@@ -69,7 +69,8 @@ public final class DeviceStateRotationLockSettingsManager implements
    public DeviceStateRotationLockSettingsManager(Context context, SecureSettings secureSettings) {
        mSecureSettings = secureSettings;

        mPosturesHelper = new PosturesHelper(context, getDeviceStateManager(context));
        mPostureDeviceStateConverter = new PostureDeviceStateConverter(context,
                getDeviceStateManager(context));
        mPostureRotationLockDefaults =
                context.getResources()
                        .getStringArray(R.array.config_perDeviceStateRotationLockDefaults);
@@ -124,7 +125,7 @@ public final class DeviceStateRotationLockSettingsManager implements
    /** Updates the rotation lock setting for a specified device state. */
    @Override
    public void updateSetting(int deviceState, boolean rotationLocked) {
        int posture = mPosturesHelper.deviceStateToPosture(deviceState);
        int posture = mPostureDeviceStateConverter.deviceStateToPosture(deviceState);
        if (mPostureRotationLockFallbackSettings.indexOfKey(posture) >= 0) {
            // The setting for this device posture is IGNORED, and has a fallback posture.
            // The setting for that fallback posture should be the changed in this case.
@@ -156,7 +157,7 @@ public final class DeviceStateRotationLockSettingsManager implements
    @Settings.Secure.DeviceStateRotationLockSetting
    @Override
    public Integer getRotationLockSetting(int deviceState) {
        int devicePosture = mPosturesHelper.deviceStateToPosture(deviceState);
        int devicePosture = mPostureDeviceStateConverter.deviceStateToPosture(deviceState);
        int rotationLockSetting = mPostureRotationLockSettings.get(
                devicePosture, /* valueIfKeyNotFound= */ DEVICE_STATE_ROTATION_LOCK_IGNORED);
        if (rotationLockSetting == DEVICE_STATE_ROTATION_LOCK_IGNORED) {
@@ -333,7 +334,7 @@ public final class DeviceStateRotationLockSettingsManager implements
                    }
                }
                boolean isSettable = rotationLockSetting != DEVICE_STATE_ROTATION_LOCK_IGNORED;
                Integer deviceState = mPosturesHelper.postureToDeviceState(posture);
                Integer deviceState = mPostureDeviceStateConverter.postureToDeviceState(posture);
                if (deviceState != null) {
                    mSettableDeviceStates.add(new SettableDeviceState(deviceState, isSettable));
                } else {
+11 −11
Original line number Diff line number Diff line
@@ -75,7 +75,7 @@ class DeviceStateAutoRotateSettingManagerImplTest {
    private lateinit var mockContentResolver: ContentResolver

    @Mock
    private lateinit var mockPosturesHelper: PosturesHelper
    private lateinit var mMockPostureDeviceStateConverter: PostureDeviceStateConverter

    @Mock
    private lateinit var mockHandler: Handler
@@ -108,7 +108,7 @@ class DeviceStateAutoRotateSettingManagerImplTest {
                executor,
                fakeSecureSettings,
                mockHandler,
                mockPosturesHelper,
                mMockPostureDeviceStateConverter,
            )
    }

@@ -339,24 +339,24 @@ class DeviceStateAutoRotateSettingManagerImplTest {
    }

    private fun setUpMockPostureHelper() {
        whenever(mockPosturesHelper.deviceStateToPosture(eq(DEVICE_STATE_UNFOLDED)))
        whenever(mMockPostureDeviceStateConverter.deviceStateToPosture(eq(DEVICE_STATE_UNFOLDED)))
            .thenReturn(DEVICE_STATE_ROTATION_KEY_UNFOLDED)
        whenever(mockPosturesHelper.deviceStateToPosture(eq(DEVICE_STATE_FOLDED)))
        whenever(mMockPostureDeviceStateConverter.deviceStateToPosture(eq(DEVICE_STATE_FOLDED)))
            .thenReturn(DEVICE_STATE_ROTATION_KEY_FOLDED)
        whenever(mockPosturesHelper.deviceStateToPosture(eq(DEVICE_STATE_HALF_FOLDED)))
        whenever(mMockPostureDeviceStateConverter.deviceStateToPosture(eq(DEVICE_STATE_HALF_FOLDED)))
            .thenReturn(DEVICE_STATE_ROTATION_KEY_HALF_FOLDED)
        whenever(mockPosturesHelper.deviceStateToPosture(eq(DEVICE_STATE_INVALID)))
        whenever(mMockPostureDeviceStateConverter.deviceStateToPosture(eq(DEVICE_STATE_INVALID)))
            .thenReturn(DEVICE_STATE_ROTATION_LOCK_IGNORED)
        whenever(mockPosturesHelper.deviceStateToPosture(eq(DEVICE_STATE_REAR_DISPLAY)))
        whenever(mMockPostureDeviceStateConverter.deviceStateToPosture(eq(DEVICE_STATE_REAR_DISPLAY)))
            .thenReturn(DEVICE_STATE_ROTATION_KEY_REAR_DISPLAY)

        whenever(mockPosturesHelper.postureToDeviceState(eq(DEVICE_STATE_ROTATION_KEY_UNFOLDED)))
        whenever(mMockPostureDeviceStateConverter.postureToDeviceState(eq(DEVICE_STATE_ROTATION_KEY_UNFOLDED)))
            .thenReturn(DEVICE_STATE_UNFOLDED)
        whenever(mockPosturesHelper.postureToDeviceState(eq(DEVICE_STATE_ROTATION_KEY_FOLDED)))
        whenever(mMockPostureDeviceStateConverter.postureToDeviceState(eq(DEVICE_STATE_ROTATION_KEY_FOLDED)))
            .thenReturn(DEVICE_STATE_FOLDED)
        whenever(mockPosturesHelper.postureToDeviceState(eq(DEVICE_STATE_ROTATION_KEY_HALF_FOLDED)))
        whenever(mMockPostureDeviceStateConverter.postureToDeviceState(eq(DEVICE_STATE_ROTATION_KEY_HALF_FOLDED)))
            .thenReturn(DEVICE_STATE_HALF_FOLDED)
        whenever(mockPosturesHelper.postureToDeviceState(eq(DEVICE_STATE_ROTATION_KEY_REAR_DISPLAY)))
        whenever(mMockPostureDeviceStateConverter.postureToDeviceState(eq(DEVICE_STATE_ROTATION_KEY_REAR_DISPLAY)))
            .thenReturn(DEVICE_STATE_REAR_DISPLAY)
    }

Loading