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