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

Commit 002a253f authored by Michael Sun's avatar Michael Sun Committed by Automerger Merge Worker
Browse files

Merge changes from topics "btaa-jni-1", "btaa-jni-2" am: 531982f9

Original change: https://android-review.googlesource.com/c/platform/packages/apps/Bluetooth/+/1487842

Change-Id: Ib469b3c47561e05d15e3327d3e817358ee309802
parents 9a1e7863 531982f9
Loading
Loading
Loading
Loading
+5 −3
Original line number Original line Diff line number Diff line
@@ -155,6 +155,8 @@ int register_com_android_bluetooth_sdp (JNIEnv* env);
int register_com_android_bluetooth_hearing_aid(JNIEnv* env);
int register_com_android_bluetooth_hearing_aid(JNIEnv* env);


int register_com_android_bluetooth_btservice_BluetoothKeystore(JNIEnv* env);
int register_com_android_bluetooth_btservice_BluetoothKeystore(JNIEnv* env);
}

int register_com_android_bluetooth_btservice_activity_attribution(JNIEnv* env);
}  // namespace android


#endif /* COM_ANDROID_BLUETOOTH_H */
#endif /* COM_ANDROID_BLUETOOTH_H */
+148 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "BluetoothActivityAttributionJni"

#include <string.h>

#include <shared_mutex>

#include "base/logging.h"
#include "com_android_bluetooth.h"
#include "hardware/bt_activity_attribution.h"

using bluetooth::activity_attribution::Activity;
using bluetooth::activity_attribution::ActivityAttributionCallbacks;
using bluetooth::activity_attribution::ActivityAttributionInterface;

namespace android {
static jmethodID method_onWakeup;

static ActivityAttributionInterface* sActivityAttributionInterface = nullptr;
static std::shared_timed_mutex interface_mutex;

static jobject mCallbacksObj = nullptr;
static std::shared_timed_mutex callbacks_mutex;

class ActivityAttributionCallbacksImpl : public ActivityAttributionCallbacks {
 public:
  ~ActivityAttributionCallbacksImpl() = default;

  void OnWakeup(Activity activity, const RawAddress& bd_addr) override {
    LOG(INFO) << __func__;

    std::shared_lock<std::shared_timed_mutex> lock(callbacks_mutex);
    CallbackEnv sCallbackEnv(__func__);
    if (!sCallbackEnv.valid() || mCallbacksObj == nullptr) return;

    ScopedLocalRef<jbyteArray> addr(
        sCallbackEnv.get(), sCallbackEnv->NewByteArray(sizeof(RawAddress)));
    if (!addr.get()) {
      LOG(ERROR)
          << "Failed to allocate jbyteArray for bd_addr of wakeup callback";
      return;
    }

    sCallbackEnv->SetByteArrayRegion(addr.get(), 0, sizeof(RawAddress),
                                     (jbyte*)&bd_addr);
    sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onWakeup, (jint)activity,
                                 addr.get());
  }
};

static ActivityAttributionCallbacksImpl sActivityAttributionCallbacks;

static void classInitNative(JNIEnv* env, jclass clazz) {
  method_onWakeup = env->GetMethodID(clazz, "onWakeup", "(I[B)V");

  LOG(INFO) << __func__ << ": succeeds";
}

static void initNative(JNIEnv* env, jobject object) {
  std::unique_lock<std::shared_timed_mutex> interface_lock(interface_mutex);
  std::unique_lock<std::shared_timed_mutex> callbacks_lock(callbacks_mutex);
  const bt_interface_t* btInf = getBluetoothInterface();
  if (btInf == nullptr) {
    LOG(ERROR) << "Bluetooth module is not loaded";
    return;
  }

  if (sActivityAttributionInterface != nullptr) {
    LOG(INFO)
        << "Cleaning up ActivityAttribution Interface before initializing...";
    sActivityAttributionInterface->Cleanup();
    sActivityAttributionInterface = nullptr;
  }

  if (mCallbacksObj != nullptr) {
    LOG(INFO) << "Cleaning up ActivityAttribution callback object";
    env->DeleteGlobalRef(mCallbacksObj);
    mCallbacksObj = nullptr;
  }

  if ((mCallbacksObj = env->NewGlobalRef(object)) == nullptr) {
    LOG(ERROR)
        << "Failed to allocate Global Ref for ActivityAttribution Callbacks";
    return;
  }

  sActivityAttributionInterface =
      (ActivityAttributionInterface*)btInf->get_profile_interface(
          BT_ACTIVITY_ATTRIBUTION_ID);
  if (sActivityAttributionInterface == nullptr) {
    LOG(ERROR) << "Failed to get ActivityAttribution Interface";
    return;
  }

  sActivityAttributionInterface->Init(&sActivityAttributionCallbacks);
}

static void cleanupNative(JNIEnv* env, jobject object) {
  std::unique_lock<std::shared_timed_mutex> interface_lock(interface_mutex);
  std::unique_lock<std::shared_timed_mutex> callbacks_lock(callbacks_mutex);

  const bt_interface_t* btInf = getBluetoothInterface();
  if (btInf == nullptr) {
    LOG(ERROR) << "Bluetooth module is not loaded";
    return;
  }

  if (sActivityAttributionInterface != nullptr) {
    sActivityAttributionInterface->Cleanup();
    sActivityAttributionInterface = nullptr;
  }

  if (mCallbacksObj != nullptr) {
    env->DeleteGlobalRef(mCallbacksObj);
    mCallbacksObj = nullptr;
  }
}

static JNINativeMethod sMethods[] = {
    {"classInitNative", "()V", (void*)classInitNative},
    {"initNative", "()V", (void*)initNative},
    {"cleanupNative", "()V", (void*)cleanupNative},
};

int register_com_android_bluetooth_btservice_activity_attribution(JNIEnv* env) {
  return jniRegisterNativeMethods(
      env,
      "com/android/bluetooth/btservice/activityattribution/"
      "ActivityAttributionNativeInterface",
      sMethods, NELEM(sMethods));
}

}  // namespace android
+7 −0
Original line number Original line Diff line number Diff line
@@ -1401,6 +1401,13 @@ jint JNI_OnLoad(JavaVM* jvm, void* reserved) {
    return JNI_ERR;
    return JNI_ERR;
  }
  }


  status =
      android::register_com_android_bluetooth_btservice_activity_attribution(e);
  if (status < 0) {
    ALOGE("jni activity attribution registration failure: %d", status);
    return JNI_ERR;
  }

  status =
  status =
      android::register_com_android_bluetooth_btservice_BluetoothKeystore(e);
      android::register_com_android_bluetooth_btservice_BluetoothKeystore(e);
  if (status < 0) {
  if (status < 0) {
+9 −0
Original line number Original line Diff line number Diff line
@@ -84,6 +84,7 @@ import com.android.bluetooth.Utils;
import com.android.bluetooth.a2dp.A2dpService;
import com.android.bluetooth.a2dp.A2dpService;
import com.android.bluetooth.a2dpsink.A2dpSinkService;
import com.android.bluetooth.a2dpsink.A2dpSinkService;
import com.android.bluetooth.btservice.RemoteDevices.DeviceProperties;
import com.android.bluetooth.btservice.RemoteDevices.DeviceProperties;
import com.android.bluetooth.btservice.activityattribution.ActivityAttributionService;
import com.android.bluetooth.btservice.bluetoothkeystore.BluetoothKeystoreService;
import com.android.bluetooth.btservice.bluetoothkeystore.BluetoothKeystoreService;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.btservice.storage.MetadataDatabase;
import com.android.bluetooth.btservice.storage.MetadataDatabase;
@@ -234,6 +235,7 @@ public class AdapterService extends Service {
    private BluetoothKeystoreService mBluetoothKeystoreService;
    private BluetoothKeystoreService mBluetoothKeystoreService;
    private A2dpService mA2dpService;
    private A2dpService mA2dpService;
    private A2dpSinkService mA2dpSinkService;
    private A2dpSinkService mA2dpSinkService;
    private ActivityAttributionService mActivityAttributionService;
    private HeadsetService mHeadsetService;
    private HeadsetService mHeadsetService;
    private HeadsetClientService mHeadsetClientService;
    private HeadsetClientService mHeadsetClientService;
    private BluetoothMapService mMapService;
    private BluetoothMapService mMapService;
@@ -444,6 +446,8 @@ public class AdapterService extends Service {
        mJniCallbacks = new JniCallbacks(this, mAdapterProperties);
        mJniCallbacks = new JniCallbacks(this, mAdapterProperties);
        mBluetoothKeystoreService = new BluetoothKeystoreService(isNiapMode());
        mBluetoothKeystoreService = new BluetoothKeystoreService(isNiapMode());
        mBluetoothKeystoreService.start();
        mBluetoothKeystoreService.start();
        mActivityAttributionService = new ActivityAttributionService();
        mActivityAttributionService.start();
        int configCompareResult = mBluetoothKeystoreService.getCompareResult();
        int configCompareResult = mBluetoothKeystoreService.getCompareResult();


        // Android TV doesn't show consent dialogs for just works and encryption only le pairing
        // Android TV doesn't show consent dialogs for just works and encryption only le pairing
@@ -464,6 +468,7 @@ public class AdapterService extends Service {
                ServiceManager.getService(BatteryStats.SERVICE_NAME));
                ServiceManager.getService(BatteryStats.SERVICE_NAME));


        mBluetoothKeystoreService.initJni();
        mBluetoothKeystoreService.initJni();
        mActivityAttributionService.initJni();


        mSdpManager = SdpManager.init(this);
        mSdpManager = SdpManager.init(this);
        registerReceiver(mAlarmBroadcastReceiver, new IntentFilter(ACTION_ALARM_WAKEUP));
        registerReceiver(mAlarmBroadcastReceiver, new IntentFilter(ACTION_ALARM_WAKEUP));
@@ -766,6 +771,10 @@ public class AdapterService extends Service {
            mBluetoothKeystoreService.cleanup();
            mBluetoothKeystoreService.cleanup();
        }
        }


        if (mActivityAttributionService != null) {
            mActivityAttributionService.cleanup();
        }

        if (mNativeAvailable) {
        if (mNativeAvailable) {
            debugLog("cleanup() - Cleaning up adapter native");
            debugLog("cleanup() - Cleaning up adapter native");
            cleanupNative();
            cleanupNative();