Run the wakelock JNI callbacks on the JNI thread

All JNI callbacks must be on the JNI thread, otherwise this could
impact the threads' operation.

Bug: 111277984
Test: A2DP streaming
Change-Id: I94fa4500aea62a574c80749e904434e46eb4e942
(cherry picked from commit c788ad703836f6ac0f63e47b20f27b41dcce9353)
diff --git a/btif/src/bluetooth.cc b/btif/src/bluetooth.cc
index 92a4092..dc59046 100644
--- a/btif/src/bluetooth.cc
+++ b/btif/src/bluetooth.cc
@@ -415,8 +415,32 @@
   return btif_le_test_mode(opcode, buf, len);
 }
 
+static bt_os_callouts_t* wakelock_os_callouts_saved = nullptr;
+
+static int acquire_wake_lock_cb(const char* lock_name) {
+  return do_in_jni_thread(
+      FROM_HERE, base::Bind(base::IgnoreResult(
+                                wakelock_os_callouts_saved->acquire_wake_lock),
+                            lock_name));
+}
+
+static int release_wake_lock_cb(const char* lock_name) {
+  return do_in_jni_thread(
+      FROM_HERE, base::Bind(base::IgnoreResult(
+                                wakelock_os_callouts_saved->release_wake_lock),
+                            lock_name));
+}
+
+static bt_os_callouts_t wakelock_os_callouts_jni = {
+    sizeof(wakelock_os_callouts_jni),
+    nullptr /* not used */,
+    acquire_wake_lock_cb,
+    release_wake_lock_cb,
+};
+
 static int set_os_callouts(bt_os_callouts_t* callouts) {
-  wakelock_set_os_callouts(callouts);
+  wakelock_os_callouts_saved = callouts;
+  wakelock_set_os_callouts(&wakelock_os_callouts_jni);
   return BT_STATUS_SUCCESS;
 }