1 /*
2  * Copyright (C) 2008 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;
18 
19 import android.app.Activity;
20 import android.graphics.Insets;
21 import android.os.Bundle;
22 import android.view.KeyEvent;
23 import android.view.Menu;
24 import android.view.View;
25 import android.view.Window;
26 
27 import android.server.wm.cts.R;
28 import android.view.WindowInsets;
29 
30 public class WindowCtsActivity extends Activity {
31 
32     private static boolean mIsOnCreateOptionsMenuCalled;
33     private static boolean mIsOnOptionsMenuClosedCalled;
34     private static boolean mIsOnKeyDownCalled;
35 
36     private WindowInsets mLastInsets;
37 
38     @Override
onCreate(Bundle savedInstanceState)39     protected void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         getWindow().requestFeature(Window.FEATURE_LEFT_ICON);
42         getWindow().setDecorFitsSystemWindows(true);
43         setContentView(R.layout.windowstub_layout);
44         getContentView().setOnApplyWindowInsetsListener((v, insets) -> {
45             mLastInsets = insets;
46             return insets;
47         });
48     }
49 
50     @Override
onCreateOptionsMenu(Menu menu)51     public boolean onCreateOptionsMenu(Menu menu) {
52         menu.add(Menu.NONE, Menu.NONE, Menu.NONE, "Quit").setShortcut('1', 'q');
53         menu.add(Menu.NONE, Menu.NONE, Menu.NONE, "Action").setShortcut('2', 'a');
54         mIsOnCreateOptionsMenuCalled = true;
55         return super.onCreateOptionsMenu(menu);
56     }
57 
58     @Override
onOptionsMenuClosed(Menu menu)59     public void onOptionsMenuClosed(Menu menu) {
60         super.onOptionsMenuClosed(menu);
61         mIsOnOptionsMenuClosedCalled = true;
62     }
63 
64     @Override
onKeyDown(int keyCode, KeyEvent event)65     public boolean onKeyDown(int keyCode, KeyEvent event) {
66         mIsOnKeyDownCalled = true;
67         return super.onKeyDown(keyCode, event);
68     }
69 
getContentView()70     public View getContentView() {
71         return findViewById(R.id.listview_window);
72     }
73 
getAppliedInsets()74     public Insets getAppliedInsets() {
75         View view = (View) getContentView().getParent();
76         int[] location = new int[2];
77         view.getLocationInWindow(location);
78         View decorView = getWindow().getDecorView();
79         return Insets.of(location[0], location[1],
80                 decorView.getWidth() - (location[0] + view.getWidth()),
81                 decorView.getHeight() - (location[1] + view.getHeight()));
82     }
83 
getLastInsets()84     public WindowInsets getLastInsets() {
85         return mLastInsets;
86     }
87 
isOnCreateOptionsMenuCalled()88     public boolean isOnCreateOptionsMenuCalled() {
89         return mIsOnCreateOptionsMenuCalled;
90     }
91 
isOnOptionsMenuClosedCalled()92     public boolean isOnOptionsMenuClosedCalled() {
93         return mIsOnOptionsMenuClosedCalled;
94     }
95 
isOnKeyDownCalled()96     public boolean isOnKeyDownCalled() {
97         return mIsOnKeyDownCalled;
98     }
99 
setFlagFalse()100     public void setFlagFalse() {
101         mIsOnCreateOptionsMenuCalled = false;
102         mIsOnOptionsMenuClosedCalled = false;
103     }
104 }
105