Root cause: The ag/3373822 introduce the comparison by string since 2017, but it would conflict with AccessibilityService#setAnimationScale(float) which is added by API level 33. Solution: Use float comparison instead and observe the key changed to update status if the setting page is on foreground. Bug: 244687190 Test: atest DisableAnimationsPreferenceControllerTest Change-Id: I59b7edb70a27af4276eccd6f19d2ef46ba30b604
126 lines
4.2 KiB
Java
126 lines
4.2 KiB
Java
/*
|
|
* Copyright (C) 2019 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package com.android.settings.accessibility;
|
|
|
|
import android.content.ContentResolver;
|
|
import android.content.Context;
|
|
import android.database.ContentObserver;
|
|
import android.os.Handler;
|
|
import android.os.Looper;
|
|
import android.os.UserHandle;
|
|
import android.provider.Settings;
|
|
|
|
import androidx.annotation.VisibleForTesting;
|
|
import androidx.preference.PreferenceScreen;
|
|
import androidx.preference.SwitchPreference;
|
|
|
|
import com.android.settings.R;
|
|
import com.android.settings.core.TogglePreferenceController;
|
|
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
|
import com.android.settingslib.core.lifecycle.events.OnStart;
|
|
import com.android.settingslib.core.lifecycle.events.OnStop;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
/** A toggle preference controller for disable animations. */
|
|
public class DisableAnimationsPreferenceController extends TogglePreferenceController implements
|
|
LifecycleObserver, OnStart, OnStop {
|
|
|
|
@VisibleForTesting
|
|
static final float ANIMATION_ON_VALUE = 1.0f;
|
|
@VisibleForTesting
|
|
static final float ANIMATION_OFF_VALUE = 0.0f;
|
|
|
|
// Settings that should be changed when toggling animations
|
|
@VisibleForTesting
|
|
static final List<String> TOGGLE_ANIMATION_TARGETS = Arrays.asList(
|
|
Settings.Global.WINDOW_ANIMATION_SCALE, Settings.Global.TRANSITION_ANIMATION_SCALE,
|
|
Settings.Global.ANIMATOR_DURATION_SCALE
|
|
);
|
|
|
|
private final ContentObserver mSettingsContentObserver = new ContentObserver(
|
|
new Handler(Looper.getMainLooper())){
|
|
@Override
|
|
public void onChange(boolean selfChange) {
|
|
updateState(mPreference);
|
|
}
|
|
};
|
|
|
|
private final ContentResolver mContentResolver;
|
|
private SwitchPreference mPreference;
|
|
|
|
public DisableAnimationsPreferenceController(Context context, String preferenceKey) {
|
|
super(context, preferenceKey);
|
|
mContentResolver = context.getContentResolver();
|
|
}
|
|
|
|
@Override
|
|
public boolean isChecked() {
|
|
boolean allAnimationsDisabled = true;
|
|
for (String animationSetting : TOGGLE_ANIMATION_TARGETS) {
|
|
final float value = Settings.Global.getFloat(mContentResolver, animationSetting,
|
|
ANIMATION_ON_VALUE);
|
|
if (value > ANIMATION_OFF_VALUE) {
|
|
allAnimationsDisabled = false;
|
|
break;
|
|
}
|
|
}
|
|
return allAnimationsDisabled;
|
|
}
|
|
|
|
@Override
|
|
public boolean setChecked(boolean isChecked) {
|
|
final float newAnimationValue = isChecked ? ANIMATION_OFF_VALUE : ANIMATION_ON_VALUE;
|
|
boolean allAnimationSet = true;
|
|
for (String animationPreference : TOGGLE_ANIMATION_TARGETS) {
|
|
allAnimationSet &= Settings.Global.putFloat(mContentResolver, animationPreference,
|
|
newAnimationValue);
|
|
}
|
|
return allAnimationSet;
|
|
}
|
|
|
|
@Override
|
|
public int getAvailabilityStatus() {
|
|
return AVAILABLE;
|
|
}
|
|
|
|
@Override
|
|
public int getSliceHighlightMenuRes() {
|
|
return R.string.menu_key_accessibility;
|
|
}
|
|
|
|
@Override
|
|
public void displayPreference(PreferenceScreen screen) {
|
|
super.displayPreference(screen);
|
|
mPreference = screen.findPreference(getPreferenceKey());
|
|
}
|
|
|
|
@Override
|
|
public void onStart() {
|
|
for (String key : TOGGLE_ANIMATION_TARGETS) {
|
|
mContentResolver.registerContentObserver(Settings.Global.getUriFor(key),
|
|
false, mSettingsContentObserver, UserHandle.USER_ALL);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onStop() {
|
|
mContentResolver.unregisterContentObserver(mSettingsContentObserver);
|
|
}
|
|
}
|