OSDN Git Service

adapter non persistent service. Fixed null exception when profile services connect...
authorfredc <fredc@broadcom.com>
Sun, 15 Apr 2012 22:19:37 +0000 (15:19 -0700)
committerAndroid (Google) Code Review <android-gerrit@google.com>
Tue, 17 Jul 2012 04:59:30 +0000 (21:59 -0700)
Change-Id: Iefafd1115471256d3107f039897e2af16077cdae

src/com/android/bluetooth/a2dp/A2dpStateMachine.java
src/com/android/bluetooth/btservice/AdapterService.java
src/com/android/bluetooth/hdp/HealthService.java
src/com/android/bluetooth/hfp/HeadsetStateMachine.java
src/com/android/bluetooth/hid/HidService.java
src/com/android/bluetooth/pan/PanService.java

index c4dd1e0..73eb9bd 100755 (executable)
@@ -30,6 +30,7 @@ import android.os.ServiceManager;
 import android.os.ParcelUuid;
 import android.util.Log;
 import com.android.bluetooth.Utils;
+import com.android.bluetooth.btservice.AdapterService;
 import com.android.internal.util.IState;
 import com.android.internal.util.State;
 import com.android.internal.util.StateMachine;
@@ -52,7 +53,6 @@ final class A2dpStateMachine extends StateMachine {
 
     private Context mContext;
     private BluetoothAdapter mAdapter;
-    private IBluetooth mAdapterService;
     private static final ParcelUuid[] A2DP_UUIDS = {
         BluetoothUuid.AudioSink
     };
@@ -91,7 +91,6 @@ final class A2dpStateMachine extends StateMachine {
         super(TAG);
         mContext = context;
         mAdapter = BluetoothAdapter.getDefaultAdapter();
-        mAdapterService = IBluetooth.Stub.asInterface(ServiceManager.getService("bluetooth"));
 
         initNative();
 
@@ -563,11 +562,9 @@ final class A2dpStateMachine extends StateMachine {
         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
         mContext.sendBroadcast(intent, A2dpService.BLUETOOTH_PERM);
         if (DBG) log("Connection state " + device + ": " + prevState + "->" + newState);
-        try {
-            mAdapterService.sendConnectionStateChange(device, BluetoothProfile.A2DP, newState,
-                                                      prevState);
-        } catch (RemoteException e) {
-            Log.e(TAG, Log.getStackTraceString(new Throwable()));
+        AdapterService svc = AdapterService.getAdapterService();
+        if (svc != null) {
+            svc.onProfileConnectionStateChanged(device, BluetoothProfile.A2DP, newState, prevState);
         }
     }
 
index c5338c5..d0fb90c 100755 (executable)
@@ -14,12 +14,15 @@ import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothProfile;
 import android.bluetooth.IBluetooth;
+import android.bluetooth.IBluetoothManager;
+import android.bluetooth.IBluetoothManagerCallback;
 import android.content.BroadcastReceiver;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.os.Binder;
+import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
 import android.os.Message;
@@ -29,7 +32,6 @@ import android.os.RemoteException;
 import android.provider.Settings;
 import android.util.Log;
 import android.util.Pair;
-
 import com.android.bluetooth.a2dp.A2dpService;
 import com.android.bluetooth.hid.HidService;
 import com.android.bluetooth.hfp.HeadsetService;
@@ -37,7 +39,6 @@ import com.android.bluetooth.hdp.HealthService;
 import com.android.bluetooth.pan.PanService;
 import com.android.bluetooth.Utils;
 import com.android.bluetooth.btservice.RemoteDevices.DeviceProperties;
-
 import java.io.FileDescriptor;
 import java.io.IOException;
 import java.util.HashMap;
@@ -46,7 +47,7 @@ import java.util.Map;
 import java.util.Iterator;
 import java.util.Map.Entry;
 import android.content.pm.PackageManager;
-
+import android.os.ServiceManager;
 
 public class AdapterService extends Service {
     private static final String TAG = "BluetoothAdapterService";
@@ -73,6 +74,8 @@ public class AdapterService extends Service {
         android.Manifest.permission.BLUETOOTH_ADMIN;
     static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
 
+    private IBluetoothManager mBluetoothManager;
+    private IBluetooth mBluetoothService;
     private AdapterProperties mAdapterProperties;
     private int mAdapterState;
     private Context mContext;
@@ -105,7 +108,35 @@ public class AdapterService extends Service {
         return sAdapterService;
     }
 
+    public void onProfileConnectionStateChanged(BluetoothDevice device, int profileId, int newState, int prevState) {
+        Message m = mHandler.obtainMessage(MESSAGE_PROFILE_CONNECTION_STATE_CHANGED);
+        m.obj = device;
+        m.arg1 = profileId;
+        m.arg2 = newState;
+        Bundle b = new Bundle(1);
+        b.putInt("prevState", prevState);
+        mHandler.sendMessage(m);
+    }
+
+    private void processProfileStateChanged(BluetoothDevice device, int profileId, int newState, int prevState) {
+        if (mBluetoothService != null) {
+            try {
+                mBluetoothService.sendConnectionStateChange(device, profileId, newState,
+                prevState);
+            } catch (RemoteException re) {
+                Log.e(TAG, "",re);
+            }
+        }
+    }
+
     public void onProfileServiceStateChanged(String serviceName, int state) {
+        Message m = mHandler.obtainMessage(MESSAGE_PROFILE_SERVICE_STATE_CHANGED);
+        m.obj=serviceName;
+        m.arg1 = state;
+        mHandler.sendMessage(m);
+    }
+
+    private void processProfileServiceStateChanged(String serviceName, int state) {
         if (DBG) Log.d(TAG,"onProfileServiceStateChange: serviceName=" + serviceName + ", state = " + state);
         boolean doUpdate=false;
         synchronized (mProfileServicesState) {
@@ -133,8 +164,7 @@ public class AdapterService extends Service {
                 }
             }
             if (DBG) Log.d(TAG, "All profile services stopped...");
-            Message m = mHandler.obtainMessage(MESSAGE_ONSTOPPED_STOPPENDING);
-            mHandler.sendMessage(m);
+            processStopped();
         } else if (mStartPending) {
             //Process start pending
             //Check if all services are started if so, update state
@@ -150,11 +180,35 @@ public class AdapterService extends Service {
                 }
             }
             if (DBG) Log.d(TAG, "All profile services started.");
-            Message m = mHandler.obtainMessage(MESSAGE_ONSTARTED);
-            mHandler.sendMessage(m);
+            processStarted();
         }
     }
 
+    private void processStarted() {
+        Log.d(TAG, "processStarted()");
+        IntentFilter filter = new IntentFilter();
+        filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
+        registerForAirplaneMode(filter);
+        registerReceiver(mReceiver, filter);
+        mProfilesStarted = true;
+        mStartPending = false;
+    }
+
+    private void processStopped() {
+        Log.d(TAG, "processStopped");
+        mStopPending=false;
+        if (mBluetoothManager != null) {
+            try {
+                mBluetoothManager.unregisterAdapter(mManagerCallback);
+            } catch (RemoteException re) {
+                Log.e(TAG, "",re);
+            }
+        }
+        mBondStateMachine.quit();
+        mAdapterStateMachine.quit();
+        finish();
+    }
+
     @Override
     public void onCreate() {
         super.onCreate();
@@ -228,6 +282,23 @@ public class AdapterService extends Service {
         return START_STICKY;
     }
 
+    final private IBluetoothManagerCallback mManagerCallback =
+            new IBluetoothManagerCallback.Stub() {
+                public void onBluetoothServiceUp(IBluetooth bluetoothService) {
+                    if (DBG) Log.d(TAG, "onBluetoothServiceUp");
+                    synchronized (mManagerCallback) {
+                        mBluetoothService = bluetoothService;
+                    }
+                }
+
+                public void onBluetoothServiceDown() {
+                    if (DBG) Log.d(TAG, "onBluetoothServiceDown");
+                    synchronized (mManagerCallback) {
+                        mBluetoothService = null;
+                    }
+                }
+        };
+
     private void start() {
         if (DBG) Log.d(TAG,"start() called");
         if (mProfilesStarted || mStartPending) return;
@@ -235,6 +306,20 @@ public class AdapterService extends Service {
         mStartPending=true;
         mAdapterStateMachine.start();
         mBondStateMachine.start();
+
+        //Get bluetooth service for profiles
+        IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE);
+        if (b != null) {
+            mBluetoothManager = IBluetoothManager.Stub.asInterface(b);
+            if (mBluetoothManager != null) {
+                try {
+                    mBluetoothService = mBluetoothManager.registerAdapter(mManagerCallback);
+                } catch (RemoteException re) {
+                    Log.e(TAG, "",re);
+                }
+            }
+        }
+        
         //Start profile services
         for (int i=0; i <SUPPORTED_PROFILE_SERVICES.length;i++) {
             startProfileService(SUPPORTED_PROFILE_SERVICES[i]);
@@ -259,31 +344,25 @@ public class AdapterService extends Service {
         }
     }
 
-    private static final int MESSAGE_ONSTARTED=1;
-    private static final int MESSAGE_ONSTOPPED_STOPPENDING =2;
+    private static final int MESSAGE_PROFILE_SERVICE_STATE_CHANGED =1;
+    private static final int MESSAGE_STARTED=2;
+    private static final int MESSAGE_STOPPED=3;
+    private static final int MESSAGE_PROFILE_CONNECTION_STATE_CHANGED=4;
+
     private final Handler mHandler = new Handler() {
         @Override
         public void handleMessage(Message msg) {
             if (DBG) Log.d (TAG, "Message: " + msg.what);
 
             switch (msg.what) {
-                case MESSAGE_ONSTARTED: {
-                    Log.d(TAG, "onStarted()");
-                    IntentFilter filter = new IntentFilter();
-                    filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
-                    registerForAirplaneMode(filter);
-                    registerReceiver(mReceiver, filter);
-                    mProfilesStarted = true;
-                    mStartPending = false;
+                case MESSAGE_PROFILE_SERVICE_STATE_CHANGED: {
+                    Log.d(TAG, "MESSAGE_PROFILE_SERVICE_STATE_CHANGED");
+                    processProfileServiceStateChanged((String) msg.obj, msg.arg1);
                 }
                 break;
-
-                case MESSAGE_ONSTOPPED_STOPPENDING: {
-                    Log.d(TAG, "onStopped_StopPending() called");
-                    mStopPending=false;
-                    mBondStateMachine.quit();
-                    mAdapterStateMachine.quit();
-                    finish();
+                case MESSAGE_PROFILE_CONNECTION_STATE_CHANGED: {
+                    Log.d(TAG, "MESSAGE_PROFILE_CONNECTION_STATE_CHANGED");
+                    processProfileStateChanged((BluetoothDevice) msg.obj, msg.arg1,msg.arg2, msg.getData().getInt("prevState",BluetoothAdapter.ERROR));
                 }
                 break;
             }
index ee6f390..af6b0b6 100644 (file)
@@ -53,8 +53,6 @@ public class HealthService extends Service {
     private Map <BluetoothHealthAppConfiguration, AppInfo> mApps;
     private Map <BluetoothDevice, Integer> mHealthDevices;
     private HealthServiceMessageHandler mHandler;
-    private IBluetooth mAdapterService;
-
     private static final int MESSAGE_REGISTER_APPLICATION = 1;
     private static final int MESSAGE_UNREGISTER_APPLICATION = 2;
     private static final int MESSAGE_CONNECT_CHANNEL = 3;
@@ -121,7 +119,6 @@ public class HealthService extends Service {
         thread.start();
         Looper looper = thread.getLooper();
         mHandler = new HealthServiceMessageHandler(looper);
-        mAdapterService = IBluetooth.Stub.asInterface(ServiceManager.getService("bluetooth"));
         initializeNative();
 
         //Notify adapter service
@@ -591,11 +588,9 @@ public class HealthService extends Service {
         } else {
             mHealthDevices.put(device, newDeviceState);
         }
-        try {
-            mAdapterService.sendConnectionStateChange(device, BluetoothProfile.HEALTH,
-                                                      newDeviceState, prevDeviceState);
-        } catch (RemoteException e) {
-            Log.e(TAG, Log.getStackTraceString(new Throwable()));
+        AdapterService svc = AdapterService.getAdapterService();
+        if (svc != null) {
+            svc.onProfileConnectionStateChanged(device, BluetoothProfile.HEALTH, newDeviceState, prevDeviceState);
         }
     }
 
index f496ae2..6b768ad 100755 (executable)
@@ -44,6 +44,7 @@ import android.os.PowerManager.WakeLock;
 import android.telephony.PhoneNumberUtils;
 import android.util.Log;
 import com.android.bluetooth.Utils;
+import com.android.bluetooth.btservice.AdapterService;
 import com.android.internal.util.IState;
 import com.android.internal.util.State;
 import com.android.internal.util.StateMachine;
@@ -108,7 +109,6 @@ final class HeadsetStateMachine extends StateMachine {
     private HeadsetPhoneState mPhoneState;
     private int mAudioState;
     private BluetoothAdapter mAdapter;
-    private IBluetooth mAdapterService;
     private IBluetoothHeadsetPhone mPhoneProxy;
 
     // mCurrentDevice is the device connected before the state changes
@@ -159,7 +159,6 @@ final class HeadsetStateMachine extends StateMachine {
         mPhoneState = new HeadsetPhoneState(context, this);
         mAudioState = BluetoothHeadset.STATE_AUDIO_DISCONNECTED;
         mAdapter = BluetoothAdapter.getDefaultAdapter();
-        mAdapterService = IBluetooth.Stub.asInterface(ServiceManager.getService("bluetooth"));
         if (!context.bindService(new Intent(IBluetoothHeadsetPhone.class.getName()),
                                  mConnection, 0)) {
             Log.e(TAG, "Could not bind to Bluetooth Headset Phone Service");
@@ -1112,11 +1111,9 @@ final class HeadsetStateMachine extends StateMachine {
         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
         mContext.sendBroadcast(intent, HeadsetService.BLUETOOTH_PERM);
         if (DBG) log("Connection state " + device + ": " + prevState + "->" + newState);
-        try {
-            mAdapterService.sendConnectionStateChange(device, BluetoothProfile.HEADSET, newState,
-                                                      prevState);
-        } catch (RemoteException e) {
-            Log.e(TAG, Log.getStackTraceString(new Throwable()));
+        AdapterService svc = AdapterService.getAdapterService();
+        if (svc != null) {
+            svc.onProfileConnectionStateChanged(device, BluetoothProfile.HEADSET, newState, prevState);
         }
     }
 
index 6243627..8d09044 100755 (executable)
@@ -43,7 +43,6 @@ public class HidService extends Service {
     static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
 
     private BluetoothAdapter mAdapter;
-    private IBluetooth mAdapterService;
     private Map<BluetoothDevice, Integer> mInputDevices;
 
     private static final int MESSAGE_CONNECT = 1;
@@ -108,7 +107,6 @@ public class HidService extends Service {
     }
 
     private void start() {
-        mAdapterService = IBluetooth.Stub.asInterface(ServiceManager.getService("bluetooth"));
         mInputDevices = Collections.synchronizedMap(new HashMap<BluetoothDevice, Integer>());
         initializeNative();
 
@@ -458,11 +456,9 @@ public class HidService extends Service {
         intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
         sendBroadcast(intent, BLUETOOTH_PERM);
         if (DBG) log("Connection state " + device + ": " + prevState + "->" + newState);
-        try {
-            mAdapterService.sendConnectionStateChange(device, BluetoothProfile.INPUT_DEVICE, newState,
-                                                      prevState);
-        } catch (RemoteException e) {
-            Log.e(TAG, Log.getStackTraceString(new Throwable()));
+        AdapterService svc = AdapterService.getAdapterService();
+        if (svc != null) {
+            svc.onProfileConnectionStateChanged(device, BluetoothProfile.INPUT_DEVICE, newState, prevState);
         }
     }
 
index 3f79df4..be130fd 100644 (file)
@@ -56,7 +56,6 @@ public class PanService extends Service {
     private static final int BLUETOOTH_PREFIX_LENGTH        = 24;
 
     private BluetoothAdapter mAdapter;
-    private IBluetooth mAdapterService;
     private HashMap<BluetoothDevice, BluetoothPanDevice> mPanDevices;
     private ArrayList<String> mBluetoothIfaceAddresses;
     private int mMaxPanDevices;
@@ -119,9 +118,7 @@ public class PanService extends Service {
 
     private void start() {
         if (DBG) log("start");
-
         mPanDevices = new HashMap<BluetoothDevice, BluetoothPanDevice>();
-        mAdapterService = IBluetooth.Stub.asInterface(ServiceManager.getService("bluetooth"));
         mBluetoothIfaceAddresses = new ArrayList<String>();
         try {
             mMaxPanDevices = getResources().getInteger(
@@ -405,11 +402,9 @@ public class PanService extends Service {
 
         if (DBG) Log.d(TAG, "Pan Device state : device: " + device + " State:" +
                        prevState + "->" + state);
-        try {
-            mAdapterService.sendConnectionStateChange(device, BluetoothProfile.PAN, state,
-                                                      prevState);
-        } catch (RemoteException e) {
-            Log.e(TAG, Log.getStackTraceString(new Throwable()));
+        AdapterService svc = AdapterService.getAdapterService();
+        if (svc != null) {
+            svc.onProfileConnectionStateChanged(device, BluetoothProfile.PAN, state, prevState);
         }
     }