Example usage for android.app AppOpsManager checkPackage

List of usage examples for android.app AppOpsManager checkPackage

Introduction

In this page you can find the example usage for android.app AppOpsManager checkPackage.

Prototype

public void checkPackage(int uid, String packageName) 

Source Link

Document

Do a quick check to validate if a package name belongs to a UID.

Usage

From source file:com.example.android.commitcontent.app.ImageKeyboard.java

private boolean validatePackageName(@Nullable EditorInfo editorInfo) {
    if (editorInfo == null) {
        return false;
    }/*ww  w .  j  av a2s .c  om*/
    final String packageName = editorInfo.packageName;
    if (packageName == null) {
        return false;
    }

    // In Android L MR-1 and prior devices, EditorInfo.packageName is not a reliable identifier
    // of the target application because:
    //   1. the system does not verify it [1]
    //   2. InputMethodManager.startInputInner() had filled EditorInfo.packageName with
    //      view.getContext().getPackageName() [2]
    // [1]: https://android.googlesource.com/platform/frameworks/base/+/a0f3ad1b5aabe04d9eb1df8bad34124b826ab641
    // [2]: https://android.googlesource.com/platform/frameworks/base/+/02df328f0cd12f2af87ca96ecf5819c8a3470dc8
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return true;
    }

    final InputBinding inputBinding = getCurrentInputBinding();
    if (inputBinding == null) {
        // Due to b.android.com/225029, it is possible that getCurrentInputBinding() returns
        // null even after onStartInputView() is called.
        // TODO: Come up with a way to work around this bug....
        Log.e(TAG,
                "inputBinding should not be null here. " + "You are likely to be hitting b.android.com/225029");
        return false;
    }
    final int packageUid = inputBinding.getUid();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        final AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
        try {
            appOpsManager.checkPackage(packageUid, packageName);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    final PackageManager packageManager = getPackageManager();
    final String possiblePackageNames[] = packageManager.getPackagesForUid(packageUid);
    for (final String possiblePackageName : possiblePackageNames) {
        if (packageName.equals(possiblePackageName)) {
            return true;
        }
    }
    return false;
}

From source file:com.android.server.MountService.java

@Override
public int mkdirs(String callingPkg, String appPath) {
    final int userId = UserHandle.getUserId(Binder.getCallingUid());
    final UserEnvironment userEnv = new UserEnvironment(userId);

    // Validate that reported package name belongs to caller
    final AppOpsManager appOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
    appOps.checkPackage(Binder.getCallingUid(), callingPkg);

    try {/*from w w  w  . j  av a  2 s  .com*/
        appPath = new File(appPath).getCanonicalPath();
    } catch (IOException e) {
        Slog.e(TAG, "Failed to resolve " + appPath + ": " + e);
        return -1;
    }

    if (!appPath.endsWith("/")) {
        appPath = appPath + "/";
    }

    // Try translating the app path into a vold path, but require that it
    // belong to the calling package.
    String voldPath = maybeTranslatePathForVold(appPath, userEnv.buildExternalStorageAppDataDirs(callingPkg),
            userEnv.buildExternalStorageAppDataDirsForVold(callingPkg));
    if (voldPath != null) {
        try {
            mConnector.execute("volume", "mkdirs", voldPath);
            return 0;
        } catch (NativeDaemonConnectorException e) {
            return e.getCode();
        }
    }

    voldPath = maybeTranslatePathForVold(appPath, userEnv.buildExternalStorageAppObbDirs(callingPkg),
            userEnv.buildExternalStorageAppObbDirsForVold(callingPkg));
    if (voldPath != null) {
        try {
            mConnector.execute("volume", "mkdirs", voldPath);
            return 0;
        } catch (NativeDaemonConnectorException e) {
            return e.getCode();
        }
    }

    voldPath = maybeTranslatePathForVold(appPath, userEnv.buildExternalStorageAppMediaDirs(callingPkg),
            userEnv.buildExternalStorageAppMediaDirsForVold(callingPkg));
    if (voldPath != null) {
        try {
            mConnector.execute("volume", "mkdirs", voldPath);
            return 0;
        } catch (NativeDaemonConnectorException e) {
            return e.getCode();
        }
    }

    throw new SecurityException("Invalid mkdirs path: " + appPath);
}