Copy class MainActivity : ComponentActivity() {
private lateinit var sdk: LocatorSDK
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Configure and start the SDK
setupLocatorSDK()
}
private fun setupLocatorSDK() {
// 1. Get SDK instance
LocatorSDK.getInstance()
.onSuccess { instance ->
sdk = instance
// 2. Register custom integrator (optional)
registerCustomIntegration()
// 3. Configure the SDK
configureSDK()
// 4. Check permissions before starting
checkPermissionsAndStart()
}
.onFailure { exception ->
when (exception) {
is LocatorSDKNotInitializedException -> {
Log.e("LocatorSDK", "SDK not initialized. Initialize in Application.")
// Try to initialize here if necessary
LocatorSDK.initialize(initContext = this)
}
else -> {
Log.e("LocatorSDK", "Error getting instance: ${exception.message}")
}
}
}
}
private fun registerCustomIntegration() {
// Register custom integrator (optional)
// If not registered, DefaultLocatorSDKIntegrationApiImpl will be used
sdk.registerIntegration(
integration = object : LocatorIntegration {
override suspend fun getCert(payload: LocatorRequestApiCert): LocatorResponseApiCert {
// Custom implementation to get certificate
return yourApiService.getCert(payload)
}
override suspend fun getToken(payload: LocatorRequestApiToken): LocatorResponseApiToken {
// Custom implementation to get token
return yourApiService.getToken(payload)
}
override suspend fun getScopes(payload: LocatorRequestApiScopes): LocatorResponseApiScopes {
return yourApiService.getScopes(payload)
}
override suspend fun getFeatures(payload: LocatorRequestApiFeatures): LocatorResponseApiFeatures {
return yourApiService.getFeatures(payload)
}
override suspend fun getConfig(payload: LocatorRequestApiConfig): LocatorResponseApiConfig {
return yourApiService.getConfig(payload)
}
override suspend fun getGroups(payload: LocatorRequestApiGroups): LocatorResponseApiGroups {
return yourApiService.getGroups(payload)
}
override suspend fun getGeofences(payload: LocatorRequestApiGeofenses): LocatorResponseApiGeofenses {
return yourApiService.getGeofences(payload)
}
}
)
}
private fun configureSDK() {
// Create complete configuration
val config = LocatorConfig(
license = "LICENSE_12345",
sdkVersion = BuildConfig.LIBRARY_VERSION,
osPlatform = OS_PLATFORM_ANDROID,
mqtt = LocatorMqttConfig(
clientId = "android-${System.currentTimeMillis()}",
broker = "mqtt.yourserver.com",
port = "8883",
username = "mqtt-username"
),
api = LocatorApiConfig(
token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
certUrl = "https://api.yourserver.com/v1/cert",
scopesUrl = "https://api.yourserver.com/v1/scopes",
tokenUrl = "https://api.yourserver.com/v1/token",
configUrl = "https://api.yourserver.com/v1/config",
groupsUrl = "https://api.yourserver.com/v1/groups",
featuresUrl = "https://api.yourserver.com/v1/features",
geofencesUrl = "https://api.yourserver.com/v1/geofences"
),
process = LocatorProcessConfig(
retryPolicy = LocatorRetryPolicy(
maxRetries = 5,
baseDelayMs = 2000L,
backoffFactor = 1.5
),
offlineRetentionDays = 10,
foregroundServiceNotification = ForegroundServiceNotification(
title = "Location",
message = "Collecting location data"
)
),
battery = LocatorBatteryConfig(
events = listOf(
LocatorBatteryEvent(
name = "battery_low",
min = 0,
max = 15,
interval = 1800000L, // 30 minutes
charging = false,
powerMode = listOf(LocatorPowerMode.NORMAL, LocatorPowerMode.LOW_POWER)
),
LocatorBatteryEvent(
name = "battery_charging",
min = 80,
max = 100,
interval = 600000L, // 10 minutes
charging = true,
powerMode = listOf(LocatorPowerMode.NORMAL)
)
)
),
motion = LocatorMotionConfig(
sensitivity = 0.7
),
collect = LocatorCollectConfig(
collectIntervalMillis = 15000L, // 15 seconds
sendIntervalMillis = 30000L, // 30 seconds
minDisplacementMeters = 5.0,
maxTravelDistanceMeters = 500.0,
highAccuracy = false,
maxBatchSize = 100
)
)
// Apply configuration
sdk.setConfig(config = config)
}
private fun checkPermissionsAndStart() {
// Check pending permissions
val pendingPermissions = sdk.pendingPermissions()
if (pendingPermissions.isEmpty()) {
// All permissions granted, can start
startSDK()
} else {
// Request missing permissions
requestPermissions(pendingPermissions)
}
}
private fun requestPermissions(permissions: List<String>) {
// Implement permission request logic
// After granting permissions, call startSDK()
ActivityCompat.requestPermissions(
this,
permissions.toTypedArray(),
PERMISSION_REQUEST_CODE
)
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == PERMISSION_REQUEST_CODE) {
val allGranted = grantResults.all { it == PackageManager.PERMISSION_GRANTED }
if (allGranted) {
startSDK()
} else {
Log.w("LocatorSDK", "Permissions denied. SDK cannot start.")
}
}
}
private fun startSDK() {
try {
// IMPORTANT: Before initializing the SDK, it is necessary to set the SDK state
// To enable SDK functionality for users with the feature
// Call the setState method with LocatorState.IDLE value before calling start()
sdk.setState(LocatorState.IDLE)
sdk.start()
Log.d("LocatorSDK", "SDK started successfully!")
} catch (e: LocatorSDKMissingPermissionsException) {
Log.e("LocatorSDK", "Missing permissions: ${e.message}")
// Request permissions again
checkPermissionsAndStart()
} catch (e: LocatorSDKNoConfigSetException) {
Log.e("LocatorSDK", "Configuration not set: ${e.message}")
// Configure again
configureSDK()
} catch (e: Exception) {
Log.e("LocatorSDK", "Error starting SDK: ${e.message}")
}
}
companion object {
private const val PERMISSION_REQUEST_CODE = 1001
}
}