1 /*
2  * Copyright (C) 2020 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 android.server.wm.overlay;
18 
19 import static android.server.wm.overlay.UntrustedTouchTestService.BACKGROUND_COLOR;
20 
21 import android.app.Activity;
22 import android.content.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.os.Bundle;
27 import android.view.View;
28 
29 import androidx.annotation.AnimRes;
30 import androidx.annotation.Nullable;
31 
32 
33 /**
34  * Activity that registers a receiver to listen to actions in {@link
35  * Components.ExitAnimationActivityReceiver} to exit with animations.
36  */
37 public class ExitAnimationActivity extends Activity {
38     @Override
onCreate(@ullable Bundle savedInstanceState)39     protected void onCreate(@Nullable Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         View view = new View(this);
42         view.setBackgroundColor(BACKGROUND_COLOR);
43         setContentView(view);
44     }
45 
46     @Override
onStart()47     protected void onStart() {
48         super.onStart();
49         registerReceiver(mReceiver,
50                 new IntentFilter(Components.ExitAnimationActivityReceiver.ACTION_FINISH),
51                 Context.RECEIVER_EXPORTED);
52     }
53 
54     @Override
onStop()55     protected void onStop() {
56         super.onStop();
57         unregisterReceiver(mReceiver);
58     }
59 
60     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
61         @Override
62         public void onReceive(Context context, Intent intent) {
63             switch (intent.getAction()) {
64                 case Components.ExitAnimationActivityReceiver.ACTION_FINISH:
65                     int exitAnimation = intent.getIntExtra(
66                             Components.ExitAnimationActivityReceiver.EXTRA_ANIMATION,
67                             Components.ExitAnimationActivityReceiver.EXTRA_VALUE_ANIMATION_EMPTY);
68                     finish();
69                     overridePendingTransition(getEnterAnimationRes(exitAnimation),
70                             getAnimationRes(exitAnimation));
71                     break;
72                 default:
73                     throw new AssertionError("Unknown action" + intent.getAction());
74             }
75         }
76     };
77 
78     /** An enter animation for a certain exit animation, mostly so durations match. */
79     @AnimRes
getEnterAnimationRes(int exitAnimation)80     private static int getEnterAnimationRes(int exitAnimation) {
81         switch (exitAnimation) {
82             case Components.ExitAnimationActivityReceiver.EXTRA_VALUE_ANIMATION_EMPTY:
83             case Components.ExitAnimationActivityReceiver.EXTRA_VALUE_ANIMATION_0_7:
84             case Components.ExitAnimationActivityReceiver.EXTRA_VALUE_ANIMATION_0_9:
85                 return R.anim.alpha_1;
86             case Components.ExitAnimationActivityReceiver.EXTRA_VALUE_LONG_ANIMATION_0_7:
87                 return R.anim.long_alpha_1;
88             default:
89                 throw new AssertionError("Unknown animation value " + exitAnimation);
90         }
91     }
92 
93     @AnimRes
getAnimationRes(int animation)94     private static int getAnimationRes(int animation) {
95         switch (animation) {
96             case Components.ExitAnimationActivityReceiver.EXTRA_VALUE_ANIMATION_EMPTY:
97                 return 0;
98             case Components.ExitAnimationActivityReceiver.EXTRA_VALUE_ANIMATION_0_7:
99                 return R.anim.alpha_0_7;
100             case Components.ExitAnimationActivityReceiver.EXTRA_VALUE_ANIMATION_0_9:
101                 return R.anim.alpha_0_9;
102             case Components.ExitAnimationActivityReceiver.EXTRA_VALUE_LONG_ANIMATION_0_7:
103                 return R.anim.long_alpha_0_7;
104             default:
105                 throw new AssertionError("Unknown animation value " + animation);
106         }
107     }
108 }
109