diff --git a/res/values/strings.xml b/res/values/strings.xml index 503da8732d2..4242609ec64 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -11076,6 +11076,10 @@ Force desktop mode Force experimental desktop mode on secondary displays + + Enable freeform sizecompat + + Allows sizecompat apps to be in freeform Override force-dark diff --git a/res/xml/development_settings.xml b/res/xml/development_settings.xml index a89997a7826..fb1e0baf76d 100644 --- a/res/xml/development_settings.xml +++ b/res/xml/development_settings.xml @@ -542,6 +542,11 @@ android:title="@string/force_desktop_mode" android:summary="@string/force_desktop_mode_summary" /> + + diff --git a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java index 8e8a82d9231..4342c5840c3 100644 --- a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java +++ b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java @@ -495,6 +495,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra controllers.add(new ResizableActivityPreferenceController(context)); controllers.add(new FreeformWindowsPreferenceController(context)); controllers.add(new DesktopModePreferenceController(context)); + controllers.add(new SizeCompatFreeformPreferenceController(context)); controllers.add(new ShortcutManagerThrottlingPreferenceController(context)); controllers.add(new BubbleGlobalPreferenceController(context)); controllers.add(new EnableGnssRawMeasFullTrackingPreferenceController(context)); diff --git a/src/com/android/settings/development/SizeCompatFreeformPreferenceController.java b/src/com/android/settings/development/SizeCompatFreeformPreferenceController.java new file mode 100644 index 00000000000..2e54d1df988 --- /dev/null +++ b/src/com/android/settings/development/SizeCompatFreeformPreferenceController.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2019 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.development; + +import static android.provider.Settings.Global.DEVELOPMENT_ENABLE_SIZECOMPAT_FREEFORM; + +import android.content.Context; +import android.provider.Settings; + +import androidx.annotation.VisibleForTesting; +import androidx.preference.Preference; +import androidx.preference.SwitchPreference; + +import com.android.settings.core.PreferenceControllerMixin; +import com.android.settingslib.development.DeveloperOptionsPreferenceController; + +/** + * Preference Controller for whether to allow launching size-compat apps in freeform windows. + */ +public class SizeCompatFreeformPreferenceController extends DeveloperOptionsPreferenceController + implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin { + + private static final String ENABLE_SIZECOMPAT_FREEFORM_KEY = "enable_sizecompat_freeform"; + + @VisibleForTesting + static final int SETTING_VALUE_OFF = 0; + @VisibleForTesting + static final int SETTING_VALUE_ON = 1; + + public SizeCompatFreeformPreferenceController(Context context) { + super(context); + } + + @Override + public String getPreferenceKey() { + return ENABLE_SIZECOMPAT_FREEFORM_KEY; + } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + final boolean isEnabled = (Boolean) newValue; + Settings.Global.putInt(mContext.getContentResolver(), + DEVELOPMENT_ENABLE_SIZECOMPAT_FREEFORM, + isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF); + return true; + } + + @Override + public void updateState(Preference preference) { + final int mode = Settings.Global.getInt(mContext.getContentResolver(), + DEVELOPMENT_ENABLE_SIZECOMPAT_FREEFORM, SETTING_VALUE_OFF); + ((SwitchPreference) mPreference).setChecked(mode != SETTING_VALUE_OFF); + } + + @Override + protected void onDeveloperOptionsSwitchDisabled() { + super.onDeveloperOptionsSwitchDisabled(); + Settings.Global.putInt(mContext.getContentResolver(), + DEVELOPMENT_ENABLE_SIZECOMPAT_FREEFORM, SETTING_VALUE_OFF); + ((SwitchPreference) mPreference).setChecked(false); + } +} diff --git a/tests/robotests/src/com/android/settings/development/SizeCompatFreeformPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/SizeCompatFreeformPreferenceControllerTest.java new file mode 100644 index 00000000000..7e37b267cb5 --- /dev/null +++ b/tests/robotests/src/com/android/settings/development/SizeCompatFreeformPreferenceControllerTest.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2019 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.development; + +import static android.provider.Settings.Global.DEVELOPMENT_ENABLE_SIZECOMPAT_FREEFORM; + +import static com.android.settings.development.SizeCompatFreeformPreferenceController.SETTING_VALUE_OFF; +import static com.android.settings.development.SizeCompatFreeformPreferenceController.SETTING_VALUE_ON; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.provider.Settings; + +import androidx.preference.PreferenceScreen; +import androidx.preference.SwitchPreference; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; + +@RunWith(RobolectricTestRunner.class) +public class SizeCompatFreeformPreferenceControllerTest { + + @Mock + private SwitchPreference mPreference; + @Mock + private PreferenceScreen mScreen; + + private Context mContext; + private SizeCompatFreeformPreferenceController mController; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + mContext = RuntimeEnvironment.application; + mController = new SizeCompatFreeformPreferenceController(mContext); + when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); + mController.displayPreference(mScreen); + } + + @Test + public void onPreferenceChange_switchEnabled_shouldEnableSizeCompatFreeform() { + mController.onPreferenceChange(mPreference, true /* new value */); + + final int mode = Settings.Global.getInt(mContext.getContentResolver(), + DEVELOPMENT_ENABLE_SIZECOMPAT_FREEFORM, -1 /* default */); + assertThat(mode).isEqualTo(SETTING_VALUE_ON); + } + + @Test + public void onPreferenceChange_switchDisabled_shouldDisableSizeCompatFreeform() { + mController.onPreferenceChange(mPreference, false /* new value */); + + final int mode = Settings.Global.getInt(mContext.getContentResolver(), + DEVELOPMENT_ENABLE_SIZECOMPAT_FREEFORM, -1 /* default */); + assertThat(mode).isEqualTo(SETTING_VALUE_OFF); + } + + @Test + public void updateState_settingEnabled_preferenceShouldBeChecked() { + Settings.Global.putInt(mContext.getContentResolver(), + DEVELOPMENT_ENABLE_SIZECOMPAT_FREEFORM, SETTING_VALUE_ON); + + mController.updateState(mPreference); + + verify(mPreference).setChecked(true); + } + + @Test + public void updateState_settingDisabled_preferenceShouldNotBeChecked() { + Settings.Global.putInt(mContext.getContentResolver(), + DEVELOPMENT_ENABLE_SIZECOMPAT_FREEFORM, SETTING_VALUE_OFF); + + mController.updateState(mPreference); + + verify(mPreference).setChecked(false); + } + + @Test + public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() { + mController.onDeveloperOptionsSwitchDisabled(); + + final int mode = Settings.Global.getInt(mContext.getContentResolver(), + DEVELOPMENT_ENABLE_SIZECOMPAT_FREEFORM, -1 /* default */); + assertThat(mode).isEqualTo(SETTING_VALUE_OFF); + verify(mPreference).setEnabled(false); + } +}