Configure SDK and handle the result
In this example, we'll use a Fragment to set up the SDK configuration. Add a new Fragment class named ConfigFragment.kt
to handle the configuration process.
Here is the ConfigFragment
that configures the Passio SDK:
package com.passioai.quickstart.ui.config
import ai.passio.passiosdk.core.config.PassioConfiguration
import ai.passio.passiosdk.core.config.PassioMode
import ai.passio.passiosdk.passiofood.PassioSDK
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import com.passioai.quickstart.BuildConfig
import com.passioai.quickstart.databinding.FragmentConfigBinding
class ConfigFragment : Fragment() {
private lateinit var _binding: FragmentConfigBinding
private val binding get() = _binding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentConfigBinding.inflate(inflater, container, false)
return _binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
configureSDK()
}
private fun configureSDK() {
val passioConfiguration = PassioConfiguration(
requireActivity().applicationContext,
BuildConfig.SDK_KEY
).apply {
sdkDownloadsModels = true
debugMode = 0
remoteOnly = true
}
PassioSDK.instance.configure(passioConfiguration) { passioStatus ->
when (passioStatus.mode) {
PassioMode.NOT_READY -> onSDKError("Not ready")
PassioMode.FAILED_TO_CONFIGURE -> onSDKError(getString(passioStatus.error!!.errorRes))
PassioMode.IS_READY_FOR_DETECTION -> onSDKReadyToUse()
PassioMode.IS_BEING_CONFIGURED -> {
binding.tvSDKStatus.text = "Configuring..."
}
PassioMode.IS_DOWNLOADING_MODELS -> {
binding.tvSDKStatus.text = "Downloading models..."
}
}
}
}
private fun onSDKError(message: String) {
binding.tvSDKStatus.text = message
}
private fun onSDKReadyToUse() {
binding.tvSDKStatus.text = "Configured and ready to use"
findNavController().navigate(ConfigFragmentDirections.configToImageRecognitions())
}
}
Key Points to Note
PassioConfiguration:
Set up
PassioConfiguration
with your context and SDK key.Enable
sdkDownloadsModels
to allow the SDK to download necessary models.remoteOnly = true
enables the SDK to access remote resources if required.
PassioMode States: The SDK configuration callback provides different statuses:
NOT_READY: SDK is not ready, usually due to an issue.
FAILED_TO_CONFIGURE: Configuration failed.
IS_READY_FOR_DETECTION: SDK is configured and ready.
IS_BEING_CONFIGURED: SDK is currently being configured.
IS_DOWNLOADING_MODELS: SDK is downloading required models.
Navigation on Ready:
On successful configuration, the app navigates to
ImageRecognitionsFragment
.
Layout Setup
To display the configuration status, add a TextView
(e.g., tvSDKStatus
) in the fragment’s XML layout file.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ui.config.ConfigFragment">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvSDKStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Hello World!" />
</androidx.constraintlayout.widget.ConstraintLayout>
Error Handling
The SDK setup can fail due to configuration or network issues. The onSDKError
function in the ConfigFragment
updates the UI with the relevant error message to help troubleshoot.
Check out the full code here. FULL CODE
Last updated