Skip to content

Commit

Permalink
Ask for camera permissions for qr code capturing
Browse files Browse the repository at this point in the history
If android version allows it.
  • Loading branch information
eikek committed Aug 18, 2020
1 parent 140f438 commit 8b4412c
Showing 1 changed file with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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();
}
}
}
}

0 comments on commit 8b4412c

Please sign in to comment.