• 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 WindowAnimationScalePreferenceController extends
33         DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener,
34         PreferenceControllerMixin {
35 
36     private static final String WINDOW_ANIMATION_SCALE_KEY = "window_animation_scale";
37 
38     @VisibleForTesting
39     static final int WINDOW_ANIMATION_SCALE_SELECTOR = 0;
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 
WindowAnimationScalePreferenceController(Context context)47     public WindowAnimationScalePreferenceController(Context context) {
48         super(context);
49 
50         mWindowManager = IWindowManager.Stub.asInterface(
51                 ServiceManager.getService(Context.WINDOW_SERVICE));
52         mListValues = context.getResources().getStringArray(R.array.window_animation_scale_values);
53         mListSummaries = context.getResources().getStringArray(
54                 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