Merge "Remove native binder implementation details in libbluetooth_jni"
diff --git a/jni/Android.bp b/jni/Android.bp
index ec72acf..39b9f3f 100644
--- a/jni/Android.bp
+++ b/jni/Android.bp
@@ -2,7 +2,6 @@
     name: "libbluetooth_jni",
     compile_multilib: "first",
     srcs: [
-        "bluetooth_socket_manager.cc",
         "com_android_bluetooth_btservice_AdapterService.cpp",
         "com_android_bluetooth_hfp.cpp",
         "com_android_bluetooth_hfpclient.cpp",
@@ -16,18 +15,13 @@
         "com_android_bluetooth_pan.cpp",
         "com_android_bluetooth_gatt.cpp",
         "com_android_bluetooth_sdp.cpp",
-        "IUserManager.cc",
-        "permission_helpers.cc",
     ],
     header_libs: ["libbluetooth_headers"],
     include_dirs: [
         "system/bt/types",
     ],
     shared_libs: [
-        "libandroid_runtime",
         "libbase",
-        "libbinder",
-        "libbluetooth-binder",
         "libchrome",
         "libnativehelper",
         "liblog",
diff --git a/jni/IUserManager.cc b/jni/IUserManager.cc
deleted file mode 100644
index 0e36292..0000000
--- a/jni/IUserManager.cc
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Copyright (C) 2015 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 "IUserManager"
-#include <binder/Parcel.h>
-#include <stdint.h>
-#include <sys/types.h>
-#include <utils/Log.h>
-
-#include "IUserManager.h"
-
-namespace android {
-
-class BpUserManager : public BpInterface<IUserManager> {
- public:
-  explicit BpUserManager(const sp<IBinder>& impl)
-      : BpInterface<IUserManager>(impl) {}
-  virtual int32_t getCredentialOwnerProfile(int32_t user_id) {
-    Parcel data, reply;
-    data.writeInterfaceToken(IUserManager::getInterfaceDescriptor());
-    data.writeInt32(user_id);
-    status_t rc =
-        remote()->transact(GET_CREDENTIAL_OWNER_PROFILE, data, &reply, 0);
-    if (rc != NO_ERROR) {
-      ALOGE("%s: failed (%d)\n", __func__, rc);
-      return -1;
-    }
-
-    int32_t exception = reply.readExceptionCode();
-    if (exception != 0) {
-      ALOGE("%s: got exception (%d)\n", __func__, exception);
-      return -1;
-    }
-
-    return reply.readInt32();
-  }
-
-  virtual int32_t getProfileParentId(int32_t user_handle) {
-    Parcel data, reply;
-    data.writeInterfaceToken(IUserManager::getInterfaceDescriptor());
-    data.writeInt32(user_handle);
-    status_t rc = remote()->transact(GET_PROFILE_PARENT_ID, data, &reply, 0);
-    if (rc != NO_ERROR) {
-      ALOGE("%s: failed (%d)\n", __func__, rc);
-      return -1;
-    }
-
-    int32_t exception = reply.readExceptionCode();
-    if (exception != 0) {
-      ALOGE("%s: got exception (%d)\n", __func__, exception);
-      return -1;
-    }
-
-    return reply.readInt32();
-  }
-};
-
-IMPLEMENT_META_INTERFACE(UserManager, "android.os.IUserManager");
-
-};  // namespace android
diff --git a/jni/IUserManager.h b/jni/IUserManager.h
deleted file mode 100644
index 2164d9a..0000000
--- a/jni/IUserManager.h
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright (C) 2015 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.
- */
-
-#ifndef IUSERMANAGER_H_
-#define IUSERMANAGER_H_
-
-#include <binder/IInterface.h>
-#include <binder/Parcel.h>
-#include <inttypes.h>
-#include <utils/Errors.h>
-#include <utils/Vector.h>
-
-namespace android {
-
-/*
- * Communication channel to UserManager
- */
-class IUserManager : public IInterface {
- public:
-  // must be kept in sync with IUserManager.aidl
-  enum {
-    GET_CREDENTIAL_OWNER_PROFILE = IBinder::FIRST_CALL_TRANSACTION + 0,
-    GET_PROFILE_PARENT_ID,
-  };
-
-  virtual int32_t getCredentialOwnerProfile(int32_t user_id) = 0;
-  virtual int32_t getProfileParentId(int32_t user_handle) = 0;
-
-  DECLARE_META_INTERFACE(UserManager);
-};
-
-};  // namespace android
-
-#endif  // IUSERMANAGER_H_
diff --git a/jni/bluetooth_socket_manager.cc b/jni/bluetooth_socket_manager.cc
deleted file mode 100644
index ba45aa5..0000000
--- a/jni/bluetooth_socket_manager.cc
+++ /dev/null
@@ -1,121 +0,0 @@
-/*
- * Copyright (C) 2017 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.
- */
-
-#include "bluetooth_socket_manager.h"
-#include "permission_helpers.h"
-
-#include <base/logging.h>
-#include <binder/IPCThreadState.h>
-
-using ::android::String8;
-using ::android::binder::Status;
-using ::android::os::ParcelFileDescriptor;
-using ::android::os::ParcelUuid;
-
-namespace android {
-namespace bluetooth {
-
-Status BluetoothSocketManagerBinderServer::connectSocket(
-    const BluetoothDevice& device, int32_t type,
-    const std::unique_ptr<ParcelUuid>& uuid, int32_t port, int32_t flag,
-    std::unique_ptr<ParcelFileDescriptor>* _aidl_return) {
-  if (!isCallerActiveUserOrManagedProfile()) {
-    LOG(WARNING) << "connectSocket() - Not allowed for non-active users";
-    return Status::fromExceptionCode(
-        Status::EX_SECURITY, String8("Not allowed for non-active users"));
-  }
-
-  ENFORCE_PERMISSION(PERMISSION_BLUETOOTH);
-
-  IPCThreadState* ipc = IPCThreadState::self();
-
-  int socket_fd = -1;
-  bt_status_t status = socketInterface->connect(
-      &device.address, (btsock_type_t)type, uuid ? &uuid->uuid : nullptr, port,
-      &socket_fd, flag, ipc->getCallingUid());
-  if (status != BT_STATUS_SUCCESS) {
-    LOG(ERROR) << "Socket connection failed: " << +status;
-    socket_fd = -1;
-  }
-
-  if (socket_fd < 0) {
-    LOG(ERROR) << "Fail to create file descriptor on socket fd";
-    return Status::ok();
-  }
-
-  _aidl_return->reset(
-      new ParcelFileDescriptor(android::base::unique_fd(socket_fd)));
-  return Status::ok();
-}
-
-Status BluetoothSocketManagerBinderServer::createSocketChannel(
-    int32_t type, const std::unique_ptr<::android::String16>& serviceName,
-    const std::unique_ptr<ParcelUuid>& uuid, int32_t port, int32_t flag,
-    std::unique_ptr<ParcelFileDescriptor>* _aidl_return) {
-  if (!isCallerActiveUserOrManagedProfile()) {
-    LOG(WARNING) << "createSocketChannel() - Not allowed for non-active users";
-    return Status::fromExceptionCode(
-        Status::EX_SECURITY, String8("Not allowed for non-active users"));
-  }
-
-  ENFORCE_PERMISSION(PERMISSION_BLUETOOTH);
-
-  VLOG(1) << __func__ << ": SOCK FLAG=" << flag;
-
-  IPCThreadState* ipc = IPCThreadState::self();
-  int socket_fd = -1;
-
-  const std::string payload_url{};
-
-  bt_status_t status = socketInterface->listen(
-      (btsock_type_t)type,
-      serviceName ? String8{*serviceName}.c_str() : nullptr,
-      uuid ? &uuid->uuid : nullptr, port, &socket_fd, flag,
-      ipc->getCallingUid());
-
-  if (status != BT_STATUS_SUCCESS) {
-    LOG(ERROR) << "Socket listen failed: " << +status;
-    socket_fd = -1;
-  }
-
-  if (socket_fd < 0) {
-    LOG(ERROR) << "Failed to create file descriptor on socket fd";
-    return Status::ok();
-  }
-
-  _aidl_return->reset(
-      new ParcelFileDescriptor(android::base::unique_fd(socket_fd)));
-  return Status::ok();
-}
-
-Status BluetoothSocketManagerBinderServer::requestMaximumTxDataLength(
-    const BluetoothDevice& device) {
-  if (!isCallerActiveUserOrManagedProfile()) {
-    LOG(WARNING) << __func__ << ": Not allowed for non-active users";
-    return Status::fromExceptionCode(
-        Status::EX_SECURITY, String8("Not allowed for non-active users"));
-  }
-
-  ENFORCE_PERMISSION(PERMISSION_BLUETOOTH);
-
-  VLOG(1) << __func__;
-
-  socketInterface->request_max_tx_data_length(device.address);
-  return Status::ok();
-}
-
-}  // namespace bluetooth
-}  // namespace android
diff --git a/jni/bluetooth_socket_manager.h b/jni/bluetooth_socket_manager.h
deleted file mode 100644
index e732dcc..0000000
--- a/jni/bluetooth_socket_manager.h
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Copyright (C) 2017 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.
- */
-
-#include <android/bluetooth/BnBluetoothSocketManager.h>
-#include <android/bluetooth/IBluetoothSocketManager.h>
-#include <base/macros.h>
-#include <binder/IBinder.h>
-#include <binder/IInterface.h>
-#include <hardware/bluetooth.h>
-#include <hardware/bt_sock.h>
-
-namespace android {
-namespace bluetooth {
-
-using ::android::binder::Status;
-
-class BluetoothSocketManagerBinderServer : public BnBluetoothSocketManager {
- public:
-  explicit BluetoothSocketManagerBinderServer(
-      const btsock_interface_t* socketInterface)
-      : socketInterface(socketInterface) {}
-  ~BluetoothSocketManagerBinderServer() override = default;
-
-  Status connectSocket(
-      const BluetoothDevice& device, int32_t type,
-      const std::unique_ptr<::android::os::ParcelUuid>& uuid, int32_t port,
-      int32_t flag,
-      std::unique_ptr<::android::os::ParcelFileDescriptor>* _aidl_return);
-
-  Status createSocketChannel(
-      int32_t type, const std::unique_ptr<::android::String16>& serviceName,
-      const std::unique_ptr<::android::os::ParcelUuid>& uuid, int32_t port,
-      int32_t flag,
-      std::unique_ptr<::android::os::ParcelFileDescriptor>* _aidl_return)
-      override;
-
-  Status requestMaximumTxDataLength(const BluetoothDevice& device);
-
- private:
-  const btsock_interface_t* socketInterface;
-  DISALLOW_COPY_AND_ASSIGN(BluetoothSocketManagerBinderServer);
-};
-}  // namespace bluetooth
-}  // namespace android
\ No newline at end of file
diff --git a/jni/com_android_bluetooth_btservice_AdapterService.cpp b/jni/com_android_bluetooth_btservice_AdapterService.cpp
index c6bbc0c..6ba1653 100644
--- a/jni/com_android_bluetooth_btservice_AdapterService.cpp
+++ b/jni/com_android_bluetooth_btservice_AdapterService.cpp
@@ -15,10 +15,8 @@
  */
 
 #define LOG_TAG "BluetoothServiceJni"
-#include "bluetooth_socket_manager.h"
 #include "com_android_bluetooth.h"
 #include "hardware/bt_sock.h"
-#include "permission_helpers.h"
 #include "utils/Log.h"
 #include "utils/misc.h"
 
@@ -1127,14 +1125,6 @@
   return (ret == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;
 }
 
-static void setSystemUiUidNative(JNIEnv* env, jobject obj, jint uid) {
-  android::bluetooth::systemUiUid = uid;
-}
-
-static void setForegroundUserIdNative(JNIEnv* env, jclass clazz, jint id) {
-  android::bluetooth::foregroundUserId = id;
-}
-
 static int readEnergyInfo() {
   ALOGV("%s", __func__);
 
@@ -1334,8 +1324,6 @@
     {"pinReplyNative", "([BZI[B)Z", (void*)pinReplyNative},
     {"sspReplyNative", "([BIZI)Z", (void*)sspReplyNative},
     {"getRemoteServicesNative", "([B)Z", (void*)getRemoteServicesNative},
-    {"setSystemUiUidNative", "(I)V", (void*)setSystemUiUidNative},
-    {"setForegroundUserIdNative", "(I)V", (void*)setForegroundUserIdNative},
     {"alarmFiredNative", "()V", (void*)alarmFiredNative},
     {"readEnergyInfo", "()I", (void*)readEnergyInfo},
     {"dumpNative", "(Ljava/io/FileDescriptor;[Ljava/lang/String;)V",
diff --git a/jni/permission_helpers.cc b/jni/permission_helpers.cc
deleted file mode 100644
index 26a8a89..0000000
--- a/jni/permission_helpers.cc
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Copyright 2017 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.
- */
-
-#include "permission_helpers.h"
-
-#include <base/logging.h>
-#include <base/strings/stringprintf.h>
-#include <binder/IPCThreadState.h>
-#include <binder/IServiceManager.h>
-#include <pwd.h>
-#include <sys/types.h>
-#include "IUserManager.h"
-
-using ::android::binder::Status;
-
-namespace android {
-namespace bluetooth {
-
-uid_t foregroundUserId;
-uid_t systemUiUid;
-static uid_t SYSTEM_UID = 1000;
-constexpr int PER_USER_RANGE = 100000;
-
-Status checkPermission(const char* permission) {
-  int32_t pid;
-  int32_t uid;
-
-  if (android::checkCallingPermission(String16(permission), &pid, &uid)) {
-    return Status::ok();
-  }
-
-  auto err = ::base::StringPrintf("UID %d / PID %d lacks permission %s", uid,
-                                  pid, permission);
-  return Status::fromExceptionCode(Status::EX_SECURITY, String8(err.c_str()));
-}
-
-bool isCallerActiveUser() {
-  IPCThreadState* ipcState = IPCThreadState::selfOrNull();
-  if (!ipcState) return true;  // It's a local call
-
-  uid_t callingUid = ipcState->getCallingUid();
-  uid_t callingUser = callingUid / PER_USER_RANGE;
-  if (callingUid == getuid()) return true;  // It's a local call
-
-  return (foregroundUserId == callingUser) || (systemUiUid == callingUid) ||
-         (SYSTEM_UID == callingUid);
-}
-
-bool isCallerActiveUserOrManagedProfile() {
-  IPCThreadState* ipcState = IPCThreadState::selfOrNull();
-  if (!ipcState) return true;  // It's a local call
-
-  uid_t callingUid = ipcState->getCallingUid();
-  uid_t callingUser = callingUid / PER_USER_RANGE;
-  if (callingUid == getuid()) return true;  // It's a local call
-
-  if ((foregroundUserId == callingUser) || (systemUiUid == callingUid) ||
-      (SYSTEM_UID == callingUid))
-    return true;
-
-  uid_t parentUser = callingUser;
-
-  sp<IServiceManager> sm = defaultServiceManager();
-  sp<IBinder> binder = sm->getService(String16("user"));
-  sp<IUserManager> um = interface_cast<IUserManager>(binder);
-  if (um != NULL) {
-    // Must use Bluetooth process identity when making call to get parent user
-    int64_t ident = ipcState->clearCallingIdentity();
-    parentUser = um->getProfileParentId(callingUser);
-    ipcState->restoreCallingIdentity(ident);
-  }
-
-  return foregroundUserId == parentUser;
-}
-
-}  // namespace bluetooth
-}  // namespace android
diff --git a/jni/permission_helpers.h b/jni/permission_helpers.h
deleted file mode 100644
index 952281f..0000000
--- a/jni/permission_helpers.h
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Copyright 2017 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.
- */
-#pragma once
-
-#include <binder/Status.h>
-
-namespace android {
-namespace bluetooth {
-
-const char PERMISSION_BLUETOOTH[] = "android.permission.BLUETOOTH";
-const char PERMISSION_BLUETOOTH_ADMIN[] = "android.permission.BLUETOOTH_ADMIN";
-const char PERMISSION_BLUETOOTH_PRIVILEGED[] =
-    "android.permission.BLUETOOTH_PRIVILEGED";
-
-extern uid_t foregroundUserId;
-extern uid_t systemUiUid;
-
-android::binder::Status checkPermission(const char* permission);
-bool isCallerActiveUser();
-bool isCallerActiveUserOrManagedProfile();
-
-}  // namespace bluetooth
-}  // namespace android
-
-#define ENFORCE_PERMISSION(permission)                     \
-  {                                                        \
-    android::binder::Status status =                       \
-        android::bluetooth::checkPermission((permission)); \
-    if (!status.isOk()) {                                  \
-      return status;                                       \
-    }                                                      \
-  }
\ No newline at end of file
diff --git a/src/com/android/bluetooth/btservice/AdapterService.java b/src/com/android/bluetooth/btservice/AdapterService.java
index d68b5f1..e0452bf 100644
--- a/src/com/android/bluetooth/btservice/AdapterService.java
+++ b/src/com/android/bluetooth/btservice/AdapterService.java
@@ -458,7 +458,6 @@
                     "com.android.systemui", PackageManager.MATCH_SYSTEM_ONLY,
                     UserHandle.USER_SYSTEM);
             Utils.setSystemUiUid(systemUiUid);
-            setSystemUiUidNative(systemUiUid);
         } catch (PackageManager.NameNotFoundException e) {
             // Some platforms, such as wearables do not have a system ui.
             Log.w(TAG, "Unable to resolve SystemUI's UID.", e);
@@ -469,7 +468,6 @@
                 filter, null, null);
         int fuid = ActivityManager.getCurrentUser();
         Utils.setForegroundUserId(fuid);
-        setForegroundUserIdNative(fuid);
     }
 
     @Override
@@ -502,7 +500,6 @@
             if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
                 int fuid = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
                 Utils.setForegroundUserId(fuid);
-                setForegroundUserIdNative(fuid);
             }
         }
     };
@@ -2943,10 +2940,6 @@
 
     private native int readEnergyInfo();
 
-    private native void setSystemUiUidNative(int systemUiUid);
-
-    private static native void setForegroundUserIdNative(int foregroundUserId);
-
     /*package*/
     native boolean factoryResetNative();