Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 30143f7e authored by Hemant Gupta's avatar Hemant Gupta Committed by Linux Build Service Account
Browse files

Bluetooth: Pbap: Acquire wakelocks only when required

This patch updates the wakelock acquiring logic in PBAP Server only when
OBEX transaction is being done by PBAP Client. Without this patch it was
observed that wakelock was acquired during obex server connection
establishement and was only released when connection was disconnected
which was resulting in extra power consumption when PBAP connection was
idle.

Change-Id: I3189036649f09c6df944d575eabb5b03863ee7d7
CRs-Fixed: 565528
(cherry picked from commit c586750631298660bcbcb4587ddc1233648527da)
(cherry picked from commit 366ab6df5821bf4a9593ff1cdbe353d487a8609f)
parent 47bd93a8
Loading
Loading
Loading
Loading
+65 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ package com.android.bluetooth.pbap;
import android.content.Context;
import android.os.Message;
import android.os.Handler;
import android.os.PowerManager;
import android.text.TextUtils;
import android.util.Log;
import android.provider.CallLog.Calls;
@@ -154,6 +155,8 @@ public class BluetoothPbapObexServer extends ServerRequestHandler {

    private static int CALLLOG_NUM_LIMIT = 50;

    private PowerManager.WakeLock mWakeLock = null;

    public static int ORDER_BY_INDEXED = 0;

    public static int ORDER_BY_ALPHABETICAL = 1;
@@ -187,6 +190,15 @@ public class BluetoothPbapObexServer extends ServerRequestHandler {

    @Override
    public int onConnect(final HeaderSet request, HeaderSet reply) {
        if (V) Log.v(TAG, "onConnect");
        acquirePbapWakeLock();
        int retVal = onConnectInternal(request, reply);
        if (V) Log.v(TAG, "exiting from onConnect");
        releasePbapWakeLock();
        return retVal;
    }

    private int onConnectInternal(final HeaderSet request, HeaderSet reply) {
        if (V) logHeader(request);
        try {
            byte[] uuid = (byte[])request.getHeader(HeaderSet.TARGET);
@@ -235,6 +247,7 @@ public class BluetoothPbapObexServer extends ServerRequestHandler {
    @Override
    public void onDisconnect(final HeaderSet req, final HeaderSet resp) {
        if (D) Log.d(TAG, "onDisconnect(): enter");
        acquirePbapWakeLock();
        if (V) logHeader(req);

        resp.responseCode = ResponseCodes.OBEX_HTTP_OK;
@@ -244,24 +257,39 @@ public class BluetoothPbapObexServer extends ServerRequestHandler {
            msg.sendToTarget();
            if (V) Log.v(TAG, "onDisconnect(): msg MSG_SESSION_DISCONNECTED sent out.");
        }
        releasePbapWakeLock();
    }

    @Override
    public int onAbort(HeaderSet request, HeaderSet reply) {
        if (D) Log.d(TAG, "onAbort(): enter.");
        acquirePbapWakeLock();
        sIsAborted = true;
        releasePbapWakeLock();
        return ResponseCodes.OBEX_HTTP_OK;
    }

    @Override
    public int onPut(final Operation op) {
        acquirePbapWakeLock();
        if (D) Log.d(TAG, "onPut(): not support PUT request.");
        releasePbapWakeLock();
        return ResponseCodes.OBEX_HTTP_BAD_REQUEST;
    }

    @Override
    public int onSetPath(final HeaderSet request, final HeaderSet reply, final boolean backup,
            final boolean create) {
        if (V) Log.v(TAG, "onSetPath");
        acquirePbapWakeLock();
        int retVal = onSetPathInternal(request, reply, backup, create);
        if (V) Log.v(TAG, "exiting from onSetPath");
        releasePbapWakeLock();
        return retVal;
    }

    private int onSetPathInternal(final HeaderSet request, final HeaderSet reply, final boolean backup,
            final boolean create) {
        if (V) logHeader(request);
        if (D) Log.d(TAG, "before setPath, mCurrentPath ==  " + mCurrentPath);

@@ -305,16 +333,27 @@ public class BluetoothPbapObexServer extends ServerRequestHandler {

    @Override
    public void onClose() {
        acquirePbapWakeLock();
        if (mCallback != null) {
            Message msg = Message.obtain(mCallback);
            msg.what = BluetoothPbapService.MSG_SERVERSESSION_CLOSE;
            msg.sendToTarget();
            if (D) Log.d(TAG, "onClose(): msg MSG_SERVERSESSION_CLOSE sent out.");
        }
        releasePbapWakeLock();
    }

    @Override
    public int onGet(Operation op) {
        if (V) Log.v(TAG, "onGet");
        acquirePbapWakeLock();
        int retVal = onGetInternal(op);
        if (V) Log.v(TAG, "exiting from onGet");
        releasePbapWakeLock();
        return retVal;
    }

    private int onGetInternal(Operation op) {
        sIsAborted = false;
        HeaderSet request = null;
        HeaderSet reply = new HeaderSet();
@@ -1119,4 +1158,30 @@ public class BluetoothPbapObexServer extends ServerRequestHandler {
            Log.e(TAG, "dump HeaderSet error " + e);
        }
    }

    private void acquirePbapWakeLock() {
        if (mWakeLock == null) {
            PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "StartingObexPbapTransaction");
            mWakeLock.setReferenceCounted(false);
            mWakeLock.acquire();
            if (V) Log.v(TAG, "Pbap: mWakeLock acquired");
        }
        else
        {
            Log.e(TAG, "Pbap:mWakeLock already acquired");
        }
    }

    private void releasePbapWakeLock() {
        if (mWakeLock != null) {
            if (mWakeLock.isHeld()) {
                mWakeLock.release();
                if (V) Log.v(TAG, "Pbap: mWakeLock released");
            } else {
                if (V) Log.v(TAG, "Pbap: mWakeLock already released");
            }
            mWakeLock = null;
        }
    }
}
+0 −22
Original line number Diff line number Diff line
@@ -51,7 +51,6 @@ import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.PowerManager;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.telephony.TelephonyManager;
@@ -141,8 +140,6 @@ public class BluetoothPbapService extends Service {

    private static final int NOTIFICATION_ID_AUTH = -1000002;

    private PowerManager.WakeLock mWakeLock = null;

    private BluetoothAdapter mAdapter;

    private SocketAcceptThread mAcceptThread = null;
@@ -442,11 +439,6 @@ public class BluetoothPbapService extends Service {
            }
        }

        if (mWakeLock != null) {
            mWakeLock.release();
            mWakeLock = null;
        }

        if (mServerSession != null) {
            mServerSession.close();
            mServerSession = null;
@@ -465,14 +457,6 @@ public class BluetoothPbapService extends Service {
    private final void startObexServerSession() throws IOException {
        if (VERBOSE) Log.v(TAG, "Pbap Service startObexServerSession");

        // acquire the wakeLock before start Obex transaction thread
        if (mWakeLock == null) {
            PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    "StartingObexPbapTransaction");
            mWakeLock.setReferenceCounted(false);
            mWakeLock.acquire();
        }
        TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        if (tm != null) {
            sLocalPhoneNum = tm.getLine1Number();
@@ -499,12 +483,6 @@ public class BluetoothPbapService extends Service {
    private void stopObexServerSession() {
        if (VERBOSE) Log.v(TAG, "Pbap Service stopObexServerSession");

        // Release the wake lock if obex transaction is over
        if (mWakeLock != null) {
            mWakeLock.release();
            mWakeLock = null;
        }

        if (mServerSession != null) {
            mServerSession.close();
            mServerSession = null;