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.ContentResolver; 20 import android.content.Context; 21 import android.database.ContentObserver; 22 import android.os.Handler; 23 import android.os.Looper; 24 import android.os.UserHandle; 25 import android.provider.Settings; 26 27 import androidx.annotation.VisibleForTesting; 28 import androidx.preference.PreferenceScreen; 29 import androidx.preference.TwoStatePreference; 30 31 import com.android.settings.R; 32 import com.android.settings.core.TogglePreferenceController; 33 import com.android.settingslib.core.lifecycle.LifecycleObserver; 34 import com.android.settingslib.core.lifecycle.events.OnStart; 35 import com.android.settingslib.core.lifecycle.events.OnStop; 36 37 import java.util.Arrays; 38 import java.util.List; 39 40 /** A toggle preference controller for disable animations. */ 41 public class DisableAnimationsPreferenceController extends TogglePreferenceController implements 42 LifecycleObserver, OnStart, OnStop { 43 44 @VisibleForTesting 45 static final float ANIMATION_ON_VALUE = 1.0f; 46 @VisibleForTesting 47 static final float ANIMATION_OFF_VALUE = 0.0f; 48 49 // Settings that should be changed when toggling animations 50 @VisibleForTesting 51 static final List<String> TOGGLE_ANIMATION_TARGETS = Arrays.asList( 52 Settings.Global.WINDOW_ANIMATION_SCALE, Settings.Global.TRANSITION_ANIMATION_SCALE, 53 Settings.Global.ANIMATOR_DURATION_SCALE 54 ); 55 56 private final ContentObserver mSettingsContentObserver = new ContentObserver( 57 new Handler(Looper.getMainLooper())){ 58 @Override 59 public void onChange(boolean selfChange) { 60 updateState(mPreference); 61 } 62 }; 63 64 private final ContentResolver mContentResolver; 65 private TwoStatePreference mPreference; 66 DisableAnimationsPreferenceController(Context context, String preferenceKey)67 public DisableAnimationsPreferenceController(Context context, String preferenceKey) { 68 super(context, preferenceKey); 69 mContentResolver = context.getContentResolver(); 70 } 71 72 @Override isChecked()73 public boolean isChecked() { 74 boolean allAnimationsDisabled = true; 75 for (String animationSetting : TOGGLE_ANIMATION_TARGETS) { 76 final float value = Settings.Global.getFloat(mContentResolver, animationSetting, 77 ANIMATION_ON_VALUE); 78 if (value > ANIMATION_OFF_VALUE) { 79 allAnimationsDisabled = false; 80 break; 81 } 82 } 83 return allAnimationsDisabled; 84 } 85 86 @Override setChecked(boolean isChecked)87 public boolean setChecked(boolean isChecked) { 88 final float newAnimationValue = isChecked ? ANIMATION_OFF_VALUE : ANIMATION_ON_VALUE; 89 boolean allAnimationSet = true; 90 for (String animationPreference : TOGGLE_ANIMATION_TARGETS) { 91 allAnimationSet &= Settings.Global.putFloat(mContentResolver, animationPreference, 92 newAnimationValue); 93 } 94 return allAnimationSet; 95 } 96 97 @Override getAvailabilityStatus()98 public int getAvailabilityStatus() { 99 return AVAILABLE; 100 } 101 102 @Override getSliceHighlightMenuRes()103 public int getSliceHighlightMenuRes() { 104 return R.string.menu_key_accessibility; 105 } 106 107 @Override displayPreference(PreferenceScreen screen)108 public void displayPreference(PreferenceScreen screen) { 109 super.displayPreference(screen); 110 mPreference = screen.findPreference(getPreferenceKey()); 111 } 112 113 @Override onStart()114 public void onStart() { 115 for (String key : TOGGLE_ANIMATION_TARGETS) { 116 mContentResolver.registerContentObserver(Settings.Global.getUriFor(key), 117 false, mSettingsContentObserver, UserHandle.USER_ALL); 118 } 119 } 120 121 @Override onStop()122 public void onStop() { 123 mContentResolver.unregisterContentObserver(mSettingsContentObserver); 124 } 125 } 126