Skip to content

Commit 0605925

Browse files
adrianroosofficialputuid
authored andcommitted
Restrict getInputMethodWindowVisibleHeight
Make sure only the app currently interacting with the IME can query this, and restrict the API to apps targeting SDKs before T Fixes: 204906124 Test: atest 'InputMethodManagerTest#getInputMethodWindowVisibleHeight_returnsZeroIfNotFocused' Change-Id: If1da19a3dd8c29542afc970b4b201d87547c27a9 Merged-In: If1da19a3dd8c29542afc970b4b201d87547c27a9 (cherry picked from commit fd7847b) Merged-In: If1da19a3dd8c29542afc970b4b201d87547c27a9
1 parent 1ff396f commit 0605925

4 files changed

Lines changed: 55 additions & 20 deletions

File tree

core/java/android/view/inputmethod/InputMethodManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2957,7 +2957,7 @@ public Map<InputMethodInfo, List<InputMethodSubtype>> getShortcutInputMethodsAnd
29572957
@UnsupportedAppUsage
29582958
public int getInputMethodWindowVisibleHeight() {
29592959
try {
2960-
return mService.getInputMethodWindowVisibleHeight();
2960+
return mService.getInputMethodWindowVisibleHeight(mClient);
29612961
} catch (RemoteException e) {
29622962
throw e.rethrowFromSystemServer();
29632963
}

core/java/com/android/internal/view/IInputMethodManager.aidl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ interface IInputMethodManager {
6767
void setAdditionalInputMethodSubtypes(String id, in InputMethodSubtype[] subtypes);
6868
// This is kept due to @UnsupportedAppUsage.
6969
// TODO(Bug 113914148): Consider removing this.
70-
int getInputMethodWindowVisibleHeight();
70+
int getInputMethodWindowVisibleHeight(in IInputMethodClient client);
7171

7272
void reportActivityView(in IInputMethodClient parentClient, int childDisplayId,
7373
in float[] matrixValues);

services/core/java/com/android/server/inputmethod/InputMethodManagerService.java

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
import android.util.Printer;
103103
import android.util.Slog;
104104
import android.util.SparseArray;
105+
import android.util.SparseBooleanArray;
105106
import android.view.ContextThemeWrapper;
106107
import android.view.DisplayInfo;
107108
import android.view.IWindowManager;
@@ -314,6 +315,8 @@ private static final class DebugFlags {
314315
final InputMethodSettings mSettings;
315316
final SettingsObserver mSettingsObserver;
316317
final IWindowManager mIWindowManager;
318+
private final SparseBooleanArray mLoggedDeniedGetInputMethodWindowVisibleHeightForUid =
319+
new SparseBooleanArray(0);
317320
final WindowManagerInternal mWindowManagerInternal;
318321
final PackageManagerInternal mPackageManagerInternal;
319322
final InputManagerInternal mInputManagerInternal;
@@ -1386,6 +1389,13 @@ public void onFinishPackageChanges() {
13861389
clearPackageChangeState();
13871390
}
13881391

1392+
@Override
1393+
public void onUidRemoved(int uid) {
1394+
synchronized (mMethodMap) {
1395+
mLoggedDeniedGetInputMethodWindowVisibleHeightForUid.delete(uid);
1396+
}
1397+
}
1398+
13891399
private void clearPackageChangeState() {
13901400
// No need to lock them because we access these fields only on getRegisteredHandler().
13911401
mChangedPackages.clear();
@@ -3134,20 +3144,8 @@ public boolean showSoftInput(IInputMethodClient client, IBinder windowToken, int
31343144
}
31353145
final long ident = Binder.clearCallingIdentity();
31363146
try {
3137-
if (mCurClient == null || client == null
3138-
|| mCurClient.client.asBinder() != client.asBinder()) {
3139-
// We need to check if this is the current client with
3140-
// focus in the window manager, to allow this call to
3141-
// be made before input is started in it.
3142-
final ClientState cs = mClients.get(client.asBinder());
3143-
if (cs == null) {
3144-
throw new IllegalArgumentException("unknown client " + client.asBinder());
3145-
}
3146-
if (!mWindowManagerInternal.isInputMethodClientFocus(cs.uid, cs.pid,
3147-
cs.selfReportedDisplayId)) {
3148-
Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
3149-
return false;
3150-
}
3147+
if (!canInteractWithImeLocked(uid, client, "showSoftInput")) {
3148+
return false;
31513149
}
31523150
if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
31533151
return showCurrentInputLocked(windowToken, flags, resultReceiver,
@@ -3928,9 +3926,46 @@ public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[]
39283926
* @return {@link WindowManagerInternal#getInputMethodWindowVisibleHeight()}
39293927
*/
39303928
@Override
3931-
public int getInputMethodWindowVisibleHeight() {
3932-
// TODO(yukawa): Should we verify the display ID?
3933-
return mWindowManagerInternal.getInputMethodWindowVisibleHeight(mCurTokenDisplayId);
3929+
@Deprecated
3930+
public int getInputMethodWindowVisibleHeight(@NonNull IInputMethodClient client) {
3931+
int callingUid = Binder.getCallingUid();
3932+
return Binder.withCleanCallingIdentity(() -> {
3933+
final int curTokenDisplayId;
3934+
synchronized (mMethodMap) {
3935+
if (!canInteractWithImeLocked(callingUid, client,
3936+
"getInputMethodWindowVisibleHeight")) {
3937+
if (!mLoggedDeniedGetInputMethodWindowVisibleHeightForUid.get(callingUid)) {
3938+
EventLog.writeEvent(0x534e4554, "204906124", callingUid, "");
3939+
mLoggedDeniedGetInputMethodWindowVisibleHeightForUid.put(callingUid, true);
3940+
}
3941+
return 0;
3942+
}
3943+
// This should probably use the caller's display id, but because this is unsupported
3944+
// and maintained only for compatibility, there's no point in fixing it.
3945+
curTokenDisplayId = mCurTokenDisplayId;
3946+
}
3947+
return mWindowManagerInternal.getInputMethodWindowVisibleHeight(curTokenDisplayId);
3948+
});
3949+
}
3950+
3951+
private boolean canInteractWithImeLocked(int callingUid, IInputMethodClient client,
3952+
String method) {
3953+
if (mCurClient == null || client == null
3954+
|| mCurClient.client.asBinder() != client.asBinder()) {
3955+
// We need to check if this is the current client with
3956+
// focus in the window manager, to allow this call to
3957+
// be made before input is started in it.
3958+
final ClientState cs = mClients.get(client.asBinder());
3959+
if (cs == null) {
3960+
throw new IllegalArgumentException("unknown client " + client.asBinder());
3961+
}
3962+
if (!mWindowManagerInternal.isInputMethodClientFocus(cs.uid, cs.pid,
3963+
cs.selfReportedDisplayId)) {
3964+
Slog.w(TAG, "Ignoring " + method + " of uid " + callingUid + ": " + client);
3965+
return false;
3966+
}
3967+
}
3968+
return true;
39343969
}
39353970

39363971
@Override

services/core/java/com/android/server/inputmethod/MultiClientInputMethodManagerService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1754,7 +1754,7 @@ public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[]
17541754

17551755
@BinderThread
17561756
@Override
1757-
public int getInputMethodWindowVisibleHeight() {
1757+
public int getInputMethodWindowVisibleHeight(IInputMethodClient client) {
17581758
reportNotSupported();
17591759
return 0;
17601760
}

0 commit comments

Comments
 (0)