blob: ac683641feab4722fdec00972598040abebb0eff [file] [log] [blame]
jeffreyhuang93c41ba2017-10-11 16:41:17 -07001/*
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
17package com.android.settings.development;
18
19import android.content.Context;
20import android.os.RemoteException;
21import android.os.ServiceManager;
jeffreyhuang93c41ba2017-10-11 16:41:17 -070022import android.view.IWindowManager;
23
Fan Zhangc7162cd2018-06-18 15:21:41 -070024import androidx.annotation.VisibleForTesting;
25import androidx.preference.ListPreference;
26import androidx.preference.Preference;
27
Fan Zhang23f8d592018-08-28 15:11:40 -070028import com.android.settings.core.PreferenceControllerMixin;
29import com.android.settingslib.development.DeveloperOptionsPreferenceController;
30
jeffreyhuang93c41ba2017-10-11 16:41:17 -070031public 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;
jeffreyhuang93c41ba2017-10-11 16:41:17 -070045
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 Wang25413812023-07-31 15:56:42 +080052 com.android.settingslib.R.array.transition_animation_scale_values);
jeffreyhuang93c41ba2017-10-11 16:41:17 -070053 mListSummaries = context.getResources().getStringArray(
Chaohui Wang25413812023-07-31 15:56:42 +080054 com.android.settingslib.R.array.transition_animation_scale_entries);
jeffreyhuang93c41ba2017-10-11 16:41:17 -070055 }
56
57 @Override
58 public String getPreferenceKey() {
59 return TRANSITION_ANIMATION_SCALE_KEY;
60 }
61
62 @Override
jeffreyhuang93c41ba2017-10-11 16:41:17 -070063 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
jeffreyhuang93c41ba2017-10-11 16:41:17 -070074 protected void onDeveloperOptionsSwitchDisabled() {
Doris Ling4fbf04c2018-03-01 10:33:14 -080075 super.onDeveloperOptionsSwitchDisabled();
jeffreyhuang93c41ba2017-10-11 16:41:17 -070076 writeAnimationScaleOption(null);
jeffreyhuang93c41ba2017-10-11 16:41:17 -070077 }
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 Ling4fbf04c2018-03-01 10:33:14 -0800101 final ListPreference listPreference = (ListPreference) mPreference;
102 listPreference.setValue(mListValues[index]);
103 listPreference.setSummary(mListSummaries[index]);
jeffreyhuang93c41ba2017-10-11 16:41:17 -0700104 } catch (RemoteException e) {
105 // intentional no-op
106 }
107 }
108}