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.os.RemoteException; 21 import android.os.ServiceManager; 22 import android.view.IWindowManager; 23 24 import androidx.annotation.VisibleForTesting; 25 import androidx.preference.ListPreference; 26 import androidx.preference.Preference; 27 28 import com.android.settings.core.PreferenceControllerMixin; 29 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 30 31 public class AnimatorDurationScalePreferenceController extends DeveloperOptionsPreferenceController 32 implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin { 33 34 private static final String ANIMATOR_DURATION_SCALE_KEY = "animator_duration_scale"; 35 36 @VisibleForTesting 37 static final int ANIMATOR_DURATION_SCALE_SELECTOR = 2; 38 @VisibleForTesting 39 static final float DEFAULT_VALUE = 1; 40 41 private final IWindowManager mWindowManager; 42 private final String[] mListValues; 43 private final String[] mListSummaries; 44 AnimatorDurationScalePreferenceController(Context context)45 public AnimatorDurationScalePreferenceController(Context context) { 46 super(context); 47 48 mWindowManager = IWindowManager.Stub.asInterface( 49 ServiceManager.getService(Context.WINDOW_SERVICE)); 50 mListValues = context.getResources() 51 .getStringArray(com.android.settingslib.R.array.animator_duration_scale_values); 52 mListSummaries = context.getResources().getStringArray( 53 com.android.settingslib.R.array.animator_duration_scale_entries); 54 } 55 56 @Override getPreferenceKey()57 public String getPreferenceKey() { 58 return ANIMATOR_DURATION_SCALE_KEY; 59 } 60 61 @Override onPreferenceChange(Preference preference, Object newValue)62 public boolean onPreferenceChange(Preference preference, Object newValue) { 63 writeAnimationScaleOption(newValue); 64 return true; 65 } 66 67 @Override updateState(Preference preference)68 public void updateState(Preference preference) { 69 updateAnimationScaleValue(); 70 } 71 72 @Override onDeveloperOptionsSwitchDisabled()73 protected void onDeveloperOptionsSwitchDisabled() { 74 super.onDeveloperOptionsSwitchDisabled(); 75 writeAnimationScaleOption(null); 76 } 77 writeAnimationScaleOption(Object newValue)78 private void writeAnimationScaleOption(Object newValue) { 79 try { 80 float scale = newValue != null ? Float.parseFloat(newValue.toString()) : DEFAULT_VALUE; 81 mWindowManager.setAnimationScale(ANIMATOR_DURATION_SCALE_SELECTOR, scale); 82 updateAnimationScaleValue(); 83 } catch (RemoteException e) { 84 // intentional no-op 85 } 86 } 87 updateAnimationScaleValue()88 private void updateAnimationScaleValue() { 89 try { 90 final float scale = mWindowManager.getAnimationScale(ANIMATOR_DURATION_SCALE_SELECTOR); 91 int index = 0; // default 92 for (int i = 0; i < mListValues.length; i++) { 93 float val = Float.parseFloat(mListValues[i]); 94 if (scale <= val) { 95 index = i; 96 break; 97 } 98 } 99 final ListPreference listPreference = (ListPreference) mPreference; 100 listPreference.setValue(mListValues[index]); 101 listPreference.setSummary(mListSummaries[index]); 102 } catch (RemoteException e) { 103 // intentional no-op 104 } 105 } 106 } 107