Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request camera pemission #2

Open
wants to merge 1 commit into
base: step_01_empty_project
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nekdenis.camera">

<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
42 changes: 42 additions & 0 deletions app/src/main/java/com/nekdenis/camera/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,59 @@
package com.nekdenis.camera

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.nekdenis.camera.ui.theme.CameraComposeWorkshopTheme

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (permissionGranted()) {
initView()
} else {
requestPermission()
}

}

private fun permissionGranted() =
ContextCompat.checkSelfPermission(
this,
Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED

private fun requestPermission() {
ActivityCompat.requestPermissions(
this, arrayOf(Manifest.permission.CAMERA), 0
)
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String?>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == 0) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initView()
} else {
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show()
}
}
}

private fun initView() {
setContent {
CameraComposeWorkshopTheme {
// A surface container using the 'background' color from the theme
Expand All @@ -24,6 +65,7 @@ class MainActivity : ComponentActivity() {
}
}


@Composable
fun Greeting(name: String) {
Text(text = "Hello $name!")
Expand Down