Created
October 20, 2022 21:04
-
-
Save ThomasGorisse/1a7a386693b418f6085bdc6f9561ff43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.sceneview.ar.utils | |
import android.Manifest | |
import android.content.Context | |
import android.content.Intent | |
import android.content.pm.PackageManager | |
import android.net.Uri | |
import android.provider.Settings | |
import android.widget.Toast | |
import androidx.activity.ComponentActivity | |
import androidx.activity.result.contract.ActivityResultContracts | |
import androidx.core.content.ContextCompat | |
import androidx.fragment.app.Fragment | |
import io.github.sceneview.ar.R | |
/** | |
* Helper to ask camera permission | |
*/ | |
object PermissionHelper { | |
/** | |
* Check to see if we have the necessary permissions for Camera | |
*/ | |
fun hasCameraPermission(context: Context) = ContextCompat.checkSelfPermission( | |
context, | |
Manifest.permission.CAMERA | |
) == PackageManager.PERMISSION_GRANTED | |
/** | |
* Request the Camera permission | |
* | |
* Must be called at the onCreate() time (before onResume()). | |
* Use your own permission check if you need to call it elsewhere. | |
* | |
* @param onDenied Callback on permission denied and !shouldShowRequestPermissionRationale. | |
* Return true to show the permission settings. | |
* @param onGranted Callback to start using the camera | |
*/ | |
fun requestCameraPermission( | |
activity: ComponentActivity, | |
onDenied: () -> Boolean = { | |
Toast.makeText( | |
activity, | |
R.string.sceneview_camera_permission_required, | |
Toast.LENGTH_LONG | |
).show() | |
true | |
}, onGranted: () -> Unit | |
) { | |
// Must be called before on resume | |
val activityResultLauncher = activity.registerForActivityResult( | |
ActivityResultContracts.StartActivityForResult() | |
) { | |
if (hasCameraPermission(activity)) { | |
onGranted() | |
} | |
} | |
// Must be called before on resume | |
val permissionResultLauncher = activity.registerForActivityResult( | |
ActivityResultContracts.RequestPermission() | |
) { isGranted: Boolean -> | |
if (isGranted) { | |
onGranted() | |
} else if (!activity.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) { | |
if (onDenied()) { | |
// Launch Application Setting to grant permission | |
activityResultLauncher.launch( | |
Intent().apply { | |
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS | |
data = Uri.fromParts("package", activity.packageName, null) | |
}) | |
} | |
} | |
} | |
permissionResultLauncher.launch(Manifest.permission.CAMERA) | |
} | |
/** | |
* Request the Camera permission | |
* | |
* Must be called at the onCreate() time (before onResume()). | |
* Use your own permission check if you need to call it elsewhere. | |
* | |
* @param onDenied Callback on permission denied and !shouldShowRequestPermissionRationale. | |
* Return true to show the permission settings. | |
* @param onGranted Callback to start using the camera | |
*/ | |
fun requestCameraPermission( | |
fragment: Fragment, | |
onDenied: () -> Boolean = { | |
Toast.makeText( | |
fragment.requireContext(), | |
R.string.sceneview_camera_permission_required, | |
Toast.LENGTH_LONG | |
).show() | |
true | |
}, | |
onGranted: () -> Unit | |
) { | |
// Must be called before on resume | |
val activityResultLauncher = fragment.registerForActivityResult( | |
ActivityResultContracts.StartActivityForResult() | |
) { | |
if (hasCameraPermission(fragment.requireContext())) { | |
onGranted() | |
} | |
} | |
// Must be called before on resume | |
val permissionResultLauncher = fragment.registerForActivityResult( | |
ActivityResultContracts.RequestPermission() | |
) { isGranted: Boolean -> | |
if (isGranted) { | |
onGranted() | |
} else if (!fragment.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) { | |
if (onDenied()) { | |
// Launch Application Setting to grant permission | |
activityResultLauncher.launch( | |
Intent().apply { | |
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS | |
data = Uri.fromParts( | |
"package", | |
fragment.requireContext().packageName, | |
null | |
) | |
}) | |
} | |
} | |
} | |
permissionResultLauncher.launch(Manifest.permission.CAMERA) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment