1 /*
2  * Copyright (C) 2018 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.example.android.multiclientinputmethod;
18 
19 import android.content.Context;
20 import android.hardware.display.DisplayManager;
21 import android.inputmethodservice.MultiClientInputMethodServiceDelegate;
22 import android.os.IBinder;
23 import android.util.SparseArray;
24 import android.view.Display;
25 
26 final class SoftInputWindowManager {
27     private final Context mContext;
28     private final MultiClientInputMethodServiceDelegate mDelegate;
29     private final SparseArray<SoftInputWindow> mSoftInputWindows = new SparseArray<>();
30 
SoftInputWindowManager(Context context, MultiClientInputMethodServiceDelegate delegate)31     SoftInputWindowManager(Context context, MultiClientInputMethodServiceDelegate delegate) {
32         mContext = context;
33         mDelegate = delegate;
34     }
35 
getOrCreateSoftInputWindow(int displayId)36     SoftInputWindow getOrCreateSoftInputWindow(int displayId) {
37         final SoftInputWindow existingWindow = mSoftInputWindows.get(displayId);
38         if (existingWindow != null) {
39             return existingWindow;
40         }
41 
42         final Display display =
43                 mContext.getSystemService(DisplayManager.class).getDisplay(displayId);
44         if (display == null) {
45             return null;
46         }
47         final IBinder windowToken = mDelegate.createInputMethodWindowToken(displayId);
48         if (windowToken == null) {
49             return null;
50         }
51 
52         final Context displayContext = mContext.createDisplayContext(display);
53         final SoftInputWindow newWindow = new SoftInputWindow(displayContext, windowToken);
54         mSoftInputWindows.put(displayId, newWindow);
55         return newWindow;
56     }
57 
getSoftInputWindow(int displayId)58     SoftInputWindow getSoftInputWindow(int displayId) {
59         return mSoftInputWindows.get(displayId);
60     }
61 }
62