• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.development.desktopexperience;
18 
19 import static android.content.pm.PackageManager.FEATURE_FREEFORM_WINDOW_MANAGEMENT;
20 
21 import android.content.Context;
22 import android.provider.Settings;
23 
24 import androidx.annotation.Nullable;
25 import androidx.annotation.VisibleForTesting;
26 import androidx.preference.Preference;
27 import androidx.preference.TwoStatePreference;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.PreferenceControllerMixin;
31 import com.android.settings.development.DevelopmentSettingsDashboardFragment;
32 import com.android.settings.development.RebootConfirmationDialogFragment;
33 import com.android.settings.development.RebootConfirmationDialogHost;
34 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
35 import com.android.wm.shell.shared.desktopmode.DesktopModeStatus;
36 
37 public class FreeformWindowsPreferenceController extends DeveloperOptionsPreferenceController
38         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin,
39         RebootConfirmationDialogHost {
40 
41     private static final String ENABLE_FREEFORM_SUPPORT_KEY = "enable_freeform_support";
42 
43     @VisibleForTesting
44     static final int SETTING_VALUE_OFF = 0;
45     @VisibleForTesting
46     static final int SETTING_VALUE_ON = 1;
47 
48     @Nullable
49     private final DevelopmentSettingsDashboardFragment mFragment;
50 
FreeformWindowsPreferenceController( Context context, @Nullable DevelopmentSettingsDashboardFragment fragment)51     public FreeformWindowsPreferenceController(
52             Context context, @Nullable DevelopmentSettingsDashboardFragment fragment) {
53         super(context);
54         mFragment = fragment;
55     }
56 
57     @Override
isAvailable()58     public boolean isAvailable() {
59         // When devices have the system feature FEATURE_FREEFORM_WINDOW_MANAGEMENT, freeform
60         // mode is enabled automatically, and this toggle is not needed.
61         return !mContext.getPackageManager().hasSystemFeature(FEATURE_FREEFORM_WINDOW_MANAGEMENT)
62                 && !DesktopModeStatus.canShowDesktopExperienceDevOption(mContext)
63                 && !DesktopModeStatus.canShowDesktopModeDevOption(mContext);
64     }
65 
66     @Override
getPreferenceKey()67     public String getPreferenceKey() {
68         return ENABLE_FREEFORM_SUPPORT_KEY;
69     }
70 
71     @Override
onPreferenceChange(Preference preference, Object newValue)72     public boolean onPreferenceChange(Preference preference, Object newValue) {
73         final boolean isEnabled = (Boolean) newValue;
74         Settings.Global.putInt(mContext.getContentResolver(),
75                 Settings.Global.DEVELOPMENT_ENABLE_FREEFORM_WINDOWS_SUPPORT,
76                 isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF);
77         if (isEnabled) {
78             RebootConfirmationDialogFragment.show(
79                     mFragment, R.string.reboot_dialog_enable_freeform_support, this);
80         }
81         return true;
82     }
83 
84     @Override
updateState(Preference preference)85     public void updateState(Preference preference) {
86         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
87                 Settings.Global.DEVELOPMENT_ENABLE_FREEFORM_WINDOWS_SUPPORT, SETTING_VALUE_OFF);
88         ((TwoStatePreference) mPreference).setChecked(mode != SETTING_VALUE_OFF);
89     }
90 
91     @Override
onDeveloperOptionsSwitchDisabled()92     protected void onDeveloperOptionsSwitchDisabled() {
93         super.onDeveloperOptionsSwitchDisabled();
94         Settings.Global.putInt(mContext.getContentResolver(),
95                 Settings.Global.DEVELOPMENT_ENABLE_FREEFORM_WINDOWS_SUPPORT, SETTING_VALUE_OFF);
96         ((TwoStatePreference) mPreference).setChecked(false);
97     }
98 }
99