diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 4e8ec5c..f98e655 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -32,7 +32,10 @@ android { } buildFeatures { viewBinding = true + buildConfig = true } + + } dependencies { @@ -50,14 +53,14 @@ dependencies { testImplementation(libs.junit) implementation(libs.volley) androidTestImplementation(libs.androidx.junit) - implementation (libs.play.services.code.scanner) + implementation(libs.zxing.android.embedded) + implementation("com.google.zxing:core:3.4.1") androidTestImplementation(libs.androidx.espresso.core) -// implementation("com.google.android.material:1.2.0") - // Barcode scanning API implementation (libs.barcode.scanning) -// CameraX library + + // CameraX library implementation (libs.androidx.camera.camera2) implementation (libs.androidx.camera.lifecycle) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 529a2f4..d45c6f1 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -43,6 +43,10 @@ android:label="@string/title_activity_fullscreen" android:theme="@style/Theme.BarcodeScannerForEmployees.Fullscreen"/> + + - - - - this.barcode = barcode.rawValue.toString() - - performRequest(this.barcode) - } - .addOnFailureListener { e -> - Toast.makeText( - this, - "Failed to scan barcode. Please, try again or enter data manually", - Toast.LENGTH_LONG - ).show() - } + scanningBarcode = true + requestPermissionLauncher.launch(android.Manifest.permission.CAMERA) } } @@ -170,7 +160,11 @@ class AddAbstractProductActivity : AppCompatActivity() { requester.request(this, barcode) var abstractProduct: AbstractProduct - if (DBStorageController(this).findAbstractProductByBarcode(DBStorageController(this).readableDatabase, this.barcode) != null) { + if (DBStorageController(this).findAbstractProductByBarcode( + DBStorageController(this).readableDatabase, + this.barcode + ) != null + ) { AlertDialog.Builder(this) .setMessage("You've got an abstract product with such barcode in your database") .setPositiveButton("Quit") { _: DialogInterface, _: Int -> @@ -179,7 +173,10 @@ class AddAbstractProductActivity : AppCompatActivity() { .setNegativeButton("Edit existing") { _: DialogInterface, _: Int -> val addProductIntent = Intent(this, AddAbstractProductActivity::class.java) val extras = Bundle() - val existingAbstractProduct = DBStorageController(this).findAbstractProductByBarcode(DBStorageController(this).readableDatabase, this.barcode) + val existingAbstractProduct = DBStorageController(this).findAbstractProductByBarcode( + DBStorageController(this).readableDatabase, + this.barcode + ) extras.putParcelable("abstractProduct", existingAbstractProduct) addProductIntent.putExtras(extras) ContextCompat.startActivity(this, addProductIntent, extras) @@ -189,10 +186,12 @@ class AddAbstractProductActivity : AppCompatActivity() { thread { // Я сам в ахуях какой это костыль, пока хз как фиксить, потом придумаю :)) - while (requester.response == "") { } + while (requester.response == "") { + } if (requester.response == "Not found 404") { runOnUiThread { - Toast.makeText(this, "Product not found. Please, try again or type manually", Toast.LENGTH_LONG).show() + Toast.makeText(this, "Product not found. Please, try again or type manually", Toast.LENGTH_LONG) + .show() } return@thread } @@ -217,7 +216,8 @@ class AddAbstractProductActivity : AppCompatActivity() { ) - val arrayAdapter = ArrayAdapter(this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, units) + val arrayAdapter = + ArrayAdapter(this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, units) arrayAdapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item) unitTypeSpinner.adapter = arrayAdapter } @@ -231,15 +231,24 @@ class AddAbstractProductActivity : AppCompatActivity() { CategoriesContract.CategoryEntry.CATEGORY_NAME ) - val cursor = db.query(CategoriesContract.CategoryEntry.TABLE_NAME, projection, null, null, null, null, BaseColumns._ID+" ASC") + val cursor = db.query( + CategoriesContract.CategoryEntry.TABLE_NAME, + projection, + null, + null, + null, + null, + BaseColumns._ID + " ASC" + ) - with (cursor) { + with(cursor) { while (moveToNext()) { categories.add(getString(getColumnIndexOrThrow(CategoriesContract.CategoryEntry.CATEGORY_NAME))) } } - val arrayAdapter = ArrayAdapter(this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, categories) + val arrayAdapter = + ArrayAdapter(this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, categories) arrayAdapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item) categorySpinner.adapter = arrayAdapter } @@ -264,7 +273,7 @@ class AddAbstractProductActivity : AppCompatActivity() { } @RequiresApi(Build.VERSION_CODES.R) - fun getPicture () { + fun getPicture() { //Saving picture to a temp file for further hash calculation and moving to a proper directory val imageFile = File(this.filesDir, "image.png") val imageUri = getImageUri(this, imageFile) @@ -279,9 +288,38 @@ class AddAbstractProductActivity : AppCompatActivity() { ActivityResultContracts.RequestPermission() ) { isGranted: Boolean -> if (isGranted) { - getPicture() + if (scanningBarcode) { + prepareBarcodeScanner() + } else { + getPicture() + } } else { Toast.makeText(this, "I need permission in order to take a picture", Toast.LENGTH_LONG).show() } } + + private val scanLauncher = registerForActivityResult(ScanContract()) { result: ScanIntentResult -> + run { + if (result.contents == null) { + Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show() + } else { + scanningBarcode = false + val scannedBarcode = result.contents + barcodeText.setText(scannedBarcode) + performRequest(scannedBarcode) + } + } + } + + private fun prepareBarcodeScanner() { + val options = ScanOptions() + options.setDesiredBarcodeFormats(ScanOptions.EAN_13) + options.setPrompt("Scan barcode of a product") + options.setCameraId(0) + options.setBeepEnabled(false) + options.setBarcodeImageEnabled(true) + options.setOrientationLocked(false) + + scanLauncher.launch(options) + } } \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt index 329118e..1820d74 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt @@ -1,12 +1,17 @@ package org.foxarmy.barcodescannerforemployees.activities import android.app.DatePickerDialog +import android.os.Build import android.os.Bundle import android.view.View import android.widget.* +import androidx.activity.result.contract.ActivityResultContracts +import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity import androidx.core.widget.addTextChangedListener -import com.google.mlkit.vision.codescanner.GmsBarcodeScanning +import com.journeyapps.barcodescanner.ScanContract +import com.journeyapps.barcodescanner.ScanIntentResult +import com.journeyapps.barcodescanner.ScanOptions import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct @@ -77,17 +82,7 @@ class AddProductActivity : AppCompatActivity() { } scanButton.setOnClickListener { - val scanner = GmsBarcodeScanning.getClient(this) - scanner.startScan() - .addOnSuccessListener { bc -> - abstractProduct = DBStorageController(this).findAbstractProductByBarcode(DBStorageController(this).readableDatabase, bc.rawValue.toString()) - product!!.abstractProductId = abstractProduct!!.id - - displayAbstractProduct(abstractProduct!!) - } - .addOnFailureListener { - Toast.makeText(this, "Cannot scan barcode", Toast.LENGTH_SHORT).show() - } + requestPermissionLauncher.launch(android.Manifest.permission.CAMERA) } expiryDateRadioButton.setOnClickListener { @@ -215,4 +210,41 @@ class AddProductActivity : AppCompatActivity() { abstractProductView.abstractProduct = abstractProduct abstractProductView.update() } -} \ No newline at end of file + + @RequiresApi(Build.VERSION_CODES.R) + val requestPermissionLauncher = + registerForActivityResult( + ActivityResultContracts.RequestPermission() + ) { isGranted: Boolean -> + if (isGranted) { + prepareBarcodeScanner() + } else { + Toast.makeText(this, "I need permission in order to take a picture", Toast.LENGTH_LONG).show() + } + } + + private val scanLauncher = registerForActivityResult(ScanContract()) { result: ScanIntentResult -> + run { + if (result.contents == null) { + Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show() + } else { + val scannedBarcode = result.contents + abstractProduct = DBStorageController(this).findAbstractProductByBarcode(DBStorageController(this).readableDatabase, scannedBarcode) + displayAbstractProduct(abstractProduct!!) + product?.abstractProductId = abstractProduct!!.id + } + } + } + + private fun prepareBarcodeScanner() { + val options = ScanOptions() + options.setDesiredBarcodeFormats(ScanOptions.EAN_13) + options.setPrompt("Scan barcode of a product") + options.setCameraId(0) + options.setBeepEnabled(false) + options.setBarcodeImageEnabled(true) + options.setOrientationLocked(false) + + scanLauncher.launch(options) + } +} diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt index 95ea038..24d688e 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt @@ -121,7 +121,6 @@ class StorageFragment : Fragment() { ) var orderBy: String = "" - Log.d("QWERTYUIOP", binding.spinner.selectedItem.toString()) when(binding.spinner.selectedItem) { "Name" -> { orderBy = "${AbstractProductContract.AbstractProductEntry.PRODUCT_NAME} ASC" diff --git a/app/src/main/res/layout/fragment_add_abstract_product.xml b/app/src/main/res/layout/fragment_add_abstract_product.xml index 940e50c..5fe0b8c 100644 --- a/app/src/main/res/layout/fragment_add_abstract_product.xml +++ b/app/src/main/res/layout/fragment_add_abstract_product.xml @@ -9,7 +9,7 @@ + android:padding="16dp" android:layout_gravity="center">