Ability to generate random EAN13

This commit is contained in:
ligi 2016-04-18 02:02:13 +02:00
parent e0ff222563
commit a5cf371d9d
3 changed files with 60 additions and 14 deletions

View file

@ -13,7 +13,8 @@ import org.ligi.axt.simplifications.SimpleTextWatcher
import org.ligi.passandroid.R
import org.ligi.passandroid.model.pass.BarCode
import org.ligi.passandroid.model.pass.PassBarCodeFormat
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.ui.BarcodeUIController
import org.ligi.passandroid.ui.PassViewHelper
import java.util.*
@ -45,7 +46,7 @@ class BarcodeEditController(val rootView: View, internal val context: AppCompatA
formats.forEach {
val radioButton = RadioButton(context)
radioGroup.addView(radioButton)
radioButton.text = it.name
radioButton.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked) {
@ -61,26 +62,27 @@ class BarcodeEditController(val rootView: View, internal val context: AppCompatA
init {
randomButton.setOnClickListener({
messageInput.setText(UUID.randomUUID().toString().toUpperCase())
})
intentFragment = IntentFragment()
barcodeFormat = barCode.format
randomButton.setOnClickListener({
if (barcodeFormat == EAN_13) {
messageInput.setText(EANHelper.getRandomEAN13())
} else {
messageInput.setText(UUID.randomUUID().toString().toUpperCase())
}
})
scanButton.setOnClickListener({
val barCodeIntentIntegrator = BarCodeIntentIntegrator(intentFragment)
if (barcodeFormat == QR_CODE) {
barCodeIntentIntegrator.initiateScan(BarCodeIntentIntegrator.QR_CODE_TYPES)
} else if (barcodeFormat == AZTEC) {
barCodeIntentIntegrator.initiateScan(setOf("AZTEC"))
} else if (barcodeFormat == PDF_417) {
barCodeIntentIntegrator.initiateScan(setOf("PDF417"))
} else if (barcodeFormat == CODE_39) {
barCodeIntentIntegrator.initiateScan(setOf("CODE_39"))
} else if (barcodeFormat == CODE_128) {
barCodeIntentIntegrator.initiateScan(setOf("CODE_128"))
} else {
barCodeIntentIntegrator.initiateScan(setOf(barcodeFormat!!.name))
}
})

View file

@ -0,0 +1,19 @@
package org.ligi.passandroid.ui.edit
import com.google.zxing.oned.EAN13Writer
object EANHelper {
fun getRandomEAN13(): String {
val randomString = 0.rangeTo(11).map { "" + (Math.random() * 9).toInt() }.joinToString(separator = "") { it }
return 0.rangeTo(9).map { randomString + it }.first() { isValidEAN13(it) }
}
fun isValidEAN13(payload: String): Boolean {
try {
EAN13Writer().encode(payload)
return true
} catch(e: IllegalArgumentException) {
return false
}
}
}

View file

@ -0,0 +1,25 @@
package org.ligi.passandroid.unittest;
import java.io.IOException;
import java.net.URISyntaxException;
import org.junit.Test;
import org.ligi.passandroid.ui.edit.EANHelper;
import static org.assertj.core.api.Assertions.assertThat;
public class TheEANHelper {
@Test
public void randomEAN13HasCorrectLength() throws URISyntaxException, IOException {
assertThat(EANHelper.INSTANCE.getRandomEAN13().length()).isEqualTo(13);
}
@Test
public void acceptGoodEAN13() throws URISyntaxException, IOException {
assertThat(EANHelper.INSTANCE.isValidEAN13("6416016588755")).isTrue();
}
@Test
public void rejectBadEAN13() throws URISyntaxException, IOException {
assertThat(EANHelper.INSTANCE.isValidEAN13("foo")).isFalse();
}
}