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;
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<Intent> 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);
}
}

View file

@ -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;
@ -59,6 +57,8 @@ public class DrugListActivity extends AppCompatActivity {
public final int CUSTOMIZED_REQUEST_CODE = 0x0000ffff;
private ActivityResultLauncher<Intent> 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<ScanOptions> barcodeLauncher = registerForActivityResult(new PilldroidScanContract(),
/*private final ActivityResultLauncher<ScanOptions> 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

View file

@ -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">
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="@+id/zxing_barcode_scanner"

View file

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