OSDN Git Service

Device cannot go to sleep while connected to HBM-570 BT headset
authorZhihai Xu <zhihaixu@google.com>
Mon, 14 Oct 2013 19:16:39 +0000 (12:16 -0700)
committerVineeta Srivastava <vsrivastava@google.com>
Thu, 6 Mar 2014 17:35:56 +0000 (09:35 -0800)
The headset never disconnect the Obex connection for PBAP,
so in this case, we never release the wakelock.
Our PBAP implementation won't release wakelock
until the Obex is disconnected by the remote device
To better control the wake lock, we should
only acquire the wakelock when we receive the reqeust from the remote device
will release the wakelock after we send the response packets.

bug:10957699
Change-Id: I85e0609e36788d9c36334dd804ec52c90b90615b

src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java
src/com/android/bluetooth/pbap/BluetoothPbapService.java

index f5c48ed..9fc8f1e 100755 (executable)
@@ -181,6 +181,7 @@ public class BluetoothPbapObexServer extends ServerRequestHandler {
     @Override
     public int onConnect(final HeaderSet request, HeaderSet reply) {
         if (V) logHeader(request);
+        notifyUpdateWakeLock();
         try {
             byte[] uuid = (byte[])request.getHeader(HeaderSet.TARGET);
             if (uuid == null) {
@@ -229,7 +230,7 @@ public class BluetoothPbapObexServer extends ServerRequestHandler {
     public void onDisconnect(final HeaderSet req, final HeaderSet resp) {
         if (D) Log.d(TAG, "onDisconnect(): enter");
         if (V) logHeader(req);
-
+        notifyUpdateWakeLock();
         resp.responseCode = ResponseCodes.OBEX_HTTP_OK;
         if (mCallback != null) {
             Message msg = Message.obtain(mCallback);
@@ -242,6 +243,7 @@ public class BluetoothPbapObexServer extends ServerRequestHandler {
     @Override
     public int onAbort(HeaderSet request, HeaderSet reply) {
         if (D) Log.d(TAG, "onAbort(): enter.");
+        notifyUpdateWakeLock();
         sIsAborted = true;
         return ResponseCodes.OBEX_HTTP_OK;
     }
@@ -249,6 +251,7 @@ public class BluetoothPbapObexServer extends ServerRequestHandler {
     @Override
     public int onPut(final Operation op) {
         if (D) Log.d(TAG, "onPut(): not support PUT request.");
+        notifyUpdateWakeLock();
         return ResponseCodes.OBEX_HTTP_BAD_REQUEST;
     }
 
@@ -257,7 +260,7 @@ public class BluetoothPbapObexServer extends ServerRequestHandler {
             final boolean create) {
         if (V) logHeader(request);
         if (D) Log.d(TAG, "before setPath, mCurrentPath ==  " + mCurrentPath);
-
+        notifyUpdateWakeLock();
         String current_path_tmp = mCurrentPath;
         String tmp_path = null;
         try {
@@ -308,6 +311,7 @@ public class BluetoothPbapObexServer extends ServerRequestHandler {
 
     @Override
     public int onGet(Operation op) {
+        notifyUpdateWakeLock();
         sIsAborted = false;
         HeaderSet request = null;
         HeaderSet reply = new HeaderSet();
@@ -1021,6 +1025,12 @@ public class BluetoothPbapObexServer extends ServerRequestHandler {
         result.append("\"/>");
     }
 
+    private void notifyUpdateWakeLock() {
+        Message msg = Message.obtain(mCallback);
+        msg.what = BluetoothPbapService.MSG_ACQUIRE_WAKE_LOCK;
+        msg.sendToTarget();
+    }
+
     public static final void logHeader(HeaderSet hs) {
         Log.v(TAG, "Dumping HeaderSet " + hs.toString());
         try {
index 2f577d5..865b2f1 100755 (executable)
@@ -121,6 +121,10 @@ public class BluetoothPbapService extends Service {
 
     public static final int MSG_OBEX_AUTH_CHALL = 5003;
 
+    public static final int MSG_ACQUIRE_WAKE_LOCK = 5004;
+
+    public static final int MSG_RELEASE_WAKE_LOCK = 5005;
+
     private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
 
     private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN;
@@ -134,6 +138,7 @@ public class BluetoothPbapService extends Service {
 
     private static final int USER_CONFIRM_TIMEOUT_VALUE = 30000;
 
+    private static final int RELEASE_WAKE_LOCK_DELAY = 10000;
 
     // Ensure not conflict with Opp notification ID
     private static final int NOTIFICATION_ID_ACCESS = -1000001;
@@ -470,6 +475,11 @@ public class BluetoothPbapService extends Service {
         BluetoothPbapRfcommTransport transport = new BluetoothPbapRfcommTransport(mConnSocket);
         mServerSession = new ServerSession(transport, mPbapServer, mAuth);
         setState(BluetoothPbap.STATE_CONNECTED);
+
+        mSessionStatusHandler.removeMessages(MSG_RELEASE_WAKE_LOCK);
+        mSessionStatusHandler.sendMessageDelayed(mSessionStatusHandler
+            .obtainMessage(MSG_RELEASE_WAKE_LOCK), RELEASE_WAKE_LOCK_DELAY);
+
         if (VERBOSE) {
             Log.v(TAG, "startObexServerSession() success!");
         }
@@ -478,6 +488,8 @@ public class BluetoothPbapService extends Service {
     private void stopObexServerSession() {
         if (VERBOSE) Log.v(TAG, "Pbap Service stopObexServerSession");
 
+        mSessionStatusHandler.removeMessages(MSG_ACQUIRE_WAKE_LOCK);
+        mSessionStatusHandler.removeMessages(MSG_RELEASE_WAKE_LOCK);
         // Release the wake lock if obex transaction is over
         if (mWakeLock != null) {
             mWakeLock.release();
@@ -660,6 +672,27 @@ public class BluetoothPbapService extends Service {
                     mSessionStatusHandler.sendMessageDelayed(mSessionStatusHandler
                             .obtainMessage(AUTH_TIMEOUT), USER_CONFIRM_TIMEOUT_VALUE);
                     break;
+                case MSG_ACQUIRE_WAKE_LOCK:
+                    if (mWakeLock == null) {
+                        PowerManager pm = (PowerManager)getSystemService(
+                                          Context.POWER_SERVICE);
+                        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
+                                    "StartingObexPbapTransaction");
+                        mWakeLock.setReferenceCounted(false);
+                        mWakeLock.acquire();
+                        Log.w(TAG, "Acquire Wake Lock");
+                    }
+                    mSessionStatusHandler.removeMessages(MSG_RELEASE_WAKE_LOCK);
+                    mSessionStatusHandler.sendMessageDelayed(mSessionStatusHandler
+                      .obtainMessage(MSG_RELEASE_WAKE_LOCK), RELEASE_WAKE_LOCK_DELAY);
+                    break;
+                case MSG_RELEASE_WAKE_LOCK:
+                    if (mWakeLock != null) {
+                        mWakeLock.release();
+                        mWakeLock = null;
+                        Log.w(TAG, "Release Wake Lock");
+                    }
+                    break;
                 default:
                     break;
             }