Last active
July 28, 2020 14:23
-
-
Save vipulshah2010/fbaede661cb588bef98fd0b5b2889b83 to your computer and use it in GitHub Desktop.
Permission handling using new ActivityResultContract API introduced in 1.2.0-alpha02 and Fragment 1.3.0-alpha02
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
class PermissionDemoFragment : BaseFragment<PermissionFragmentBinding>(),PermissionCallback { | |
private lateinit var permissionObserver: PermissionObserver | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
permissionObserver = | |
PermissionObserver( | |
requireActivity(), | |
PermissionUtils.phoneStateParmission, this | |
) | |
// These observers will auto unregistered on activity onDestroy. | |
lifecycle.addObserver(permissionObserver) | |
} | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
permissionObserver.requestPermission() // Trigger permission request | |
} | |
override fun getBinding(inflater: LayoutInflater,container: ViewGroup?,bundle: Bundle?) | |
= PermissionFragmentBinding.inflate(inflater, container, false) | |
override fun onPermissionGranted() { | |
// perform action to be taken after permission approval. | |
} | |
override fun onPermissionDenied() { | |
// User denied permission,Display meanigful message with retry button. | |
} | |
override fun onPermissionPermanentlyDenied() { | |
// User permanently denied permission, display more detailed message with settings button. | |
} | |
} |
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
/** | |
* @{Lifecycle aware PermissionHandler, it will automatically unregister when the Activity is destroyed } | |
*/ | |
class PermissionObserver( | |
private val activity: FragmentActivity, | |
private val permission: String, | |
private val callback: PermissionCallback | |
) : DefaultLifecycleObserver { | |
private lateinit var requestPermissionLauncher: ActivityResultLauncher<String> | |
override fun onCreate(owner: LifecycleOwner) { | |
requestPermissionLauncher = | |
activity.activityResultRegistry.register(permission, | |
owner, | |
ActivityResultContracts.RequestPermission(), | |
ActivityResultCallback<Boolean> { isGranted: Boolean -> | |
if (isGranted) { | |
callback.onPermissionGranted() | |
} else { | |
if (!shouldShowRequestPermissionRationale(activity, permission)) { | |
callback.onPermissionPermanentlyDenied() | |
} else { | |
callback.onPermissionDenied() | |
} | |
} | |
}) | |
} | |
fun requestPermission() { | |
requestPermissionLauncher.launch(permission) | |
} | |
} | |
interface PermissionCallback { | |
fun onPermissionGranted() | |
fun onPermissionDenied() | |
fun onPermissionPermanentlyDenied() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment