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;
18 
19 import android.content.Context;
20 import android.os.Build;
21 import android.provider.Settings;
22 
23 import androidx.annotation.Nullable;
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.Preference;
26 import androidx.preference.TwoStatePreference;
27 
28 import com.android.settings.R;
29 import com.android.settings.core.PreferenceControllerMixin;
30 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
31 
32 public class FreeformWindowsPreferenceController extends DeveloperOptionsPreferenceController
33         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin,
34         RebootConfirmationDialogHost {
35 
36     private static final String ENABLE_FREEFORM_SUPPORT_KEY = "enable_freeform_support";
37 
38     @VisibleForTesting
39     static final int SETTING_VALUE_OFF = 0;
40     @VisibleForTesting
41     static final int SETTING_VALUE_ON = 1;
42 
43     @Nullable private final DevelopmentSettingsDashboardFragment mFragment;
44 
FreeformWindowsPreferenceController( Context context, @Nullable DevelopmentSettingsDashboardFragment fragment)45     public FreeformWindowsPreferenceController(
46             Context context, @Nullable DevelopmentSettingsDashboardFragment fragment) {
47         super(context);
48         mFragment = fragment;
49     }
50 
51     @Override
getPreferenceKey()52     public String getPreferenceKey() {
53         return ENABLE_FREEFORM_SUPPORT_KEY;
54     }
55 
56     @Override
onPreferenceChange(Preference preference, Object newValue)57     public boolean onPreferenceChange(Preference preference, Object newValue) {
58         final boolean isEnabled = (Boolean) newValue;
59         Settings.Global.putInt(mContext.getContentResolver(),
60                 Settings.Global.DEVELOPMENT_ENABLE_FREEFORM_WINDOWS_SUPPORT,
61                 isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF);
62         if (isEnabled) {
63             RebootConfirmationDialogFragment.show(
64                     mFragment, R.string.reboot_dialog_enable_freeform_support, this);
65         }
66         return true;
67     }
68 
69     @Override
updateState(Preference preference)70     public void updateState(Preference preference) {
71         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
72                 Settings.Global.DEVELOPMENT_ENABLE_FREEFORM_WINDOWS_SUPPORT, SETTING_VALUE_OFF);
73         ((TwoStatePreference) mPreference).setChecked(mode != SETTING_VALUE_OFF);
74     }
75 
76     @Override
onDeveloperOptionsSwitchDisabled()77     protected void onDeveloperOptionsSwitchDisabled() {
78         super.onDeveloperOptionsSwitchDisabled();
79         Settings.Global.putInt(mContext.getContentResolver(),
80                 Settings.Global.DEVELOPMENT_ENABLE_FREEFORM_WINDOWS_SUPPORT, SETTING_VALUE_OFF);
81         ((TwoStatePreference) mPreference).setChecked(false);
82     }
83 
84     @VisibleForTesting
getBuildType()85     String getBuildType() {
86         return Build.TYPE;
87     }
88 }
89