From 8b4412c5b51bf92b1f2ab91d0adddfad6ecf6790 Mon Sep 17 00:00:00 2001 From: Eike Kettner Date: Tue, 18 Aug 2020 22:45:11 +0200 Subject: [PATCH] Ask for camera permissions for qr code capturing If android version allows it. --- .../activity/AddUrlActivity.java | 45 +++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/docspell/docspellshare/activity/AddUrlActivity.java b/app/src/main/java/org/docspell/docspellshare/activity/AddUrlActivity.java index 0a0f325..e25de18 100644 --- a/app/src/main/java/org/docspell/docspellshare/activity/AddUrlActivity.java +++ b/app/src/main/java/org/docspell/docspellshare/activity/AddUrlActivity.java @@ -1,21 +1,24 @@ package org.docspell.docspellshare.activity; +import android.Manifest; import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.Build; import android.os.Bundle; import android.view.View; import android.widget.EditText; - +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; - +import androidx.core.content.ContextCompat; import com.google.android.material.snackbar.Snackbar; - import org.docspell.docspellshare.R; import org.docspell.docspellshare.data.UrlItem; import org.docspell.docspellshare.util.Strings; public class AddUrlActivity extends AppCompatActivity { private static final int GET_QR_CODE = 1; + private static final int CAMERA_PERM_REQUEST_CODE = 2; public static final String URL_ITEM_EXTRA = "extraUrlItem"; @@ -63,8 +66,42 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten } } - public void runQrCode(View view) { + private void showCameraRequiredMessage() { + View view = findViewById(R.id.addUrlLayout); + Snackbar.make(view, "Camera permission is required to capture QR codes.", Snackbar.LENGTH_LONG) + .setAction("Action", null) + .show(); + } + + private void captureQRCodeActivity() { Intent intent = new Intent(this, QrCodeActivity.class); startActivityForResult(intent, GET_QR_CODE); } + + @Override + public void onRequestPermissionsResult( + int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + + if (requestCode == CAMERA_PERM_REQUEST_CODE) { + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + captureQRCodeActivity(); + } else { + showCameraRequiredMessage(); + } + } + } + + public void runQrCode(View view) { + if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) + == PackageManager.PERMISSION_GRANTED) { + captureQRCodeActivity(); + } else { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + requestPermissions(new String[] {Manifest.permission.CAMERA}, CAMERA_PERM_REQUEST_CODE); + } else { + showCameraRequiredMessage(); + } + } + } }