Home | History | Annotate | Download | only in development
      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 WindowAnimationScalePreferenceController extends
     32         DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener,
     33         PreferenceControllerMixin {
     34 
     35     private static final String WINDOW_ANIMATION_SCALE_KEY = "window_animation_scale";
     36 
     37     @VisibleForTesting
     38     static final int WINDOW_ANIMATION_SCALE_SELECTOR = 0;
     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 
     46     public WindowAnimationScalePreferenceController(Context context) {
     47         super(context);
     48 
     49         mWindowManager = IWindowManager.Stub.asInterface(
     50                 ServiceManager.getService(Context.WINDOW_SERVICE));
     51         mListValues = context.getResources().getStringArray(R.array.window_animation_scale_values);
     52         mListSummaries = context.getResources().getStringArray(
     53                 R.array.window_animation_scale_entries);
     54     }
     55 
     56     @Override
     57     public String getPreferenceKey() {
     58         return WINDOW_ANIMATION_SCALE_KEY;
     59     }
     60 
     61     @Override
     62     public boolean onPreferenceChange(Preference preference, Object newValue) {
     63         writeAnimationScaleOption(newValue);
     64         return true;
     65     }
     66 
     67     @Override
     68     public void updateState(Preference preference) {
     69         updateAnimationScaleValue();
     70     }
     71 
     72     @Override
     73     protected void onDeveloperOptionsSwitchDisabled() {
     74         super.onDeveloperOptionsSwitchDisabled();
     75         writeAnimationScaleOption(null);
     76     }
     77 
     78     private void writeAnimationScaleOption(Object newValue) {
     79         try {
     80             float scale = newValue != null ? Float.parseFloat(newValue.toString()) : DEFAULT_VALUE;
     81             mWindowManager.setAnimationScale(WINDOW_ANIMATION_SCALE_SELECTOR, scale);
     82             updateAnimationScaleValue();
     83         } catch (RemoteException e) {
     84             // intentional no-op
     85         }
     86     }
     87 
     88     private void updateAnimationScaleValue() {
     89         try {
     90             final float scale = mWindowManager.getAnimationScale(WINDOW_ANIMATION_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