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

Commit bb7a2170 authored by Prabir Pradhan's avatar Prabir Pradhan
Browse files

InputManagerService: Add method to get the UID for a package

The InputTracer will use this to tell if a UID target matches a list of
packages specified in the trace config.

Bug: 210460522
Test: manual with perfetto
Change-Id: I53bd1b33eefcc7656655e6cbfbef753406b93bd3
parent 982d9a38
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -2604,6 +2604,19 @@ public class InputManagerService extends IInputManager.Stub
        mBatteryController.notifyStylusGestureStarted(deviceId, eventTime);
    }

    // Native callback.
    @SuppressWarnings("unused")
    private int getPackageUid(String pkg) {
        if (TextUtils.isEmpty(pkg)) {
            return Process.INVALID_UID;
        }
        try {
            return mContext.getPackageManager().getPackageUid(pkg, 0 /*flags*/);
        } catch (PackageManager.NameNotFoundException e) {
            return Process.INVALID_UID;
        }
    }

    /**
     * Flatten a map into a string list, with value positioned directly next to the
     * key.
+19 −0
Original line number Diff line number Diff line
@@ -143,6 +143,7 @@ static struct {
    jmethodID getTouchCalibrationForInputDevice;
    jmethodID notifyDropWindow;
    jmethodID getParentSurfaceForPointers;
    jmethodID getPackageUid;
} gServiceClassInfo;

static struct {
@@ -362,6 +363,7 @@ public:
    void notifyDropWindow(const sp<IBinder>& token, float x, float y) override;
    void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp,
                                 const std::set<gui::Uid>& uids) override;
    gui::Uid getPackageUid(std::string package) override;

    /* --- PointerControllerPolicyInterface implementation --- */

@@ -1116,6 +1118,21 @@ void NativeInputManager::notifyDeviceInteraction(int32_t deviceId, nsecs_t times
    mInputManager->getMetricsCollector().notifyDeviceInteraction(deviceId, timestamp, uids);
}

gui::Uid NativeInputManager::getPackageUid(std::string package) {
    ATRACE_CALL();
    JNIEnv* env = jniEnv();
    ScopedLocalFrame localFrame(env);

    ScopedLocalRef<jstring> javaPackage(env, env->NewStringUTF(package.c_str()));
    const jint uid =
            env->CallIntMethod(mServiceObj, gServiceClassInfo.getPackageUid, javaPackage.get());
    if (checkAndClearExceptionFromCallback(env, "getPackageUid")) {
        LOG(FATAL) << __func__ << ": Failed to get UID for package: " << package;
    }

    return gui::Uid{static_cast<uint32_t>(uid)};
}

void NativeInputManager::notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
                                           InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
                                           const std::vector<float>& values) {
@@ -3101,6 +3118,8 @@ int register_android_server_InputManager(JNIEnv* env) {
    GET_METHOD_ID(gServiceClassInfo.getParentSurfaceForPointers, clazz,
                  "getParentSurfaceForPointers", "(I)J");

    GET_METHOD_ID(gServiceClassInfo.getPackageUid, clazz, "getPackageUid", "(Ljava/lang/String;)I");

    // InputDevice

    FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");