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