Skip to content

Commit

Permalink
Add OCR Settings and update results screen
Browse files Browse the repository at this point in the history
  • Loading branch information
jlduragos committed Dec 6, 2023
1 parent 417fff7 commit 5d68c6e
Show file tree
Hide file tree
Showing 16 changed files with 904 additions and 113 deletions.
107 changes: 86 additions & 21 deletions app/src/main/java/org/newlogic/smartscanner/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,35 @@ import org.idpass.smartscanner.lib.SmartScannerActivity
import org.idpass.smartscanner.lib.SmartScannerActivity.Companion.SCANNER_FAIL_RESULT
import org.idpass.smartscanner.lib.SmartScannerActivity.Companion.SCANNER_HEADER_RESULT
import org.idpass.smartscanner.lib.SmartScannerActivity.Companion.SCANNER_IMAGE_TYPE
import org.idpass.smartscanner.lib.SmartScannerActivity.Companion.SCANNER_INTENT_EXTRAS
import org.idpass.smartscanner.lib.SmartScannerActivity.Companion.SCANNER_JWT_CONFIG_UPDATE
import org.idpass.smartscanner.lib.SmartScannerActivity.Companion.SCANNER_RAW_RESULT
import org.idpass.smartscanner.lib.SmartScannerActivity.Companion.SCANNER_RESULT
import org.idpass.smartscanner.lib.SmartScannerActivity.Companion.SCANNER_RESULT_BYTES
import org.idpass.smartscanner.lib.SmartScannerActivity.Companion.SCANNER_SETTINGS_CALL
import org.idpass.smartscanner.lib.SmartScannerActivity.Companion.SCANNER_SIGNATURE_VERIFICATION
import org.idpass.smartscanner.lib.nfc.NFCActivity
import org.idpass.smartscanner.lib.scanner.config.*
import org.idpass.smartscanner.lib.scanner.config.Config.Companion.HEIGHT_GUIDE
import org.idpass.smartscanner.lib.scanner.config.Config.Companion.ORIENTATION
import org.idpass.smartscanner.lib.scanner.config.Config.Companion.REGEX
import org.idpass.smartscanner.lib.scanner.config.Config.Companion.WIDTH_GUIDE
import org.idpass.smartscanner.lib.scanner.config.Config.Companion.X_GUIDE
import org.idpass.smartscanner.lib.scanner.config.Config.Companion.Y_GUIDE
import org.idpass.smartscanner.lib.utils.LanguageUtils
import org.newlogic.smartscanner.databinding.ActivityMainBinding
import org.newlogic.smartscanner.result.IDPassResultActivity
import org.newlogic.smartscanner.result.ResultActivity
import org.newlogic.smartscanner.settings.SettingsActivity
import java.util.Locale


class MainActivity : AppCompatActivity() {

companion object {

const val OP_SCANNER = 1001
const val OP_SETTINGS = 1002
private val imageType = ImageResultType.PATH.value

private fun sampleConfig(isManualCapture: Boolean, label: String = "", orientation : String? = Orientation.PORTRAIT.value) = Config(
Expand All @@ -63,13 +73,29 @@ class MainActivity : AppCompatActivity() {

private var preference : SharedPreferences? = null
private lateinit var binding : ActivityMainBinding
private var currentLanguage: String = Language.EN

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
preference = getSharedPreferences(Config.SHARED, Context.MODE_PRIVATE)
currentLanguage = preference?.getString(Language.NAME, Language.EN) ?: Language.EN
LanguageUtils.changeLanguageIfNotMatch(this, currentLanguage)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
preference = getSharedPreferences(Config.SHARED, Context.MODE_PRIVATE)
}

override fun onResume() {
super.onResume()
checkLanguage()
}

private fun checkLanguage() {
//if saved language does not match locale language.
if (LanguageUtils.getLocaleLanguage(this) != Locale(currentLanguage).language) {
//Recreate so that the correct language will reflect on app.
recreate()
}
}

override fun onStart() {
Expand Down Expand Up @@ -101,9 +127,9 @@ class MainActivity : AppCompatActivity() {
SmartScannerActivity.SCANNER_OPTIONS,
ScannerOptions(
mode = Modes.BARCODE.value,
language = getLanguage(preference),
language = getLanguage(),
scannerSize = ScannerSize.SMALL.value,
config = sampleConfig(false),
config = sampleConfig(false).copy(showSettings = true),
barcodeOptions = barcodeOptions
)
)
Expand All @@ -116,9 +142,9 @@ class MainActivity : AppCompatActivity() {
SmartScannerActivity.SCANNER_OPTIONS,
ScannerOptions(
mode = Modes.IDPASS_LITE.value,
language = getLanguage(preference),
language = getLanguage(),
scannerSize = ScannerSize.LARGE.value,
config = sampleConfig(false)
config = sampleConfig(false).copy(showSettings = true)
)
)
startActivityForResult(intent, OP_SCANNER)
Expand All @@ -130,14 +156,15 @@ class MainActivity : AppCompatActivity() {
SmartScannerActivity.SCANNER_OPTIONS,
ScannerOptions(
mode = Modes.MRZ.value,
language = getLanguage(preference),
language = getLanguage(),
config = Config(
branding = true,
imageResultType = imageType,
label = "",
isManualCapture = true,
orientation = getOrientation(preference),
showGuide = true
orientation = getOrientation(),
showGuide = true,
showSettings = true
)
)
)
Expand All @@ -146,20 +173,28 @@ class MainActivity : AppCompatActivity() {

private fun scanOCR() {
val intent = Intent(this, SmartScannerActivity::class.java)
val enableOCRGuide = getWidthGuide() != null && getHeightGuide() != null && getWidthGuide() != 0 && getHeightGuide() != 0
intent.putExtra(
SmartScannerActivity.SCANNER_OPTIONS,
ScannerOptions(
mode = Modes.OCR.value,
language = getLanguage(preference),
language = getLanguage(),
ocrOptions = OCROptions(
analyzeStart = 1000
analyzeStart = 1000,
regex = getRegex()
),
config = Config(
branding = true,
imageResultType = imageType,
label = "",
isManualCapture = false,
orientation = getOrientation(preference)
showOcrGuide = enableOCRGuide,
orientation = getOrientation(),
widthGuide = getWidthGuide() ?: 0,
heightGuide = getHeightGuide() ?: 0,
xGuide = getXGuide(),
yGuide = getYGuide(),
showSettings = true
)
)
)
Expand All @@ -174,15 +209,16 @@ class MainActivity : AppCompatActivity() {
SmartScannerActivity.SCANNER_OPTIONS,
ScannerOptions(
mode = Modes.NFC_SCAN.value,
language = getLanguage(preference),
language = getLanguage(),
nfcOptions = NFCOptions.default,
sentryLogger = SentryLogger.default,
config = Config(
header = getString(R.string.label_scan_nfc_capture),
subHeader = getString(R.string.label_scan_nfc_via_mrz),
isManualCapture = false,
branding = true,
showGuide = true
showGuide = true,
showSettings = true
)
)
)
Expand All @@ -197,9 +233,9 @@ class MainActivity : AppCompatActivity() {
SmartScannerActivity.SCANNER_OPTIONS,
ScannerOptions(
mode = Modes.PDF_417.value,
language = getLanguage(preference),
language = getLanguage(),
scannerSize = ScannerSize.SMALL.value,
config = sampleConfig(false)
config = sampleConfig(false).copy(showSettings = true)
)
)
startActivityForResult(intent, OP_SCANNER)
Expand All @@ -214,20 +250,34 @@ class MainActivity : AppCompatActivity() {
language = preference?.getString(Language.NAME, Language.EN),
scannerSize = ScannerSize.LARGE.value,
qrCodeOptions = QRcodeOptions(isJson = true),
config = sampleConfig(false)
config = sampleConfig(false).copy(showSettings = true)
)
)
startActivityForResult(intent, OP_SCANNER)
}

private fun getOrientation(pref : SharedPreferences?) = pref?.getString(ORIENTATION, Orientation.PORTRAIT.value)
private fun getLanguage(pref : SharedPreferences?) = pref?.getString(Language.NAME, Language.EN)
private fun getOrientation() = preference?.getString(ORIENTATION, Orientation.PORTRAIT.value)
private fun getLanguage() = preference?.getString(Language.NAME, Language.EN)
private fun getRegex() = preference?.getString(REGEX, null)
private fun getWidthGuide() = preference?.getInt(WIDTH_GUIDE, 0)
private fun getHeightGuide() = preference?.getInt(HEIGHT_GUIDE, 0)
private fun getXGuide() = preference?.getFloat(X_GUIDE, 0f)
private fun getYGuide() = preference?.getFloat(Y_GUIDE, 0f)

@SuppressLint("LogNotTimber")
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
super.onActivityResult(requestCode, resultCode, intent)
Log.d(SmartScannerActivity.TAG, "Scanner requestCode $requestCode")
if (requestCode == OP_SCANNER) {
if (requestCode == OP_SETTINGS) {
if (resultCode == RESULT_OK) {
val extras: ScannerOptions? = intent?.getParcelableExtra(SettingsActivity.SCANNER_INTENT_EXTRAS)

val scannerIntent = Intent(this, SmartScannerActivity::class.java)
scannerIntent.putExtra(SmartScannerActivity.SCANNER_OPTIONS, extras)

startActivityForResult(scannerIntent, OP_SCANNER)
}
} else if (requestCode == OP_SCANNER) {
Log.d(SmartScannerActivity.TAG, "Scanner resultCode $resultCode")
if (resultCode == RESULT_OK) {
val bundle = intent?.getBundleExtra(ScannerConstants.RESULT)
Expand All @@ -254,16 +304,31 @@ class MainActivity : AppCompatActivity() {
} else {

// Check if it should go to the settings instead
val isConfigUpdated = intent?.getBooleanExtra(SCANNER_JWT_CONFIG_UPDATE, false)
val isConfigUpdated = intent?.getBooleanExtra(SCANNER_JWT_CONFIG_UPDATE, false) ?: false

// should go to settings
if (isConfigUpdated == true) {
if (isConfigUpdated) {
val sIntent = Intent(this, SettingsActivity::class.java)
sIntent.putExtra(SettingsActivity.CONFIG_UPDATED, true)
startActivity(sIntent)
return
}

val isSettingsCall = intent?.getBooleanExtra(SCANNER_SETTINGS_CALL, false) ?: false

// should go to settings
if (isSettingsCall) {
val extras = intent?.getParcelableExtra<ScannerOptions>(SCANNER_INTENT_EXTRAS)

val sIntent = Intent(this, SettingsActivity::class.java)
sIntent.putExtra(SettingsActivity.SCANNER_INTENT_EXTRAS, extras)
if (intent?.getStringExtra(ScannerConstants.MODE) == Modes.OCR.value) {
sIntent.putExtra(SettingsActivity.OCR_SETTINGS_CALL, true)
}
startActivityForResult(sIntent, OP_SETTINGS)
return
}

// Go to Results Screen
val result = intent?.getStringExtra(SCANNER_RESULT)
val verified = intent?.getBooleanExtra(SCANNER_SIGNATURE_VERIFICATION, false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,70 @@
package org.newlogic.smartscanner.adapters

import android.R.attr.label
import android.R.attr.text
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat.getSystemService
import androidx.recyclerview.widget.RecyclerView
import org.newlogic.smartscanner.R

class RecyclerResultAdapter (private var results: HashMap<String, String>): RecyclerView.Adapter<RecyclerResultAdapter.ViewHolder>() {

inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
class RecyclerResultAdapter(private var results: HashMap<String, String>) :
RecyclerView.Adapter<RecyclerResultAdapter.ViewHolder>() {

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val container: View = itemView.findViewById(R.id.container)
val itemLabel: TextView = itemView.findViewById(R.id.tv_label)
val itemValue: TextView = itemView.findViewById(R.id.tv_value)

// no click listener here
init {
itemValue.setOnClickListener {
val key = it.tag.toString()
val value = results.getValue(key)
val clipboard: ClipboardManager =
it.context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("key", value)
clipboard.setPrimaryClip(clip)

Toast.makeText(it.context, "$key value copied to clipboard", Toast.LENGTH_SHORT)
.show()
}

}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.recycler_result_item, parent, false)
val v = LayoutInflater.from(parent.context)
.inflate(R.layout.recycler_result_item, parent, false)
return ViewHolder(v)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

val keyByIndex = results.keys.elementAt(position)
val valueByKey = results.getValue(keyByIndex)
holder.itemLabel.text = keyByIndex
holder.container.visibility = if (valueByKey.isEmpty()) View.GONE else View.VISIBLE
holder.itemLabel.text = splitCamelCase(keyByIndex)
holder.itemValue.text = valueByKey
holder.itemValue.tag = keyByIndex
}

override fun getItemCount(): Int {
return results.size
}

private fun splitCamelCase(camelCaseString: String): String {
val regex = "(?<=\\p{Ll})(?=\\p{Lu})".toRegex()
val array = regex.split(camelCaseString)
var key = ""
for (a in array) {
key += "$a "
}
return key
}
}

0 comments on commit 5d68c6e

Please sign in to comment.