| jeffreyhuang | 93c41ba | 2017-10-11 16:41:17 -0700 | [diff] [blame] | 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; |
| jeffreyhuang | 93c41ba | 2017-10-11 16:41:17 -0700 | [diff] [blame] | 22 | import android.view.IWindowManager; |
| 23 | |
| Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 24 | import androidx.annotation.VisibleForTesting; |
| 25 | import androidx.preference.ListPreference; |
| 26 | import androidx.preference.Preference; |
| 27 | |
| Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 28 | import com.android.settings.core.PreferenceControllerMixin; |
| 29 | import com.android.settingslib.development.DeveloperOptionsPreferenceController; |
| 30 | |
| jeffreyhuang | 93c41ba | 2017-10-11 16:41:17 -0700 | [diff] [blame] | 31 | public class TransitionAnimationScalePreferenceController extends |
| 32 | DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener, |
| 33 | PreferenceControllerMixin { |
| 34 | |
| 35 | private static final String TRANSITION_ANIMATION_SCALE_KEY = "transition_animation_scale"; |
| 36 | |
| 37 | @VisibleForTesting |
| 38 | static final int TRANSITION_ANIMATION_SCALE_SELECTOR = 1; |
| 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; |
| jeffreyhuang | 93c41ba | 2017-10-11 16:41:17 -0700 | [diff] [blame] | 45 | |
| 46 | public TransitionAnimationScalePreferenceController(Context context) { |
| 47 | super(context); |
| 48 | |
| 49 | mWindowManager = IWindowManager.Stub.asInterface( |
| 50 | ServiceManager.getService(Context.WINDOW_SERVICE)); |
| 51 | mListValues = context.getResources().getStringArray( |
| Chaohui Wang | 2541381 | 2023-07-31 15:56:42 +0800 | [diff] [blame^] | 52 | com.android.settingslib.R.array.transition_animation_scale_values); |
| jeffreyhuang | 93c41ba | 2017-10-11 16:41:17 -0700 | [diff] [blame] | 53 | mListSummaries = context.getResources().getStringArray( |
| Chaohui Wang | 2541381 | 2023-07-31 15:56:42 +0800 | [diff] [blame^] | 54 | com.android.settingslib.R.array.transition_animation_scale_entries); |
| jeffreyhuang | 93c41ba | 2017-10-11 16:41:17 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public String getPreferenceKey() { |
| 59 | return TRANSITION_ANIMATION_SCALE_KEY; |
| 60 | } |
| 61 | |
| 62 | @Override |
| jeffreyhuang | 93c41ba | 2017-10-11 16:41:17 -0700 | [diff] [blame] | 63 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 64 | writeAnimationScaleOption(newValue); |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void updateState(Preference preference) { |
| 70 | updateAnimationScaleValue(); |
| 71 | } |
| 72 | |
| 73 | @Override |
| jeffreyhuang | 93c41ba | 2017-10-11 16:41:17 -0700 | [diff] [blame] | 74 | protected void onDeveloperOptionsSwitchDisabled() { |
| Doris Ling | 4fbf04c | 2018-03-01 10:33:14 -0800 | [diff] [blame] | 75 | super.onDeveloperOptionsSwitchDisabled(); |
| jeffreyhuang | 93c41ba | 2017-10-11 16:41:17 -0700 | [diff] [blame] | 76 | writeAnimationScaleOption(null); |
| jeffreyhuang | 93c41ba | 2017-10-11 16:41:17 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | private void writeAnimationScaleOption(Object newValue) { |
| 80 | try { |
| 81 | float scale = newValue != null ? Float.parseFloat(newValue.toString()) : DEFAULT_VALUE; |
| 82 | mWindowManager.setAnimationScale(TRANSITION_ANIMATION_SCALE_SELECTOR, scale); |
| 83 | updateAnimationScaleValue(); |
| 84 | } catch (RemoteException e) { |
| 85 | // intentional no-op |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private void updateAnimationScaleValue() { |
| 90 | try { |
| 91 | final float scale = mWindowManager.getAnimationScale( |
| 92 | TRANSITION_ANIMATION_SCALE_SELECTOR); |
| 93 | int index = 0; // default |
| 94 | for (int i = 0; i < mListValues.length; i++) { |
| 95 | float val = Float.parseFloat(mListValues[i]); |
| 96 | if (scale <= val) { |
| 97 | index = i; |
| 98 | break; |
| 99 | } |
| 100 | } |
| Doris Ling | 4fbf04c | 2018-03-01 10:33:14 -0800 | [diff] [blame] | 101 | final ListPreference listPreference = (ListPreference) mPreference; |
| 102 | listPreference.setValue(mListValues[index]); |
| 103 | listPreference.setSummary(mListSummaries[index]); |
| jeffreyhuang | 93c41ba | 2017-10-11 16:41:17 -0700 | [diff] [blame] | 104 | } catch (RemoteException e) { |
| 105 | // intentional no-op |
| 106 | } |
| 107 | } |
| 108 | } |