Skip to content

Commit

Permalink
Update proper usage of annotation and request code callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmangubat23 committed Aug 31, 2022
1 parent 75bdb3e commit 7cde26e
Showing 1 changed file with 33 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,64 @@ package org.idpass.smartscanner.capacitor

import android.app.Activity
import android.content.Intent
import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import com.getcapacitor.annotation.Permission;
import androidx.activity.result.ActivityResult
import com.getcapacitor.JSObject
import com.getcapacitor.Plugin
import com.getcapacitor.PluginCall
import com.getcapacitor.PluginMethod
import com.getcapacitor.annotation.ActivityCallback
import com.getcapacitor.annotation.CapacitorPlugin
import com.google.gson.Gson
import org.idpass.smartscanner.lib.SmartScannerActivity
import org.idpass.smartscanner.lib.scanner.config.ScannerOptions
import org.json.JSONException
import org.json.JSONObject
import timber.log.Timber

@CapacitorPlugin(
name = "SmartScanner",
requestCodes = [SmartScannerPlugin.REQUEST_OP_SCANNER],
permissions = {
@Permission(
strings = { Manifest.permission.ACCESS_NETWORK_STATE },
alias = "network"
),
@Permission(strings = { Manifest.permission.INTERNET }, alias = "internet"),
@Permission(
strings = { Manifest.permission.CAMERA },
alias = "camera"
)
}
)
@CapacitorPlugin(name = "SmartScanner")
class SmartScannerPlugin : Plugin() {

companion object {
const val REQUEST_OP_SCANNER = 1001
}

@PluginMethod
fun executeScanner(call: PluginCall) {
val action = call.getString("action")
val options: JSONObject = call.getObject("options")
saveCall(call)
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, REQUEST_OP_SCANNER)
startActivityForResult(call, intent, "requestScannerResult")
} else {
call.reject("\"$action\" is not a recognized action.")
}
}

override fun handleOnActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.handleOnActivityResult(requestCode, resultCode, data)
val savedCall = savedCall ?: return
if (requestCode == REQUEST_OP_SCANNER) {
try {
Timber.d("Plugin post SmartScannerActivity resultCode %d", resultCode)
if (resultCode == Activity.RESULT_OK) {
val returnedResult = data?.getStringExtra(SmartScannerActivity.SCANNER_RESULT)
Timber.d("Plugin post SmartScannerActivity result %s", returnedResult)
try {
if (returnedResult != null) {
val result = JSONObject(returnedResult)
val ret = JSObject()
ret.put(SmartScannerActivity.SCANNER_RESULT, result)
savedCall.resolve(ret)
} else {
savedCall.reject("Scanning result is null.")
}
} catch (e: JSONException) {
e.printStackTrace()
@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.")
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Timber.d("Plugin post SmartScannerActivity RESULT CANCELLED")
savedCall.reject("Scanning Cancelled.")
} else {
savedCall.reject("Scanning Failed.")
} catch (e: JSONException) {
e.printStackTrace()
}
} catch (exception: Exception) {
Timber.e(exception)
exception.printStackTrace()
} else {
Timber.d("SmartScanner: RESULT SCANNING FAILED")
call.reject("Scanning Failed.")
}
} else {
savedCall.reject("Unknown Request Code!")
}
}
}
}

0 comments on commit 7cde26e

Please sign in to comment.