Merge "Add support for device state based auto-rotation preferences in Settings."
This commit is contained in:
committed by
Android (Google) Code Review
commit
52b6da247e
@@ -22,6 +22,7 @@ import static android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK_UNLOCK
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.database.ContentObserver;
|
||||
import android.os.Handler;
|
||||
import android.os.UserHandle;
|
||||
@@ -33,7 +34,10 @@ import android.util.SparseIntArray;
|
||||
import com.android.internal.R;
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -48,11 +52,12 @@ public final class DeviceStateRotationLockSettingsManager {
|
||||
private static DeviceStateRotationLockSettingsManager sSingleton;
|
||||
|
||||
private final ContentResolver mContentResolver;
|
||||
private final String[] mDeviceStateRotationLockDefaults;
|
||||
private final Handler mMainHandler = Handler.getMain();
|
||||
private final Set<DeviceStateRotationLockSettingsListener> mListeners = new HashSet<>();
|
||||
private String[] mDeviceStateRotationLockDefaults;
|
||||
private SparseIntArray mDeviceStateRotationLockSettings;
|
||||
private SparseIntArray mDeviceStateRotationLockFallbackSettings;
|
||||
private List<SettableDeviceState> mSettableDeviceStates;
|
||||
|
||||
private DeviceStateRotationLockSettingsManager(Context context) {
|
||||
mContentResolver = context.getContentResolver();
|
||||
@@ -73,6 +78,12 @@ public final class DeviceStateRotationLockSettingsManager {
|
||||
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()
|
||||
@@ -180,6 +191,12 @@ public final class DeviceStateRotationLockSettingsManager {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Returns a list of device states and their respective auto-rotation setting availability. */
|
||||
public List<SettableDeviceState> getSettableDeviceStates() {
|
||||
// Returning a copy to make sure that nothing outside can mutate our internal list.
|
||||
return new ArrayList<>(mSettableDeviceStates);
|
||||
}
|
||||
|
||||
private void initializeInMemoryMap() {
|
||||
String serializedSetting =
|
||||
Settings.Secure.getStringForUser(
|
||||
@@ -215,6 +232,17 @@ public final class DeviceStateRotationLockSettingsManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the state of the class and saved settings back to the default values provided by the
|
||||
* resources config.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public void resetStateForTesting(Resources resources) {
|
||||
mDeviceStateRotationLockDefaults =
|
||||
resources.getStringArray(R.array.config_perDeviceStateRotationLockDefaults);
|
||||
fallbackOnDefaults();
|
||||
}
|
||||
|
||||
private void fallbackOnDefaults() {
|
||||
loadDefaults();
|
||||
persistSettings();
|
||||
@@ -251,6 +279,7 @@ public final class DeviceStateRotationLockSettingsManager {
|
||||
}
|
||||
|
||||
private void loadDefaults() {
|
||||
mSettableDeviceStates = new ArrayList<>(mDeviceStateRotationLockDefaults.length);
|
||||
mDeviceStateRotationLockSettings = new SparseIntArray(
|
||||
mDeviceStateRotationLockDefaults.length);
|
||||
mDeviceStateRotationLockFallbackSettings = new SparseIntArray(1);
|
||||
@@ -271,6 +300,8 @@ public final class DeviceStateRotationLockSettingsManager {
|
||||
+ values.length);
|
||||
}
|
||||
}
|
||||
boolean isSettable = rotationLockSetting != DEVICE_STATE_ROTATION_LOCK_IGNORED;
|
||||
mSettableDeviceStates.add(new SettableDeviceState(deviceState, isSettable));
|
||||
mDeviceStateRotationLockSettings.put(deviceState, rotationLockSetting);
|
||||
} catch (NumberFormatException e) {
|
||||
Log.wtf(TAG, "Error parsing settings entry. Entry was: " + entry, e);
|
||||
@@ -300,4 +331,38 @@ public final class DeviceStateRotationLockSettingsManager {
|
||||
/** Called whenever the settings have changed. */
|
||||
void onSettingsChanged();
|
||||
}
|
||||
|
||||
/** Represents a device state and whether it has an auto-rotation setting. */
|
||||
public static class SettableDeviceState {
|
||||
private final int mDeviceState;
|
||||
private final boolean mIsSettable;
|
||||
|
||||
SettableDeviceState(int deviceState, boolean isSettable) {
|
||||
mDeviceState = deviceState;
|
||||
mIsSettable = isSettable;
|
||||
}
|
||||
|
||||
/** Returns the device state associated with this object. */
|
||||
public int getDeviceState() {
|
||||
return mDeviceState;
|
||||
}
|
||||
|
||||
/** Returns whether there is an auto-rotation setting for this device state. */
|
||||
public boolean isSettable() {
|
||||
return mIsSettable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof SettableDeviceState)) return false;
|
||||
SettableDeviceState that = (SettableDeviceState) o;
|
||||
return mDeviceState == that.mDeviceState && mIsSettable == that.mIsSettable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(mDeviceState, mIsSettable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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 com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
|
||||
import androidx.test.InstrumentationRegistry;
|
||||
import androidx.test.filters.SmallTest;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import com.android.internal.R;
|
||||
import com.android.settingslib.devicestate.DeviceStateRotationLockSettingsManager.SettableDeviceState;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@SmallTest
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class DeviceStateRotationLockSettingsManagerTest {
|
||||
|
||||
@Mock private Context mMockContext;
|
||||
@Mock private Resources mMockResources;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
Context context = InstrumentationRegistry.getTargetContext();
|
||||
when(mMockContext.getApplicationContext()).thenReturn(mMockContext);
|
||||
when(mMockContext.getResources()).thenReturn(mMockResources);
|
||||
when(mMockContext.getContentResolver()).thenReturn(context.getContentResolver());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSettableDeviceStates_returnsExpectedValuesInOriginalOrder() {
|
||||
when(mMockResources.getStringArray(
|
||||
R.array.config_perDeviceStateRotationLockDefaults)).thenReturn(
|
||||
new String[]{"2:2", "4:0", "1:1", "0:0"});
|
||||
|
||||
List<SettableDeviceState> settableDeviceStates =
|
||||
DeviceStateRotationLockSettingsManager.getInstance(
|
||||
mMockContext).getSettableDeviceStates();
|
||||
|
||||
assertThat(settableDeviceStates).containsExactly(
|
||||
new SettableDeviceState(/* deviceState= */ 2, /* isSettable= */ true),
|
||||
new SettableDeviceState(/* deviceState= */ 4, /* isSettable= */ false),
|
||||
new SettableDeviceState(/* deviceState= */ 1, /* isSettable= */ true),
|
||||
new SettableDeviceState(/* deviceState= */ 0, /* isSettable= */ false)
|
||||
).inOrder();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user