|
| 1 | +package org.joinmastodon.android; |
| 2 | + |
| 3 | +import android.Manifest; |
| 4 | +import android.app.Activity; |
| 5 | +import android.content.Intent; |
| 6 | +import android.content.pm.PackageManager; |
| 7 | +import android.net.Uri; |
| 8 | +import android.os.Build; |
| 9 | +import android.os.Bundle; |
| 10 | +import android.os.VibrationEffect; |
| 11 | +import android.os.Vibrator; |
| 12 | +import android.provider.Settings; |
| 13 | +import android.view.HapticFeedbackConstants; |
| 14 | +import android.view.View; |
| 15 | + |
| 16 | +import androidx.annotation.NonNull; |
| 17 | + |
| 18 | +import org.joinmastodon.android.ui.M3AlertDialogBuilder; |
| 19 | +import org.joinmastodon.android.ui.utils.UiUtils; |
| 20 | + |
| 21 | +import de.markusfisch.android.barcodescannerview.widget.BarcodeScannerView; |
| 22 | + |
| 23 | +public class QrCodeScanActivity extends Activity{ |
| 24 | + private static final int PERMISSION_RESULT=65537; |
| 25 | + private BarcodeScannerView scannerView; |
| 26 | + |
| 27 | + @Override |
| 28 | + public void onCreate(Bundle savedInstanceState){ |
| 29 | + super.onCreate(savedInstanceState); |
| 30 | + UiUtils.setUserPreferredTheme(this); |
| 31 | + setContentView(R.layout.activity_qr_scan); |
| 32 | + |
| 33 | + if(this.checkSelfPermission(Manifest.permission.CAMERA)!=PackageManager.PERMISSION_GRANTED){ |
| 34 | + requestPermissions(new String[]{Manifest.permission.CAMERA}, PERMISSION_RESULT); |
| 35 | + } |
| 36 | + |
| 37 | + findViewById(R.id.dismiss).setOnClickListener(view -> finish()); |
| 38 | + scannerView=findViewById(R.id.scanner); |
| 39 | + scannerView.setCropRatio(.75f); |
| 40 | + scannerView.setOnBarcodeListener(barcode -> { |
| 41 | + vibrate(scannerView); |
| 42 | + Intent result=new Intent(); |
| 43 | + result.putExtra("barcode", barcode.getText()); |
| 44 | + setResult(RESULT_OK, result); |
| 45 | + finish(); |
| 46 | + return false; |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){ |
| 52 | + if(requestCode==PERMISSION_RESULT){ |
| 53 | + if(grantResults[0]==PackageManager.PERMISSION_GRANTED){ |
| 54 | + scannerView.openAsync(); |
| 55 | + }else if(!this.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)){ |
| 56 | + new M3AlertDialogBuilder(this) |
| 57 | + .setTitle(R.string.permission_required) |
| 58 | + .setMessage(R.string.storage_permission_to_download) |
| 59 | + .setPositiveButton(R.string.open_settings, (dialog, which)->this.startActivity(new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", this.getPackageName(), null)))) |
| 60 | + .setNegativeButton(R.string.cancel, (dialogInterface, i) -> finish()) |
| 61 | + .setOnCancelListener(dialogInterface -> finish()) |
| 62 | + .show(); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + private static void vibrate(View v) { |
| 69 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { |
| 70 | + v.performHapticFeedback(HapticFeedbackConstants.CONFIRM); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + Vibrator vibrator=v.getContext().getSystemService(Vibrator.class); |
| 75 | + |
| 76 | + if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) { |
| 77 | + vibrator.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)); |
| 78 | + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 79 | + VibrationEffect effect=VibrationEffect.createOneShot(75L, 128); |
| 80 | + vibrator.vibrate(effect); |
| 81 | + } else { |
| 82 | + vibrator.vibrate(75L); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + @Override |
| 88 | + public void onResume() { |
| 89 | + super.onResume(); |
| 90 | + scannerView.openAsync(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void onPause() { |
| 95 | + super.onPause(); |
| 96 | + scannerView.close(); |
| 97 | + } |
| 98 | +} |
0 commit comments