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

Commit 2aaa1ce1 authored by Jean-Baptiste Queru's avatar Jean-Baptiste Queru Committed by Android Code Review
Browse files

Merge "Hold partial wakelock during shutdown to avoid entering sleep"

parents 0e430ccc cd4e4279
Loading
Loading
Loading
Loading
+24 −7
Original line number Original line Diff line number Diff line
@@ -67,7 +67,8 @@ public final class ShutdownThread extends Thread {
    private boolean mActionDone;
    private boolean mActionDone;
    private Context mContext;
    private Context mContext;
    private PowerManager mPowerManager;
    private PowerManager mPowerManager;
    private PowerManager.WakeLock mWakeLock;
    private PowerManager.WakeLock mCpuWakeLock;
    private PowerManager.WakeLock mScreenWakeLock;
    private Handler mHandler;
    private Handler mHandler;
    
    
    private ShutdownThread() {
    private ShutdownThread() {
@@ -155,20 +156,36 @@ public final class ShutdownThread extends Thread {


        pd.show();
        pd.show();


        // start the thread that initiates shutdown
        sInstance.mContext = context;
        sInstance.mContext = context;
        sInstance.mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
        sInstance.mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
        sInstance.mWakeLock = null;

        // make sure we never fall asleep again
        sInstance.mCpuWakeLock = null;
        try {
            sInstance.mCpuWakeLock = sInstance.mPowerManager.newWakeLock(
                    PowerManager.PARTIAL_WAKE_LOCK, TAG + "-cpu");
            sInstance.mCpuWakeLock.setReferenceCounted(false);
            sInstance.mCpuWakeLock.acquire();
        } catch (SecurityException e) {
            Log.w(TAG, "No permission to acquire wake lock", e);
            sInstance.mCpuWakeLock = null;
        }

        // also make sure the screen stays on for better user experience
        sInstance.mScreenWakeLock = null;
        if (sInstance.mPowerManager.isScreenOn()) {
        if (sInstance.mPowerManager.isScreenOn()) {
            try {
            try {
                sInstance.mWakeLock = sInstance.mPowerManager.newWakeLock(
                sInstance.mScreenWakeLock = sInstance.mPowerManager.newWakeLock(
                        PowerManager.FULL_WAKE_LOCK, "Shutdown");
                        PowerManager.FULL_WAKE_LOCK, TAG + "-screen");
                sInstance.mWakeLock.acquire();
                sInstance.mScreenWakeLock.setReferenceCounted(false);
                sInstance.mScreenWakeLock.acquire();
            } catch (SecurityException e) {
            } catch (SecurityException e) {
                Log.w(TAG, "No permission to acquire wake lock", e);
                Log.w(TAG, "No permission to acquire wake lock", e);
                sInstance.mWakeLock = null;
                sInstance.mScreenWakeLock = null;
            }
            }
        }
        }

        // start the thread that initiates shutdown
        sInstance.mHandler = new Handler() {
        sInstance.mHandler = new Handler() {
        };
        };
        sInstance.start();
        sInstance.start();