• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 AnimatorDurationScalePreferenceController extends DeveloperOptionsPreferenceController
33         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
34 
35     private static final String ANIMATOR_DURATION_SCALE_KEY = "animator_duration_scale";
36 
37     @VisibleForTesting
38     static final int ANIMATOR_DURATION_SCALE_SELECTOR = 2;
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 
AnimatorDurationScalePreferenceController(Context context)46     public AnimatorDurationScalePreferenceController(Context context) {
47         super(context);
48 
49         mWindowManager = IWindowManager.Stub.asInterface(
50                 ServiceManager.getService(Context.WINDOW_SERVICE));
51         mListValues = context.getResources().getStringArray(R.array.animator_duration_scale_values);
52         mListSummaries = context.getResources().getStringArray(
53                 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