1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.view;
import static android.view.ImeFocusControllerProto.HAS_IME_FOCUS;
import static android.view.ImeFocusControllerProto.NEXT_SERVED_VIEW;
import static android.view.ImeFocusControllerProto.SERVED_VIEW;
import android.annotation.AnyThread;
import android.annotation.NonNull;
import android.annotation.UiThread;
import android.util.Log;
import android.util.proto.ProtoOutputStream;
import android.view.inputmethod.InputMethodManager;
import com.android.internal.inputmethod.InputMethodDebug;
import com.android.internal.inputmethod.StartInputFlags;
import com.android.internal.inputmethod.StartInputReason;
import java.util.Objects;
/**
* Responsible for IME focus handling inside {@link ViewRootImpl}.
* @hide
*/
public final class ImeFocusController {
private static final boolean DEBUG = false;
private static final String TAG = "ImeFocusController";
private final ViewRootImpl mViewRootImpl;
private boolean mHasImeFocus = false;
private View mServedView;
private View mNextServedView;
private InputMethodManagerDelegate mDelegate;
@UiThread
ImeFocusController(@NonNull ViewRootImpl viewRootImpl) {
mViewRootImpl = viewRootImpl;
}
@NonNull
private InputMethodManagerDelegate getImmDelegate() {
if (mDelegate == null) {
mDelegate = mViewRootImpl.mContext.getSystemService(
InputMethodManager.class).getDelegate();
}
return mDelegate;
}
/** Called when the view root is moved to a different display. */
@UiThread
void onMovedToDisplay() {
// InputMethodManager managed its instances for different displays. So if the associated
// display is changed, the delegate also needs to be refreshed (by getImmDelegate).
// See the comment in {@link android.app.SystemServiceRegistry} for InputMethodManager
// and {@link android.view.inputmethod.InputMethodManager#forContext}.
mDelegate = null;
}
@UiThread
void onTraversal(boolean hasWindowFocus, WindowManager.LayoutParams windowAttribute) {
final boolean hasImeFocus = updateImeFocusable(windowAttribute, false /* force */);
if (!hasWindowFocus || isInLocalFocusMode(windowAttribute)) {
return;
}
if (hasImeFocus == mHasImeFocus) {
return;
}
mHasImeFocus = hasImeFocus;
if (mHasImeFocus) {
onPreWindowFocus(true /* hasWindowFocus */, windowAttribute);
onPostWindowFocus(mViewRootImpl.mView.findFocus(), true /* hasWindowFocus */,
windowAttribute);
}
}
@UiThread
void onPreWindowFocus(boolean hasWindowFocus, WindowManager.LayoutParams windowAttribute) {
if (!mHasImeFocus || isInLocalFocusMode(windowAttribute)) {
return;
}
if (hasWindowFocus) {
getImmDelegate().setCurrentRootView(mViewRootImpl);
}
}
@UiThread
boolean updateImeFocusable(WindowManager.LayoutParams windowAttribute, boolean force) {
final boolean hasImeFocus = WindowManager.LayoutParams.mayUseInputMethod(
windowAttribute.flags);
if (force) {
mHasImeFocus = hasImeFocus;
}
return hasImeFocus;
}
@UiThread
void onPostWindowFocus(View focusedView, boolean hasWindowFocus,
WindowManager.LayoutParams windowAttribute) {
if (!hasWindowFocus || !mHasImeFocus || isInLocalFocusMode(windowAttribute)) {
return;
}
View viewForWindowFocus = focusedView != null ? focusedView : mViewRootImpl.mView;
if (DEBUG) {
Log.v(TAG, "onWindowFocus: " + viewForWindowFocus
+ " softInputMode=" + InputMethodDebug.softInputModeToString(
windowAttribute.softInputMode));
}
boolean forceFocus = false;
final InputMethodManagerDelegate immDelegate = getImmDelegate();
if (immDelegate.isRestartOnNextWindowFocus(true /* reset */)) {
if (DEBUG) Log.v(TAG, "Restarting due to isRestartOnNextWindowFocus as true");
forceFocus = true;
}
// Update mNextServedView when focusedView changed.
onViewFocusChanged(viewForWindowFocus, true);
// Starting new input when the next focused view is same as served view but the currently
// active connection (if any) is not associated with it.
final boolean nextFocusIsServedView = mServedView == viewForWindowFocus;
if (nextFocusIsServedView && !immDelegate.hasActiveConnection(viewForWindowFocus)) {
forceFocus = true;
}
immDelegate.startInputAsyncOnWindowFocusGain(viewForWindowFocus,
windowAttribute.softInputMode, windowAttribute.flags, forceFocus);
}
public boolean checkFocus(boolean forceNewFocus, boolean startInput) {
final InputMethodManagerDelegate immDelegate = getImmDelegate();
if (!immDelegate.isCurrentRootView(mViewRootImpl)
|| (mServedView == mNextServedView && !forceNewFocus)) {
return false;
}
if (DEBUG) Log.v(TAG, "checkFocus: view=" + mServedView
+ " next=" + mNextServedView
+ " force=" + forceNewFocus
+ " package="
+ (mServedView != null ? mServedView.getContext().getPackageName() : "<none>"));
// Close the connection when no next served view coming.
if (mNextServedView == null) {
immDelegate.finishInput();
immDelegate.closeCurrentIme();
return false;
}
mServedView = mNextServedView;
immDelegate.finishComposingText();
if (startInput) {
immDelegate.startInput(StartInputReason.CHECK_FOCUS, null /* focusedView */,
0 /* startInputFlags */, 0 /* softInputMode */, 0 /* windowFlags */);
}
return true;
}
@UiThread
void onViewFocusChanged(View view, boolean hasFocus) {
if (view == null || view.isTemporarilyDetached()) {
return;
}
if (!getImmDelegate().isCurrentRootView(view.getViewRootImpl())) {
return;
}
if (!view.hasImeFocus() || !view.hasWindowFocus()) {
return;
}
if (DEBUG) Log.d(TAG, "onViewFocusChanged, view=" + view + ", mServedView=" + mServedView);
// We don't need to track the next served view when the view lost focus here because:
// 1) The current view focus may be cleared temporary when in touch mode, closing input
// at this moment isn't the right way.
// 2) We only care about the served view change when it focused, since changing input
// connection when the focus target changed is reasonable.
// 3) Setting the next served view as null when no more served view should be handled in
// other special events (e.g. view detached from window or the window dismissed).
if (hasFocus) {
mNextServedView = view;
}
mViewRootImpl.dispatchCheckFocus();
}
@UiThread
void onViewDetachedFromWindow(View view) {
if (!getImmDelegate().isCurrentRootView(view.getViewRootImpl())) {
return;
}
if (mNextServedView == view) {
mNextServedView = null;
}
if (mServedView == view) {
mViewRootImpl.dispatchCheckFocus();
}
}
@UiThread
void onWindowDismissed() {
final InputMethodManagerDelegate immDelegate = getImmDelegate();
if (!immDelegate.isCurrentRootView(mViewRootImpl)) {
return;
}
if (mServedView != null) {
immDelegate.finishInput();
}
immDelegate.setCurrentRootView(null);
mHasImeFocus = false;
}
/**
* To handle the lifecycle of the input connection when the device interactivity state changed.
* (i.e. Calling IMS#onFinishInput when the device screen-off and Calling IMS#onStartInput
* when the device screen-on again).
*/
@UiThread
public void onInteractiveChanged(boolean interactive) {
final InputMethodManagerDelegate immDelegate = getImmDelegate();
if (!immDelegate.isCurrentRootView(mViewRootImpl)) {
return;
}
if (interactive) {
final View focusedView = mViewRootImpl.mView.findFocus();
onViewFocusChanged(focusedView, focusedView != null);
} else {
mDelegate.finishInputAndReportToIme();
}
}
/**
* @param windowAttribute {@link WindowManager.LayoutParams} to be checked.
* @return Whether the window is in local focus mode or not.
*/
@AnyThread
private static boolean isInLocalFocusMode(WindowManager.LayoutParams windowAttribute) {
return (windowAttribute.flags & WindowManager.LayoutParams.FLAG_LOCAL_FOCUS_MODE) != 0;
}
int onProcessImeInputStage(Object token, InputEvent event,
WindowManager.LayoutParams windowAttribute,
InputMethodManager.FinishedInputEventCallback callback) {
if (!mHasImeFocus || isInLocalFocusMode(windowAttribute)) {
return InputMethodManager.DISPATCH_NOT_HANDLED;
}
final InputMethodManager imm =
mViewRootImpl.mContext.getSystemService(InputMethodManager.class);
if (imm == null) {
return InputMethodManager.DISPATCH_NOT_HANDLED;
}
return imm.dispatchInputEvent(event, token, callback, mViewRootImpl.mHandler);
}
/**
* A delegate implementing some basic {@link InputMethodManager} APIs.
* @hide
*/
public interface InputMethodManagerDelegate {
boolean startInput(@StartInputReason int startInputReason, View focusedView,
@StartInputFlags int startInputFlags,
@WindowManager.LayoutParams.SoftInputModeFlags int softInputMode, int windowFlags);
void startInputAsyncOnWindowFocusGain(View rootView,
@WindowManager.LayoutParams.SoftInputModeFlags int softInputMode, int windowFlags,
boolean forceNewFocus);
void finishInput();
void finishInputAndReportToIme();
void closeCurrentIme();
void finishComposingText();
void setCurrentRootView(ViewRootImpl rootView);
boolean isCurrentRootView(ViewRootImpl rootView);
boolean isRestartOnNextWindowFocus(boolean reset);
boolean hasActiveConnection(View view);
}
public View getServedView() {
return mServedView;
}
public View getNextServedView() {
return mNextServedView;
}
public void setServedView(View view) {
mServedView = view;
}
public void setNextServedView(View view) {
mNextServedView = view;
}
/**
* Indicates whether the view's window has IME focused.
*/
@UiThread
boolean hasImeFocus() {
return mHasImeFocus;
}
void dumpDebug(ProtoOutputStream proto, long fieldId) {
final long token = proto.start(fieldId);
proto.write(HAS_IME_FOCUS, mHasImeFocus);
proto.write(SERVED_VIEW, Objects.toString(mServedView));
proto.write(NEXT_SERVED_VIEW, Objects.toString(mNextServedView));
proto.end(token);
}
}
|