Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagekotlin
String action = intent.getAction();
if (Intent.ACTION_VIEW.equals(action) || Intent.ACTION_SEND.equals(action)) {
    ...
}

Launch the SDK as an activity

If the Intent passes the above check then register an activity result handler with the CaptureFlowImportContract() and pass the Intent to GiniBank.startCaptureFlowFortentstartCaptureFlowForIntent():

Code Block
languagenone
fun configureCapture() {
  GiniBank.setCaptureConfiguration(this,
    CaptureConfiguration(
      fileImportEnabled = true,
      ... // other configuration options
    )
  )
}

// Use the androidx's Activity Result API to register a handler for the capture result.
val captureImportLauncher = registerForActivityResult(CaptureFlowImportContract()) { result: CaptureResult ->
    when (result) {
        is CaptureResult.Success -> {
            handleExtractions(result.specificExtractions)
        }
        is CaptureResult.Error -> {
            when (result.value) {
                is ResultError.Capture -> {
                    val captureError: GiniCaptureError = (result.value as ResultError.Capture).giniCaptureError
                    handleCaptureError(captureError)
                }
                is ResultError.FileImport -> {
                    val fileImportError = result.value as ResultError.FileImport
                    handleFileImportError(fileImportError)
                }
            }
        }
        CaptureResult.Empty -> {
            handleNoExtractions()
        }
        CaptureResult.Cancel -> {
            handleCancellation()
        }
    }
}

fun handleFileImportError(exception: ImportedFileValidationException) {
    var message = ...
    exception.validationError?.let { validationError ->
        // Get the default message
        message = getString(validationError.textResource)
        // Or use custom messages
        message = when (validationError) {
            FileImportValidator.Error.TYPE_NOT_SUPPORTED -> ...
            FileImportValidator.Error.SIZE_TOO_LARGE -> ...
            FileImportValidator.Error.TOO_MANY_PDF_PAGES -> ...
            FileImportValidator.Error.PASSWORD_PROTECTED_PDF -> ...
            FileImportValidator.Error.TOO_MANY_DOCUMENT_PAGES -> ...
        }
    }
    AlertDialog.Builder(this)
        .setMessage(message)
        .setPositiveButton("OK") { _, _ -> finish() }
        .show()
}

fun startGiniBankSDKForImportedFile(importedFileIntent: Intent) {
    // Configure capture first
    configureCapture();

    fileImportCancellationToken =
        GiniBank.startCaptureFlowForIntent(captureImportLauncher, this, importedFileIntent)
}

Launch the SDK from a different activity

When your activity (declaring the intent filter shown above) is not the same as the activity from which you launch the Gini Bank SDK you need to create a Document from the intent first and then pass it to your other activity as an extra. There you can then launch the SDK with the Document.

...

  1. Get the Document from the intent extras in your activity B and launch the Gini Bank SDK:

    Code Block
    languagekotlin
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    
        if (savedInstanceState == null) {
            if (intent.hasExtra(EXTRA_IN_OPEN_WITH_DOCUMENT)) {
                IntentCompat.getParcelableExtra(intent, EXTRA_IN_OPEN_WITH_DOCUMENT, Document::class.java)?.let {
                    GiniBank.startCaptureFlowForDocument(
                                resultLauncher = captureImportLauncher,
                                document = document
                            )
                }
            }
        }
    }

Launch the SDK as a fragment

If the Intent passes the check for ACTION_VIEW and ACTION_SEND, then pass the Intent to GiniBank.createCaptureFlowFragmentForIntent():

Code Block
languagekotlin
fun configureCapture() {
  GiniBank.setCaptureConfiguration(this,
    CaptureConfiguration(
      fileImportEnabled = true,
      ... // other configuration options
    )
  )
}

fun startGiniBankSDKForImportedFile(importedFileIntent: Intent) {
    // Configure capture first
    configureCapture();

    fileImportCancellationToken =
        GiniBank.createCaptureFlowFragmentForIntent(this /* activity context */, importedFileIntent) { result ->
            when (result) {
                GiniBank.CreateCaptureFlowFragmentForIntentResult.Cancelled -> {
                    handleCancellation()
                }
                is GiniBank.CreateCaptureFlowFragmentForIntentResult.Error -> {
                    handleFileImportError(result.exception)
                }

                is GiniBank.CreateCaptureFlowFragmentForIntentResult.Success -> {
                    // Opening the file(s) from the intent and creating the CaptureFlowFragment
                    
                    // Set the listener to receive the Gini Bank SDK's results
                    result.fragment.setListener(this)
                    
                    // Show the CaptureFlowFragment for example via the fragment manager:
                    requireActivity().supportFragmentManager.beginTransaction()
                        .replace(R.id.fragment_container, result.fragment, "CaptureFlowFragment")
                        .addToBackStack(null)
                        .commit()
                }
            }
        }
}

// Handle the results from Gini Bank SDK
override fun onFinishedWithResult(result: CaptureResult) {
    when (result) {
        is CaptureResult.Success -> {
            // Handle extraction results (to proceed with the transaction)
            handleExtractions(result.specificExtractions)
            // After the user has seen (and maybe edited) the extractions and has executed the transfer 
            // please send the transfer summary to Gini with the final data approved by the user.
        }
        is CaptureResult.Error -> {
            when (result.value) {
                is ResultError.Capture ->
                    // There was a capture error
                    val captureError = result.value as ResultError.Capture
                    handleCaptureError(captureError)
                is ResultError.FileImport ->
                    // There was a file import error
                    val fileImportError = result.value as ResultError.FileImport
                    handleFileImportError(fileImportError)
            }
        }
        CaptureResult.Empty -> {
            // Handle empty result
            handleNoExtractions()
        }
        CaptureResult.Cancel -> {
            // Process was cancelled by user
            handleCancellation()
        }
        CaptureResult.EnterManually -> {
            // User wants to enter the invoice data manually
            handleEnterManually()
        }
    }
}

fun handleFileImportError(exception: ImportedFileValidationException) {
    var message = ...
    exception.validationError?.let { validationError ->
        // Get the default message
        message = getString(validationError.textResource)
        // Or use custom messages
        message = when (validationError) {
            FileImportValidator.Error.TYPE_NOT_SUPPORTED -> ...
            FileImportValidator.Error.SIZE_TOO_LARGE -> ...
            FileImportValidator.Error.TOO_MANY_PDF_PAGES -> ...
            FileImportValidator.Error.PASSWORD_PROTECTED_PDF -> ...
            FileImportValidator.Error.TOO_MANY_DOCUMENT_PAGES -> ...
        }
    }
    AlertDialog.Builder(this)
        .setMessage(message)
        .setPositiveButton("OK") { _, _ -> finish() }
        .show()
}

Detect when your app is the default app for opening images or PDF files

...