/*
========================================================================================
Filename: 32_FlashLightApp.txt
Program 32: Create an app to turn on and off the flashlight.
Files Included: MainActivity.java
========================================================================================

WHAT ALL TO DO / SETUP INSTRUCTIONS:
----------------------------------------------------------------------------------------
1. UI Components to Add (in res/layout/activity_main.xml):
   Add 2 TextView:
     - Title: 'Flashlight'
     - statusText: 'Flashlight is OFF' (clickable with toggle or icon)

2. Layout Configuration:
   - LinearLayout (vertical, gravity='center', padding 20dp)

3. Menu Configuration:
   - None required.

4. Themes Configuration (res/values/themes.xml):
   - Theme.MaterialComponents.DayNight.NoActionBar in themes.xml.

5. AndroidManifest.xml:
   Add Camera permission and feature above <application>:
   <uses-permission android:name="android.permission.CAMERA" />
   <uses-feature android:name="android.hardware.camera.flash" />

6. Additional Files & Resources Required:
   - Uses CameraManager.setTorchMode(cameraId, state).
========================================================================================
*/

// ======================================================================================
// File: MainActivity.java
// ======================================================================================
package com.example.flashlightapp;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.ToggleButton;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
    ToggleButton toggleButton;
    TextView statusText;
    CameraManager cameraManager;
    String cameraId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toggleButton = findViewById(R.id.toggleButton);
        statusText = findViewById(R.id.statusText);
        cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
        try {
            cameraId = cameraManager.getCameraIdList()[0];
        } catch (Exception e) {
            e.printStackTrace();
        }
        toggleButton.setOnCheckedChangeListener((button, isChecked) -> {
            try {
                cameraManager.setTorchMode(cameraId, isChecked);
                if (isChecked) {
                    statusText.setText("Flashlight is ON");
                } else {
                    statusText.setText("Flashlight is OFF");
                }
            } catch (Exception e) {
                statusText.setText("Flashlight is OFF");
            }
        });
    }
}
