Stop relying on AppOpsManager#checkPackage(), which is deprecated
With this CL,
InputMethodUtils#checkIfPackageBelongsToUid()
starts using
PackageManagerInternal#getPackageUid()
instead of
AppOpsManager#checkPackage(),
which is deprecated [1].
There should be no semantic change in this CL.
[1]: I90215a638a3c380fcffde2b9209c4386f8fd70ea
a04b9ab403
Bug: 234882948
Test: presubmit
Change-Id: I069b058b4240fb7ab638915b03d433e32660870f
This commit is contained in:
@@ -69,7 +69,6 @@ import android.annotation.UserIdInt;
|
||||
import android.app.ActivityManager;
|
||||
import android.app.ActivityManagerInternal;
|
||||
import android.app.AppGlobals;
|
||||
import android.app.AppOpsManager;
|
||||
import android.app.KeyguardManager;
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
@@ -309,7 +308,6 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
|
||||
final boolean mHasFeature;
|
||||
private final ArrayMap<String, List<InputMethodSubtype>> mAdditionalSubtypeMap =
|
||||
new ArrayMap<>();
|
||||
private final AppOpsManager mAppOpsManager;
|
||||
private final UserManager mUserManager;
|
||||
private final UserManagerInternal mUserManagerInternal;
|
||||
private final InputMethodMenuController mMenuController;
|
||||
@@ -1737,7 +1735,6 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
|
||||
mInputMethodDeviceConfigs = new InputMethodDeviceConfigs();
|
||||
mImeDisplayValidator = mWindowManagerInternal::getDisplayImePolicy;
|
||||
mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
|
||||
mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
|
||||
mUserManager = mContext.getSystemService(UserManager.class);
|
||||
mUserManagerInternal = LocalServices.getService(UserManagerInternal.class);
|
||||
mAccessibilityManager = AccessibilityManager.getInstance(context);
|
||||
@@ -2523,7 +2520,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
|
||||
null, null, null, selectedMethodId, getSequenceNumberLocked(), null, false);
|
||||
}
|
||||
|
||||
if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.mUid,
|
||||
if (!InputMethodUtils.checkIfPackageBelongsToUid(mPackageManagerInternal, cs.mUid,
|
||||
editorInfo.packageName)) {
|
||||
Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
|
||||
+ " uid=" + cs.mUid + " package=" + editorInfo.packageName);
|
||||
@@ -3962,7 +3959,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
|
||||
return false;
|
||||
}
|
||||
if (getCurIntentLocked() != null && InputMethodUtils.checkIfPackageBelongsToUid(
|
||||
mAppOpsManager,
|
||||
mPackageManagerInternal,
|
||||
uid,
|
||||
getCurIntentLocked().getComponent().getPackageName())) {
|
||||
return true;
|
||||
@@ -4224,8 +4221,8 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
|
||||
final int callingUid = Binder.getCallingUid();
|
||||
final ComponentName imeComponentName =
|
||||
imeId != null ? ComponentName.unflattenFromString(imeId) : null;
|
||||
if (imeComponentName == null || !InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager,
|
||||
callingUid, imeComponentName.getPackageName())) {
|
||||
if (imeComponentName == null || !InputMethodUtils.checkIfPackageBelongsToUid(
|
||||
mPackageManagerInternal, callingUid, imeComponentName.getPackageName())) {
|
||||
throw new SecurityException("Calling UID=" + callingUid + " does not belong to imeId="
|
||||
+ imeId);
|
||||
}
|
||||
|
||||
@@ -20,12 +20,12 @@ import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.annotation.UserHandleAware;
|
||||
import android.annotation.UserIdInt;
|
||||
import android.app.AppOpsManager;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.IPackageManager;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.PackageManagerInternal;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Binder;
|
||||
import android.os.Build;
|
||||
@@ -218,20 +218,20 @@ final class InputMethodUtils {
|
||||
/**
|
||||
* Returns true if a package name belongs to a UID.
|
||||
*
|
||||
* <p>This is a simple wrapper of {@link AppOpsManager#checkPackage(int, String)}.</p>
|
||||
* @param appOpsManager the {@link AppOpsManager} object to be used for the validation.
|
||||
* <p>This is a simple wrapper of
|
||||
* {@link PackageManagerInternal#getPackageUid(String, long, int)}.</p>
|
||||
* @param packageManagerInternal the {@link PackageManagerInternal} object to be used for the
|
||||
* validation.
|
||||
* @param uid the UID to be validated.
|
||||
* @param packageName the package name.
|
||||
* @return {@code true} if the package name belongs to the UID.
|
||||
*/
|
||||
static boolean checkIfPackageBelongsToUid(AppOpsManager appOpsManager,
|
||||
static boolean checkIfPackageBelongsToUid(PackageManagerInternal packageManagerInternal,
|
||||
int uid, String packageName) {
|
||||
try {
|
||||
appOpsManager.checkPackage(uid, packageName);
|
||||
return true;
|
||||
} catch (SecurityException e) {
|
||||
return false;
|
||||
}
|
||||
// PackageManagerInternal#getPackageUid() doesn't check MATCH_INSTANT/MATCH_APEX as of
|
||||
// writing. So setting 0 should be fine.
|
||||
return packageManagerInternal.getPackageUid(packageName, 0 /* flags */,
|
||||
UserHandle.getUserId(uid)) == uid;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user