Android Activity Result API with Hilt(Dependency Injection)

interface PermissionListener {
fun permissionGranted()
fun permissionDenied()
}
abstract class AbstractPermissionObserver : LifecycleObserver {

var granted: Boolean = false
var permissionListener: PermissionListener? = null

abstract fun launch()

abstract fun checkPermission(): Boolean
}
@ActivityScoped
class CameraPermissionObserver @Inject constructor(
@ActivityContext private val appCompatContext: Context
) : AbstractPermissionObserver() {

private var requestPermission: ActivityResultLauncher<Array<String>>? = null

@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreate() {
if (appCompatContext is AppCompatActivity) {
requestPermission = appCompatContext.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
permissions.entries.forEach {
granted = it.value
if (!it.value) {
return@forEach
}
}
if (granted) {
permissionListener?.permissionGranted()
} else {
permissionListener?.permissionDenied()
}
}
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
requestPermission = null
}
override fun launch() {
requestPermission?.launch(arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE))
}

override fun checkPermission(): Boolean {
val cameraPermission = ContextCompat.checkSelfPermission(appCompatContext, Manifest.permission.CAMERA)
val writePermission = ContextCompat.checkSelfPermission(appCompatContext, Manifest.permission.WRITE_EXTERNAL_STORAGE)
val readPermission = ContextCompat.checkSelfPermission(appCompatContext, Manifest.permission.READ_EXTERNAL_STORAGE)
return cameraPermission == PackageManager.PERMISSION_GRANTED &&
writePermission == PackageManager.PERMISSION_GRANTED &&
readPermission == PackageManager.PERMISSION_GRANTED
}
}
override fun launch() {
requestPermission?.launch(arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE))
}
override fun checkPermission(): Boolean {
val cameraPermission = ContextCompat.checkSelfPermission(appCompatContext, Manifest.permission.CAMERA)
val writePermission = ContextCompat.checkSelfPermission(appCompatContext, Manifest.permission.WRITE_EXTERNAL_STORAGE)
val readPermission = ContextCompat.checkSelfPermission(appCompatContext, Manifest.permission.READ_EXTERNAL_STORAGE)
return cameraPermission == PackageManager.PERMISSION_GRANTED &&
writePermission == PackageManager.PERMISSION_GRANTED &&
readPermission == PackageManager.PERMISSION_GRANTED
}
open class BaseActivity : AppCompatActivity() {

@Inject
lateinit var cameraPermissionObserver: CameraPermissionObserver

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setTransparent()

lifecycle.addObserver(cameraPermissionObserver)
}
override fun onDestroy() {
super.onDestroy()
lifecycle.removeObserver(cameraPermissionObserver)
}
}
@AndroidEntryPoint
class CameraPermissionSampleFragment : Fragment(R.layout.fragment_camera_permission_sample), PermissionListener {

@Inject
lateinit var cameraPermissionObserver: CameraPermissionObserver

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
cameraPermissionObserver.permissionListener = this

btn_camera_permission_request.setOnClickListener {
requestPermission()
}
}

override fun onDestroyView() {
super.onDestroyView()
cameraPermissionObserver.permissionListener = null
}

override fun permissionGranted() {
Toast.makeText(requireActivity(), "Permission Granted", Toast.LENGTH_SHORT).show()
}

override fun permissionDenied() {
Toast.makeText(requireActivity(), "Permission Denied", Toast.LENGTH_SHORT).show()
}

private fun requestPermission() {
if (cameraPermissionObserver.checkPermission()){
// Go
} else {
cameraPermissionObserver.launch()
}
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store