Yuvaraj Vodiboina
All work
(16)Case studyAndroid, in Java

IdentiFlash

My first Android app: a flashlight, in Java. One toggle turns the torch on and off, and it turns itself off when you leave.

Platform
Android, in Java
When
2021
Role
Sole developer
Status
Earlier work, on GitHub

Press the toggle. No hardware is used: it’s a picture of what the app does.

(01)

The problem

Put one thing the phone can do behind the most direct control possible, while learning how Android activities and the camera APIs work.

(02)

What I built

MainActivity gets the CameraManager, picks the first camera, and calls setTorchMode whenever the toggle changes. A toast confirms each change, and finish() switches the torch off on the way out. Camera access is wrapped in CameraAccessException handling, and the torch calls are marked for Android 6.0 and later.

Hard partMaking sure the torch never stays on after the app has gone.

(03)

Under the hood

  1. 01ToggleOn or off
  2. 02CameraManagerThe first camera
  3. 03setTorchModeThe torch itself
  4. 04finish()Off, on the way out
MainActivity.java
public void toggleFlashLight(View view) {
    if (toggleFlashLightOnOff.isChecked()) {
        try {
            cameraManager.setTorchMode(getCameraID, true);
            Toast.makeText(MainActivity.this, "Flashlight is turned ON", Toast.LENGTH_SHORT).show();
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    } else {
        try {
            cameraManager.setTorchMode(getCameraID, false);
            Toast.makeText(MainActivity.this, "Flashlight is turned OFF", Toast.LENGTH_SHORT).show();
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
}
(04)

Decisions

  1. 01

    The camera service’s torch mode, not the camera

    setTorchMode switches the flash without opening the camera at all.

    Trade-offIt needs Android 6.0 or later.

(05)

What I learned

  • It takes the first camera and assumes it has a flash. Real code should check each camera for one.
(06)

Built with

JavaAndroidGradle

Next01 of 16Chrome extension + website ShopGrade
An audit for any Shopify storefront. It turns what it finds on a public store into a prioritized list of fixes, with no admin access needed.Read the case study