1 /*
2  * Copyright (C) 2018 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 static android.provider.Settings.Global.DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS;
20 
21 import android.content.Context;
22 import android.os.Build;
23 import android.provider.Settings;
24 
25 import androidx.annotation.VisibleForTesting;
26 import androidx.preference.Preference;
27 import androidx.preference.SwitchPreference;
28 
29 import com.android.settings.core.PreferenceControllerMixin;
30 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
31 
32 public class DesktopModePreferenceController extends DeveloperOptionsPreferenceController
33         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
34 
35     private static final String FORCE_DESKTOP_MODE_KEY = "force_desktop_mode_on_external_displays";
36 
37     @VisibleForTesting
38     static final int SETTING_VALUE_OFF = 0;
39     @VisibleForTesting
40     static final int SETTING_VALUE_ON = 1;
41 
DesktopModePreferenceController(Context context)42     public DesktopModePreferenceController(Context context) {
43         super(context);
44     }
45 
46     @Override
getPreferenceKey()47     public String getPreferenceKey() {
48         return FORCE_DESKTOP_MODE_KEY;
49     }
50 
51     @Override
onPreferenceChange(Preference preference, Object newValue)52     public boolean onPreferenceChange(Preference preference, Object newValue) {
53         final boolean isEnabled = (Boolean) newValue;
54         Settings.Global.putInt(mContext.getContentResolver(),
55                 DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS,
56                 isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF);
57         return true;
58     }
59 
60     @Override
updateState(Preference preference)61     public void updateState(Preference preference) {
62         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
63                 DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS, SETTING_VALUE_OFF);
64         ((SwitchPreference) mPreference).setChecked(mode != SETTING_VALUE_OFF);
65     }
66 
67     @Override
onDeveloperOptionsSwitchDisabled()68     protected void onDeveloperOptionsSwitchDisabled() {
69         super.onDeveloperOptionsSwitchDisabled();
70         Settings.Global.putInt(mContext.getContentResolver(),
71                 DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS, SETTING_VALUE_OFF);
72         ((SwitchPreference) mPreference).setChecked(false);
73     }
74 
75     @VisibleForTesting
getBuildType()76     String getBuildType() {
77         return Build.TYPE;
78     }
79 }
80