The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | * use this file except in compliance with the License. You may obtain a copy of |
| 5 | * the License at |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | * License for the specific language governing permissions and limitations under |
| 13 | * the License. |
| 14 | */ |
| 15 | |
Yohei Yukawa | 603f4d0 | 2018-09-11 15:04:58 -0700 | [diff] [blame] | 16 | package com.android.server.inputmethod; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | |
lumark | ef1965b | 2018-09-12 17:42:53 +0800 | [diff] [blame] | 18 | import static android.view.Display.DEFAULT_DISPLAY; |
lumark | 90120a8 | 2018-08-15 00:33:03 +0800 | [diff] [blame] | 19 | import static android.view.Display.INVALID_DISPLAY; |
Yohei Yukawa | 0569a18 | 2018-08-28 16:09:28 -0700 | [diff] [blame] | 20 | |
Yohei Yukawa | fa0e47e | 2016-04-05 09:55:56 -0700 | [diff] [blame] | 21 | import static java.lang.annotation.RetentionPolicy.SOURCE; |
| 22 | |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 23 | import android.Manifest; |
Phil Weaver | 03a65b0 | 2018-07-19 16:07:57 -0700 | [diff] [blame] | 24 | import android.accessibilityservice.AccessibilityService; |
Tarandeep Singh | 75a9239 | 2018-01-12 14:58:59 -0800 | [diff] [blame] | 25 | import android.annotation.AnyThread; |
Yohei Yukawa | d6475a6 | 2017-04-17 10:35:27 -0700 | [diff] [blame] | 26 | import android.annotation.BinderThread; |
Tadashi G. Takaoka | 01065a5 | 2017-07-19 14:10:24 +0900 | [diff] [blame] | 27 | import android.annotation.ColorInt; |
Yohei Yukawa | 41b094f | 2018-09-09 23:58:45 -0700 | [diff] [blame] | 28 | import android.annotation.DrawableRes; |
Yohei Yukawa | fa0e47e | 2016-04-05 09:55:56 -0700 | [diff] [blame] | 29 | import android.annotation.IntDef; |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 30 | import android.annotation.MainThread; |
Yohei Yukawa | 6ab1a82 | 2015-08-25 14:01:28 -0700 | [diff] [blame] | 31 | import android.annotation.NonNull; |
Yohei Yukawa | e13a20fa | 2015-09-30 19:11:32 -0700 | [diff] [blame] | 32 | import android.annotation.Nullable; |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 33 | import android.annotation.RequiresPermission; |
Yohei Yukawa | 7b18aec | 2016-03-07 13:04:32 -0800 | [diff] [blame] | 34 | import android.annotation.UserIdInt; |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 35 | import android.app.ActivityManager; |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 36 | import android.app.ActivityManagerInternal; |
Andrew Sapperstein | 8a3b4cb | 2017-04-28 14:35:31 -0700 | [diff] [blame] | 37 | import android.app.ActivityThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.app.AlertDialog; |
Yohei Yukawa | 6ab1a82 | 2015-08-25 14:01:28 -0700 | [diff] [blame] | 39 | import android.app.AppGlobals; |
Yohei Yukawa | e63b5fa | 2014-09-19 13:14:55 +0900 | [diff] [blame] | 40 | import android.app.AppOpsManager; |
satok | f90a33e | 2011-07-19 11:55:52 +0900 | [diff] [blame] | 41 | import android.app.KeyguardManager; |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 42 | import android.app.Notification; |
| 43 | import android.app.NotificationManager; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 44 | import android.app.PendingIntent; |
satok | 5b927c43 | 2012-05-01 20:09:34 +0900 | [diff] [blame] | 45 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.content.ComponentName; |
Yohei Yukawa | 3933a6e | 2016-11-10 00:47:48 -0800 | [diff] [blame] | 47 | import android.content.ContentProvider; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.content.ContentResolver; |
| 49 | import android.content.Context; |
| 50 | import android.content.DialogInterface; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.content.DialogInterface.OnCancelListener; |
Tadashi G. Takaoka | d130b80 | 2014-08-01 14:05:47 +0900 | [diff] [blame] | 52 | import android.content.DialogInterface.OnClickListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.content.Intent; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 54 | import android.content.IntentFilter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.content.ServiceConnection; |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 56 | import android.content.pm.ApplicationInfo; |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 57 | import android.content.pm.IPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.content.pm.PackageManager; |
Yohei Yukawa | d5f402b | 2020-05-15 10:23:32 -0700 | [diff] [blame] | 59 | import android.content.pm.PackageManagerInternal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.content.pm.ResolveInfo; |
| 61 | import android.content.pm.ServiceInfo; |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 62 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.content.res.Resources; |
| 64 | import android.content.res.TypedArray; |
| 65 | import android.database.ContentObserver; |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 66 | import android.graphics.Matrix; |
Alan Viverette | 505e3ab | 2014-11-24 15:22:11 -0800 | [diff] [blame] | 67 | import android.graphics.drawable.Drawable; |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 68 | import android.hardware.display.DisplayManagerInternal; |
lpeter | 133fce0 | 2020-03-05 20:32:16 +0800 | [diff] [blame] | 69 | import android.hardware.input.InputManagerInternal; |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 70 | import android.inputmethodservice.InputMethodService; |
Yohei Yukawa | d5f402b | 2020-05-15 10:23:32 -0700 | [diff] [blame] | 71 | import android.media.AudioManagerInternal; |
Michael Wright | 7b5a96b | 2014-08-09 19:28:42 -0700 | [diff] [blame] | 72 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | import android.os.Binder; |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 74 | import android.os.Bundle; |
Seigo Nonaka | e27dc2b | 2015-08-14 18:21:27 -0700 | [diff] [blame] | 75 | import android.os.Debug; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | import android.os.Handler; |
| 77 | import android.os.IBinder; |
| 78 | import android.os.IInterface; |
Yohei Yukawa | 23cbe85 | 2016-05-17 16:42:58 -0700 | [diff] [blame] | 79 | import android.os.LocaleList; |
Yohei Yukawa | 0569a18 | 2018-08-28 16:09:28 -0700 | [diff] [blame] | 80 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | import android.os.Parcel; |
Yohei Yukawa | 6ab1a82 | 2015-08-25 14:01:28 -0700 | [diff] [blame] | 82 | import android.os.Process; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | import android.os.RemoteException; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 84 | import android.os.ResultReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | import android.os.ServiceManager; |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 86 | import android.os.ShellCallback; |
| 87 | import android.os.ShellCommand; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | import android.os.SystemClock; |
Tarandeep Singh | 75a9239 | 2018-01-12 14:58:59 -0800 | [diff] [blame] | 89 | import android.os.SystemProperties; |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 90 | import android.os.UserHandle; |
Amith Yamasani | 734983f | 2014-03-04 16:48:05 -0800 | [diff] [blame] | 91 | import android.os.UserManager; |
Yohei Yukawa | 4208140 | 2019-01-15 09:57:50 -0800 | [diff] [blame] | 92 | import android.os.UserManagerInternal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | import android.provider.Settings; |
| 94 | import android.text.TextUtils; |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 95 | import android.text.style.SuggestionSpan; |
Yohei Yukawa | ac9311e | 2018-11-20 19:25:23 -0800 | [diff] [blame] | 96 | import android.util.ArrayMap; |
Christopher Tate | 7b9a28c | 2015-03-18 13:06:16 -0700 | [diff] [blame] | 97 | import android.util.ArraySet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | import android.util.EventLog; |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 99 | import android.util.LruCache; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 100 | import android.util.Pair; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | import android.util.PrintWriterPrinter; |
| 102 | import android.util.Printer; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 103 | import android.util.Slog; |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 104 | import android.util.SparseArray; |
Tadashi G. Takaoka | d130b80 | 2014-08-01 14:05:47 +0900 | [diff] [blame] | 105 | import android.view.ContextThemeWrapper; |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 106 | import android.view.DisplayInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | import android.view.IWindowManager; |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 108 | import android.view.InputChannel; |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 109 | import android.view.LayoutInflater; |
| 110 | import android.view.View; |
| 111 | import android.view.ViewGroup; |
Wale Ogunwale | 3a93169 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 112 | import android.view.Window; |
Yohei Yukawa | 6e87559 | 2019-01-28 00:49:30 -0800 | [diff] [blame] | 113 | import android.view.WindowManager.LayoutParams; |
Yohei Yukawa | b0c2645 | 2018-10-21 10:42:52 -0700 | [diff] [blame] | 114 | import android.view.WindowManager.LayoutParams.SoftInputModeFlags; |
Feng Cao | 7c85eb7 | 2020-02-28 11:39:56 -0800 | [diff] [blame] | 115 | import android.view.autofill.AutofillId; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 116 | import android.view.inputmethod.EditorInfo; |
Feng Cao | 16b2de5 | 2020-01-09 17:27:27 -0800 | [diff] [blame] | 117 | import android.view.inputmethod.InlineSuggestionsRequest; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | import android.view.inputmethod.InputBinding; |
Yohei Yukawa | 6db3bfe | 2017-02-13 12:04:41 -0800 | [diff] [blame] | 119 | import android.view.inputmethod.InputConnection; |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 120 | import android.view.inputmethod.InputConnectionInspector; |
Yohei Yukawa | 432cf60 | 2018-10-21 10:41:37 -0700 | [diff] [blame] | 121 | import android.view.inputmethod.InputConnectionInspector.MissingMethodFlags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | import android.view.inputmethod.InputMethod; |
| 123 | import android.view.inputmethod.InputMethodInfo; |
| 124 | import android.view.inputmethod.InputMethodManager; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 125 | import android.view.inputmethod.InputMethodSubtype; |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 126 | import android.widget.ArrayAdapter; |
satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 127 | import android.widget.CompoundButton; |
| 128 | import android.widget.CompoundButton.OnCheckedChangeListener; |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 129 | import android.widget.RadioButton; |
satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 130 | import android.widget.Switch; |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 131 | import android.widget.TextView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | |
Yohei Yukawa | 0569a18 | 2018-08-28 16:09:28 -0700 | [diff] [blame] | 133 | import com.android.internal.annotations.GuardedBy; |
| 134 | import com.android.internal.content.PackageMonitor; |
| 135 | import com.android.internal.inputmethod.IInputContentUriToken; |
Yohei Yukawa | c54c117 | 2018-09-06 11:39:50 -0700 | [diff] [blame] | 136 | import com.android.internal.inputmethod.IInputMethodPrivilegedOperations; |
Yohei Yukawa | a468d70 | 2018-10-21 11:42:34 -0700 | [diff] [blame] | 137 | import com.android.internal.inputmethod.InputMethodDebug; |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 138 | import com.android.internal.inputmethod.SoftInputShowHideReason; |
Yohei Yukawa | 35fa6d5 | 2018-10-31 11:33:32 -0700 | [diff] [blame] | 139 | import com.android.internal.inputmethod.StartInputFlags; |
Yohei Yukawa | c6632df | 2018-10-21 11:47:16 -0700 | [diff] [blame] | 140 | import com.android.internal.inputmethod.StartInputReason; |
Yohei Yukawa | 499e3f7 | 2018-10-21 20:15:11 -0700 | [diff] [blame] | 141 | import com.android.internal.inputmethod.UnbindReason; |
Yohei Yukawa | 0569a18 | 2018-08-28 16:09:28 -0700 | [diff] [blame] | 142 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
| 143 | import com.android.internal.notification.SystemNotificationChannels; |
| 144 | import com.android.internal.os.HandlerCaller; |
| 145 | import com.android.internal.os.SomeArgs; |
| 146 | import com.android.internal.os.TransferPipe; |
| 147 | import com.android.internal.util.DumpUtils; |
Yohei Yukawa | 0569a18 | 2018-08-28 16:09:28 -0700 | [diff] [blame] | 148 | import com.android.internal.util.IndentingPrintWriter; |
Adam He | bc67f2e | 2019-11-13 14:34:56 -0800 | [diff] [blame] | 149 | import com.android.internal.view.IInlineSuggestionsRequestCallback; |
Feng Cao | 16b2de5 | 2020-01-09 17:27:27 -0800 | [diff] [blame] | 150 | import com.android.internal.view.IInlineSuggestionsResponseCallback; |
Yohei Yukawa | 0569a18 | 2018-08-28 16:09:28 -0700 | [diff] [blame] | 151 | import com.android.internal.view.IInputContext; |
| 152 | import com.android.internal.view.IInputMethod; |
| 153 | import com.android.internal.view.IInputMethodClient; |
| 154 | import com.android.internal.view.IInputMethodManager; |
| 155 | import com.android.internal.view.IInputMethodSession; |
| 156 | import com.android.internal.view.IInputSessionCallback; |
Feng Cao | 36960ee | 2020-02-18 18:23:30 -0800 | [diff] [blame] | 157 | import com.android.internal.view.InlineSuggestionsRequestInfo; |
Yohei Yukawa | 0569a18 | 2018-08-28 16:09:28 -0700 | [diff] [blame] | 158 | import com.android.internal.view.InputBindResult; |
Yohei Yukawa | 603f4d0 | 2018-09-11 15:04:58 -0700 | [diff] [blame] | 159 | import com.android.server.EventLogTags; |
| 160 | import com.android.server.LocalServices; |
| 161 | import com.android.server.SystemService; |
Yuichiro Hanada | 34c4c0e | 2020-01-15 16:53:07 +0900 | [diff] [blame] | 162 | import com.android.server.inputmethod.InputMethodManagerInternal.InputMethodListListener; |
Yohei Yukawa | e6b6e0e | 2018-09-12 16:42:48 -0700 | [diff] [blame] | 163 | import com.android.server.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem; |
| 164 | import com.android.server.inputmethod.InputMethodUtils.InputMethodSettings; |
Yohei Yukawa | 0569a18 | 2018-08-28 16:09:28 -0700 | [diff] [blame] | 165 | import com.android.server.statusbar.StatusBarManagerService; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 166 | import com.android.server.wm.WindowManagerInternal; |
| 167 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | import java.io.FileDescriptor; |
| 169 | import java.io.IOException; |
| 170 | import java.io.PrintWriter; |
Yohei Yukawa | fa0e47e | 2016-04-05 09:55:56 -0700 | [diff] [blame] | 171 | import java.lang.annotation.Retention; |
Yohei Yukawa | 25e0813 | 2016-06-22 16:31:41 -0700 | [diff] [blame] | 172 | import java.security.InvalidParameterException; |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 173 | import java.text.SimpleDateFormat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | import java.util.ArrayList; |
Yohei Yukawa | b8d240f | 2018-12-26 10:03:11 -0800 | [diff] [blame] | 175 | import java.util.Arrays; |
satok | 688bd47 | 2012-02-09 20:09:17 +0900 | [diff] [blame] | 176 | import java.util.Collections; |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 177 | import java.util.Date; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | import java.util.List; |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 179 | import java.util.Locale; |
Yohei Yukawa | 6db3bfe | 2017-02-13 12:04:41 -0800 | [diff] [blame] | 180 | import java.util.WeakHashMap; |
Yuichiro Hanada | 34c4c0e | 2020-01-15 16:53:07 +0900 | [diff] [blame] | 181 | import java.util.concurrent.CopyOnWriteArrayList; |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 182 | import java.util.concurrent.atomic.AtomicInteger; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | |
| 184 | /** |
| 185 | * This class provides a system service that manages input methods. |
| 186 | */ |
| 187 | public class InputMethodManagerService extends IInputMethodManager.Stub |
| 188 | implements ServiceConnection, Handler.Callback { |
| 189 | static final boolean DEBUG = false; |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 190 | static final String TAG = "InputMethodManagerService"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 192 | @Retention(SOURCE) |
| 193 | @IntDef({ShellCommandResult.SUCCESS, ShellCommandResult.FAILURE}) |
| 194 | private @interface ShellCommandResult { |
| 195 | int SUCCESS = 0; |
| 196 | int FAILURE = -1; |
| 197 | } |
| 198 | |
Seigo Nonaka | d4474cb | 2015-05-04 16:53:24 -0700 | [diff] [blame] | 199 | static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1; |
| 200 | static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2; |
| 201 | static final int MSG_SHOW_IM_CONFIG = 3; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 202 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | static final int MSG_UNBIND_INPUT = 1000; |
| 204 | static final int MSG_BIND_INPUT = 1010; |
| 205 | static final int MSG_SHOW_SOFT_INPUT = 1020; |
| 206 | static final int MSG_HIDE_SOFT_INPUT = 1030; |
Jorim Jaggi | 3c5d0f1 | 2016-05-24 19:04:30 -0700 | [diff] [blame] | 207 | static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035; |
Yohei Yukawa | c54c117 | 2018-09-06 11:39:50 -0700 | [diff] [blame] | 208 | static final int MSG_INITIALIZE_IME = 1040; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | static final int MSG_CREATE_SESSION = 1050; |
Yunfan Chen | c02a5ac | 2020-06-16 01:52:41 +0000 | [diff] [blame] | 210 | static final int MSG_REMOVE_IME_SURFACE = 1060; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 211 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | static final int MSG_START_INPUT = 2000; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 213 | |
Yohei Yukawa | 33e8179 | 2015-11-17 21:14:42 -0800 | [diff] [blame] | 214 | static final int MSG_UNBIND_CLIENT = 3000; |
| 215 | static final int MSG_BIND_CLIENT = 3010; |
Dianne Hackborn | a6e4134 | 2012-05-22 16:30:34 -0700 | [diff] [blame] | 216 | static final int MSG_SET_ACTIVE = 3020; |
Yohei Yukawa | fa6e0a8 | 2015-07-23 15:08:59 -0700 | [diff] [blame] | 217 | static final int MSG_SET_INTERACTIVE = 3030; |
Yohei Yukawa | 2bc6617 | 2017-02-08 11:13:25 -0800 | [diff] [blame] | 218 | static final int MSG_REPORT_FULLSCREEN_MODE = 3045; |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 219 | static final int MSG_REPORT_PRE_RENDERED = 3060; |
| 220 | static final int MSG_APPLY_IME_VISIBILITY = 3070; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 221 | |
satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 222 | static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000; |
| 223 | |
Fyodor Kupolov | 7877b8a | 2016-06-29 14:39:19 -0700 | [diff] [blame] | 224 | static final int MSG_SYSTEM_UNLOCK_USER = 5000; |
Yuichiro Hanada | 34c4c0e | 2020-01-15 16:53:07 +0900 | [diff] [blame] | 225 | static final int MSG_DISPATCH_ON_INPUT_METHOD_LIST_UPDATED = 5010; |
Fyodor Kupolov | 7877b8a | 2016-06-29 14:39:19 -0700 | [diff] [blame] | 226 | |
Adam He | bc67f2e | 2019-11-13 14:34:56 -0800 | [diff] [blame] | 227 | static final int MSG_INLINE_SUGGESTIONS_REQUEST = 6000; |
| 228 | |
Yohei Yukawa | d5f402b | 2020-05-15 10:23:32 -0700 | [diff] [blame] | 229 | static final int MSG_NOTIFY_IME_UID_TO_AUDIO_SERVICE = 7000; |
| 230 | |
Satoshi Kataoka | bcacc32 | 2013-10-21 17:57:27 -0700 | [diff] [blame] | 231 | static final long TIME_TO_RECONNECT = 3 * 1000; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 232 | |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 233 | static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20; |
| 234 | |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 235 | private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID; |
satok | b635941 | 2011-06-28 17:47:41 +0900 | [diff] [blame] | 236 | private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher"; |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 237 | |
Yohei Yukawa | a67a459 | 2017-03-30 15:57:02 -0700 | [diff] [blame] | 238 | /** |
| 239 | * Binding flags for establishing connection to the {@link InputMethodService}. |
| 240 | */ |
| 241 | private static final int IME_CONNECTION_BIND_FLAGS = |
| 242 | Context.BIND_AUTO_CREATE |
| 243 | | Context.BIND_NOT_VISIBLE |
| 244 | | Context.BIND_NOT_FOREGROUND |
Yohei Yukawa | ad78a61 | 2017-08-04 01:57:27 -0700 | [diff] [blame] | 245 | | Context.BIND_IMPORTANT_BACKGROUND; |
Yohei Yukawa | a67a459 | 2017-03-30 15:57:02 -0700 | [diff] [blame] | 246 | |
| 247 | /** |
| 248 | * Binding flags used only while the {@link InputMethodService} is showing window. |
| 249 | */ |
| 250 | private static final int IME_VISIBLE_BIND_FLAGS = |
| 251 | Context.BIND_AUTO_CREATE |
| 252 | | Context.BIND_TREAT_LIKE_ACTIVITY |
Yohei Yukawa | ad78a61 | 2017-08-04 01:57:27 -0700 | [diff] [blame] | 253 | | Context.BIND_FOREGROUND_SERVICE |
Amith Yamasani | c45a990 | 2019-04-05 16:29:30 -0700 | [diff] [blame] | 254 | | Context.BIND_INCLUDE_CAPABILITIES |
Yohei Yukawa | 5973096 | 2019-03-18 10:47:22 -0700 | [diff] [blame] | 255 | | Context.BIND_SHOWING_UI |
| 256 | | Context.BIND_SCHEDULE_LIKE_TOP_APP; |
Yohei Yukawa | a67a459 | 2017-03-30 15:57:02 -0700 | [diff] [blame] | 257 | |
Tadashi G. Takaoka | 01065a5 | 2017-07-19 14:10:24 +0900 | [diff] [blame] | 258 | /** |
| 259 | * A protected broadcast intent action for internal use for {@link PendingIntent} in |
| 260 | * the notification. |
| 261 | */ |
| 262 | private static final String ACTION_SHOW_INPUT_METHOD_PICKER = |
Yohei Yukawa | 603f4d0 | 2018-09-11 15:04:58 -0700 | [diff] [blame] | 263 | "com.android.server.inputmethod.InputMethodManagerService.SHOW_INPUT_METHOD_PICKER"; |
Tadashi G. Takaoka | 01065a5 | 2017-07-19 14:10:24 +0900 | [diff] [blame] | 264 | |
Tarandeep Singh | 75a9239 | 2018-01-12 14:58:59 -0800 | [diff] [blame] | 265 | /** |
| 266 | * Debug flag for overriding runtime {@link SystemProperties}. |
| 267 | */ |
| 268 | @AnyThread |
| 269 | private static final class DebugFlag { |
| 270 | private static final Object LOCK = new Object(); |
| 271 | private final String mKey; |
Tarandeep Singh | 9df4ad1 | 2018-04-03 13:35:50 -0700 | [diff] [blame] | 272 | private final boolean mDefaultValue; |
Tarandeep Singh | 75a9239 | 2018-01-12 14:58:59 -0800 | [diff] [blame] | 273 | @GuardedBy("LOCK") |
| 274 | private boolean mValue; |
| 275 | |
Tarandeep Singh | 1c042c5 | 2018-03-29 17:04:44 -0700 | [diff] [blame] | 276 | public DebugFlag(String key, boolean defaultValue) { |
Tarandeep Singh | 75a9239 | 2018-01-12 14:58:59 -0800 | [diff] [blame] | 277 | mKey = key; |
Tarandeep Singh | 9df4ad1 | 2018-04-03 13:35:50 -0700 | [diff] [blame] | 278 | mDefaultValue = defaultValue; |
Tarandeep Singh | 1c042c5 | 2018-03-29 17:04:44 -0700 | [diff] [blame] | 279 | mValue = SystemProperties.getBoolean(key, defaultValue); |
Tarandeep Singh | 75a9239 | 2018-01-12 14:58:59 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void refresh() { |
| 283 | synchronized (LOCK) { |
Tarandeep Singh | 9df4ad1 | 2018-04-03 13:35:50 -0700 | [diff] [blame] | 284 | mValue = SystemProperties.getBoolean(mKey, mDefaultValue); |
Tarandeep Singh | 75a9239 | 2018-01-12 14:58:59 -0800 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | boolean value() { |
| 289 | synchronized (LOCK) { |
| 290 | return mValue; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Debug flags that can be overridden using "adb shell setprop <key>" |
| 297 | * Note: These flags are cached. To refresh, run "adb shell ime refresh_debug_properties". |
| 298 | */ |
| 299 | private static final class DebugFlags { |
| 300 | static final DebugFlag FLAG_OPTIMIZE_START_INPUT = |
Tarandeep Singh | 1c042c5 | 2018-03-29 17:04:44 -0700 | [diff] [blame] | 301 | new DebugFlag("debug.optimize_startinput", false); |
Tarandeep Singh | eadb139 | 2018-11-09 18:15:57 +0100 | [diff] [blame] | 302 | static final DebugFlag FLAG_PRE_RENDER_IME_VIEWS = |
| 303 | new DebugFlag("persist.pre_render_ime_views", false); |
Tarandeep Singh | 75a9239 | 2018-01-12 14:58:59 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Yohei Yukawa | a7babbb | 2019-01-08 16:45:34 -0800 | [diff] [blame] | 306 | @UserIdInt |
| 307 | private int mLastSwitchUserId; |
Tarandeep Singh | 75a9239 | 2018-01-12 14:58:59 -0800 | [diff] [blame] | 308 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | final Context mContext; |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 310 | final Resources mRes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | final Handler mHandler; |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 312 | final InputMethodSettings mSettings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | final SettingsObserver mSettingsObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | final IWindowManager mIWindowManager; |
Seigo Nonaka | 7309b12 | 2015-08-17 18:34:13 -0700 | [diff] [blame] | 315 | final WindowManagerInternal mWindowManagerInternal; |
Yohei Yukawa | d5f402b | 2020-05-15 10:23:32 -0700 | [diff] [blame] | 316 | final PackageManagerInternal mPackageManagerInternal; |
lpeter | 133fce0 | 2020-03-05 20:32:16 +0800 | [diff] [blame] | 317 | final InputManagerInternal mInputManagerInternal; |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 318 | private final DisplayManagerInternal mDisplayManagerInternal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | final HandlerCaller mCaller; |
Dianne Hackborn | 119bbc3 | 2013-03-22 17:27:25 -0700 | [diff] [blame] | 320 | final boolean mHasFeature; |
Yohei Yukawa | b557d57 | 2018-12-29 21:26:26 -0800 | [diff] [blame] | 321 | private final ArrayMap<String, List<InputMethodSubtype>> mAdditionalSubtypeMap = |
| 322 | new ArrayMap<>(); |
Tarandeep Singh | eadb139 | 2018-11-09 18:15:57 +0100 | [diff] [blame] | 323 | private final boolean mIsLowRam; |
satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 324 | private final HardKeyboardListener mHardKeyboardListener; |
Yohei Yukawa | e63b5fa | 2014-09-19 13:14:55 +0900 | [diff] [blame] | 325 | private final AppOpsManager mAppOpsManager; |
Yohei Yukawa | ed4952a | 2016-02-17 07:57:25 -0800 | [diff] [blame] | 326 | private final UserManager mUserManager; |
Yohei Yukawa | 4208140 | 2019-01-15 09:57:50 -0800 | [diff] [blame] | 327 | private final UserManagerInternal mUserManagerInternal; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 328 | |
Yohei Yukawa | d5f402b | 2020-05-15 10:23:32 -0700 | [diff] [blame] | 329 | /** |
| 330 | * Cache the result of {@code LocalServices.getService(AudioManagerInternal.class)}. |
| 331 | * |
| 332 | * <p>This field is used only within {@link #handleMessage(Message)} hence synchronization is |
| 333 | * not necessary.</p> |
| 334 | */ |
| 335 | @Nullable |
| 336 | private AudioManagerInternal mAudioManagerInternal = null; |
| 337 | |
| 338 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | // All known input methods. mMethodMap also serves as the global |
| 340 | // lock for this class. |
Yohei Yukawa | b0377bb | 2015-08-10 21:06:30 -0700 | [diff] [blame] | 341 | final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>(); |
Yohei Yukawa | 1dd7de6 | 2018-11-20 19:25:29 -0800 | [diff] [blame] | 342 | final ArrayMap<String, InputMethodInfo> mMethodMap = new ArrayMap<>(); |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 343 | private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans = |
Yohei Yukawa | b0377bb | 2015-08-10 21:06:30 -0700 | [diff] [blame] | 344 | new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE); |
Satoshi Kataoka | d787f69 | 2013-10-26 04:44:21 +0900 | [diff] [blame] | 345 | private final InputMethodSubtypeSwitchingController mSwitchingController; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 346 | |
Yohei Yukawa | e073306 | 2017-02-09 22:49:35 -0800 | [diff] [blame] | 347 | /** |
| 348 | * Tracks how many times {@link #mMethodMap} was updated. |
| 349 | */ |
| 350 | @GuardedBy("mMethodMap") |
| 351 | private int mMethodMapUpdateCount = 0; |
| 352 | |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 353 | // Used to bring IME service up to visible adjustment while it is being shown. |
| 354 | final ServiceConnection mVisibleConnection = new ServiceConnection() { |
Yohei Yukawa | d3ef842 | 2018-06-07 16:28:07 -0700 | [diff] [blame] | 355 | @Override public void onBindingDied(ComponentName name) { |
| 356 | synchronized (mMethodMap) { |
| 357 | if (mVisibleBound) { |
| 358 | mContext.unbindService(mVisibleConnection); |
| 359 | mVisibleBound = false; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 364 | @Override public void onServiceConnected(ComponentName name, IBinder service) { |
| 365 | } |
| 366 | |
| 367 | @Override public void onServiceDisconnected(ComponentName name) { |
| 368 | } |
| 369 | }; |
| 370 | boolean mVisibleBound = false; |
| 371 | |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 372 | // Ongoing notification |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 373 | private NotificationManager mNotificationManager; |
| 374 | private KeyguardManager mKeyguardManager; |
Griff Hazen | 6090c26 | 2016-03-25 08:11:24 -0700 | [diff] [blame] | 375 | private @Nullable StatusBarManagerService mStatusBar; |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 376 | private Notification.Builder mImeSwitcherNotification; |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 377 | private PendingIntent mImeSwitchPendingIntent; |
satok | b858c73 | 2011-07-22 19:54:34 +0900 | [diff] [blame] | 378 | private boolean mShowOngoingImeSwitcherForPhones; |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 379 | private boolean mNotificationShown; |
| 380 | |
Tadashi G. Takaoka | 8c6d477 | 2014-08-05 15:29:17 +0900 | [diff] [blame] | 381 | static class SessionState { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | final ClientState client; |
| 383 | final IInputMethod method; |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 384 | |
| 385 | IInputMethodSession session; |
| 386 | InputChannel channel; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 387 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | @Override |
| 389 | public String toString() { |
| 390 | return "SessionState{uid " + client.uid + " pid " + client.pid |
| 391 | + " method " + Integer.toHexString( |
| 392 | System.identityHashCode(method)) |
| 393 | + " session " + Integer.toHexString( |
| 394 | System.identityHashCode(session)) |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 395 | + " channel " + channel |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | + "}"; |
| 397 | } |
| 398 | |
| 399 | SessionState(ClientState _client, IInputMethod _method, |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 400 | IInputMethodSession _session, InputChannel _channel) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | client = _client; |
| 402 | method = _method; |
| 403 | session = _session; |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 404 | channel = _channel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | } |
| 406 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 407 | |
Yohei Yukawa | a71bb25 | 2018-09-19 19:21:24 -0700 | [diff] [blame] | 408 | private static final class ClientDeathRecipient implements IBinder.DeathRecipient { |
| 409 | private final InputMethodManagerService mImms; |
| 410 | private final IInputMethodClient mClient; |
| 411 | |
| 412 | ClientDeathRecipient(InputMethodManagerService imms, IInputMethodClient client) { |
| 413 | mImms = imms; |
| 414 | mClient = client; |
| 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public void binderDied() { |
| 419 | mImms.removeClient(mClient); |
| 420 | } |
| 421 | } |
| 422 | |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 423 | static final class ClientState { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | final IInputMethodClient client; |
| 425 | final IInputContext inputContext; |
| 426 | final int uid; |
| 427 | final int pid; |
Yohei Yukawa | 4052a10f | 2018-10-15 15:35:55 +0800 | [diff] [blame] | 428 | final int selfReportedDisplayId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | final InputBinding binding; |
Yohei Yukawa | a71bb25 | 2018-09-19 19:21:24 -0700 | [diff] [blame] | 430 | final ClientDeathRecipient clientDeathRecipient; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 431 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | boolean sessionRequested; |
Tarandeep Singh | eadb139 | 2018-11-09 18:15:57 +0100 | [diff] [blame] | 433 | // Determines if IMEs should be pre-rendered. |
| 434 | // DebugFlag can be flipped anytime. This flag is kept per-client to maintain behavior |
| 435 | // through the life of the current client. |
| 436 | boolean shouldPreRenderIme; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | SessionState curSession; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 438 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | @Override |
| 440 | public String toString() { |
| 441 | return "ClientState{" + Integer.toHexString( |
Yohei Yukawa | 4052a10f | 2018-10-15 15:35:55 +0800 | [diff] [blame] | 442 | System.identityHashCode(this)) + " uid=" + uid |
| 443 | + " pid=" + pid + " displayId=" + selfReportedDisplayId + "}"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | ClientState(IInputMethodClient _client, IInputContext _inputContext, |
Yohei Yukawa | 4052a10f | 2018-10-15 15:35:55 +0800 | [diff] [blame] | 447 | int _uid, int _pid, int _selfReportedDisplayId, |
| 448 | ClientDeathRecipient _clientDeathRecipient) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | client = _client; |
| 450 | inputContext = _inputContext; |
| 451 | uid = _uid; |
| 452 | pid = _pid; |
Yohei Yukawa | 4052a10f | 2018-10-15 15:35:55 +0800 | [diff] [blame] | 453 | selfReportedDisplayId = _selfReportedDisplayId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | binding = new InputBinding(null, inputContext.asBinder(), uid, pid); |
Yohei Yukawa | a71bb25 | 2018-09-19 19:21:24 -0700 | [diff] [blame] | 455 | clientDeathRecipient = _clientDeathRecipient; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | } |
| 457 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 458 | |
Yohei Yukawa | ac9311e | 2018-11-20 19:25:23 -0800 | [diff] [blame] | 459 | final ArrayMap<IBinder, ClientState> mClients = new ArrayMap<>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 460 | |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 461 | private static final class ActivityViewInfo { |
| 462 | /** |
| 463 | * {@link ClientState} where {@link android.app.ActivityView} is running. |
| 464 | */ |
| 465 | private final ClientState mParentClient; |
| 466 | /** |
| 467 | * {@link Matrix} to convert screen coordinates in the embedded virtual display to |
| 468 | * screen coordinates where {@link #mParentClient} exists. |
| 469 | */ |
| 470 | private final Matrix mMatrix; |
| 471 | |
| 472 | ActivityViewInfo(ClientState parentClient, Matrix matrix) { |
| 473 | mParentClient = parentClient; |
| 474 | mMatrix = matrix; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * A mapping table from virtual display IDs created for {@link android.app.ActivityView} |
| 480 | * to its parent IME client where {@link android.app.ActivityView} is running. |
| 481 | * |
| 482 | * <p>Note: this can be used only for virtual display IDs created by |
| 483 | * {@link android.app.ActivityView}.</p> |
| 484 | */ |
| 485 | private SparseArray<ActivityViewInfo> mActivityViewDisplayIdToParentMap = new SparseArray<>(); |
| 486 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | /** |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 488 | * Set once the system is ready to run third party code. |
| 489 | */ |
| 490 | boolean mSystemReady; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 491 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 492 | /** |
Yohei Yukawa | e13a20fa | 2015-09-30 19:11:32 -0700 | [diff] [blame] | 493 | * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method. |
| 494 | * method. This is to be synchronized with the secure settings keyed with |
| 495 | * {@link Settings.Secure#DEFAULT_INPUT_METHOD}. |
| 496 | * |
| 497 | * <p>This can be transiently {@code null} when the system is re-initializing input method |
| 498 | * settings, e.g., the system locale is just changed.</p> |
| 499 | * |
| 500 | * <p>Note that {@link #mCurId} is used to track which IME is being connected to |
| 501 | * {@link InputMethodManagerService}.</p> |
| 502 | * |
| 503 | * @see #mCurId |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | */ |
Yohei Yukawa | e13a20fa | 2015-09-30 19:11:32 -0700 | [diff] [blame] | 505 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | String mCurMethodId; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 507 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | /** |
| 509 | * The current binding sequence number, incremented every time there is |
| 510 | * a new bind performed. |
| 511 | */ |
| 512 | int mCurSeq; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 513 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | /** |
| 515 | * The client that is currently bound to an input method. |
| 516 | */ |
| 517 | ClientState mCurClient; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 518 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | /** |
Yohei Yukawa | e39d4ed | 2015-11-19 03:38:49 -0800 | [diff] [blame] | 520 | * The last window token that we confirmed to be focused. This is always updated upon reports |
| 521 | * from the input method client. If the window state is already changed before the report is |
| 522 | * handled, this field just keeps the last value. |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 523 | */ |
| 524 | IBinder mCurFocusedWindow; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 525 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 526 | /** |
Yohei Yukawa | 99e1f6e | 2018-06-22 17:33:19 -0700 | [diff] [blame] | 527 | * The last window token that we confirmed that IME started talking to. This is always updated |
| 528 | * upon reports from the input method. If the window state is already changed before the report |
| 529 | * is handled, this field just keeps the last value. |
| 530 | */ |
| 531 | IBinder mLastImeTargetWindow; |
| 532 | |
| 533 | /** |
Yohei Yukawa | 6e87559 | 2019-01-28 00:49:30 -0800 | [diff] [blame] | 534 | * {@link LayoutParams#softInputMode} of {@link #mCurFocusedWindow}. |
Yohei Yukawa | 22a8923 | 2017-02-12 16:38:59 -0800 | [diff] [blame] | 535 | * |
| 536 | * @see #mCurFocusedWindow |
| 537 | */ |
Yohei Yukawa | b0c2645 | 2018-10-21 10:42:52 -0700 | [diff] [blame] | 538 | @SoftInputModeFlags |
Yohei Yukawa | 22a8923 | 2017-02-12 16:38:59 -0800 | [diff] [blame] | 539 | int mCurFocusedWindowSoftInputMode; |
| 540 | |
| 541 | /** |
Tarandeep Singh | eb57061 | 2018-01-29 16:20:32 -0800 | [diff] [blame] | 542 | * The client by which {@link #mCurFocusedWindow} was reported. |
Yohei Yukawa | e39d4ed | 2015-11-19 03:38:49 -0800 | [diff] [blame] | 543 | */ |
| 544 | ClientState mCurFocusedWindowClient; |
| 545 | |
| 546 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | * The input context last provided by the current client. |
| 548 | */ |
| 549 | IInputContext mCurInputContext; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 550 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | /** |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 552 | * The missing method flags for the input context last provided by the current client. |
| 553 | * |
| 554 | * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags |
| 555 | */ |
Yohei Yukawa | 432cf60 | 2018-10-21 10:41:37 -0700 | [diff] [blame] | 556 | @MissingMethodFlags |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 557 | int mCurInputContextMissingMethods; |
| 558 | |
| 559 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | * The attributes last provided by the current client. |
| 561 | */ |
| 562 | EditorInfo mCurAttribute; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 563 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | /** |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 565 | * A special {@link Matrix} to convert virtual screen coordinates to the IME target display |
| 566 | * coordinates. |
| 567 | * |
| 568 | * <p>Used only while the IME client is running in a virtual display inside |
| 569 | * {@link android.app.ActivityView}. {@code null} otherwise.</p> |
| 570 | */ |
| 571 | @Nullable |
| 572 | private Matrix mCurActivityViewToScreenMatrix = null; |
| 573 | |
| 574 | /** |
Yohei Yukawa | e13a20fa | 2015-09-30 19:11:32 -0700 | [diff] [blame] | 575 | * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | * connected to or in the process of connecting to. |
Yohei Yukawa | e13a20fa | 2015-09-30 19:11:32 -0700 | [diff] [blame] | 577 | * |
| 578 | * <p>This can be {@code null} when no input method is connected.</p> |
| 579 | * |
| 580 | * @see #mCurMethodId |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | */ |
Yohei Yukawa | e13a20fa | 2015-09-30 19:11:32 -0700 | [diff] [blame] | 582 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | String mCurId; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 584 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | /** |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 586 | * The current subtype of the current input method. |
| 587 | */ |
| 588 | private InputMethodSubtype mCurrentSubtype; |
| 589 | |
John Spurlock | e098050 | 2013-10-25 11:59:29 -0400 | [diff] [blame] | 590 | // Was the keyguard locked when this client became current? |
| 591 | private boolean mCurClientInKeyguard; |
| 592 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 593 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | * Set to true if our ServiceConnection is currently actively bound to |
| 595 | * a service (whether or not we have gotten its IBinder back yet). |
| 596 | */ |
| 597 | boolean mHaveConnection; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 598 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | /** |
| 600 | * Set if the client has asked for the input method to be shown. |
| 601 | */ |
| 602 | boolean mShowRequested; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 603 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 604 | /** |
| 605 | * Set if we were explicitly told to show the input method. |
| 606 | */ |
| 607 | boolean mShowExplicitlyRequested; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 608 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | /** |
| 610 | * Set if we were forced to be shown. |
| 611 | */ |
| 612 | boolean mShowForced; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 613 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | /** |
| 615 | * Set if we last told the input method to show itself. |
| 616 | */ |
| 617 | boolean mInputShown; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 618 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | /** |
Yohei Yukawa | 2bc6617 | 2017-02-08 11:13:25 -0800 | [diff] [blame] | 620 | * {@code true} if the current input method is in fullscreen mode. |
| 621 | */ |
| 622 | boolean mInFullscreenMode; |
| 623 | |
| 624 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | * The Intent used to connect to the current input method. |
| 626 | */ |
| 627 | Intent mCurIntent; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 628 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | /** |
| 630 | * The token we have made for the currently active input method, to |
| 631 | * identify it in the future. |
| 632 | */ |
| 633 | IBinder mCurToken; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 634 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | /** |
lumark | 90120a8 | 2018-08-15 00:33:03 +0800 | [diff] [blame] | 636 | * The displayId of current active input method. |
| 637 | */ |
| 638 | int mCurTokenDisplayId = INVALID_DISPLAY; |
| 639 | |
lumark | 7570cac | 2019-03-07 22:14:38 +0800 | [diff] [blame] | 640 | /** |
lpeter | 133fce0 | 2020-03-05 20:32:16 +0800 | [diff] [blame] | 641 | * The host input token of the current active input method. |
| 642 | */ |
| 643 | @GuardedBy("mMethodMap") |
| 644 | @Nullable |
| 645 | private IBinder mCurHostInputToken; |
| 646 | |
| 647 | /** |
lumark | 7570cac | 2019-03-07 22:14:38 +0800 | [diff] [blame] | 648 | * The display ID of the input method indicates the fallback display which returned by |
| 649 | * {@link #computeImeDisplayIdForTarget}. |
| 650 | */ |
| 651 | private static final int FALLBACK_DISPLAY_ID = DEFAULT_DISPLAY; |
| 652 | |
lumark | ef1965b | 2018-09-12 17:42:53 +0800 | [diff] [blame] | 653 | final ImeDisplayValidator mImeDisplayValidator; |
| 654 | |
lumark | 90120a8 | 2018-08-15 00:33:03 +0800 | [diff] [blame] | 655 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 656 | * If non-null, this is the input method service we are currently connected |
| 657 | * to. |
| 658 | */ |
| 659 | IInputMethod mCurMethod; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 660 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 661 | /** |
Yohei Yukawa | d5f402b | 2020-05-15 10:23:32 -0700 | [diff] [blame] | 662 | * If not {@link Process#INVALID_UID}, then the UID of {@link #mCurIntent}. |
| 663 | */ |
| 664 | int mCurMethodUid = Process.INVALID_UID; |
| 665 | |
| 666 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | * Time that we last initiated a bind to the input method, to determine |
| 668 | * if we should try to disconnect and reconnect to it. |
| 669 | */ |
| 670 | long mLastBindTime; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 671 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | /** |
| 673 | * Have we called mCurMethod.bindInput()? |
| 674 | */ |
| 675 | boolean mBoundToMethod; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 676 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | /** |
| 678 | * Currently enabled session. Only touched by service thread, not |
| 679 | * protected by a lock. |
| 680 | */ |
| 681 | SessionState mEnabledSession; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 682 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 683 | /** |
Yohei Yukawa | fa6e0a8 | 2015-07-23 15:08:59 -0700 | [diff] [blame] | 684 | * True if the device is currently interactive with user. The value is true initially. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 685 | */ |
Yohei Yukawa | fa6e0a8 | 2015-07-23 15:08:59 -0700 | [diff] [blame] | 686 | boolean mIsInteractive = true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 687 | |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 688 | int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT; |
Seigo Nonaka | d9eb911 | 2015-05-26 20:54:43 +0900 | [diff] [blame] | 689 | |
| 690 | /** |
| 691 | * A set of status bits regarding the active IME. |
| 692 | * |
| 693 | * <p>This value is a combination of following two bits:</p> |
| 694 | * <dl> |
| 695 | * <dt>{@link InputMethodService#IME_ACTIVE}</dt> |
| 696 | * <dd> |
| 697 | * If this bit is ON, connected IME is ready to accept touch/key events. |
| 698 | * </dd> |
| 699 | * <dt>{@link InputMethodService#IME_VISIBLE}</dt> |
| 700 | * <dd> |
| 701 | * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible. |
| 702 | * </dd> |
Tarandeep Singh | eadb139 | 2018-11-09 18:15:57 +0100 | [diff] [blame] | 703 | * dt>{@link InputMethodService#IME_INVISIBLE}</dt> |
| 704 | * <dd> If this bit is ON, IME is ready with views from last EditorInfo but is |
| 705 | * currently invisible. |
| 706 | * </dd> |
Seigo Nonaka | d9eb911 | 2015-05-26 20:54:43 +0900 | [diff] [blame] | 707 | * </dl> |
lumark | 7570cac | 2019-03-07 22:14:38 +0800 | [diff] [blame] | 708 | * <em>Do not update this value outside of {@link #setImeWindowStatus(IBinder, int, int)} and |
| 709 | * {@link #unbindCurrentMethodLocked()}.</em> |
Seigo Nonaka | d9eb911 | 2015-05-26 20:54:43 +0900 | [diff] [blame] | 710 | */ |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 711 | int mImeWindowVis; |
| 712 | |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 713 | private AlertDialog.Builder mDialogBuilder; |
| 714 | private AlertDialog mSwitchingDialog; |
Wale Ogunwale | 3a93169 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 715 | private IBinder mSwitchingDialogToken = new Binder(); |
satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 716 | private View mSwitchingDialogTitleView; |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 717 | private InputMethodInfo[] mIms; |
| 718 | private int[] mSubtypeIds; |
Yohei Yukawa | e985c24 | 2016-02-24 18:27:04 -0800 | [diff] [blame] | 719 | private LocaleList mLastSystemLocales; |
Michael Wright | 7b5a96b | 2014-08-09 19:28:42 -0700 | [diff] [blame] | 720 | private boolean mShowImeWithHardKeyboard; |
Anna Galusza | 9b27811 | 2016-01-04 11:37:37 -0800 | [diff] [blame] | 721 | private boolean mAccessibilityRequestingNoSoftKeyboard; |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 722 | private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor(); |
| 723 | private final IPackageManager mIPackageManager; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 724 | private final String mSlotIme; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 725 | |
Yohei Yukawa | 6db3bfe | 2017-02-13 12:04:41 -0800 | [diff] [blame] | 726 | /** |
Yuichiro Hanada | 34c4c0e | 2020-01-15 16:53:07 +0900 | [diff] [blame] | 727 | * Registered {@link InputMethodListListeners}. |
| 728 | * This variable can be accessed from both of MainThread and BinderThread. |
| 729 | */ |
| 730 | private final CopyOnWriteArrayList<InputMethodListListener> mInputMethodListListeners = |
| 731 | new CopyOnWriteArrayList<>(); |
| 732 | |
| 733 | /** |
Yohei Yukawa | 6db3bfe | 2017-02-13 12:04:41 -0800 | [diff] [blame] | 734 | * Internal state snapshot when {@link #MSG_START_INPUT} message is about to be posted to the |
| 735 | * internal message queue. Any subsequent state change inside {@link InputMethodManagerService} |
| 736 | * will not affect those tasks that are already posted. |
| 737 | * |
| 738 | * <p>Posting {@link #MSG_START_INPUT} message basically means that |
| 739 | * {@link InputMethodService#doStartInput(InputConnection, EditorInfo, boolean)} will be called |
| 740 | * back in the current IME process shortly, which will also affect what the current IME starts |
| 741 | * receiving from {@link InputMethodService#getCurrentInputConnection()}. In other words, this |
| 742 | * snapshot will be taken every time when {@link InputMethodManagerService} is initiating a new |
| 743 | * logical input session between the client application and the current IME.</p> |
| 744 | * |
| 745 | * <p>Be careful to not keep strong references to this object forever, which can prevent |
| 746 | * {@link StartInputInfo#mImeToken} and {@link StartInputInfo#mTargetWindow} from being GC-ed. |
| 747 | * </p> |
| 748 | */ |
| 749 | private static class StartInputInfo { |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 750 | private static final AtomicInteger sSequenceNumber = new AtomicInteger(0); |
| 751 | |
| 752 | final int mSequenceNumber; |
| 753 | final long mTimestamp; |
| 754 | final long mWallTime; |
Yohei Yukawa | 7979e1a | 2019-02-12 02:01:10 -0800 | [diff] [blame] | 755 | @UserIdInt |
| 756 | final int mImeUserId; |
Yohei Yukawa | 6db3bfe | 2017-02-13 12:04:41 -0800 | [diff] [blame] | 757 | @NonNull |
| 758 | final IBinder mImeToken; |
Yohei Yukawa | 557afbb | 2019-02-12 02:01:00 -0800 | [diff] [blame] | 759 | final int mImeDisplayId; |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 760 | @NonNull |
| 761 | final String mImeId; |
Yohei Yukawa | dc66e52 | 2018-10-21 10:43:14 -0700 | [diff] [blame] | 762 | @StartInputReason |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 763 | final int mStartInputReason; |
| 764 | final boolean mRestarting; |
Yohei Yukawa | 7979e1a | 2019-02-12 02:01:10 -0800 | [diff] [blame] | 765 | @UserIdInt |
| 766 | final int mTargetUserId; |
Yohei Yukawa | 557afbb | 2019-02-12 02:01:00 -0800 | [diff] [blame] | 767 | final int mTargetDisplayId; |
Yohei Yukawa | 6db3bfe | 2017-02-13 12:04:41 -0800 | [diff] [blame] | 768 | @Nullable |
| 769 | final IBinder mTargetWindow; |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 770 | @NonNull |
| 771 | final EditorInfo mEditorInfo; |
Yohei Yukawa | b0c2645 | 2018-10-21 10:42:52 -0700 | [diff] [blame] | 772 | @SoftInputModeFlags |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 773 | final int mTargetWindowSoftInputMode; |
| 774 | final int mClientBindSequenceNumber; |
Yohei Yukawa | 6db3bfe | 2017-02-13 12:04:41 -0800 | [diff] [blame] | 775 | |
Yohei Yukawa | 7979e1a | 2019-02-12 02:01:10 -0800 | [diff] [blame] | 776 | StartInputInfo(@UserIdInt int imeUserId, @NonNull IBinder imeToken, int imeDisplayId, |
| 777 | @NonNull String imeId, @StartInputReason int startInputReason, boolean restarting, |
| 778 | @UserIdInt int targetUserId, int targetDisplayId, @Nullable IBinder targetWindow, |
| 779 | @NonNull EditorInfo editorInfo, @SoftInputModeFlags int targetWindowSoftInputMode, |
| 780 | int clientBindSequenceNumber) { |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 781 | mSequenceNumber = sSequenceNumber.getAndIncrement(); |
| 782 | mTimestamp = SystemClock.uptimeMillis(); |
| 783 | mWallTime = System.currentTimeMillis(); |
Yohei Yukawa | 7979e1a | 2019-02-12 02:01:10 -0800 | [diff] [blame] | 784 | mImeUserId = imeUserId; |
Yohei Yukawa | 6db3bfe | 2017-02-13 12:04:41 -0800 | [diff] [blame] | 785 | mImeToken = imeToken; |
Yohei Yukawa | 557afbb | 2019-02-12 02:01:00 -0800 | [diff] [blame] | 786 | mImeDisplayId = imeDisplayId; |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 787 | mImeId = imeId; |
| 788 | mStartInputReason = startInputReason; |
| 789 | mRestarting = restarting; |
Yohei Yukawa | 7979e1a | 2019-02-12 02:01:10 -0800 | [diff] [blame] | 790 | mTargetUserId = targetUserId; |
Yohei Yukawa | 557afbb | 2019-02-12 02:01:00 -0800 | [diff] [blame] | 791 | mTargetDisplayId = targetDisplayId; |
Yohei Yukawa | 6db3bfe | 2017-02-13 12:04:41 -0800 | [diff] [blame] | 792 | mTargetWindow = targetWindow; |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 793 | mEditorInfo = editorInfo; |
| 794 | mTargetWindowSoftInputMode = targetWindowSoftInputMode; |
| 795 | mClientBindSequenceNumber = clientBindSequenceNumber; |
Yohei Yukawa | 6db3bfe | 2017-02-13 12:04:41 -0800 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | |
Yohei Yukawa | b37d8bd | 2017-02-13 18:29:05 -0800 | [diff] [blame] | 799 | @GuardedBy("mMethodMap") |
Yohei Yukawa | 99e1f6e | 2018-06-22 17:33:19 -0700 | [diff] [blame] | 800 | private final WeakHashMap<IBinder, IBinder> mImeTargetWindowMap = new WeakHashMap<>(); |
Yohei Yukawa | 6db3bfe | 2017-02-13 12:04:41 -0800 | [diff] [blame] | 801 | |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 802 | private static final class SoftInputShowHideHistory { |
| 803 | private Entry[] mEntries = new Entry[16]; |
| 804 | private int mNextIndex = 0; |
| 805 | private static final AtomicInteger sSequenceNumber = new AtomicInteger(0); |
| 806 | |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 807 | private static final class Entry { |
Ming-Shin Lu | a0605a6 | 2020-04-15 23:49:40 +0800 | [diff] [blame] | 808 | final int mSequenceNumber = sSequenceNumber.getAndIncrement(); |
Anmol Gupta | 29f209d | 2020-03-11 11:34:46 -0700 | [diff] [blame] | 809 | final ClientState mClientState; |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 810 | @SoftInputModeFlags |
Anmol Gupta | 29f209d | 2020-03-11 11:34:46 -0700 | [diff] [blame] | 811 | final int mFocusedWindowSoftInputMode; |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 812 | @SoftInputShowHideReason |
Anmol Gupta | 29f209d | 2020-03-11 11:34:46 -0700 | [diff] [blame] | 813 | final int mReason; |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 814 | // The timing of handling MSG_SHOW_SOFT_INPUT or MSG_HIDE_SOFT_INPUT. |
Anmol Gupta | 29f209d | 2020-03-11 11:34:46 -0700 | [diff] [blame] | 815 | final long mTimestamp; |
| 816 | final long mWallTime; |
| 817 | final boolean mInFullscreenMode; |
| 818 | @NonNull |
| 819 | final String mFocusedWindowName; |
| 820 | @NonNull |
| 821 | final EditorInfo mEditorInfo; |
| 822 | @NonNull |
| 823 | final String mRequestWindowName; |
Ming-Shin Lu | d0bc171 | 2020-05-11 09:23:37 +0800 | [diff] [blame] | 824 | @Nullable |
| 825 | final String mImeControlTargetName; |
| 826 | @Nullable |
| 827 | final String mImeTargetNameFromWm; |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 828 | |
Anmol Gupta | 29f209d | 2020-03-11 11:34:46 -0700 | [diff] [blame] | 829 | Entry(ClientState client, EditorInfo editorInfo, String focusedWindowName, |
| 830 | @SoftInputModeFlags int softInputMode, @SoftInputShowHideReason int reason, |
Ming-Shin Lu | d0bc171 | 2020-05-11 09:23:37 +0800 | [diff] [blame] | 831 | boolean inFullscreenMode, String requestWindowName, |
| 832 | @Nullable String imeControlTargetName, @Nullable String imeTargetName) { |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 833 | mClientState = client; |
Anmol Gupta | 29f209d | 2020-03-11 11:34:46 -0700 | [diff] [blame] | 834 | mEditorInfo = editorInfo; |
| 835 | mFocusedWindowName = focusedWindowName; |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 836 | mFocusedWindowSoftInputMode = softInputMode; |
| 837 | mReason = reason; |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 838 | mTimestamp = SystemClock.uptimeMillis(); |
| 839 | mWallTime = System.currentTimeMillis(); |
Anmol Gupta | 29f209d | 2020-03-11 11:34:46 -0700 | [diff] [blame] | 840 | mInFullscreenMode = inFullscreenMode; |
| 841 | mRequestWindowName = requestWindowName; |
Ming-Shin Lu | d0bc171 | 2020-05-11 09:23:37 +0800 | [diff] [blame] | 842 | mImeControlTargetName = imeControlTargetName; |
| 843 | mImeTargetNameFromWm = imeTargetName; |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 844 | } |
| 845 | } |
| 846 | |
| 847 | void addEntry(@NonNull Entry entry) { |
| 848 | final int index = mNextIndex; |
| 849 | mEntries[index] = entry; |
| 850 | mNextIndex = (mNextIndex + 1) % mEntries.length; |
| 851 | } |
| 852 | |
| 853 | void dump(@NonNull PrintWriter pw, @NonNull String prefix) { |
| 854 | final SimpleDateFormat dataFormat = |
| 855 | new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US); |
| 856 | |
| 857 | for (int i = 0; i < mEntries.length; ++i) { |
| 858 | final Entry entry = mEntries[(i + mNextIndex) % mEntries.length]; |
| 859 | if (entry == null) { |
| 860 | continue; |
| 861 | } |
| 862 | pw.print(prefix); |
Ming-Shin Lu | a0605a6 | 2020-04-15 23:49:40 +0800 | [diff] [blame] | 863 | pw.println("SoftInputShowHideHistory #" + entry.mSequenceNumber + ":"); |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 864 | |
| 865 | pw.print(prefix); |
| 866 | pw.println(" time=" + dataFormat.format(new Date(entry.mWallTime)) |
| 867 | + " (timestamp=" + entry.mTimestamp + ")"); |
| 868 | |
| 869 | pw.print(prefix); |
Anmol Gupta | 29f209d | 2020-03-11 11:34:46 -0700 | [diff] [blame] | 870 | pw.print(" reason=" + InputMethodDebug.softInputDisplayReasonToString( |
| 871 | entry.mReason)); |
| 872 | pw.println(" inFullscreenMode=" + entry.mInFullscreenMode); |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 873 | |
| 874 | pw.print(prefix); |
Anmol Gupta | 29f209d | 2020-03-11 11:34:46 -0700 | [diff] [blame] | 875 | pw.println(" requestClient=" + entry.mClientState); |
| 876 | |
| 877 | pw.print(prefix); |
| 878 | pw.println(" focusedWindowName=" + entry.mFocusedWindowName); |
| 879 | |
| 880 | pw.print(prefix); |
| 881 | pw.println(" requestWindowName=" + entry.mRequestWindowName); |
| 882 | |
| 883 | pw.print(prefix); |
Ming-Shin Lu | d0bc171 | 2020-05-11 09:23:37 +0800 | [diff] [blame] | 884 | pw.println(" imeControlTargetName=" + entry.mImeControlTargetName); |
| 885 | |
| 886 | pw.print(prefix); |
| 887 | pw.println(" imeTargetNameFromWm=" + entry.mImeTargetNameFromWm); |
| 888 | |
| 889 | pw.print(prefix); |
Anmol Gupta | 29f209d | 2020-03-11 11:34:46 -0700 | [diff] [blame] | 890 | pw.print(" editorInfo: "); |
| 891 | pw.print(" inputType=" + entry.mEditorInfo.inputType); |
| 892 | pw.print(" privateImeOptions=" + entry.mEditorInfo.privateImeOptions); |
| 893 | pw.println(" fieldId (viewId)=" + entry.mEditorInfo.fieldId); |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 894 | |
| 895 | pw.print(prefix); |
| 896 | pw.println(" focusedWindowSoftInputMode=" + InputMethodDebug.softInputModeToString( |
| 897 | entry.mFocusedWindowSoftInputMode)); |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 902 | /** |
Tarandeep Singh | bb0e2f7 | 2020-01-15 13:58:29 -0800 | [diff] [blame] | 903 | * Map of generated token to windowToken that is requesting |
| 904 | * {@link InputMethodManager#showSoftInput(View, int)}. |
| 905 | * This map tracks origin of showSoftInput requests. |
| 906 | */ |
| 907 | @GuardedBy("mMethodMap") |
| 908 | private final WeakHashMap<IBinder, IBinder> mShowRequestWindowMap = new WeakHashMap<>(); |
| 909 | |
| 910 | /** |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 911 | * Map of generated token to windowToken that is requesting |
| 912 | * {@link InputMethodManager#hideSoftInputFromWindow(IBinder, int)}. |
| 913 | * This map tracks origin of hideSoftInput requests. |
| 914 | */ |
| 915 | @GuardedBy("mMethodMap") |
| 916 | private final WeakHashMap<IBinder, IBinder> mHideRequestWindowMap = new WeakHashMap<>(); |
| 917 | |
| 918 | /** |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 919 | * A ring buffer to store the history of {@link StartInputInfo}. |
| 920 | */ |
| 921 | private static final class StartInputHistory { |
| 922 | /** |
| 923 | * Entry size for non low-RAM devices. |
| 924 | * |
| 925 | * <p>TODO: Consider to follow what other system services have been doing to manage |
| 926 | * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p> |
| 927 | */ |
| 928 | private final static int ENTRY_SIZE_FOR_HIGH_RAM_DEVICE = 16; |
| 929 | |
| 930 | /** |
| 931 | * Entry size for non low-RAM devices. |
| 932 | * |
| 933 | * <p>TODO: Consider to follow what other system services have been doing to manage |
| 934 | * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p> |
| 935 | */ |
| 936 | private final static int ENTRY_SIZE_FOR_LOW_RAM_DEVICE = 5; |
| 937 | |
| 938 | private static int getEntrySize() { |
| 939 | if (ActivityManager.isLowRamDeviceStatic()) { |
| 940 | return ENTRY_SIZE_FOR_LOW_RAM_DEVICE; |
| 941 | } else { |
| 942 | return ENTRY_SIZE_FOR_HIGH_RAM_DEVICE; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | /** |
| 947 | * Backing store for the ring bugger. |
| 948 | */ |
| 949 | private final Entry[] mEntries = new Entry[getEntrySize()]; |
| 950 | |
| 951 | /** |
| 952 | * An index of {@link #mEntries}, to which next {@link #addEntry(StartInputInfo)} should |
| 953 | * write. |
| 954 | */ |
| 955 | private int mNextIndex = 0; |
| 956 | |
| 957 | /** |
| 958 | * Recyclable entry to store the information in {@link StartInputInfo}. |
| 959 | */ |
| 960 | private static final class Entry { |
| 961 | int mSequenceNumber; |
| 962 | long mTimestamp; |
| 963 | long mWallTime; |
Yohei Yukawa | 7979e1a | 2019-02-12 02:01:10 -0800 | [diff] [blame] | 964 | @UserIdInt |
| 965 | int mImeUserId; |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 966 | @NonNull |
| 967 | String mImeTokenString; |
Yohei Yukawa | 557afbb | 2019-02-12 02:01:00 -0800 | [diff] [blame] | 968 | int mImeDisplayId; |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 969 | @NonNull |
| 970 | String mImeId; |
Yohei Yukawa | dc66e52 | 2018-10-21 10:43:14 -0700 | [diff] [blame] | 971 | @StartInputReason |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 972 | int mStartInputReason; |
| 973 | boolean mRestarting; |
Yohei Yukawa | 7979e1a | 2019-02-12 02:01:10 -0800 | [diff] [blame] | 974 | @UserIdInt |
| 975 | int mTargetUserId; |
Yohei Yukawa | 557afbb | 2019-02-12 02:01:00 -0800 | [diff] [blame] | 976 | int mTargetDisplayId; |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 977 | @NonNull |
| 978 | String mTargetWindowString; |
| 979 | @NonNull |
| 980 | EditorInfo mEditorInfo; |
Yohei Yukawa | b0c2645 | 2018-10-21 10:42:52 -0700 | [diff] [blame] | 981 | @SoftInputModeFlags |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 982 | int mTargetWindowSoftInputMode; |
| 983 | int mClientBindSequenceNumber; |
| 984 | |
| 985 | Entry(@NonNull StartInputInfo original) { |
| 986 | set(original); |
| 987 | } |
| 988 | |
| 989 | void set(@NonNull StartInputInfo original) { |
| 990 | mSequenceNumber = original.mSequenceNumber; |
| 991 | mTimestamp = original.mTimestamp; |
| 992 | mWallTime = original.mWallTime; |
Yohei Yukawa | 7979e1a | 2019-02-12 02:01:10 -0800 | [diff] [blame] | 993 | mImeUserId = original.mImeUserId; |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 994 | // Intentionally convert to String so as not to keep a strong reference to a Binder |
| 995 | // object. |
| 996 | mImeTokenString = String.valueOf(original.mImeToken); |
Yohei Yukawa | 557afbb | 2019-02-12 02:01:00 -0800 | [diff] [blame] | 997 | mImeDisplayId = original.mImeDisplayId; |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 998 | mImeId = original.mImeId; |
| 999 | mStartInputReason = original.mStartInputReason; |
| 1000 | mRestarting = original.mRestarting; |
Yohei Yukawa | 7979e1a | 2019-02-12 02:01:10 -0800 | [diff] [blame] | 1001 | mTargetUserId = original.mTargetUserId; |
Yohei Yukawa | 557afbb | 2019-02-12 02:01:00 -0800 | [diff] [blame] | 1002 | mTargetDisplayId = original.mTargetDisplayId; |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 1003 | // Intentionally convert to String so as not to keep a strong reference to a Binder |
| 1004 | // object. |
| 1005 | mTargetWindowString = String.valueOf(original.mTargetWindow); |
| 1006 | mEditorInfo = original.mEditorInfo; |
| 1007 | mTargetWindowSoftInputMode = original.mTargetWindowSoftInputMode; |
| 1008 | mClientBindSequenceNumber = original.mClientBindSequenceNumber; |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | /** |
| 1013 | * Add a new entry and discard the oldest entry as needed. |
| 1014 | * @param info {@lin StartInputInfo} to be added. |
| 1015 | */ |
| 1016 | void addEntry(@NonNull StartInputInfo info) { |
| 1017 | final int index = mNextIndex; |
| 1018 | if (mEntries[index] == null) { |
| 1019 | mEntries[index] = new Entry(info); |
| 1020 | } else { |
| 1021 | mEntries[index].set(info); |
| 1022 | } |
| 1023 | mNextIndex = (mNextIndex + 1) % mEntries.length; |
| 1024 | } |
| 1025 | |
| 1026 | void dump(@NonNull PrintWriter pw, @NonNull String prefix) { |
| 1027 | final SimpleDateFormat dataFormat = |
| 1028 | new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US); |
| 1029 | |
| 1030 | for (int i = 0; i < mEntries.length; ++i) { |
| 1031 | final Entry entry = mEntries[(i + mNextIndex) % mEntries.length]; |
| 1032 | if (entry == null) { |
| 1033 | continue; |
| 1034 | } |
| 1035 | pw.print(prefix); |
| 1036 | pw.println("StartInput #" + entry.mSequenceNumber + ":"); |
| 1037 | |
| 1038 | pw.print(prefix); |
| 1039 | pw.println(" time=" + dataFormat.format(new Date(entry.mWallTime)) |
| 1040 | + " (timestamp=" + entry.mTimestamp + ")" |
| 1041 | + " reason=" |
Yohei Yukawa | a468d70 | 2018-10-21 11:42:34 -0700 | [diff] [blame] | 1042 | + InputMethodDebug.startInputReasonToString(entry.mStartInputReason) |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 1043 | + " restarting=" + entry.mRestarting); |
| 1044 | |
| 1045 | pw.print(prefix); |
Yohei Yukawa | 557afbb | 2019-02-12 02:01:00 -0800 | [diff] [blame] | 1046 | pw.print(" imeToken=" + entry.mImeTokenString + " [" + entry.mImeId + "]"); |
Yohei Yukawa | 7979e1a | 2019-02-12 02:01:10 -0800 | [diff] [blame] | 1047 | pw.print(" imeUserId=" + entry.mImeUserId); |
Yohei Yukawa | 557afbb | 2019-02-12 02:01:00 -0800 | [diff] [blame] | 1048 | pw.println(" imeDisplayId=" + entry.mImeDisplayId); |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 1049 | |
| 1050 | pw.print(prefix); |
| 1051 | pw.println(" targetWin=" + entry.mTargetWindowString |
| 1052 | + " [" + entry.mEditorInfo.packageName + "]" |
Yohei Yukawa | 7979e1a | 2019-02-12 02:01:10 -0800 | [diff] [blame] | 1053 | + " targetUserId=" + entry.mTargetUserId |
Yohei Yukawa | 557afbb | 2019-02-12 02:01:00 -0800 | [diff] [blame] | 1054 | + " targetDisplayId=" + entry.mTargetDisplayId |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 1055 | + " clientBindSeq=" + entry.mClientBindSequenceNumber); |
| 1056 | |
| 1057 | pw.print(prefix); |
Yohei Yukawa | a468d70 | 2018-10-21 11:42:34 -0700 | [diff] [blame] | 1058 | pw.println(" softInputMode=" + InputMethodDebug.softInputModeToString( |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 1059 | entry.mTargetWindowSoftInputMode)); |
| 1060 | |
| 1061 | pw.print(prefix); |
| 1062 | pw.println(" inputType=0x" + Integer.toHexString(entry.mEditorInfo.inputType) |
| 1063 | + " imeOptions=0x" + Integer.toHexString(entry.mEditorInfo.imeOptions) |
| 1064 | + " fieldId=0x" + Integer.toHexString(entry.mEditorInfo.fieldId) |
| 1065 | + " fieldName=" + entry.mEditorInfo.fieldName |
| 1066 | + " actionId=" + entry.mEditorInfo.actionId |
| 1067 | + " actionLabel=" + entry.mEditorInfo.actionLabel); |
| 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | @GuardedBy("mMethodMap") |
| 1073 | @NonNull |
| 1074 | private final StartInputHistory mStartInputHistory = new StartInputHistory(); |
| 1075 | |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 1076 | @GuardedBy("mMethodMap") |
| 1077 | @NonNull |
| 1078 | private final SoftInputShowHideHistory mSoftInputShowHideHistory = |
| 1079 | new SoftInputShowHideHistory(); |
| 1080 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | class SettingsObserver extends ContentObserver { |
Yohei Yukawa | 8148297 | 2015-06-04 00:58:59 -0700 | [diff] [blame] | 1082 | int mUserId; |
| 1083 | boolean mRegistered = false; |
Yohei Yukawa | 7b574cb | 2016-03-16 17:22:22 -0700 | [diff] [blame] | 1084 | @NonNull |
Dianne Hackborn | fd7aded | 2013-01-22 17:10:23 -0800 | [diff] [blame] | 1085 | String mLastEnabled = ""; |
| 1086 | |
Yohei Yukawa | 8148297 | 2015-06-04 00:58:59 -0700 | [diff] [blame] | 1087 | /** |
| 1088 | * <em>This constructor must be called within the lock.</em> |
| 1089 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1090 | SettingsObserver(Handler handler) { |
| 1091 | super(handler); |
Yohei Yukawa | 8148297 | 2015-06-04 00:58:59 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
Yohei Yukawa | 7b18aec | 2016-03-07 13:04:32 -0800 | [diff] [blame] | 1094 | public void registerContentObserverLocked(@UserIdInt int userId) { |
Yohei Yukawa | 8148297 | 2015-06-04 00:58:59 -0700 | [diff] [blame] | 1095 | if (mRegistered && mUserId == userId) { |
| 1096 | return; |
| 1097 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1098 | ContentResolver resolver = mContext.getContentResolver(); |
Yohei Yukawa | 8148297 | 2015-06-04 00:58:59 -0700 | [diff] [blame] | 1099 | if (mRegistered) { |
| 1100 | mContext.getContentResolver().unregisterContentObserver(this); |
| 1101 | mRegistered = false; |
| 1102 | } |
| 1103 | if (mUserId != userId) { |
| 1104 | mLastEnabled = ""; |
| 1105 | mUserId = userId; |
| 1106 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1107 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
Yohei Yukawa | 8148297 | 2015-06-04 00:58:59 -0700 | [diff] [blame] | 1108 | Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1109 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
Yohei Yukawa | 8148297 | 2015-06-04 00:58:59 -0700 | [diff] [blame] | 1110 | Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId); |
satok | b6109bb | 2011-02-03 22:24:54 +0900 | [diff] [blame] | 1111 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
Yohei Yukawa | 8148297 | 2015-06-04 00:58:59 -0700 | [diff] [blame] | 1112 | Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId); |
Michael Wright | 7b5a96b | 2014-08-09 19:28:42 -0700 | [diff] [blame] | 1113 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
Yohei Yukawa | 8148297 | 2015-06-04 00:58:59 -0700 | [diff] [blame] | 1114 | Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId); |
Anna Galusza | 9b27811 | 2016-01-04 11:37:37 -0800 | [diff] [blame] | 1115 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 1116 | Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId); |
Yohei Yukawa | 8148297 | 2015-06-04 00:58:59 -0700 | [diff] [blame] | 1117 | mRegistered = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1118 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1119 | |
Michael Wright | 7b5a96b | 2014-08-09 19:28:42 -0700 | [diff] [blame] | 1120 | @Override public void onChange(boolean selfChange, Uri uri) { |
Anna Galusza | 9b27811 | 2016-01-04 11:37:37 -0800 | [diff] [blame] | 1121 | final Uri showImeUri = Settings.Secure.getUriFor( |
| 1122 | Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD); |
| 1123 | final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor( |
| 1124 | Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1125 | synchronized (mMethodMap) { |
Michael Wright | 7b5a96b | 2014-08-09 19:28:42 -0700 | [diff] [blame] | 1126 | if (showImeUri.equals(uri)) { |
| 1127 | updateKeyboardFromSettingsLocked(); |
Anna Galusza | 9b27811 | 2016-01-04 11:37:37 -0800 | [diff] [blame] | 1128 | } else if (accessibilityRequestingNoImeUri.equals(uri)) { |
Phil Weaver | 03a65b0 | 2018-07-19 16:07:57 -0700 | [diff] [blame] | 1129 | final int accessibilitySoftKeyboardSetting = Settings.Secure.getIntForUser( |
Anna Galusza | 9b27811 | 2016-01-04 11:37:37 -0800 | [diff] [blame] | 1130 | mContext.getContentResolver(), |
Phil Weaver | 03a65b0 | 2018-07-19 16:07:57 -0700 | [diff] [blame] | 1131 | Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE, 0, mUserId); |
| 1132 | mAccessibilityRequestingNoSoftKeyboard = |
| 1133 | (accessibilitySoftKeyboardSetting & AccessibilityService.SHOW_MODE_MASK) |
| 1134 | == AccessibilityService.SHOW_MODE_HIDDEN; |
Anna Galusza | 9b27811 | 2016-01-04 11:37:37 -0800 | [diff] [blame] | 1135 | if (mAccessibilityRequestingNoSoftKeyboard) { |
| 1136 | final boolean showRequested = mShowRequested; |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 1137 | hideCurrentInputLocked(mCurFocusedWindow, 0, null, |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 1138 | SoftInputShowHideReason.HIDE_SETTINGS_ON_CHANGE); |
Anna Galusza | 9b27811 | 2016-01-04 11:37:37 -0800 | [diff] [blame] | 1139 | mShowRequested = showRequested; |
| 1140 | } else if (mShowRequested) { |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 1141 | showCurrentInputLocked(mCurFocusedWindow, |
| 1142 | InputMethodManager.SHOW_IMPLICIT, null, |
| 1143 | SoftInputShowHideReason.SHOW_SETTINGS_ON_CHANGE); |
Anna Galusza | 9b27811 | 2016-01-04 11:37:37 -0800 | [diff] [blame] | 1144 | } |
Michael Wright | 7b5a96b | 2014-08-09 19:28:42 -0700 | [diff] [blame] | 1145 | } else { |
| 1146 | boolean enabledChanged = false; |
| 1147 | String newEnabled = mSettings.getEnabledInputMethodsStr(); |
| 1148 | if (!mLastEnabled.equals(newEnabled)) { |
| 1149 | mLastEnabled = newEnabled; |
| 1150 | enabledChanged = true; |
| 1151 | } |
| 1152 | updateInputMethodsFromSettingsLocked(enabledChanged); |
Dianne Hackborn | fd7aded | 2013-01-22 17:10:23 -0800 | [diff] [blame] | 1153 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1154 | } |
| 1155 | } |
Yohei Yukawa | 8148297 | 2015-06-04 00:58:59 -0700 | [diff] [blame] | 1156 | |
| 1157 | @Override |
| 1158 | public String toString() { |
| 1159 | return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered |
| 1160 | + " mLastEnabled=" + mLastEnabled + "}"; |
| 1161 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1162 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1163 | |
Yohei Yukawa | 0ec0800 | 2019-03-08 10:48:26 -0800 | [diff] [blame] | 1164 | /** |
| 1165 | * {@link BroadcastReceiver} that is intended to listen to broadcasts sent to the system user |
| 1166 | * only. |
| 1167 | */ |
| 1168 | private final class ImmsBroadcastReceiverForSystemUser extends BroadcastReceiver { |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1169 | @Override |
| 1170 | public void onReceive(Context context, Intent intent) { |
| 1171 | final String action = intent.getAction(); |
Yohei Yukawa | 4225c7d | 2019-03-08 00:06:11 -0800 | [diff] [blame] | 1172 | if (Intent.ACTION_USER_ADDED.equals(action) |
Amith Yamasani | 734983f | 2014-03-04 16:48:05 -0800 | [diff] [blame] | 1173 | || Intent.ACTION_USER_REMOVED.equals(action)) { |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1174 | updateCurrentProfileIds(); |
Amith Yamasani | 734983f | 2014-03-04 16:48:05 -0800 | [diff] [blame] | 1175 | return; |
Yohei Yukawa | 7924782 | 2017-01-23 15:26:15 -0800 | [diff] [blame] | 1176 | } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) { |
Yohei Yukawa | 0d7aff8 | 2017-02-10 00:40:51 -0800 | [diff] [blame] | 1177 | onActionLocaleChanged(); |
Tadashi G. Takaoka | 01065a5 | 2017-07-19 14:10:24 +0900 | [diff] [blame] | 1178 | } else if (ACTION_SHOW_INPUT_METHOD_PICKER.equals(action)) { |
| 1179 | // ACTION_SHOW_INPUT_METHOD_PICKER action is a protected-broadcast and it is |
| 1180 | // guaranteed to be send only from the system, so that there is no need for extra |
| 1181 | // security check such as |
| 1182 | // {@link #canShowInputMethodPickerLocked(IInputMethodClient)}. |
| 1183 | mHandler.obtainMessage( |
| 1184 | MSG_SHOW_IM_SUBTYPE_PICKER, |
lumark | 0b05f9e | 2018-11-26 15:09:06 +0800 | [diff] [blame] | 1185 | // TODO(b/120076400): Design and implement IME switcher for heterogeneous |
| 1186 | // navbar configuration. |
Tadashi G. Takaoka | 01065a5 | 2017-07-19 14:10:24 +0900 | [diff] [blame] | 1187 | InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES, |
lumark | 0b05f9e | 2018-11-26 15:09:06 +0800 | [diff] [blame] | 1188 | DEFAULT_DISPLAY).sendToTarget(); |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1189 | } else { |
| 1190 | Slog.w(TAG, "Unexpected intent " + intent); |
| 1191 | } |
| 1192 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1193 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1194 | |
Yohei Yukawa | 0d7aff8 | 2017-02-10 00:40:51 -0800 | [diff] [blame] | 1195 | /** |
Yohei Yukawa | 0ec0800 | 2019-03-08 10:48:26 -0800 | [diff] [blame] | 1196 | * {@link BroadcastReceiver} that is intended to listen to broadcasts sent to all the users. |
| 1197 | */ |
| 1198 | private final class ImmsBroadcastReceiverForAllUsers extends BroadcastReceiver { |
| 1199 | @Override |
| 1200 | public void onReceive(Context context, Intent intent) { |
| 1201 | final String action = intent.getAction(); |
| 1202 | if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) { |
| 1203 | final PendingResult pendingResult = getPendingResult(); |
| 1204 | if (pendingResult == null) { |
| 1205 | return; |
| 1206 | } |
| 1207 | // sender userId can be a real user ID or USER_ALL. |
| 1208 | final int senderUserId = pendingResult.getSendingUserId(); |
| 1209 | if (senderUserId != UserHandle.USER_ALL) { |
Tarandeep Singh | cf5ff82 | 2019-07-08 14:06:27 -0700 | [diff] [blame] | 1210 | if (senderUserId != mSettings.getCurrentUserId()) { |
Yohei Yukawa | 0ec0800 | 2019-03-08 10:48:26 -0800 | [diff] [blame] | 1211 | // A background user is trying to hide the dialog. Ignore. |
| 1212 | return; |
| 1213 | } |
| 1214 | } |
| 1215 | hideInputMethodMenu(); |
| 1216 | } else { |
| 1217 | Slog.w(TAG, "Unexpected intent " + intent); |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | /** |
Yohei Yukawa | 0d7aff8 | 2017-02-10 00:40:51 -0800 | [diff] [blame] | 1223 | * Handles {@link Intent#ACTION_LOCALE_CHANGED}. |
| 1224 | * |
| 1225 | * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all |
| 1226 | * the users. We should ignore this event if this is about any background user's locale.</p> |
| 1227 | * |
| 1228 | * <p>Caution: This method must not be called when system is not ready.</p> |
| 1229 | */ |
| 1230 | void onActionLocaleChanged() { |
| 1231 | synchronized (mMethodMap) { |
| 1232 | final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales(); |
| 1233 | if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) { |
| 1234 | return; |
| 1235 | } |
| 1236 | buildInputMethodListLocked(true); |
| 1237 | // If the locale is changed, needs to reset the default ime |
| 1238 | resetDefaultImeLocked(mContext); |
| 1239 | updateFromSettingsLocked(true); |
| 1240 | mLastSystemLocales = possibleNewLocale; |
| 1241 | } |
| 1242 | } |
| 1243 | |
Yohei Yukawa | c4e4491 | 2017-02-09 19:30:22 -0800 | [diff] [blame] | 1244 | final class MyPackageMonitor extends PackageMonitor { |
| 1245 | /** |
Yohei Yukawa | 5e3e8a5 | 2017-02-14 20:05:17 -0800 | [diff] [blame] | 1246 | * Package names that are known to contain {@link InputMethodService}. |
Yohei Yukawa | c4e4491 | 2017-02-09 19:30:22 -0800 | [diff] [blame] | 1247 | * |
| 1248 | * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan |
| 1249 | * all the packages when the user is unlocked, and direct-boot awareness will not be changed |
| 1250 | * dynamically unless the entire package is updated, which also always triggers package |
| 1251 | * rescanning.</p> |
| 1252 | */ |
| 1253 | @GuardedBy("mMethodMap") |
Yohei Yukawa | 5e3e8a5 | 2017-02-14 20:05:17 -0800 | [diff] [blame] | 1254 | final private ArraySet<String> mKnownImePackageNames = new ArraySet<>(); |
| 1255 | |
| 1256 | /** |
| 1257 | * Packages that are appeared, disappeared, or modified for whatever reason. |
| 1258 | * |
| 1259 | * <p>Note: For now we intentionally use {@link ArrayList} instead of {@link ArraySet} |
| 1260 | * because 1) the number of elements is almost always 1 or so, and 2) we do not care |
| 1261 | * duplicate elements for our use case.</p> |
| 1262 | * |
| 1263 | * <p>This object must be accessed only from callback methods in {@link PackageMonitor}, |
| 1264 | * which should be bound to {@link #getRegisteredHandler()}.</p> |
| 1265 | */ |
| 1266 | private final ArrayList<String> mChangedPackages = new ArrayList<>(); |
| 1267 | |
| 1268 | /** |
| 1269 | * {@code true} if one or more packages that contain {@link InputMethodService} appeared. |
| 1270 | * |
| 1271 | * <p>This field must be accessed only from callback methods in {@link PackageMonitor}, |
| 1272 | * which should be bound to {@link #getRegisteredHandler()}.</p> |
| 1273 | */ |
| 1274 | private boolean mImePackageAppeared = false; |
Yohei Yukawa | c4e4491 | 2017-02-09 19:30:22 -0800 | [diff] [blame] | 1275 | |
| 1276 | @GuardedBy("mMethodMap") |
Yohei Yukawa | 5e3e8a5 | 2017-02-14 20:05:17 -0800 | [diff] [blame] | 1277 | void clearKnownImePackageNamesLocked() { |
| 1278 | mKnownImePackageNames.clear(); |
Yohei Yukawa | c4e4491 | 2017-02-09 19:30:22 -0800 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | @GuardedBy("mMethodMap") |
Yohei Yukawa | 5e3e8a5 | 2017-02-14 20:05:17 -0800 | [diff] [blame] | 1282 | final void addKnownImePackageNameLocked(@NonNull String packageName) { |
| 1283 | mKnownImePackageNames.add(packageName); |
Yohei Yukawa | c4e4491 | 2017-02-09 19:30:22 -0800 | [diff] [blame] | 1284 | } |
| 1285 | |
Yohei Yukawa | 278f2ab | 2017-02-14 19:51:33 -0800 | [diff] [blame] | 1286 | @GuardedBy("mMethodMap") |
| 1287 | private boolean isChangingPackagesOfCurrentUserLocked() { |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1288 | final int userId = getChangingUserId(); |
| 1289 | final boolean retval = userId == mSettings.getCurrentUserId(); |
| 1290 | if (DEBUG) { |
satok | 81f8b7c | 2012-12-04 20:42:56 +0900 | [diff] [blame] | 1291 | if (!retval) { |
| 1292 | Slog.d(TAG, "--- ignore this call back from a background user: " + userId); |
| 1293 | } |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1294 | } |
| 1295 | return retval; |
| 1296 | } |
| 1297 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1298 | @Override |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1299 | public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1300 | synchronized (mMethodMap) { |
Yohei Yukawa | 278f2ab | 2017-02-14 19:51:33 -0800 | [diff] [blame] | 1301 | if (!isChangingPackagesOfCurrentUserLocked()) { |
| 1302 | return false; |
| 1303 | } |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1304 | String curInputMethodId = mSettings.getSelectedInputMethod(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1305 | final int N = mMethodList.size(); |
| 1306 | if (curInputMethodId != null) { |
| 1307 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1308 | InputMethodInfo imi = mMethodList.get(i); |
| 1309 | if (imi.getId().equals(curInputMethodId)) { |
| 1310 | for (String pkg : packages) { |
| 1311 | if (imi.getPackageName().equals(pkg)) { |
| 1312 | if (!doit) { |
| 1313 | return true; |
| 1314 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 1315 | resetSelectedInputMethodAndSubtypeLocked(""); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1316 | chooseNewDefaultIMELocked(); |
| 1317 | return true; |
| 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | } |
| 1322 | } |
| 1323 | } |
| 1324 | return false; |
| 1325 | } |
| 1326 | |
| 1327 | @Override |
Yohei Yukawa | 5e3e8a5 | 2017-02-14 20:05:17 -0800 | [diff] [blame] | 1328 | public void onBeginPackageChanges() { |
| 1329 | clearPackageChangeState(); |
| 1330 | } |
| 1331 | |
| 1332 | @Override |
| 1333 | public void onPackageAppeared(String packageName, int reason) { |
| 1334 | if (!mImePackageAppeared) { |
| 1335 | final PackageManager pm = mContext.getPackageManager(); |
| 1336 | final List<ResolveInfo> services = pm.queryIntentServicesAsUser( |
| 1337 | new Intent(InputMethod.SERVICE_INTERFACE).setPackage(packageName), |
Yohei Yukawa | 5ec1fce | 2018-12-17 07:56:14 -0800 | [diff] [blame] | 1338 | PackageManager.MATCH_DISABLED_COMPONENTS, getChangingUserId()); |
Yohei Yukawa | 5e3e8a5 | 2017-02-14 20:05:17 -0800 | [diff] [blame] | 1339 | // No need to lock this because we access it only on getRegisteredHandler(). |
| 1340 | if (!services.isEmpty()) { |
| 1341 | mImePackageAppeared = true; |
| 1342 | } |
| 1343 | } |
| 1344 | // No need to lock this because we access it only on getRegisteredHandler(). |
| 1345 | mChangedPackages.add(packageName); |
| 1346 | } |
| 1347 | |
| 1348 | @Override |
| 1349 | public void onPackageDisappeared(String packageName, int reason) { |
| 1350 | // No need to lock this because we access it only on getRegisteredHandler(). |
| 1351 | mChangedPackages.add(packageName); |
| 1352 | } |
| 1353 | |
| 1354 | @Override |
| 1355 | public void onPackageModified(String packageName) { |
| 1356 | // No need to lock this because we access it only on getRegisteredHandler(). |
| 1357 | mChangedPackages.add(packageName); |
| 1358 | } |
| 1359 | |
| 1360 | @Override |
| 1361 | public void onPackagesSuspended(String[] packages) { |
| 1362 | // No need to lock this because we access it only on getRegisteredHandler(). |
| 1363 | for (String packageName : packages) { |
| 1364 | mChangedPackages.add(packageName); |
Yohei Yukawa | c4e4491 | 2017-02-09 19:30:22 -0800 | [diff] [blame] | 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | @Override |
Yohei Yukawa | 5e3e8a5 | 2017-02-14 20:05:17 -0800 | [diff] [blame] | 1369 | public void onPackagesUnsuspended(String[] packages) { |
| 1370 | // No need to lock this because we access it only on getRegisteredHandler(). |
| 1371 | for (String packageName : packages) { |
| 1372 | mChangedPackages.add(packageName); |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | @Override |
| 1377 | public void onFinishPackageChanges() { |
| 1378 | onFinishPackageChangesInternal(); |
| 1379 | clearPackageChangeState(); |
| 1380 | } |
| 1381 | |
| 1382 | private void clearPackageChangeState() { |
| 1383 | // No need to lock them because we access these fields only on getRegisteredHandler(). |
| 1384 | mChangedPackages.clear(); |
| 1385 | mImePackageAppeared = false; |
| 1386 | } |
| 1387 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1388 | @GuardedBy("mMethodMap") |
Yohei Yukawa | 5e3e8a5 | 2017-02-14 20:05:17 -0800 | [diff] [blame] | 1389 | private boolean shouldRebuildInputMethodListLocked() { |
| 1390 | // This method is guaranteed to be called only by getRegisteredHandler(). |
| 1391 | |
| 1392 | // If there is any new package that contains at least one IME, then rebuilt the list |
| 1393 | // of IMEs. |
| 1394 | if (mImePackageAppeared) { |
| 1395 | return true; |
| 1396 | } |
| 1397 | |
| 1398 | // Otherwise, check if mKnownImePackageNames and mChangedPackages have any intersection. |
| 1399 | // TODO: Consider to create a utility method to do the following test. List.retainAll() |
| 1400 | // is an option, but it may still do some extra operations that we do not need here. |
| 1401 | final int N = mChangedPackages.size(); |
| 1402 | for (int i = 0; i < N; ++i) { |
| 1403 | final String packageName = mChangedPackages.get(i); |
| 1404 | if (mKnownImePackageNames.contains(packageName)) { |
| 1405 | return true; |
| 1406 | } |
| 1407 | } |
| 1408 | return false; |
| 1409 | } |
| 1410 | |
| 1411 | private void onFinishPackageChangesInternal() { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1412 | synchronized (mMethodMap) { |
Yohei Yukawa | 278f2ab | 2017-02-14 19:51:33 -0800 | [diff] [blame] | 1413 | if (!isChangingPackagesOfCurrentUserLocked()) { |
| 1414 | return; |
| 1415 | } |
Yohei Yukawa | 5e3e8a5 | 2017-02-14 20:05:17 -0800 | [diff] [blame] | 1416 | if (!shouldRebuildInputMethodListLocked()) { |
| 1417 | return; |
| 1418 | } |
| 1419 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1420 | InputMethodInfo curIm = null; |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1421 | String curInputMethodId = mSettings.getSelectedInputMethod(); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1422 | final int N = mMethodList.size(); |
| 1423 | if (curInputMethodId != null) { |
| 1424 | for (int i=0; i<N; i++) { |
| 1425 | InputMethodInfo imi = mMethodList.get(i); |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1426 | final String imiId = imi.getId(); |
| 1427 | if (imiId.equals(curInputMethodId)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1428 | curIm = imi; |
| 1429 | } |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1430 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1431 | int change = isPackageDisappearing(imi.getPackageName()); |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1432 | if (isPackageModified(imi.getPackageName())) { |
Yohei Yukawa | b557d57 | 2018-12-29 21:26:26 -0800 | [diff] [blame] | 1433 | mAdditionalSubtypeMap.remove(imi.getId()); |
| 1434 | AdditionalSubtypeUtils.save(mAdditionalSubtypeMap, mMethodMap, |
| 1435 | mSettings.getCurrentUserId()); |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1436 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1437 | if (change == PACKAGE_TEMPORARY_CHANGE |
| 1438 | || change == PACKAGE_PERMANENT_CHANGE) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1439 | Slog.i(TAG, "Input method uninstalled, disabling: " |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1440 | + imi.getComponent()); |
| 1441 | setInputMethodEnabledLocked(imi.getId(), false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1442 | } |
| 1443 | } |
| 1444 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1445 | |
Yohei Yukawa | 94e3330 | 2016-02-12 19:37:03 -0800 | [diff] [blame] | 1446 | buildInputMethodListLocked(false /* resetDefaultEnabledIme */); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1447 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1448 | boolean changed = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1449 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1450 | if (curIm != null) { |
Anna Galusza | 9b27811 | 2016-01-04 11:37:37 -0800 | [diff] [blame] | 1451 | int change = isPackageDisappearing(curIm.getPackageName()); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1452 | if (change == PACKAGE_TEMPORARY_CHANGE |
| 1453 | || change == PACKAGE_PERMANENT_CHANGE) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1454 | ServiceInfo si = null; |
| 1455 | try { |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1456 | si = mIPackageManager.getServiceInfo( |
| 1457 | curIm.getComponent(), 0, mSettings.getCurrentUserId()); |
| 1458 | } catch (RemoteException ex) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1459 | } |
| 1460 | if (si == null) { |
| 1461 | // Uh oh, current input method is no longer around! |
| 1462 | // Pick another one... |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1463 | Slog.i(TAG, "Current input method removed: " + curInputMethodId); |
Yohei Yukawa | 849443c | 2019-01-21 09:02:25 -0800 | [diff] [blame] | 1464 | updateSystemUiLocked(0 /* vis */, mBackDisposition); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1465 | if (!chooseNewDefaultIMELocked()) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1466 | changed = true; |
| 1467 | curIm = null; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1468 | Slog.i(TAG, "Unsetting current input method"); |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 1469 | resetSelectedInputMethodAndSubtypeLocked(""); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1470 | } |
| 1471 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1472 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1473 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1474 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1475 | if (curIm == null) { |
| 1476 | // We currently don't have a default input method... is |
| 1477 | // one now available? |
| 1478 | changed = chooseNewDefaultIMELocked(); |
Yohei Yukawa | 54d512c | 2015-05-19 22:15:02 -0700 | [diff] [blame] | 1479 | } else if (!changed && isPackageModified(curIm.getPackageName())) { |
| 1480 | // Even if the current input method is still available, mCurrentSubtype could |
| 1481 | // be obsolete when the package is modified in practice. |
| 1482 | changed = true; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1483 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1484 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1485 | if (changed) { |
Dianne Hackborn | fd7aded | 2013-01-22 17:10:23 -0800 | [diff] [blame] | 1486 | updateFromSettingsLocked(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1487 | } |
| 1488 | } |
| 1489 | } |
| 1490 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1491 | |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 1492 | private static final class MethodCallback extends IInputSessionCallback.Stub { |
Jean Chalard | e0d32a6 | 2011-10-20 20:36:07 +0900 | [diff] [blame] | 1493 | private final InputMethodManagerService mParentIMMS; |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 1494 | private final IInputMethod mMethod; |
| 1495 | private final InputChannel mChannel; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1496 | |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 1497 | MethodCallback(InputMethodManagerService imms, IInputMethod method, |
| 1498 | InputChannel channel) { |
Jean Chalard | e0d32a6 | 2011-10-20 20:36:07 +0900 | [diff] [blame] | 1499 | mParentIMMS = imms; |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 1500 | mMethod = method; |
| 1501 | mChannel = channel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1502 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1503 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1504 | @Override |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 1505 | public void sessionCreated(IInputMethodSession session) { |
Dianne Hackborn | 6b6b3fd | 2014-03-24 11:27:18 -0700 | [diff] [blame] | 1506 | long ident = Binder.clearCallingIdentity(); |
| 1507 | try { |
| 1508 | mParentIMMS.onSessionCreated(mMethod, session, mChannel); |
| 1509 | } finally { |
| 1510 | Binder.restoreCallingIdentity(ident); |
| 1511 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1512 | } |
| 1513 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1514 | |
satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 1515 | private class HardKeyboardListener |
Seigo Nonaka | 7309b12 | 2015-08-17 18:34:13 -0700 | [diff] [blame] | 1516 | implements WindowManagerInternal.OnHardKeyboardStatusChangeListener { |
satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 1517 | @Override |
Michael Wright | 7b5a96b | 2014-08-09 19:28:42 -0700 | [diff] [blame] | 1518 | public void onHardKeyboardStatusChange(boolean available) { |
| 1519 | mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED, |
| 1520 | available ? 1 : 0)); |
satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 1521 | } |
| 1522 | |
Michael Wright | 7b5a96b | 2014-08-09 19:28:42 -0700 | [diff] [blame] | 1523 | public void handleHardKeyboardStatusChange(boolean available) { |
satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 1524 | if (DEBUG) { |
Michael Wright | 7b5a96b | 2014-08-09 19:28:42 -0700 | [diff] [blame] | 1525 | Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available); |
satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 1526 | } |
| 1527 | synchronized(mMethodMap) { |
| 1528 | if (mSwitchingDialog != null && mSwitchingDialogTitleView != null |
| 1529 | && mSwitchingDialog.isShowing()) { |
| 1530 | mSwitchingDialogTitleView.findViewById( |
| 1531 | com.android.internal.R.id.hard_keyboard_section).setVisibility( |
| 1532 | available ? View.VISIBLE : View.GONE); |
| 1533 | } |
| 1534 | } |
| 1535 | } |
| 1536 | } |
| 1537 | |
Yohei Yukawa | d277d69 | 2020-02-19 17:12:17 -0800 | [diff] [blame] | 1538 | private static final class UserSwitchHandlerTask implements Runnable { |
| 1539 | final InputMethodManagerService mService; |
| 1540 | |
| 1541 | @UserIdInt |
| 1542 | final int mToUserId; |
| 1543 | |
| 1544 | @Nullable |
| 1545 | IInputMethodClient mClientToBeReset; |
| 1546 | |
| 1547 | UserSwitchHandlerTask(InputMethodManagerService service, @UserIdInt int toUserId, |
| 1548 | @Nullable IInputMethodClient clientToBeReset) { |
| 1549 | mService = service; |
| 1550 | mToUserId = toUserId; |
| 1551 | mClientToBeReset = clientToBeReset; |
| 1552 | } |
| 1553 | |
| 1554 | @Override |
| 1555 | public void run() { |
| 1556 | synchronized (mService.mMethodMap) { |
| 1557 | if (mService.mUserSwitchHandlerTask != this) { |
| 1558 | // This task was already canceled before it is handled here. So do nothing. |
| 1559 | return; |
| 1560 | } |
| 1561 | mService.switchUserOnHandlerLocked(mService.mUserSwitchHandlerTask.mToUserId, |
| 1562 | mClientToBeReset); |
| 1563 | mService.mUserSwitchHandlerTask = null; |
| 1564 | } |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | /** |
| 1569 | * When non-{@code null}, this represents pending user-switch task, which is to be executed as |
| 1570 | * a handler callback. This needs to be set and unset only within the lock. |
| 1571 | */ |
| 1572 | @Nullable |
| 1573 | @GuardedBy("mMethodMap") |
| 1574 | private UserSwitchHandlerTask mUserSwitchHandlerTask; |
| 1575 | |
Fyodor Kupolov | 1e33dc8 | 2016-01-21 12:02:58 -0800 | [diff] [blame] | 1576 | public static final class Lifecycle extends SystemService { |
| 1577 | private InputMethodManagerService mService; |
| 1578 | |
| 1579 | public Lifecycle(Context context) { |
| 1580 | super(context); |
| 1581 | mService = new InputMethodManagerService(context); |
| 1582 | } |
| 1583 | |
| 1584 | @Override |
| 1585 | public void onStart() { |
| 1586 | LocalServices.addService(InputMethodManagerInternal.class, |
Yohei Yukawa | fffc0e5 | 2018-09-04 13:24:00 -0700 | [diff] [blame] | 1587 | new LocalServiceImpl(mService)); |
Fyodor Kupolov | 1e33dc8 | 2016-01-21 12:02:58 -0800 | [diff] [blame] | 1588 | publishBinderService(Context.INPUT_METHOD_SERVICE, mService); |
| 1589 | } |
| 1590 | |
| 1591 | @Override |
Yohei Yukawa | 7b18aec | 2016-03-07 13:04:32 -0800 | [diff] [blame] | 1592 | public void onSwitchUser(@UserIdInt int userHandle) { |
Fyodor Kupolov | 7877b8a | 2016-06-29 14:39:19 -0700 | [diff] [blame] | 1593 | // Called on ActivityManager thread. |
Yohei Yukawa | d277d69 | 2020-02-19 17:12:17 -0800 | [diff] [blame] | 1594 | synchronized (mService.mMethodMap) { |
| 1595 | mService.scheduleSwitchUserTaskLocked(userHandle, null /* clientToBeReset */); |
| 1596 | } |
Yohei Yukawa | 7b18aec | 2016-03-07 13:04:32 -0800 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | @Override |
Fyodor Kupolov | 1e33dc8 | 2016-01-21 12:02:58 -0800 | [diff] [blame] | 1600 | public void onBootPhase(int phase) { |
Fyodor Kupolov | 7877b8a | 2016-06-29 14:39:19 -0700 | [diff] [blame] | 1601 | // Called on ActivityManager thread. |
Yohei Yukawa | 7b18aec | 2016-03-07 13:04:32 -0800 | [diff] [blame] | 1602 | // TODO: Dispatch this to a worker thread as needed. |
Fyodor Kupolov | 1e33dc8 | 2016-01-21 12:02:58 -0800 | [diff] [blame] | 1603 | if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) { |
| 1604 | StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager |
| 1605 | .getService(Context.STATUS_BAR_SERVICE); |
| 1606 | mService.systemRunning(statusBarService); |
| 1607 | } |
| 1608 | } |
Yohei Yukawa | ed4952a | 2016-02-17 07:57:25 -0800 | [diff] [blame] | 1609 | |
| 1610 | @Override |
Fyodor Kupolov | 7877b8a | 2016-06-29 14:39:19 -0700 | [diff] [blame] | 1611 | public void onUnlockUser(final @UserIdInt int userHandle) { |
| 1612 | // Called on ActivityManager thread. |
| 1613 | mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER, |
Fyodor Kupolov | 0f57cce | 2016-09-09 10:36:30 -0700 | [diff] [blame] | 1614 | userHandle /* arg1 */, 0 /* arg2 */)); |
Yohei Yukawa | ed4952a | 2016-02-17 07:57:25 -0800 | [diff] [blame] | 1615 | } |
| 1616 | } |
| 1617 | |
Yohei Yukawa | 7b18aec | 2016-03-07 13:04:32 -0800 | [diff] [blame] | 1618 | void onUnlockUser(@UserIdInt int userId) { |
Yohei Yukawa | ed4952a | 2016-02-17 07:57:25 -0800 | [diff] [blame] | 1619 | synchronized(mMethodMap) { |
| 1620 | final int currentUserId = mSettings.getCurrentUserId(); |
| 1621 | if (DEBUG) { |
| 1622 | Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId); |
| 1623 | } |
| 1624 | if (userId != currentUserId) { |
| 1625 | return; |
| 1626 | } |
| 1627 | mSettings.switchCurrentUser(currentUserId, !mSystemReady); |
Yohei Yukawa | 7924782 | 2017-01-23 15:26:15 -0800 | [diff] [blame] | 1628 | if (mSystemReady) { |
| 1629 | // We need to rebuild IMEs. |
| 1630 | buildInputMethodListLocked(false /* resetDefaultEnabledIme */); |
| 1631 | updateInputMethodsFromSettingsLocked(true /* enabledChanged */); |
| 1632 | } |
Yohei Yukawa | ed4952a | 2016-02-17 07:57:25 -0800 | [diff] [blame] | 1633 | } |
Fyodor Kupolov | 1e33dc8 | 2016-01-21 12:02:58 -0800 | [diff] [blame] | 1634 | } |
| 1635 | |
Yohei Yukawa | d277d69 | 2020-02-19 17:12:17 -0800 | [diff] [blame] | 1636 | @GuardedBy("mMethodMap") |
| 1637 | void scheduleSwitchUserTaskLocked(@UserIdInt int userId, |
| 1638 | @Nullable IInputMethodClient clientToBeReset) { |
| 1639 | if (mUserSwitchHandlerTask != null) { |
| 1640 | if (mUserSwitchHandlerTask.mToUserId == userId) { |
| 1641 | mUserSwitchHandlerTask.mClientToBeReset = clientToBeReset; |
| 1642 | return; |
| 1643 | } |
| 1644 | mHandler.removeCallbacks(mUserSwitchHandlerTask); |
Yohei Yukawa | 7b18aec | 2016-03-07 13:04:32 -0800 | [diff] [blame] | 1645 | } |
Yohei Yukawa | d277d69 | 2020-02-19 17:12:17 -0800 | [diff] [blame] | 1646 | final UserSwitchHandlerTask task = new UserSwitchHandlerTask(this, userId, |
| 1647 | clientToBeReset); |
| 1648 | mUserSwitchHandlerTask = task; |
| 1649 | mHandler.post(task); |
Yohei Yukawa | 7b18aec | 2016-03-07 13:04:32 -0800 | [diff] [blame] | 1650 | } |
| 1651 | |
Seigo Nonaka | 7309b12 | 2015-08-17 18:34:13 -0700 | [diff] [blame] | 1652 | public InputMethodManagerService(Context context) { |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1653 | mIPackageManager = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1654 | mContext = context; |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1655 | mRes = context.getResources(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1656 | mHandler = new Handler(this); |
Yohei Yukawa | 8148297 | 2015-06-04 00:58:59 -0700 | [diff] [blame] | 1657 | // Note: SettingsObserver doesn't register observers in its constructor. |
| 1658 | mSettingsObserver = new SettingsObserver(mHandler); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1659 | mIWindowManager = IWindowManager.Stub.asInterface( |
| 1660 | ServiceManager.getService(Context.WINDOW_SERVICE)); |
Seigo Nonaka | 7309b12 | 2015-08-17 18:34:13 -0700 | [diff] [blame] | 1661 | mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class); |
Yohei Yukawa | d5f402b | 2020-05-15 10:23:32 -0700 | [diff] [blame] | 1662 | mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class); |
lpeter | 133fce0 | 2020-03-05 20:32:16 +0800 | [diff] [blame] | 1663 | mInputManagerInternal = LocalServices.getService(InputManagerInternal.class); |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 1664 | mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class); |
lumark | 9a72d22 | 2019-03-30 18:31:45 +0800 | [diff] [blame] | 1665 | mImeDisplayValidator = displayId -> mWindowManagerInternal.shouldShowIme(displayId); |
Mita Yun | ed218c7 | 2012-12-06 17:18:25 -0800 | [diff] [blame] | 1666 | mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() { |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1667 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1668 | public void executeMessage(Message msg) { |
| 1669 | handleMessage(msg); |
| 1670 | } |
Mita Yun | ed218c7 | 2012-12-06 17:18:25 -0800 | [diff] [blame] | 1671 | }, true /*asyncHandler*/); |
Yohei Yukawa | d34e148 | 2016-02-11 08:03:52 -0800 | [diff] [blame] | 1672 | mAppOpsManager = mContext.getSystemService(AppOpsManager.class); |
Yohei Yukawa | ed4952a | 2016-02-17 07:57:25 -0800 | [diff] [blame] | 1673 | mUserManager = mContext.getSystemService(UserManager.class); |
Yohei Yukawa | 4208140 | 2019-01-15 09:57:50 -0800 | [diff] [blame] | 1674 | mUserManagerInternal = LocalServices.getService(UserManagerInternal.class); |
satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 1675 | mHardKeyboardListener = new HardKeyboardListener(); |
Dianne Hackborn | 119bbc3 | 2013-03-22 17:27:25 -0700 | [diff] [blame] | 1676 | mHasFeature = context.getPackageManager().hasSystemFeature( |
| 1677 | PackageManager.FEATURE_INPUT_METHODS); |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 1678 | mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime); |
Tarandeep Singh | eadb139 | 2018-11-09 18:15:57 +0100 | [diff] [blame] | 1679 | mIsLowRam = ActivityManager.isLowRamDeviceStatic(); |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1680 | |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 1681 | Bundle extras = new Bundle(); |
| 1682 | extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true); |
Tadashi G. Takaoka | 01065a5 | 2017-07-19 14:10:24 +0900 | [diff] [blame] | 1683 | @ColorInt final int accentColor = mContext.getColor( |
| 1684 | com.android.internal.R.color.system_notification_accent_color); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 1685 | mImeSwitcherNotification = |
| 1686 | new Notification.Builder(mContext, SystemNotificationChannels.VIRTUAL_KEYBOARD) |
| 1687 | .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default) |
| 1688 | .setWhen(0) |
| 1689 | .setOngoing(true) |
| 1690 | .addExtras(extras) |
| 1691 | .setCategory(Notification.CATEGORY_SYSTEM) |
Tadashi G. Takaoka | 01065a5 | 2017-07-19 14:10:24 +0900 | [diff] [blame] | 1692 | .setColor(accentColor); |
Daniel Sandler | 590d515 | 2012-06-14 16:10:13 -0400 | [diff] [blame] | 1693 | |
Tadashi G. Takaoka | 01065a5 | 2017-07-19 14:10:24 +0900 | [diff] [blame] | 1694 | Intent intent = new Intent(ACTION_SHOW_INPUT_METHOD_PICKER) |
| 1695 | .setPackage(mContext.getPackageName()); |
satok | 683e238 | 2011-07-12 08:28:52 +0900 | [diff] [blame] | 1696 | mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0); |
satok | b858c73 | 2011-07-22 19:54:34 +0900 | [diff] [blame] | 1697 | |
| 1698 | mShowOngoingImeSwitcherForPhones = false; |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1699 | |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1700 | mNotificationShown = false; |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1701 | int userId = 0; |
| 1702 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1703 | userId = ActivityManager.getService().getCurrentUser().id; |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1704 | } catch (RemoteException e) { |
| 1705 | Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e); |
| 1706 | } |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 1707 | |
Yohei Yukawa | a7babbb | 2019-01-08 16:45:34 -0800 | [diff] [blame] | 1708 | mLastSwitchUserId = userId; |
| 1709 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1710 | // mSettings should be created before buildInputMethodListLocked |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 1711 | mSettings = new InputMethodSettings( |
Yohei Yukawa | f927753 | 2019-01-25 02:47:32 -0800 | [diff] [blame] | 1712 | mRes, context.getContentResolver(), mMethodMap, userId, !mSystemReady); |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 1713 | |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1714 | updateCurrentProfileIds(); |
Yohei Yukawa | b557d57 | 2018-12-29 21:26:26 -0800 | [diff] [blame] | 1715 | AdditionalSubtypeUtils.load(mAdditionalSubtypeMap, userId); |
Yohei Yukawa | 7924782 | 2017-01-23 15:26:15 -0800 | [diff] [blame] | 1716 | mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked( |
| 1717 | mSettings, context); |
satok | 5b927c43 | 2012-05-01 20:09:34 +0900 | [diff] [blame] | 1718 | } |
| 1719 | |
satok | 5b927c43 | 2012-05-01 20:09:34 +0900 | [diff] [blame] | 1720 | private void resetDefaultImeLocked(Context context) { |
| 1721 | // Do not reset the default (current) IME when it is a 3rd-party IME |
Yohei Yukawa | fd70fe8 | 2018-04-08 12:19:56 -0700 | [diff] [blame] | 1722 | if (mCurMethodId != null && !mMethodMap.get(mCurMethodId).isSystem()) { |
satok | 5b927c43 | 2012-05-01 20:09:34 +0900 | [diff] [blame] | 1723 | return; |
| 1724 | } |
Yohei Yukawa | c2393ac | 2016-02-18 00:30:45 -0800 | [diff] [blame] | 1725 | final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes( |
Yohei Yukawa | af5cee8 | 2017-01-23 16:17:11 -0800 | [diff] [blame] | 1726 | context, mSettings.getEnabledInputMethodListLocked()); |
Yohei Yukawa | c2393ac | 2016-02-18 00:30:45 -0800 | [diff] [blame] | 1727 | if (suitableImes.isEmpty()) { |
| 1728 | Slog.i(TAG, "No default found"); |
| 1729 | return; |
satok | 5b927c43 | 2012-05-01 20:09:34 +0900 | [diff] [blame] | 1730 | } |
Yohei Yukawa | c2393ac | 2016-02-18 00:30:45 -0800 | [diff] [blame] | 1731 | final InputMethodInfo defIm = suitableImes.get(0); |
Yohei Yukawa | d033283 | 2017-02-01 13:59:43 -0800 | [diff] [blame] | 1732 | if (DEBUG) { |
| 1733 | Slog.i(TAG, "Default found, using " + defIm.getId()); |
| 1734 | } |
Yohei Yukawa | c2393ac | 2016-02-18 00:30:45 -0800 | [diff] [blame] | 1735 | setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false); |
satok | 5b927c43 | 2012-05-01 20:09:34 +0900 | [diff] [blame] | 1736 | } |
| 1737 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1738 | @GuardedBy("mMethodMap") |
Yohei Yukawa | d277d69 | 2020-02-19 17:12:17 -0800 | [diff] [blame] | 1739 | private void switchUserOnHandlerLocked(@UserIdInt int newUserId, |
| 1740 | IInputMethodClient clientToBeReset) { |
Yohei Yukawa | 90bf708 | 2015-06-03 23:50:27 -0700 | [diff] [blame] | 1741 | if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId |
| 1742 | + " currentUserId=" + mSettings.getCurrentUserId()); |
| 1743 | |
Yohei Yukawa | 8148297 | 2015-06-04 00:58:59 -0700 | [diff] [blame] | 1744 | // ContentObserver should be registered again when the user is changed |
| 1745 | mSettingsObserver.registerContentObserverLocked(newUserId); |
Yohei Yukawa | 68645a6 | 2016-02-17 07:54:20 -0800 | [diff] [blame] | 1746 | |
Yohei Yukawa | ed4952a | 2016-02-17 07:57:25 -0800 | [diff] [blame] | 1747 | // If the system is not ready or the device is not yed unlocked by the user, then we use |
| 1748 | // copy-on-write settings. |
| 1749 | final boolean useCopyOnWriteSettings = |
Yohei Yukawa | 4208140 | 2019-01-15 09:57:50 -0800 | [diff] [blame] | 1750 | !mSystemReady || !mUserManagerInternal.isUserUnlockingOrUnlocked(newUserId); |
Yohei Yukawa | 68645a6 | 2016-02-17 07:54:20 -0800 | [diff] [blame] | 1751 | mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings); |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1752 | updateCurrentProfileIds(); |
Yohei Yukawa | b557d57 | 2018-12-29 21:26:26 -0800 | [diff] [blame] | 1753 | // Additional subtypes should be reset when the user is changed |
| 1754 | AdditionalSubtypeUtils.load(mAdditionalSubtypeMap, newUserId); |
Satoshi Kataoka | 7f7535f | 2013-02-18 12:54:16 +0900 | [diff] [blame] | 1755 | final String defaultImiId = mSettings.getSelectedInputMethod(); |
Yohei Yukawa | 90bf708 | 2015-06-03 23:50:27 -0700 | [diff] [blame] | 1756 | |
| 1757 | if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId |
| 1758 | + " defaultImiId=" + defaultImiId); |
| 1759 | |
Satoshi Kataoka | 7c4a2a1 | 2013-02-25 15:25:43 +0900 | [diff] [blame] | 1760 | // For secondary users, the list of enabled IMEs may not have been updated since the |
| 1761 | // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may |
| 1762 | // not be empty even if the IME has been uninstalled by the primary user. |
| 1763 | // Even in such cases, IMMS works fine because it will find the most applicable |
| 1764 | // IME for that user. |
Satoshi Kataoka | ed1cdb2 | 2013-04-17 16:41:58 +0900 | [diff] [blame] | 1765 | final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId); |
Yohei Yukawa | 0d7aff8 | 2017-02-10 00:40:51 -0800 | [diff] [blame] | 1766 | mLastSystemLocales = mRes.getConfiguration().getLocales(); |
| 1767 | |
| 1768 | // TODO: Is it really possible that switchUserLocked() happens before system ready? |
| 1769 | if (mSystemReady) { |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 1770 | hideCurrentInputLocked( |
| 1771 | mCurFocusedWindow, 0, null, SoftInputShowHideReason.HIDE_SWITCH_USER); |
| 1772 | |
Yohei Yukawa | b752645 | 2018-10-21 20:15:17 -0700 | [diff] [blame] | 1773 | resetCurrentMethodAndClient(UnbindReason.SWITCH_USER); |
Yohei Yukawa | 0d7aff8 | 2017-02-10 00:40:51 -0800 | [diff] [blame] | 1774 | buildInputMethodListLocked(initialUserSwitch); |
| 1775 | if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) { |
| 1776 | // This is the first time of the user switch and |
| 1777 | // set the current ime to the proper one. |
| 1778 | resetDefaultImeLocked(mContext); |
| 1779 | } |
| 1780 | updateFromSettingsLocked(true); |
Yohei Yukawa | 0d7aff8 | 2017-02-10 00:40:51 -0800 | [diff] [blame] | 1781 | } |
| 1782 | |
Satoshi Kataoka | ed1cdb2 | 2013-04-17 16:41:58 +0900 | [diff] [blame] | 1783 | if (initialUserSwitch) { |
Yohei Yukawa | 094c71f | 2015-06-20 00:41:31 -0700 | [diff] [blame] | 1784 | InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager, |
| 1785 | mSettings.getEnabledInputMethodListLocked(), newUserId, |
| 1786 | mContext.getBasePackageName()); |
Satoshi Kataoka | ed1cdb2 | 2013-04-17 16:41:58 +0900 | [diff] [blame] | 1787 | } |
Yohei Yukawa | 90bf708 | 2015-06-03 23:50:27 -0700 | [diff] [blame] | 1788 | |
| 1789 | if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId |
| 1790 | + " selectedIme=" + mSettings.getSelectedInputMethod()); |
Yohei Yukawa | a7babbb | 2019-01-08 16:45:34 -0800 | [diff] [blame] | 1791 | |
| 1792 | mLastSwitchUserId = newUserId; |
Yohei Yukawa | d277d69 | 2020-02-19 17:12:17 -0800 | [diff] [blame] | 1793 | |
| 1794 | if (mIsInteractive && clientToBeReset != null) { |
| 1795 | final ClientState cs = mClients.get(clientToBeReset.asBinder()); |
| 1796 | if (cs == null) { |
| 1797 | // The client is already gone. |
| 1798 | return; |
| 1799 | } |
| 1800 | try { |
| 1801 | cs.client.scheduleStartInputIfNecessary(mInFullscreenMode); |
| 1802 | } catch (RemoteException e) { |
| 1803 | } |
| 1804 | } |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1805 | } |
| 1806 | |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1807 | void updateCurrentProfileIds() { |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 1808 | mSettings.setCurrentProfileIds( |
| 1809 | mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId())); |
Amith Yamasani | 734983f | 2014-03-04 16:48:05 -0800 | [diff] [blame] | 1810 | } |
| 1811 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1812 | @Override |
| 1813 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1814 | throws RemoteException { |
| 1815 | try { |
| 1816 | return super.onTransact(code, data, reply, flags); |
| 1817 | } catch (RuntimeException e) { |
| 1818 | // The input method manager only throws security exceptions, so let's |
| 1819 | // log all others. |
| 1820 | if (!(e instanceof SecurityException)) { |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 1821 | Slog.wtf(TAG, "Input Method Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1822 | } |
| 1823 | throw e; |
| 1824 | } |
| 1825 | } |
| 1826 | |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 1827 | public void systemRunning(StatusBarManagerService statusBar) { |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 1828 | synchronized (mMethodMap) { |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1829 | if (DEBUG) { |
| 1830 | Slog.d(TAG, "--- systemReady"); |
| 1831 | } |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 1832 | if (!mSystemReady) { |
| 1833 | mSystemReady = true; |
Yohei Yukawa | 7924782 | 2017-01-23 15:26:15 -0800 | [diff] [blame] | 1834 | mLastSystemLocales = mRes.getConfiguration().getLocales(); |
Yohei Yukawa | 68645a6 | 2016-02-17 07:54:20 -0800 | [diff] [blame] | 1835 | final int currentUserId = mSettings.getCurrentUserId(); |
Yohei Yukawa | ed4952a | 2016-02-17 07:57:25 -0800 | [diff] [blame] | 1836 | mSettings.switchCurrentUser(currentUserId, |
Yohei Yukawa | 4208140 | 2019-01-15 09:57:50 -0800 | [diff] [blame] | 1837 | !mUserManagerInternal.isUserUnlockingOrUnlocked(currentUserId)); |
Yohei Yukawa | d34e148 | 2016-02-11 08:03:52 -0800 | [diff] [blame] | 1838 | mKeyguardManager = mContext.getSystemService(KeyguardManager.class); |
| 1839 | mNotificationManager = mContext.getSystemService(NotificationManager.class); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 1840 | mStatusBar = statusBar; |
Griff Hazen | 6090c26 | 2016-03-25 08:11:24 -0700 | [diff] [blame] | 1841 | if (mStatusBar != null) { |
| 1842 | mStatusBar.setIconVisibility(mSlotIme, false); |
| 1843 | } |
Yohei Yukawa | 849443c | 2019-01-21 09:02:25 -0800 | [diff] [blame] | 1844 | updateSystemUiLocked(mImeWindowVis, mBackDisposition); |
satok | b858c73 | 2011-07-22 19:54:34 +0900 | [diff] [blame] | 1845 | mShowOngoingImeSwitcherForPhones = mRes.getBoolean( |
| 1846 | com.android.internal.R.bool.show_ongoing_ime_switcher); |
satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 1847 | if (mShowOngoingImeSwitcherForPhones) { |
Seigo Nonaka | 7309b12 | 2015-08-17 18:34:13 -0700 | [diff] [blame] | 1848 | mWindowManagerInternal.setOnHardKeyboardStatusChangeListener( |
satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 1849 | mHardKeyboardListener); |
| 1850 | } |
Yohei Yukawa | 7924782 | 2017-01-23 15:26:15 -0800 | [diff] [blame] | 1851 | |
| 1852 | mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true); |
| 1853 | mSettingsObserver.registerContentObserverLocked(currentUserId); |
| 1854 | |
Yohei Yukawa | 0ec0800 | 2019-03-08 10:48:26 -0800 | [diff] [blame] | 1855 | final IntentFilter broadcastFilterForSystemUser = new IntentFilter(); |
| 1856 | broadcastFilterForSystemUser.addAction(Intent.ACTION_USER_ADDED); |
| 1857 | broadcastFilterForSystemUser.addAction(Intent.ACTION_USER_REMOVED); |
| 1858 | broadcastFilterForSystemUser.addAction(Intent.ACTION_LOCALE_CHANGED); |
| 1859 | broadcastFilterForSystemUser.addAction(ACTION_SHOW_INPUT_METHOD_PICKER); |
| 1860 | mContext.registerReceiver(new ImmsBroadcastReceiverForSystemUser(), |
| 1861 | broadcastFilterForSystemUser); |
| 1862 | |
| 1863 | final IntentFilter broadcastFilterForAllUsers = new IntentFilter(); |
| 1864 | broadcastFilterForAllUsers.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
| 1865 | mContext.registerReceiverAsUser(new ImmsBroadcastReceiverForAllUsers(), |
| 1866 | UserHandle.ALL, broadcastFilterForAllUsers, null, null); |
Yohei Yukawa | 7924782 | 2017-01-23 15:26:15 -0800 | [diff] [blame] | 1867 | |
Yohei Yukawa | 1f9a3cb | 2017-10-26 15:00:59 -0700 | [diff] [blame] | 1868 | final String defaultImiId = mSettings.getSelectedInputMethod(); |
| 1869 | final boolean imeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId); |
| 1870 | buildInputMethodListLocked(!imeSelectedOnBoot /* resetDefaultEnabledIme */); |
Yohei Yukawa | 7924782 | 2017-01-23 15:26:15 -0800 | [diff] [blame] | 1871 | updateFromSettingsLocked(true); |
| 1872 | InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager, |
| 1873 | mSettings.getEnabledInputMethodListLocked(), currentUserId, |
| 1874 | mContext.getBasePackageName()); |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 1875 | } |
| 1876 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1877 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1878 | |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1879 | // --------------------------------------------------------------------------------------- |
| 1880 | // Check whether or not this is a valid IPC. Assumes an IPC is valid when either |
| 1881 | // 1) it comes from the system process |
| 1882 | // 2) the calling process' user id is identical to the current user id IMMS thinks. |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 1883 | @GuardedBy("mMethodMap") |
Yohei Yukawa | 46d7476 | 2019-01-22 10:17:22 -0800 | [diff] [blame] | 1884 | private boolean calledFromValidUserLocked() { |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1885 | final int uid = Binder.getCallingUid(); |
| 1886 | final int userId = UserHandle.getUserId(uid); |
| 1887 | if (DEBUG) { |
| 1888 | Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? " |
| 1889 | + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID |
| 1890 | + " calling userId = " + userId + ", foreground user id = " |
Satoshi Kataoka | 87c2914 | 2013-07-31 23:11:54 +0900 | [diff] [blame] | 1891 | + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid() |
| 1892 | + InputMethodUtils.getApiCallStack()); |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1893 | } |
Yohei Yukawa | a878b95 | 2019-01-10 19:36:24 -0800 | [diff] [blame] | 1894 | if (uid == Process.SYSTEM_UID) { |
| 1895 | return true; |
| 1896 | } |
| 1897 | if (userId == mSettings.getCurrentUserId()) { |
| 1898 | return true; |
| 1899 | } |
Satoshi Kataoka | 135e5fb | 2012-09-28 18:25:06 +0900 | [diff] [blame] | 1900 | |
| 1901 | // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the |
| 1902 | // foreground user, not for the user of that process. Accordingly InputMethodManagerService |
| 1903 | // must not manage background users' states in any functions. |
| 1904 | // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded |
| 1905 | // by a token. |
| 1906 | if (mContext.checkCallingOrSelfPermission( |
| 1907 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1908 | == PackageManager.PERMISSION_GRANTED) { |
| 1909 | if (DEBUG) { |
| 1910 | Slog.d(TAG, "--- Access granted because the calling process has " |
| 1911 | + "the INTERACT_ACROSS_USERS_FULL permission"); |
| 1912 | } |
| 1913 | return true; |
| 1914 | } |
Yohei Yukawa | d033283 | 2017-02-01 13:59:43 -0800 | [diff] [blame] | 1915 | // TODO(b/34886274): The semantics of this verification is actually not well-defined. |
Seigo Nonaka | e27dc2b | 2015-08-14 18:21:27 -0700 | [diff] [blame] | 1916 | Slog.w(TAG, "--- IPC called from background users. Ignore. callers=" |
| 1917 | + Debug.getCallers(10)); |
Satoshi Kataoka | 135e5fb | 2012-09-28 18:25:06 +0900 | [diff] [blame] | 1918 | return false; |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1919 | } |
| 1920 | |
Yohei Yukawa | 22c97be | 2014-06-04 19:43:36 +0900 | [diff] [blame] | 1921 | |
| 1922 | /** |
| 1923 | * Returns true iff the caller is identified to be the current input method with the token. |
| 1924 | * @param token The window token given to the input method when it was started. |
| 1925 | * @return true if and only if non-null valid token is specified. |
| 1926 | */ |
Yohei Yukawa | 79e4c6a | 2018-12-09 19:07:17 -0800 | [diff] [blame] | 1927 | @GuardedBy("mMethodMap") |
Yohei Yukawa | d3e0fd93 | 2019-01-21 09:16:48 -0800 | [diff] [blame] | 1928 | private boolean calledWithValidTokenLocked(@NonNull IBinder token) { |
| 1929 | if (token == null) { |
| 1930 | throw new InvalidParameterException("token must not be null."); |
Yohei Yukawa | d033283 | 2017-02-01 13:59:43 -0800 | [diff] [blame] | 1931 | } |
Yohei Yukawa | d3e0fd93 | 2019-01-21 09:16:48 -0800 | [diff] [blame] | 1932 | if (token != mCurToken) { |
Yohei Yukawa | d033283 | 2017-02-01 13:59:43 -0800 | [diff] [blame] | 1933 | Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token." |
| 1934 | + " uid:" + Binder.getCallingUid() + " token:" + token); |
Yohei Yukawa | 22c97be | 2014-06-04 19:43:36 +0900 | [diff] [blame] | 1935 | return false; |
| 1936 | } |
| 1937 | return true; |
| 1938 | } |
| 1939 | |
Yohei Yukawa | f80087c | 2018-05-21 09:47:53 -0700 | [diff] [blame] | 1940 | @GuardedBy("mMethodMap") |
| 1941 | private boolean bindCurrentInputMethodServiceLocked( |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1942 | Intent service, ServiceConnection conn, int flags) { |
| 1943 | if (service == null || conn == null) { |
| 1944 | Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn); |
| 1945 | return false; |
| 1946 | } |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 1947 | return mContext.bindServiceAsUser(service, conn, flags, |
| 1948 | new UserHandle(mSettings.getCurrentUserId())); |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1949 | } |
| 1950 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1951 | @Override |
Yohei Yukawa | d20eef8 | 2019-02-05 10:45:32 -0800 | [diff] [blame] | 1952 | public List<InputMethodInfo> getInputMethodList(@UserIdInt int userId) { |
| 1953 | if (UserHandle.getCallingUserId() != userId) { |
| 1954 | mContext.enforceCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL, null); |
| 1955 | } |
Yohei Yukawa | 7f8ee4b | 2019-01-10 08:44:57 -0800 | [diff] [blame] | 1956 | synchronized (mMethodMap) { |
Yohei Yukawa | d20eef8 | 2019-02-05 10:45:32 -0800 | [diff] [blame] | 1957 | final int[] resolvedUserIds = InputMethodUtils.resolveUserId(userId, |
Yohei Yukawa | 7f8ee4b | 2019-01-10 08:44:57 -0800 | [diff] [blame] | 1958 | mSettings.getCurrentUserId(), null); |
| 1959 | if (resolvedUserIds.length != 1) { |
| 1960 | return Collections.emptyList(); |
| 1961 | } |
| 1962 | final long ident = Binder.clearCallingIdentity(); |
| 1963 | try { |
Yohei Yukawa | 2988d2d | 2019-01-22 21:29:09 -0800 | [diff] [blame] | 1964 | return getInputMethodListLocked(resolvedUserIds[0]); |
Yohei Yukawa | 7f8ee4b | 2019-01-10 08:44:57 -0800 | [diff] [blame] | 1965 | } finally { |
| 1966 | Binder.restoreCallingIdentity(ident); |
Tarandeep Singh | 89a6c48 | 2017-11-21 14:26:11 -0800 | [diff] [blame] | 1967 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1968 | } |
| 1969 | } |
| 1970 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1971 | @Override |
Yohei Yukawa | 1fb13c5 | 2019-02-05 07:55:28 -0800 | [diff] [blame] | 1972 | public List<InputMethodInfo> getEnabledInputMethodList(@UserIdInt int userId) { |
| 1973 | if (UserHandle.getCallingUserId() != userId) { |
| 1974 | mContext.enforceCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL, null); |
| 1975 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1976 | synchronized (mMethodMap) { |
Yohei Yukawa | 1fb13c5 | 2019-02-05 07:55:28 -0800 | [diff] [blame] | 1977 | final int[] resolvedUserIds = InputMethodUtils.resolveUserId(userId, |
Yohei Yukawa | 7f8ee4b | 2019-01-10 08:44:57 -0800 | [diff] [blame] | 1978 | mSettings.getCurrentUserId(), null); |
| 1979 | if (resolvedUserIds.length != 1) { |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 1980 | return Collections.emptyList(); |
| 1981 | } |
Yohei Yukawa | 7f8ee4b | 2019-01-10 08:44:57 -0800 | [diff] [blame] | 1982 | final long ident = Binder.clearCallingIdentity(); |
| 1983 | try { |
| 1984 | return getEnabledInputMethodListLocked(resolvedUserIds[0]); |
| 1985 | } finally { |
| 1986 | Binder.restoreCallingIdentity(ident); |
| 1987 | } |
| 1988 | } |
| 1989 | } |
| 1990 | |
| 1991 | @GuardedBy("mMethodMap") |
Yohei Yukawa | 2988d2d | 2019-01-22 21:29:09 -0800 | [diff] [blame] | 1992 | private List<InputMethodInfo> getInputMethodListLocked(@UserIdInt int userId) { |
Yohei Yukawa | 7f8ee4b | 2019-01-10 08:44:57 -0800 | [diff] [blame] | 1993 | final ArrayList<InputMethodInfo> methodList; |
| 1994 | if (userId == mSettings.getCurrentUserId()) { |
| 1995 | // Create a copy. |
| 1996 | methodList = new ArrayList<>(mMethodList); |
| 1997 | } else { |
| 1998 | final ArrayMap<String, InputMethodInfo> methodMap = new ArrayMap<>(); |
| 1999 | methodList = new ArrayList<>(); |
| 2000 | final ArrayMap<String, List<InputMethodSubtype>> additionalSubtypeMap = |
| 2001 | new ArrayMap<>(); |
| 2002 | AdditionalSubtypeUtils.load(additionalSubtypeMap, userId); |
| 2003 | queryInputMethodServicesInternal(mContext, userId, additionalSubtypeMap, methodMap, |
| 2004 | methodList); |
| 2005 | } |
Yohei Yukawa | 7f8ee4b | 2019-01-10 08:44:57 -0800 | [diff] [blame] | 2006 | return methodList; |
| 2007 | } |
| 2008 | |
| 2009 | @GuardedBy("mMethodMap") |
| 2010 | private List<InputMethodInfo> getEnabledInputMethodListLocked(@UserIdInt int userId) { |
| 2011 | if (userId == mSettings.getCurrentUserId()) { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2012 | return mSettings.getEnabledInputMethodListLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2013 | } |
Yohei Yukawa | 7f8ee4b | 2019-01-10 08:44:57 -0800 | [diff] [blame] | 2014 | final ArrayMap<String, InputMethodInfo> methodMap = new ArrayMap<>(); |
| 2015 | final ArrayList<InputMethodInfo> methodList = new ArrayList<>(); |
| 2016 | final ArrayMap<String, List<InputMethodSubtype>> additionalSubtypeMap = |
| 2017 | new ArrayMap<>(); |
| 2018 | AdditionalSubtypeUtils.load(additionalSubtypeMap, userId); |
| 2019 | queryInputMethodServicesInternal(mContext, userId, additionalSubtypeMap, methodMap, |
| 2020 | methodList); |
| 2021 | final InputMethodSettings settings = new InputMethodSettings(mContext.getResources(), |
Yohei Yukawa | f927753 | 2019-01-25 02:47:32 -0800 | [diff] [blame] | 2022 | mContext.getContentResolver(), methodMap, userId, true); |
Yohei Yukawa | 7f8ee4b | 2019-01-10 08:44:57 -0800 | [diff] [blame] | 2023 | return settings.getEnabledInputMethodListLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2024 | } |
| 2025 | |
Adam He | bc67f2e | 2019-11-13 14:34:56 -0800 | [diff] [blame] | 2026 | @GuardedBy("mMethodMap") |
Feng Cao | 16b2de5 | 2020-01-09 17:27:27 -0800 | [diff] [blame] | 2027 | private void onCreateInlineSuggestionsRequestLocked(@UserIdInt int userId, |
Feng Cao | 36960ee | 2020-02-18 18:23:30 -0800 | [diff] [blame] | 2028 | InlineSuggestionsRequestInfo requestInfo, IInlineSuggestionsRequestCallback callback) { |
Adam He | 7bc8f60 | 2019-12-12 17:00:34 -0800 | [diff] [blame] | 2029 | final InputMethodInfo imi = mMethodMap.get(mCurMethodId); |
| 2030 | try { |
Feng Cao | 16b2de5 | 2020-01-09 17:27:27 -0800 | [diff] [blame] | 2031 | if (userId == mSettings.getCurrentUserId() && imi != null |
| 2032 | && imi.isInlineSuggestionsEnabled() && mCurMethod != null) { |
Adam He | 7bc8f60 | 2019-12-12 17:00:34 -0800 | [diff] [blame] | 2033 | executeOrSendMessage(mCurMethod, |
Feng Cao | 36960ee | 2020-02-18 18:23:30 -0800 | [diff] [blame] | 2034 | mCaller.obtainMessageOOO(MSG_INLINE_SUGGESTIONS_REQUEST, mCurMethod, |
| 2035 | requestInfo, new InlineSuggestionsRequestCallbackDecorator(callback, |
lpeter | 133fce0 | 2020-03-05 20:32:16 +0800 | [diff] [blame] | 2036 | imi.getPackageName(), mCurTokenDisplayId, mCurToken, |
| 2037 | this))); |
Adam He | 7bc8f60 | 2019-12-12 17:00:34 -0800 | [diff] [blame] | 2038 | } else { |
| 2039 | callback.onInlineSuggestionsUnsupported(); |
| 2040 | } |
| 2041 | } catch (RemoteException e) { |
| 2042 | Slog.w(TAG, "RemoteException calling onCreateInlineSuggestionsRequest(): " + e); |
Adam He | bc67f2e | 2019-11-13 14:34:56 -0800 | [diff] [blame] | 2043 | } |
| 2044 | } |
| 2045 | |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 2046 | /** |
Feng Cao | 16b2de5 | 2020-01-09 17:27:27 -0800 | [diff] [blame] | 2047 | * The decorator which validates the host package name in the |
| 2048 | * {@link InlineSuggestionsRequest} argument to make sure it matches the IME package name. |
| 2049 | */ |
| 2050 | private static final class InlineSuggestionsRequestCallbackDecorator |
| 2051 | extends IInlineSuggestionsRequestCallback.Stub { |
| 2052 | @NonNull |
| 2053 | private final IInlineSuggestionsRequestCallback mCallback; |
| 2054 | @NonNull |
| 2055 | private final String mImePackageName; |
| 2056 | |
Svet Ganov | 02fdd34 | 2020-02-20 20:48:34 -0800 | [diff] [blame] | 2057 | private final int mImeDisplayId; |
| 2058 | |
lpeter | 133fce0 | 2020-03-05 20:32:16 +0800 | [diff] [blame] | 2059 | @NonNull |
| 2060 | private final IBinder mImeToken; |
| 2061 | @NonNull |
| 2062 | private final InputMethodManagerService mImms; |
| 2063 | |
Feng Cao | 16b2de5 | 2020-01-09 17:27:27 -0800 | [diff] [blame] | 2064 | InlineSuggestionsRequestCallbackDecorator( |
lpeter | 133fce0 | 2020-03-05 20:32:16 +0800 | [diff] [blame] | 2065 | @NonNull IInlineSuggestionsRequestCallback callback, @NonNull String imePackageName, |
| 2066 | int displayId, @NonNull IBinder imeToken, @NonNull InputMethodManagerService imms) { |
Feng Cao | 16b2de5 | 2020-01-09 17:27:27 -0800 | [diff] [blame] | 2067 | mCallback = callback; |
| 2068 | mImePackageName = imePackageName; |
Svet Ganov | 02fdd34 | 2020-02-20 20:48:34 -0800 | [diff] [blame] | 2069 | mImeDisplayId = displayId; |
lpeter | 133fce0 | 2020-03-05 20:32:16 +0800 | [diff] [blame] | 2070 | mImeToken = imeToken; |
| 2071 | mImms = imms; |
Feng Cao | 16b2de5 | 2020-01-09 17:27:27 -0800 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | @Override |
| 2075 | public void onInlineSuggestionsUnsupported() throws RemoteException { |
| 2076 | mCallback.onInlineSuggestionsUnsupported(); |
| 2077 | } |
| 2078 | |
| 2079 | @Override |
| 2080 | public void onInlineSuggestionsRequest(InlineSuggestionsRequest request, |
Feng Cao | 97ec1c4 | 2020-03-25 12:20:42 -0700 | [diff] [blame] | 2081 | IInlineSuggestionsResponseCallback callback) |
Feng Cao | 7c85eb7 | 2020-02-28 11:39:56 -0800 | [diff] [blame] | 2082 | throws RemoteException { |
Feng Cao | 16b2de5 | 2020-01-09 17:27:27 -0800 | [diff] [blame] | 2083 | if (!mImePackageName.equals(request.getHostPackageName())) { |
| 2084 | throw new SecurityException( |
| 2085 | "Host package name in the provide request=[" + request.getHostPackageName() |
| 2086 | + "] doesn't match the IME package name=[" + mImePackageName |
| 2087 | + "]."); |
| 2088 | } |
Svet Ganov | 02fdd34 | 2020-02-20 20:48:34 -0800 | [diff] [blame] | 2089 | request.setHostDisplayId(mImeDisplayId); |
lpeter | 133fce0 | 2020-03-05 20:32:16 +0800 | [diff] [blame] | 2090 | mImms.setCurHostInputToken(mImeToken, request.getHostInputToken()); |
Feng Cao | 97ec1c4 | 2020-03-25 12:20:42 -0700 | [diff] [blame] | 2091 | mCallback.onInlineSuggestionsRequest(request, callback); |
Feng Cao | 7c85eb7 | 2020-02-28 11:39:56 -0800 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | @Override |
Feng Cao | 97ec1c4 | 2020-03-25 12:20:42 -0700 | [diff] [blame] | 2095 | public void onInputMethodStartInput(AutofillId imeFieldId) throws RemoteException { |
| 2096 | mCallback.onInputMethodStartInput(imeFieldId); |
Feng Cao | 7c85eb7 | 2020-02-28 11:39:56 -0800 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | @Override |
Feng Cao | 97ec1c4 | 2020-03-25 12:20:42 -0700 | [diff] [blame] | 2100 | public void onInputMethodShowInputRequested(boolean requestResult) throws RemoteException { |
| 2101 | mCallback.onInputMethodShowInputRequested(requestResult); |
| 2102 | } |
| 2103 | |
| 2104 | @Override |
| 2105 | public void onInputMethodStartInputView() throws RemoteException { |
| 2106 | mCallback.onInputMethodStartInputView(); |
| 2107 | } |
| 2108 | |
| 2109 | @Override |
| 2110 | public void onInputMethodFinishInputView() throws RemoteException { |
| 2111 | mCallback.onInputMethodFinishInputView(); |
| 2112 | } |
| 2113 | |
| 2114 | @Override |
| 2115 | public void onInputMethodFinishInput() throws RemoteException { |
| 2116 | mCallback.onInputMethodFinishInput(); |
Feng Cao | 16b2de5 | 2020-01-09 17:27:27 -0800 | [diff] [blame] | 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | /** |
lpeter | 133fce0 | 2020-03-05 20:32:16 +0800 | [diff] [blame] | 2121 | * Sets current host input token. |
| 2122 | * |
| 2123 | * @param callerImeToken the token has been made for the current active input method |
| 2124 | * @param hostInputToken the host input token of the current active input method |
| 2125 | */ |
| 2126 | void setCurHostInputToken(@NonNull IBinder callerImeToken, @Nullable IBinder hostInputToken) { |
| 2127 | synchronized (mMethodMap) { |
| 2128 | if (!calledWithValidTokenLocked(callerImeToken)) { |
| 2129 | return; |
| 2130 | } |
| 2131 | mCurHostInputToken = hostInputToken; |
| 2132 | } |
| 2133 | } |
| 2134 | |
| 2135 | /** |
Satoshi Kataoka | b3c21ac | 2013-08-07 15:43:29 +0900 | [diff] [blame] | 2136 | * @param imiId if null, returns enabled subtypes for the current imi |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 2137 | * @return enabled subtypes of the specified imi |
| 2138 | */ |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 2139 | @Override |
Satoshi Kataoka | b3c21ac | 2013-08-07 15:43:29 +0900 | [diff] [blame] | 2140 | public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId, |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2141 | boolean allowsImplicitlySelectedSubtypes) { |
Yohei Yukawa | 7f8ee4b | 2019-01-10 08:44:57 -0800 | [diff] [blame] | 2142 | final int callingUserId = UserHandle.getCallingUserId(); |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2143 | synchronized (mMethodMap) { |
Yohei Yukawa | 7f8ee4b | 2019-01-10 08:44:57 -0800 | [diff] [blame] | 2144 | final int[] resolvedUserIds = InputMethodUtils.resolveUserId(callingUserId, |
| 2145 | mSettings.getCurrentUserId(), null); |
| 2146 | if (resolvedUserIds.length != 1) { |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 2147 | return Collections.emptyList(); |
| 2148 | } |
Yohei Yukawa | 7f8ee4b | 2019-01-10 08:44:57 -0800 | [diff] [blame] | 2149 | final long ident = Binder.clearCallingIdentity(); |
| 2150 | try { |
| 2151 | return getEnabledInputMethodSubtypeListLocked(imiId, |
| 2152 | allowsImplicitlySelectedSubtypes, resolvedUserIds[0]); |
| 2153 | } finally { |
| 2154 | Binder.restoreCallingIdentity(ident); |
| 2155 | } |
| 2156 | } |
| 2157 | } |
| 2158 | |
| 2159 | @GuardedBy("mMethodMap") |
| 2160 | private List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(String imiId, |
| 2161 | boolean allowsImplicitlySelectedSubtypes, @UserIdInt int userId) { |
| 2162 | if (userId == mSettings.getCurrentUserId()) { |
Satoshi Kataoka | b3c21ac | 2013-08-07 15:43:29 +0900 | [diff] [blame] | 2163 | final InputMethodInfo imi; |
| 2164 | if (imiId == null && mCurMethodId != null) { |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 2165 | imi = mMethodMap.get(mCurMethodId); |
Satoshi Kataoka | b3c21ac | 2013-08-07 15:43:29 +0900 | [diff] [blame] | 2166 | } else { |
| 2167 | imi = mMethodMap.get(imiId); |
| 2168 | } |
| 2169 | if (imi == null) { |
Yohei Yukawa | b0377bb | 2015-08-10 21:06:30 -0700 | [diff] [blame] | 2170 | return Collections.emptyList(); |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 2171 | } |
| 2172 | return mSettings.getEnabledInputMethodSubtypeListLocked( |
| 2173 | mContext, imi, allowsImplicitlySelectedSubtypes); |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2174 | } |
Yohei Yukawa | 7f8ee4b | 2019-01-10 08:44:57 -0800 | [diff] [blame] | 2175 | final ArrayMap<String, InputMethodInfo> methodMap = new ArrayMap<>(); |
| 2176 | final ArrayList<InputMethodInfo> methodList = new ArrayList<>(); |
| 2177 | final ArrayMap<String, List<InputMethodSubtype>> additionalSubtypeMap = |
| 2178 | new ArrayMap<>(); |
| 2179 | AdditionalSubtypeUtils.load(additionalSubtypeMap, userId); |
| 2180 | queryInputMethodServicesInternal(mContext, userId, additionalSubtypeMap, methodMap, |
| 2181 | methodList); |
| 2182 | final InputMethodInfo imi = methodMap.get(imiId); |
| 2183 | if (imi == null) { |
| 2184 | return Collections.emptyList(); |
| 2185 | } |
| 2186 | final InputMethodSettings settings = new InputMethodSettings(mContext.getResources(), |
Yohei Yukawa | f927753 | 2019-01-25 02:47:32 -0800 | [diff] [blame] | 2187 | mContext.getContentResolver(), methodMap, userId, true); |
Yohei Yukawa | 7f8ee4b | 2019-01-10 08:44:57 -0800 | [diff] [blame] | 2188 | return settings.getEnabledInputMethodSubtypeListLocked( |
| 2189 | mContext, imi, allowsImplicitlySelectedSubtypes); |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2190 | } |
| 2191 | |
Yohei Yukawa | a71bb25 | 2018-09-19 19:21:24 -0700 | [diff] [blame] | 2192 | /** |
| 2193 | * Called by each application process as a preparation to start interacting with |
| 2194 | * {@link InputMethodManagerService}. |
| 2195 | * |
| 2196 | * <p>As a general principle, IPCs from the application process that take |
Yohei Yukawa | 499e3f7 | 2018-10-21 20:15:11 -0700 | [diff] [blame] | 2197 | * {@link IInputMethodClient} will be rejected without this step.</p> |
Yohei Yukawa | a71bb25 | 2018-09-19 19:21:24 -0700 | [diff] [blame] | 2198 | * |
| 2199 | * @param client {@link android.os.Binder} proxy that is associated with the singleton instance |
| 2200 | * of {@link android.view.inputmethod.InputMethodManager} that runs on the client |
| 2201 | * process |
| 2202 | * @param inputContext communication channel for the dummy |
| 2203 | * {@link android.view.inputmethod.InputConnection} |
Yohei Yukawa | 4052a10f | 2018-10-15 15:35:55 +0800 | [diff] [blame] | 2204 | * @param selfReportedDisplayId self-reported display ID to which the client is associated. |
| 2205 | * Whether the client is still allowed to access to this display |
| 2206 | * or not needs to be evaluated every time the client interacts |
| 2207 | * with the display |
Yohei Yukawa | a71bb25 | 2018-09-19 19:21:24 -0700 | [diff] [blame] | 2208 | */ |
| 2209 | @Override |
Yohei Yukawa | 4052a10f | 2018-10-15 15:35:55 +0800 | [diff] [blame] | 2210 | public void addClient(IInputMethodClient client, IInputContext inputContext, |
| 2211 | int selfReportedDisplayId) { |
Yohei Yukawa | cb768bc | 2018-10-24 16:05:09 -0700 | [diff] [blame] | 2212 | // Here there are two scenarios where this method is called: |
| 2213 | // A. IMM is being instantiated in a different process and this is an IPC from that process |
| 2214 | // B. IMM is being instantiated in the same process but Binder.clearCallingIdentity() is |
| 2215 | // called in the caller side if necessary. |
| 2216 | // In either case the following UID/PID should be the ones where InputMethodManager is |
| 2217 | // actually running. |
Yohei Yukawa | a71bb25 | 2018-09-19 19:21:24 -0700 | [diff] [blame] | 2218 | final int callerUid = Binder.getCallingUid(); |
| 2219 | final int callerPid = Binder.getCallingPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2220 | synchronized (mMethodMap) { |
Yohei Yukawa | a71bb25 | 2018-09-19 19:21:24 -0700 | [diff] [blame] | 2221 | // TODO: Optimize this linear search. |
Yohei Yukawa | ac9311e | 2018-11-20 19:25:23 -0800 | [diff] [blame] | 2222 | final int numClients = mClients.size(); |
| 2223 | for (int i = 0; i < numClients; ++i) { |
| 2224 | final ClientState state = mClients.valueAt(i); |
Yohei Yukawa | 4052a10f | 2018-10-15 15:35:55 +0800 | [diff] [blame] | 2225 | if (state.uid == callerUid && state.pid == callerPid |
| 2226 | && state.selfReportedDisplayId == selfReportedDisplayId) { |
Yohei Yukawa | a71bb25 | 2018-09-19 19:21:24 -0700 | [diff] [blame] | 2227 | throw new SecurityException("uid=" + callerUid + "/pid=" + callerPid |
Yohei Yukawa | cb768bc | 2018-10-24 16:05:09 -0700 | [diff] [blame] | 2228 | + "/displayId=" + selfReportedDisplayId + " is already registered."); |
Yohei Yukawa | a71bb25 | 2018-09-19 19:21:24 -0700 | [diff] [blame] | 2229 | } |
| 2230 | } |
| 2231 | final ClientDeathRecipient deathRecipient = new ClientDeathRecipient(this, client); |
| 2232 | try { |
| 2233 | client.asBinder().linkToDeath(deathRecipient, 0); |
| 2234 | } catch (RemoteException e) { |
| 2235 | throw new IllegalStateException(e); |
| 2236 | } |
Yohei Yukawa | 4052a10f | 2018-10-15 15:35:55 +0800 | [diff] [blame] | 2237 | // We cannot fully avoid race conditions where the client UID already lost the access to |
| 2238 | // the given self-reported display ID, even if the client is not maliciously reporting |
| 2239 | // a fake display ID. Unconditionally returning SecurityException just because the |
| 2240 | // client doesn't pass display ID verification can cause many test failures hence not an |
| 2241 | // option right now. At the same time |
| 2242 | // context.getSystemService(InputMethodManager.class) |
| 2243 | // is expected to return a valid non-null instance at any time if we do not choose to |
| 2244 | // have the client crash. Thus we do not verify the display ID at all here. Instead we |
| 2245 | // later check the display ID every time the client needs to interact with the specified |
| 2246 | // display. |
| 2247 | mClients.put(client.asBinder(), new ClientState(client, inputContext, callerUid, |
| 2248 | callerPid, selfReportedDisplayId, deathRecipient)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2249 | } |
| 2250 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2251 | |
Yohei Yukawa | e24ed79 | 2018-08-28 19:10:32 -0700 | [diff] [blame] | 2252 | void removeClient(IInputMethodClient client) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2253 | synchronized (mMethodMap) { |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 2254 | ClientState cs = mClients.remove(client.asBinder()); |
| 2255 | if (cs != null) { |
Yohei Yukawa | a71bb25 | 2018-09-19 19:21:24 -0700 | [diff] [blame] | 2256 | client.asBinder().unlinkToDeath(cs.clientDeathRecipient, 0); |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 2257 | clearClientSessionLocked(cs); |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 2258 | |
| 2259 | final int numItems = mActivityViewDisplayIdToParentMap.size(); |
| 2260 | for (int i = numItems - 1; i >= 0; --i) { |
| 2261 | final ActivityViewInfo info = mActivityViewDisplayIdToParentMap.valueAt(i); |
| 2262 | if (info.mParentClient == cs) { |
| 2263 | mActivityViewDisplayIdToParentMap.removeAt(i); |
| 2264 | } |
| 2265 | } |
| 2266 | |
Yohei Yukawa | 072b1b5 | 2015-11-18 15:54:34 -0800 | [diff] [blame] | 2267 | if (mCurClient == cs) { |
Tarandeep Singh | 93c00cea | 2018-02-16 14:31:17 -0800 | [diff] [blame] | 2268 | if (mBoundToMethod) { |
| 2269 | mBoundToMethod = false; |
| 2270 | if (mCurMethod != null) { |
| 2271 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageO( |
| 2272 | MSG_UNBIND_INPUT, mCurMethod)); |
| 2273 | } |
| 2274 | } |
Yohei Yukawa | 072b1b5 | 2015-11-18 15:54:34 -0800 | [diff] [blame] | 2275 | mCurClient = null; |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 2276 | mCurActivityViewToScreenMatrix = null; |
Yohei Yukawa | 072b1b5 | 2015-11-18 15:54:34 -0800 | [diff] [blame] | 2277 | } |
Yohei Yukawa | e39d4ed | 2015-11-19 03:38:49 -0800 | [diff] [blame] | 2278 | if (mCurFocusedWindowClient == cs) { |
| 2279 | mCurFocusedWindowClient = null; |
| 2280 | } |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 2281 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2282 | } |
| 2283 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2284 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2285 | void executeOrSendMessage(IInterface target, Message msg) { |
| 2286 | if (target.asBinder() instanceof Binder) { |
| 2287 | mCaller.sendMessage(msg); |
| 2288 | } else { |
| 2289 | handleMessage(msg); |
| 2290 | msg.recycle(); |
| 2291 | } |
| 2292 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2293 | |
Yohei Yukawa | 4afd933 | 2018-10-21 10:43:53 -0700 | [diff] [blame] | 2294 | void unbindCurrentClientLocked(@UnbindReason int unbindClientReason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2295 | if (mCurClient != null) { |
Yohei Yukawa | 35d3f37 | 2015-11-25 11:07:19 -0800 | [diff] [blame] | 2296 | if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2297 | + mCurClient.client.asBinder()); |
| 2298 | if (mBoundToMethod) { |
| 2299 | mBoundToMethod = false; |
| 2300 | if (mCurMethod != null) { |
| 2301 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageO( |
| 2302 | MSG_UNBIND_INPUT, mCurMethod)); |
| 2303 | } |
| 2304 | } |
Dianne Hackborn | a6e4134 | 2012-05-22 16:30:34 -0700 | [diff] [blame] | 2305 | |
Yohei Yukawa | 2bc6617 | 2017-02-08 11:13:25 -0800 | [diff] [blame] | 2306 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO( |
| 2307 | MSG_SET_ACTIVE, 0, 0, mCurClient)); |
Yohei Yukawa | 33e8179 | 2015-11-17 21:14:42 -0800 | [diff] [blame] | 2308 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO( |
| 2309 | MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2310 | mCurClient.sessionRequested = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2311 | mCurClient = null; |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 2312 | mCurActivityViewToScreenMatrix = null; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2313 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2314 | hideInputMethodMenuLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2315 | } |
| 2316 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2317 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2318 | private int getImeShowFlags() { |
| 2319 | int flags = 0; |
| 2320 | if (mShowForced) { |
| 2321 | flags |= InputMethod.SHOW_FORCED |
| 2322 | | InputMethod.SHOW_EXPLICIT; |
| 2323 | } else if (mShowExplicitlyRequested) { |
| 2324 | flags |= InputMethod.SHOW_EXPLICIT; |
| 2325 | } |
| 2326 | return flags; |
| 2327 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2328 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2329 | private int getAppShowFlags() { |
| 2330 | int flags = 0; |
| 2331 | if (mShowForced) { |
| 2332 | flags |= InputMethodManager.SHOW_FORCED; |
| 2333 | } else if (!mShowExplicitlyRequested) { |
| 2334 | flags |= InputMethodManager.SHOW_IMPLICIT; |
| 2335 | } |
| 2336 | return flags; |
| 2337 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2338 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 2339 | @GuardedBy("mMethodMap") |
Yohei Yukawa | 2553e48 | 2017-12-15 15:47:33 -0800 | [diff] [blame] | 2340 | @NonNull |
Yohei Yukawa | dc66e52 | 2018-10-21 10:43:14 -0700 | [diff] [blame] | 2341 | InputBindResult attachNewInputLocked(@StartInputReason int startInputReason, boolean initial) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2342 | if (!mBoundToMethod) { |
| 2343 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 2344 | MSG_BIND_INPUT, mCurMethod, mCurClient.binding)); |
| 2345 | mBoundToMethod = true; |
| 2346 | } |
Yohei Yukawa | 6db3bfe | 2017-02-13 12:04:41 -0800 | [diff] [blame] | 2347 | |
| 2348 | final Binder startInputToken = new Binder(); |
Yohei Yukawa | 7979e1a | 2019-02-12 02:01:10 -0800 | [diff] [blame] | 2349 | final StartInputInfo info = new StartInputInfo(mSettings.getCurrentUserId(), mCurToken, |
| 2350 | mCurTokenDisplayId, mCurId, startInputReason, !initial, |
| 2351 | UserHandle.getUserId(mCurClient.uid), mCurClient.selfReportedDisplayId, |
| 2352 | mCurFocusedWindow, mCurAttribute, mCurFocusedWindowSoftInputMode, mCurSeq); |
Yohei Yukawa | 99e1f6e | 2018-06-22 17:33:19 -0700 | [diff] [blame] | 2353 | mImeTargetWindowMap.put(startInputToken, mCurFocusedWindow); |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 2354 | mStartInputHistory.addEntry(info); |
Yohei Yukawa | 6db3bfe | 2017-02-13 12:04:41 -0800 | [diff] [blame] | 2355 | |
Yohei Yukawa | 724c5bb | 2020-06-10 17:05:17 -0700 | [diff] [blame] | 2356 | // Seems that PackageManagerInternal#grantImplicitAccess() doesn't handle cross-user |
| 2357 | // implicit visibility (e.g. IME[user=10] -> App[user=0]) thus we do this only for the |
| 2358 | // same-user scenarios. |
| 2359 | // That said ignoring cross-user scenario will never affect IMEs that do not have |
| 2360 | // INTERACT_ACROSS_USERS(_FULL) permissions, which is actually almost always the case. |
| 2361 | if (mSettings.getCurrentUserId() == UserHandle.getUserId(mCurClient.uid)) { |
| 2362 | mPackageManagerInternal.grantImplicitAccess(mSettings.getCurrentUserId(), |
| 2363 | null /* intent */, UserHandle.getAppId(mCurMethodUid), mCurClient.uid, true); |
| 2364 | } |
| 2365 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2366 | final SessionState session = mCurClient.curSession; |
Yohei Yukawa | 6db3bfe | 2017-02-13 12:04:41 -0800 | [diff] [blame] | 2367 | executeOrSendMessage(session.method, mCaller.obtainMessageIIOOOO( |
Yohei Yukawa | f7526b5 | 2017-02-11 20:57:10 -0800 | [diff] [blame] | 2368 | MSG_START_INPUT, mCurInputContextMissingMethods, initial ? 0 : 1 /* restarting */, |
Yohei Yukawa | 6db3bfe | 2017-02-13 12:04:41 -0800 | [diff] [blame] | 2369 | startInputToken, session, mCurInputContext, mCurAttribute)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2370 | if (mShowRequested) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2371 | if (DEBUG) Slog.v(TAG, "Attach new input asks to show input"); |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 2372 | showCurrentInputLocked(mCurFocusedWindow, getAppShowFlags(), null, |
| 2373 | SoftInputShowHideReason.ATTACH_NEW_INPUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2374 | } |
Yohei Yukawa | 2553e48 | 2017-12-15 15:47:33 -0800 | [diff] [blame] | 2375 | return new InputBindResult(InputBindResult.ResultCode.SUCCESS_WITH_IME_SESSION, |
| 2376 | session.session, (session.channel != null ? session.channel.dup() : null), |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 2377 | mCurId, mCurSeq, mCurActivityViewToScreenMatrix); |
| 2378 | } |
| 2379 | |
| 2380 | @Nullable |
| 2381 | private Matrix getActivityViewToScreenMatrixLocked(int clientDisplayId, int imeDisplayId) { |
| 2382 | if (clientDisplayId == imeDisplayId) { |
| 2383 | return null; |
| 2384 | } |
| 2385 | int displayId = clientDisplayId; |
| 2386 | Matrix matrix = null; |
| 2387 | while (true) { |
| 2388 | final ActivityViewInfo info = mActivityViewDisplayIdToParentMap.get(displayId); |
| 2389 | if (info == null) { |
| 2390 | return null; |
| 2391 | } |
| 2392 | if (matrix == null) { |
| 2393 | matrix = new Matrix(info.mMatrix); |
| 2394 | } else { |
| 2395 | matrix.postConcat(info.mMatrix); |
| 2396 | } |
| 2397 | if (info.mParentClient.selfReportedDisplayId == imeDisplayId) { |
| 2398 | return matrix; |
| 2399 | } |
| 2400 | displayId = info.mParentClient.selfReportedDisplayId; |
| 2401 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2402 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2403 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 2404 | @GuardedBy("mMethodMap") |
Yohei Yukawa | 2553e48 | 2017-12-15 15:47:33 -0800 | [diff] [blame] | 2405 | @NonNull |
Yohei Yukawa | 35d3f37 | 2015-11-25 11:07:19 -0800 | [diff] [blame] | 2406 | InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext, |
Yohei Yukawa | 35fa6d5 | 2018-10-31 11:33:32 -0700 | [diff] [blame] | 2407 | @MissingMethodFlags int missingMethods, @NonNull EditorInfo attribute, |
| 2408 | @StartInputFlags int startInputFlags, @StartInputReason int startInputReason) { |
Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 2409 | // If no method is currently selected, do nothing. |
| 2410 | if (mCurMethodId == null) { |
Yohei Yukawa | 2553e48 | 2017-12-15 15:47:33 -0800 | [diff] [blame] | 2411 | return InputBindResult.NO_IME; |
Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 2412 | } |
| 2413 | |
Yohei Yukawa | 0deaef0 | 2018-10-17 10:33:48 +0800 | [diff] [blame] | 2414 | if (!mSystemReady) { |
| 2415 | // If the system is not yet ready, we shouldn't be running third |
| 2416 | // party code. |
| 2417 | return new InputBindResult( |
| 2418 | InputBindResult.ResultCode.ERROR_SYSTEM_NOT_READY, |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 2419 | null, null, mCurMethodId, mCurSeq, null); |
Yohei Yukawa | 0deaef0 | 2018-10-17 10:33:48 +0800 | [diff] [blame] | 2420 | } |
| 2421 | |
Yohei Yukawa | d57ba67 | 2015-06-08 16:39:46 -0700 | [diff] [blame] | 2422 | if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid, |
| 2423 | attribute.packageName)) { |
| 2424 | Slog.e(TAG, "Rejecting this client as it reported an invalid package name." |
| 2425 | + " uid=" + cs.uid + " package=" + attribute.packageName); |
Yohei Yukawa | 2553e48 | 2017-12-15 15:47:33 -0800 | [diff] [blame] | 2426 | return InputBindResult.INVALID_PACKAGE_NAME; |
Yohei Yukaw | a0f3ad1 | 2015-04-06 16:48:24 -0700 | [diff] [blame] | 2427 | } |
| 2428 | |
Yohei Yukawa | 0deaef0 | 2018-10-17 10:33:48 +0800 | [diff] [blame] | 2429 | if (!mWindowManagerInternal.isUidAllowedOnDisplay(cs.selfReportedDisplayId, cs.uid)) { |
| 2430 | // Wait, the client no longer has access to the display. |
| 2431 | return InputBindResult.INVALID_DISPLAY_ID; |
| 2432 | } |
lumark | ef1965b | 2018-09-12 17:42:53 +0800 | [diff] [blame] | 2433 | // Compute the final shown display ID with validated cs.selfReportedDisplayId for this |
| 2434 | // session & other conditions. |
Yohei Yukawa | 2988d2d | 2019-01-22 21:29:09 -0800 | [diff] [blame] | 2435 | final int displayIdToShowIme = computeImeDisplayIdForTarget(cs.selfReportedDisplayId, |
| 2436 | mImeDisplayValidator); |
Yohei Yukawa | 0deaef0 | 2018-10-17 10:33:48 +0800 | [diff] [blame] | 2437 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2438 | if (mCurClient != cs) { |
John Spurlock | e098050 | 2013-10-25 11:59:29 -0400 | [diff] [blame] | 2439 | // Was the keyguard locked when switching over to the new client? |
| 2440 | mCurClientInKeyguard = isKeyguardLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2441 | // If the client is changing, we need to switch over to the new |
| 2442 | // one. |
Yohei Yukawa | b752645 | 2018-10-21 20:15:17 -0700 | [diff] [blame] | 2443 | unbindCurrentClientLocked(UnbindReason.SWITCH_CLIENT); |
Yohei Yukawa | 35d3f37 | 2015-11-25 11:07:19 -0800 | [diff] [blame] | 2444 | if (DEBUG) Slog.v(TAG, "switching to client: client=" |
John Spurlock | e098050 | 2013-10-25 11:59:29 -0400 | [diff] [blame] | 2445 | + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2446 | |
| 2447 | // If the screen is on, inform the new client it is active |
Yohei Yukawa | fa6e0a8 | 2015-07-23 15:08:59 -0700 | [diff] [blame] | 2448 | if (mIsInteractive) { |
tiansiming [田思明] | e102c97 | 2018-04-17 18:15:33 +0800 | [diff] [blame] | 2449 | executeOrSendMessage(cs.client, mCaller.obtainMessageIO(MSG_SET_ACTIVE, 1, cs)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2450 | } |
| 2451 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2452 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2453 | // Bump up the sequence for this client and attach it. |
| 2454 | mCurSeq++; |
| 2455 | if (mCurSeq <= 0) mCurSeq = 1; |
| 2456 | mCurClient = cs; |
| 2457 | mCurInputContext = inputContext; |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 2458 | mCurActivityViewToScreenMatrix = |
| 2459 | getActivityViewToScreenMatrixLocked(cs.selfReportedDisplayId, displayIdToShowIme); |
| 2460 | if (cs.selfReportedDisplayId != displayIdToShowIme |
| 2461 | && mCurActivityViewToScreenMatrix == null) { |
Yohei Yukawa | 3d2cc0f | 2019-04-25 18:32:15 -0700 | [diff] [blame] | 2462 | // CursorAnchorInfo API does not work as-is for cross-display scenario. Pretend that |
| 2463 | // InputConnection#requestCursorUpdates() is not implemented in the application so that |
| 2464 | // IMEs will always receive false from this API. |
| 2465 | missingMethods |= MissingMethodFlags.REQUEST_CURSOR_UPDATES; |
| 2466 | } |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 2467 | mCurInputContextMissingMethods = missingMethods; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2468 | mCurAttribute = attribute; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2469 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2470 | // Check if the input method is changing. |
Yohei Yukawa | 4052a10f | 2018-10-15 15:35:55 +0800 | [diff] [blame] | 2471 | // We expect the caller has already verified that the client is allowed to access this |
| 2472 | // display ID. |
Yohei Yukawa | 0deaef0 | 2018-10-17 10:33:48 +0800 | [diff] [blame] | 2473 | if (mCurId != null && mCurId.equals(mCurMethodId) |
| 2474 | && displayIdToShowIme == mCurTokenDisplayId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2475 | if (cs.curSession != null) { |
| 2476 | // Fast case: if we are already connected to the input method, |
| 2477 | // then just return it. |
Yohei Yukawa | 87ca840 | 2017-02-07 00:13:14 -0800 | [diff] [blame] | 2478 | return attachNewInputLocked(startInputReason, |
Yohei Yukawa | 35fa6d5 | 2018-10-31 11:33:32 -0700 | [diff] [blame] | 2479 | (startInputFlags & StartInputFlags.INITIAL_CONNECTION) != 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2480 | } |
| 2481 | if (mHaveConnection) { |
| 2482 | if (mCurMethod != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2483 | // Return to client, and we will get back with it when |
| 2484 | // we have had a session made for it. |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 2485 | requestClientSessionLocked(cs); |
Yohei Yukawa | 2553e48 | 2017-12-15 15:47:33 -0800 | [diff] [blame] | 2486 | return new InputBindResult( |
| 2487 | InputBindResult.ResultCode.SUCCESS_WAITING_IME_SESSION, |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 2488 | null, null, mCurId, mCurSeq, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2489 | } else if (SystemClock.uptimeMillis() |
| 2490 | < (mLastBindTime+TIME_TO_RECONNECT)) { |
| 2491 | // In this case we have connected to the service, but |
| 2492 | // don't yet have its interface. If it hasn't been too |
| 2493 | // long since we did the connection, we'll return to |
| 2494 | // the client and wait to get the service interface so |
| 2495 | // we can report back. If it has been too long, we want |
| 2496 | // to fall through so we can try a disconnect/reconnect |
| 2497 | // to see if we can get back in touch with the service. |
Yohei Yukawa | 2553e48 | 2017-12-15 15:47:33 -0800 | [diff] [blame] | 2498 | return new InputBindResult( |
| 2499 | InputBindResult.ResultCode.SUCCESS_WAITING_IME_BINDING, |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 2500 | null, null, mCurId, mCurSeq, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2501 | } else { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2502 | EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, |
| 2503 | mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2504 | } |
| 2505 | } |
| 2506 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2507 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2508 | InputMethodInfo info = mMethodMap.get(mCurMethodId); |
| 2509 | if (info == null) { |
| 2510 | throw new IllegalArgumentException("Unknown id: " + mCurMethodId); |
| 2511 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2512 | |
Yohei Yukawa | d4d895e | 2018-09-24 16:01:20 -0700 | [diff] [blame] | 2513 | unbindCurrentMethodLocked(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2514 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2515 | mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE); |
| 2516 | mCurIntent.setComponent(info.getComponent()); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 2517 | mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL, |
| 2518 | com.android.internal.R.string.input_method_binding_label); |
| 2519 | mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity( |
| 2520 | mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0)); |
Yohei Yukawa | 4052a10f | 2018-10-15 15:35:55 +0800 | [diff] [blame] | 2521 | |
Yohei Yukawa | f80087c | 2018-05-21 09:47:53 -0700 | [diff] [blame] | 2522 | if (bindCurrentInputMethodServiceLocked(mCurIntent, this, IME_CONNECTION_BIND_FLAGS)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2523 | mLastBindTime = SystemClock.uptimeMillis(); |
| 2524 | mHaveConnection = true; |
| 2525 | mCurId = info.getId(); |
| 2526 | mCurToken = new Binder(); |
Yohei Yukawa | 0deaef0 | 2018-10-17 10:33:48 +0800 | [diff] [blame] | 2527 | mCurTokenDisplayId = displayIdToShowIme; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2528 | try { |
lumark | 90120a8 | 2018-08-15 00:33:03 +0800 | [diff] [blame] | 2529 | if (DEBUG) { |
| 2530 | Slog.v(TAG, "Adding window token: " + mCurToken + " for display: " |
| 2531 | + mCurTokenDisplayId); |
| 2532 | } |
Yohei Yukawa | 6e87559 | 2019-01-28 00:49:30 -0800 | [diff] [blame] | 2533 | mIWindowManager.addWindowToken(mCurToken, LayoutParams.TYPE_INPUT_METHOD, |
| 2534 | mCurTokenDisplayId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2535 | } catch (RemoteException e) { |
| 2536 | } |
Yohei Yukawa | 2553e48 | 2017-12-15 15:47:33 -0800 | [diff] [blame] | 2537 | return new InputBindResult( |
| 2538 | InputBindResult.ResultCode.SUCCESS_WAITING_IME_BINDING, |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 2539 | null, null, mCurId, mCurSeq, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2540 | } |
Yohei Yukawa | 2553e48 | 2017-12-15 15:47:33 -0800 | [diff] [blame] | 2541 | mCurIntent = null; |
| 2542 | Slog.w(TAG, "Failure connecting to input method service: " + mCurIntent); |
| 2543 | return InputBindResult.IME_NOT_CONNECTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2544 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2545 | |
lumark | ef1965b | 2018-09-12 17:42:53 +0800 | [diff] [blame] | 2546 | @FunctionalInterface |
| 2547 | interface ImeDisplayValidator { |
| 2548 | boolean displayCanShowIme(int displayId); |
| 2549 | } |
| 2550 | |
| 2551 | /** |
| 2552 | * Find the display where the IME should be shown. |
| 2553 | * |
| 2554 | * @param displayId the ID of the display where the IME client target is. |
lumark | ef1965b | 2018-09-12 17:42:53 +0800 | [diff] [blame] | 2555 | * @param checker instance of {@link ImeDisplayValidator} which is used for |
| 2556 | * checking display config to adjust the final target display. |
| 2557 | * @return The ID of the display where the IME should be shown. |
| 2558 | */ |
Yohei Yukawa | 2988d2d | 2019-01-22 21:29:09 -0800 | [diff] [blame] | 2559 | static int computeImeDisplayIdForTarget(int displayId, @NonNull ImeDisplayValidator checker) { |
lumark | ef1965b | 2018-09-12 17:42:53 +0800 | [diff] [blame] | 2560 | if (displayId == DEFAULT_DISPLAY || displayId == INVALID_DISPLAY) { |
lumark | 7570cac | 2019-03-07 22:14:38 +0800 | [diff] [blame] | 2561 | return FALLBACK_DISPLAY_ID; |
lumark | ef1965b | 2018-09-12 17:42:53 +0800 | [diff] [blame] | 2562 | } |
lumark | 9a72d22 | 2019-03-30 18:31:45 +0800 | [diff] [blame] | 2563 | |
| 2564 | // Show IME window on fallback display when the display doesn't support system decorations |
| 2565 | // or the display is virtual and isn't owned by system for security concern. |
lumark | 7570cac | 2019-03-07 22:14:38 +0800 | [diff] [blame] | 2566 | return checker.displayCanShowIme(displayId) ? displayId : FALLBACK_DISPLAY_ID; |
lumark | ef1965b | 2018-09-12 17:42:53 +0800 | [diff] [blame] | 2567 | } |
| 2568 | |
Yohei Yukawa | d5f402b | 2020-05-15 10:23:32 -0700 | [diff] [blame] | 2569 | @AnyThread |
| 2570 | private void scheduleNotifyImeUidToAudioService(int uid) { |
| 2571 | mCaller.removeMessages(MSG_NOTIFY_IME_UID_TO_AUDIO_SERVICE); |
| 2572 | mCaller.obtainMessageI(MSG_NOTIFY_IME_UID_TO_AUDIO_SERVICE, uid).sendToTarget(); |
| 2573 | } |
| 2574 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 2575 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2576 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 2577 | synchronized (mMethodMap) { |
| 2578 | if (mCurIntent != null && name.equals(mCurIntent.getComponent())) { |
| 2579 | mCurMethod = IInputMethod.Stub.asInterface(service); |
Yohei Yukawa | d5f402b | 2020-05-15 10:23:32 -0700 | [diff] [blame] | 2580 | final String curMethodPackage = mCurIntent.getComponent().getPackageName(); |
| 2581 | final int curMethodUid = mPackageManagerInternal.getPackageUidInternal( |
| 2582 | curMethodPackage, 0 /* flags */, mSettings.getCurrentUserId()); |
| 2583 | if (curMethodUid < 0) { |
| 2584 | Slog.e(TAG, "Failed to get UID for package=" + curMethodPackage); |
| 2585 | mCurMethodUid = Process.INVALID_UID; |
| 2586 | } else { |
| 2587 | mCurMethodUid = curMethodUid; |
| 2588 | } |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 2589 | if (mCurToken == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2590 | Slog.w(TAG, "Service connected without a token!"); |
Yohei Yukawa | d4d895e | 2018-09-24 16:01:20 -0700 | [diff] [blame] | 2591 | unbindCurrentMethodLocked(); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 2592 | return; |
| 2593 | } |
Craig Mautner | e4bbb1c | 2013-03-15 11:38:44 -0700 | [diff] [blame] | 2594 | if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken); |
lumark | 90120a8 | 2018-08-15 00:33:03 +0800 | [diff] [blame] | 2595 | // Dispatch display id for InputMethodService to update context display. |
| 2596 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO( |
| 2597 | MSG_INITIALIZE_IME, mCurTokenDisplayId, mCurMethod, mCurToken)); |
Yohei Yukawa | d5f402b | 2020-05-15 10:23:32 -0700 | [diff] [blame] | 2598 | scheduleNotifyImeUidToAudioService(mCurMethodUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2599 | if (mCurClient != null) { |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 2600 | clearClientSessionLocked(mCurClient); |
| 2601 | requestClientSessionLocked(mCurClient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2602 | } |
| 2603 | } |
| 2604 | } |
| 2605 | } |
| 2606 | |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 2607 | void onSessionCreated(IInputMethod method, IInputMethodSession session, |
| 2608 | InputChannel channel) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2609 | synchronized (mMethodMap) { |
Yohei Yukawa | 32807ad | 2020-03-04 12:27:29 -0800 | [diff] [blame] | 2610 | if (mUserSwitchHandlerTask != null) { |
| 2611 | // We have a pending user-switching task so it's better to just ignore this session. |
| 2612 | channel.dispose(); |
| 2613 | return; |
| 2614 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2615 | if (mCurMethod != null && method != null |
| 2616 | && mCurMethod.asBinder() == method.asBinder()) { |
| 2617 | if (mCurClient != null) { |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 2618 | clearClientSessionLocked(mCurClient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2619 | mCurClient.curSession = new SessionState(mCurClient, |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 2620 | method, session, channel); |
Yohei Yukawa | 87ca840 | 2017-02-07 00:13:14 -0800 | [diff] [blame] | 2621 | InputBindResult res = attachNewInputLocked( |
Yohei Yukawa | 4219422 | 2018-10-21 20:14:40 -0700 | [diff] [blame] | 2622 | StartInputReason.SESSION_CREATED_BY_IME, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2623 | if (res.method != null) { |
| 2624 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO( |
Yohei Yukawa | 33e8179 | 2015-11-17 21:14:42 -0800 | [diff] [blame] | 2625 | MSG_BIND_CLIENT, mCurClient.client, res)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2626 | } |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 2627 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2628 | } |
| 2629 | } |
| 2630 | } |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 2631 | |
| 2632 | // Session abandoned. Close its associated input channel. |
| 2633 | channel.dispose(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2634 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2635 | |
Yohei Yukawa | d4d895e | 2018-09-24 16:01:20 -0700 | [diff] [blame] | 2636 | void unbindCurrentMethodLocked() { |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 2637 | if (mVisibleBound) { |
| 2638 | mContext.unbindService(mVisibleConnection); |
| 2639 | mVisibleBound = false; |
| 2640 | } |
| 2641 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2642 | if (mHaveConnection) { |
| 2643 | mContext.unbindService(this); |
| 2644 | mHaveConnection = false; |
| 2645 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2646 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2647 | if (mCurToken != null) { |
| 2648 | try { |
lumark | 90120a8 | 2018-08-15 00:33:03 +0800 | [diff] [blame] | 2649 | if (DEBUG) { |
| 2650 | Slog.v(TAG, "Removing window token: " + mCurToken + " for display: " |
| 2651 | + mCurTokenDisplayId); |
| 2652 | } |
lumark | 90120a8 | 2018-08-15 00:33:03 +0800 | [diff] [blame] | 2653 | mIWindowManager.removeWindowToken(mCurToken, mCurTokenDisplayId); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2654 | } catch (RemoteException e) { |
| 2655 | } |
lumark | 7570cac | 2019-03-07 22:14:38 +0800 | [diff] [blame] | 2656 | // Set IME window status as invisible when unbind current method. |
| 2657 | mImeWindowVis = 0; |
| 2658 | mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT; |
| 2659 | updateSystemUiLocked(mImeWindowVis, mBackDisposition); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2660 | mCurToken = null; |
lumark | 90120a8 | 2018-08-15 00:33:03 +0800 | [diff] [blame] | 2661 | mCurTokenDisplayId = INVALID_DISPLAY; |
lpeter | 133fce0 | 2020-03-05 20:32:16 +0800 | [diff] [blame] | 2662 | mCurHostInputToken = null; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2663 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2664 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2665 | mCurId = null; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2666 | clearCurMethodLocked(); |
Yohei Yukawa | bc7b526 | 2015-11-17 17:38:41 -0800 | [diff] [blame] | 2667 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2668 | |
Yohei Yukawa | 4afd933 | 2018-10-21 10:43:53 -0700 | [diff] [blame] | 2669 | void resetCurrentMethodAndClient(@UnbindReason int unbindClientReason) { |
Yohei Yukawa | bc7b526 | 2015-11-17 17:38:41 -0800 | [diff] [blame] | 2670 | mCurMethodId = null; |
Yohei Yukawa | d4d895e | 2018-09-24 16:01:20 -0700 | [diff] [blame] | 2671 | unbindCurrentMethodLocked(); |
Yohei Yukawa | 33e8179 | 2015-11-17 21:14:42 -0800 | [diff] [blame] | 2672 | unbindCurrentClientLocked(unbindClientReason); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2673 | } |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 2674 | |
| 2675 | void requestClientSessionLocked(ClientState cs) { |
| 2676 | if (!cs.sessionRequested) { |
| 2677 | if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs); |
| 2678 | InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString()); |
| 2679 | cs.sessionRequested = true; |
| 2680 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO( |
| 2681 | MSG_CREATE_SESSION, mCurMethod, channels[1], |
| 2682 | new MethodCallback(this, mCurMethod, channels[0]))); |
| 2683 | } |
| 2684 | } |
| 2685 | |
| 2686 | void clearClientSessionLocked(ClientState cs) { |
| 2687 | finishSessionLocked(cs.curSession); |
| 2688 | cs.curSession = null; |
| 2689 | cs.sessionRequested = false; |
| 2690 | } |
| 2691 | |
| 2692 | private void finishSessionLocked(SessionState sessionState) { |
| 2693 | if (sessionState != null) { |
| 2694 | if (sessionState.session != null) { |
| 2695 | try { |
| 2696 | sessionState.session.finishSession(); |
| 2697 | } catch (RemoteException e) { |
| 2698 | Slog.w(TAG, "Session failed to close due to remote exception", e); |
Yohei Yukawa | 849443c | 2019-01-21 09:02:25 -0800 | [diff] [blame] | 2699 | updateSystemUiLocked(0 /* vis */, mBackDisposition); |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 2700 | } |
| 2701 | sessionState.session = null; |
| 2702 | } |
| 2703 | if (sessionState.channel != null) { |
| 2704 | sessionState.channel.dispose(); |
| 2705 | sessionState.channel = null; |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 2706 | } |
| 2707 | } |
| 2708 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2709 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2710 | void clearCurMethodLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2711 | if (mCurMethod != null) { |
Yohei Yukawa | ac9311e | 2018-11-20 19:25:23 -0800 | [diff] [blame] | 2712 | final int numClients = mClients.size(); |
| 2713 | for (int i = 0; i < numClients; ++i) { |
| 2714 | clearClientSessionLocked(mClients.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2715 | } |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 2716 | |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 2717 | finishSessionLocked(mEnabledSession); |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 2718 | mEnabledSession = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2719 | mCurMethod = null; |
Yohei Yukawa | d5f402b | 2020-05-15 10:23:32 -0700 | [diff] [blame] | 2720 | mCurMethodUid = Process.INVALID_UID; |
| 2721 | scheduleNotifyImeUidToAudioService(mCurMethodUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2722 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 2723 | if (mStatusBar != null) { |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 2724 | mStatusBar.setIconVisibility(mSlotIme, false); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 2725 | } |
Yohei Yukawa | 2bc6617 | 2017-02-08 11:13:25 -0800 | [diff] [blame] | 2726 | mInFullscreenMode = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2727 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2728 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 2729 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2730 | public void onServiceDisconnected(ComponentName name) { |
Yohei Yukawa | 817d5f7 | 2017-01-04 20:15:02 -0800 | [diff] [blame] | 2731 | // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the |
| 2732 | // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed), |
| 2733 | // which is irregular but can eventually happen for everyone just by continuing using the |
| 2734 | // device. Thus it is important to make sure that all the internal states are properly |
| 2735 | // refreshed when this method is called back. Running |
| 2736 | // adb install -r <APK that implements the current IME> |
| 2737 | // would be a good way to trigger such a situation. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2738 | synchronized (mMethodMap) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2739 | if (DEBUG) Slog.v(TAG, "Service disconnected: " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2740 | + " mCurIntent=" + mCurIntent); |
| 2741 | if (mCurMethod != null && mCurIntent != null |
| 2742 | && name.equals(mCurIntent.getComponent())) { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2743 | clearCurMethodLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2744 | // We consider this to be a new bind attempt, since the system |
| 2745 | // should now try to restart the service for us. |
| 2746 | mLastBindTime = SystemClock.uptimeMillis(); |
| 2747 | mShowRequested = mInputShown; |
| 2748 | mInputShown = false; |
Yohei Yukawa | b752645 | 2018-10-21 20:15:17 -0700 | [diff] [blame] | 2749 | unbindCurrentClientLocked(UnbindReason.DISCONNECT_IME); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2750 | } |
| 2751 | } |
| 2752 | } |
| 2753 | |
Yohei Yukawa | eec552e | 2018-09-09 20:48:41 -0700 | [diff] [blame] | 2754 | @BinderThread |
Yohei Yukawa | 41b094f | 2018-09-09 23:58:45 -0700 | [diff] [blame] | 2755 | private void updateStatusIcon(@NonNull IBinder token, String packageName, |
| 2756 | @DrawableRes int iconId) { |
Yohei Yukawa | 59377ca | 2017-01-31 21:32:26 -0800 | [diff] [blame] | 2757 | synchronized (mMethodMap) { |
Yohei Yukawa | 79e4c6a | 2018-12-09 19:07:17 -0800 | [diff] [blame] | 2758 | if (!calledWithValidTokenLocked(token)) { |
Yohei Yukawa | 59377ca | 2017-01-31 21:32:26 -0800 | [diff] [blame] | 2759 | return; |
| 2760 | } |
| 2761 | final long ident = Binder.clearCallingIdentity(); |
| 2762 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2763 | if (iconId == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2764 | if (DEBUG) Slog.d(TAG, "hide the small icon for the input method"); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 2765 | if (mStatusBar != null) { |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 2766 | mStatusBar.setIconVisibility(mSlotIme, false); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 2767 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2768 | } else if (packageName != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2769 | if (DEBUG) Slog.d(TAG, "show a small icon for the input method"); |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 2770 | CharSequence contentDescription = null; |
| 2771 | try { |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2772 | // Use PackageManager to load label |
| 2773 | final PackageManager packageManager = mContext.getPackageManager(); |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 2774 | contentDescription = packageManager.getApplicationLabel( |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2775 | mIPackageManager.getApplicationInfo(packageName, 0, |
| 2776 | mSettings.getCurrentUserId())); |
| 2777 | } catch (RemoteException e) { |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 2778 | /* ignore */ |
| 2779 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 2780 | if (mStatusBar != null) { |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 2781 | mStatusBar.setIcon(mSlotIme, packageName, iconId, 0, |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 2782 | contentDescription != null |
| 2783 | ? contentDescription.toString() : null); |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 2784 | mStatusBar.setIconVisibility(mSlotIme, true); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 2785 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2786 | } |
Yohei Yukawa | 59377ca | 2017-01-31 21:32:26 -0800 | [diff] [blame] | 2787 | } finally { |
| 2788 | Binder.restoreCallingIdentity(ident); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2789 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2790 | } |
| 2791 | } |
| 2792 | |
Seigo Nonaka | d9eb911 | 2015-05-26 20:54:43 +0900 | [diff] [blame] | 2793 | private boolean shouldShowImeSwitcherLocked(int visibility) { |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 2794 | if (!mShowOngoingImeSwitcherForPhones) return false; |
Jason Monk | 807ef76 | 2014-05-08 15:47:46 -0400 | [diff] [blame] | 2795 | if (mSwitchingDialog != null) return false; |
Yohei Yukawa | d2bc309 | 2017-07-31 15:37:14 -0700 | [diff] [blame] | 2796 | if (mWindowManagerInternal.isKeyguardShowingAndNotOccluded() |
| 2797 | && mKeyguardManager != null && mKeyguardManager.isKeyguardSecure()) return false; |
Tarandeep Singh | eadb139 | 2018-11-09 18:15:57 +0100 | [diff] [blame] | 2798 | if ((visibility & InputMethodService.IME_ACTIVE) == 0 |
| 2799 | || (visibility & InputMethodService.IME_INVISIBLE) != 0) { |
| 2800 | return false; |
| 2801 | } |
Seigo Nonaka | 7309b12 | 2015-08-17 18:34:13 -0700 | [diff] [blame] | 2802 | if (mWindowManagerInternal.isHardKeyboardAvailable()) { |
Yohei Yukawa | af023f2 | 2019-06-21 16:38:22 -0700 | [diff] [blame] | 2803 | // When physical keyboard is attached, we show the ime switcher (or notification if |
| 2804 | // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently |
| 2805 | // exists in the IME switcher dialog. Might be OK to remove this condition once |
| 2806 | // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live. |
| 2807 | return true; |
Yohei Yukawa | 8939838 | 2016-03-29 11:37:04 -0700 | [diff] [blame] | 2808 | } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) { |
| 2809 | return false; |
Yohei Yukawa | c6c7cd2 | 2015-05-12 15:00:33 -0700 | [diff] [blame] | 2810 | } |
Yohei Yukawa | c6c7cd2 | 2015-05-12 15:00:33 -0700 | [diff] [blame] | 2811 | |
| 2812 | List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked(); |
| 2813 | final int N = imis.size(); |
| 2814 | if (N > 2) return true; |
| 2815 | if (N < 1) return false; |
| 2816 | int nonAuxCount = 0; |
| 2817 | int auxCount = 0; |
| 2818 | InputMethodSubtype nonAuxSubtype = null; |
| 2819 | InputMethodSubtype auxSubtype = null; |
| 2820 | for(int i = 0; i < N; ++i) { |
| 2821 | final InputMethodInfo imi = imis.get(i); |
| 2822 | final List<InputMethodSubtype> subtypes = |
| 2823 | mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true); |
| 2824 | final int subtypeCount = subtypes.size(); |
| 2825 | if (subtypeCount == 0) { |
| 2826 | ++nonAuxCount; |
| 2827 | } else { |
| 2828 | for (int j = 0; j < subtypeCount; ++j) { |
| 2829 | final InputMethodSubtype subtype = subtypes.get(j); |
| 2830 | if (!subtype.isAuxiliary()) { |
| 2831 | ++nonAuxCount; |
| 2832 | nonAuxSubtype = subtype; |
| 2833 | } else { |
| 2834 | ++auxCount; |
| 2835 | auxSubtype = subtype; |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 2836 | } |
| 2837 | } |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 2838 | } |
| 2839 | } |
Yohei Yukawa | c6c7cd2 | 2015-05-12 15:00:33 -0700 | [diff] [blame] | 2840 | if (nonAuxCount > 1 || auxCount > 1) { |
| 2841 | return true; |
| 2842 | } else if (nonAuxCount == 1 && auxCount == 1) { |
| 2843 | if (nonAuxSubtype != null && auxSubtype != null |
| 2844 | && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale()) |
| 2845 | || auxSubtype.overridesImplicitlyEnabledSubtype() |
| 2846 | || nonAuxSubtype.overridesImplicitlyEnabledSubtype()) |
| 2847 | && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) { |
| 2848 | return false; |
| 2849 | } |
| 2850 | return true; |
| 2851 | } |
| 2852 | return false; |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 2853 | } |
| 2854 | |
John Spurlock | e098050 | 2013-10-25 11:59:29 -0400 | [diff] [blame] | 2855 | private boolean isKeyguardLocked() { |
| 2856 | return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked(); |
| 2857 | } |
| 2858 | |
Yohei Yukawa | d6475a6 | 2017-04-17 10:35:27 -0700 | [diff] [blame] | 2859 | @BinderThread |
satok | dbf2950 | 2011-08-25 15:28:23 +0900 | [diff] [blame] | 2860 | @SuppressWarnings("deprecation") |
Yohei Yukawa | d3e0fd93 | 2019-01-21 09:16:48 -0800 | [diff] [blame] | 2861 | private void setImeWindowStatus(@NonNull IBinder token, int vis, int backDisposition) { |
lumark | 7570cac | 2019-03-07 22:14:38 +0800 | [diff] [blame] | 2862 | final int topFocusedDisplayId = mWindowManagerInternal.getTopFocusedDisplayId(); |
| 2863 | |
Seigo Nonaka | d9eb911 | 2015-05-26 20:54:43 +0900 | [diff] [blame] | 2864 | synchronized (mMethodMap) { |
Yohei Yukawa | 79e4c6a | 2018-12-09 19:07:17 -0800 | [diff] [blame] | 2865 | if (!calledWithValidTokenLocked(token)) { |
| 2866 | return; |
| 2867 | } |
lumark | 7570cac | 2019-03-07 22:14:38 +0800 | [diff] [blame] | 2868 | // Skip update IME status when current token display is not same as focused display. |
| 2869 | // Note that we still need to update IME status when focusing external display |
| 2870 | // that does not support system decoration and fallback to show IME on default |
| 2871 | // display since it is intentional behavior. |
| 2872 | if (mCurTokenDisplayId != topFocusedDisplayId |
| 2873 | && mCurTokenDisplayId != FALLBACK_DISPLAY_ID) { |
| 2874 | return; |
| 2875 | } |
Seigo Nonaka | d9eb911 | 2015-05-26 20:54:43 +0900 | [diff] [blame] | 2876 | mImeWindowVis = vis; |
| 2877 | mBackDisposition = backDisposition; |
Yohei Yukawa | 849443c | 2019-01-21 09:02:25 -0800 | [diff] [blame] | 2878 | updateSystemUiLocked(vis, backDisposition); |
Seigo Nonaka | d9eb911 | 2015-05-26 20:54:43 +0900 | [diff] [blame] | 2879 | } |
Yohei Yukawa | d6475a6 | 2017-04-17 10:35:27 -0700 | [diff] [blame] | 2880 | |
| 2881 | final boolean dismissImeOnBackKeyPressed; |
| 2882 | switch (backDisposition) { |
| 2883 | case InputMethodService.BACK_DISPOSITION_WILL_DISMISS: |
| 2884 | dismissImeOnBackKeyPressed = true; |
| 2885 | break; |
| 2886 | case InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS: |
| 2887 | dismissImeOnBackKeyPressed = false; |
| 2888 | break; |
| 2889 | default: |
| 2890 | case InputMethodService.BACK_DISPOSITION_DEFAULT: |
| 2891 | dismissImeOnBackKeyPressed = ((vis & InputMethodService.IME_VISIBLE) != 0); |
| 2892 | break; |
| 2893 | } |
Yohei Yukawa | ee2a7ed | 2017-02-15 21:38:57 -0800 | [diff] [blame] | 2894 | mWindowManagerInternal.updateInputMethodWindowStatus(token, |
Yohei Yukawa | 99e1f6e | 2018-06-22 17:33:19 -0700 | [diff] [blame] | 2895 | (vis & InputMethodService.IME_VISIBLE) != 0, dismissImeOnBackKeyPressed); |
Seigo Nonaka | d9eb911 | 2015-05-26 20:54:43 +0900 | [diff] [blame] | 2896 | } |
| 2897 | |
Yohei Yukawa | 99e1f6e | 2018-06-22 17:33:19 -0700 | [diff] [blame] | 2898 | @BinderThread |
Yohei Yukawa | d3e0fd93 | 2019-01-21 09:16:48 -0800 | [diff] [blame] | 2899 | private void reportStartInput(@NonNull IBinder token, IBinder startInputToken) { |
Yohei Yukawa | 99e1f6e | 2018-06-22 17:33:19 -0700 | [diff] [blame] | 2900 | synchronized (mMethodMap) { |
Yohei Yukawa | 79e4c6a | 2018-12-09 19:07:17 -0800 | [diff] [blame] | 2901 | if (!calledWithValidTokenLocked(token)) { |
| 2902 | return; |
| 2903 | } |
Yohei Yukawa | 99e1f6e | 2018-06-22 17:33:19 -0700 | [diff] [blame] | 2904 | final IBinder targetWindow = mImeTargetWindowMap.get(startInputToken); |
| 2905 | if (targetWindow != null && mLastImeTargetWindow != targetWindow) { |
| 2906 | mWindowManagerInternal.updateInputMethodTargetWindow(token, targetWindow); |
| 2907 | } |
| 2908 | mLastImeTargetWindow = targetWindow; |
| 2909 | } |
| 2910 | } |
| 2911 | |
Seigo Nonaka | d9eb911 | 2015-05-26 20:54:43 +0900 | [diff] [blame] | 2912 | // Caution! This method is called in this class. Handle multi-user carefully |
Yohei Yukawa | 849443c | 2019-01-21 09:02:25 -0800 | [diff] [blame] | 2913 | private void updateSystemUiLocked(int vis, int backDisposition) { |
| 2914 | if (mCurToken == null) { |
Seigo Nonaka | d9eb911 | 2015-05-26 20:54:43 +0900 | [diff] [blame] | 2915 | return; |
| 2916 | } |
Tarandeep Singh | eadb139 | 2018-11-09 18:15:57 +0100 | [diff] [blame] | 2917 | if (DEBUG) { |
| 2918 | Slog.d(TAG, "IME window vis: " + vis |
| 2919 | + " active: " + (vis & InputMethodService.IME_ACTIVE) |
lumark | 7570cac | 2019-03-07 22:14:38 +0800 | [diff] [blame] | 2920 | + " inv: " + (vis & InputMethodService.IME_INVISIBLE) |
| 2921 | + " displayId: " + mCurTokenDisplayId); |
Tarandeep Singh | eadb139 | 2018-11-09 18:15:57 +0100 | [diff] [blame] | 2922 | } |
| 2923 | |
Seigo Nonaka | d9eb911 | 2015-05-26 20:54:43 +0900 | [diff] [blame] | 2924 | // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure |
| 2925 | // all updateSystemUi happens on system previlege. |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2926 | final long ident = Binder.clearCallingIdentity(); |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 2927 | try { |
Seigo Nonaka | d9eb911 | 2015-05-26 20:54:43 +0900 | [diff] [blame] | 2928 | // apply policy for binder calls |
| 2929 | if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) { |
| 2930 | vis = 0; |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 2931 | } |
Seigo Nonaka | d9eb911 | 2015-05-26 20:54:43 +0900 | [diff] [blame] | 2932 | // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked(). |
| 2933 | final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis); |
| 2934 | if (mStatusBar != null) { |
lumark | 7570cac | 2019-03-07 22:14:38 +0800 | [diff] [blame] | 2935 | mStatusBar.setImeWindowStatus(mCurTokenDisplayId, mCurToken, vis, backDisposition, |
Tarandeep Singh | 07b318b | 2019-07-17 11:12:04 -0700 | [diff] [blame] | 2936 | needsToShowImeSwitcher, false /*isMultiClientImeEnabled*/); |
Seigo Nonaka | d9eb911 | 2015-05-26 20:54:43 +0900 | [diff] [blame] | 2937 | } |
| 2938 | final InputMethodInfo imi = mMethodMap.get(mCurMethodId); |
| 2939 | if (imi != null && needsToShowImeSwitcher) { |
| 2940 | // Used to load label |
| 2941 | final CharSequence title = mRes.getText( |
| 2942 | com.android.internal.R.string.select_input_method); |
| 2943 | final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName( |
| 2944 | mContext, imi, mCurrentSubtype); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 2945 | mImeSwitcherNotification.setContentTitle(title) |
| 2946 | .setContentText(summary) |
| 2947 | .setContentIntent(mImeSwitchPendingIntent); |
Seigo Nonaka | 7309b12 | 2015-08-17 18:34:13 -0700 | [diff] [blame] | 2948 | try { |
Charles Chen | ea6e7f0 | 2018-11-19 21:37:45 +0800 | [diff] [blame] | 2949 | // TODO(b/120076400): Figure out what is the best behavior |
Seigo Nonaka | 7309b12 | 2015-08-17 18:34:13 -0700 | [diff] [blame] | 2950 | if ((mNotificationManager != null) |
Charles Chen | ea6e7f0 | 2018-11-19 21:37:45 +0800 | [diff] [blame] | 2951 | && !mIWindowManager.hasNavigationBar(DEFAULT_DISPLAY)) { |
Seigo Nonaka | 7309b12 | 2015-08-17 18:34:13 -0700 | [diff] [blame] | 2952 | if (DEBUG) { |
| 2953 | Slog.d(TAG, "--- show notification: label = " + summary); |
| 2954 | } |
| 2955 | mNotificationManager.notifyAsUser(null, |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 2956 | SystemMessage.NOTE_SELECT_INPUT_METHOD, |
Seigo Nonaka | 7309b12 | 2015-08-17 18:34:13 -0700 | [diff] [blame] | 2957 | mImeSwitcherNotification.build(), UserHandle.ALL); |
| 2958 | mNotificationShown = true; |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 2959 | } |
Seigo Nonaka | 7309b12 | 2015-08-17 18:34:13 -0700 | [diff] [blame] | 2960 | } catch (RemoteException e) { |
Seigo Nonaka | d9eb911 | 2015-05-26 20:54:43 +0900 | [diff] [blame] | 2961 | } |
| 2962 | } else { |
| 2963 | if (mNotificationShown && mNotificationManager != null) { |
| 2964 | if (DEBUG) { |
| 2965 | Slog.d(TAG, "--- hide notification"); |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 2966 | } |
Seigo Nonaka | d9eb911 | 2015-05-26 20:54:43 +0900 | [diff] [blame] | 2967 | mNotificationManager.cancelAsUser(null, |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 2968 | SystemMessage.NOTE_SELECT_INPUT_METHOD, UserHandle.ALL); |
Seigo Nonaka | d9eb911 | 2015-05-26 20:54:43 +0900 | [diff] [blame] | 2969 | mNotificationShown = false; |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 2970 | } |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 2971 | } |
| 2972 | } finally { |
| 2973 | Binder.restoreCallingIdentity(ident); |
| 2974 | } |
| 2975 | } |
| 2976 | |
Dianne Hackborn | fd7aded | 2013-01-22 17:10:23 -0800 | [diff] [blame] | 2977 | void updateFromSettingsLocked(boolean enabledMayChange) { |
Michael Wright | 7b5a96b | 2014-08-09 19:28:42 -0700 | [diff] [blame] | 2978 | updateInputMethodsFromSettingsLocked(enabledMayChange); |
| 2979 | updateKeyboardFromSettingsLocked(); |
| 2980 | } |
| 2981 | |
| 2982 | void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) { |
Dianne Hackborn | fd7aded | 2013-01-22 17:10:23 -0800 | [diff] [blame] | 2983 | if (enabledMayChange) { |
| 2984 | List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked(); |
| 2985 | for (int i=0; i<enabled.size(); i++) { |
| 2986 | // We allow the user to select "disabled until used" apps, so if they |
| 2987 | // are enabling one of those here we now need to make it enabled. |
| 2988 | InputMethodInfo imm = enabled.get(i); |
| 2989 | try { |
| 2990 | ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(), |
| 2991 | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS, |
| 2992 | mSettings.getCurrentUserId()); |
Satoshi Kataoka | 7987a31 | 2013-04-17 18:59:33 +0900 | [diff] [blame] | 2993 | if (ai != null && ai.enabledSetting |
Dianne Hackborn | fd7aded | 2013-01-22 17:10:23 -0800 | [diff] [blame] | 2994 | == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) { |
Satoshi Kataoka | ed1cdb2 | 2013-04-17 16:41:58 +0900 | [diff] [blame] | 2995 | if (DEBUG) { |
| 2996 | Slog.d(TAG, "Update state(" + imm.getId() |
| 2997 | + "): DISABLED_UNTIL_USED -> DEFAULT"); |
| 2998 | } |
Dianne Hackborn | fd7aded | 2013-01-22 17:10:23 -0800 | [diff] [blame] | 2999 | mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(), |
| 3000 | PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, |
Dianne Hackborn | 3fa3c28a | 2013-03-26 16:15:41 -0700 | [diff] [blame] | 3001 | PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(), |
| 3002 | mContext.getBasePackageName()); |
Dianne Hackborn | fd7aded | 2013-01-22 17:10:23 -0800 | [diff] [blame] | 3003 | } |
| 3004 | } catch (RemoteException e) { |
| 3005 | } |
| 3006 | } |
| 3007 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3008 | // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and |
| 3009 | // ENABLED_INPUT_METHODS is taking care of keeping them correctly in |
| 3010 | // sync, so we will never have a DEFAULT_INPUT_METHOD that is not |
| 3011 | // enabled. |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3012 | String id = mSettings.getSelectedInputMethod(); |
satok | 03eb319a | 2010-11-11 18:17:42 +0900 | [diff] [blame] | 3013 | // There is no input method selected, try to choose new applicable input method. |
| 3014 | if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) { |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3015 | id = mSettings.getSelectedInputMethod(); |
satok | 03eb319a | 2010-11-11 18:17:42 +0900 | [diff] [blame] | 3016 | } |
| 3017 | if (!TextUtils.isEmpty(id)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3018 | try { |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 3019 | setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3020 | } catch (IllegalArgumentException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3021 | Slog.w(TAG, "Unknown input method from prefs: " + id, e); |
Yohei Yukawa | b752645 | 2018-10-21 20:15:17 -0700 | [diff] [blame] | 3022 | resetCurrentMethodAndClient(UnbindReason.SWITCH_IME_FAILED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3023 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3024 | } else { |
| 3025 | // There is no longer an input method set, so stop any current one. |
Yohei Yukawa | b752645 | 2018-10-21 20:15:17 -0700 | [diff] [blame] | 3026 | resetCurrentMethodAndClient(UnbindReason.NO_IME); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3027 | } |
Yohei Yukawa | 3d46bab | 2014-05-30 18:10:18 +0900 | [diff] [blame] | 3028 | // Here is not the perfect place to reset the switching controller. Ideally |
| 3029 | // mSwitchingController and mSettings should be able to share the same state. |
| 3030 | // TODO: Make sure that mSwitchingController and mSettings are sharing the |
| 3031 | // the same enabled IMEs list. |
| 3032 | mSwitchingController.resetCircularListLocked(mContext); |
Michael Wright | 7b5a96b | 2014-08-09 19:28:42 -0700 | [diff] [blame] | 3033 | |
| 3034 | } |
| 3035 | |
| 3036 | public void updateKeyboardFromSettingsLocked() { |
| 3037 | mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled(); |
| 3038 | if (mSwitchingDialog != null |
| 3039 | && mSwitchingDialogTitleView != null |
| 3040 | && mSwitchingDialog.isShowing()) { |
| 3041 | final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById( |
| 3042 | com.android.internal.R.id.hard_keyboard_switch); |
| 3043 | hardKeySwitch.setChecked(mShowImeWithHardKeyboard); |
| 3044 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3045 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3046 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 3047 | /* package */ void setInputMethodLocked(String id, int subtypeId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3048 | InputMethodInfo info = mMethodMap.get(id); |
| 3049 | if (info == null) { |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 3050 | throw new IllegalArgumentException("Unknown id: " + id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3051 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3052 | |
satok | d81e950 | 2012-05-21 12:58:45 +0900 | [diff] [blame] | 3053 | // See if we need to notify a subtype change within the same IME. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3054 | if (id.equals(mCurMethodId)) { |
satok | d81e950 | 2012-05-21 12:58:45 +0900 | [diff] [blame] | 3055 | final int subtypeCount = info.getSubtypeCount(); |
| 3056 | if (subtypeCount <= 0) { |
| 3057 | return; |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 3058 | } |
satok | d81e950 | 2012-05-21 12:58:45 +0900 | [diff] [blame] | 3059 | final InputMethodSubtype oldSubtype = mCurrentSubtype; |
| 3060 | final InputMethodSubtype newSubtype; |
| 3061 | if (subtypeId >= 0 && subtypeId < subtypeCount) { |
| 3062 | newSubtype = info.getSubtypeAt(subtypeId); |
| 3063 | } else { |
| 3064 | // If subtype is null, try to find the most applicable one from |
| 3065 | // getCurrentInputMethodSubtype. |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3066 | newSubtype = getCurrentInputMethodSubtypeLocked(); |
satok | d81e950 | 2012-05-21 12:58:45 +0900 | [diff] [blame] | 3067 | } |
| 3068 | if (newSubtype == null || oldSubtype == null) { |
| 3069 | Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype |
| 3070 | + ", new subtype = " + newSubtype); |
| 3071 | return; |
| 3072 | } |
| 3073 | if (newSubtype != oldSubtype) { |
| 3074 | setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true); |
| 3075 | if (mCurMethod != null) { |
| 3076 | try { |
Yohei Yukawa | 849443c | 2019-01-21 09:02:25 -0800 | [diff] [blame] | 3077 | updateSystemUiLocked(mImeWindowVis, mBackDisposition); |
satok | d81e950 | 2012-05-21 12:58:45 +0900 | [diff] [blame] | 3078 | mCurMethod.changeInputMethodSubtype(newSubtype); |
| 3079 | } catch (RemoteException e) { |
| 3080 | Slog.w(TAG, "Failed to call changeInputMethodSubtype"); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 3081 | } |
| 3082 | } |
| 3083 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3084 | return; |
| 3085 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3086 | |
satok | d81e950 | 2012-05-21 12:58:45 +0900 | [diff] [blame] | 3087 | // Changing to a different IME. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3088 | final long ident = Binder.clearCallingIdentity(); |
| 3089 | try { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 3090 | // Set a subtype to this input method. |
| 3091 | // subtypeId the name of a subtype which will be set. |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3092 | setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false); |
| 3093 | // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked() |
| 3094 | // because mCurMethodId is stored as a history in |
| 3095 | // setSelectedInputMethodAndSubtypeLocked(). |
| 3096 | mCurMethodId = id; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3097 | |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 3098 | if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3099 | Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 3100 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3101 | intent.putExtra("input_method_id", id); |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 3102 | mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3103 | } |
Yohei Yukawa | b752645 | 2018-10-21 20:15:17 -0700 | [diff] [blame] | 3104 | unbindCurrentClientLocked(UnbindReason.SWITCH_IME); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3105 | } finally { |
| 3106 | Binder.restoreCallingIdentity(ident); |
| 3107 | } |
| 3108 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3109 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 3110 | @Override |
Tarandeep Singh | bb0e2f7 | 2020-01-15 13:58:29 -0800 | [diff] [blame] | 3111 | public boolean showSoftInput(IInputMethodClient client, IBinder windowToken, int flags, |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3112 | ResultReceiver resultReceiver) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 3113 | int uid = Binder.getCallingUid(); |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 3114 | synchronized (mMethodMap) { |
Yohei Yukawa | 46d7476 | 2019-01-22 10:17:22 -0800 | [diff] [blame] | 3115 | if (!calledFromValidUserLocked()) { |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 3116 | return false; |
| 3117 | } |
| 3118 | final long ident = Binder.clearCallingIdentity(); |
| 3119 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3120 | if (mCurClient == null || client == null |
| 3121 | || mCurClient.client.asBinder() != client.asBinder()) { |
Yohei Yukawa | cf93f9a | 2018-08-30 15:58:47 -0700 | [diff] [blame] | 3122 | // We need to check if this is the current client with |
| 3123 | // focus in the window manager, to allow this call to |
| 3124 | // be made before input is started in it. |
Yohei Yukawa | 41f89c3 | 2018-09-19 14:30:04 -0700 | [diff] [blame] | 3125 | final ClientState cs = mClients.get(client.asBinder()); |
| 3126 | if (cs == null) { |
| 3127 | throw new IllegalArgumentException("unknown client " + client.asBinder()); |
| 3128 | } |
Yohei Yukawa | 4052a10f | 2018-10-15 15:35:55 +0800 | [diff] [blame] | 3129 | if (!mWindowManagerInternal.isInputMethodClientFocus(cs.uid, cs.pid, |
| 3130 | cs.selfReportedDisplayId)) { |
Yohei Yukawa | cf93f9a | 2018-08-30 15:58:47 -0700 | [diff] [blame] | 3131 | Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3132 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3133 | } |
| 3134 | } |
Craig Mautner | e4bbb1c | 2013-03-15 11:38:44 -0700 | [diff] [blame] | 3135 | if (DEBUG) Slog.v(TAG, "Client requesting input be shown"); |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 3136 | return showCurrentInputLocked(windowToken, flags, resultReceiver, |
| 3137 | SoftInputShowHideReason.SHOW_SOFT_INPUT); |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 3138 | } finally { |
| 3139 | Binder.restoreCallingIdentity(ident); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3140 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3141 | } |
| 3142 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3143 | |
Andreas Gampe | d6d4206 | 2018-07-20 13:08:21 -0700 | [diff] [blame] | 3144 | @GuardedBy("mMethodMap") |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 3145 | boolean showCurrentInputLocked(IBinder windowToken, int flags, ResultReceiver resultReceiver, |
| 3146 | @SoftInputShowHideReason int reason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3147 | mShowRequested = true; |
Anna Galusza | 9b27811 | 2016-01-04 11:37:37 -0800 | [diff] [blame] | 3148 | if (mAccessibilityRequestingNoSoftKeyboard) { |
| 3149 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3150 | } |
Anna Galusza | 9b27811 | 2016-01-04 11:37:37 -0800 | [diff] [blame] | 3151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3152 | if ((flags&InputMethodManager.SHOW_FORCED) != 0) { |
| 3153 | mShowExplicitlyRequested = true; |
| 3154 | mShowForced = true; |
Anna Galusza | 9b27811 | 2016-01-04 11:37:37 -0800 | [diff] [blame] | 3155 | } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) { |
| 3156 | mShowExplicitlyRequested = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3157 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3158 | |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 3159 | if (!mSystemReady) { |
| 3160 | return false; |
| 3161 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3162 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3163 | boolean res = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3164 | if (mCurMethod != null) { |
Craig Mautner | e4bbb1c | 2013-03-15 11:38:44 -0700 | [diff] [blame] | 3165 | if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken); |
Tarandeep Singh | bb0e2f7 | 2020-01-15 13:58:29 -0800 | [diff] [blame] | 3166 | // create a dummy token for IMS so that IMS cannot inject windows into client app. |
| 3167 | Binder showInputToken = new Binder(); |
| 3168 | mShowRequestWindowMap.put(showInputToken, windowToken); |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 3169 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageIIOOO( |
| 3170 | MSG_SHOW_SOFT_INPUT, getImeShowFlags(), reason, mCurMethod, resultReceiver, |
| 3171 | showInputToken)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3172 | mInputShown = true; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 3173 | if (mHaveConnection && !mVisibleBound) { |
Yohei Yukawa | f80087c | 2018-05-21 09:47:53 -0700 | [diff] [blame] | 3174 | bindCurrentInputMethodServiceLocked( |
Yohei Yukawa | a67a459 | 2017-03-30 15:57:02 -0700 | [diff] [blame] | 3175 | mCurIntent, mVisibleConnection, IME_VISIBLE_BIND_FLAGS); |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 3176 | mVisibleBound = true; |
| 3177 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3178 | res = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3179 | } else if (mHaveConnection && SystemClock.uptimeMillis() |
satok | 59b424c | 2011-09-30 17:21:46 +0900 | [diff] [blame] | 3180 | >= (mLastBindTime+TIME_TO_RECONNECT)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3181 | // The client has asked to have the input method shown, but |
| 3182 | // we have been sitting here too long with a connection to the |
| 3183 | // service and no interface received, so let's disconnect/connect |
| 3184 | // to try to prod things along. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3185 | EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3186 | SystemClock.uptimeMillis()-mLastBindTime,1); |
satok | 59b424c | 2011-09-30 17:21:46 +0900 | [diff] [blame] | 3187 | Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3188 | mContext.unbindService(this); |
Yohei Yukawa | f80087c | 2018-05-21 09:47:53 -0700 | [diff] [blame] | 3189 | bindCurrentInputMethodServiceLocked(mCurIntent, this, IME_CONNECTION_BIND_FLAGS); |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3190 | } else { |
| 3191 | if (DEBUG) { |
| 3192 | Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = " |
| 3193 | + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis())); |
| 3194 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3195 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3196 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3197 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3198 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3199 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 3200 | @Override |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 3201 | public boolean hideSoftInput(IInputMethodClient client, IBinder windowToken, int flags, |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3202 | ResultReceiver resultReceiver) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 3203 | int uid = Binder.getCallingUid(); |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 3204 | synchronized (mMethodMap) { |
Yohei Yukawa | 46d7476 | 2019-01-22 10:17:22 -0800 | [diff] [blame] | 3205 | if (!calledFromValidUserLocked()) { |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 3206 | return false; |
| 3207 | } |
| 3208 | final long ident = Binder.clearCallingIdentity(); |
| 3209 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3210 | if (mCurClient == null || client == null |
| 3211 | || mCurClient.client.asBinder() != client.asBinder()) { |
Yohei Yukawa | cf93f9a | 2018-08-30 15:58:47 -0700 | [diff] [blame] | 3212 | // We need to check if this is the current client with |
| 3213 | // focus in the window manager, to allow this call to |
| 3214 | // be made before input is started in it. |
Yohei Yukawa | 41f89c3 | 2018-09-19 14:30:04 -0700 | [diff] [blame] | 3215 | final ClientState cs = mClients.get(client.asBinder()); |
| 3216 | if (cs == null) { |
| 3217 | throw new IllegalArgumentException("unknown client " + client.asBinder()); |
| 3218 | } |
Yohei Yukawa | 4052a10f | 2018-10-15 15:35:55 +0800 | [diff] [blame] | 3219 | if (!mWindowManagerInternal.isInputMethodClientFocus(cs.uid, cs.pid, |
| 3220 | cs.selfReportedDisplayId)) { |
Yohei Yukawa | cf93f9a | 2018-08-30 15:58:47 -0700 | [diff] [blame] | 3221 | if (DEBUG) { |
| 3222 | Slog.w(TAG, "Ignoring hideSoftInput of uid " + uid + ": " + client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3223 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3224 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3225 | } |
| 3226 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3227 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3228 | if (DEBUG) Slog.v(TAG, "Client requesting input be hidden"); |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 3229 | return hideCurrentInputLocked(windowToken, flags, resultReceiver, |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 3230 | SoftInputShowHideReason.HIDE_SOFT_INPUT); |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 3231 | } finally { |
| 3232 | Binder.restoreCallingIdentity(ident); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3233 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3234 | } |
| 3235 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3236 | |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 3237 | boolean hideCurrentInputLocked(IBinder windowToken, int flags, ResultReceiver resultReceiver, |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 3238 | @SoftInputShowHideReason int reason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3239 | if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0 |
| 3240 | && (mShowExplicitlyRequested || mShowForced)) { |
Craig Mautner | e4bbb1c | 2013-03-15 11:38:44 -0700 | [diff] [blame] | 3241 | if (DEBUG) Slog.v(TAG, "Not hiding: explicit show not cancelled by non-explicit hide"); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3242 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3243 | } |
| 3244 | if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) { |
Craig Mautner | e4bbb1c | 2013-03-15 11:38:44 -0700 | [diff] [blame] | 3245 | if (DEBUG) Slog.v(TAG, "Not hiding: forced show not cancelled by not-always hide"); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3246 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3247 | } |
Seigo Nonaka | ec92865 | 2015-06-10 15:31:20 +0900 | [diff] [blame] | 3248 | |
| 3249 | // There is a chance that IMM#hideSoftInput() is called in a transient state where |
| 3250 | // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting |
| 3251 | // to be updated with the new value sent from IME process. Even in such a transient state |
| 3252 | // historically we have accepted an incoming call of IMM#hideSoftInput() from the |
| 3253 | // application process as a valid request, and have even promised such a behavior with CTS |
| 3254 | // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only |
| 3255 | // IMMS#InputShown indicates that the software keyboard is shown. |
| 3256 | // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested. |
| 3257 | final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown || |
| 3258 | (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3259 | boolean res; |
Seigo Nonaka | ec92865 | 2015-06-10 15:31:20 +0900 | [diff] [blame] | 3260 | if (shouldHideSoftInput) { |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 3261 | final Binder hideInputToken = new Binder(); |
| 3262 | mHideRequestWindowMap.put(hideInputToken, windowToken); |
Seigo Nonaka | ec92865 | 2015-06-10 15:31:20 +0900 | [diff] [blame] | 3263 | // The IME will report its visible state again after the following message finally |
| 3264 | // delivered to the IME process as an IPC. Hence the inconsistency between |
| 3265 | // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in |
| 3266 | // the final state. |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 3267 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOOO(MSG_HIDE_SOFT_INPUT, |
| 3268 | reason, mCurMethod, resultReceiver, hideInputToken)); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3269 | res = true; |
| 3270 | } else { |
| 3271 | res = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3272 | } |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 3273 | if (mHaveConnection && mVisibleBound) { |
| 3274 | mContext.unbindService(mVisibleConnection); |
| 3275 | mVisibleBound = false; |
| 3276 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3277 | mInputShown = false; |
| 3278 | mShowRequested = false; |
| 3279 | mShowExplicitlyRequested = false; |
| 3280 | mShowForced = false; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 3281 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3282 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3283 | |
Yohei Yukawa | 2553e48 | 2017-12-15 15:47:33 -0800 | [diff] [blame] | 3284 | @NonNull |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 3285 | @Override |
Yohei Yukawa | 05c25f8 | 2016-02-22 12:41:17 -0800 | [diff] [blame] | 3286 | public InputBindResult startInputOrWindowGainedFocus( |
Yohei Yukawa | dc66e52 | 2018-10-21 10:43:14 -0700 | [diff] [blame] | 3287 | @StartInputReason int startInputReason, IInputMethodClient client, IBinder windowToken, |
Yohei Yukawa | 35fa6d5 | 2018-10-31 11:33:32 -0700 | [diff] [blame] | 3288 | @StartInputFlags int startInputFlags, @SoftInputModeFlags int softInputMode, |
| 3289 | int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext, |
Yohei Yukawa | dc66e52 | 2018-10-21 10:43:14 -0700 | [diff] [blame] | 3290 | @MissingMethodFlags int missingMethods, int unverifiedTargetSdkVersion) { |
Yohei Yukawa | 80498d5 | 2018-06-21 16:24:36 -0700 | [diff] [blame] | 3291 | if (windowToken == null) { |
| 3292 | Slog.e(TAG, "windowToken cannot be null."); |
| 3293 | return InputBindResult.NULL; |
Yohei Yukawa | 05c25f8 | 2016-02-22 12:41:17 -0800 | [diff] [blame] | 3294 | } |
Yohei Yukawa | 0f5eade | 2019-01-18 08:48:07 -0800 | [diff] [blame] | 3295 | final int callingUserId = UserHandle.getCallingUserId(); |
| 3296 | final int userId; |
Yohei Yukawa | 716897c | 2019-01-22 00:00:53 -0800 | [diff] [blame] | 3297 | if (attribute != null && attribute.targetInputMethodUser != null |
Yohei Yukawa | 0f5eade | 2019-01-18 08:48:07 -0800 | [diff] [blame] | 3298 | && attribute.targetInputMethodUser.getIdentifier() != callingUserId) { |
| 3299 | mContext.enforceCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
Yohei Yukawa | 670abea | 2019-01-28 00:00:50 -0800 | [diff] [blame] | 3300 | "Using EditorInfo.targetInputMethodUser requires INTERACT_ACROSS_USERS_FULL."); |
Yohei Yukawa | 0f5eade | 2019-01-18 08:48:07 -0800 | [diff] [blame] | 3301 | userId = attribute.targetInputMethodUser.getIdentifier(); |
| 3302 | if (!mUserManagerInternal.isUserRunning(userId)) { |
| 3303 | // There is a chance that we hit here because of race condition. Let's just return |
| 3304 | // an error code instead of crashing the caller process, which at least has |
| 3305 | // INTERACT_ACROSS_USERS_FULL permission thus is likely to be an important process. |
| 3306 | Slog.e(TAG, "User #" + userId + " is not running."); |
| 3307 | return InputBindResult.INVALID_USER; |
| 3308 | } |
| 3309 | } else { |
| 3310 | userId = callingUserId; |
| 3311 | } |
Yohei Yukawa | 7cc3356 | 2019-01-28 00:49:18 -0800 | [diff] [blame] | 3312 | final InputBindResult result; |
| 3313 | synchronized (mMethodMap) { |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3314 | final long ident = Binder.clearCallingIdentity(); |
| 3315 | try { |
| 3316 | result = startInputOrWindowGainedFocusInternalLocked(startInputReason, client, |
| 3317 | windowToken, startInputFlags, softInputMode, windowFlags, attribute, |
| 3318 | inputContext, missingMethods, unverifiedTargetSdkVersion, userId); |
| 3319 | } finally { |
| 3320 | Binder.restoreCallingIdentity(ident); |
| 3321 | } |
Yohei Yukawa | 7cc3356 | 2019-01-28 00:49:18 -0800 | [diff] [blame] | 3322 | } |
Yohei Yukawa | 2553e48 | 2017-12-15 15:47:33 -0800 | [diff] [blame] | 3323 | if (result == null) { |
| 3324 | // This must never happen, but just in case. |
| 3325 | Slog.wtf(TAG, "InputBindResult is @NonNull. startInputReason=" |
Yohei Yukawa | a468d70 | 2018-10-21 11:42:34 -0700 | [diff] [blame] | 3326 | + InputMethodDebug.startInputReasonToString(startInputReason) |
Yohei Yukawa | 2553e48 | 2017-12-15 15:47:33 -0800 | [diff] [blame] | 3327 | + " windowFlags=#" + Integer.toHexString(windowFlags) |
| 3328 | + " editorInfo=" + attribute); |
| 3329 | return InputBindResult.NULL; |
| 3330 | } |
| 3331 | return result; |
Yohei Yukawa | 05c25f8 | 2016-02-22 12:41:17 -0800 | [diff] [blame] | 3332 | } |
| 3333 | |
Yohei Yukawa | 2553e48 | 2017-12-15 15:47:33 -0800 | [diff] [blame] | 3334 | @NonNull |
Yohei Yukawa | 7cc3356 | 2019-01-28 00:49:18 -0800 | [diff] [blame] | 3335 | private InputBindResult startInputOrWindowGainedFocusInternalLocked( |
Yohei Yukawa | dc66e52 | 2018-10-21 10:43:14 -0700 | [diff] [blame] | 3336 | @StartInputReason int startInputReason, IInputMethodClient client, |
Yohei Yukawa | 35fa6d5 | 2018-10-31 11:33:32 -0700 | [diff] [blame] | 3337 | @NonNull IBinder windowToken, @StartInputFlags int startInputFlags, |
| 3338 | @SoftInputModeFlags int softInputMode, int windowFlags, EditorInfo attribute, |
| 3339 | IInputContext inputContext, @MissingMethodFlags int missingMethods, |
Yohei Yukawa | 4391c20 | 2019-01-28 00:49:10 -0800 | [diff] [blame] | 3340 | int unverifiedTargetSdkVersion, @UserIdInt int userId) { |
Yohei Yukawa | 6746452 | 2019-01-28 00:50:09 -0800 | [diff] [blame] | 3341 | if (DEBUG) { |
| 3342 | Slog.v(TAG, "startInputOrWindowGainedFocusInternalLocked: reason=" |
| 3343 | + InputMethodDebug.startInputReasonToString(startInputReason) |
| 3344 | + " client=" + client.asBinder() |
| 3345 | + " inputContext=" + inputContext |
| 3346 | + " missingMethods=" |
| 3347 | + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods) |
| 3348 | + " attribute=" + attribute |
| 3349 | + " startInputFlags=" |
| 3350 | + InputMethodDebug.startInputFlagsToString(startInputFlags) |
| 3351 | + " softInputMode=" + InputMethodDebug.softInputModeToString(softInputMode) |
| 3352 | + " windowFlags=#" + Integer.toHexString(windowFlags) |
| 3353 | + " unverifiedTargetSdkVersion=" + unverifiedTargetSdkVersion); |
| 3354 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3355 | |
Yohei Yukawa | 6746452 | 2019-01-28 00:50:09 -0800 | [diff] [blame] | 3356 | final int windowDisplayId = mWindowManagerInternal.getDisplayIdForWindow(windowToken); |
| 3357 | |
| 3358 | final ClientState cs = mClients.get(client.asBinder()); |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3359 | if (cs == null) { |
Yohei Yukawa | 6746452 | 2019-01-28 00:50:09 -0800 | [diff] [blame] | 3360 | throw new IllegalArgumentException("unknown client " + client.asBinder()); |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3361 | } |
| 3362 | if (cs.selfReportedDisplayId != windowDisplayId) { |
| 3363 | Slog.e(TAG, "startInputOrWindowGainedFocusInternal: display ID mismatch." |
| 3364 | + " from client:" + cs.selfReportedDisplayId |
| 3365 | + " from window:" + windowDisplayId); |
| 3366 | return InputBindResult.DISPLAY_ID_MISMATCH; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3367 | } |
Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 3368 | |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3369 | if (!mWindowManagerInternal.isInputMethodClientFocus(cs.uid, cs.pid, |
| 3370 | cs.selfReportedDisplayId)) { |
| 3371 | // Check with the window manager to make sure this client actually |
| 3372 | // has a window with focus. If not, reject. This is thread safe |
| 3373 | // because if the focus changes some time before or after, the |
| 3374 | // next client receiving focus that has any interest in input will |
| 3375 | // be calling through here after that change happens. |
Yohei Yukawa | 7cc3356 | 2019-01-28 00:49:18 -0800 | [diff] [blame] | 3376 | if (DEBUG) { |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3377 | Slog.w(TAG, "Focus gain on non-focused client " + cs.client |
| 3378 | + " (uid=" + cs.uid + " pid=" + cs.pid + ")"); |
Yohei Yukawa | 7cc3356 | 2019-01-28 00:49:18 -0800 | [diff] [blame] | 3379 | } |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3380 | return InputBindResult.NOT_IME_TARGET_WINDOW; |
| 3381 | } |
Yohei Yukawa | 7cc3356 | 2019-01-28 00:49:18 -0800 | [diff] [blame] | 3382 | |
Yohei Yukawa | d277d69 | 2020-02-19 17:12:17 -0800 | [diff] [blame] | 3383 | if (mUserSwitchHandlerTask != null) { |
| 3384 | // There is already an on-going pending user switch task. |
| 3385 | final int nextUserId = mUserSwitchHandlerTask.mToUserId; |
| 3386 | if (userId == nextUserId) { |
| 3387 | scheduleSwitchUserTaskLocked(userId, cs.client); |
| 3388 | return InputBindResult.USER_SWITCHING; |
| 3389 | } |
| 3390 | for (int profileId : mUserManager.getProfileIdsWithDisabled(nextUserId)) { |
| 3391 | if (profileId == userId) { |
| 3392 | scheduleSwitchUserTaskLocked(userId, cs.client); |
| 3393 | return InputBindResult.USER_SWITCHING; |
| 3394 | } |
| 3395 | } |
| 3396 | return InputBindResult.INVALID_USER; |
| 3397 | } |
| 3398 | |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3399 | // cross-profile access is always allowed here to allow profile-switching. |
| 3400 | if (!mSettings.isCurrentProfile(userId)) { |
| 3401 | Slog.w(TAG, "A background user is requesting window. Hiding IME."); |
| 3402 | Slog.w(TAG, "If you need to impersonate a foreground user/profile from" |
| 3403 | + " a background user, use EditorInfo.targetInputMethodUser with" |
| 3404 | + " INTERACT_ACROSS_USERS_FULL permission."); |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 3405 | hideCurrentInputLocked( |
| 3406 | mCurFocusedWindow, 0, null, SoftInputShowHideReason.HIDE_INVALID_USER); |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3407 | return InputBindResult.INVALID_USER; |
| 3408 | } |
| 3409 | |
Tarandeep Singh | cf5ff82 | 2019-07-08 14:06:27 -0700 | [diff] [blame] | 3410 | if (userId != mSettings.getCurrentUserId()) { |
Yohei Yukawa | d277d69 | 2020-02-19 17:12:17 -0800 | [diff] [blame] | 3411 | scheduleSwitchUserTaskLocked(userId, cs.client); |
| 3412 | return InputBindResult.USER_SWITCHING; |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3413 | } |
Yohei Yukawa | d277d69 | 2020-02-19 17:12:17 -0800 | [diff] [blame] | 3414 | |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3415 | // Master feature flag that overrides other conditions and forces IME preRendering. |
| 3416 | if (DEBUG) { |
| 3417 | Slog.v(TAG, "IME PreRendering MASTER flag: " |
Yohei Yukawa | 6746452 | 2019-01-28 00:50:09 -0800 | [diff] [blame] | 3418 | + DebugFlags.FLAG_PRE_RENDER_IME_VIEWS.value() + ", LowRam: " + mIsLowRam); |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3419 | } |
| 3420 | // pre-rendering not supported on low-ram devices. |
| 3421 | cs.shouldPreRenderIme = DebugFlags.FLAG_PRE_RENDER_IME_VIEWS.value() && !mIsLowRam; |
| 3422 | |
| 3423 | if (mCurFocusedWindow == windowToken) { |
| 3424 | if (DEBUG) { |
| 3425 | Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client |
| 3426 | + " attribute=" + attribute + ", token = " + windowToken); |
Yohei Yukawa | 7cc3356 | 2019-01-28 00:49:18 -0800 | [diff] [blame] | 3427 | } |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3428 | if (attribute != null) { |
| 3429 | return startInputUncheckedLocked(cs, inputContext, missingMethods, |
| 3430 | attribute, startInputFlags, startInputReason); |
| 3431 | } |
| 3432 | return new InputBindResult( |
| 3433 | InputBindResult.ResultCode.SUCCESS_REPORT_WINDOW_FOCUS_ONLY, |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 3434 | null, null, null, -1, null); |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3435 | } |
| 3436 | mCurFocusedWindow = windowToken; |
| 3437 | mCurFocusedWindowSoftInputMode = softInputMode; |
| 3438 | mCurFocusedWindowClient = cs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3439 | |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3440 | // Should we auto-show the IME even if the caller has not |
| 3441 | // specified what should be done with it? |
| 3442 | // We only do this automatically if the window can resize |
| 3443 | // to accommodate the IME (so what the user sees will give |
| 3444 | // them good context without input information being obscured |
| 3445 | // by the IME) or if running on a large screen where there |
| 3446 | // is more room for the target window + IME. |
| 3447 | final boolean doAutoShow = |
Yohei Yukawa | 6e87559 | 2019-01-28 00:49:30 -0800 | [diff] [blame] | 3448 | (softInputMode & LayoutParams.SOFT_INPUT_MASK_ADJUST) |
| 3449 | == LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3450 | || mRes.getConfiguration().isLayoutSizeAtLeast( |
| 3451 | Configuration.SCREENLAYOUT_SIZE_LARGE); |
Yohei Yukawa | 6746452 | 2019-01-28 00:50:09 -0800 | [diff] [blame] | 3452 | final boolean isTextEditor = (startInputFlags & StartInputFlags.IS_TEXT_EDITOR) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3453 | |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3454 | // We want to start input before showing the IME, but after closing |
| 3455 | // it. We want to do this after closing it to help the IME disappear |
| 3456 | // more quickly (not get stuck behind it initializing itself for the |
| 3457 | // new focused input, even if its window wants to hide the IME). |
| 3458 | boolean didStart = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3459 | |
Yohei Yukawa | 6746452 | 2019-01-28 00:50:09 -0800 | [diff] [blame] | 3460 | InputBindResult res = null; |
Yohei Yukawa | 6e87559 | 2019-01-28 00:49:30 -0800 | [diff] [blame] | 3461 | switch (softInputMode & LayoutParams.SOFT_INPUT_MASK_STATE) { |
| 3462 | case LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED: |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3463 | if (!isTextEditor || !doAutoShow) { |
Yohei Yukawa | 6e87559 | 2019-01-28 00:49:30 -0800 | [diff] [blame] | 3464 | if (LayoutParams.mayUseInputMethod(windowFlags)) { |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3465 | // There is no focus view, and this window will |
| 3466 | // be behind any soft input window, so hide the |
| 3467 | // soft input window if it is shown. |
| 3468 | if (DEBUG) Slog.v(TAG, "Unspecified window will hide input"); |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 3469 | hideCurrentInputLocked( |
| 3470 | mCurFocusedWindow, InputMethodManager.HIDE_NOT_ALWAYS, null, |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 3471 | SoftInputShowHideReason.HIDE_UNSPECIFIED_WINDOW); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3472 | |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3473 | // If focused display changed, we should unbind current method |
| 3474 | // to make app window in previous display relayout after Ime |
| 3475 | // window token removed. |
| 3476 | // Note that we can trust client's display ID as long as it matches |
| 3477 | // to the display ID obtained from the window. |
| 3478 | if (cs.selfReportedDisplayId != mCurTokenDisplayId) { |
| 3479 | unbindCurrentMethodLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3480 | } |
Yohei Yukawa | 7cc3356 | 2019-01-28 00:49:18 -0800 | [diff] [blame] | 3481 | } |
Yohei Yukawa | 6e87559 | 2019-01-28 00:49:30 -0800 | [diff] [blame] | 3482 | } else if (isTextEditor && doAutoShow |
| 3483 | && (softInputMode & LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3484 | // There is a focus view, and we are navigating forward |
| 3485 | // into the window, so show the input window for the user. |
| 3486 | // We only do this automatically if the window can resize |
| 3487 | // to accommodate the IME (so what the user sees will give |
| 3488 | // them good context without input information being obscured |
| 3489 | // by the IME) or if running on a large screen where there |
| 3490 | // is more room for the target window + IME. |
| 3491 | if (DEBUG) Slog.v(TAG, "Unspecified window will show input"); |
| 3492 | if (attribute != null) { |
| 3493 | res = startInputUncheckedLocked(cs, inputContext, missingMethods, |
| 3494 | attribute, startInputFlags, startInputReason); |
| 3495 | didStart = true; |
Yohei Yukawa | 7cc3356 | 2019-01-28 00:49:18 -0800 | [diff] [blame] | 3496 | } |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 3497 | showCurrentInputLocked(windowToken, InputMethodManager.SHOW_IMPLICIT, null, |
| 3498 | SoftInputShowHideReason.SHOW_AUTO_EDITOR_FORWARD_NAV); |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3499 | } |
| 3500 | break; |
Yohei Yukawa | 6e87559 | 2019-01-28 00:49:30 -0800 | [diff] [blame] | 3501 | case LayoutParams.SOFT_INPUT_STATE_UNCHANGED: |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3502 | // Do nothing. |
| 3503 | break; |
Yohei Yukawa | 6e87559 | 2019-01-28 00:49:30 -0800 | [diff] [blame] | 3504 | case LayoutParams.SOFT_INPUT_STATE_HIDDEN: |
| 3505 | if ((softInputMode & LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3506 | if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward"); |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 3507 | hideCurrentInputLocked(mCurFocusedWindow, 0, null, |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 3508 | SoftInputShowHideReason.HIDE_STATE_HIDDEN_FORWARD_NAV); |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3509 | } |
| 3510 | break; |
Yohei Yukawa | 6e87559 | 2019-01-28 00:49:30 -0800 | [diff] [blame] | 3511 | case LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN: |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3512 | if (DEBUG) Slog.v(TAG, "Window asks to hide input"); |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 3513 | hideCurrentInputLocked(mCurFocusedWindow, 0, null, |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 3514 | SoftInputShowHideReason.HIDE_ALWAYS_HIDDEN_STATE); |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3515 | break; |
Yohei Yukawa | 6e87559 | 2019-01-28 00:49:30 -0800 | [diff] [blame] | 3516 | case LayoutParams.SOFT_INPUT_STATE_VISIBLE: |
| 3517 | if ((softInputMode & LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3518 | if (DEBUG) Slog.v(TAG, "Window asks to show input going forward"); |
Yohei Yukawa | 7cc3356 | 2019-01-28 00:49:18 -0800 | [diff] [blame] | 3519 | if (InputMethodUtils.isSoftInputModeStateVisibleAllowed( |
| 3520 | unverifiedTargetSdkVersion, startInputFlags)) { |
| 3521 | if (attribute != null) { |
Yohei Yukawa | 6746452 | 2019-01-28 00:50:09 -0800 | [diff] [blame] | 3522 | res = startInputUncheckedLocked(cs, inputContext, missingMethods, |
| 3523 | attribute, startInputFlags, startInputReason); |
Yohei Yukawa | 7cc3356 | 2019-01-28 00:49:18 -0800 | [diff] [blame] | 3524 | didStart = true; |
| 3525 | } |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 3526 | showCurrentInputLocked(windowToken, InputMethodManager.SHOW_IMPLICIT, null, |
| 3527 | SoftInputShowHideReason.SHOW_STATE_VISIBLE_FORWARD_NAV); |
Yohei Yukawa | 7cc3356 | 2019-01-28 00:49:18 -0800 | [diff] [blame] | 3528 | } else { |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3529 | Slog.e(TAG, "SOFT_INPUT_STATE_VISIBLE is ignored because" |
Yohei Yukawa | 7cc3356 | 2019-01-28 00:49:18 -0800 | [diff] [blame] | 3530 | + " there is no focused view that also returns true from" |
| 3531 | + " View#onCheckIsTextEditor()"); |
| 3532 | } |
Yohei Yukawa | 7cc3356 | 2019-01-28 00:49:18 -0800 | [diff] [blame] | 3533 | } |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3534 | break; |
Yohei Yukawa | 6e87559 | 2019-01-28 00:49:30 -0800 | [diff] [blame] | 3535 | case LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE: |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3536 | if (DEBUG) Slog.v(TAG, "Window asks to always show input"); |
| 3537 | if (InputMethodUtils.isSoftInputModeStateVisibleAllowed( |
| 3538 | unverifiedTargetSdkVersion, startInputFlags)) { |
| 3539 | if (attribute != null) { |
| 3540 | res = startInputUncheckedLocked(cs, inputContext, missingMethods, |
| 3541 | attribute, startInputFlags, startInputReason); |
| 3542 | didStart = true; |
| 3543 | } |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 3544 | showCurrentInputLocked(windowToken, InputMethodManager.SHOW_IMPLICIT, null, |
| 3545 | SoftInputShowHideReason.SHOW_STATE_ALWAYS_VISIBLE); |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3546 | } else { |
| 3547 | Slog.e(TAG, "SOFT_INPUT_STATE_ALWAYS_VISIBLE is ignored because" |
| 3548 | + " there is no focused view that also returns true from" |
| 3549 | + " View#onCheckIsTextEditor()"); |
| 3550 | } |
| 3551 | break; |
| 3552 | } |
| 3553 | |
| 3554 | if (!didStart) { |
| 3555 | if (attribute != null) { |
| 3556 | if (!DebugFlags.FLAG_OPTIMIZE_START_INPUT.value() |
| 3557 | || (startInputFlags & StartInputFlags.IS_TEXT_EDITOR) != 0) { |
Yohei Yukawa | 6746452 | 2019-01-28 00:50:09 -0800 | [diff] [blame] | 3558 | res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute, |
Yohei Yukawa | 13dc12f | 2019-01-28 00:49:24 -0800 | [diff] [blame] | 3559 | startInputFlags, startInputReason); |
| 3560 | } else { |
| 3561 | res = InputBindResult.NO_EDITOR; |
| 3562 | } |
| 3563 | } else { |
| 3564 | res = InputBindResult.NULL_EDITOR_INFO; |
Yohei Yukawa | 7cc3356 | 2019-01-28 00:49:18 -0800 | [diff] [blame] | 3565 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3566 | } |
Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 3567 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3568 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3569 | |
Guliz Tuncay | 6908c15 | 2017-06-02 16:06:10 -0700 | [diff] [blame] | 3570 | private boolean canShowInputMethodPickerLocked(IInputMethodClient client) { |
Yohei Yukawa | 4052a10f | 2018-10-15 15:35:55 +0800 | [diff] [blame] | 3571 | // TODO(yukawa): multi-display support. |
Guliz Tuncay | 6908c15 | 2017-06-02 16:06:10 -0700 | [diff] [blame] | 3572 | final int uid = Binder.getCallingUid(); |
lumark | 0b05f9e | 2018-11-26 15:09:06 +0800 | [diff] [blame] | 3573 | if (mCurFocusedWindowClient != null && client != null |
Tarandeep Singh | eb57061 | 2018-01-29 16:20:32 -0800 | [diff] [blame] | 3574 | && mCurFocusedWindowClient.client.asBinder() == client.asBinder()) { |
Guliz Tuncay | 6908c15 | 2017-06-02 16:06:10 -0700 | [diff] [blame] | 3575 | return true; |
| 3576 | } else if (mCurIntent != null && InputMethodUtils.checkIfPackageBelongsToUid( |
| 3577 | mAppOpsManager, |
| 3578 | uid, |
| 3579 | mCurIntent.getComponent().getPackageName())) { |
| 3580 | return true; |
Guliz Tuncay | 6908c15 | 2017-06-02 16:06:10 -0700 | [diff] [blame] | 3581 | } |
Guliz Tuncay | 6908c15 | 2017-06-02 16:06:10 -0700 | [diff] [blame] | 3582 | return false; |
| 3583 | } |
| 3584 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 3585 | @Override |
Seigo Nonaka | 14e1391 | 2015-05-06 21:04:13 -0700 | [diff] [blame] | 3586 | public void showInputMethodPickerFromClient( |
| 3587 | IInputMethodClient client, int auxiliarySubtypeMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3588 | synchronized (mMethodMap) { |
Yohei Yukawa | 46d7476 | 2019-01-22 10:17:22 -0800 | [diff] [blame] | 3589 | if (!calledFromValidUserLocked()) { |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 3590 | return; |
| 3591 | } |
Guliz Tuncay | 6908c15 | 2017-06-02 16:06:10 -0700 | [diff] [blame] | 3592 | if(!canShowInputMethodPickerLocked(client)) { |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 3593 | Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid " |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 3594 | + Binder.getCallingUid() + ": " + client); |
Guliz Tuncay | 6908c15 | 2017-06-02 16:06:10 -0700 | [diff] [blame] | 3595 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3596 | } |
| 3597 | |
satok | 440aab5 | 2010-11-25 09:43:11 +0900 | [diff] [blame] | 3598 | // Always call subtype picker, because subtype picker is a superset of input method |
| 3599 | // picker. |
lumark | 0b05f9e | 2018-11-26 15:09:06 +0800 | [diff] [blame] | 3600 | mHandler.sendMessage(mCaller.obtainMessageII( |
| 3601 | MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode, |
| 3602 | (mCurClient != null) ? mCurClient.selfReportedDisplayId : DEFAULT_DISPLAY)); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 3603 | } |
| 3604 | } |
| 3605 | |
lumark | 0b05f9e | 2018-11-26 15:09:06 +0800 | [diff] [blame] | 3606 | @Override |
| 3607 | public void showInputMethodPickerFromSystem(IInputMethodClient client, int auxiliarySubtypeMode, |
| 3608 | int displayId) { |
| 3609 | if (mContext.checkCallingPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 3610 | != PackageManager.PERMISSION_GRANTED) { |
| 3611 | throw new SecurityException( |
| 3612 | "showInputMethodPickerFromSystem requires WRITE_SECURE_SETTINGS permission"); |
| 3613 | } |
| 3614 | // Always call subtype picker, because subtype picker is a superset of input method |
| 3615 | // picker. |
| 3616 | mHandler.sendMessage(mCaller.obtainMessageII( |
| 3617 | MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode, displayId)); |
| 3618 | } |
| 3619 | |
Tarandeep Singh | eb57061 | 2018-01-29 16:20:32 -0800 | [diff] [blame] | 3620 | public boolean isInputMethodPickerShownForTest() { |
| 3621 | synchronized(mMethodMap) { |
| 3622 | if (mSwitchingDialog == null) { |
| 3623 | return false; |
| 3624 | } |
| 3625 | return mSwitchingDialog.isShowing(); |
| 3626 | } |
| 3627 | } |
| 3628 | |
Yohei Yukawa | 0c1ebff | 2018-12-27 14:06:28 -0800 | [diff] [blame] | 3629 | @BinderThread |
Yohei Yukawa | d3e0fd93 | 2019-01-21 09:16:48 -0800 | [diff] [blame] | 3630 | private void setInputMethod(@NonNull IBinder token, String id) { |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 3631 | synchronized (mMethodMap) { |
Yohei Yukawa | 0c1ebff | 2018-12-27 14:06:28 -0800 | [diff] [blame] | 3632 | if (!calledWithValidTokenLocked(token)) { |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 3633 | return; |
| 3634 | } |
| 3635 | setInputMethodWithSubtypeIdLocked(token, id, NOT_A_SUBTYPE_ID); |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3636 | } |
satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 3637 | } |
| 3638 | |
Yohei Yukawa | 4773ee1 | 2018-12-24 21:13:53 -0800 | [diff] [blame] | 3639 | @BinderThread |
Yohei Yukawa | d3e0fd93 | 2019-01-21 09:16:48 -0800 | [diff] [blame] | 3640 | private void setInputMethodAndSubtype(@NonNull IBinder token, String id, |
| 3641 | InputMethodSubtype subtype) { |
satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 3642 | synchronized (mMethodMap) { |
Yohei Yukawa | 4773ee1 | 2018-12-24 21:13:53 -0800 | [diff] [blame] | 3643 | if (!calledWithValidTokenLocked(token)) { |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 3644 | return; |
| 3645 | } |
satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 3646 | if (subtype != null) { |
Yohei Yukawa | 4e02bc6 | 2014-06-04 18:37:20 +0900 | [diff] [blame] | 3647 | setInputMethodWithSubtypeIdLocked(token, id, |
| 3648 | InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id), |
| 3649 | subtype.hashCode())); |
satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 3650 | } else { |
| 3651 | setInputMethod(token, id); |
| 3652 | } |
| 3653 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 3654 | } |
| 3655 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 3656 | @Override |
satok | b416a71 | 2010-11-25 20:42:14 +0900 | [diff] [blame] | 3657 | public void showInputMethodAndSubtypeEnablerFromClient( |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 3658 | IInputMethodClient client, String inputMethodId) { |
satok | b416a71 | 2010-11-25 20:42:14 +0900 | [diff] [blame] | 3659 | synchronized (mMethodMap) { |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 3660 | // TODO(yukawa): Should we verify the display ID? |
Yohei Yukawa | 46d7476 | 2019-01-22 10:17:22 -0800 | [diff] [blame] | 3661 | if (!calledFromValidUserLocked()) { |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 3662 | return; |
| 3663 | } |
satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 3664 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageO( |
| 3665 | MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId)); |
satok | b416a71 | 2010-11-25 20:42:14 +0900 | [diff] [blame] | 3666 | } |
| 3667 | } |
| 3668 | |
Yohei Yukawa | 0c49908 | 2018-12-09 18:52:02 -0800 | [diff] [blame] | 3669 | @BinderThread |
Yohei Yukawa | d3e0fd93 | 2019-01-21 09:16:48 -0800 | [diff] [blame] | 3670 | private boolean switchToPreviousInputMethod(@NonNull IBinder token) { |
satok | 735cf38 | 2010-11-11 20:40:09 +0900 | [diff] [blame] | 3671 | synchronized (mMethodMap) { |
Yohei Yukawa | 79e4c6a | 2018-12-09 19:07:17 -0800 | [diff] [blame] | 3672 | if (!calledWithValidTokenLocked(token)) { |
Yohei Yukawa | 0c49908 | 2018-12-09 18:52:02 -0800 | [diff] [blame] | 3673 | return false; |
| 3674 | } |
satok | c445bcd | 2011-01-25 18:57:24 +0900 | [diff] [blame] | 3675 | final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked(); |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 3676 | final InputMethodInfo lastImi; |
satok | 208d563 | 2011-05-20 22:13:38 +0900 | [diff] [blame] | 3677 | if (lastIme != null) { |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 3678 | lastImi = mMethodMap.get(lastIme.first); |
| 3679 | } else { |
| 3680 | lastImi = null; |
satok | 735cf38 | 2010-11-11 20:40:09 +0900 | [diff] [blame] | 3681 | } |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 3682 | String targetLastImiId = null; |
| 3683 | int subtypeId = NOT_A_SUBTYPE_ID; |
| 3684 | if (lastIme != null && lastImi != null) { |
| 3685 | final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId); |
Narayan Kamath | a09b4d2 | 2016-04-15 18:32:45 +0100 | [diff] [blame] | 3686 | final int lastSubtypeHash = Integer.parseInt(lastIme.second); |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 3687 | final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID |
| 3688 | : mCurrentSubtype.hashCode(); |
| 3689 | // If the last IME is the same as the current IME and the last subtype is not |
| 3690 | // defined, there is no need to switch to the last IME. |
| 3691 | if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) { |
| 3692 | targetLastImiId = lastIme.first; |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 3693 | subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash); |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 3694 | } |
| 3695 | } |
| 3696 | |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 3697 | if (TextUtils.isEmpty(targetLastImiId) |
| 3698 | && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) { |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 3699 | // This is a safety net. If the currentSubtype can't be added to the history |
| 3700 | // and the framework couldn't find the last ime, we will make the last ime be |
| 3701 | // the most applicable enabled keyboard subtype of the system imes. |
| 3702 | final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked(); |
| 3703 | if (enabled != null) { |
| 3704 | final int N = enabled.size(); |
| 3705 | final String locale = mCurrentSubtype == null |
| 3706 | ? mRes.getConfiguration().locale.toString() |
| 3707 | : mCurrentSubtype.getLocale(); |
| 3708 | for (int i = 0; i < N; ++i) { |
| 3709 | final InputMethodInfo imi = enabled.get(i); |
Yohei Yukawa | fd70fe8 | 2018-04-08 12:19:56 -0700 | [diff] [blame] | 3710 | if (imi.getSubtypeCount() > 0 && imi.isSystem()) { |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 3711 | InputMethodSubtype keyboardSubtype = |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 3712 | InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes, |
| 3713 | InputMethodUtils.getSubtypes(imi), |
| 3714 | InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true); |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 3715 | if (keyboardSubtype != null) { |
| 3716 | targetLastImiId = imi.getId(); |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 3717 | subtypeId = InputMethodUtils.getSubtypeIdFromHashCode( |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 3718 | imi, keyboardSubtype.hashCode()); |
| 3719 | if(keyboardSubtype.getLocale().equals(locale)) { |
| 3720 | break; |
| 3721 | } |
| 3722 | } |
| 3723 | } |
| 3724 | } |
| 3725 | } |
| 3726 | } |
| 3727 | |
| 3728 | if (!TextUtils.isEmpty(targetLastImiId)) { |
| 3729 | if (DEBUG) { |
| 3730 | Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second |
| 3731 | + ", from: " + mCurMethodId + ", " + subtypeId); |
| 3732 | } |
Yohei Yukawa | 4e02bc6 | 2014-06-04 18:37:20 +0900 | [diff] [blame] | 3733 | setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId); |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 3734 | return true; |
| 3735 | } else { |
| 3736 | return false; |
| 3737 | } |
satok | 735cf38 | 2010-11-11 20:40:09 +0900 | [diff] [blame] | 3738 | } |
| 3739 | } |
| 3740 | |
Yohei Yukawa | 70f17e7 | 2018-12-09 18:51:38 -0800 | [diff] [blame] | 3741 | @BinderThread |
Yohei Yukawa | d3e0fd93 | 2019-01-21 09:16:48 -0800 | [diff] [blame] | 3742 | private boolean switchToNextInputMethod(@NonNull IBinder token, boolean onlyCurrentIme) { |
satok | 688bd47 | 2012-02-09 20:09:17 +0900 | [diff] [blame] | 3743 | synchronized (mMethodMap) { |
Yohei Yukawa | 79e4c6a | 2018-12-09 19:07:17 -0800 | [diff] [blame] | 3744 | if (!calledWithValidTokenLocked(token)) { |
Yohei Yukawa | a075574 | 2014-06-04 20:28:18 +0900 | [diff] [blame] | 3745 | return false; |
| 3746 | } |
Yohei Yukawa | 5a647b69 | 2014-05-22 12:49:00 +0900 | [diff] [blame] | 3747 | final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked( |
Yohei Yukawa | 136b6ce | 2018-05-02 10:55:35 -0700 | [diff] [blame] | 3748 | onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype); |
satok | 688bd47 | 2012-02-09 20:09:17 +0900 | [diff] [blame] | 3749 | if (nextSubtype == null) { |
| 3750 | return false; |
| 3751 | } |
Yohei Yukawa | 4e02bc6 | 2014-06-04 18:37:20 +0900 | [diff] [blame] | 3752 | setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(), |
| 3753 | nextSubtype.mSubtypeId); |
satok | 688bd47 | 2012-02-09 20:09:17 +0900 | [diff] [blame] | 3754 | return true; |
| 3755 | } |
| 3756 | } |
| 3757 | |
Yohei Yukawa | eec552e | 2018-09-09 20:48:41 -0700 | [diff] [blame] | 3758 | @BinderThread |
| 3759 | private boolean shouldOfferSwitchingToNextInputMethod(@NonNull IBinder token) { |
Satoshi Kataoka | 2b10b52 | 2013-08-21 20:39:12 +0900 | [diff] [blame] | 3760 | synchronized (mMethodMap) { |
Yohei Yukawa | 79e4c6a | 2018-12-09 19:07:17 -0800 | [diff] [blame] | 3761 | if (!calledWithValidTokenLocked(token)) { |
Yohei Yukawa | a075574 | 2014-06-04 20:28:18 +0900 | [diff] [blame] | 3762 | return false; |
| 3763 | } |
Yohei Yukawa | 5a647b69 | 2014-05-22 12:49:00 +0900 | [diff] [blame] | 3764 | final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked( |
Yohei Yukawa | 136b6ce | 2018-05-02 10:55:35 -0700 | [diff] [blame] | 3765 | false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype); |
Satoshi Kataoka | 2b10b52 | 2013-08-21 20:39:12 +0900 | [diff] [blame] | 3766 | if (nextSubtype == null) { |
| 3767 | return false; |
| 3768 | } |
| 3769 | return true; |
| 3770 | } |
| 3771 | } |
| 3772 | |
| 3773 | @Override |
satok | 68f1b78 | 2011-04-11 14:26:04 +0900 | [diff] [blame] | 3774 | public InputMethodSubtype getLastInputMethodSubtype() { |
| 3775 | synchronized (mMethodMap) { |
Yohei Yukawa | 46d7476 | 2019-01-22 10:17:22 -0800 | [diff] [blame] | 3776 | if (!calledFromValidUserLocked()) { |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 3777 | return null; |
| 3778 | } |
satok | 68f1b78 | 2011-04-11 14:26:04 +0900 | [diff] [blame] | 3779 | final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked(); |
| 3780 | // TODO: Handle the case of the last IME with no subtypes |
| 3781 | if (lastIme == null || TextUtils.isEmpty(lastIme.first) |
| 3782 | || TextUtils.isEmpty(lastIme.second)) return null; |
| 3783 | final InputMethodInfo lastImi = mMethodMap.get(lastIme.first); |
| 3784 | if (lastImi == null) return null; |
| 3785 | try { |
Narayan Kamath | a09b4d2 | 2016-04-15 18:32:45 +0100 | [diff] [blame] | 3786 | final int lastSubtypeHash = Integer.parseInt(lastIme.second); |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 3787 | final int lastSubtypeId = |
| 3788 | InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash); |
satok | 0e7d7d6 | 2011-07-05 13:28:06 +0900 | [diff] [blame] | 3789 | if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) { |
| 3790 | return null; |
| 3791 | } |
| 3792 | return lastImi.getSubtypeAt(lastSubtypeId); |
satok | 68f1b78 | 2011-04-11 14:26:04 +0900 | [diff] [blame] | 3793 | } catch (NumberFormatException e) { |
| 3794 | return null; |
| 3795 | } |
| 3796 | } |
| 3797 | } |
| 3798 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3799 | @Override |
satok | ee5e77c | 2011-09-02 18:50:15 +0900 | [diff] [blame] | 3800 | public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) { |
satok | 91e8812 | 2011-07-18 11:11:42 +0900 | [diff] [blame] | 3801 | // By this IPC call, only a process which shares the same uid with the IME can add |
| 3802 | // additional input method subtypes to the IME. |
Yohei Yukawa | 70f5c48 | 2016-01-04 19:42:36 -0800 | [diff] [blame] | 3803 | if (TextUtils.isEmpty(imiId) || subtypes == null) return; |
Yohei Yukawa | b557d57 | 2018-12-29 21:26:26 -0800 | [diff] [blame] | 3804 | final ArrayList<InputMethodSubtype> toBeAdded = new ArrayList<>(); |
| 3805 | for (InputMethodSubtype subtype : subtypes) { |
| 3806 | if (!toBeAdded.contains(subtype)) { |
| 3807 | toBeAdded.add(subtype); |
| 3808 | } else { |
| 3809 | Slog.w(TAG, "Duplicated subtype definition found: " |
| 3810 | + subtype.getLocale() + ", " + subtype.getMode()); |
| 3811 | } |
| 3812 | } |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3813 | synchronized (mMethodMap) { |
Yohei Yukawa | 46d7476 | 2019-01-22 10:17:22 -0800 | [diff] [blame] | 3814 | if (!calledFromValidUserLocked()) { |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 3815 | return; |
| 3816 | } |
Yohei Yukawa | 7924782 | 2017-01-23 15:26:15 -0800 | [diff] [blame] | 3817 | if (!mSystemReady) { |
| 3818 | return; |
| 3819 | } |
satok | 91e8812 | 2011-07-18 11:11:42 +0900 | [diff] [blame] | 3820 | final InputMethodInfo imi = mMethodMap.get(imiId); |
satok | ee5e77c | 2011-09-02 18:50:15 +0900 | [diff] [blame] | 3821 | if (imi == null) return; |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3822 | final String[] packageInfos; |
| 3823 | try { |
| 3824 | packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid()); |
| 3825 | } catch (RemoteException e) { |
| 3826 | Slog.e(TAG, "Failed to get package infos"); |
| 3827 | return; |
| 3828 | } |
satok | 91e8812 | 2011-07-18 11:11:42 +0900 | [diff] [blame] | 3829 | if (packageInfos != null) { |
| 3830 | final int packageNum = packageInfos.length; |
| 3831 | for (int i = 0; i < packageNum; ++i) { |
| 3832 | if (packageInfos[i].equals(imi.getPackageName())) { |
Yohei Yukawa | 7b1c8d7 | 2018-12-19 11:21:04 -0800 | [diff] [blame] | 3833 | if (subtypes.length > 0) { |
Yohei Yukawa | b557d57 | 2018-12-29 21:26:26 -0800 | [diff] [blame] | 3834 | mAdditionalSubtypeMap.put(imi.getId(), toBeAdded); |
Yohei Yukawa | 7b1c8d7 | 2018-12-19 11:21:04 -0800 | [diff] [blame] | 3835 | } else { |
Yohei Yukawa | b557d57 | 2018-12-29 21:26:26 -0800 | [diff] [blame] | 3836 | mAdditionalSubtypeMap.remove(imi.getId()); |
Yohei Yukawa | 7b1c8d7 | 2018-12-19 11:21:04 -0800 | [diff] [blame] | 3837 | } |
Yohei Yukawa | b557d57 | 2018-12-29 21:26:26 -0800 | [diff] [blame] | 3838 | AdditionalSubtypeUtils.save(mAdditionalSubtypeMap, mMethodMap, |
| 3839 | mSettings.getCurrentUserId()); |
satok | c593380 | 2011-08-31 21:26:04 +0900 | [diff] [blame] | 3840 | final long ident = Binder.clearCallingIdentity(); |
| 3841 | try { |
Yohei Yukawa | 94e3330 | 2016-02-12 19:37:03 -0800 | [diff] [blame] | 3842 | buildInputMethodListLocked(false /* resetDefaultEnabledIme */); |
satok | c593380 | 2011-08-31 21:26:04 +0900 | [diff] [blame] | 3843 | } finally { |
| 3844 | Binder.restoreCallingIdentity(ident); |
| 3845 | } |
satok | ee5e77c | 2011-09-02 18:50:15 +0900 | [diff] [blame] | 3846 | return; |
satok | 91e8812 | 2011-07-18 11:11:42 +0900 | [diff] [blame] | 3847 | } |
| 3848 | } |
| 3849 | } |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3850 | } |
satok | ee5e77c | 2011-09-02 18:50:15 +0900 | [diff] [blame] | 3851 | return; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3852 | } |
| 3853 | |
Yohei Yukawa | b985e6e | 2018-09-05 17:07:52 -0700 | [diff] [blame] | 3854 | /** |
Artur Satayev | 192ca30 | 2020-01-22 21:38:23 +0000 | [diff] [blame] | 3855 | * This is kept due to {@code @UnsupportedAppUsage} in |
Yohei Yukawa | b985e6e | 2018-09-05 17:07:52 -0700 | [diff] [blame] | 3856 | * {@link InputMethodManager#getInputMethodWindowVisibleHeight()} and a dependency in |
| 3857 | * {@link InputMethodService#onCreate()}. |
| 3858 | * |
| 3859 | * <p>TODO(Bug 113914148): Check if we can remove this.</p> |
| 3860 | * @return {@link WindowManagerInternal#getInputMethodWindowVisibleHeight()} |
| 3861 | */ |
Satoshi Kataoka | 658c7b8 | 2013-10-10 17:03:51 +0900 | [diff] [blame] | 3862 | @Override |
| 3863 | public int getInputMethodWindowVisibleHeight() { |
Yohei Yukawa | 4052a10f | 2018-10-15 15:35:55 +0800 | [diff] [blame] | 3864 | // TODO(yukawa): Should we verify the display ID? |
lumark | 90120a8 | 2018-08-15 00:33:03 +0800 | [diff] [blame] | 3865 | return mWindowManagerInternal.getInputMethodWindowVisibleHeight(mCurTokenDisplayId); |
Satoshi Kataoka | 658c7b8 | 2013-10-10 17:03:51 +0900 | [diff] [blame] | 3866 | } |
| 3867 | |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 3868 | @Override |
| 3869 | public void reportActivityView(IInputMethodClient parentClient, int childDisplayId, |
| 3870 | float[] matrixValues) { |
| 3871 | final DisplayInfo displayInfo = mDisplayManagerInternal.getDisplayInfo(childDisplayId); |
| 3872 | if (displayInfo == null) { |
| 3873 | throw new IllegalArgumentException( |
| 3874 | "Cannot find display for non-existent displayId: " + childDisplayId); |
| 3875 | } |
| 3876 | final int callingUid = Binder.getCallingUid(); |
| 3877 | if (callingUid != displayInfo.ownerUid) { |
| 3878 | throw new SecurityException("The caller doesn't own the display."); |
| 3879 | } |
| 3880 | |
| 3881 | synchronized (mMethodMap) { |
| 3882 | final ClientState cs = mClients.get(parentClient.asBinder()); |
| 3883 | if (cs == null) { |
| 3884 | return; |
| 3885 | } |
| 3886 | |
| 3887 | // null matrixValues means that the entry needs to be removed. |
| 3888 | if (matrixValues == null) { |
| 3889 | final ActivityViewInfo info = mActivityViewDisplayIdToParentMap.get(childDisplayId); |
| 3890 | if (info == null) { |
| 3891 | return; |
| 3892 | } |
| 3893 | if (info.mParentClient != cs) { |
| 3894 | throw new SecurityException("Only the owner client can clear" |
| 3895 | + " ActivityViewGeometry for display #" + childDisplayId); |
| 3896 | } |
| 3897 | mActivityViewDisplayIdToParentMap.remove(childDisplayId); |
| 3898 | return; |
| 3899 | } |
| 3900 | |
| 3901 | ActivityViewInfo info = mActivityViewDisplayIdToParentMap.get(childDisplayId); |
| 3902 | if (info != null && info.mParentClient != cs) { |
| 3903 | throw new InvalidParameterException("Display #" + childDisplayId |
| 3904 | + " is already registered by " + info.mParentClient); |
| 3905 | } |
| 3906 | if (info == null) { |
| 3907 | if (!mWindowManagerInternal.isUidAllowedOnDisplay(childDisplayId, cs.uid)) { |
| 3908 | throw new SecurityException(cs + " cannot access to display #" |
| 3909 | + childDisplayId); |
| 3910 | } |
| 3911 | info = new ActivityViewInfo(cs, new Matrix()); |
| 3912 | mActivityViewDisplayIdToParentMap.put(childDisplayId, info); |
| 3913 | } |
| 3914 | info.mMatrix.setValues(matrixValues); |
| 3915 | |
| 3916 | if (mCurClient == null || mCurClient.curSession == null) { |
| 3917 | return; |
| 3918 | } |
| 3919 | |
| 3920 | Matrix matrix = null; |
| 3921 | int displayId = mCurClient.selfReportedDisplayId; |
| 3922 | boolean needToNotify = false; |
| 3923 | while (true) { |
| 3924 | needToNotify |= (displayId == childDisplayId); |
| 3925 | final ActivityViewInfo next = mActivityViewDisplayIdToParentMap.get(displayId); |
| 3926 | if (next == null) { |
| 3927 | break; |
| 3928 | } |
| 3929 | if (matrix == null) { |
| 3930 | matrix = new Matrix(next.mMatrix); |
| 3931 | } else { |
| 3932 | matrix.postConcat(next.mMatrix); |
| 3933 | } |
| 3934 | if (next.mParentClient.selfReportedDisplayId == mCurTokenDisplayId) { |
| 3935 | if (needToNotify) { |
| 3936 | final float[] values = new float[9]; |
| 3937 | matrix.getValues(values); |
| 3938 | try { |
| 3939 | mCurClient.client.updateActivityViewToScreenMatrix(mCurSeq, values); |
| 3940 | } catch (RemoteException e) { |
| 3941 | } |
| 3942 | } |
| 3943 | break; |
| 3944 | } |
| 3945 | displayId = info.mParentClient.selfReportedDisplayId; |
| 3946 | } |
| 3947 | } |
| 3948 | } |
| 3949 | |
Yunfan Chen | c02a5ac | 2020-06-16 01:52:41 +0000 | [diff] [blame] | 3950 | @Override |
| 3951 | public void removeImeSurface() { |
| 3952 | mContext.enforceCallingPermission(Manifest.permission.INTERNAL_SYSTEM_WINDOW, null); |
| 3953 | mHandler.sendMessage(mHandler.obtainMessage(MSG_REMOVE_IME_SURFACE)); |
| 3954 | } |
| 3955 | |
Yohei Yukawa | c54c117 | 2018-09-06 11:39:50 -0700 | [diff] [blame] | 3956 | @BinderThread |
Yohei Yukawa | c07fd4c | 2018-09-11 11:37:13 -0700 | [diff] [blame] | 3957 | private void notifyUserAction(@NonNull IBinder token) { |
Satoshi Kataoka | d7443c8 | 2013-10-15 17:45:43 +0900 | [diff] [blame] | 3958 | if (DEBUG) { |
Yohei Yukawa | c07fd4c | 2018-09-11 11:37:13 -0700 | [diff] [blame] | 3959 | Slog.d(TAG, "Got the notification of a user action."); |
Satoshi Kataoka | d7443c8 | 2013-10-15 17:45:43 +0900 | [diff] [blame] | 3960 | } |
Yohei Yukawa | 5a647b69 | 2014-05-22 12:49:00 +0900 | [diff] [blame] | 3961 | synchronized (mMethodMap) { |
Yohei Yukawa | c07fd4c | 2018-09-11 11:37:13 -0700 | [diff] [blame] | 3962 | if (mCurToken != token) { |
Yohei Yukawa | 3d1e812 | 2014-06-06 19:12:47 +0900 | [diff] [blame] | 3963 | if (DEBUG) { |
Yohei Yukawa | c07fd4c | 2018-09-11 11:37:13 -0700 | [diff] [blame] | 3964 | Slog.d(TAG, "Ignoring the user action notification from IMEs that are no longer" |
| 3965 | + " active."); |
Yohei Yukawa | 3d1e812 | 2014-06-06 19:12:47 +0900 | [diff] [blame] | 3966 | } |
| 3967 | return; |
| 3968 | } |
Yohei Yukawa | 5a647b69 | 2014-05-22 12:49:00 +0900 | [diff] [blame] | 3969 | final InputMethodInfo imi = mMethodMap.get(mCurMethodId); |
| 3970 | if (imi != null) { |
Yohei Yukawa | 0297051 | 2014-06-05 16:16:18 +0900 | [diff] [blame] | 3971 | mSwitchingController.onUserActionLocked(imi, mCurrentSubtype); |
Yohei Yukawa | 5a647b69 | 2014-05-22 12:49:00 +0900 | [diff] [blame] | 3972 | } |
Satoshi Kataoka | d7443c8 | 2013-10-15 17:45:43 +0900 | [diff] [blame] | 3973 | } |
| 3974 | } |
| 3975 | |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 3976 | @BinderThread |
| 3977 | private void reportPreRendered(IBinder token, EditorInfo info) { |
| 3978 | synchronized (mMethodMap) { |
| 3979 | if (!calledWithValidTokenLocked(token)) { |
| 3980 | return; |
| 3981 | } |
| 3982 | if (mCurClient != null && mCurClient.client != null) { |
| 3983 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO( |
| 3984 | MSG_REPORT_PRE_RENDERED, info, mCurClient)); |
| 3985 | } |
| 3986 | } |
| 3987 | } |
| 3988 | |
| 3989 | @BinderThread |
Tarandeep Singh | bb0e2f7 | 2020-01-15 13:58:29 -0800 | [diff] [blame] | 3990 | private void applyImeVisibility(IBinder token, IBinder windowToken, boolean setVisible) { |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 3991 | synchronized (mMethodMap) { |
| 3992 | if (!calledWithValidTokenLocked(token)) { |
| 3993 | return; |
| 3994 | } |
Tarandeep Singh | 500a38f | 2019-09-26 13:36:40 -0700 | [diff] [blame] | 3995 | if (!setVisible) { |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 3996 | if (mCurClient != null) { |
| 3997 | // IMMS only knows of focused window, not the actual IME target. |
| 3998 | // e.g. it isn't aware of any window that has both |
| 3999 | // NOT_FOCUSABLE, ALT_FOCUSABLE_IM flags set and can the IME target. |
| 4000 | // Send it to window manager to hide IME from IME target window. |
| 4001 | // TODO(b/139861270): send to mCurClient.client once IMMS is aware of |
| 4002 | // actual IME target. |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 4003 | mWindowManagerInternal.hideIme(mHideRequestWindowMap.get(windowToken)); |
Tarandeep Singh | 500a38f | 2019-09-26 13:36:40 -0700 | [diff] [blame] | 4004 | } |
| 4005 | } else { |
| 4006 | // Send to window manager to show IME after IME layout finishes. |
Tarandeep Singh | bb0e2f7 | 2020-01-15 13:58:29 -0800 | [diff] [blame] | 4007 | mWindowManagerInternal.showImePostLayout(mShowRequestWindowMap.get(windowToken)); |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 4008 | } |
| 4009 | } |
| 4010 | } |
| 4011 | |
Yohei Yukawa | 4e02bc6 | 2014-06-04 18:37:20 +0900 | [diff] [blame] | 4012 | private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) { |
| 4013 | if (token == null) { |
| 4014 | if (mContext.checkCallingOrSelfPermission( |
| 4015 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 4016 | != PackageManager.PERMISSION_GRANTED) { |
| 4017 | throw new SecurityException( |
| 4018 | "Using null token requires permission " |
| 4019 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4020 | } |
Yohei Yukawa | 4e02bc6 | 2014-06-04 18:37:20 +0900 | [diff] [blame] | 4021 | } else if (mCurToken != token) { |
| 4022 | Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid() |
| 4023 | + " token: " + token); |
| 4024 | return; |
| 4025 | } |
| 4026 | |
| 4027 | final long ident = Binder.clearCallingIdentity(); |
| 4028 | try { |
| 4029 | setInputMethodLocked(id, subtypeId); |
| 4030 | } finally { |
| 4031 | Binder.restoreCallingIdentity(ident); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4032 | } |
| 4033 | } |
| 4034 | |
Yohei Yukawa | eec552e | 2018-09-09 20:48:41 -0700 | [diff] [blame] | 4035 | @BinderThread |
| 4036 | private void hideMySoftInput(@NonNull IBinder token, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4037 | synchronized (mMethodMap) { |
Yohei Yukawa | 79e4c6a | 2018-12-09 19:07:17 -0800 | [diff] [blame] | 4038 | if (!calledWithValidTokenLocked(token)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4039 | return; |
| 4040 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4041 | long ident = Binder.clearCallingIdentity(); |
| 4042 | try { |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 4043 | hideCurrentInputLocked( |
| 4044 | mLastImeTargetWindow, flags, null, |
| 4045 | SoftInputShowHideReason.HIDE_MY_SOFT_INPUT); |
| 4046 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 4047 | } finally { |
| 4048 | Binder.restoreCallingIdentity(ident); |
| 4049 | } |
| 4050 | } |
| 4051 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4052 | |
Yohei Yukawa | eec552e | 2018-09-09 20:48:41 -0700 | [diff] [blame] | 4053 | @BinderThread |
| 4054 | private void showMySoftInput(@NonNull IBinder token, int flags) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 4055 | synchronized (mMethodMap) { |
Yohei Yukawa | 79e4c6a | 2018-12-09 19:07:17 -0800 | [diff] [blame] | 4056 | if (!calledWithValidTokenLocked(token)) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 4057 | return; |
| 4058 | } |
| 4059 | long ident = Binder.clearCallingIdentity(); |
| 4060 | try { |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 4061 | showCurrentInputLocked(mLastImeTargetWindow, flags, null, |
| 4062 | SoftInputShowHideReason.SHOW_MY_SOFT_INPUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4063 | } finally { |
| 4064 | Binder.restoreCallingIdentity(ident); |
| 4065 | } |
| 4066 | } |
| 4067 | } |
| 4068 | |
| 4069 | void setEnabledSessionInMainThread(SessionState session) { |
| 4070 | if (mEnabledSession != session) { |
Yohei Yukawa | 9d91b43 | 2014-05-19 16:03:24 +0900 | [diff] [blame] | 4071 | if (mEnabledSession != null && mEnabledSession.session != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4072 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4073 | if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession); |
Yohei Yukawa | 9d91b43 | 2014-05-19 16:03:24 +0900 | [diff] [blame] | 4074 | mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4075 | } catch (RemoteException e) { |
| 4076 | } |
| 4077 | } |
| 4078 | mEnabledSession = session; |
Yohei Yukawa | 9d91b43 | 2014-05-19 16:03:24 +0900 | [diff] [blame] | 4079 | if (mEnabledSession != null && mEnabledSession.session != null) { |
| 4080 | try { |
| 4081 | if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession); |
| 4082 | mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true); |
| 4083 | } catch (RemoteException e) { |
| 4084 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4085 | } |
| 4086 | } |
| 4087 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4088 | |
Yohei Yukawa | 930328c | 2017-10-18 20:19:53 -0700 | [diff] [blame] | 4089 | @MainThread |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 4090 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4091 | public boolean handleMessage(Message msg) { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 4092 | SomeArgs args; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4093 | switch (msg.what) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 4094 | case MSG_SHOW_IM_SUBTYPE_PICKER: |
Seigo Nonaka | 14e1391 | 2015-05-06 21:04:13 -0700 | [diff] [blame] | 4095 | final boolean showAuxSubtypes; |
lumark | 0b05f9e | 2018-11-26 15:09:06 +0800 | [diff] [blame] | 4096 | final int displayId = msg.arg2; |
Seigo Nonaka | 14e1391 | 2015-05-06 21:04:13 -0700 | [diff] [blame] | 4097 | switch (msg.arg1) { |
| 4098 | case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO: |
| 4099 | // This is undocumented so far, but IMM#showInputMethodPicker() has been |
| 4100 | // implemented so that auxiliary subtypes will be excluded when the soft |
| 4101 | // keyboard is invisible. |
| 4102 | showAuxSubtypes = mInputShown; |
| 4103 | break; |
| 4104 | case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES: |
| 4105 | showAuxSubtypes = true; |
| 4106 | break; |
| 4107 | case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES: |
| 4108 | showAuxSubtypes = false; |
| 4109 | break; |
| 4110 | default: |
| 4111 | Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1); |
| 4112 | return false; |
| 4113 | } |
lumark | 0b05f9e | 2018-11-26 15:09:06 +0800 | [diff] [blame] | 4114 | showInputMethodMenu(showAuxSubtypes, displayId); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 4115 | return true; |
| 4116 | |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 4117 | case MSG_SHOW_IM_SUBTYPE_ENABLER: |
Yohei Yukawa | 41f3427 | 2015-12-14 15:41:52 -0800 | [diff] [blame] | 4118 | showInputMethodAndSubtypeEnabler((String)msg.obj); |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 4119 | return true; |
| 4120 | |
| 4121 | case MSG_SHOW_IM_CONFIG: |
| 4122 | showConfigureInputMethods(); |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 4123 | return true; |
| 4124 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4125 | // --------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4126 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4127 | case MSG_UNBIND_INPUT: |
| 4128 | try { |
| 4129 | ((IInputMethod)msg.obj).unbindInput(); |
| 4130 | } catch (RemoteException e) { |
| 4131 | // There is nothing interesting about the method dying. |
| 4132 | } |
| 4133 | return true; |
| 4134 | case MSG_BIND_INPUT: |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 4135 | args = (SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4136 | try { |
| 4137 | ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2); |
| 4138 | } catch (RemoteException e) { |
| 4139 | } |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 4140 | args.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4141 | return true; |
| 4142 | case MSG_SHOW_SOFT_INPUT: |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 4143 | args = (SomeArgs) msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4144 | try { |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 4145 | final @SoftInputShowHideReason int reason = msg.arg2; |
Craig Mautner | e4bbb1c | 2013-03-15 11:38:44 -0700 | [diff] [blame] | 4146 | if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput(" |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 4147 | + args.arg3 + ", " + msg.arg1 + ", " + args.arg2 + ") for reason: " |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 4148 | + InputMethodDebug.softInputDisplayReasonToString(reason)); |
Tarandeep Singh | bb0e2f7 | 2020-01-15 13:58:29 -0800 | [diff] [blame] | 4149 | ((IInputMethod) args.arg1).showSoftInput( |
| 4150 | (IBinder) args.arg3, msg.arg1, (ResultReceiver) args.arg2); |
Anmol Gupta | 29f209d | 2020-03-11 11:34:46 -0700 | [diff] [blame] | 4151 | mSoftInputShowHideHistory.addEntry(new SoftInputShowHideHistory.Entry( |
| 4152 | mCurClient, mCurAttribute, |
| 4153 | mWindowManagerInternal.getWindowName(mCurFocusedWindow), |
| 4154 | mCurFocusedWindowSoftInputMode, reason, mInFullscreenMode, |
| 4155 | mWindowManagerInternal.getWindowName( |
Ming-Shin Lu | d0bc171 | 2020-05-11 09:23:37 +0800 | [diff] [blame] | 4156 | mShowRequestWindowMap.get(args.arg3)), |
| 4157 | mWindowManagerInternal.getImeControlTargetNameForLogging( |
| 4158 | mCurTokenDisplayId), |
| 4159 | mWindowManagerInternal.getImeTargetNameForLogging( |
| 4160 | mCurTokenDisplayId))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4161 | } catch (RemoteException e) { |
| 4162 | } |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 4163 | args.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4164 | return true; |
| 4165 | case MSG_HIDE_SOFT_INPUT: |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 4166 | args = (SomeArgs) msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4167 | try { |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 4168 | final @SoftInputShowHideReason int reason = msg.arg1; |
Craig Mautner | e4bbb1c | 2013-03-15 11:38:44 -0700 | [diff] [blame] | 4169 | if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, " |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 4170 | + args.arg3 + ", " + args.arg2 + ") for reason: " |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 4171 | + InputMethodDebug.softInputDisplayReasonToString(reason)); |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 4172 | ((IInputMethod)args.arg1).hideSoftInput( |
| 4173 | (IBinder) args.arg3, 0, (ResultReceiver)args.arg2); |
Anmol Gupta | 29f209d | 2020-03-11 11:34:46 -0700 | [diff] [blame] | 4174 | mSoftInputShowHideHistory.addEntry(new SoftInputShowHideHistory.Entry( |
| 4175 | mCurClient, mCurAttribute, |
| 4176 | mWindowManagerInternal.getWindowName(mCurFocusedWindow), |
| 4177 | mCurFocusedWindowSoftInputMode, reason, mInFullscreenMode, |
| 4178 | mWindowManagerInternal.getWindowName( |
Ming-Shin Lu | d0bc171 | 2020-05-11 09:23:37 +0800 | [diff] [blame] | 4179 | mHideRequestWindowMap.get(args.arg3)), |
| 4180 | mWindowManagerInternal.getImeControlTargetNameForLogging( |
| 4181 | mCurTokenDisplayId), |
| 4182 | mWindowManagerInternal.getImeTargetNameForLogging( |
| 4183 | mCurTokenDisplayId))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4184 | } catch (RemoteException e) { |
| 4185 | } |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 4186 | args.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4187 | return true; |
Jorim Jaggi | 3c5d0f1 | 2016-05-24 19:04:30 -0700 | [diff] [blame] | 4188 | case MSG_HIDE_CURRENT_INPUT_METHOD: |
| 4189 | synchronized (mMethodMap) { |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 4190 | final @SoftInputShowHideReason int reason = (int) msg.obj; |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 4191 | hideCurrentInputLocked(mCurFocusedWindow, 0, null, reason); |
| 4192 | |
Jorim Jaggi | 3c5d0f1 | 2016-05-24 19:04:30 -0700 | [diff] [blame] | 4193 | } |
| 4194 | return true; |
Yohei Yukawa | c54c117 | 2018-09-06 11:39:50 -0700 | [diff] [blame] | 4195 | case MSG_INITIALIZE_IME: |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 4196 | args = (SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4197 | try { |
lumark | 90120a8 | 2018-08-15 00:33:03 +0800 | [diff] [blame] | 4198 | if (DEBUG) { |
| 4199 | Slog.v(TAG, "Sending attach of token: " + args.arg2 + " for display: " |
| 4200 | + msg.arg1); |
| 4201 | } |
Yohei Yukawa | c54c117 | 2018-09-06 11:39:50 -0700 | [diff] [blame] | 4202 | final IBinder token = (IBinder) args.arg2; |
lumark | 90120a8 | 2018-08-15 00:33:03 +0800 | [diff] [blame] | 4203 | ((IInputMethod) args.arg1).initializeInternal(token, msg.arg1, |
Yohei Yukawa | c54c117 | 2018-09-06 11:39:50 -0700 | [diff] [blame] | 4204 | new InputMethodPrivilegedOperationsImpl(this, token)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4205 | } catch (RemoteException e) { |
| 4206 | } |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 4207 | args.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4208 | return true; |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 4209 | case MSG_CREATE_SESSION: { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 4210 | args = (SomeArgs)msg.obj; |
Jeff Brown | 1951ce8 | 2013-04-04 22:45:12 -0700 | [diff] [blame] | 4211 | IInputMethod method = (IInputMethod)args.arg1; |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 4212 | InputChannel channel = (InputChannel)args.arg2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4213 | try { |
Jeff Brown | 1951ce8 | 2013-04-04 22:45:12 -0700 | [diff] [blame] | 4214 | method.createSession(channel, (IInputSessionCallback)args.arg3); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4215 | } catch (RemoteException e) { |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 4216 | } finally { |
Jeff Brown | 1951ce8 | 2013-04-04 22:45:12 -0700 | [diff] [blame] | 4217 | // Dispose the channel if the input method is not local to this process |
| 4218 | // because the remote proxy will get its own copy when unparceled. |
| 4219 | if (channel != null && Binder.isProxy(method)) { |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 4220 | channel.dispose(); |
| 4221 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4222 | } |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 4223 | args.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4224 | return true; |
Jeff Brown | c28867a | 2013-03-26 15:42:39 -0700 | [diff] [blame] | 4225 | } |
Yunfan Chen | c02a5ac | 2020-06-16 01:52:41 +0000 | [diff] [blame] | 4226 | case MSG_REMOVE_IME_SURFACE: { |
| 4227 | try { |
| 4228 | if (mEnabledSession != null && mEnabledSession.session != null) { |
| 4229 | mEnabledSession.session.removeImeSurface(); |
| 4230 | } |
| 4231 | } catch (RemoteException e) { |
| 4232 | } |
| 4233 | return true; |
| 4234 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4235 | // --------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4236 | |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 4237 | case MSG_START_INPUT: { |
Yohei Yukawa | f7526b5 | 2017-02-11 20:57:10 -0800 | [diff] [blame] | 4238 | final int missingMethods = msg.arg1; |
| 4239 | final boolean restarting = msg.arg2 != 0; |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 4240 | args = (SomeArgs) msg.obj; |
Yohei Yukawa | 6db3bfe | 2017-02-13 12:04:41 -0800 | [diff] [blame] | 4241 | final IBinder startInputToken = (IBinder) args.arg1; |
| 4242 | final SessionState session = (SessionState) args.arg2; |
| 4243 | final IInputContext inputContext = (IInputContext) args.arg3; |
| 4244 | final EditorInfo editorInfo = (EditorInfo) args.arg4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4245 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4246 | setEnabledSessionInMainThread(session); |
Yohei Yukawa | 6db3bfe | 2017-02-13 12:04:41 -0800 | [diff] [blame] | 4247 | session.method.startInput(startInputToken, inputContext, missingMethods, |
Tarandeep Singh | eadb139 | 2018-11-09 18:15:57 +0100 | [diff] [blame] | 4248 | editorInfo, restarting, session.client.shouldPreRenderIme); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4249 | } catch (RemoteException e) { |
| 4250 | } |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 4251 | args.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4252 | return true; |
Yohei Yukawa | 19a80a1 | 2016-03-14 22:57:37 -0700 | [diff] [blame] | 4253 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4254 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4255 | // --------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4256 | |
Yohei Yukawa | 33e8179 | 2015-11-17 21:14:42 -0800 | [diff] [blame] | 4257 | case MSG_UNBIND_CLIENT: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4258 | try { |
Yohei Yukawa | 33e8179 | 2015-11-17 21:14:42 -0800 | [diff] [blame] | 4259 | ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4260 | } catch (RemoteException e) { |
| 4261 | // There is nothing interesting about the last client dying. |
| 4262 | } |
| 4263 | return true; |
Yohei Yukawa | 33e8179 | 2015-11-17 21:14:42 -0800 | [diff] [blame] | 4264 | case MSG_BIND_CLIENT: { |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 4265 | args = (SomeArgs)msg.obj; |
Jeff Brown | 1951ce8 | 2013-04-04 22:45:12 -0700 | [diff] [blame] | 4266 | IInputMethodClient client = (IInputMethodClient)args.arg1; |
| 4267 | InputBindResult res = (InputBindResult)args.arg2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4268 | try { |
Jeff Brown | 1951ce8 | 2013-04-04 22:45:12 -0700 | [diff] [blame] | 4269 | client.onBindMethod(res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4270 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4271 | Slog.w(TAG, "Client died receiving input method " + args.arg2); |
Jeff Brown | 1951ce8 | 2013-04-04 22:45:12 -0700 | [diff] [blame] | 4272 | } finally { |
| 4273 | // Dispose the channel if the input method is not local to this process |
| 4274 | // because the remote proxy will get its own copy when unparceled. |
| 4275 | if (res.channel != null && Binder.isProxy(client)) { |
| 4276 | res.channel.dispose(); |
| 4277 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4278 | } |
Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 4279 | args.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4280 | return true; |
Jeff Brown | 1951ce8 | 2013-04-04 22:45:12 -0700 | [diff] [blame] | 4281 | } |
Dianne Hackborn | a6e4134 | 2012-05-22 16:30:34 -0700 | [diff] [blame] | 4282 | case MSG_SET_ACTIVE: |
| 4283 | try { |
Yohei Yukawa | 2bc6617 | 2017-02-08 11:13:25 -0800 | [diff] [blame] | 4284 | ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0); |
Dianne Hackborn | a6e4134 | 2012-05-22 16:30:34 -0700 | [diff] [blame] | 4285 | } catch (RemoteException e) { |
| 4286 | Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid " |
| 4287 | + ((ClientState)msg.obj).pid + " uid " |
| 4288 | + ((ClientState)msg.obj).uid); |
| 4289 | } |
| 4290 | return true; |
Ming-Shin Lu | 2c6e80b | 2020-05-13 00:12:26 +0800 | [diff] [blame] | 4291 | case MSG_SET_INTERACTIVE: |
| 4292 | handleSetInteractive(msg.arg1 != 0); |
| 4293 | return true; |
Yohei Yukawa | 2bc6617 | 2017-02-08 11:13:25 -0800 | [diff] [blame] | 4294 | case MSG_REPORT_FULLSCREEN_MODE: { |
| 4295 | final boolean fullscreen = msg.arg1 != 0; |
| 4296 | final ClientState clientState = (ClientState)msg.obj; |
| 4297 | try { |
| 4298 | clientState.client.reportFullscreenMode(fullscreen); |
| 4299 | } catch (RemoteException e) { |
| 4300 | Slog.w(TAG, "Got RemoteException sending " |
| 4301 | + "reportFullscreen(" + fullscreen + ") notification to pid=" |
| 4302 | + clientState.pid + " uid=" + clientState.uid); |
| 4303 | } |
| 4304 | return true; |
| 4305 | } |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 4306 | case MSG_REPORT_PRE_RENDERED: { |
| 4307 | args = (SomeArgs) msg.obj; |
| 4308 | final EditorInfo info = (EditorInfo) args.arg1; |
| 4309 | final ClientState clientState = (ClientState) args.arg2; |
| 4310 | try { |
| 4311 | clientState.client.reportPreRendered(info); |
| 4312 | } catch (RemoteException e) { |
| 4313 | Slog.w(TAG, "Got RemoteException sending " |
| 4314 | + "reportPreRendered(" + info + ") notification to pid=" |
| 4315 | + clientState.pid + " uid=" + clientState.uid); |
| 4316 | } |
| 4317 | args.recycle(); |
| 4318 | return true; |
| 4319 | } |
| 4320 | case MSG_APPLY_IME_VISIBILITY: { |
| 4321 | final boolean setVisible = msg.arg1 != 0; |
| 4322 | final ClientState clientState = (ClientState) msg.obj; |
| 4323 | try { |
| 4324 | clientState.client.applyImeVisibility(setVisible); |
| 4325 | } catch (RemoteException e) { |
| 4326 | Slog.w(TAG, "Got RemoteException sending " |
| 4327 | + "applyImeVisibility(" + setVisible + ") notification to pid=" |
| 4328 | + clientState.pid + " uid=" + clientState.uid); |
| 4329 | } |
| 4330 | return true; |
| 4331 | } |
satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 4332 | |
| 4333 | // -------------------------------------------------------------- |
| 4334 | case MSG_HARD_KEYBOARD_SWITCH_CHANGED: |
Michael Wright | 7b5a96b | 2014-08-09 19:28:42 -0700 | [diff] [blame] | 4335 | mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1); |
satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 4336 | return true; |
Yuichiro Hanada | 34c4c0e | 2020-01-15 16:53:07 +0900 | [diff] [blame] | 4337 | case MSG_SYSTEM_UNLOCK_USER: { |
Fyodor Kupolov | 7877b8a | 2016-06-29 14:39:19 -0700 | [diff] [blame] | 4338 | final int userId = msg.arg1; |
| 4339 | onUnlockUser(userId); |
| 4340 | return true; |
Yuichiro Hanada | 34c4c0e | 2020-01-15 16:53:07 +0900 | [diff] [blame] | 4341 | } |
| 4342 | case MSG_DISPATCH_ON_INPUT_METHOD_LIST_UPDATED: { |
| 4343 | final int userId = msg.arg1; |
| 4344 | final List<InputMethodInfo> imes = (List<InputMethodInfo>) msg.obj; |
| 4345 | mInputMethodListListeners.forEach( |
| 4346 | listener -> listener.onInputMethodListUpdated(imes, userId)); |
| 4347 | return true; |
| 4348 | } |
Adam He | bc67f2e | 2019-11-13 14:34:56 -0800 | [diff] [blame] | 4349 | |
| 4350 | // --------------------------------------------------------------- |
Yohei Yukawa | 6e9768e | 2020-05-14 18:14:24 -0700 | [diff] [blame] | 4351 | case MSG_INLINE_SUGGESTIONS_REQUEST: { |
Adam He | bc67f2e | 2019-11-13 14:34:56 -0800 | [diff] [blame] | 4352 | args = (SomeArgs) msg.obj; |
Feng Cao | 36960ee | 2020-02-18 18:23:30 -0800 | [diff] [blame] | 4353 | final InlineSuggestionsRequestInfo requestInfo = |
| 4354 | (InlineSuggestionsRequestInfo) args.arg2; |
Adam He | bc67f2e | 2019-11-13 14:34:56 -0800 | [diff] [blame] | 4355 | final IInlineSuggestionsRequestCallback callback = |
Feng Cao | 36960ee | 2020-02-18 18:23:30 -0800 | [diff] [blame] | 4356 | (IInlineSuggestionsRequestCallback) args.arg3; |
Adam He | bc67f2e | 2019-11-13 14:34:56 -0800 | [diff] [blame] | 4357 | try { |
Feng Cao | 36960ee | 2020-02-18 18:23:30 -0800 | [diff] [blame] | 4358 | ((IInputMethod) args.arg1).onCreateInlineSuggestionsRequest(requestInfo, |
| 4359 | callback); |
Adam He | bc67f2e | 2019-11-13 14:34:56 -0800 | [diff] [blame] | 4360 | } catch (RemoteException e) { |
| 4361 | Slog.w(TAG, "RemoteException calling onCreateInlineSuggestionsRequest(): " + e); |
| 4362 | } |
Yohei Yukawa | 6e9768e | 2020-05-14 18:14:24 -0700 | [diff] [blame] | 4363 | args.recycle(); |
Adam He | bc67f2e | 2019-11-13 14:34:56 -0800 | [diff] [blame] | 4364 | return true; |
Yohei Yukawa | 6e9768e | 2020-05-14 18:14:24 -0700 | [diff] [blame] | 4365 | } |
Yohei Yukawa | d5f402b | 2020-05-15 10:23:32 -0700 | [diff] [blame] | 4366 | |
| 4367 | // --------------------------------------------------------------- |
| 4368 | case MSG_NOTIFY_IME_UID_TO_AUDIO_SERVICE: { |
| 4369 | if (mAudioManagerInternal == null) { |
| 4370 | mAudioManagerInternal = LocalServices.getService(AudioManagerInternal.class); |
| 4371 | } |
| 4372 | if (mAudioManagerInternal != null) { |
| 4373 | mAudioManagerInternal.setInputMethodServiceUid(msg.arg1 /* uid */); |
| 4374 | } |
| 4375 | return true; |
| 4376 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4377 | } |
| 4378 | return false; |
| 4379 | } |
| 4380 | |
Ming-Shin Lu | 2c6e80b | 2020-05-13 00:12:26 +0800 | [diff] [blame] | 4381 | private void handleSetInteractive(final boolean interactive) { |
| 4382 | synchronized (mMethodMap) { |
| 4383 | mIsInteractive = interactive; |
| 4384 | updateSystemUiLocked(interactive ? mImeWindowVis : 0, mBackDisposition); |
| 4385 | |
| 4386 | // Inform the current client of the change in active status |
| 4387 | if (mCurClient != null && mCurClient.client != null) { |
| 4388 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO( |
| 4389 | MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0, |
| 4390 | mCurClient)); |
| 4391 | } |
| 4392 | } |
| 4393 | } |
| 4394 | |
satok | dc9ddae | 2011-10-06 12:22:36 +0900 | [diff] [blame] | 4395 | private boolean chooseNewDefaultIMELocked() { |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 4396 | final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME( |
| 4397 | mSettings.getEnabledInputMethodListLocked()); |
satok | dc9ddae | 2011-10-06 12:22:36 +0900 | [diff] [blame] | 4398 | if (imi != null) { |
satok | 03eb319a | 2010-11-11 18:17:42 +0900 | [diff] [blame] | 4399 | if (DEBUG) { |
| 4400 | Slog.d(TAG, "New default IME was selected: " + imi.getId()); |
| 4401 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 4402 | resetSelectedInputMethodAndSubtypeLocked(imi.getId()); |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 4403 | return true; |
| 4404 | } |
| 4405 | |
| 4406 | return false; |
| 4407 | } |
| 4408 | |
Yohei Yukawa | 0513932 | 2018-12-25 10:34:14 -0800 | [diff] [blame] | 4409 | static void queryInputMethodServicesInternal(Context context, |
| 4410 | @UserIdInt int userId, ArrayMap<String, List<InputMethodSubtype>> additionalSubtypeMap, |
| 4411 | ArrayMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) { |
| 4412 | methodList.clear(); |
| 4413 | methodMap.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4414 | |
Yohei Yukawa | ed4952a | 2016-02-17 07:57:25 -0800 | [diff] [blame] | 4415 | // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default |
| 4416 | // behavior of PackageManager is exactly what we want. It by default picks up appropriate |
| 4417 | // services depending on the unlock state for the specified user. |
Yohei Yukawa | 0513932 | 2018-12-25 10:34:14 -0800 | [diff] [blame] | 4418 | final List<ResolveInfo> services = context.getPackageManager().queryIntentServicesAsUser( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4419 | new Intent(InputMethod.SERVICE_INTERFACE), |
Yohei Yukawa | 5ec1fce | 2018-12-17 07:56:14 -0800 | [diff] [blame] | 4420 | PackageManager.GET_META_DATA | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, |
Yohei Yukawa | 0513932 | 2018-12-25 10:34:14 -0800 | [diff] [blame] | 4421 | userId); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4422 | |
Yohei Yukawa | 0513932 | 2018-12-25 10:34:14 -0800 | [diff] [blame] | 4423 | methodList.ensureCapacity(services.size()); |
| 4424 | methodMap.ensureCapacity(services.size()); |
| 4425 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4426 | for (int i = 0; i < services.size(); ++i) { |
| 4427 | ResolveInfo ri = services.get(i); |
| 4428 | ServiceInfo si = ri.serviceInfo; |
Yohei Yukawa | ddad4b9 | 2017-02-02 01:46:13 -0800 | [diff] [blame] | 4429 | final String imeId = InputMethodInfo.computeId(ri); |
| 4430 | if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) { |
| 4431 | Slog.w(TAG, "Skipping input method " + imeId |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4432 | + ": it does not require the permission " |
| 4433 | + android.Manifest.permission.BIND_INPUT_METHOD); |
| 4434 | continue; |
| 4435 | } |
| 4436 | |
Yohei Yukawa | ddad4b9 | 2017-02-02 01:46:13 -0800 | [diff] [blame] | 4437 | if (DEBUG) Slog.d(TAG, "Checking " + imeId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4438 | |
| 4439 | try { |
Yohei Yukawa | 0513932 | 2018-12-25 10:34:14 -0800 | [diff] [blame] | 4440 | final InputMethodInfo imi = new InputMethodInfo(context, ri, |
| 4441 | additionalSubtypeMap.get(imeId)); |
Yohei Yukawa | 2988d2d | 2019-01-22 21:29:09 -0800 | [diff] [blame] | 4442 | if (imi.isVrOnly()) { |
| 4443 | continue; // Skip VR-only IME, which isn't supported for now. |
| 4444 | } |
Yohei Yukawa | 0513932 | 2018-12-25 10:34:14 -0800 | [diff] [blame] | 4445 | methodList.add(imi); |
| 4446 | methodMap.put(imi.getId(), imi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4447 | if (DEBUG) { |
Yohei Yukawa | 0513932 | 2018-12-25 10:34:14 -0800 | [diff] [blame] | 4448 | Slog.d(TAG, "Found an input method " + imi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4449 | } |
Tadashi G. Takaoka | 3c23d5b | 2016-09-16 11:41:07 +0900 | [diff] [blame] | 4450 | } catch (Exception e) { |
Yohei Yukawa | ddad4b9 | 2017-02-02 01:46:13 -0800 | [diff] [blame] | 4451 | Slog.wtf(TAG, "Unable to load input method " + imeId, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4452 | } |
| 4453 | } |
Yohei Yukawa | 0513932 | 2018-12-25 10:34:14 -0800 | [diff] [blame] | 4454 | } |
| 4455 | |
| 4456 | @GuardedBy("mMethodMap") |
| 4457 | void buildInputMethodListLocked(boolean resetDefaultEnabledIme) { |
| 4458 | if (DEBUG) { |
| 4459 | Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme |
| 4460 | + " \n ------ caller=" + Debug.getCallers(10)); |
| 4461 | } |
| 4462 | if (!mSystemReady) { |
| 4463 | Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready"); |
| 4464 | return; |
| 4465 | } |
| 4466 | mMethodMapUpdateCount++; |
| 4467 | mMyPackageMonitor.clearKnownImePackageNamesLocked(); |
| 4468 | |
| 4469 | queryInputMethodServicesInternal(mContext, mSettings.getCurrentUserId(), |
Yohei Yukawa | b557d57 | 2018-12-29 21:26:26 -0800 | [diff] [blame] | 4470 | mAdditionalSubtypeMap, mMethodMap, mMethodList); |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 4471 | |
Yohei Yukawa | c4e4491 | 2017-02-09 19:30:22 -0800 | [diff] [blame] | 4472 | // Construct the set of possible IME packages for onPackageChanged() to avoid false |
| 4473 | // negatives when the package state remains to be the same but only the component state is |
| 4474 | // changed. |
| 4475 | { |
| 4476 | // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose |
| 4477 | // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more |
| 4478 | // conservative, but it seems we cannot use it for now (Issue 35176630). |
Yohei Yukawa | 0513932 | 2018-12-25 10:34:14 -0800 | [diff] [blame] | 4479 | final List<ResolveInfo> allInputMethodServices = |
| 4480 | mContext.getPackageManager().queryIntentServicesAsUser( |
| 4481 | new Intent(InputMethod.SERVICE_INTERFACE), |
| 4482 | PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId()); |
Yohei Yukawa | c4e4491 | 2017-02-09 19:30:22 -0800 | [diff] [blame] | 4483 | final int N = allInputMethodServices.size(); |
| 4484 | for (int i = 0; i < N; ++i) { |
| 4485 | final ServiceInfo si = allInputMethodServices.get(i).serviceInfo; |
Yohei Yukawa | 5e3e8a5 | 2017-02-14 20:05:17 -0800 | [diff] [blame] | 4486 | if (android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) { |
| 4487 | mMyPackageMonitor.addKnownImePackageNameLocked(si.packageName); |
Yohei Yukawa | c4e4491 | 2017-02-09 19:30:22 -0800 | [diff] [blame] | 4488 | } |
Yohei Yukawa | c4e4491 | 2017-02-09 19:30:22 -0800 | [diff] [blame] | 4489 | } |
| 4490 | } |
| 4491 | |
Yohei Yukawa | 9c37219 | 2018-03-20 22:54:56 -0700 | [diff] [blame] | 4492 | boolean reenableMinimumNonAuxSystemImes = false; |
Yohei Yukawa | 859df05 | 2016-02-17 07:56:46 -0800 | [diff] [blame] | 4493 | // TODO: The following code should find better place to live. |
| 4494 | if (!resetDefaultEnabledIme) { |
| 4495 | boolean enabledImeFound = false; |
Yohei Yukawa | 9c37219 | 2018-03-20 22:54:56 -0700 | [diff] [blame] | 4496 | boolean enabledNonAuxImeFound = false; |
Yohei Yukawa | 859df05 | 2016-02-17 07:56:46 -0800 | [diff] [blame] | 4497 | final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked(); |
| 4498 | final int N = enabledImes.size(); |
| 4499 | for (int i = 0; i < N; ++i) { |
| 4500 | final InputMethodInfo imi = enabledImes.get(i); |
| 4501 | if (mMethodList.contains(imi)) { |
| 4502 | enabledImeFound = true; |
Yohei Yukawa | 9c37219 | 2018-03-20 22:54:56 -0700 | [diff] [blame] | 4503 | if (!imi.isAuxiliaryIme()) { |
| 4504 | enabledNonAuxImeFound = true; |
| 4505 | break; |
| 4506 | } |
Yohei Yukawa | 859df05 | 2016-02-17 07:56:46 -0800 | [diff] [blame] | 4507 | } |
| 4508 | } |
| 4509 | if (!enabledImeFound) { |
Yohei Yukawa | d033283 | 2017-02-01 13:59:43 -0800 | [diff] [blame] | 4510 | if (DEBUG) { |
| 4511 | Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs."); |
| 4512 | } |
Yohei Yukawa | 859df05 | 2016-02-17 07:56:46 -0800 | [diff] [blame] | 4513 | resetDefaultEnabledIme = true; |
| 4514 | resetSelectedInputMethodAndSubtypeLocked(""); |
Yohei Yukawa | 9c37219 | 2018-03-20 22:54:56 -0700 | [diff] [blame] | 4515 | } else if (!enabledNonAuxImeFound) { |
| 4516 | if (DEBUG) { |
| 4517 | Slog.i(TAG, "All the enabled non-Aux IMEs are gone. Do partial reset."); |
| 4518 | } |
| 4519 | reenableMinimumNonAuxSystemImes = true; |
Yohei Yukawa | 859df05 | 2016-02-17 07:56:46 -0800 | [diff] [blame] | 4520 | } |
| 4521 | } |
| 4522 | |
Yohei Yukawa | 9c37219 | 2018-03-20 22:54:56 -0700 | [diff] [blame] | 4523 | if (resetDefaultEnabledIme || reenableMinimumNonAuxSystemImes) { |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 4524 | final ArrayList<InputMethodInfo> defaultEnabledIme = |
Yohei Yukawa | 9c37219 | 2018-03-20 22:54:56 -0700 | [diff] [blame] | 4525 | InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList, |
| 4526 | reenableMinimumNonAuxSystemImes); |
Yohei Yukawa | 68645a6 | 2016-02-17 07:54:20 -0800 | [diff] [blame] | 4527 | final int N = defaultEnabledIme.size(); |
| 4528 | for (int i = 0; i < N; ++i) { |
Satoshi Kataoka | f1367b7 | 2013-01-25 17:20:12 +0900 | [diff] [blame] | 4529 | final InputMethodInfo imi = defaultEnabledIme.get(i); |
| 4530 | if (DEBUG) { |
| 4531 | Slog.d(TAG, "--- enable ime = " + imi); |
| 4532 | } |
| 4533 | setInputMethodEnabledLocked(imi.getId(), true); |
| 4534 | } |
| 4535 | } |
| 4536 | |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 4537 | final String defaultImiId = mSettings.getSelectedInputMethod(); |
satok | 0a1bcf4 | 2012-05-16 19:26:31 +0900 | [diff] [blame] | 4538 | if (!TextUtils.isEmpty(defaultImiId)) { |
Yohei Yukawa | 94e3330 | 2016-02-12 19:37:03 -0800 | [diff] [blame] | 4539 | if (!mMethodMap.containsKey(defaultImiId)) { |
satok | 0a1bcf4 | 2012-05-16 19:26:31 +0900 | [diff] [blame] | 4540 | Slog.w(TAG, "Default IME is uninstalled. Choose new default IME."); |
| 4541 | if (chooseNewDefaultIMELocked()) { |
Michael Wright | 7b5a96b | 2014-08-09 19:28:42 -0700 | [diff] [blame] | 4542 | updateInputMethodsFromSettingsLocked(true); |
satok | 0a1bcf4 | 2012-05-16 19:26:31 +0900 | [diff] [blame] | 4543 | } |
| 4544 | } else { |
| 4545 | // Double check that the default IME is certainly enabled. |
| 4546 | setInputMethodEnabledLocked(defaultImiId, true); |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 4547 | } |
| 4548 | } |
Yohei Yukawa | 3d46bab | 2014-05-30 18:10:18 +0900 | [diff] [blame] | 4549 | // Here is not the perfect place to reset the switching controller. Ideally |
| 4550 | // mSwitchingController and mSettings should be able to share the same state. |
| 4551 | // TODO: Make sure that mSwitchingController and mSettings are sharing the |
| 4552 | // the same enabled IMEs list. |
Yohei Yukawa | c834a25 | 2014-05-21 22:42:32 +0900 | [diff] [blame] | 4553 | mSwitchingController.resetCircularListLocked(mContext); |
Yuichiro Hanada | 34c4c0e | 2020-01-15 16:53:07 +0900 | [diff] [blame] | 4554 | |
| 4555 | // Notify InputMethodListListeners of the new installed InputMethods. |
| 4556 | final List<InputMethodInfo> inputMethodList = new ArrayList<>(mMethodList); |
| 4557 | mHandler.obtainMessage(MSG_DISPATCH_ON_INPUT_METHOD_LIST_UPDATED, |
| 4558 | mSettings.getCurrentUserId(), 0 /* unused */, inputMethodList).sendToTarget(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4559 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4560 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4561 | // ---------------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4562 | |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 4563 | private void showInputMethodAndSubtypeEnabler(String inputMethodId) { |
Tadashi G. Takaoka | f49688f | 2011-01-20 17:56:13 +0900 | [diff] [blame] | 4564 | Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS); |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 4565 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
satok | 86417ea | 2010-10-27 14:11:03 +0900 | [diff] [blame] | 4566 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
| 4567 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 4568 | if (!TextUtils.isEmpty(inputMethodId)) { |
Tadashi G. Takaoka | 2548020 | 2011-01-20 23:13:02 +0900 | [diff] [blame] | 4569 | intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId); |
satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 4570 | } |
Yohei Yukawa | 41f3427 | 2015-12-14 15:41:52 -0800 | [diff] [blame] | 4571 | final int userId; |
| 4572 | synchronized (mMethodMap) { |
| 4573 | userId = mSettings.getCurrentUserId(); |
| 4574 | } |
| 4575 | mContext.startActivityAsUser(intent, null, UserHandle.of(userId)); |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 4576 | } |
| 4577 | |
| 4578 | private void showConfigureInputMethods() { |
| 4579 | Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS); |
| 4580 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 4581 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
| 4582 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
Satoshi Kataoka | 3ba439d | 2012-10-05 18:30:13 +0900 | [diff] [blame] | 4583 | mContext.startActivityAsUser(intent, null, UserHandle.CURRENT); |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 4584 | } |
| 4585 | |
satok | 2c93efc | 2012-04-02 19:33:47 +0900 | [diff] [blame] | 4586 | private boolean isScreenLocked() { |
| 4587 | return mKeyguardManager != null |
| 4588 | && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure(); |
| 4589 | } |
Tadashi G. Takaoka | d130b80 | 2014-08-01 14:05:47 +0900 | [diff] [blame] | 4590 | |
lumark | 0b05f9e | 2018-11-26 15:09:06 +0800 | [diff] [blame] | 4591 | private void showInputMethodMenu(boolean showAuxSubtypes, int displayId) { |
Seigo Nonaka | 14e1391 | 2015-05-06 21:04:13 -0700 | [diff] [blame] | 4592 | if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4593 | |
satok | 2c93efc | 2012-04-02 19:33:47 +0900 | [diff] [blame] | 4594 | final boolean isScreenLocked = isScreenLocked(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4595 | |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 4596 | final String lastInputMethodId = mSettings.getSelectedInputMethod(); |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 4597 | int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4598 | if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4599 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 4600 | synchronized (mMethodMap) { |
Yohei Yukawa | cf3bbff | 2018-11-20 17:24:20 -0800 | [diff] [blame] | 4601 | final List<ImeSubtypeListItem> imList = |
| 4602 | mSwitchingController.getSortedInputMethodAndSubtypeListLocked( |
| 4603 | showAuxSubtypes, isScreenLocked); |
| 4604 | if (imList.isEmpty()) { |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 4605 | return; |
| 4606 | } |
| 4607 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 4608 | hideInputMethodMenuLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4609 | |
satok | c369056 | 2012-01-10 20:14:43 +0900 | [diff] [blame] | 4610 | if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) { |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 4611 | final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked(); |
satok | c369056 | 2012-01-10 20:14:43 +0900 | [diff] [blame] | 4612 | if (currentSubtype != null) { |
| 4613 | final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId); |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 4614 | lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode( |
| 4615 | currentImi, currentSubtype.hashCode()); |
satok | c369056 | 2012-01-10 20:14:43 +0900 | [diff] [blame] | 4616 | } |
| 4617 | } |
| 4618 | |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 4619 | final int N = imList.size(); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 4620 | mIms = new InputMethodInfo[N]; |
| 4621 | mSubtypeIds = new int[N]; |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 4622 | int checkedItem = 0; |
| 4623 | for (int i = 0; i < N; ++i) { |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 4624 | final ImeSubtypeListItem item = imList.get(i); |
| 4625 | mIms[i] = item.mImi; |
| 4626 | mSubtypeIds[i] = item.mSubtypeId; |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 4627 | if (mIms[i].getId().equals(lastInputMethodId)) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 4628 | int subtypeId = mSubtypeIds[i]; |
| 4629 | if ((subtypeId == NOT_A_SUBTYPE_ID) |
| 4630 | || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0) |
| 4631 | || (subtypeId == lastInputMethodSubtypeId)) { |
| 4632 | checkedItem = i; |
| 4633 | } |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 4634 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4635 | } |
Alan Viverette | 505e3ab | 2014-11-24 15:22:11 -0800 | [diff] [blame] | 4636 | |
lumark | 0b05f9e | 2018-11-26 15:09:06 +0800 | [diff] [blame] | 4637 | final ActivityThread currentThread = ActivityThread.currentActivityThread(); |
Andrew Sapperstein | 8a3b4cb | 2017-04-28 14:35:31 -0700 | [diff] [blame] | 4638 | final Context settingsContext = new ContextThemeWrapper( |
lumark | 0b05f9e | 2018-11-26 15:09:06 +0800 | [diff] [blame] | 4639 | displayId == DEFAULT_DISPLAY ? currentThread.getSystemUiContext() |
| 4640 | : currentThread.createSystemUiContext(displayId), |
Alan Viverette | 505e3ab | 2014-11-24 15:22:11 -0800 | [diff] [blame] | 4641 | com.android.internal.R.style.Theme_DeviceDefault_Settings); |
| 4642 | |
| 4643 | mDialogBuilder = new AlertDialog.Builder(settingsContext); |
Tadashi G. Takaoka | d130b80 | 2014-08-01 14:05:47 +0900 | [diff] [blame] | 4644 | mDialogBuilder.setOnCancelListener(new OnCancelListener() { |
| 4645 | @Override |
| 4646 | public void onCancel(DialogInterface dialog) { |
| 4647 | hideInputMethodMenu(); |
| 4648 | } |
| 4649 | }); |
Alan Viverette | 505e3ab | 2014-11-24 15:22:11 -0800 | [diff] [blame] | 4650 | |
| 4651 | final Context dialogContext = mDialogBuilder.getContext(); |
| 4652 | final TypedArray a = dialogContext.obtainStyledAttributes(null, |
| 4653 | com.android.internal.R.styleable.DialogPreference, |
| 4654 | com.android.internal.R.attr.alertDialogStyle, 0); |
| 4655 | final Drawable dialogIcon = a.getDrawable( |
| 4656 | com.android.internal.R.styleable.DialogPreference_dialogIcon); |
| 4657 | a.recycle(); |
| 4658 | |
| 4659 | mDialogBuilder.setIcon(dialogIcon); |
| 4660 | |
Yohei Yukawa | d34e148 | 2016-02-11 08:03:52 -0800 | [diff] [blame] | 4661 | final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class); |
satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 4662 | final View tv = inflater.inflate( |
| 4663 | com.android.internal.R.layout.input_method_switch_dialog_title, null); |
| 4664 | mDialogBuilder.setCustomTitle(tv); |
| 4665 | |
| 4666 | // Setup layout for a toggle switch of the hardware keyboard |
| 4667 | mSwitchingDialogTitleView = tv; |
Tadashi G. Takaoka | d130b80 | 2014-08-01 14:05:47 +0900 | [diff] [blame] | 4668 | mSwitchingDialogTitleView |
| 4669 | .findViewById(com.android.internal.R.id.hard_keyboard_section) |
Seigo Nonaka | 7309b12 | 2015-08-17 18:34:13 -0700 | [diff] [blame] | 4670 | .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable() |
Tadashi G. Takaoka | d130b80 | 2014-08-01 14:05:47 +0900 | [diff] [blame] | 4671 | ? View.VISIBLE : View.GONE); |
Alan Viverette | 505e3ab | 2014-11-24 15:22:11 -0800 | [diff] [blame] | 4672 | final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById( |
Tadashi G. Takaoka | d130b80 | 2014-08-01 14:05:47 +0900 | [diff] [blame] | 4673 | com.android.internal.R.id.hard_keyboard_switch); |
Michael Wright | 7b5a96b | 2014-08-09 19:28:42 -0700 | [diff] [blame] | 4674 | hardKeySwitch.setChecked(mShowImeWithHardKeyboard); |
Tadashi G. Takaoka | d130b80 | 2014-08-01 14:05:47 +0900 | [diff] [blame] | 4675 | hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() { |
| 4676 | @Override |
| 4677 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
Michael Wright | 7b5a96b | 2014-08-09 19:28:42 -0700 | [diff] [blame] | 4678 | mSettings.setShowImeWithHardKeyboard(isChecked); |
Tadashi G. Takaoka | d130b80 | 2014-08-01 14:05:47 +0900 | [diff] [blame] | 4679 | // Ensure that the input method dialog is dismissed when changing |
| 4680 | // the hardware keyboard state. |
| 4681 | hideInputMethodMenu(); |
| 4682 | } |
| 4683 | }); |
| 4684 | |
Alan Viverette | 505e3ab | 2014-11-24 15:22:11 -0800 | [diff] [blame] | 4685 | final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext, |
Tadashi G. Takaoka | d130b80 | 2014-08-01 14:05:47 +0900 | [diff] [blame] | 4686 | com.android.internal.R.layout.input_method_switch_item, imList, checkedItem); |
| 4687 | final OnClickListener choiceListener = new OnClickListener() { |
| 4688 | @Override |
| 4689 | public void onClick(final DialogInterface dialog, final int which) { |
| 4690 | synchronized (mMethodMap) { |
| 4691 | if (mIms == null || mIms.length <= which || mSubtypeIds == null |
| 4692 | || mSubtypeIds.length <= which) { |
| 4693 | return; |
satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 4694 | } |
Tadashi G. Takaoka | d130b80 | 2014-08-01 14:05:47 +0900 | [diff] [blame] | 4695 | final InputMethodInfo im = mIms[which]; |
| 4696 | int subtypeId = mSubtypeIds[which]; |
| 4697 | adapter.mCheckedItem = which; |
| 4698 | adapter.notifyDataSetChanged(); |
| 4699 | hideInputMethodMenu(); |
| 4700 | if (im != null) { |
| 4701 | if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) { |
| 4702 | subtypeId = NOT_A_SUBTYPE_ID; |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 4703 | } |
Tadashi G. Takaoka | d130b80 | 2014-08-01 14:05:47 +0900 | [diff] [blame] | 4704 | setInputMethodLocked(im.getId(), subtypeId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4705 | } |
Tadashi G. Takaoka | d130b80 | 2014-08-01 14:05:47 +0900 | [diff] [blame] | 4706 | } |
| 4707 | } |
| 4708 | }; |
| 4709 | mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4710 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4711 | mSwitchingDialog = mDialogBuilder.create(); |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 4712 | mSwitchingDialog.setCanceledOnTouchOutside(true); |
Wale Ogunwale | 3a93169 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 4713 | final Window w = mSwitchingDialog.getWindow(); |
Yohei Yukawa | 6e87559 | 2019-01-28 00:49:30 -0800 | [diff] [blame] | 4714 | final LayoutParams attrs = w.getAttributes(); |
| 4715 | w.setType(LayoutParams.TYPE_INPUT_METHOD_DIALOG); |
Wale Ogunwale | 3a93169 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 4716 | // Use an alternate token for the dialog for that window manager can group the token |
| 4717 | // with other IME windows based on type vs. grouping based on whichever token happens |
| 4718 | // to get selected by the system later on. |
| 4719 | attrs.token = mSwitchingDialogToken; |
Roshan Pius | a3f89c6 | 2019-10-11 08:50:53 -0700 | [diff] [blame] | 4720 | attrs.privateFlags |= LayoutParams.SYSTEM_FLAG_SHOW_FOR_ALL_USERS; |
Wale Ogunwale | 3a93169 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 4721 | attrs.setTitle("Select input method"); |
| 4722 | w.setAttributes(attrs); |
Yohei Yukawa | 849443c | 2019-01-21 09:02:25 -0800 | [diff] [blame] | 4723 | updateSystemUiLocked(mImeWindowVis, mBackDisposition); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4724 | mSwitchingDialog.show(); |
| 4725 | } |
| 4726 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4727 | |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 4728 | private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> { |
| 4729 | private final LayoutInflater mInflater; |
| 4730 | private final int mTextViewResourceId; |
| 4731 | private final List<ImeSubtypeListItem> mItemsList; |
Satoshi Kataoka | d214296 | 2012-11-12 18:43:06 +0900 | [diff] [blame] | 4732 | public int mCheckedItem; |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 4733 | public ImeSubtypeListAdapter(Context context, int textViewResourceId, |
| 4734 | List<ImeSubtypeListItem> itemsList, int checkedItem) { |
| 4735 | super(context, textViewResourceId, itemsList); |
Alan Viverette | 505e3ab | 2014-11-24 15:22:11 -0800 | [diff] [blame] | 4736 | |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 4737 | mTextViewResourceId = textViewResourceId; |
| 4738 | mItemsList = itemsList; |
| 4739 | mCheckedItem = checkedItem; |
Yohei Yukawa | d34e148 | 2016-02-11 08:03:52 -0800 | [diff] [blame] | 4740 | mInflater = context.getSystemService(LayoutInflater.class); |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 4741 | } |
| 4742 | |
| 4743 | @Override |
| 4744 | public View getView(int position, View convertView, ViewGroup parent) { |
| 4745 | final View view = convertView != null ? convertView |
| 4746 | : mInflater.inflate(mTextViewResourceId, null); |
| 4747 | if (position < 0 || position >= mItemsList.size()) return view; |
| 4748 | final ImeSubtypeListItem item = mItemsList.get(position); |
| 4749 | final CharSequence imeName = item.mImeName; |
| 4750 | final CharSequence subtypeName = item.mSubtypeName; |
| 4751 | final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1); |
| 4752 | final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2); |
| 4753 | if (TextUtils.isEmpty(subtypeName)) { |
| 4754 | firstTextView.setText(imeName); |
| 4755 | secondTextView.setVisibility(View.GONE); |
| 4756 | } else { |
| 4757 | firstTextView.setText(subtypeName); |
| 4758 | secondTextView.setText(imeName); |
| 4759 | secondTextView.setVisibility(View.VISIBLE); |
| 4760 | } |
| 4761 | final RadioButton radioButton = |
| 4762 | (RadioButton)view.findViewById(com.android.internal.R.id.radio); |
| 4763 | radioButton.setChecked(position == mCheckedItem); |
| 4764 | return view; |
| 4765 | } |
| 4766 | } |
| 4767 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4768 | void hideInputMethodMenu() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4769 | synchronized (mMethodMap) { |
| 4770 | hideInputMethodMenuLocked(); |
| 4771 | } |
| 4772 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4773 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4774 | void hideInputMethodMenuLocked() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4775 | if (DEBUG) Slog.v(TAG, "Hide switching menu"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4776 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4777 | if (mSwitchingDialog != null) { |
| 4778 | mSwitchingDialog.dismiss(); |
| 4779 | mSwitchingDialog = null; |
Yohei Yukawa | 1fc7a1d | 2018-04-18 17:31:27 -0700 | [diff] [blame] | 4780 | mSwitchingDialogTitleView = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4781 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4782 | |
Yohei Yukawa | 849443c | 2019-01-21 09:02:25 -0800 | [diff] [blame] | 4783 | updateSystemUiLocked(mImeWindowVis, mBackDisposition); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4784 | mDialogBuilder = null; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4785 | mIms = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4786 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4787 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4788 | // ---------------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4789 | |
Yohei Yukawa | 3d3abd2 | 2019-04-09 23:20:12 -0700 | [diff] [blame] | 4790 | /** |
| 4791 | * Enable or disable the given IME by updating {@link Settings.Secure#ENABLED_INPUT_METHODS}. |
| 4792 | * |
| 4793 | * @param id ID of the IME is to be manipulated. It is OK to pass IME ID that is currently not |
| 4794 | * recognized by the system. |
| 4795 | * @param enabled {@code true} if {@code id} needs to be enabled. |
| 4796 | * @return {@code true} if the IME was previously enabled. {@code false} otherwise. |
| 4797 | */ |
| 4798 | private boolean setInputMethodEnabledLocked(String id, boolean enabled) { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 4799 | List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings |
| 4800 | .getEnabledInputMethodsAndSubtypeListLocked(); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4801 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 4802 | if (enabled) { |
| 4803 | for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) { |
| 4804 | if (pair.first.equals(id)) { |
| 4805 | // We are enabling this input method, but it is already enabled. |
| 4806 | // Nothing to do. The previous state was enabled. |
| 4807 | return true; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4808 | } |
| 4809 | } |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 4810 | mSettings.appendAndPutEnabledInputMethodLocked(id, false); |
| 4811 | // Previous state was disabled. |
| 4812 | return false; |
| 4813 | } else { |
| 4814 | StringBuilder builder = new StringBuilder(); |
| 4815 | if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked( |
| 4816 | builder, enabledInputMethodsList, id)) { |
| 4817 | // Disabled input method is currently selected, switch to another one. |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 4818 | final String selId = mSettings.getSelectedInputMethod(); |
satok | 03eb319a | 2010-11-11 18:17:42 +0900 | [diff] [blame] | 4819 | if (id.equals(selId) && !chooseNewDefaultIMELocked()) { |
| 4820 | Slog.i(TAG, "Can't find new IME, unsetting the current input method."); |
| 4821 | resetSelectedInputMethodAndSubtypeLocked(""); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 4822 | } |
| 4823 | // Previous state was enabled. |
| 4824 | return true; |
| 4825 | } else { |
| 4826 | // We are disabling the input method but it is already disabled. |
| 4827 | // Nothing to do. The previous state was disabled. |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4828 | return false; |
| 4829 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4830 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4831 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 4832 | |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 4833 | private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId, |
| 4834 | boolean setSubtypeOnly) { |
Yohei Yukawa | 2988d2d | 2019-01-22 21:29:09 -0800 | [diff] [blame] | 4835 | mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype); |
Tarandeep Singh | 89a6c48 | 2017-11-21 14:26:11 -0800 | [diff] [blame] | 4836 | |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 4837 | // Set Subtype here |
| 4838 | if (imi == null || subtypeId < 0) { |
| 4839 | mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID); |
Tadashi G. Takaoka | 0ba75bb | 2010-11-09 12:19:32 -0800 | [diff] [blame] | 4840 | mCurrentSubtype = null; |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 4841 | } else { |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 4842 | if (subtypeId < imi.getSubtypeCount()) { |
| 4843 | InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId); |
| 4844 | mSettings.putSelectedSubtype(subtype.hashCode()); |
| 4845 | mCurrentSubtype = subtype; |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 4846 | } else { |
| 4847 | mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID); |
satok | d81e950 | 2012-05-21 12:58:45 +0900 | [diff] [blame] | 4848 | // If the subtype is not specified, choose the most applicable one |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 4849 | mCurrentSubtype = getCurrentInputMethodSubtypeLocked(); |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 4850 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 4851 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 4852 | |
Yohei Yukawa | 68645a6 | 2016-02-17 07:54:20 -0800 | [diff] [blame] | 4853 | if (!setSubtypeOnly) { |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 4854 | // Set InputMethod here |
| 4855 | mSettings.putSelectedInputMethod(imi != null ? imi.getId() : ""); |
| 4856 | } |
| 4857 | } |
| 4858 | |
| 4859 | private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) { |
| 4860 | InputMethodInfo imi = mMethodMap.get(newDefaultIme); |
| 4861 | int lastSubtypeId = NOT_A_SUBTYPE_ID; |
| 4862 | // newDefaultIme is empty when there is no candidate for the selected IME. |
| 4863 | if (imi != null && !TextUtils.isEmpty(newDefaultIme)) { |
| 4864 | String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme); |
| 4865 | if (subtypeHashCode != null) { |
| 4866 | try { |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 4867 | lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode( |
Narayan Kamath | a09b4d2 | 2016-04-15 18:32:45 +0100 | [diff] [blame] | 4868 | imi, Integer.parseInt(subtypeHashCode)); |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 4869 | } catch (NumberFormatException e) { |
| 4870 | Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e); |
| 4871 | } |
| 4872 | } |
| 4873 | } |
| 4874 | setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 4875 | } |
| 4876 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 4877 | /** |
| 4878 | * @return Return the current subtype of this input method. |
| 4879 | */ |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 4880 | @Override |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 4881 | public InputMethodSubtype getCurrentInputMethodSubtype() { |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 4882 | synchronized (mMethodMap) { |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 4883 | // TODO: Make this work even for non-current users? |
Yohei Yukawa | 46d7476 | 2019-01-22 10:17:22 -0800 | [diff] [blame] | 4884 | if (!calledFromValidUserLocked()) { |
Yohei Yukawa | 91e6cd0 | 2018-12-09 22:58:57 -0800 | [diff] [blame] | 4885 | return null; |
| 4886 | } |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 4887 | return getCurrentInputMethodSubtypeLocked(); |
| 4888 | } |
| 4889 | } |
| 4890 | |
| 4891 | private InputMethodSubtype getCurrentInputMethodSubtypeLocked() { |
satok | fdf419e | 2012-05-08 16:52:08 +0900 | [diff] [blame] | 4892 | if (mCurMethodId == null) { |
| 4893 | return null; |
| 4894 | } |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 4895 | final boolean subtypeIsSelected = mSettings.isSubtypeSelected(); |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 4896 | final InputMethodInfo imi = mMethodMap.get(mCurMethodId); |
| 4897 | if (imi == null || imi.getSubtypeCount() == 0) { |
| 4898 | return null; |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 4899 | } |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 4900 | if (!subtypeIsSelected || mCurrentSubtype == null |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 4901 | || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) { |
| 4902 | int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId); |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 4903 | if (subtypeId == NOT_A_SUBTYPE_ID) { |
| 4904 | // If there are no selected subtypes, the framework will try to find |
| 4905 | // the most applicable subtype from explicitly or implicitly enabled |
| 4906 | // subtypes. |
| 4907 | List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes = |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 4908 | mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true); |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 4909 | // If there is only one explicitly or implicitly enabled subtype, |
| 4910 | // just returns it. |
| 4911 | if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) { |
| 4912 | mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0); |
| 4913 | } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) { |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 4914 | mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked( |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 4915 | mRes, explicitlyOrImplicitlyEnabledSubtypes, |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 4916 | InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true); |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 4917 | if (mCurrentSubtype == null) { |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 4918 | mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked( |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 4919 | mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null, |
| 4920 | true); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 4921 | } |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 4922 | } |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 4923 | } else { |
Satoshi Kataoka | 8e303cc | 2013-01-11 15:55:28 +0900 | [diff] [blame] | 4924 | mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId); |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 4925 | } |
| 4926 | } |
Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 4927 | return mCurrentSubtype; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 4928 | } |
| 4929 | |
Yohei Yukawa | a878b95 | 2019-01-10 19:36:24 -0800 | [diff] [blame] | 4930 | private List<InputMethodInfo> getInputMethodListAsUser(@UserIdInt int userId) { |
| 4931 | synchronized (mMethodMap) { |
Yohei Yukawa | 2988d2d | 2019-01-22 21:29:09 -0800 | [diff] [blame] | 4932 | return getInputMethodListLocked(userId); |
Yohei Yukawa | a878b95 | 2019-01-10 19:36:24 -0800 | [diff] [blame] | 4933 | } |
| 4934 | } |
| 4935 | |
| 4936 | private List<InputMethodInfo> getEnabledInputMethodListAsUser(@UserIdInt int userId) { |
| 4937 | synchronized (mMethodMap) { |
| 4938 | return getEnabledInputMethodListLocked(userId); |
| 4939 | } |
| 4940 | } |
| 4941 | |
Feng Cao | 16b2de5 | 2020-01-09 17:27:27 -0800 | [diff] [blame] | 4942 | private void onCreateInlineSuggestionsRequest(@UserIdInt int userId, |
Feng Cao | 36960ee | 2020-02-18 18:23:30 -0800 | [diff] [blame] | 4943 | InlineSuggestionsRequestInfo requestInfo, |
Feng Cao | 16b2de5 | 2020-01-09 17:27:27 -0800 | [diff] [blame] | 4944 | IInlineSuggestionsRequestCallback callback) { |
Adam He | bc67f2e | 2019-11-13 14:34:56 -0800 | [diff] [blame] | 4945 | synchronized (mMethodMap) { |
Feng Cao | 36960ee | 2020-02-18 18:23:30 -0800 | [diff] [blame] | 4946 | onCreateInlineSuggestionsRequestLocked(userId, requestInfo, callback); |
Adam He | bc67f2e | 2019-11-13 14:34:56 -0800 | [diff] [blame] | 4947 | } |
| 4948 | } |
| 4949 | |
mincheli | 850892b | 2019-12-05 19:47:59 +0800 | [diff] [blame] | 4950 | private boolean switchToInputMethod(String imeId, @UserIdInt int userId) { |
| 4951 | synchronized (mMethodMap) { |
| 4952 | if (userId == mSettings.getCurrentUserId()) { |
| 4953 | if (!mMethodMap.containsKey(imeId) |
| 4954 | || !mSettings.getEnabledInputMethodListLocked() |
| 4955 | .contains(mMethodMap.get(imeId))) { |
| 4956 | return false; // IME is not is found or not enabled. |
| 4957 | } |
| 4958 | setInputMethodLocked(imeId, NOT_A_SUBTYPE_ID); |
| 4959 | return true; |
| 4960 | } |
| 4961 | final ArrayMap<String, InputMethodInfo> methodMap = new ArrayMap<>(); |
| 4962 | final ArrayList<InputMethodInfo> methodList = new ArrayList<>(); |
| 4963 | final ArrayMap<String, List<InputMethodSubtype>> additionalSubtypeMap = |
| 4964 | new ArrayMap<>(); |
| 4965 | AdditionalSubtypeUtils.load(additionalSubtypeMap, userId); |
| 4966 | queryInputMethodServicesInternal(mContext, userId, additionalSubtypeMap, |
| 4967 | methodMap, methodList); |
| 4968 | final InputMethodSettings settings = new InputMethodSettings( |
| 4969 | mContext.getResources(), mContext.getContentResolver(), methodMap, |
| 4970 | userId, false); |
| 4971 | if (!methodMap.containsKey(imeId) |
| 4972 | || !settings.getEnabledInputMethodListLocked() |
| 4973 | .contains(methodMap.get(imeId))) { |
| 4974 | return false; // IME is not is found or not enabled. |
| 4975 | } |
| 4976 | settings.putSelectedInputMethod(imeId); |
| 4977 | settings.putSelectedSubtype(NOT_A_SUBTYPE_ID); |
| 4978 | return true; |
| 4979 | } |
| 4980 | } |
| 4981 | |
lpeter | 133fce0 | 2020-03-05 20:32:16 +0800 | [diff] [blame] | 4982 | private boolean transferTouchFocusToImeWindow(@NonNull IBinder sourceInputToken, |
| 4983 | int displayId) { |
| 4984 | //TODO(b/150843766): Check if Input Token is valid. |
| 4985 | final IBinder curHostInputToken; |
| 4986 | synchronized (mMethodMap) { |
| 4987 | if (displayId != mCurTokenDisplayId || mCurHostInputToken == null) { |
| 4988 | return false; |
| 4989 | } |
| 4990 | curHostInputToken = mCurHostInputToken; |
| 4991 | } |
| 4992 | return mInputManagerInternal.transferTouchFocus(sourceInputToken, curHostInputToken); |
| 4993 | } |
| 4994 | |
Yohei Yukawa | e24ed79 | 2018-08-28 19:10:32 -0700 | [diff] [blame] | 4995 | private static final class LocalServiceImpl extends InputMethodManagerInternal { |
Yohei Yukawa | fa6e0a8 | 2015-07-23 15:08:59 -0700 | [diff] [blame] | 4996 | @NonNull |
Yohei Yukawa | fffc0e5 | 2018-09-04 13:24:00 -0700 | [diff] [blame] | 4997 | private final InputMethodManagerService mService; |
Yohei Yukawa | fa6e0a8 | 2015-07-23 15:08:59 -0700 | [diff] [blame] | 4998 | |
Yohei Yukawa | fffc0e5 | 2018-09-04 13:24:00 -0700 | [diff] [blame] | 4999 | LocalServiceImpl(@NonNull InputMethodManagerService service) { |
| 5000 | mService = service; |
Yohei Yukawa | fa6e0a8 | 2015-07-23 15:08:59 -0700 | [diff] [blame] | 5001 | } |
| 5002 | |
| 5003 | @Override |
Ming-Shin Lu | 2c6e80b | 2020-05-13 00:12:26 +0800 | [diff] [blame] | 5004 | public void setInteractive(boolean interactive) { |
| 5005 | // Do everything in handler so as not to block the caller. |
| 5006 | mService.mHandler.obtainMessage(MSG_SET_INTERACTIVE, interactive ? 1 : 0, 0) |
| 5007 | .sendToTarget(); |
| 5008 | } |
| 5009 | |
| 5010 | @Override |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 5011 | public void hideCurrentInputMethod(@SoftInputShowHideReason int reason) { |
Yohei Yukawa | f16abb7 | 2018-09-05 11:28:42 -0700 | [diff] [blame] | 5012 | mService.mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD); |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 5013 | mService.mHandler.obtainMessage(MSG_HIDE_CURRENT_INPUT_METHOD, reason).sendToTarget(); |
Jorim Jaggi | 3c5d0f1 | 2016-05-24 19:04:30 -0700 | [diff] [blame] | 5014 | } |
Tarandeep Singh | 89a6c48 | 2017-11-21 14:26:11 -0800 | [diff] [blame] | 5015 | |
| 5016 | @Override |
Yohei Yukawa | a878b95 | 2019-01-10 19:36:24 -0800 | [diff] [blame] | 5017 | public List<InputMethodInfo> getInputMethodListAsUser(int userId) { |
| 5018 | return mService.getInputMethodListAsUser(userId); |
| 5019 | } |
| 5020 | |
| 5021 | @Override |
| 5022 | public List<InputMethodInfo> getEnabledInputMethodListAsUser(int userId) { |
| 5023 | return mService.getEnabledInputMethodListAsUser(userId); |
| 5024 | } |
Adam He | bc67f2e | 2019-11-13 14:34:56 -0800 | [diff] [blame] | 5025 | |
| 5026 | @Override |
Feng Cao | 36960ee | 2020-02-18 18:23:30 -0800 | [diff] [blame] | 5027 | public void onCreateInlineSuggestionsRequest(int userId, |
| 5028 | InlineSuggestionsRequestInfo requestInfo, IInlineSuggestionsRequestCallback cb) { |
| 5029 | mService.onCreateInlineSuggestionsRequest(userId, requestInfo, cb); |
Adam He | bc67f2e | 2019-11-13 14:34:56 -0800 | [diff] [blame] | 5030 | } |
mincheli | 850892b | 2019-12-05 19:47:59 +0800 | [diff] [blame] | 5031 | |
| 5032 | @Override |
| 5033 | public boolean switchToInputMethod(String imeId, int userId) { |
| 5034 | return mService.switchToInputMethod(imeId, userId); |
| 5035 | } |
Yuichiro Hanada | 34c4c0e | 2020-01-15 16:53:07 +0900 | [diff] [blame] | 5036 | |
| 5037 | @Override |
| 5038 | public void registerInputMethodListListener(InputMethodListListener listener) { |
| 5039 | mService.mInputMethodListListeners.addIfAbsent(listener); |
| 5040 | } |
lpeter | 133fce0 | 2020-03-05 20:32:16 +0800 | [diff] [blame] | 5041 | |
| 5042 | @Override |
| 5043 | public boolean transferTouchFocusToImeWindow(@NonNull IBinder sourceInputToken, |
| 5044 | int displayId) { |
| 5045 | return mService.transferTouchFocusToImeWindow(sourceInputToken, displayId); |
| 5046 | } |
Yohei Yukawa | fa6e0a8 | 2015-07-23 15:08:59 -0700 | [diff] [blame] | 5047 | } |
| 5048 | |
Yohei Yukawa | c54c117 | 2018-09-06 11:39:50 -0700 | [diff] [blame] | 5049 | @BinderThread |
| 5050 | private IInputContentUriToken createInputContentUriToken(@Nullable IBinder token, |
Yohei Yukawa | 25e0813 | 2016-06-22 16:31:41 -0700 | [diff] [blame] | 5051 | @Nullable Uri contentUri, @Nullable String packageName) { |
Yohei Yukawa | 25e0813 | 2016-06-22 16:31:41 -0700 | [diff] [blame] | 5052 | if (token == null) { |
| 5053 | throw new NullPointerException("token"); |
| 5054 | } |
| 5055 | if (packageName == null) { |
| 5056 | throw new NullPointerException("packageName"); |
| 5057 | } |
| 5058 | if (contentUri == null) { |
| 5059 | throw new NullPointerException("contentUri"); |
| 5060 | } |
| 5061 | final String contentUriScheme = contentUri.getScheme(); |
| 5062 | if (!"content".equals(contentUriScheme)) { |
| 5063 | throw new InvalidParameterException("contentUri must have content scheme"); |
| 5064 | } |
| 5065 | |
| 5066 | synchronized (mMethodMap) { |
| 5067 | final int uid = Binder.getCallingUid(); |
| 5068 | if (mCurMethodId == null) { |
| 5069 | return null; |
| 5070 | } |
| 5071 | if (mCurToken != token) { |
| 5072 | Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken |
| 5073 | + " token=" + token); |
| 5074 | return null; |
| 5075 | } |
| 5076 | // We cannot simply distinguish a bad IME that reports an arbitrary package name from |
| 5077 | // an unfortunate IME whose internal state is already obsolete due to the asynchronous |
| 5078 | // nature of our system. Let's compare it with our internal record. |
| 5079 | if (!TextUtils.equals(mCurAttribute.packageName, packageName)) { |
| 5080 | Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName=" |
| 5081 | + mCurAttribute.packageName + " packageName=" + packageName); |
| 5082 | return null; |
| 5083 | } |
Yohei Yukawa | 3933a6e | 2016-11-10 00:47:48 -0800 | [diff] [blame] | 5084 | // This user ID can never bee spoofed. |
Yohei Yukawa | 25e0813 | 2016-06-22 16:31:41 -0700 | [diff] [blame] | 5085 | final int imeUserId = UserHandle.getUserId(uid); |
Yohei Yukawa | 3933a6e | 2016-11-10 00:47:48 -0800 | [diff] [blame] | 5086 | // This user ID can never bee spoofed. |
Yohei Yukawa | 25e0813 | 2016-06-22 16:31:41 -0700 | [diff] [blame] | 5087 | final int appUserId = UserHandle.getUserId(mCurClient.uid); |
Yohei Yukawa | 3933a6e | 2016-11-10 00:47:48 -0800 | [diff] [blame] | 5088 | // This user ID may be invalid if "contentUri" embedded an invalid user ID. |
| 5089 | final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri, |
| 5090 | imeUserId); |
| 5091 | final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri); |
| 5092 | // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid") |
| 5093 | // actually has the right to grant a read permission for "contentUriWithoutUserId" that |
| 5094 | // is claimed to belong to "contentUriOwnerUserId". For example, specifying random |
| 5095 | // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown |
| 5096 | // from InputContentUriTokenHandler.take() and can never be allowed beyond what is |
| 5097 | // actually allowed to "uid", which is guaranteed to be the IME's one. |
| 5098 | return new InputContentUriTokenHandler(contentUriWithoutUserId, uid, |
| 5099 | packageName, contentUriOwnerUserId, appUserId); |
Yohei Yukawa | 25e0813 | 2016-06-22 16:31:41 -0700 | [diff] [blame] | 5100 | } |
| 5101 | } |
| 5102 | |
Yohei Yukawa | c54c117 | 2018-09-06 11:39:50 -0700 | [diff] [blame] | 5103 | @BinderThread |
Yohei Yukawa | d3e0fd93 | 2019-01-21 09:16:48 -0800 | [diff] [blame] | 5104 | private void reportFullscreenMode(@NonNull IBinder token, boolean fullscreen) { |
Yohei Yukawa | 2bc6617 | 2017-02-08 11:13:25 -0800 | [diff] [blame] | 5105 | synchronized (mMethodMap) { |
Yohei Yukawa | 79e4c6a | 2018-12-09 19:07:17 -0800 | [diff] [blame] | 5106 | if (!calledWithValidTokenLocked(token)) { |
Yohei Yukawa | 2bc6617 | 2017-02-08 11:13:25 -0800 | [diff] [blame] | 5107 | return; |
| 5108 | } |
| 5109 | if (mCurClient != null && mCurClient.client != null) { |
| 5110 | mInFullscreenMode = fullscreen; |
| 5111 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 5112 | MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient)); |
| 5113 | } |
| 5114 | } |
| 5115 | } |
| 5116 | |
| 5117 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5118 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 5119 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5120 | |
| 5121 | IInputMethod method; |
| 5122 | ClientState client; |
Yohei Yukawa | e39d4ed | 2015-11-19 03:38:49 -0800 | [diff] [blame] | 5123 | ClientState focusedWindowClient; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5124 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5125 | final Printer p = new PrintWriterPrinter(pw); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5126 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5127 | synchronized (mMethodMap) { |
| 5128 | p.println("Current Input Method Manager state:"); |
| 5129 | int N = mMethodList.size(); |
Yohei Yukawa | 5ec1fce | 2018-12-17 07:56:14 -0800 | [diff] [blame] | 5130 | p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5131 | for (int i=0; i<N; i++) { |
| 5132 | InputMethodInfo info = mMethodList.get(i); |
| 5133 | p.println(" InputMethod #" + i + ":"); |
| 5134 | info.dump(p, " "); |
| 5135 | } |
| 5136 | p.println(" Clients:"); |
Yohei Yukawa | ac9311e | 2018-11-20 19:25:23 -0800 | [diff] [blame] | 5137 | final int numClients = mClients.size(); |
| 5138 | for (int i = 0; i < numClients; ++i) { |
| 5139 | final ClientState ci = mClients.valueAt(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5140 | p.println(" Client " + ci + ":"); |
| 5141 | p.println(" client=" + ci.client); |
| 5142 | p.println(" inputContext=" + ci.inputContext); |
| 5143 | p.println(" sessionRequested=" + ci.sessionRequested); |
| 5144 | p.println(" curSession=" + ci.curSession); |
| 5145 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5146 | p.println(" mCurMethodId=" + mCurMethodId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5147 | client = mCurClient; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5148 | p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq); |
Yohei Yukawa | 22a8923 | 2017-02-12 16:38:59 -0800 | [diff] [blame] | 5149 | p.println(" mCurFocusedWindow=" + mCurFocusedWindow |
| 5150 | + " softInputMode=" + |
Yohei Yukawa | a468d70 | 2018-10-21 11:42:34 -0700 | [diff] [blame] | 5151 | InputMethodDebug.softInputModeToString(mCurFocusedWindowSoftInputMode) |
Yohei Yukawa | 22a8923 | 2017-02-12 16:38:59 -0800 | [diff] [blame] | 5152 | + " client=" + mCurFocusedWindowClient); |
Yohei Yukawa | e39d4ed | 2015-11-19 03:38:49 -0800 | [diff] [blame] | 5153 | focusedWindowClient = mCurFocusedWindowClient; |
Yohei Yukawa | d3ef842 | 2018-06-07 16:28:07 -0700 | [diff] [blame] | 5154 | p.println(" mCurId=" + mCurId + " mHaveConnection=" + mHaveConnection |
| 5155 | + " mBoundToMethod=" + mBoundToMethod + " mVisibleBound=" + mVisibleBound); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5156 | p.println(" mCurToken=" + mCurToken); |
lumark | 90120a8 | 2018-08-15 00:33:03 +0800 | [diff] [blame] | 5157 | p.println(" mCurTokenDisplayId=" + mCurTokenDisplayId); |
lpeter | 133fce0 | 2020-03-05 20:32:16 +0800 | [diff] [blame] | 5158 | p.println(" mCurHostInputToken=" + mCurHostInputToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5159 | p.println(" mCurIntent=" + mCurIntent); |
| 5160 | method = mCurMethod; |
| 5161 | p.println(" mCurMethod=" + mCurMethod); |
| 5162 | p.println(" mEnabledSession=" + mEnabledSession); |
| 5163 | p.println(" mShowRequested=" + mShowRequested |
| 5164 | + " mShowExplicitlyRequested=" + mShowExplicitlyRequested |
| 5165 | + " mShowForced=" + mShowForced |
| 5166 | + " mInputShown=" + mInputShown); |
Yohei Yukawa | 2bc6617 | 2017-02-08 11:13:25 -0800 | [diff] [blame] | 5167 | p.println(" mInFullscreenMode=" + mInFullscreenMode); |
Yohei Yukawa | fa6e0a8 | 2015-07-23 15:08:59 -0700 | [diff] [blame] | 5168 | p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive); |
Yohei Yukawa | 8148297 | 2015-06-04 00:58:59 -0700 | [diff] [blame] | 5169 | p.println(" mSettingsObserver=" + mSettingsObserver); |
Yohei Yukawa | d724886 | 2015-06-03 23:56:12 -0700 | [diff] [blame] | 5170 | p.println(" mSwitchingController:"); |
| 5171 | mSwitchingController.dump(p); |
Yohei Yukawa | 68645a6 | 2016-02-17 07:54:20 -0800 | [diff] [blame] | 5172 | p.println(" mSettings:"); |
| 5173 | mSettings.dumpLocked(p, " "); |
Yohei Yukawa | 357b2f6 | 2017-02-14 09:40:03 -0800 | [diff] [blame] | 5174 | |
| 5175 | p.println(" mStartInputHistory:"); |
| 5176 | mStartInputHistory.dump(pw, " "); |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 5177 | |
| 5178 | p.println(" mSoftInputShowHideHistory:"); |
| 5179 | mSoftInputShowHideHistory.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5180 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5181 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 5182 | p.println(" "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5183 | if (client != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5184 | pw.flush(); |
| 5185 | try { |
Jeff Sharkey | 850c83e | 2016-11-09 12:25:44 -0700 | [diff] [blame] | 5186 | TransferPipe.dumpAsync(client.client.asBinder(), fd, args); |
| 5187 | } catch (IOException | RemoteException e) { |
| 5188 | p.println("Failed to dump input method client: " + e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5189 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 5190 | } else { |
| 5191 | p.println("No input method client."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5192 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5193 | |
Yohei Yukawa | e39d4ed | 2015-11-19 03:38:49 -0800 | [diff] [blame] | 5194 | if (focusedWindowClient != null && client != focusedWindowClient) { |
| 5195 | p.println(" "); |
| 5196 | p.println("Warning: Current input method client doesn't match the last focused. " |
| 5197 | + "window."); |
| 5198 | p.println("Dumping input method client in the last focused window just in case."); |
| 5199 | p.println(" "); |
| 5200 | pw.flush(); |
| 5201 | try { |
Jeff Sharkey | 850c83e | 2016-11-09 12:25:44 -0700 | [diff] [blame] | 5202 | TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args); |
| 5203 | } catch (IOException | RemoteException e) { |
| 5204 | p.println("Failed to dump input method client in focused window: " + e); |
Yohei Yukawa | e39d4ed | 2015-11-19 03:38:49 -0800 | [diff] [blame] | 5205 | } |
| 5206 | } |
| 5207 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 5208 | p.println(" "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5209 | if (method != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5210 | pw.flush(); |
| 5211 | try { |
Jeff Sharkey | 850c83e | 2016-11-09 12:25:44 -0700 | [diff] [blame] | 5212 | TransferPipe.dumpAsync(method.asBinder(), fd, args); |
| 5213 | } catch (IOException | RemoteException e) { |
| 5214 | p.println("Failed to dump input method service: " + e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5215 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 5216 | } else { |
| 5217 | p.println("No input method service."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5218 | } |
| 5219 | } |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 5220 | |
| 5221 | @BinderThread |
| 5222 | @Override |
| 5223 | public void onShellCommand(@Nullable FileDescriptor in, @Nullable FileDescriptor out, |
| 5224 | @Nullable FileDescriptor err, |
| 5225 | @NonNull String[] args, @Nullable ShellCallback callback, |
| 5226 | @NonNull ResultReceiver resultReceiver) throws RemoteException { |
Yohei Yukawa | b8d240f | 2018-12-26 10:03:11 -0800 | [diff] [blame] | 5227 | final int callingUid = Binder.getCallingUid(); |
| 5228 | // Reject any incoming calls from non-shell users, including ones from the system user. |
| 5229 | if (callingUid != Process.ROOT_UID && callingUid != Process.SHELL_UID) { |
| 5230 | // Note that Binder#onTransact() will automatically close "in", "out", and "err" when |
| 5231 | // returned from this method, hence there is no need to close those FDs. |
| 5232 | // "resultReceiver" is the only thing that needs to be taken care of here. |
| 5233 | if (resultReceiver != null) { |
| 5234 | resultReceiver.send(ShellCommandResult.FAILURE, null); |
| 5235 | } |
| 5236 | final String errorMsg = "InputMethodManagerService does not support shell commands from" |
| 5237 | + " non-shell users. callingUid=" + callingUid |
| 5238 | + " args=" + Arrays.toString(args); |
| 5239 | if (Process.isCoreUid(callingUid)) { |
| 5240 | // Let's not crash the calling process if the caller is one of core components. |
| 5241 | Slog.e(TAG, errorMsg); |
| 5242 | return; |
| 5243 | } |
| 5244 | throw new SecurityException(errorMsg); |
| 5245 | } |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 5246 | new ShellCommandImpl(this).exec( |
| 5247 | this, in, out, err, args, callback, resultReceiver); |
| 5248 | } |
| 5249 | |
| 5250 | private static final class ShellCommandImpl extends ShellCommand { |
| 5251 | @NonNull |
| 5252 | final InputMethodManagerService mService; |
| 5253 | |
| 5254 | ShellCommandImpl(InputMethodManagerService service) { |
| 5255 | mService = service; |
| 5256 | } |
| 5257 | |
Yohei Yukawa | db25df7 | 2018-12-27 08:40:41 -0800 | [diff] [blame] | 5258 | @RequiresPermission(allOf = { |
Yohei Yukawa | 8a2b96b | 2018-12-28 17:23:29 -0800 | [diff] [blame] | 5259 | Manifest.permission.DUMP, |
| 5260 | Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
Yohei Yukawa | db25df7 | 2018-12-27 08:40:41 -0800 | [diff] [blame] | 5261 | Manifest.permission.WRITE_SECURE_SETTINGS, |
Yohei Yukawa | 8a2b96b | 2018-12-28 17:23:29 -0800 | [diff] [blame] | 5262 | }) |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 5263 | @BinderThread |
| 5264 | @ShellCommandResult |
| 5265 | @Override |
| 5266 | public int onCommand(@Nullable String cmd) { |
Yohei Yukawa | db25df7 | 2018-12-27 08:40:41 -0800 | [diff] [blame] | 5267 | // For shell command, require all the permissions here in favor of code simplicity. |
Yohei Yukawa | 8a2b96b | 2018-12-28 17:23:29 -0800 | [diff] [blame] | 5268 | Arrays.asList( |
| 5269 | Manifest.permission.DUMP, |
| 5270 | Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 5271 | Manifest.permission.WRITE_SECURE_SETTINGS |
| 5272 | ).forEach(permission -> mService.mContext.enforceCallingPermission(permission, null)); |
Yohei Yukawa | db25df7 | 2018-12-27 08:40:41 -0800 | [diff] [blame] | 5273 | |
| 5274 | final long identity = Binder.clearCallingIdentity(); |
| 5275 | try { |
| 5276 | return onCommandWithSystemIdentity(cmd); |
| 5277 | } finally { |
| 5278 | Binder.restoreCallingIdentity(identity); |
| 5279 | } |
| 5280 | } |
| 5281 | |
| 5282 | @BinderThread |
| 5283 | @ShellCommandResult |
| 5284 | private int onCommandWithSystemIdentity(@Nullable String cmd) { |
Yohei Yukawa | dfdab73 | 2018-02-21 07:16:30 +0900 | [diff] [blame] | 5285 | if ("refresh_debug_properties".equals(cmd)) { |
| 5286 | return refreshDebugProperties(); |
| 5287 | } |
| 5288 | |
Yohei Yukawa | a7babbb | 2019-01-08 16:45:34 -0800 | [diff] [blame] | 5289 | if ("get-last-switch-user-id".equals(cmd)) { |
| 5290 | return mService.getLastSwitchUserId(this); |
| 5291 | } |
| 5292 | |
Yohei Yukawa | cac9772 | 2017-12-15 16:52:05 -0800 | [diff] [blame] | 5293 | // For existing "adb shell ime <command>". |
| 5294 | if ("ime".equals(cmd)) { |
| 5295 | final String imeCommand = getNextArg(); |
| 5296 | if (imeCommand == null || "help".equals(imeCommand) || "-h".equals(imeCommand)) { |
| 5297 | onImeCommandHelp(); |
| 5298 | return ShellCommandResult.SUCCESS; |
| 5299 | } |
| 5300 | switch (imeCommand) { |
| 5301 | case "list": |
| 5302 | return mService.handleShellCommandListInputMethods(this); |
| 5303 | case "enable": |
| 5304 | return mService.handleShellCommandEnableDisableInputMethod(this, true); |
| 5305 | case "disable": |
| 5306 | return mService.handleShellCommandEnableDisableInputMethod(this, false); |
| 5307 | case "set": |
| 5308 | return mService.handleShellCommandSetInputMethod(this); |
| 5309 | case "reset": |
| 5310 | return mService.handleShellCommandResetInputMethod(this); |
| 5311 | default: |
| 5312 | getOutPrintWriter().println("Unknown command: " + imeCommand); |
| 5313 | return ShellCommandResult.FAILURE; |
| 5314 | } |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 5315 | } |
Yohei Yukawa | cac9772 | 2017-12-15 16:52:05 -0800 | [diff] [blame] | 5316 | |
| 5317 | return handleDefaultCommands(cmd); |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 5318 | } |
| 5319 | |
| 5320 | @BinderThread |
Tarandeep Singh | 75a9239 | 2018-01-12 14:58:59 -0800 | [diff] [blame] | 5321 | @ShellCommandResult |
| 5322 | private int refreshDebugProperties() { |
| 5323 | DebugFlags.FLAG_OPTIMIZE_START_INPUT.refresh(); |
Tarandeep Singh | eadb139 | 2018-11-09 18:15:57 +0100 | [diff] [blame] | 5324 | DebugFlags.FLAG_PRE_RENDER_IME_VIEWS.refresh(); |
Tarandeep Singh | 75a9239 | 2018-01-12 14:58:59 -0800 | [diff] [blame] | 5325 | return ShellCommandResult.SUCCESS; |
| 5326 | } |
| 5327 | |
| 5328 | @BinderThread |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 5329 | @Override |
| 5330 | public void onHelp() { |
| 5331 | try (PrintWriter pw = getOutPrintWriter()) { |
| 5332 | pw.println("InputMethodManagerService commands:"); |
| 5333 | pw.println(" help"); |
| 5334 | pw.println(" Prints this help text."); |
| 5335 | pw.println(" dump [options]"); |
| 5336 | pw.println(" Synonym of dumpsys."); |
Yohei Yukawa | cac9772 | 2017-12-15 16:52:05 -0800 | [diff] [blame] | 5337 | pw.println(" ime <command> [options]"); |
| 5338 | pw.println(" Manipulate IMEs. Run \"ime help\" for details."); |
| 5339 | } |
| 5340 | } |
| 5341 | |
| 5342 | private void onImeCommandHelp() { |
| 5343 | try (IndentingPrintWriter pw = |
| 5344 | new IndentingPrintWriter(getOutPrintWriter(), " ", 100)) { |
| 5345 | pw.println("ime <command>:"); |
| 5346 | pw.increaseIndent(); |
| 5347 | |
| 5348 | pw.println("list [-a] [-s]"); |
| 5349 | pw.increaseIndent(); |
| 5350 | pw.println("prints all enabled input methods."); |
| 5351 | pw.increaseIndent(); |
| 5352 | pw.println("-a: see all input methods"); |
| 5353 | pw.println("-s: only a single summary line of each"); |
| 5354 | pw.decreaseIndent(); |
| 5355 | pw.decreaseIndent(); |
| 5356 | |
Yohei Yukawa | e177170 | 2019-04-10 23:20:51 -0700 | [diff] [blame] | 5357 | pw.println("enable [--user <USER_ID>] <ID>"); |
Yohei Yukawa | cac9772 | 2017-12-15 16:52:05 -0800 | [diff] [blame] | 5358 | pw.increaseIndent(); |
| 5359 | pw.println("allows the given input method ID to be used."); |
Yohei Yukawa | e177170 | 2019-04-10 23:20:51 -0700 | [diff] [blame] | 5360 | pw.increaseIndent(); |
| 5361 | pw.print("--user <USER_ID>: Specify which user to enable."); |
| 5362 | pw.println(" Assumes the current user if not specified."); |
| 5363 | pw.decreaseIndent(); |
Yohei Yukawa | cac9772 | 2017-12-15 16:52:05 -0800 | [diff] [blame] | 5364 | pw.decreaseIndent(); |
| 5365 | |
Yohei Yukawa | e177170 | 2019-04-10 23:20:51 -0700 | [diff] [blame] | 5366 | pw.println("disable [--user <USER_ID>] <ID>"); |
Yohei Yukawa | cac9772 | 2017-12-15 16:52:05 -0800 | [diff] [blame] | 5367 | pw.increaseIndent(); |
| 5368 | pw.println("disallows the given input method ID to be used."); |
Yohei Yukawa | e177170 | 2019-04-10 23:20:51 -0700 | [diff] [blame] | 5369 | pw.increaseIndent(); |
| 5370 | pw.print("--user <USER_ID>: Specify which user to disable."); |
| 5371 | pw.println(" Assumes the current user if not specified."); |
| 5372 | pw.decreaseIndent(); |
Yohei Yukawa | cac9772 | 2017-12-15 16:52:05 -0800 | [diff] [blame] | 5373 | pw.decreaseIndent(); |
| 5374 | |
Yohei Yukawa | 099f80c | 2019-04-10 23:23:17 -0700 | [diff] [blame] | 5375 | pw.println("set [--user <USER_ID>] <ID>"); |
Yohei Yukawa | cac9772 | 2017-12-15 16:52:05 -0800 | [diff] [blame] | 5376 | pw.increaseIndent(); |
| 5377 | pw.println("switches to the given input method ID."); |
Yohei Yukawa | 099f80c | 2019-04-10 23:23:17 -0700 | [diff] [blame] | 5378 | pw.increaseIndent(); |
| 5379 | pw.print("--user <USER_ID>: Specify which user to enable."); |
| 5380 | pw.println(" Assumes the current user if not specified."); |
| 5381 | pw.decreaseIndent(); |
Yohei Yukawa | cac9772 | 2017-12-15 16:52:05 -0800 | [diff] [blame] | 5382 | pw.decreaseIndent(); |
| 5383 | |
Yohei Yukawa | 2d4b343 | 2019-04-10 23:23:25 -0700 | [diff] [blame] | 5384 | pw.println("reset [--user <USER_ID>]"); |
Yohei Yukawa | cac9772 | 2017-12-15 16:52:05 -0800 | [diff] [blame] | 5385 | pw.increaseIndent(); |
| 5386 | pw.println("reset currently selected/enabled IMEs to the default ones as if " |
| 5387 | + "the device is initially booted with the current locale."); |
Yohei Yukawa | 2d4b343 | 2019-04-10 23:23:25 -0700 | [diff] [blame] | 5388 | pw.increaseIndent(); |
| 5389 | pw.print("--user <USER_ID>: Specify which user to reset."); |
| 5390 | pw.println(" Assumes the current user if not specified."); |
| 5391 | pw.decreaseIndent(); |
| 5392 | |
Yohei Yukawa | cac9772 | 2017-12-15 16:52:05 -0800 | [diff] [blame] | 5393 | pw.decreaseIndent(); |
| 5394 | |
| 5395 | pw.decreaseIndent(); |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 5396 | } |
| 5397 | } |
| 5398 | } |
| 5399 | |
| 5400 | // ---------------------------------------------------------------------- |
| 5401 | // Shell command handlers: |
| 5402 | |
Yohei Yukawa | a7babbb | 2019-01-08 16:45:34 -0800 | [diff] [blame] | 5403 | @BinderThread |
| 5404 | @ShellCommandResult |
| 5405 | private int getLastSwitchUserId(@NonNull ShellCommand shellCommand) { |
| 5406 | synchronized (mMethodMap) { |
| 5407 | shellCommand.getOutPrintWriter().println(mLastSwitchUserId); |
| 5408 | return ShellCommandResult.SUCCESS; |
| 5409 | } |
| 5410 | } |
| 5411 | |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 5412 | /** |
| 5413 | * Handles {@code adb shell ime list}. |
| 5414 | * @param shellCommand {@link ShellCommand} object that is handling this command. |
| 5415 | * @return Exit code of the command. |
| 5416 | */ |
| 5417 | @BinderThread |
| 5418 | @ShellCommandResult |
| 5419 | private int handleShellCommandListInputMethods(@NonNull ShellCommand shellCommand) { |
| 5420 | boolean all = false; |
| 5421 | boolean brief = false; |
Yohei Yukawa | 7f8ee4b | 2019-01-10 08:44:57 -0800 | [diff] [blame] | 5422 | int userIdToBeResolved = UserHandle.USER_CURRENT; |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 5423 | while (true) { |
| 5424 | final String nextOption = shellCommand.getNextOption(); |
| 5425 | if (nextOption == null) { |
| 5426 | break; |
| 5427 | } |
| 5428 | switch (nextOption) { |
| 5429 | case "-a": |
| 5430 | all = true; |
| 5431 | break; |
| 5432 | case "-s": |
| 5433 | brief = true; |
| 5434 | break; |
Yohei Yukawa | 7f8ee4b | 2019-01-10 08:44:57 -0800 | [diff] [blame] | 5435 | case "-u": |
| 5436 | case "--user": |
| 5437 | userIdToBeResolved = UserHandle.parseUserArg(shellCommand.getNextArgRequired()); |
| 5438 | break; |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 5439 | } |
| 5440 | } |
Yohei Yukawa | 7f8ee4b | 2019-01-10 08:44:57 -0800 | [diff] [blame] | 5441 | synchronized (mMethodMap) { |
| 5442 | final PrintWriter pr = shellCommand.getOutPrintWriter(); |
| 5443 | final int[] userIds = InputMethodUtils.resolveUserId(userIdToBeResolved, |
| 5444 | mSettings.getCurrentUserId(), shellCommand.getErrPrintWriter()); |
| 5445 | for (int userId : userIds) { |
| 5446 | final List<InputMethodInfo> methods = all |
Yohei Yukawa | 2988d2d | 2019-01-22 21:29:09 -0800 | [diff] [blame] | 5447 | ? getInputMethodListLocked(userId) |
Yohei Yukawa | 7f8ee4b | 2019-01-10 08:44:57 -0800 | [diff] [blame] | 5448 | : getEnabledInputMethodListLocked(userId); |
| 5449 | if (userIds.length > 1) { |
| 5450 | pr.print("User #"); |
| 5451 | pr.print(userId); |
| 5452 | pr.println(":"); |
| 5453 | } |
| 5454 | for (InputMethodInfo info : methods) { |
| 5455 | if (brief) { |
| 5456 | pr.println(info.getId()); |
| 5457 | } else { |
| 5458 | pr.print(info.getId()); |
| 5459 | pr.println(":"); |
| 5460 | info.dump(pr::println, " "); |
| 5461 | } |
| 5462 | } |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 5463 | } |
| 5464 | } |
| 5465 | return ShellCommandResult.SUCCESS; |
| 5466 | } |
| 5467 | |
| 5468 | /** |
| 5469 | * Handles {@code adb shell ime enable} and {@code adb shell ime disable}. |
| 5470 | * @param shellCommand {@link ShellCommand} object that is handling this command. |
| 5471 | * @param enabled {@code true} if the command was {@code adb shell ime enable}. |
| 5472 | * @return Exit code of the command. |
| 5473 | */ |
| 5474 | @BinderThread |
| 5475 | @ShellCommandResult |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 5476 | private int handleShellCommandEnableDisableInputMethod( |
| 5477 | @NonNull ShellCommand shellCommand, boolean enabled) { |
Yohei Yukawa | 3403f2d | 2019-04-15 08:16:58 -0700 | [diff] [blame] | 5478 | final int userIdToBeResolved = handleOptionsForCommandsThatOnlyHaveUserOption(shellCommand); |
Yohei Yukawa | e177170 | 2019-04-10 23:20:51 -0700 | [diff] [blame] | 5479 | final String imeId = shellCommand.getNextArgRequired(); |
| 5480 | final PrintWriter out = shellCommand.getOutPrintWriter(); |
| 5481 | final PrintWriter error = shellCommand.getErrPrintWriter(); |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 5482 | synchronized (mMethodMap) { |
Yohei Yukawa | e177170 | 2019-04-10 23:20:51 -0700 | [diff] [blame] | 5483 | final int[] userIds = InputMethodUtils.resolveUserId(userIdToBeResolved, |
| 5484 | mSettings.getCurrentUserId(), shellCommand.getErrPrintWriter()); |
| 5485 | for (int userId : userIds) { |
| 5486 | if (!userHasDebugPriv(userId, shellCommand)) { |
| 5487 | continue; |
| 5488 | } |
| 5489 | handleShellCommandEnableDisableInputMethodInternalLocked(userId, imeId, enabled, |
| 5490 | out, error); |
Tarandeep Singh | 87e2e51 | 2019-03-06 14:28:52 -0800 | [diff] [blame] | 5491 | } |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 5492 | } |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 5493 | return ShellCommandResult.SUCCESS; |
| 5494 | } |
| 5495 | |
| 5496 | /** |
Yohei Yukawa | e177170 | 2019-04-10 23:20:51 -0700 | [diff] [blame] | 5497 | * A special helper method for commands that only have {@code -u} and {@code --user} options. |
| 5498 | * |
| 5499 | * <p>You cannot use this helper method if the command has other options.</p> |
| 5500 | * |
Yohei Yukawa | 3403f2d | 2019-04-15 08:16:58 -0700 | [diff] [blame] | 5501 | * <p>CAVEAT: This method must be called only once before any other |
| 5502 | * {@link ShellCommand#getNextArg()} and {@link ShellCommand#getNextArgRequired()} for the |
| 5503 | * main arguments.</p> |
| 5504 | * |
Yohei Yukawa | e177170 | 2019-04-10 23:20:51 -0700 | [diff] [blame] | 5505 | * @param shellCommand {@link ShellCommand} from which options should be obtained. |
| 5506 | * @return User ID to be resolved. {@link UserHandle#CURRENT} if not specified. |
| 5507 | */ |
| 5508 | @BinderThread |
| 5509 | @UserIdInt |
| 5510 | private static int handleOptionsForCommandsThatOnlyHaveUserOption(ShellCommand shellCommand) { |
| 5511 | while (true) { |
| 5512 | final String nextOption = shellCommand.getNextOption(); |
| 5513 | if (nextOption == null) { |
| 5514 | break; |
| 5515 | } |
| 5516 | switch (nextOption) { |
| 5517 | case "-u": |
| 5518 | case "--user": |
| 5519 | return UserHandle.parseUserArg(shellCommand.getNextArgRequired()); |
| 5520 | } |
| 5521 | } |
| 5522 | return UserHandle.USER_CURRENT; |
| 5523 | } |
| 5524 | |
| 5525 | @BinderThread |
| 5526 | private void handleShellCommandEnableDisableInputMethodInternalLocked( |
| 5527 | @UserIdInt int userId, String imeId, boolean enabled, PrintWriter out, |
| 5528 | PrintWriter error) { |
| 5529 | boolean failedToEnableUnknownIme = false; |
| 5530 | boolean previouslyEnabled = false; |
| 5531 | if (userId == mSettings.getCurrentUserId()) { |
| 5532 | if (enabled && !mMethodMap.containsKey(imeId)) { |
| 5533 | failedToEnableUnknownIme = true; |
| 5534 | } else { |
| 5535 | previouslyEnabled = setInputMethodEnabledLocked(imeId, enabled); |
| 5536 | } |
| 5537 | } else { |
| 5538 | final ArrayMap<String, InputMethodInfo> methodMap = new ArrayMap<>(); |
| 5539 | final ArrayList<InputMethodInfo> methodList = new ArrayList<>(); |
| 5540 | final ArrayMap<String, List<InputMethodSubtype>> additionalSubtypeMap = |
| 5541 | new ArrayMap<>(); |
| 5542 | AdditionalSubtypeUtils.load(additionalSubtypeMap, userId); |
| 5543 | queryInputMethodServicesInternal(mContext, userId, additionalSubtypeMap, |
| 5544 | methodMap, methodList); |
| 5545 | final InputMethodSettings settings = new InputMethodSettings(mContext.getResources(), |
| 5546 | mContext.getContentResolver(), methodMap, userId, false); |
| 5547 | if (enabled) { |
| 5548 | if (!methodMap.containsKey(imeId)) { |
| 5549 | failedToEnableUnknownIme = true; |
| 5550 | } else { |
| 5551 | for (InputMethodInfo imi : settings.getEnabledInputMethodListLocked()) { |
| 5552 | if (TextUtils.equals(imi.getId(), imeId)) { |
| 5553 | previouslyEnabled = true; |
| 5554 | break; |
| 5555 | } |
| 5556 | } |
| 5557 | if (!previouslyEnabled) { |
| 5558 | settings.appendAndPutEnabledInputMethodLocked(imeId, false); |
| 5559 | } |
| 5560 | } |
| 5561 | } else { |
| 5562 | previouslyEnabled = |
| 5563 | settings.buildAndPutEnabledInputMethodsStrRemovingIdLocked( |
| 5564 | new StringBuilder(), |
| 5565 | settings.getEnabledInputMethodsAndSubtypeListLocked(), imeId); |
| 5566 | } |
| 5567 | } |
| 5568 | if (failedToEnableUnknownIme) { |
| 5569 | error.print("Unknown input method "); |
| 5570 | error.print(imeId); |
| 5571 | error.println(" cannot be enabled for user #" + userId); |
| 5572 | } else { |
| 5573 | out.print("Input method "); |
| 5574 | out.print(imeId); |
| 5575 | out.print(": "); |
| 5576 | out.print((enabled == previouslyEnabled) ? "already " : "now "); |
| 5577 | out.print(enabled ? "enabled" : "disabled"); |
| 5578 | out.print(" for user #"); |
| 5579 | out.println(userId); |
| 5580 | } |
| 5581 | } |
| 5582 | |
| 5583 | /** |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 5584 | * Handles {@code adb shell ime set}. |
| 5585 | * @param shellCommand {@link ShellCommand} object that is handling this command. |
| 5586 | * @return Exit code of the command. |
| 5587 | */ |
| 5588 | @BinderThread |
| 5589 | @ShellCommandResult |
| 5590 | private int handleShellCommandSetInputMethod(@NonNull ShellCommand shellCommand) { |
Yohei Yukawa | 3403f2d | 2019-04-15 08:16:58 -0700 | [diff] [blame] | 5591 | final int userIdToBeResolved = handleOptionsForCommandsThatOnlyHaveUserOption(shellCommand); |
Yohei Yukawa | 099f80c | 2019-04-10 23:23:17 -0700 | [diff] [blame] | 5592 | final String imeId = shellCommand.getNextArgRequired(); |
| 5593 | final PrintWriter out = shellCommand.getOutPrintWriter(); |
| 5594 | final PrintWriter error = shellCommand.getErrPrintWriter(); |
Yohei Yukawa | db25df7 | 2018-12-27 08:40:41 -0800 | [diff] [blame] | 5595 | synchronized (mMethodMap) { |
Yohei Yukawa | 099f80c | 2019-04-10 23:23:17 -0700 | [diff] [blame] | 5596 | final int[] userIds = InputMethodUtils.resolveUserId(userIdToBeResolved, |
| 5597 | mSettings.getCurrentUserId(), shellCommand.getErrPrintWriter()); |
| 5598 | for (int userId : userIds) { |
| 5599 | if (!userHasDebugPriv(userId, shellCommand)) { |
| 5600 | continue; |
| 5601 | } |
mincheli | 850892b | 2019-12-05 19:47:59 +0800 | [diff] [blame] | 5602 | boolean failedToSelectUnknownIme = !switchToInputMethod(imeId, userId); |
Yohei Yukawa | 099f80c | 2019-04-10 23:23:17 -0700 | [diff] [blame] | 5603 | if (failedToSelectUnknownIme) { |
| 5604 | error.print("Unknown input method "); |
| 5605 | error.print(imeId); |
| 5606 | error.print(" cannot be selected for user #"); |
| 5607 | error.println(userId); |
| 5608 | } else { |
| 5609 | out.print("Input method "); |
| 5610 | out.print(imeId); |
| 5611 | out.print(" selected for user #"); |
Yohei Yukawa | 3403f2d | 2019-04-15 08:16:58 -0700 | [diff] [blame] | 5612 | out.println(userId); |
Yohei Yukawa | 099f80c | 2019-04-10 23:23:17 -0700 | [diff] [blame] | 5613 | } |
Tarandeep Singh | 87e2e51 | 2019-03-06 14:28:52 -0800 | [diff] [blame] | 5614 | } |
Yohei Yukawa | db25df7 | 2018-12-27 08:40:41 -0800 | [diff] [blame] | 5615 | } |
Yohei Yukawa | 926488d | 2017-12-11 17:24:55 -0800 | [diff] [blame] | 5616 | return ShellCommandResult.SUCCESS; |
| 5617 | } |
Yohei Yukawa | cc97ebd | 2017-12-11 17:54:43 -0800 | [diff] [blame] | 5618 | |
| 5619 | /** |
| 5620 | * Handles {@code adb shell ime reset-ime}. |
| 5621 | * @param shellCommand {@link ShellCommand} object that is handling this command. |
| 5622 | * @return Exit code of the command. |
| 5623 | */ |
| 5624 | @BinderThread |
| 5625 | @ShellCommandResult |
Yohei Yukawa | cc97ebd | 2017-12-11 17:54:43 -0800 | [diff] [blame] | 5626 | private int handleShellCommandResetInputMethod(@NonNull ShellCommand shellCommand) { |
Yohei Yukawa | 2d4b343 | 2019-04-10 23:23:25 -0700 | [diff] [blame] | 5627 | final PrintWriter out = shellCommand.getOutPrintWriter(); |
| 5628 | final int userIdToBeResolved = handleOptionsForCommandsThatOnlyHaveUserOption(shellCommand); |
Yohei Yukawa | cc97ebd | 2017-12-11 17:54:43 -0800 | [diff] [blame] | 5629 | synchronized (mMethodMap) { |
Yohei Yukawa | 2d4b343 | 2019-04-10 23:23:25 -0700 | [diff] [blame] | 5630 | final int[] userIds = InputMethodUtils.resolveUserId(userIdToBeResolved, |
| 5631 | mSettings.getCurrentUserId(), shellCommand.getErrPrintWriter()); |
| 5632 | for (int userId : userIds) { |
| 5633 | if (!userHasDebugPriv(userId, shellCommand)) { |
| 5634 | continue; |
Yohei Yukawa | cc97ebd | 2017-12-11 17:54:43 -0800 | [diff] [blame] | 5635 | } |
Yohei Yukawa | 2d4b343 | 2019-04-10 23:23:25 -0700 | [diff] [blame] | 5636 | final String nextIme; |
| 5637 | final List<InputMethodInfo> nextEnabledImes; |
| 5638 | if (userId == mSettings.getCurrentUserId()) { |
Tarandeep Singh | 4fe5b65 | 2020-02-20 17:20:19 -0800 | [diff] [blame] | 5639 | hideCurrentInputLocked(mCurFocusedWindow, 0, null, |
lumark | d85e158 | 2019-12-29 20:20:41 +0800 | [diff] [blame] | 5640 | SoftInputShowHideReason.HIDE_RESET_SHELL_COMMAND); |
Yohei Yukawa | 2d4b343 | 2019-04-10 23:23:25 -0700 | [diff] [blame] | 5641 | unbindCurrentMethodLocked(); |
| 5642 | // Reset the current IME |
| 5643 | resetSelectedInputMethodAndSubtypeLocked(null); |
| 5644 | // Also reset the settings of the current IME |
| 5645 | mSettings.putSelectedInputMethod(null); |
| 5646 | // Disable all enabled IMEs. |
| 5647 | mSettings.getEnabledInputMethodListLocked().forEach( |
| 5648 | imi -> setInputMethodEnabledLocked(imi.getId(), false)); |
| 5649 | // Re-enable with default enabled IMEs. |
| 5650 | InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList).forEach( |
| 5651 | imi -> setInputMethodEnabledLocked(imi.getId(), true)); |
| 5652 | updateInputMethodsFromSettingsLocked(true /* enabledMayChange */); |
| 5653 | InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager, |
| 5654 | mSettings.getEnabledInputMethodListLocked(), |
| 5655 | mSettings.getCurrentUserId(), |
| 5656 | mContext.getBasePackageName()); |
| 5657 | nextIme = mSettings.getSelectedInputMethod(); |
| 5658 | nextEnabledImes = mSettings.getEnabledInputMethodListLocked(); |
| 5659 | } else { |
| 5660 | final ArrayMap<String, InputMethodInfo> methodMap = new ArrayMap<>(); |
| 5661 | final ArrayList<InputMethodInfo> methodList = new ArrayList<>(); |
| 5662 | final ArrayMap<String, List<InputMethodSubtype>> additionalSubtypeMap = |
| 5663 | new ArrayMap<>(); |
| 5664 | AdditionalSubtypeUtils.load(additionalSubtypeMap, userId); |
| 5665 | queryInputMethodServicesInternal(mContext, userId, additionalSubtypeMap, |
| 5666 | methodMap, methodList); |
| 5667 | final InputMethodSettings settings = new InputMethodSettings( |
| 5668 | mContext.getResources(), mContext.getContentResolver(), methodMap, |
| 5669 | userId, false); |
| 5670 | |
| 5671 | nextEnabledImes = InputMethodUtils.getDefaultEnabledImes(mContext, methodList); |
| 5672 | nextIme = InputMethodUtils.getMostApplicableDefaultIME(nextEnabledImes).getId(); |
| 5673 | |
| 5674 | // Reset enabled IMEs. |
| 5675 | settings.putEnabledInputMethodsStr(""); |
| 5676 | nextEnabledImes.forEach(imi -> settings.appendAndPutEnabledInputMethodLocked( |
| 5677 | imi.getId(), false)); |
| 5678 | |
| 5679 | // Reset selected IME. |
| 5680 | settings.putSelectedInputMethod(nextIme); |
| 5681 | settings.putSelectedSubtype(NOT_A_SUBTYPE_ID); |
| 5682 | } |
| 5683 | out.println("Reset current and enabled IMEs for user #" + userId); |
| 5684 | out.println(" Selected: " + nextIme); |
| 5685 | nextEnabledImes.forEach(ime -> out.println(" Enabled: " + ime.getId())); |
Yohei Yukawa | cc97ebd | 2017-12-11 17:54:43 -0800 | [diff] [blame] | 5686 | } |
Yohei Yukawa | cc97ebd | 2017-12-11 17:54:43 -0800 | [diff] [blame] | 5687 | } |
Yohei Yukawa | 2d4b343 | 2019-04-10 23:23:25 -0700 | [diff] [blame] | 5688 | return ShellCommandResult.SUCCESS; |
Yohei Yukawa | cc97ebd | 2017-12-11 17:54:43 -0800 | [diff] [blame] | 5689 | } |
Yohei Yukawa | c54c117 | 2018-09-06 11:39:50 -0700 | [diff] [blame] | 5690 | |
Tarandeep Singh | 87e2e51 | 2019-03-06 14:28:52 -0800 | [diff] [blame] | 5691 | /** |
| 5692 | * @param userId the actual user handle obtained by {@link UserHandle#getIdentifier()} |
| 5693 | * and *not* pseudo ids like {@link UserHandle#USER_ALL etc}. |
| 5694 | * @return {@code true} if userId has debugging privileges. |
| 5695 | * i.e. {@link UserManager#DISALLOW_DEBUGGING_FEATURES} is {@code false}. |
| 5696 | */ |
| 5697 | private boolean userHasDebugPriv(int userId, final ShellCommand shellCommand) { |
| 5698 | if (mUserManager.hasUserRestriction( |
| 5699 | UserManager.DISALLOW_DEBUGGING_FEATURES, UserHandle.of(userId))) { |
| 5700 | shellCommand.getErrPrintWriter().println("User #" + userId |
| 5701 | + " is restricted with DISALLOW_DEBUGGING_FEATURES."); |
| 5702 | return false; |
| 5703 | } |
| 5704 | return true; |
| 5705 | } |
| 5706 | |
Yohei Yukawa | c54c117 | 2018-09-06 11:39:50 -0700 | [diff] [blame] | 5707 | private static final class InputMethodPrivilegedOperationsImpl |
| 5708 | extends IInputMethodPrivilegedOperations.Stub { |
| 5709 | private final InputMethodManagerService mImms; |
Yohei Yukawa | d3e0fd93 | 2019-01-21 09:16:48 -0800 | [diff] [blame] | 5710 | @NonNull |
Yohei Yukawa | c54c117 | 2018-09-06 11:39:50 -0700 | [diff] [blame] | 5711 | private final IBinder mToken; |
Yohei Yukawa | d3e0fd93 | 2019-01-21 09:16:48 -0800 | [diff] [blame] | 5712 | InputMethodPrivilegedOperationsImpl(InputMethodManagerService imms, |
| 5713 | @NonNull IBinder token) { |
Yohei Yukawa | c54c117 | 2018-09-06 11:39:50 -0700 | [diff] [blame] | 5714 | mImms = imms; |
| 5715 | mToken = token; |
| 5716 | } |
| 5717 | |
| 5718 | @BinderThread |
| 5719 | @Override |
| 5720 | public void setImeWindowStatus(int vis, int backDisposition) { |
| 5721 | mImms.setImeWindowStatus(mToken, vis, backDisposition); |
| 5722 | } |
| 5723 | |
| 5724 | @BinderThread |
| 5725 | @Override |
| 5726 | public void reportStartInput(IBinder startInputToken) { |
| 5727 | mImms.reportStartInput(mToken, startInputToken); |
| 5728 | } |
| 5729 | |
| 5730 | @BinderThread |
| 5731 | @Override |
Yohei Yukawa | c54c117 | 2018-09-06 11:39:50 -0700 | [diff] [blame] | 5732 | public IInputContentUriToken createInputContentUriToken(Uri contentUri, |
| 5733 | String packageName) { |
| 5734 | return mImms.createInputContentUriToken(mToken, contentUri, packageName); |
| 5735 | } |
| 5736 | |
| 5737 | @BinderThread |
| 5738 | @Override |
| 5739 | public void reportFullscreenMode(boolean fullscreen) { |
| 5740 | mImms.reportFullscreenMode(mToken, fullscreen); |
| 5741 | } |
Yohei Yukawa | c7ca368 | 2018-09-09 20:48:38 -0700 | [diff] [blame] | 5742 | |
| 5743 | @BinderThread |
| 5744 | @Override |
| 5745 | public void setInputMethod(String id) { |
| 5746 | mImms.setInputMethod(mToken, id); |
| 5747 | } |
| 5748 | |
| 5749 | @BinderThread |
| 5750 | @Override |
| 5751 | public void setInputMethodAndSubtype(String id, InputMethodSubtype subtype) { |
| 5752 | mImms.setInputMethodAndSubtype(mToken, id, subtype); |
| 5753 | } |
| 5754 | |
| 5755 | @BinderThread |
| 5756 | @Override |
| 5757 | public void hideMySoftInput(int flags) { |
| 5758 | mImms.hideMySoftInput(mToken, flags); |
| 5759 | } |
| 5760 | |
| 5761 | @BinderThread |
| 5762 | @Override |
| 5763 | public void showMySoftInput(int flags) { |
| 5764 | mImms.showMySoftInput(mToken, flags); |
| 5765 | } |
| 5766 | |
| 5767 | @BinderThread |
| 5768 | @Override |
Yohei Yukawa | 41b094f | 2018-09-09 23:58:45 -0700 | [diff] [blame] | 5769 | public void updateStatusIcon(String packageName, @DrawableRes int iconId) { |
Yohei Yukawa | c7ca368 | 2018-09-09 20:48:38 -0700 | [diff] [blame] | 5770 | mImms.updateStatusIcon(mToken, packageName, iconId); |
| 5771 | } |
| 5772 | |
| 5773 | @BinderThread |
| 5774 | @Override |
| 5775 | public boolean switchToPreviousInputMethod() { |
| 5776 | return mImms.switchToPreviousInputMethod(mToken); |
| 5777 | } |
| 5778 | |
| 5779 | @BinderThread |
| 5780 | @Override |
| 5781 | public boolean switchToNextInputMethod(boolean onlyCurrentIme) { |
| 5782 | return mImms.switchToNextInputMethod(mToken, onlyCurrentIme); |
| 5783 | } |
| 5784 | |
| 5785 | @BinderThread |
| 5786 | @Override |
| 5787 | public boolean shouldOfferSwitchingToNextInputMethod() { |
| 5788 | return mImms.shouldOfferSwitchingToNextInputMethod(mToken); |
| 5789 | } |
Yohei Yukawa | c07fd4c | 2018-09-11 11:37:13 -0700 | [diff] [blame] | 5790 | |
| 5791 | @BinderThread |
| 5792 | @Override |
Yohei Yukawa | 9b60ba0 | 2019-01-21 00:06:27 -0800 | [diff] [blame] | 5793 | public void notifyUserAction() { |
Yohei Yukawa | c07fd4c | 2018-09-11 11:37:13 -0700 | [diff] [blame] | 5794 | mImms.notifyUserAction(mToken); |
| 5795 | } |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 5796 | |
| 5797 | @BinderThread |
| 5798 | @Override |
| 5799 | public void reportPreRendered(EditorInfo info) { |
| 5800 | mImms.reportPreRendered(mToken, info); |
| 5801 | } |
| 5802 | |
| 5803 | @BinderThread |
| 5804 | @Override |
Tarandeep Singh | bb0e2f7 | 2020-01-15 13:58:29 -0800 | [diff] [blame] | 5805 | public void applyImeVisibility(IBinder windowToken, boolean setVisible) { |
| 5806 | mImms.applyImeVisibility(mToken, windowToken, setVisible); |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 5807 | } |
Yohei Yukawa | c54c117 | 2018-09-06 11:39:50 -0700 | [diff] [blame] | 5808 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5809 | } |