aboutsummaryrefslogtreecommitdiff
path: root/framework/java/android/bluetooth/BluetoothLeCallControl.java
diff options
context:
space:
mode:
authorEtienne Ruffieux <eruffieux@google.com>2022-11-07 14:40:27 -0800
committerEtienne Ruffieux <eruffieux@google.com>2022-11-29 13:50:09 -0800
commit312688bba41f74b9e3362a5bbbf419a00bda8d16 (patch)
tree1177d82e7a646ef1aec7f86ee9d55f3e1891f8cc /framework/java/android/bluetooth/BluetoothLeCallControl.java
parent16f28028d5c66685d3a8cbd395dfd1a1737bb8f0 (diff)
Move all profile services bind to BluetoothManagerService
We are currently binding the profile services in framework/ BluetoothProfileConnector, except for BluetoothHeadset and BluetoothLeCallControl that are bound in BMS. The issue with binding the profiles in framework/ is that calling apps don't always have the required permission (BLUETOOTH_PRIVILEGED) to check if the service is enabled or not (BluetoothAdapter#getSupportedProfiles), so we are trying to bind the service even if the profile is disabled. This change will unify the profiles service binding by moving the bind process in BluetoothProfileConnector in BluetoothManagerService and making BluetoothHeadset and BluetoothLeCallControl use BluetoothProfileConnector. Bug: 241827236 Bug: 236399693 Test: atest CtsBluetoothTestCases Test: atest BluetoothInstrumentationTests Tag: #feature Ignore-AOSP-First: Cherry-pick Change-Id: I925b4415c023ff9e43a8ff947ae7ca5456babe64 Merged-In: I925b4415c023ff9e43a8ff947ae7ca5456babe64
Diffstat (limited to 'framework/java/android/bluetooth/BluetoothLeCallControl.java')
-rw-r--r--framework/java/android/bluetooth/BluetoothLeCallControl.java142
1 files changed, 12 insertions, 130 deletions
diff --git a/framework/java/android/bluetooth/BluetoothLeCallControl.java b/framework/java/android/bluetooth/BluetoothLeCallControl.java
index 5ad9e5d9bd..e3d9378dbb 100644
--- a/framework/java/android/bluetooth/BluetoothLeCallControl.java
+++ b/framework/java/android/bluetooth/BluetoothLeCallControl.java
@@ -17,33 +17,23 @@
package android.bluetooth;
-import android.Manifest;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
-import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
-import android.content.ComponentName;
+import android.annotation.SuppressLint;
import android.content.AttributionSource;
import android.content.Context;
import android.os.Binder;
-import android.os.Handler;
import android.os.IBinder;
-import android.os.Looper;
-import android.os.Message;
import android.os.ParcelUuid;
import android.os.RemoteException;
import android.util.Log;
-import android.annotation.SuppressLint;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
import java.util.List;
-import java.util.Map;
-import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Executor;
@@ -199,9 +189,6 @@ public final class BluetoothLeCallControl implements BluetoothProfile {
*/
public static final int CAPABILITY_JOIN_CALLS = 0x00000002;
- private static final int MESSAGE_TBS_SERVICE_CONNECTED = 102;
- private static final int MESSAGE_TBS_SERVICE_DISCONNECTED = 103;
-
private static final int REG_TIMEOUT = 10000;
/**
@@ -387,83 +374,29 @@ public final class BluetoothLeCallControl implements BluetoothProfile {
}
};
- private Context mContext;
- private ServiceListener mServiceListener;
- private volatile IBluetoothLeCallControl mService;
private BluetoothAdapter mAdapter;
private final AttributionSource mAttributionSource;
private int mCcid = 0;
private String mToken;
private Callback mCallback = null;
-
- private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
- new IBluetoothStateChangeCallback.Stub() {
- public void onBluetoothStateChange(boolean up) {
- if (DBG)
- Log.d(TAG, "onBluetoothStateChange: up=" + up);
- if (!up) {
- doUnbind();
- } else {
- doBind();
- }
- }
+ private final BluetoothProfileConnector<IBluetoothLeCallControl> mProfileConnector =
+ new BluetoothProfileConnector(this, BluetoothProfile.LE_CALL_CONTROL,
+ "BluetoothLeCallControl", IBluetoothLeCallControl.class.getName()) {
+ @Override
+ public IBluetoothLeCallControl getServiceInterface(IBinder service) {
+ return IBluetoothLeCallControl.Stub.asInterface(service);
+ }
};
+
/**
* Create a BluetoothLeCallControl proxy object for interacting with the local Bluetooth
* telephone bearer service.
*/
/* package */ BluetoothLeCallControl(Context context, ServiceListener listener) {
- mContext = context;
mAdapter = BluetoothAdapter.getDefaultAdapter();
mAttributionSource = mAdapter.getAttributionSource();
- mServiceListener = listener;
-
- IBluetoothManager mgr = mAdapter.getBluetoothManager();
- if (mgr != null) {
- try {
- mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
- } catch (RemoteException e) {
- Log.e(TAG, "", e);
- }
- }
-
- doBind();
- }
-
- private boolean doBind() {
- synchronized (mConnection) {
- if (mService == null) {
- if (VDBG)
- Log.d(TAG, "Binding service...");
- try {
- return mAdapter.getBluetoothManager().
- bindBluetoothProfileService(BluetoothProfile.LE_CALL_CONTROL,
- mConnection);
- } catch (RemoteException e) {
- Log.e(TAG, "Unable to bind TelephoneBearerService", e);
- }
- }
- }
- return false;
- }
-
- private void doUnbind() {
- synchronized (mConnection) {
- if (mService != null) {
- if (VDBG)
- Log.d(TAG, "Unbinding service...");
- try {
- mAdapter.getBluetoothManager().
- unbindBluetoothProfileService(BluetoothProfile.LE_CALL_CONTROL,
- mConnection);
- } catch (RemoteException e) {
- Log.e(TAG, "Unable to unbind TelephoneBearerService", e);
- } finally {
- mService = null;
- }
- }
- }
+ mProfileConnector.connect(context, listener);
}
/* package */ void close() {
@@ -471,20 +404,11 @@ public final class BluetoothLeCallControl implements BluetoothProfile {
log("close()");
unregisterBearer();
- IBluetoothManager mgr = mAdapter.getBluetoothManager();
- if (mgr != null) {
- try {
- mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback);
- } catch (RemoteException re) {
- Log.e(TAG, "", re);
- }
- }
- mServiceListener = null;
- doUnbind();
+ mProfileConnector.disconnect();
}
private IBluetoothLeCallControl getService() {
- return mService;
+ return mProfileConnector.getService();
}
/**
@@ -863,46 +787,4 @@ public final class BluetoothLeCallControl implements BluetoothProfile {
private static void log(String msg) {
Log.d(TAG, msg);
}
-
- private final IBluetoothProfileServiceConnection mConnection =
- new IBluetoothProfileServiceConnection.Stub() {
- @Override
- public void onServiceConnected(ComponentName className, IBinder service) {
- if (DBG) {
- Log.d(TAG, "Proxy object connected");
- }
- mService = IBluetoothLeCallControl.Stub.asInterface(service);
- mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_TBS_SERVICE_CONNECTED));
- }
-
- @Override
- public void onServiceDisconnected(ComponentName className) {
- if (DBG) {
- Log.d(TAG, "Proxy object disconnected");
- }
- doUnbind();
- mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_TBS_SERVICE_DISCONNECTED));
- }
- };
-
- private final Handler mHandler = new Handler(Looper.getMainLooper()) {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case MESSAGE_TBS_SERVICE_CONNECTED: {
- if (mServiceListener != null) {
- mServiceListener.onServiceConnected(BluetoothProfile.LE_CALL_CONTROL,
- BluetoothLeCallControl.this);
- }
- break;
- }
- case MESSAGE_TBS_SERVICE_DISCONNECTED: {
- if (mServiceListener != null) {
- mServiceListener.onServiceDisconnected(BluetoothProfile.LE_CALL_CONTROL);
- }
- break;
- }
- }
- }
- };
}