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 android.server.wm.alertwindowappsdk25;
18 
19 import android.app.Activity;
20 import android.graphics.Color;
21 import android.graphics.Point;
22 import android.util.Log;
23 import android.view.WindowManager;
24 import android.widget.TextView;
25 
26 import static android.view.Gravity.LEFT;
27 import static android.view.Gravity.TOP;
28 import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
29 import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
30 import static android.view.WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
31 
32 public abstract class AlertWindowTestBaseActivity extends Activity {
33 
createAllAlertWindows(String windowName)34     protected void createAllAlertWindows(String windowName) {
35         final int[] alertWindowTypes = getAlertWindowTypes();
36         for (int type : alertWindowTypes) {
37             try {
38                 createAlertWindow(type, windowName);
39             } catch (Exception e) {
40                 Log.e("AlertWindowTestBaseActivity", "Can't create type=" + type, e);
41             }
42         }
43     }
44 
createAlertWindow(int type)45     protected void createAlertWindow(int type) {
46         createAlertWindow(type, getPackageName());
47     }
48 
createAlertWindow(int type, String windowName)49     protected void createAlertWindow(int type, String windowName) {
50         if (!isSystemAlertWindowType(type)) {
51             throw new IllegalArgumentException("Well...you are not an alert window type=" + type);
52         }
53 
54         final Point size = new Point();
55         final WindowManager wm = getSystemService(WindowManager.class);
56         wm.getDefaultDisplay().getSize(size);
57 
58         WindowManager.LayoutParams params = new WindowManager.LayoutParams(
59                 type, FLAG_NOT_FOCUSABLE | FLAG_WATCH_OUTSIDE_TOUCH | FLAG_NOT_TOUCHABLE);
60         params.width = size.x / 3;
61         params.height = size.y / 3;
62         params.gravity = TOP | LEFT;
63         params.setTitle(windowName);
64 
65         final TextView view = new TextView(this);
66         view.setText(windowName + "   type=" + type);
67         view.setBackgroundColor(Color.RED);
68         wm.addView(view, params);
69     }
70 
isSystemAlertWindowType(int type)71     private boolean isSystemAlertWindowType(int type) {
72         final int[] alertWindowTypes = getAlertWindowTypes();
73         for (int current : alertWindowTypes) {
74             if (current == type) {
75                 return true;
76             }
77         }
78         return false;
79     }
80 
getAlertWindowTypes()81     protected abstract int[] getAlertWindowTypes();
82 }
83