1 /*
2  * Copyright (C) 2019 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.accessibility;
18 
19 import android.content.Context;
20 import android.provider.Settings;
21 import android.text.TextUtils;
22 
23 import androidx.annotation.VisibleForTesting;
24 
25 import com.android.settings.R;
26 import com.android.settings.core.TogglePreferenceController;
27 
28 public class DisableAnimationsPreferenceController extends TogglePreferenceController {
29 
30     @VisibleForTesting
31     static final String ANIMATION_ON_VALUE = "1";
32     @VisibleForTesting
33     static final String ANIMATION_OFF_VALUE = "0";
34 
35     // Settings that should be changed when toggling animations
36     @VisibleForTesting
37     static final String[] TOGGLE_ANIMATION_TARGETS = {
38             Settings.Global.WINDOW_ANIMATION_SCALE, Settings.Global.TRANSITION_ANIMATION_SCALE,
39             Settings.Global.ANIMATOR_DURATION_SCALE
40     };
41 
DisableAnimationsPreferenceController(Context context, String preferenceKey)42     public DisableAnimationsPreferenceController(Context context, String preferenceKey) {
43         super(context, preferenceKey);
44     }
45 
46     @Override
isChecked()47     public boolean isChecked() {
48         boolean allAnimationsDisabled = true;
49         for (String animationSetting : TOGGLE_ANIMATION_TARGETS) {
50             if (!TextUtils.equals(
51                     Settings.Global.getString(mContext.getContentResolver(), animationSetting),
52                     ANIMATION_OFF_VALUE)) {
53                 allAnimationsDisabled = false;
54                 break;
55             }
56         }
57         return allAnimationsDisabled;
58     }
59 
60     @Override
setChecked(boolean isChecked)61     public boolean setChecked(boolean isChecked) {
62         final String newAnimationValue = isChecked ? ANIMATION_OFF_VALUE : ANIMATION_ON_VALUE;
63         boolean allAnimationSet = true;
64         for (String animationPreference : TOGGLE_ANIMATION_TARGETS) {
65             allAnimationSet &= Settings.Global.putString(mContext.getContentResolver(),
66                     animationPreference, newAnimationValue);
67         }
68         return allAnimationSet;
69     }
70 
71     @Override
getAvailabilityStatus()72     public int getAvailabilityStatus() {
73         return AVAILABLE;
74     }
75 
76     @Override
getSliceHighlightMenuRes()77     public int getSliceHighlightMenuRes() {
78         return R.string.menu_key_accessibility;
79     }
80 }
81