Example usage for android.view.inputmethod InputBinding getUid

List of usage examples for android.view.inputmethod InputBinding getUid

Introduction

In this page you can find the example usage for android.view.inputmethod InputBinding getUid.

Prototype

public int getUid() 

Source Link

Document

Return the user id of the client associated with this binding.

Usage

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

private boolean validatePackageName(@Nullable EditorInfo editorInfo) {
    if (editorInfo == null) {
        return false;
    }/*from   w  w w. ja v a 2  s. 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;
}