diff options
| author | Benjamin Franz <bfranz@google.com> | 2014-11-12 15:57:54 +0000 |
|---|---|---|
| committer | Benjamin Franz <bfranz@google.com> | 2014-12-04 16:57:25 +0000 |
| commit | e8b98925d08f720c4d22b626d0650de536840a9a (patch) | |
| tree | eb47d9695a5190339edaabc0efa32b75a3ca60f7 /core/java/android/bluetooth/BluetoothHeadset.java | |
| parent | e47dc36071884600c2b3c1dfe1ffe031fc9f0fde (diff) | |
Change the routing path of bluetooth headset connections.
The HeadsetService is now bound directly by the BluetoothManagerService.
The IBinder object related to the HeadsetService is then given back to
the BluetoothHeadset and to the client app. This change makes the
HeadsetService available for managed profile clients.
Bug: 16968338
Change-Id: I016d1837e4f987c0fab1fc2c64cb06eb91b24d87
Diffstat (limited to 'core/java/android/bluetooth/BluetoothHeadset.java')
| -rw-r--r-- | core/java/android/bluetooth/BluetoothHeadset.java | 99 |
1 files changed, 61 insertions, 38 deletions
diff --git a/core/java/android/bluetooth/BluetoothHeadset.java b/core/java/android/bluetooth/BluetoothHeadset.java index 353f0fba05d3..546a50e2ef4a 100644 --- a/core/java/android/bluetooth/BluetoothHeadset.java +++ b/core/java/android/bluetooth/BluetoothHeadset.java @@ -20,9 +20,10 @@ import android.annotation.SdkConstant; import android.annotation.SdkConstant.SdkConstantType; import android.content.ComponentName; import android.content.Context; -import android.content.Intent; -import android.content.ServiceConnection; +import android.os.Handler; import android.os.IBinder; +import android.os.Looper; +import android.os.Message; import android.os.RemoteException; import android.util.Log; @@ -221,11 +222,14 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public static final int STATE_AUDIO_CONNECTED = 12; + private static final int MESSAGE_HEADSET_SERVICE_CONNECTED = 100; + private static final int MESSAGE_HEADSET_SERVICE_DISCONNECTED = 101; private Context mContext; private ServiceListener mServiceListener; private IBluetoothHeadset mService; private BluetoothAdapter mAdapter; + private boolean mIsClosed; final private IBluetoothStateChangeCallback mBluetoothStateChangeCallback = new IBluetoothStateChangeCallback.Stub() { @@ -233,14 +237,7 @@ public final class BluetoothHeadset implements BluetoothProfile { if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up); if (!up) { if (VDBG) Log.d(TAG,"Unbinding service..."); - synchronized (mConnection) { - try { - mService = null; - mContext.unbindService(mConnection); - } catch (Exception re) { - Log.e(TAG,"",re); - } - } + doUnbind(); } else { synchronized (mConnection) { try { @@ -263,6 +260,7 @@ public final class BluetoothHeadset implements BluetoothProfile { mContext = context; mServiceListener = l; mAdapter = BluetoothAdapter.getDefaultAdapter(); + mIsClosed = false; IBluetoothManager mgr = mAdapter.getBluetoothManager(); if (mgr != null) { @@ -277,15 +275,26 @@ public final class BluetoothHeadset implements BluetoothProfile { } boolean doBind() { - Intent intent = new Intent(IBluetoothHeadset.class.getName()); - ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); - intent.setComponent(comp); - if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0, - android.os.Process.myUserHandle())) { - Log.e(TAG, "Could not bind to Bluetooth Headset Service with " + intent); - return false; + try { + return mAdapter.getBluetoothManager().bindBluetoothProfileService( + BluetoothProfile.HEADSET, mConnection); + } catch (RemoteException e) { + Log.e(TAG, "Unable to bind HeadsetService", e); + } + return false; + } + + void doUnbind() { + synchronized (mConnection) { + if (mService != null) { + try { + mAdapter.getBluetoothManager().unbindBluetoothProfileService( + BluetoothProfile.HEADSET, mConnection); + } catch (RemoteException e) { + Log.e(TAG,"Unable to unbind HeadsetService", e); + } + } } - return true; } /** @@ -305,18 +314,8 @@ public final class BluetoothHeadset implements BluetoothProfile { Log.e(TAG,"",e); } } - - synchronized (mConnection) { - if (mService != null) { - try { - mService = null; - mContext.unbindService(mConnection); - } catch (Exception re) { - Log.e(TAG,"",re); - } - } - } - mServiceListener = null; + mIsClosed = true; + doUnbind(); } /** @@ -930,21 +929,21 @@ public final class BluetoothHeadset implements BluetoothProfile { return false; } - private final ServiceConnection mConnection = new ServiceConnection() { + private final IBluetoothProfileServiceConnection mConnection + = new IBluetoothProfileServiceConnection.Stub() { + @Override public void onServiceConnected(ComponentName className, IBinder service) { if (DBG) Log.d(TAG, "Proxy object connected"); mService = IBluetoothHeadset.Stub.asInterface(service); - - if (mServiceListener != null) { - mServiceListener.onServiceConnected(BluetoothProfile.HEADSET, BluetoothHeadset.this); - } + mHandler.sendMessage(mHandler.obtainMessage( + MESSAGE_HEADSET_SERVICE_CONNECTED)); } + @Override public void onServiceDisconnected(ComponentName className) { if (DBG) Log.d(TAG, "Proxy object disconnected"); mService = null; - if (mServiceListener != null) { - mServiceListener.onServiceDisconnected(BluetoothProfile.HEADSET); - } + mHandler.sendMessage(mHandler.obtainMessage( + MESSAGE_HEADSET_SERVICE_DISCONNECTED)); } }; @@ -968,4 +967,28 @@ public final class BluetoothHeadset implements BluetoothProfile { private static void log(String msg) { Log.d(TAG, msg); } + + private final Handler mHandler = new Handler(Looper.getMainLooper()) { + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case MESSAGE_HEADSET_SERVICE_CONNECTED: { + if (mServiceListener != null) { + mServiceListener.onServiceConnected(BluetoothProfile.HEADSET, + BluetoothHeadset.this); + } + break; + } + case MESSAGE_HEADSET_SERVICE_DISCONNECTED: { + if (mServiceListener != null) { + mServiceListener.onServiceDisconnected(BluetoothProfile.HEADSET); + } + if (mIsClosed){ + mServiceListener = null; + } + break; + } + } + } + }; } |
