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;
18 
19 import static android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
20 
21 import android.graphics.Color;
22 
23 import com.android.cts.mockime.ImeSettings;
24 import com.android.cts.mockime.MockImeSession;
25 
26 /**
27  * Centralizes the creation of {@link MockImeSession}. This class ins't placed in utility group
28  * because only this package uses it.
29  */
30 public class MockImeHelper {
31 
32     /**
33      * Leverage MockImeSession to ensure at least an IME exists as default.
34      *
35      * @see ObjectTracker#manage(AutoCloseable)
36      */
createManagedMockImeSession(ActivityManagerTestBase base)37     public static MockImeSession createManagedMockImeSession(ActivityManagerTestBase base) {
38         try {
39             return base.mObjectTracker.manage(MockImeSession.create(base.mContext));
40         } catch (Exception e) {
41             throw new RuntimeException("Failed to create MockImeSession", e);
42         }
43     }
44 
createManagedMockImeSession(ActivityManagerTestBase base, int keyboardHeight, boolean useFloating)45     public static MockImeSession createManagedMockImeSession(ActivityManagerTestBase base,
46             int keyboardHeight, boolean useFloating) {
47         final ImeSettings.Builder builder = new ImeSettings.Builder();
48         if (useFloating) {
49             builder.setWindowFlags(0, FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
50             // As documented, Window#setNavigationBarColor() is actually ignored when the IME
51             // window does not have FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS.  We are calling
52             // setNavigationBarColor() to ensure it.
53             builder.setNavigationBarColor(Color.BLACK);
54         } else {
55             builder.setInputViewHeight(keyboardHeight).setDrawsBehindNavBar(true);
56         }
57         try {
58             return base.mObjectTracker.manage(MockImeSession.create(
59                     base.mContext, base.mInstrumentation.getUiAutomation(), builder));
60         } catch (Exception e) {
61             throw new RuntimeException("Failed to create MockImeSession", e);
62         }
63     }
64 }
65