1 /*
2  * Copyright (C) 2019 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.activity;
18 
19 import android.app.Activity;
20 import android.content.BroadcastReceiver;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.os.Bundle;
26 import android.widget.Button;
27 import android.widget.LinearLayout;
28 
29 import java.util.concurrent.atomic.AtomicInteger;
30 
31 public class ActivityRecordInputSinkTestsActivity extends Activity {
32 
33     static final String LAUNCH_ACTIVITY_ACTION = "launch";
34     static final String COMPONENT_EXTRA = "component";
35     static final String EXTRA_EXTRA = "extra";
36 
37     Button mTopButton;
38     Button mBottomButton;
39 
40     static volatile AtomicInteger sButtonClickCount = new AtomicInteger(0);
41 
42     @Override
onCreate(Bundle savedInstanceState)43     protected void onCreate(Bundle savedInstanceState) {
44         super.onCreate(savedInstanceState);
45         mTopButton = new Button(this);
46         mTopButton.setOnClickListener(v -> sButtonClickCount.getAndIncrement());
47         mTopButton.setLayoutParams(
48                 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
49                         LinearLayout.LayoutParams.MATCH_PARENT));
50         setContentView(mTopButton);
51     }
52 
53     @Override
onStart()54     protected void onStart() {
55         super.onStart();
56         registerReceiver(
57                 mReceiver, new IntentFilter(LAUNCH_ACTIVITY_ACTION), Context.RECEIVER_NOT_EXPORTED);
58     }
59 
60     @Override
onStop()61     protected void onStop() {
62         unregisterReceiver(mReceiver);
63         super.onStop();
64     }
65 
66     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
67         @Override
68         public void onReceive(Context context, Intent intent) {
69             switch (intent.getAction()) {
70                 case LAUNCH_ACTIVITY_ACTION:
71                     Intent activityIntent = new Intent();
72                     activityIntent.setComponent(intent.getParcelableExtra(COMPONENT_EXTRA,
73                             ComponentName.class));
74                     activityIntent.replaceExtras(intent.getBundleExtra(EXTRA_EXTRA));
75                     startActivity(activityIntent);
76                     break;
77                 default:
78                     throw new AssertionError("Unknown action" + intent.getAction());
79             }
80         }
81     };
82 
83 }
84