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