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 android.accessibilityservice.cts.activities;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.net.ConnectivityManager;
24 import android.os.Bundle;
25 import android.view.Gravity;
26 import android.view.View;
27 import android.view.WindowManager;
28 import android.widget.Button;
29 
30 public class NotTouchableWindowTestActivity extends AccessibilityTestActivity  {
31     private class CommandReceiver extends BroadcastReceiver {
32         private View rootView;
33 
34         @Override
onReceive(Context context, Intent intent)35         public void onReceive(Context context, Intent intent) {
36             WindowManager.LayoutParams params;
37             switch (intent.getAction()) {
38                 case ADD_WINDOW:
39                     if (rootView != null) {
40                         throw new IllegalStateException("Window already exists");
41                     }
42                     rootView = new View(context);
43                     params = createDefaultWindowParams();
44                     context.getSystemService(WindowManager.class).addView(rootView, params);
45                     break;
46 
47                 case ADD_TRUSTED_WINDOW:
48                     if (rootView != null) {
49                         throw new IllegalStateException("Window already exists");
50                     }
51                     rootView = new Button(context);
52                     params = createDefaultWindowParams();
53                     params.privateFlags |= PRIVATE_FLAG_TRUSTED_OVERLAY;
54                     context.getSystemService(WindowManager.class).addView(rootView, params);
55                     break;
56 
57                 case FINISH_ACTIVITY:
58                     finish();
59             }
60         }
61 
62     }
63 
64     private BroadcastReceiver mBroadcastReceiver = new CommandReceiver();
65 
66     public static final String TITLE =
67             "NotTouchableWindowTestActivity";
68     public static final String NON_TOUCHABLE_WINDOW_TITLE =
69             "android.accessibilityservice.cts.activities.NON_TOUCHABLE_WINDOW_TITLE";
70 
71     public static final String ADD_WINDOW =
72             "android.accessibilityservice.cts.ADD_WINDOW";
73     public static final String ADD_TRUSTED_WINDOW =
74             "android.accessibilityservice.cts.ADD_TRUSTED_WINDOW";
75     public static final String FINISH_ACTIVITY =
76             "android.accessibilityservice.cts.FINISH_ACTIVITY";
77 
78     // From WindowManager.java.
79     public static final int PRIVATE_FLAG_TRUSTED_OVERLAY = 0x20000000;
80 
81     @Override
onCreate(Bundle savedInstanceState)82     public void onCreate(Bundle savedInstanceState) {
83         super.onCreate(savedInstanceState);
84 
85         IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
86         filter.addAction(ADD_WINDOW);
87         filter.addAction(ADD_TRUSTED_WINDOW);
88         filter.addAction(FINISH_ACTIVITY);
89         this.registerReceiver(mBroadcastReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
90 
91         setTitle(TITLE);
92     }
93 
94     @Override
onDestroy()95     public void onDestroy() {
96         super.onDestroy();
97 
98         this.unregisterReceiver(mBroadcastReceiver);
99     }
100 
createDefaultWindowParams()101     private static WindowManager.LayoutParams createDefaultWindowParams() {
102         WindowManager.LayoutParams params = new WindowManager.LayoutParams();
103         params.width = WindowManager.LayoutParams.MATCH_PARENT;
104         params.height = WindowManager.LayoutParams.MATCH_PARENT;
105         params.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
106                 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
107                 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
108                 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
109         params.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
110         params.setFitInsetsTypes(0);
111         params.gravity = Gravity.TOP;
112         params.setTitle(NON_TOUCHABLE_WINDOW_TITLE);
113 
114         return params;
115     }
116 }