2020-05-23 19:10:46 +02:00
|
|
|
package net.foucry.pilldroid;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2022-02-08 07:48:12 +01:00
|
|
|
import android.content.Intent;
|
2020-05-23 19:10:46 +02:00
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.os.Bundle;
|
2022-02-13 17:49:25 +01:00
|
|
|
import android.util.Log;
|
2020-05-23 19:10:46 +02:00
|
|
|
import android.view.KeyEvent;
|
|
|
|
import android.view.View;
|
2020-05-24 21:24:12 +02:00
|
|
|
import android.widget.ImageButton;
|
2020-05-23 19:10:46 +02:00
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
2022-02-13 17:49:25 +01:00
|
|
|
import com.google.zxing.client.android.Intents;
|
|
|
|
import com.journeyapps.barcodescanner.BarcodeCallback;
|
|
|
|
import com.journeyapps.barcodescanner.BarcodeResult;
|
2020-05-23 19:10:46 +02:00
|
|
|
import com.journeyapps.barcodescanner.CaptureManager;
|
|
|
|
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
|
|
|
|
import com.journeyapps.barcodescanner.ViewfinderView;
|
|
|
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom Scanner Activity extending from Activity to display a custom layout form scanner view.
|
|
|
|
*/
|
2022-02-13 17:49:25 +01:00
|
|
|
public class CustomScannerActivity extends Activity {
|
|
|
|
|
2022-02-17 11:29:19 +01:00
|
|
|
private static final String TAG = CustomScannerActivity.class.getName();
|
2020-05-23 19:10:46 +02:00
|
|
|
|
|
|
|
private CaptureManager capture;
|
|
|
|
private DecoratedBarcodeView barcodeScannerView;
|
2020-07-03 21:44:20 +02:00
|
|
|
private ImageButton switchFlashlightButton;
|
2020-05-23 19:10:46 +02:00
|
|
|
private ViewfinderView viewfinderView;
|
2020-06-29 18:29:17 +02:00
|
|
|
|
2022-02-19 12:21:24 +01:00
|
|
|
Intent captureIntent = new Intent();
|
|
|
|
Bundle captureIntentBundle = new Bundle();
|
2022-02-08 07:48:12 +01:00
|
|
|
|
2020-05-23 19:10:46 +02:00
|
|
|
@Override
|
2020-06-12 11:10:30 +02:00
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
2020-05-23 19:10:46 +02:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
2022-02-17 11:29:19 +01:00
|
|
|
setContentView(R.layout.activity_custom_scanner);
|
2022-02-19 12:21:24 +01:00
|
|
|
/* manualAddLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
|
|
|
|
result -> handleActivityResult(result.getResultCode(),
|
|
|
|
result.getData()));*/
|
2022-02-13 17:49:25 +01:00
|
|
|
|
2022-02-17 11:29:19 +01:00
|
|
|
//barcodeScannerView.setTorchListener(this);
|
2020-05-23 19:10:46 +02:00
|
|
|
|
2022-02-17 11:29:19 +01:00
|
|
|
findViewById(R.id.keyboard_button).setOnClickListener(this::onKeyboard);
|
2022-02-13 17:49:25 +01:00
|
|
|
findViewById(R.id.cancel_button).setOnClickListener(this::onCancel);
|
2022-02-19 12:21:24 +01:00
|
|
|
findViewById(R.id.switch_flashlight).setOnClickListener(this::switchFlashlight);
|
|
|
|
switchFlashlightButton = findViewById(R.id.switch_flashlight);
|
|
|
|
viewfinderView = findViewById(R.id.zxing_viewfinder_view);
|
2022-02-13 17:49:25 +01:00
|
|
|
|
|
|
|
barcodeScannerView = findViewById(R.id.zxing_barcode_scanner);
|
2020-05-23 19:10:46 +02:00
|
|
|
|
|
|
|
// if the device does not have flashlight in its camera,
|
|
|
|
// then remove the switch flashlight button...
|
|
|
|
if (!hasFlash()) {
|
2022-02-19 12:21:24 +01:00
|
|
|
findViewById(R.id.switch_flashlight).setVisibility(View.GONE);
|
2020-05-23 19:10:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
capture = new CaptureManager(this, barcodeScannerView);
|
2022-02-19 12:21:24 +01:00
|
|
|
|
2022-02-13 17:49:25 +01:00
|
|
|
captureIntentBundle.putBoolean(Intents.Scan.BEEP_ENABLED, true);
|
|
|
|
captureIntent.putExtras(captureIntentBundle);
|
2020-05-23 19:10:46 +02:00
|
|
|
capture.initializeFromIntent(getIntent(), savedInstanceState);
|
|
|
|
capture.setShowMissingCameraPermissionDialog(false);
|
2022-02-13 17:49:25 +01:00
|
|
|
//capture.decode();
|
2022-02-08 07:48:12 +01:00
|
|
|
|
2020-05-23 19:10:46 +02:00
|
|
|
changeMaskColor(null);
|
|
|
|
changeLaserVisibility(true);
|
2022-02-13 17:49:25 +01:00
|
|
|
barcodeScannerView.decodeSingle(new BarcodeCallback() {
|
|
|
|
@Override
|
|
|
|
public void barcodeResult(BarcodeResult result) {
|
|
|
|
Intent scanResult = new Intent();
|
2022-02-19 12:21:24 +01:00
|
|
|
//Bundle scanResultBundle = new Bundle();
|
|
|
|
scanResult.putExtra("Barcode Content", result.getText());
|
|
|
|
scanResult.putExtra("Barcode Format name", result.getBarcodeFormat().name());
|
|
|
|
scanResult.putExtra("returnCode", captureIntentBundle.getInt("returnCode"));
|
|
|
|
scanResult.putExtra("resultCode", 1);
|
2022-02-13 17:49:25 +01:00
|
|
|
CustomScannerActivity.this.setResult(RESULT_OK, scanResult);
|
2022-02-19 12:21:24 +01:00
|
|
|
Log.d(TAG, "scanResult == " + scanResult.toString());
|
2022-02-13 17:49:25 +01:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
});
|
2020-05-23 19:10:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
capture.onResume();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
capture.onPause();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
capture.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
capture.onSaveInstanceState(outState);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
|
|
|
return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the device's camera has a Flashlight.
|
2022-02-17 11:29:19 +01:00
|
|
|
*
|
2020-05-23 19:10:46 +02:00
|
|
|
* @return true if there is Flashlight, otherwise false.
|
|
|
|
*/
|
|
|
|
private boolean hasFlash() {
|
|
|
|
return getApplicationContext().getPackageManager()
|
|
|
|
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void switchFlashlight(View view) {
|
2022-02-13 17:49:25 +01:00
|
|
|
Log.d(TAG, "Switch torch");
|
2020-07-03 21:44:20 +02:00
|
|
|
if (switchFlashlightButton.isActivated()) {
|
2020-05-23 19:10:46 +02:00
|
|
|
barcodeScannerView.setTorchOff();
|
2020-07-03 21:44:20 +02:00
|
|
|
} else {
|
|
|
|
barcodeScannerView.setTorchOn();
|
2020-05-23 19:10:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void changeMaskColor(View view) {
|
|
|
|
Random rnd = new Random();
|
|
|
|
int color = Color.argb(100, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
|
|
|
|
viewfinderView.setMaskColor(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void changeLaserVisibility(boolean visible) {
|
|
|
|
viewfinderView.setLaserVisibility(visible);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onTorchOn() {
|
2022-02-13 17:49:25 +01:00
|
|
|
Log.d(TAG, "TorchON");
|
2020-07-03 21:44:20 +02:00
|
|
|
switchFlashlightButton.setActivated(true);
|
2020-05-23 19:10:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onTorchOff() {
|
2022-02-13 17:49:25 +01:00
|
|
|
Log.d(TAG, "TorchOFF");
|
2020-07-03 21:44:20 +02:00
|
|
|
switchFlashlightButton.setActivated(false);
|
2020-05-23 19:10:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
|
capture.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
|
}
|
2020-05-24 21:24:12 +02:00
|
|
|
|
2020-05-26 19:54:21 +02:00
|
|
|
public void onKeyboard(View view) {
|
2022-02-13 17:49:25 +01:00
|
|
|
Log.d(TAG, "onkeyboard");
|
2022-02-19 12:21:24 +01:00
|
|
|
Intent resultIntent = new Intent();
|
|
|
|
resultIntent.putExtra("returnCode",3);
|
|
|
|
CustomScannerActivity.this.setResult(RESULT_OK, resultIntent);
|
|
|
|
finish();
|
2020-05-24 21:24:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 19:54:21 +02:00
|
|
|
public void onCancel(View view) {
|
2022-02-13 17:49:25 +01:00
|
|
|
Log.d(TAG, "onCancel");
|
2022-02-19 12:21:24 +01:00
|
|
|
Intent resultIntent = new Intent();
|
|
|
|
resultIntent.putExtra("returnCode", 2);
|
|
|
|
CustomScannerActivity.this.setResult(RESULT_OK, resultIntent);
|
|
|
|
finish();
|
2020-05-24 21:24:12 +02:00
|
|
|
}
|
2022-02-17 11:29:19 +01:00
|
|
|
|
2022-02-19 12:21:24 +01:00
|
|
|
/*private void handleActivityResult(int resultCode, Intent intent) {
|
|
|
|
super.onActivityResult(Utils.SELECT_BARCODE_REQUEST, resultCode, intent);
|
2022-02-17 11:29:19 +01:00
|
|
|
|
|
|
|
BarcodeValues barcodeValues;
|
|
|
|
|
|
|
|
try {
|
2022-02-19 12:21:24 +01:00
|
|
|
barcodeValues = Utils.parseSetBarcodeActivityResult(Utils.SELECT_BARCODE_REQUEST, resultCode, intent, this);
|
2022-02-17 11:29:19 +01:00
|
|
|
} catch (NullPointerException e) {
|
|
|
|
Toast.makeText(this, "Error reading image", Toast.LENGTH_LONG).show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!barcodeValues.isEmpty()) {
|
|
|
|
Intent manualResult = new Intent();
|
|
|
|
Bundle manualResultBundle = new Bundle();
|
|
|
|
manualResultBundle.putString("BarcodeContent", barcodeValues.content());
|
|
|
|
manualResultBundle.putString("BarcodeFormat", barcodeValues.format());
|
|
|
|
|
|
|
|
manualResult.putExtras(manualResultBundle);
|
|
|
|
CustomScannerActivity.this.setResult(RESULT_OK, manualResult);
|
|
|
|
finish();
|
|
|
|
}
|
2022-02-19 12:21:24 +01:00
|
|
|
}*/
|
2020-05-23 19:10:46 +02:00
|
|
|
}
|