- // Get a handle for the the telephone Manager and the context.
- val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
- val context: Context = this
+ override fun onResume() {
+ // Run the default commands.
+ super.onResume()
+
+ // Check to see if the read phone state permission has been granted. These commands need to be run on every resume so that the listener gets reassigned as it is automatically paused.
+ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) { // The storage permission has been granted.
+ // Populate Privacy Cell.
+ populatePrivacyCell()
+ } else { // The phone permission has not been granted.
+ // Check if the user has previously denied the storage permission.
+ if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_PHONE_STATE)) { // Show a dialog explaining the request first.
+ // Check to see if a phone permission dialog is already displayed. This happens if the app is restarted when the dialog is shown.
+ if (supportFragmentManager.findFragmentByTag(getString(R.string.phone_permission)) == null) { // No dialog is currently shown.
+ // Instantiate the phone permission dialog fragment.
+ val phonePermissionDialogFragment: DialogFragment = PhonePermissionDialog()
+
+ // Show the phone permission alert dialog. The permission will be requested when the dialog is closed.
+ phonePermissionDialogFragment.show(supportFragmentManager, getString(R.string.phone_permission))
+ }
+ } else { // Show the permission request directly.
+ // Request the read phone state permission. There is only one permission request in the app, so it has a request code of 0.
+ ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_PHONE_STATE), 0)
+ }
+ }
+ }