Tarandeep Singh | 500a38f | 2019-09-26 13:36:40 -0700 | [diff] [blame] | 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 com.android.server.wm; |
| 18 | |
| 19 | import android.view.InsetsSource; |
| 20 | import android.view.WindowInsets; |
| 21 | |
| 22 | /** |
| 23 | * Controller for IME inset source on the server. It's called provider as it provides the |
| 24 | * {@link InsetsSource} to the client that uses it in {@link InsetsSourceConsumer}. |
| 25 | */ |
| 26 | class ImeInsetsSourceProvider extends InsetsSourceProvider { |
| 27 | |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 28 | private WindowState mImeTargetFromIme; |
Tarandeep Singh | 500a38f | 2019-09-26 13:36:40 -0700 | [diff] [blame] | 29 | private Runnable mShowImeRunner; |
| 30 | private boolean mIsImeLayoutDrawn; |
| 31 | |
| 32 | ImeInsetsSourceProvider(InsetsSource source, |
| 33 | InsetsStateController stateController, DisplayContent displayContent) { |
| 34 | super(source, stateController, displayContent); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Called when a layout pass has occurred. |
| 39 | */ |
| 40 | void onPostLayout() { |
| 41 | super.onPostLayout(); |
| 42 | |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 43 | if (mImeTargetFromIme != null |
| 44 | && isImeTargetFromDisplayContentAndImeSame() |
Tarandeep Singh | 500a38f | 2019-09-26 13:36:40 -0700 | [diff] [blame] | 45 | && mWin != null |
| 46 | && mWin.isDrawnLw() |
| 47 | && !mWin.mGivenInsetsPending) { |
| 48 | mIsImeLayoutDrawn = true; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Called when Insets have been dispatched to client. |
| 54 | */ |
| 55 | void onPostInsetsDispatched() { |
| 56 | if (mIsImeLayoutDrawn && mShowImeRunner != null) { |
| 57 | // Show IME if InputMethodService requested to be shown and it's layout has finished. |
| 58 | mShowImeRunner.run(); |
Tarandeep Singh | 500a38f | 2019-09-26 13:36:40 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Called from {@link WindowManagerInternal#showImePostLayout} when {@link InputMethodService} |
| 64 | * requests to show IME on {@param imeTarget}. |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 65 | * @param imeTarget imeTarget on which IME request is coming from. |
Tarandeep Singh | 500a38f | 2019-09-26 13:36:40 -0700 | [diff] [blame] | 66 | */ |
| 67 | void scheduleShowImePostLayout(WindowState imeTarget) { |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 68 | mImeTargetFromIme = imeTarget; |
Tarandeep Singh | 500a38f | 2019-09-26 13:36:40 -0700 | [diff] [blame] | 69 | mShowImeRunner = () -> { |
| 70 | // Target should still be the same. |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 71 | if (isImeTargetFromDisplayContentAndImeSame()) { |
Tarandeep Singh | 500a38f | 2019-09-26 13:36:40 -0700 | [diff] [blame] | 72 | mDisplayContent.mInputMethodTarget.showInsets( |
| 73 | WindowInsets.Type.ime(), true /* fromIme */); |
| 74 | } |
Tarandeep Singh | bd24ee7 | 2019-10-21 13:17:13 -0700 | [diff] [blame] | 75 | abortShowImePostLayout(); |
Tarandeep Singh | 500a38f | 2019-09-26 13:36:40 -0700 | [diff] [blame] | 76 | }; |
| 77 | } |
| 78 | |
Tarandeep Singh | bd24ee7 | 2019-10-21 13:17:13 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Abort any pending request to show IME post layout. |
| 81 | */ |
| 82 | void abortShowImePostLayout() { |
| 83 | mImeTargetFromIme = null; |
| 84 | mIsImeLayoutDrawn = false; |
| 85 | mShowImeRunner = null; |
| 86 | } |
| 87 | |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 88 | private boolean isImeTargetFromDisplayContentAndImeSame() { |
| 89 | // IMMS#mLastImeTargetWindow always considers focused window as |
| 90 | // IME target, however DisplayContent#computeImeTarget() can compute |
| 91 | // a different IME target. |
| 92 | // Refer to WindowManagerService#applyImeVisibility(token, false). |
| 93 | // If IMMS's imeTarget is child of DisplayContent's imeTarget and child window |
| 94 | // is above the parent, we will consider it as the same target for now. |
Tarandeep Singh | e30056a | 2019-11-18 16:53:14 -0800 | [diff] [blame^] | 95 | // Also, if imeTarget is closing, it would be considered as outdated target. |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 96 | // TODO(b/139861270): Remove the child & sublayer check once IMMS is aware of |
| 97 | // actual IME target. |
Tarandeep Singh | e30056a | 2019-11-18 16:53:14 -0800 | [diff] [blame^] | 98 | final WindowState dcTarget = mDisplayContent.mInputMethodTarget; |
| 99 | return (!dcTarget.isClosing() && mImeTargetFromIme == dcTarget) |
| 100 | || (dcTarget.getParentWindow() == mImeTargetFromIme |
| 101 | && dcTarget.mSubLayer > mImeTargetFromIme.mSubLayer); |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Tarandeep Singh | 500a38f | 2019-09-26 13:36:40 -0700 | [diff] [blame] | 104 | } |