Skip to content

Commit

Permalink
[Permission] Exit the app when user don't grant enough permission
Browse files Browse the repository at this point in the history
 - Exit the app when user don't grant enough permission
 - Remove deprecated permission

Signed-off-by: Gichan Jang <[email protected]>
  • Loading branch information
gichan-jang authored and wooksong committed Jan 23, 2025
1 parent 3ba7c9c commit 9d6b851
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
1 change: 0 additions & 1 deletion ml_inference_offloading/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:name=".App"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ai.nnstreamer.ml.inference.offloading.ui.components.ButtonList
import ai.nnstreamer.ml.inference.offloading.ui.components.ServiceList
import ai.nnstreamer.ml.inference.offloading.ui.theme.NnstreamerandroidTheme
import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.ComponentName
import android.content.Context
import android.content.Intent
Expand All @@ -20,6 +21,7 @@ import android.os.Message
import android.os.Messenger
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.camera.view.CameraController
import androidx.camera.view.LifecycleCameraController
import androidx.camera.view.PreviewView
Expand Down Expand Up @@ -69,7 +71,6 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
Expand Down Expand Up @@ -118,6 +119,22 @@ class MainActivity : ComponentActivity() {
}
}

private val requestPermissionLauncher =
registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
val allPermissionsGranted = !permissions.containsValue(false)

if (!allPermissionsGranted) {
AlertDialog.Builder(this)
.setTitle("Need all permission.")
.setMessage("If you do not grant the necessary permissions, the app will not be able to to run. You need to allow the app to access the required permissions.")
.setPositiveButton("Exit") { _, _ ->
finishAffinity()
}
.show()
}
}

/**
* A lifecycle callback method that overrides [ComponentActivity.onCreate].
*
Expand All @@ -134,10 +151,10 @@ class MainActivity : ComponentActivity() {

val info: PackageInfo =
packageManager.getPackageInfo(applicationContext.packageName, PackageManager.GET_PERMISSIONS)
val permissions = info.requestedPermissions
if (permissions != null) {
if (!permissions.all { ContextCompat.checkSelfPermission(this, it) == PackageManager.PERMISSION_GRANTED }) {
ActivityCompat.requestPermissions(this, permissions, 0)

info.requestedPermissions?.let { permissions ->
if (permissions.any {ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED}) {
requestPermissionLauncher.launch(permissions)
}
}

Expand Down

0 comments on commit 9d6b851

Please sign in to comment.