Monday, May 26, 2014

Making a Flashlight

    I'm pretty sure you can't call yourself an Android developer without making at least one flashlight app, right? Let's just go ahead and get that out of the way then. The source code is available on GitHub.

    The first thing that needs to be done is to declare permissions in the manifest

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

    I also modify the activity to handle configuration changes so that the activity doesn't destroy itself on rotate. This prevents the flashlight from turning off and then back on when the device orientation is changed.

<activity
    android:name="com.ptrprograms.flashlight.MainActivity"
    android:label="@string/app_name"
    android:configChanges="orientation|keyboardHidden|screenSize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

    The layout for this demo is a simple toggle button located in the center of the screen. That button is initialized in MainActivity, and the click listener verifies that the device has the camera flash feature.

private void initFlashlightButton() {
    ToggleButton button = (ToggleButton) findViewById( R.id.button_flashlight );
    button.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if( getPackageManager().hasSystemFeature( PackageManager.FEATURE_CAMERA_FLASH ) )
            {
                if( mFlashlightOn )
                    activateFlashlight();
                else
                    deactivateFlashlight();
            }
        }
    });
}

    The activateFlashlight method gets the parameter information from the device camera and adds a parameter to turn on the flash in torch mode, meaning the flash stays on until deactivated, rather than 'flashing.'

private void activateFlashlight()
{
    if( mCamera == null )
        mCamera = Camera.open();

    mParameters = mCamera.getParameters();
    mParameters.setFlashMode( Camera.Parameters.FLASH_MODE_TORCH );
    mCamera.setParameters( mParameters );
    mFlashlightOn = true;
}

    The deactivateFlashlight method does the same thing, but in reverse. Instead of setting the flash mode to FLASH_MODE_TORCH, it uses Camera.Parameters.FLASH_MODE_OFF.

private void deactivateFlashlight()
{
    if( mCamera == null || mParameters == null )
        return;

    mParameters = mCamera.getParameters();
    mParameters.setFlashMode( Camera.Parameters.FLASH_MODE_OFF );
    mCamera.setParameters( mParameters );
    mFlashlightOn = false;
}

    The final part to building a simple flashlight app is releasing the camera resource on destroy. This turns the light off when the app is exited.

@Override
protected void onDestroy() {
    super.onDestroy();
    if( mCamera == null )
        return;

    mCamera.release();
}

    And that's that, a simple flashlight app to add to the toolbox.