1 /*
2  * Copyright (C) 2022 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 com.android.server.inputmethod;
18 
19 import static com.android.internal.annotations.VisibleForTesting.Visibility.PACKAGE;
20 
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.app.ActivityThread;
24 import android.content.Context;
25 import android.view.ContextThemeWrapper;
26 import android.view.WindowManager;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 
30 /**
31  * Provides the window context for the IME switcher dialog.
32  */
33 @VisibleForTesting(visibility = PACKAGE)
34 public final class InputMethodDialogWindowContext {
35     @Nullable
36     private Context mDialogWindowContext;
37 
38     /**
39      * Returns the window context for IME switch dialogs to receive configuration changes.
40      *
41      * This method initializes the window context if it was not initialized, or moves the context to
42      * the targeted display if the current display of context is different from the display
43      * specified by {@code displayId}.
44      */
45     @NonNull
46     @VisibleForTesting(visibility = PACKAGE)
get(int displayId)47     public Context get(int displayId) {
48         if (mDialogWindowContext == null || mDialogWindowContext.getDisplayId() != displayId) {
49             final Context systemUiContext = ActivityThread.currentActivityThread()
50                     .getSystemUiContext(displayId);
51             final Context windowContext = systemUiContext.createWindowContext(
52                     WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG, null /* options */);
53             mDialogWindowContext = new ContextThemeWrapper(
54                     windowContext, com.android.internal.R.style.Theme_DeviceDefault_Settings);
55         }
56         return mDialogWindowContext;
57     }
58 }
59