From 55c2b67f2e55813eb5dd65dc2c5a839fda72a2dd Mon Sep 17 00:00:00 2001 From: jacques Date: Sun, 13 Feb 2022 17:49:25 +0100 Subject: [PATCH] WIP: try to create custom scanActivity --- .../pilldroid/CustomScannerActivity.java | 59 +++++++++++++++---- .../foucry/pilldroid/DrugListActivity.java | 31 ++++++---- .../res/layout/activity_custom_scanner.xml | 2 +- .../res/layout/custom_barcode_scanner.xml | 3 +- 4 files changed, 70 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/CustomScannerActivity.java b/app/src/main/java/net/foucry/pilldroid/CustomScannerActivity.java index f800c07..34f0905 100644 --- a/app/src/main/java/net/foucry/pilldroid/CustomScannerActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/CustomScannerActivity.java @@ -1,20 +1,24 @@ package net.foucry.pilldroid; import android.app.Activity; -import android.app.job.JobService; import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Color; import android.os.Bundle; +import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.widget.ImageButton; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContract; 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.DecoratedBarcodeView; -import com.journeyapps.barcodescanner.ScanContract; import com.journeyapps.barcodescanner.ViewfinderView; 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. */ -public class CustomScannerActivity extends Activity implements - DecoratedBarcodeView.TorchListener { +public class CustomScannerActivity extends Activity { + private static final String TAG = CustomScannerActivity.class.getName(); private CaptureManager capture; @@ -31,17 +35,23 @@ public class CustomScannerActivity extends Activity implements private ImageButton switchFlashlightButton; private ViewfinderView viewfinderView; + private ActivityResultLauncher manualAddLauncher; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_custom_scanner); - barcodeScannerView = findViewById(R.id.zxing_barcode_scanner); + manualAddLauncher = registerForActivityResult(new ActivityResultContract.StartActivityForResutl(), + result -> DrugListActivity.showInputDialog()); + barcodeScannerView.setTorchListener(this); - switchFlashlightButton = findViewById(R.id.switch_flashlight); - viewfinderView = findViewById(R.id.zxing_viewfinder_view); + findViewById(R.id.keyboard_button).setOnClickListener(this::addManually); + 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, // then remove the switch flashlight button... @@ -50,13 +60,29 @@ public class CustomScannerActivity extends Activity implements } 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.setShowMissingCameraPermissionDialog(false); - capture.decode(); - + //capture.decode(); changeMaskColor(null); 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 @@ -98,6 +124,7 @@ public class CustomScannerActivity extends Activity implements } public void switchFlashlight(View view) { + Log.d(TAG, "Switch torch"); if (switchFlashlightButton.isActivated()) { barcodeScannerView.setTorchOff(); } else { @@ -117,11 +144,13 @@ public class CustomScannerActivity extends Activity implements @Override public void onTorchOn() { + Log.d(TAG, "TorchON"); switchFlashlightButton.setActivated(true); } @Override public void onTorchOff() { + Log.d(TAG, "TorchOFF"); switchFlashlightButton.setActivated(false); } @@ -130,13 +159,17 @@ public class CustomScannerActivity extends Activity implements capture.onRequestPermissionsResult(requestCode, permissions, grantResults); } + private void handleActivityResult(int requestCode, int resultCode, Intent intent) { + super.onActivityResult(requestCode, resultCode); + + } public void onKeyboard(View view) { - setResult(3); - finish(); + Log.d(TAG, "onkeyboard"); + captureIntentBundle.putInt("returnCode", 3); } public void onCancel(View view) { - setResult(2); - finish(); + Log.d(TAG, "onCancel"); + captureIntentBundle.putInt("returnCode", 2); } } diff --git a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java index bf12c87..cd6e59d 100644 --- a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java @@ -25,15 +25,13 @@ import android.widget.TextView; import android.widget.Toast; import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.recyclerview.widget.RecyclerView; -import com.google.zxing.client.android.Intents; -import com.journeyapps.barcodescanner.ScanOptions; - import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @@ -53,12 +51,14 @@ public class DrugListActivity extends AppCompatActivity { * 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 DBDEMO = false; public final int CUSTOMIZED_REQUEST_CODE = 0x0000ffff; + private ActivityResultLauncher mBarcodeScannerLauncher; + /** * 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(); } @@ -215,7 +221,8 @@ public class DrugListActivity extends AppCompatActivity { /** Register the launcher and result handler * ActivityResultLauncher */ - private final ActivityResultLauncher barcodeLauncher = registerForActivityResult(new PilldroidScanContract(), + + /*private final ActivityResultLauncher barcodeLauncher = registerForActivityResult(new PilldroidScanContract(), result -> { if(result.getContents() == null) { Toast.makeText(DrugListActivity.this, "Cancelled", Toast.LENGTH_LONG).show(); @@ -226,7 +233,7 @@ public class DrugListActivity extends AppCompatActivity { Toast.LENGTH_LONG).show(); } else if (originalIntent.hasExtra(Intents.Scan.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(); } else if (originalIntent.hasExtra(Intents.Scan.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(); //Intent originalIntent = scanIntentResult.getOriginalIntent(); } - }); + });*/ // Launch scan public void onButtonClick(View view) { - ScanOptions options = new ScanOptions(); + /*ScanOptions options = new ScanOptions(); options.setDesiredBarcodeFormats(ScanOptions.DATA_MATRIX, ScanOptions.CODE_39, ScanOptions.CODE_128, ScanOptions.CODE_128); options.setCameraId(0); // Use a specific camera of the device @@ -251,8 +258,12 @@ public class DrugListActivity extends AppCompatActivity { //options.setTimeout(3600); options.setCaptureActivity(CustomScannerActivity.class); options.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.MIXED_SCAN); - options.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.INVERTED_SCAN); - barcodeLauncher.launch(options); + options.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.INVERTED_SCAN);*/ + //barcodeLauncher.launch(options); + Intent intent = new Intent(getApplicationContext(), CustomScannerActivity.class); + Bundle bundle = new Bundle(); + intent.putExtras(bundle); + mBarcodeScannerLauncher.launch(intent); } /* @Override diff --git a/app/src/main/res/layout/activity_custom_scanner.xml b/app/src/main/res/layout/activity_custom_scanner.xml index 51cda62..c4d5e1a 100644 --- a/app/src/main/res/layout/activity_custom_scanner.xml +++ b/app/src/main/res/layout/activity_custom_scanner.xml @@ -4,7 +4,7 @@ xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context="example.zxing.CustomScannerActivity"> + tools:context="net.foucry.pilldroid.CustomScannerActivity"> + android:src="@drawable/ic_keyboard_black_24dp" + tools:ignore="PrivateResource" /> \ No newline at end of file