1 /*
2  * Copyright (C) 2021 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.compatibility.common.util;
18 
19 import static com.android.compatibility.common.util.SystemUtil.runShellCommand;
20 
21 import androidx.annotation.NonNull;
22 
23 import org.junit.runner.Description;
24 import org.junit.runners.model.Statement;
25 
26 public class DisableAnimationRule extends BeforeAfterRule {
27     @NonNull
28     private final GlobalSetting mWindowAnimationScaleSetting = new GlobalSetting(
29             "window_animation_scale");
30     @NonNull
31     private final GlobalSetting mTransitionAnimationScaleSetting = new GlobalSetting(
32             "transition_animation_scale");
33     @NonNull
34     private final GlobalSetting mAnimatorDurationScaleSetting = new GlobalSetting(
35             "animator_duration_scale");
36 
37     @Override
onBefore(Statement base, Description description)38     protected void onBefore(Statement base, Description description) throws Throwable {
39         mWindowAnimationScaleSetting.put("0");
40         mTransitionAnimationScaleSetting.put("0");
41         mAnimatorDurationScaleSetting.put("0");
42     }
43 
44     @Override
onAfter(Statement base, Description description)45     protected void onAfter(Statement base, Description description) throws Throwable {
46         mWindowAnimationScaleSetting.restore();
47         mTransitionAnimationScaleSetting.restore();
48         mAnimatorDurationScaleSetting.restore();
49     }
50 
51     private static class GlobalSetting {
52         @NonNull
53         private final String mName;
54 
55         private String mInitialValue;
56 
GlobalSetting(@onNull String name)57         public GlobalSetting(@NonNull String name) {
58             mName = name;
59         }
60 
put(@onNull String value)61         public void put(@NonNull String value) {
62             mInitialValue = runShellCommand("settings get global " + mName);
63             runShellCommand("settings put global " + mName + " " + value);
64         }
65 
restore()66         public void restore() {
67             runShellCommand("settings put global " + mName + " " + mInitialValue);
68         }
69     }
70 }
71