WIP: try to create custom scanActivity

This commit is contained in:
jacques 2022-02-13 17:49:25 +01:00
parent 8ac3a79639
commit 55c2b67f2e
4 changed files with 70 additions and 25 deletions

View file

@ -1,20 +1,24 @@
package net.foucry.pilldroid; package net.foucry.pilldroid;
import android.app.Activity; import android.app.Activity;
import android.app.job.JobService;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Color; import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.View; import android.view.View;
import android.widget.ImageButton; import android.widget.ImageButton;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import com.google.zxing.client.android.Intents;
import com.journeyapps.barcodescanner.BarcodeCallback;
import com.journeyapps.barcodescanner.BarcodeResult;
import com.journeyapps.barcodescanner.CaptureManager; import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView; import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.journeyapps.barcodescanner.ScanContract;
import com.journeyapps.barcodescanner.ViewfinderView; import com.journeyapps.barcodescanner.ViewfinderView;
import java.util.Random; import java.util.Random;
@ -22,8 +26,8 @@ import java.util.Random;
/** /**
* Custom Scanner Activity extending from Activity to display a custom layout form scanner view. * Custom Scanner Activity extending from Activity to display a custom layout form scanner view.
*/ */
public class CustomScannerActivity extends Activity implements public class CustomScannerActivity extends Activity {
DecoratedBarcodeView.TorchListener {
private static final String TAG = CustomScannerActivity.class.getName(); private static final String TAG = CustomScannerActivity.class.getName();
private CaptureManager capture; private CaptureManager capture;
@ -31,17 +35,23 @@ public class CustomScannerActivity extends Activity implements
private ImageButton switchFlashlightButton; private ImageButton switchFlashlightButton;
private ViewfinderView viewfinderView; private ViewfinderView viewfinderView;
private ActivityResultLauncher<Intent> manualAddLauncher;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_scanner); setContentView(R.layout.activity_custom_scanner);
barcodeScannerView = findViewById(R.id.zxing_barcode_scanner); manualAddLauncher = registerForActivityResult(new ActivityResultContract.StartActivityForResutl(),
result -> DrugListActivity.showInputDialog());
barcodeScannerView.setTorchListener(this); barcodeScannerView.setTorchListener(this);
switchFlashlightButton = findViewById(R.id.switch_flashlight); findViewById(R.id.keyboard_button).setOnClickListener(this::addManually);
viewfinderView = findViewById(R.id.zxing_viewfinder_view); findViewById(R.id.cancel_button).setOnClickListener(this::onCancel);
// viewfinderView = findViewById(R.id.zxing_viewfinder_view);
barcodeScannerView = findViewById(R.id.zxing_barcode_scanner);
// if the device does not have flashlight in its camera, // if the device does not have flashlight in its camera,
// then remove the switch flashlight button... // then remove the switch flashlight button...
@ -50,13 +60,29 @@ public class CustomScannerActivity extends Activity implements
} }
capture = new CaptureManager(this, barcodeScannerView); capture = new CaptureManager(this, barcodeScannerView);
Intent captureIntent = new Intent();
Bundle captureIntentBundle = new Bundle();
captureIntentBundle.putBoolean(Intents.Scan.BEEP_ENABLED, true);
captureIntent.putExtras(captureIntentBundle);
capture.initializeFromIntent(getIntent(), savedInstanceState); capture.initializeFromIntent(getIntent(), savedInstanceState);
capture.setShowMissingCameraPermissionDialog(false); capture.setShowMissingCameraPermissionDialog(false);
capture.decode(); //capture.decode();
changeMaskColor(null); changeMaskColor(null);
changeLaserVisibility(true); changeLaserVisibility(true);
barcodeScannerView.decodeSingle(new BarcodeCallback() {
@Override
public void barcodeResult(BarcodeResult result) {
Intent scanResult = new Intent();
Bundle scanResultBundle = new Bundle();
scanResultBundle.putString("Barcode Content", result.getText());
scanResultBundle.putString("Barcode Format name", result.getBarcodeFormat().name());
scanResultBundle.putInt("returnCode", captureIntentBundle.getInt("returnCode"));
scanResult.putExtras(scanResultBundle);
CustomScannerActivity.this.setResult(RESULT_OK, scanResult);
finish();
}
});
} }
@Override @Override
@ -98,6 +124,7 @@ public class CustomScannerActivity extends Activity implements
} }
public void switchFlashlight(View view) { public void switchFlashlight(View view) {
Log.d(TAG, "Switch torch");
if (switchFlashlightButton.isActivated()) { if (switchFlashlightButton.isActivated()) {
barcodeScannerView.setTorchOff(); barcodeScannerView.setTorchOff();
} else { } else {
@ -117,11 +144,13 @@ public class CustomScannerActivity extends Activity implements
@Override @Override
public void onTorchOn() { public void onTorchOn() {
Log.d(TAG, "TorchON");
switchFlashlightButton.setActivated(true); switchFlashlightButton.setActivated(true);
} }
@Override @Override
public void onTorchOff() { public void onTorchOff() {
Log.d(TAG, "TorchOFF");
switchFlashlightButton.setActivated(false); switchFlashlightButton.setActivated(false);
} }
@ -130,13 +159,17 @@ public class CustomScannerActivity extends Activity implements
capture.onRequestPermissionsResult(requestCode, permissions, grantResults); capture.onRequestPermissionsResult(requestCode, permissions, grantResults);
} }
private void handleActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode);
}
public void onKeyboard(View view) { public void onKeyboard(View view) {
setResult(3); Log.d(TAG, "onkeyboard");
finish(); captureIntentBundle.putInt("returnCode", 3);
} }
public void onCancel(View view) { public void onCancel(View view) {
setResult(2); Log.d(TAG, "onCancel");
finish(); captureIntentBundle.putInt("returnCode", 2);
} }
} }

View file

@ -25,15 +25,13 @@ import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.google.zxing.client.android.Intents;
import com.journeyapps.barcodescanner.ScanOptions;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -53,12 +51,14 @@ public class DrugListActivity extends AppCompatActivity {
* device. * device.
*/ */
// TODO: Change DEMO/DBDEMO form static to non-static. In order to create fake data at only at launchtime // TODO: Change DEMO/DBDEMO form static to non-static. In order to create fake data at only at launch time
final Boolean DEMO = false; final Boolean DEMO = false;
final Boolean DBDEMO = false; final Boolean DBDEMO = false;
public final int CUSTOMIZED_REQUEST_CODE = 0x0000ffff; public final int CUSTOMIZED_REQUEST_CODE = 0x0000ffff;
private ActivityResultLauncher<Intent> mBarcodeScannerLauncher;
/** /**
* Start tutorial * Start tutorial
*/ */
@ -177,6 +177,12 @@ public class DrugListActivity extends AppCompatActivity {
} }
} }
mBarcodeScannerLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),result -> {
Intent intent = result.getData();
Log.d(TAG, "intent == " + intent);
startActivity(intent);
});
constructDrugsList(); constructDrugsList();
} }
@ -215,7 +221,8 @@ public class DrugListActivity extends AppCompatActivity {
/** Register the launcher and result handler /** Register the launcher and result handler
* ActivityResultLauncher * ActivityResultLauncher
*/ */
private final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new PilldroidScanContract(),
/*private final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new PilldroidScanContract(),
result -> { result -> {
if(result.getContents() == null) { if(result.getContents() == null) {
Toast.makeText(DrugListActivity.this, "Cancelled", Toast.LENGTH_LONG).show(); Toast.makeText(DrugListActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
@ -226,7 +233,7 @@ public class DrugListActivity extends AppCompatActivity {
Toast.LENGTH_LONG).show(); Toast.LENGTH_LONG).show();
} else if (originalIntent.hasExtra(Intents.Scan.MISSING_CAMERA_PERMISSION)) { } else if (originalIntent.hasExtra(Intents.Scan.MISSING_CAMERA_PERMISSION)) {
Log.d(TAG, "Cancelled scan due missing camera permission"); Log.d(TAG, "Cancelled scan due missing camera permission");
Toast.makeText(DrugListActivity.this, "Cancelled due missing camera persmission", Toast.makeText(DrugListActivity.this, "Cancelled due missing camera permission",
Toast.LENGTH_LONG).show(); Toast.LENGTH_LONG).show();
} else if (originalIntent.hasExtra(Intents.Scan.TIMEOUT)) { } else if (originalIntent.hasExtra(Intents.Scan.TIMEOUT)) {
Log.d(TAG, "Cancelled due timeout"); Log.d(TAG, "Cancelled due timeout");
@ -238,11 +245,11 @@ public class DrugListActivity extends AppCompatActivity {
Toast.makeText(DrugListActivity.this, "Scanned", Toast.LENGTH_LONG).show(); Toast.makeText(DrugListActivity.this, "Scanned", Toast.LENGTH_LONG).show();
//Intent originalIntent = scanIntentResult.getOriginalIntent(); //Intent originalIntent = scanIntentResult.getOriginalIntent();
} }
}); });*/
// Launch scan // Launch scan
public void onButtonClick(View view) { public void onButtonClick(View view) {
ScanOptions options = new ScanOptions(); /*ScanOptions options = new ScanOptions();
options.setDesiredBarcodeFormats(ScanOptions.DATA_MATRIX, ScanOptions.CODE_39, options.setDesiredBarcodeFormats(ScanOptions.DATA_MATRIX, ScanOptions.CODE_39,
ScanOptions.CODE_128, ScanOptions.CODE_128); ScanOptions.CODE_128, ScanOptions.CODE_128);
options.setCameraId(0); // Use a specific camera of the device options.setCameraId(0); // Use a specific camera of the device
@ -251,8 +258,12 @@ public class DrugListActivity extends AppCompatActivity {
//options.setTimeout(3600); //options.setTimeout(3600);
options.setCaptureActivity(CustomScannerActivity.class); options.setCaptureActivity(CustomScannerActivity.class);
options.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.MIXED_SCAN); options.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.MIXED_SCAN);
options.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.INVERTED_SCAN); options.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.INVERTED_SCAN);*/
barcodeLauncher.launch(options); //barcodeLauncher.launch(options);
Intent intent = new Intent(getApplicationContext(), CustomScannerActivity.class);
Bundle bundle = new Bundle();
intent.putExtras(bundle);
mBarcodeScannerLauncher.launch(intent);
} }
/* @Override /* @Override

View file

@ -4,7 +4,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context="example.zxing.CustomScannerActivity"> tools:context="net.foucry.pilldroid.CustomScannerActivity">
<com.journeyapps.barcodescanner.DecoratedBarcodeView <com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="@+id/zxing_barcode_scanner" android:id="@+id/zxing_barcode_scanner"

View file

@ -60,6 +60,7 @@
android:backgroundTint="@android:color/transparent" android:backgroundTint="@android:color/transparent"
android:contentDescription="@string/button_keyboard" android:contentDescription="@string/button_keyboard"
android:onClick="onKeyboard" android:onClick="onKeyboard"
android:src="@drawable/ic_keyboard_black_24dp" /> android:src="@drawable/ic_keyboard_black_24dp"
tools:ignore="PrivateResource" />
</merge> </merge>