Home | History | Annotate | Download | only in development
      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 import android.support.annotation.VisibleForTesting;
     23 import android.support.v14.preference.SwitchPreference;
     24 import android.support.v7.preference.Preference;
     25 import android.text.TextUtils;
     26 
     27 import com.android.settings.core.PreferenceControllerMixin;
     28 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
     29 
     30 public class FreeformWindowsPreferenceController extends DeveloperOptionsPreferenceController
     31         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
     32 
     33     private static final String ENABLE_FREEFORM_SUPPORT_KEY = "enable_freeform_support";
     34 
     35     @VisibleForTesting
     36     static final int SETTING_VALUE_OFF = 0;
     37     @VisibleForTesting
     38     static final int SETTING_VALUE_ON = 1;
     39     @VisibleForTesting
     40     static final String USER_BUILD_TYPE = "user";
     41 
     42     public FreeformWindowsPreferenceController(Context context) {
     43         super(context);
     44     }
     45 
     46     @Override
     47     public boolean isAvailable() {
     48         return !TextUtils.equals(USER_BUILD_TYPE, getBuildType());
     49     }
     50 
     51     @Override
     52     public String getPreferenceKey() {
     53         return ENABLE_FREEFORM_SUPPORT_KEY;
     54     }
     55 
     56     @Override
     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         return true;
     63     }
     64 
     65     @Override
     66     public void updateState(Preference preference) {
     67         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
     68                 Settings.Global.DEVELOPMENT_ENABLE_FREEFORM_WINDOWS_SUPPORT, SETTING_VALUE_OFF);
     69         ((SwitchPreference) mPreference).setChecked(mode != SETTING_VALUE_OFF);
     70     }
     71 
     72     @Override
     73     protected void onDeveloperOptionsSwitchDisabled() {
     74         super.onDeveloperOptionsSwitchDisabled();
     75         Settings.Global.putInt(mContext.getContentResolver(),
     76                 Settings.Global.DEVELOPMENT_ENABLE_FREEFORM_WINDOWS_SUPPORT, SETTING_VALUE_OFF);
     77         ((SwitchPreference) mPreference).setChecked(false);
     78     }
     79 
     80     @VisibleForTesting
     81     String getBuildType() {
     82         return Build.TYPE;
     83     }
     84 }
     85