-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fallback to zxing-based lib to scan QrCodes
Uses a zxing-based library as a fallback when scanning a Qr-Code and GMS is not available. Closes #825
- Loading branch information
1 parent
72df722
commit 4db8512
Showing
8 changed files
with
161 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
mastodon/src/main/java/org/joinmastodon/android/QrCodeScanActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package org.joinmastodon.android; | ||
|
||
import android.Manifest; | ||
import android.app.Activity; | ||
import android.content.Intent; | ||
import android.content.pm.PackageManager; | ||
import android.net.Uri; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.os.VibrationEffect; | ||
import android.os.Vibrator; | ||
import android.provider.Settings; | ||
import android.view.HapticFeedbackConstants; | ||
import android.view.View; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import org.joinmastodon.android.ui.M3AlertDialogBuilder; | ||
import org.joinmastodon.android.ui.utils.UiUtils; | ||
|
||
import de.markusfisch.android.barcodescannerview.widget.BarcodeScannerView; | ||
|
||
public class QrCodeScanActivity extends Activity{ | ||
private static final int PERMISSION_RESULT=65537; | ||
private BarcodeScannerView scannerView; | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState){ | ||
super.onCreate(savedInstanceState); | ||
UiUtils.setUserPreferredTheme(this); | ||
setContentView(R.layout.activity_qr_scan); | ||
|
||
if(this.checkSelfPermission(Manifest.permission.CAMERA)!=PackageManager.PERMISSION_GRANTED){ | ||
requestPermissions(new String[]{Manifest.permission.CAMERA}, PERMISSION_RESULT); | ||
} | ||
|
||
findViewById(R.id.dismiss).setOnClickListener(view -> finish()); | ||
scannerView=findViewById(R.id.scanner); | ||
scannerView.setCropRatio(.75f); | ||
scannerView.setOnBarcodeListener(barcode -> { | ||
vibrate(scannerView); | ||
Intent result=new Intent(); | ||
result.putExtra("barcode", barcode.getText()); | ||
setResult(RESULT_OK, result); | ||
finish(); | ||
return false; | ||
}); | ||
} | ||
|
||
@Override | ||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){ | ||
if(requestCode==PERMISSION_RESULT){ | ||
if(grantResults[0]==PackageManager.PERMISSION_GRANTED){ | ||
scannerView.openAsync(); | ||
}else if(!this.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)){ | ||
new M3AlertDialogBuilder(this) | ||
.setTitle(R.string.permission_required) | ||
.setMessage(R.string.storage_permission_to_download) | ||
.setPositiveButton(R.string.open_settings, (dialog, which)->this.startActivity(new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", this.getPackageName(), null)))) | ||
.setNegativeButton(R.string.cancel, (dialogInterface, i) -> finish()) | ||
.setOnCancelListener(dialogInterface -> finish()) | ||
.show(); | ||
} | ||
} | ||
} | ||
|
||
|
||
private static void vibrate(View v) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
v.performHapticFeedback(HapticFeedbackConstants.CONFIRM); | ||
return; | ||
} | ||
|
||
Vibrator vibrator=v.getContext().getSystemService(Vibrator.class); | ||
|
||
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) { | ||
vibrator.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)); | ||
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
VibrationEffect effect=VibrationEffect.createOneShot(75L, 128); | ||
vibrator.vibrate(effect); | ||
} else { | ||
vibrator.vibrate(75L); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public void onResume() { | ||
super.onResume(); | ||
scannerView.openAsync(); | ||
} | ||
|
||
@Override | ||
public void onPause() { | ||
super.onPause(); | ||
scannerView.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:clipChildren="false" | ||
android:clipToPadding="false"> | ||
|
||
<de.markusfisch.android.barcodescannerview.widget.BarcodeScannerView | ||
android:id="@+id/scanner" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"/> | ||
|
||
<ImageButton | ||
android:id="@+id/dismiss" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="start|top" | ||
android:background="?android:selectableItemBackground" | ||
android:contentDescription="@string/back" | ||
android:padding="16dp" | ||
android:src="@drawable/ic_arrow_back" | ||
android:tint="@android:color/white" | ||
android:layout_margin="16dp"/> | ||
</FrameLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters