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