Skip to content

Commit

Permalink
fix: handle no camera permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
nicola-95 authored May 4, 2022
1 parent 60e9c4a commit 50e7104
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import android.content.pm.PackageManager
import android.graphics.Typeface
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import android.text.Html
import android.text.SpannableString
import android.text.SpannableStringBuilder
Expand Down Expand Up @@ -78,9 +79,7 @@ class FirstActivity : AppCompatActivity(), View.OnClickListener,

private val requestPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
if (isGranted) {
openQrCodeReader()
}
if (isGranted) openQrCodeReader() else noPermissionsGrantedCamera()
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -406,7 +405,7 @@ class FirstActivity : AppCompatActivity(), View.OnClickListener,
startActivity(intent)
}

private fun openSettings() {
private fun openSettingsActivity() {
val intent = Intent(this, SettingsActivity::class.java)
startActivity(intent)
}
Expand Down Expand Up @@ -439,7 +438,7 @@ class FirstActivity : AppCompatActivity(), View.OnClickListener,
}
checkCameraPermission()
}
R.id.settings -> openSettings()
R.id.settings -> openSettingsActivity()
R.id.scan_mode_button -> {
viewModel.getRuleSet()?.run {
ScanModeDialogFragment(viewModel.getRuleSet()!!).show(supportFragmentManager, "SCAN_MODE_DIALOG_FRAGMENT")
Expand Down Expand Up @@ -522,6 +521,30 @@ class FirstActivity : AppCompatActivity(), View.OnClickListener,
}
}

private fun noPermissionsGrantedCamera() {
val builder = AlertDialog.Builder(this)
val dialog: AlertDialog?
builder.setTitle(
getString(R.string.no_permissions_granted_camera_title)
)
builder.setMessage(getString(R.string.no_permissions_granted_camera_message))
builder.setPositiveButton(getString(R.string.open_settings)) { _, _ -> openSettings() }
builder.setNegativeButton(getString(R.string.not_now)) { _, _ -> }
dialog = builder.create()
dialog.show()
}

private fun openSettings() {
try {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
val uri = Uri.fromParts("package", packageName, null)
intent.data = uri
startActivity(intent)
} catch (e: Exception) {
Log.i("openSettings", e.toString())
}
}

override fun onStart() {
super.onStart()
shared.registerOnSharedPreferenceChangeListener(this)
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,9 @@
<string name="userDataDoesNotMatch">Dati anagrafici non congruenti</string>
<string name="scan_mode_fragment_title">Modalità di verifica della <b>Certificazione</b></string>
<string name="confirm_scan_mode_button">Conferma</string>

<string name="no_permissions_granted_camera_title">Permessi fotocamera necessari</string>
<string name="no_permissions_granted_camera_message">VerificaC19 necessita di accedere alla fotocamera del telefono. Apri le impostazioni per autorizzare l\'app.</string>
<string name="not_now">Non ora</string>
<string name="open_settings">Apri impostazioni</string>
</resources>

0 comments on commit 50e7104

Please sign in to comment.