Skip to content

Commit

Permalink
Add trycatch, update smartscanner to 0.4.5 (#23)
Browse files Browse the repository at this point in the history
Co-authored-by: Tai Pham <tai@newlogic.com>
  • Loading branch information
megaxayda and Tai Pham authored Mar 3, 2023
1 parent 51e7ab0 commit 50bda98
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
Binary file modified android/libs/smartscannerlib-debug.aar
Binary file not shown.
Binary file modified android/libs/smartscannerlib-release.aar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Activity
import android.content.Intent
import androidx.activity.result.ActivityResult
import com.getcapacitor.JSObject
import com.getcapacitor.Logger
import com.getcapacitor.Plugin
import com.getcapacitor.PluginCall
import com.getcapacitor.PluginMethod
Expand All @@ -21,45 +22,54 @@ class SmartScannerPlugin : Plugin() {

@PluginMethod
fun executeScanner(call: PluginCall) {
val action = call.getString("action")
val options: JSONObject = call.getObject("options")
if (action == "START_SCANNER") {
Timber.d("executeScanner %s", action)
val intent = Intent(context, SmartScannerActivity::class.java)
val scannerOptions = Gson().fromJson(options.toString(), ScannerOptions::class.java)
intent.putExtra(SmartScannerActivity.SCANNER_OPTIONS, scannerOptions)
startActivityForResult(call, intent, "requestScannerResult")
} else {
call.reject("\"$action\" is not a recognized action.")
try {
val action = call.getString("action")
val options: JSONObject = call.getObject("options")
if (action == "START_SCANNER") {
Timber.d("executeScanner %s", action)
val intent = Intent(context, SmartScannerActivity::class.java)
val scannerOptions = Gson().fromJson(options.toString(), ScannerOptions::class.java)
intent.putExtra(SmartScannerActivity.SCANNER_OPTIONS, scannerOptions)
startActivityForResult(call, intent, "requestScannerResult")
} else {
call.reject("\"$action\" is not a recognized action.")
}
} catch (e: Exception) {
call.reject(e.toString() ,e)
}
}

@ActivityCallback
private fun requestScannerResult(call: PluginCall, result: ActivityResult) {
if (result.resultCode == Activity.RESULT_CANCELED) {
Timber.d("SmartScanner: RESULT CANCELLED")
call.reject("Scanning Cancelled.")
} else {
if (result.resultCode == Activity.RESULT_OK) {
val returnedResult = result.data?.getStringExtra(SmartScannerActivity.SCANNER_RESULT)
try {
if (returnedResult != null) {
Timber.d("SmartScanner: RESULT OK -- $returnedResult")
val scannerResult = JSONObject(returnedResult)
val ret = JSObject()
ret.put(SmartScannerActivity.SCANNER_RESULT, scannerResult)
call.resolve(ret)
} else {
Timber.d("SmartScanner: RESULT SCANNING NULL")
call.reject("Scanning result is null.")
try {
if (result.resultCode == Activity.RESULT_CANCELED) {
Timber.d("SmartScanner: RESULT CANCELLED")
call.reject("Scanning Cancelled.")
} else {
if (result.resultCode == Activity.RESULT_OK) {
val returnedResult = result.data?.getStringExtra(SmartScannerActivity.SCANNER_RESULT)
try {
if (returnedResult != null) {
Timber.d("SmartScanner: RESULT OK -- $returnedResult")
val scannerResult = JSONObject(returnedResult)
val ret = JSObject()
ret.put(SmartScannerActivity.SCANNER_RESULT, scannerResult)
call.resolve(ret)
} else {
Timber.d("SmartScanner: RESULT SCANNING NULL")
call.reject("Scanning result is null.")
}
} catch (e: JSONException) {
e.printStackTrace()
call.reject(e.toString() ,e)
}
} catch (e: JSONException) {
e.printStackTrace()
} else {
Timber.d("SmartScanner: RESULT SCANNING FAILED")
call.reject("Scanning Failed.")
}
} else {
Timber.d("SmartScanner: RESULT SCANNING FAILED")
call.reject("Scanning Failed.")
}
} catch (e: Exception) {
call.reject(e.toString() ,e)
}
}
}

0 comments on commit 50bda98

Please sign in to comment.