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

Commit 98f24eb4 authored by Mingguang Xu's avatar Mingguang Xu
Browse files

BTAA: Add methods to propagate uid and package name from Java to native

Tag: #feature

Bug: 202527952
Bug: 172501038

Test: atest BluetoothInstrumentationTests:com.android.bluetooth.btservice.activityattribution
Change-Id: I6bb7b066f10fed492f0214a8e7ce5c401b690971
parent 05ff73af
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
@@ -157,10 +157,41 @@ static void cleanupNative(JNIEnv* env, jobject object) {
  }
}

static void notifyActivityAttributionInfoNative(JNIEnv* env, jobject object,
                                                jint uid, jstring packageName,
                                                jstring deviceAddress) {
  const bt_interface_t* btInf = getBluetoothInterface();
  if (btInf == nullptr) {
    LOG(ERROR) << "Bluetooth module is not loaded";
    return;
  }
  sActivityAttributionInterface =
      (ActivityAttributionInterface*)btInf->get_profile_interface(
          BT_ACTIVITY_ATTRIBUTION_ID);
  if (sActivityAttributionInterface == nullptr) {
    LOG(ERROR) << "Failed to get ActivityAttribution Interface";
    return;
  }

  if (packageName == nullptr || deviceAddress == nullptr) {
    LOG(ERROR) << "Failed to get package name or device address";
    return;
  }
  const char* nativeName = env->GetStringUTFChars(packageName, nullptr);
  const char* nativeAddress = env->GetStringUTFChars(deviceAddress, nullptr);
  sActivityAttributionInterface->NotifyActivityAttributionInfo(uid, nativeName,
                                                               nativeAddress);
  env->ReleaseStringUTFChars(packageName, nativeName);
  env->ReleaseStringUTFChars(deviceAddress, nativeAddress);
}

static JNINativeMethod sMethods[] = {
    {"classInitNative", "()V", (void*)classInitNative},
    {"initNative", "()V", (void*)initNative},
    {"cleanupNative", "()V", (void*)cleanupNative},
    {"notifyActivityAttributionInfoNative",
     "(ILjava/lang/String;Ljava/lang/String;)V",
     (void*)notifyActivityAttributionInfoNative},
};

int register_com_android_bluetooth_btservice_activity_attribution(JNIEnv* env) {
+11 −0
Original line number Diff line number Diff line
@@ -3470,6 +3470,17 @@ public class AdapterService extends Service {
        return mAdapterProperties.getTotalNumOfTrackableAdvertisements();
    }

    /**
     * Notify the UID and package name of the app, and the address of associated active device
     *
     * @param source The attribution source that starts the activity
     * @param deviceAddress The address of the active device associated with the app
     */
    public void notifyActivityAttributionInfo(AttributionSource source, String deviceAddress) {
        mActivityAttributionService.notifyActivityAttributionInfo(
                source.getUid(), source.getPackageName(), deviceAddress);
    }

    private static int convertScanModeToHal(int mode) {
        switch (mode) {
            case BluetoothAdapter.SCAN_MODE_NONE:
+8 −0
Original line number Diff line number Diff line
@@ -59,6 +59,11 @@ public class ActivityAttributionNativeInterface {
        cleanupNative();
    }

    /** Notify the UID and package name of the app, and the address of associated active device */
    public void notifyActivityAttributionInfo(int uid, String packageName, String deviceAddress) {
        notifyActivityAttributionInfoNative(uid, packageName, deviceAddress);
    }

    // Callbacks from the native stack back into the Java framework.
    // All callbacks are routed via the Service which will disambiguate which
    // state machine the message should be routed to.
@@ -77,4 +82,7 @@ public class ActivityAttributionNativeInterface {
    private native void initNative();

    private native void cleanupNative();

    private native void notifyActivityAttributionInfoNative(
        int uid, String packageName, String deviceAddress);
}
+10 −0
Original line number Diff line number Diff line
@@ -95,6 +95,16 @@ public class ActivityAttributionService {
        mActivityAttributionNativeInterface.init();
    }

    /** Notify the UID and package name of the app, and the address of associated active device */
    public void notifyActivityAttributionInfo(int uid, String packageName, String deviceAddress) {
        Log.d(TAG, "notifyActivityAttributionInfo"
                + " UID=" + uid
                + " packageName=" + packageName
                + " deviceAddress=" + deviceAddress);
        mActivityAttributionNativeInterface.notifyActivityAttributionInfo(
                uid, packageName, deviceAddress);
    }

    private boolean isAvailable() {
        return !mCleaningUp;
    }
+6 −0
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ public final class ActivityAttributionServiceTest {
    public void setUp() {
        Assume.assumeTrue("Ignore test when the user is not primary.", isPrimaryUser());
        mActivityAttributionService = new ActivityAttributionService();
        mActivityAttributionService.start();
        assertThat(mActivityAttributionService).isNotNull();
    }

@@ -55,4 +56,9 @@ public final class ActivityAttributionServiceTest {

    @Test
    public void testSetUpAndTearDown() {}

    @Test
    public void testNotifyActivityAttributionInfo() {
        mActivityAttributionService.notifyActivityAttributionInfo(1, "myApp", "address1");
    }
}