Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Keep camera-LED on after screen turns off

since i'm at the beginning with Android coding i hesitated to post my question, but now i'm at the point where i can't resist.

I have a service which turns on the camera-LED onCreate:

@Override
public void onCreate() {
// make sure we don't sleep
this.pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SleepLED");

    this.mTimer = new Timer();
    this.mTimerTask = new TimerTask() {
        public void run() {
        // turn on the LED
        setFlashlight(Camera.Parameters.FLASH_MODE_TORCH);

        mWakeLock.acquire();
        }
    };

    // Get the notification-service
    this.mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    // Display a notification about us starting.  We put an icon in the status bar.
    showNotification();

    // Open camera
    this.frontCam = Camera.open();
    this.frontCamPara = frontCam.getParameters();
    this.frontCam.lock();


    // Schedule the TimerTask
    this.mTimer.schedule(mTimerTask, 0);
}

I can tell that the WakeLock acquires, I tested with FULL_WAKE_LOCK and it didn't turn off considering Screen Time-Out. But since the Screen isn't need to be on I don't want to use the full wakelock.

The Cyanogenmod for my phone (HTC Legend) brings an torch-app which can do what i want. Its source code is here:
https://github.com/CyanogenMod/android_packages_apps_Torch/tree/gingerbread/src/net/cactii/flash2
I noticed that the light turns off for a short moment with that app, if this is a hint for someone, obviously not for me ;(

I don't expect someone to change my code to do what i want but I'd be thankful if anyone could point me in the right direction!

Greets
SJ

like image 860
stefanjunker Avatar asked Nov 08 '11 18:11

stefanjunker


2 Answers

I have found a solution to the problem. When the phone turns off the screen it does deactivate the camera LED, but it allows a user to reactivate it like this:

@Override
public void onCreate() {
    // assume we start with screen on and save that state ;-)
    this.pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    screenOn = this.pm.isScreenOn();

    // program a timer which checks if the light needs to be re-activated
    this.mTimer = new Timer();
    this.mTimerTask = new TimerTask() {
        public void run() {
            // re-activate the LED if screen turned off
            if(!pm.isScreenOn() && pm.isScreenOn() != screenOn) {
                Log.i("SleepLEDservice", "re-activated the LED");

                // really it's NOT ENOUGH to just "turn it on", i double-checked this
                setFlashlight(Camera.Parameters.FLASH_MODE_OFF);
                setFlashlight(Camera.Parameters.FLASH_MODE_TORCH);
            }
            screenOn = pm.isScreenOn();                 
        }
    };
}

private void setFlashlight(String newMode) {
    try {
        this.frontCamPara = this.frontCam.getParameters();
        if(this.frontCamPara.getFlashMode() != newMode) {
            this.frontCamPara.setFlashMode(newMode);
            this.frontCam.setParameters(frontCamPara);  
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

The key is changing the state back to FLASH_MODE_OFF and than back to FLASH_MODE_TORCH.

like image 81
stefanjunker Avatar answered Nov 12 '22 21:11

stefanjunker


I don't know if HTC is in this camp, but several devices (notably the Moto Droid) cannot use the camera when the screen is off - they helpfully assume that you would never want to use your phone in this manner. If the screen locks, the app using the camera is put into the background and stops running.

The result is that the security cam/webcam apps I've seen have a feature to dim/black the screen, but they don't actually allow it to lock. This ensures that the camera continues to operate.

I suspect that because the LED is considered part of the camera hardware, it is facing the same limitations.

like image 1
ProjectJourneyman Avatar answered Nov 12 '22 21:11

ProjectJourneyman