Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 89bbe023 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Stop relying on AppOpsManager#checkPackage(), which is deprecated"

parents 94debd30 d32b7a0a
Loading
Loading
Loading
Loading
+4 −7
Original line number Diff line number Diff line
@@ -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;
@@ -307,7 +306,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;
@@ -1734,7 +1732,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);
@@ -2520,7 +2517,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);
@@ -3957,7 +3954,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
            return false;
        }
        if (getCurIntentLocked() != null && InputMethodUtils.checkIfPackageBelongsToUid(
                mAppOpsManager,
                mPackageManagerInternal,
                uid,
                getCurIntentLocked().getComponent().getPackageName())) {
            return true;
@@ -4208,8 +4205,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);
        }
+10 −10
Original line number Diff line number Diff line
@@ -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;
    }

    /**