1 /*
2  * Copyright (C) 2023 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.vdmdemo.host;
18 
19 import android.inputmethodservice.InputMethodService;
20 import android.util.Log;
21 import android.view.inputmethod.EditorInfo;
22 
23 import com.example.android.vdmdemo.common.RemoteEventProto.KeyboardVisibilityEvent;
24 import com.example.android.vdmdemo.common.RemoteEventProto.RemoteEvent;
25 import com.example.android.vdmdemo.common.RemoteIo;
26 
27 import dagger.hilt.android.AndroidEntryPoint;
28 
29 import javax.inject.Inject;
30 
31 /** A custom IME component that forwards soft keyboard visibility events to the client. */
32 @AndroidEntryPoint(InputMethodService.class)
33 public final class VdmProxyIme extends Hilt_VdmProxyIme {
34 
35     public static final String TAG = "VdmProxyIme";
36 
37     @Inject RemoteIo mRemoteIo;
38     @Inject DisplayRepository mDisplayRepository;
39 
40     @Override
onStartInputView(EditorInfo editorInfo, boolean restarting)41     public void onStartInputView(EditorInfo editorInfo, boolean restarting) {
42         super.onStartInputView(editorInfo, restarting);
43         sendVisibilityEvent(true);
44     }
45 
46     @Override
onShowInputRequested(int flags, boolean configChange)47     public boolean onShowInputRequested(int flags, boolean configChange) {
48         sendVisibilityEvent(true);
49         return true;
50     }
51 
52     @Override
onWindowHidden()53     public void onWindowHidden() {
54         super.onWindowHidden();
55         sendVisibilityEvent(false);
56     }
57 
sendVisibilityEvent(boolean visible)58     private void sendVisibilityEvent(boolean visible) {
59         Log.d(TAG, "Sending client IME visibility event, visible=" + visible);
60         mRemoteIo.sendMessage(
61                 RemoteEvent.newBuilder()
62                         .setDisplayId(getRemoteDisplayId())
63                         .setKeyboardVisibilityEvent(
64                                 KeyboardVisibilityEvent.newBuilder().setVisible(visible))
65                         .build());
66     }
67 
getRemoteDisplayId()68     private int getRemoteDisplayId() {
69         return mDisplayRepository.getRemoteDisplayId(
70                 getWindow().getWindow().getDecorView().getDisplay().getDisplayId());
71     }
72 }
73