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

Commit 0c7cf0d6 authored by niks255's avatar niks255 Committed by Zhao Wei Liew
Browse files

Add wakelock option for flashlight

On some legacy devices, the flashlight can't stay on when screen is off.
With this change, the flashlight can use wakelock. For obvious reasons,
this is turned off by default and needs to be enabled using the overlay.

Change-Id: Ia69309cf023430c9e90634a722be97ebbf6677ca
parent 880aed95
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -286,4 +286,7 @@

    <bool name="config_show4gForIWlan">false</bool>
    <bool name="config_showSignalForIWlan">false</bool>

    <!-- Allow the flashlight service to use wakelocks -->
    <bool name="config_useWakelockForFlashlight">false</bool>
</resources>
+30 −0
Original line number Diff line number Diff line
@@ -30,6 +30,8 @@ import android.hardware.camera2.CameraManager;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Process;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.text.TextUtils;
import android.util.Log;

@@ -63,6 +65,9 @@ public class FlashlightController {
    /** Lock on mListeners when accessing */
    private final ArrayList<WeakReference<FlashlightListener>> mListeners = new ArrayList<>(1);

    private final boolean mUseWakeLock;
    private final WakeLock mWakeLock;

    /** Lock on {@code this} when accessing */
    private boolean mFlashlightEnabled;

@@ -91,6 +96,10 @@ public class FlashlightController {
        mContext = context;
        mCameraManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);

        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
        mUseWakeLock = mContext.getResources().getBoolean(R.bool.config_useWakelockForFlashlight);

        tryInitCamera();
    }

@@ -114,12 +123,25 @@ public class FlashlightController {
            if (mCameraId == null) return;
            if (mFlashlightEnabled != enabled) {
                mFlashlightEnabled = enabled;

                if (mUseWakeLock) {
                    if (enabled) {
                        if (!mWakeLock.isHeld()) mWakeLock.acquire();
                    } else {
                        if (mWakeLock.isHeld()) mWakeLock.release();
                    }
                }

                try {
                    mCameraManager.setTorchMode(mCameraId, enabled);
                } catch (CameraAccessException e) {
                    Log.e(TAG, "Couldn't set torch mode", e);
                    mFlashlightEnabled = false;
                    pendingError = true;

                    if (mUseWakeLock && mWakeLock.isHeld()) {
                        mWakeLock.release();
                    }
                }
            }
        }
@@ -299,6 +321,10 @@ public class FlashlightController {
            synchronized (FlashlightController.this) {
                changed = mTorchAvailable != available;
                mTorchAvailable = available;

                if (mUseWakeLock && !available && mWakeLock.isHeld()) {
                    mWakeLock.release();
                }
            }
            if (changed) {
                if (DEBUG) Log.d(TAG, "dispatchAvailabilityChanged(" + available + ")");
@@ -311,6 +337,10 @@ public class FlashlightController {
            synchronized (FlashlightController.this) {
                changed = mFlashlightEnabled != enabled;
                mFlashlightEnabled = enabled;

                if (mUseWakeLock && !enabled && mWakeLock.isHeld()) {
                    mWakeLock.release();
                }
            }
            if (changed) {
                if (DEBUG) Log.d(TAG, "dispatchModeChanged(" + enabled + ")");