diff --git a/android/src/main/java/org/ligi/passandroid/model/pass/PassBarCodeFormat.kt b/android/src/main/java/org/ligi/passandroid/model/pass/PassBarCodeFormat.kt index bb691be8..81499f77 100644 --- a/android/src/main/java/org/ligi/passandroid/model/pass/PassBarCodeFormat.kt +++ b/android/src/main/java/org/ligi/passandroid/model/pass/PassBarCodeFormat.kt @@ -8,6 +8,7 @@ enum class PassBarCodeFormat { CODE_39, CODE_128, DATA_MATRIX, + EAN_8, EAN_13, ITF, PDF_417, @@ -23,6 +24,7 @@ enum class PassBarCodeFormat { CODE_39 -> BarcodeFormat.CODE_39 CODE_128 -> BarcodeFormat.CODE_128 DATA_MATRIX -> BarcodeFormat.DATA_MATRIX + EAN_8 -> BarcodeFormat.EAN_8 EAN_13 -> BarcodeFormat.EAN_13 ITF -> BarcodeFormat.ITF PDF_417 -> BarcodeFormat.PDF_417 diff --git a/android/src/main/java/org/ligi/passandroid/ui/edit/BarcodeEditController.kt b/android/src/main/java/org/ligi/passandroid/ui/edit/BarcodeEditController.kt index 22537af2..52d52558 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/edit/BarcodeEditController.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/edit/BarcodeEditController.kt @@ -9,8 +9,7 @@ import kotlinx.android.synthetic.main.barcode_edit.view.* import org.ligi.kaxt.doAfterEdit import org.ligi.passandroid.model.pass.BarCode import org.ligi.passandroid.model.pass.PassBarCodeFormat -import org.ligi.passandroid.model.pass.PassBarCodeFormat.EAN_13 -import org.ligi.passandroid.model.pass.PassBarCodeFormat.QR_CODE +import org.ligi.passandroid.model.pass.PassBarCodeFormat.* import org.ligi.passandroid.ui.BarcodeUIController import org.ligi.passandroid.ui.PassViewHelper import java.util.* @@ -56,6 +55,7 @@ class BarcodeEditController(val rootView: View, internal val context: AppCompatA rootView.randomButton.setOnClickListener({ rootView.messageInput.setText(when (barcodeFormat) { + EAN_8 -> getRandomEAN8() EAN_13 -> getRandomEAN13() PassBarCodeFormat.ITF -> getRandomITF() else -> UUID.randomUUID().toString().toUpperCase() diff --git a/android/src/main/java/org/ligi/passandroid/ui/edit/EANHelper.kt b/android/src/main/java/org/ligi/passandroid/ui/edit/EANHelper.kt index 2e6d54af..c122caa0 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/edit/EANHelper.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/edit/EANHelper.kt @@ -1,14 +1,25 @@ package org.ligi.passandroid.ui.edit import com.google.zxing.oned.EAN13Writer +import com.google.zxing.oned.EAN8Writer +import com.google.zxing.oned.UPCEANWriter fun getRandomEAN13(): String { val randomString = (0..11).map { (Math.random() * 9).toInt() }.joinToString(separator = "") return (0..9).map { randomString + it }.first(::isValidEAN13) } -fun isValidEAN13(payload: String) = try { - EAN13Writer().encode(payload) +fun isValidEAN13(payload: String) = isValidEAN(payload, EAN13Writer()) + +fun getRandomEAN8(): String { + val randomString = (0..6).map { (Math.random() * 9).toInt() }.joinToString(separator = "") + return (0..9).map { randomString + it }.first(::isValidEAN8) +} + +fun isValidEAN8(payload: String) = isValidEAN(payload, EAN8Writer()) + +fun isValidEAN(payload: String, writer: UPCEANWriter) = try { + writer.encode(payload) true } catch(e: IllegalArgumentException) { false