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.provider.Settings; 21 22 import androidx.preference.Preference; 23 import androidx.preference.SwitchPreference; 24 25 import com.android.internal.annotations.VisibleForTesting; 26 import com.android.settings.core.PreferenceControllerMixin; 27 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 28 29 import java.util.Objects; 30 31 /** 32 * PreferenceController for enabling/disabling animation related to back button and back gestures. 33 */ 34 public class BackAnimationPreferenceController extends DeveloperOptionsPreferenceController 35 implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin { 36 37 private static final String BACK_NAVIGATION_ANIMATION_KEY = 38 "back_navigation_animation"; 39 40 private static final int SETTING_VALUE_OFF = 0; 41 private static final int SETTING_VALUE_ON = 1; 42 private final DevelopmentSettingsDashboardFragment mFragment; 43 44 @VisibleForTesting BackAnimationPreferenceController(Context context)45 BackAnimationPreferenceController(Context context) { 46 super(context); 47 mFragment = null; 48 } 49 50 BackAnimationPreferenceController(Context context, DevelopmentSettingsDashboardFragment fragment)51 public BackAnimationPreferenceController(Context context, 52 DevelopmentSettingsDashboardFragment fragment) { 53 super(context); 54 Objects.requireNonNull(fragment); 55 mFragment = fragment; 56 } 57 58 @Override getPreferenceKey()59 public String getPreferenceKey() { 60 return BACK_NAVIGATION_ANIMATION_KEY; 61 } 62 63 @Override onPreferenceChange(Preference preference, Object newValue)64 public boolean onPreferenceChange(Preference preference, Object newValue) { 65 final boolean isEnabled = (Boolean) newValue; 66 Settings.Global.putInt(mContext.getContentResolver(), 67 Settings.Global.ENABLE_BACK_ANIMATION, 68 isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF); 69 if (mFragment != null && isEnabled) { 70 BackAnimationPreferenceDialog.show(mFragment); 71 } 72 return true; 73 } 74 75 @Override updateState(Preference preference)76 public void updateState(Preference preference) { 77 final int mode = Settings.Global.getInt(mContext.getContentResolver(), 78 Settings.Global.ENABLE_BACK_ANIMATION, SETTING_VALUE_OFF); 79 ((SwitchPreference) mPreference).setChecked(mode != SETTING_VALUE_OFF); 80 } 81 82 @Override onDeveloperOptionsSwitchDisabled()83 protected void onDeveloperOptionsSwitchDisabled() { 84 super.onDeveloperOptionsSwitchDisabled(); 85 Settings.Global.putInt(mContext.getContentResolver(), 86 Settings.Global.ENABLE_BACK_ANIMATION, SETTING_VALUE_OFF); 87 ((SwitchPreference) mPreference).setChecked(false); 88 } 89 } 90