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

Commit b87fde07 authored by Sukanya Rajkhowa's avatar Sukanya Rajkhowa Committed by Linux Build Service Account
Browse files

Release wakelock for incoming SMS

- In the current implementation, a wakelock is
acquired when SMS is received in IdleState. The
wakelock is released only 3 seconds after returning
back to IdleState. As the statemachine will be back
in IdleState only after SMS processing is complete,
there is no need to hold the wakelock for another
3 seconds

- Add a change to hold wakelock for 3 seconds as a
worst case timeout rather than for normal processing
of SMS. Also maintain a wakelock counter to make sure
that wakelock is released when statemachine is idle

Change-Id: I826603d7e5d8ea5ced3d331ddb17618be0390ffe
CRs-Fixed: 875160
parent 0a4ebc3c
Loading
Loading
Loading
Loading
+59 −9
Original line number Diff line number Diff line
@@ -138,7 +138,7 @@ public abstract class InboundSmsHandler extends StateMachine {
    /** Sent on exit from {@link WaitingState} to return to idle after sending all broadcasts. */
    static final int EVENT_RETURN_TO_IDLE = 4;

    /** Release wakelock after a short timeout when returning to idle state. */
    /** Release wakelock on entering IdleState. */
    static final int EVENT_RELEASE_WAKELOCK = 5;

    /** Sent by {@link SmsBroadcastUndelivered} after cleaning the raw table. */
@@ -150,6 +150,9 @@ public abstract class InboundSmsHandler extends StateMachine {
    /** New SMS received as an AsyncResult. */
    public static final int EVENT_INJECT_SMS = 8;

    /** Release wakelock after a short timeout.  */
    static final int EVENT_WAKE_LOCK_TIMEOUT = 9;

    /** Wakelock release delay when returning to idle state. */
    private static final int WAKELOCK_TIMEOUT = 3000;

@@ -165,6 +168,9 @@ public abstract class InboundSmsHandler extends StateMachine {
    /** Wake lock to ensure device stays awake while dispatching the SMS intents. */
    final PowerManager.WakeLock mWakeLock;

    /** Wakelock count to ensure that wakelock is released only when count is 0 */
    int mWakeLockCount = 0;

    /** DefaultState throws an exception or logs an error for unhandled message types. */
    final DefaultState mDefaultState = new DefaultState();

@@ -217,7 +223,8 @@ public abstract class InboundSmsHandler extends StateMachine {

        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name);
        mWakeLock.acquire();    // wake lock released after we enter idle state
        mWakeLock.setReferenceCounted(false);
        acquireWakeLock();    // wake lock released after we enter idle state
        mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
        mDeviceIdleController = IDeviceIdleController.Stub.asInterface(
                ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER));
@@ -263,6 +270,41 @@ public abstract class InboundSmsHandler extends StateMachine {
        return mPhone;
    }

    /**
     * Acquire PowerManager wakelock
     */
    void acquireWakeLock() {
        synchronized (mWakeLock) {
            mWakeLock.acquire();
            mWakeLockCount++;
            removeMessages(EVENT_WAKE_LOCK_TIMEOUT);
            sendMessageDelayed(EVENT_WAKE_LOCK_TIMEOUT, WAKELOCK_TIMEOUT);
        }
    }

    /**
     * Decrement wakelock
     */
    void decrementWakeLock() {
        synchronized (mWakeLock) {
            mWakeLockCount--;
            if (mWakeLockCount == 0) {
                releaseWakeLock();
            }
        }
    }

    /**
     * Release wakelock when wakelock count is 0 or when timeout occurs.
     */
    void releaseWakeLock() {
        synchronized (mWakeLock) {
            mWakeLockCount = 0;
            mWakeLock.release();
            removeMessages(EVENT_WAKE_LOCK_TIMEOUT);
        }
    }

    /**
     * This parent state throws an exception (for debug builds) or prints an error for unhandled
     * message types.
@@ -275,6 +317,12 @@ public abstract class InboundSmsHandler extends StateMachine {
                    onUpdatePhoneObject((PhoneBase) msg.obj);
                    break;
                }

                case EVENT_WAKE_LOCK_TIMEOUT:
                    loge("Release wakelock as it timed out");
                    releaseWakeLock();
                    break;

                default: {
                    String errorText = "processMessage: unhandled message type " + msg.what +
                        " currState=" + getCurrentState().getName();
@@ -334,12 +382,12 @@ public abstract class InboundSmsHandler extends StateMachine {
        @Override
        public void enter() {
            if (DBG) log("entering Idle state");
            sendMessageDelayed(EVENT_RELEASE_WAKELOCK, WAKELOCK_TIMEOUT);
            sendMessage(EVENT_RELEASE_WAKELOCK);
        }

        @Override
        public void exit() {
            mWakeLock.acquire();
            acquireWakeLock();
            if (DBG) log("acquired wakelock, leaving Idle state");
        }

@@ -356,13 +404,14 @@ public abstract class InboundSmsHandler extends StateMachine {
                    return HANDLED;

                case EVENT_RELEASE_WAKELOCK:
                    mWakeLock.release();
                    decrementWakeLock();
                    if (DBG) {
                        if (mWakeLock.isHeld()) {
                            // this is okay as long as we call release() for every acquire()
                            log("mWakeLock is still held after release");
                            log("mWakeLock is still held after release wakelockcount = "
                                    + mWakeLockCount);
                        } else {
                            log("mWakeLock released");
                            log("mWakeLock released wakelockcount = " + mWakeLockCount);
                        }
                    }
                    return HANDLED;
@@ -435,10 +484,11 @@ public abstract class InboundSmsHandler extends StateMachine {
                    return HANDLED;

                case EVENT_RELEASE_WAKELOCK:
                    mWakeLock.release();    // decrement wakelock from previous entry to Idle
                    decrementWakeLock(); // decrement wakelock from previous entry to Idle
                    if (!mWakeLock.isHeld()) {
                        // wakelock should still be held until 3 seconds after we enter Idle
                        loge("mWakeLock released while delivering/broadcasting!");
                        loge("mWakeLock released while delivering/broadcasting! wakelockcount = "
                                + mWakeLockCount);
                    }
                    return HANDLED;