1 /* 2 * Copyright (C) 2022 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.provider.Settings; 21 22 import androidx.preference.Preference; 23 import androidx.preference.TwoStatePreference; 24 25 import com.android.settings.core.PreferenceControllerMixin; 26 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 27 28 class StylusHandwritingPreferenceController extends DeveloperOptionsPreferenceController 29 implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin { 30 31 static final int SETTING_VALUE_ON = 1; 32 static final int SETTING_VALUE_OFF = 0; 33 34 private static final String STYLUS_HANDWRITING_OPTIONS_KEY = "stylus_handwriting"; 35 StylusHandwritingPreferenceController(Context context)36 StylusHandwritingPreferenceController(Context context) { 37 super(context); 38 } 39 40 @Override getPreferenceKey()41 public String getPreferenceKey() { 42 return STYLUS_HANDWRITING_OPTIONS_KEY; 43 } 44 45 @Override onPreferenceChange(Preference preference, Object newValue)46 public boolean onPreferenceChange(Preference preference, Object newValue) { 47 final boolean isEnabled = (Boolean) newValue; 48 Settings.Secure.putInt(mContext.getContentResolver(), 49 Settings.Secure.STYLUS_HANDWRITING_ENABLED, 50 isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF); 51 52 return true; 53 } 54 55 @Override updateState(Preference preference)56 public void updateState(Preference preference) { 57 final int enable = Settings.Secure.getInt(mContext.getContentResolver(), 58 Settings.Secure.STYLUS_HANDWRITING_ENABLED, 59 Settings.Secure.STYLUS_HANDWRITING_DEFAULT_VALUE); 60 ((TwoStatePreference) mPreference).setChecked(enable != SETTING_VALUE_OFF); 61 } 62 63 @Override onDeveloperOptionsSwitchDisabled()64 protected void onDeveloperOptionsSwitchDisabled() { 65 super.onDeveloperOptionsSwitchDisabled(); 66 Settings.Secure.putInt(mContext.getContentResolver(), 67 Settings.Secure.STYLUS_HANDWRITING_ENABLED, 68 Settings.Secure.STYLUS_HANDWRITING_DEFAULT_VALUE); 69 ((TwoStatePreference) mPreference).setChecked(false); 70 } 71 } 72