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