Приложение “Фонарик”

MainActivity:

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
private Button bFlash;
private FlashClass flashClass;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init()
{
bFlash = findViewById(R.id.b1);
flashClass = new FlashClass(this);

}
public void onClickFlash(View view)
{
if(flashClass.isFlash_status())
{
flashClass.flashOff();
bFlash.setText("On");
bFlash.setBackgroundResource(R.drawable.circle_green);

}
else
{
flashClass.flashOn();
bFlash.setText("Off");
bFlash.setBackgroundResource(R.drawable.circle_red);
}
}

@Override
protected void onDestroy() {
super.onDestroy();
if(flashClass.isFlash_status())
{
flashClass.flashOff();
}
}
}

FlashClass:

import android.content.Context;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;

public class FlashClass {
private boolean flash_status = false;
private Context context;

public FlashClass(Context context) {
this.context = context;
}
public void flashOn()
{
CameraManager cameraManager = (CameraManager)context.getSystemService(Context.CAMERA_SERVICE);
try {
assert cameraManager != null;
String cameraId = cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode(cameraId, true);
flash_status = true;


} catch (CameraAccessException e) {
e.printStackTrace();
}

}
public void flashOff()
{
CameraManager cameraManager = (CameraManager)context.getSystemService(Context.CAMERA_SERVICE);
try {
assert cameraManager != null;
String cameraId = cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode(cameraId, false);
flash_status = false;


} catch (CameraAccessException e) {
e.printStackTrace();
}

}

public boolean isFlash_status() {
return flash_status;
}
}

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="colorRed">#FF0000</color>
<color name="colorGreen">#1CEB05</color>
<color name="colorBlack">#161616</color>
<color name="white">#FEFEFE</color>
</resources>

circle_green.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<stroke android:width="4dp" android:color="@color/colorGreen"/>
<solid android:color="@android:color/transparent"/>



</shape>

circle_red.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<stroke android:width="4dp" android:color="@color/colorRed"/>
<solid android:color="@android:color/transparent"/>



</shape>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBlack"
tools:context=".MainActivity">

<Button
android:id="@+id/b1"
android:layout_width="190dp"
android:layout_height="190dp"
android:background="@drawable/circle_green"
android:onClick="onClickFlash"
android:text="On"
android:textColor="@color/white"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.713" />
</androidx.constraintlayout.widget.ConstraintLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.neco_desarrollo.flashlight_test">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>