Android

This section describes how to integrate DriveKit into an Android mobile application.

Requirements

DriveKit is developed with the Kotlin language and is compatible with Android 6.0 (API 23) and later versions.

We strongly recommend to configure (minSdkVersion) your project app to support Android 8.0 (API 26) or later version as DriveKit SDK will no longer support Android 6 (API 23) and Android 7.x (API 24 and 25) from mid-2024.

The latest DriveKit SDK version is configured with these attributes:

Android SDK version propertyAPI level

minSdkVersion

23 (Android 6)

compileSdkVersion

34 (Android 14)

targetSdkVersion

34 (Android 14)

Please ensure that others libraries integrated in your project are compatible with these versions ; for example if you use a library to manage runtime permissions.

DriveKit uses the libraries listed below. These are minimal required versions. Check that they are compatible with your app:

LibraryVersion

Kotlin

1.8.22

Java

17

Gson

2.10.1

Volley

1.2.1

Room

2.5.2

Work Manager

2.9.0

Play Services Location

21.0.1

DriveKit uses key-value backup. If you use Auto Backup, you need to add android:fullBackupOnly in your Manifest.

Integration

DriveKit repository

DriveKit SDK is available on the DriveQuant Maven repository.

To access modules in the repository, add the following lines to your project build.gradle file:

allprojects {
    repositories {
        maven {
            url "https://maven.drivequant.com/repository/android-sdk/"
        }
    }
}

Get module from repository

To add DriveKit Core module to your app, add the following lines to your dependencies in your application build.gradle file:

dependencies {
    implementation 'com.drivequant.drivekit:drivekit-core:$drivekit_version'
}

Replace $drivekit_version with the DriveKit version you are using in your app.

You can find the latest release version of DriveKit on the changelog page.

DriveKit initialization

To initialize DriveKit in your app, you must call the initialization method in onCreate method of your application class.

The initialization method requires an Application as first parameter and an optional implementation of DriveKitListener:

class MyApplication: Application() {

    override fun onCreate() {
        super.onCreate()
        DriveKit.initialize(application, object : DriveKitListener {
            override fun onConnected() {
                // Connected to DriveKit
            }

            override fun onDisconnected() {
                // Disconnected from DriveKit
            }

            override fun onAuthenticationError(errorType: RequestError) {
                // DriveKit authentication error
            }

            override fun userIdUpdateStatus(status: UpdateUserIdStatus, userId: String?) {
                // DriveKit userId updated
            }
            
            override fun onAccountDeleted(status: DeleteAccountStatus) {
                // Remind to check the delete account status value
            }
        })
    }
}

All DriveKit modules include a method initialize that must be called in onCreate method of your application class.

Add API key

To use DriveKit modules, you have to obtain an API Key from DriveQuant. If you don't have an API key, please contact DriveQuant.

Once you've stored your API key in a secure way in your application, you can configure DriveKit by calling the following method:

DriveKit.setApiKey("MyAPIKey")

Identify user

Each driver must be identified with a unique identifier. Once you have this identifier, configure DriveKit by calling the following method:

DriveKit.setUserId("MyUserId")

You can call these 2 configuration methods anywhere in the code. DriveKit will save the value locally. If the app is killed and relaunched, DriveKit will be reconfigured automatically.

We recommend never using an email address or phone number to define the unique user ID. It is recommended that you set up a unique, universal and anonymous user ID. For example, you can generate a globally unique identifier (GUID) for each of your users.

DriveKit SDK will not work until you set the API key and the userId.

Check your configuration

You can check if DriveKit is well configured with the following method.

DriveKit.isConfigured()

This method returns true if DriveKit is well configured.

All DriveKit modules include a method called "isConfigured" that checks the minimum system requirements for each module.

Reset

If you need to reset DriveKit configuration (user logout for example), you can call the following method:

DriveKit.reset()

All data saved locally by DriveKit will be erased.

All DriverKit modules have reset method that erases all data saved locally by the module.

Make sure that you call reset method of all modules to fully reset DriveKit configuration.

Logging

DriveKit comes with a logging feature that is enabled by default. This feature allows you to quickly identify the cause of a problem. We recommend leaving the log enabled as it does not consume memory space and is useful in the support phase. However, if you don't want to use it, it can be disabled.

You can retrieve the last Uri log file by calling the following method:

DriveKitLog.getLogUriFile(context)

If your device version is on Android 10 or below, you can directly find the log file in Android/data/<your-app-package-name>/files/<path-to-my-log-directory>

If your device version is on Android 11 and if you have The Permissions Utils component on your app, you can get a log file of the previous month and the current one with the method getZippedLogUriFiles(), or by clicking on β€œContact support” and change the email receiver. The file will be in attachment of the email.

Disable logging by calling:

DriveKit.disableLogging()

To enable logging, call the following method specifying the path of the log directory.

DriveKit.enableLogging("/path/to/my/log/directory")

Listen for permissions and sensors status changes

If the user disables the sensors or revokes permissions, the application would not be able to detect and record a trip. To avoid this, the SDK identifies important sensor state and permission changes.

These events are shared when the user is logged in and you can use them in your application to inform the user via a visual alert or by displaying a notification.

Event changes callbacks may not be fired in real time due to technical restrictions of Android.

DKDeviceConfigurationListener

DKDeviceConfigurationListener is the interface used to get callbacks when device configuration changes are detected:

interface DKDeviceConfigurationListener {
    fun onDeviceConfigurationChanged(event: DKDeviceConfigurationEvent)
}

Add a listener

To add a listener and get informed for device configuration events, you can call the following method:

DriveKit.addDeviceConfigurationListener(this)

Remove a listener

To remove a specific listener, call the following method:

DriveKit.removeDeviceConfigurationListener(this)

Remove all listeners

To remove all listeners, call the following method:

DriveKit.removeAllDeviceConfigurationListeners(this)

DKDeviceConfigurationEvent

DKDeviceConfigurationEvent is a sealed class that describes a device configuration change event:

sealed class DKDeviceConfigurationEvent {
	data class LocationPermission(val isValid: Boolean) : DKDeviceConfigurationEvent()
	data class ActivityPermission(val isValid: Boolean) : DKDeviceConfigurationEvent()
	data class NearbyDevicesPermission(val isValid: Boolean) : DKDeviceConfigurationEvent()
	data class NotificationPermission(val isValid: Boolean) : DKDeviceConfigurationEvent()
	data class AutoResetPermission(val isValid: Boolean) : DKDeviceConfigurationEvent()
	data class AppBatteryOptimisation(val isValid: Boolean) : DKDeviceConfigurationEvent()
	data class LocationSensor(val isValid: Boolean) : DKDeviceConfigurationEvent()
	data class BluetoothSensor(val isValid: Boolean) : DKDeviceConfigurationEvent()
}

Possible events are:

ValueDescription

LocationPermission

Location permission status changed

ActivityPermission

Activity Recognition permission status changed

NearbyDevicesPermission

Nearby Devices permission status changed

NotificationPermission

Notification permission status changed

AutoResetPermission

Auto-reset permission status changed

AppBatteryOptimization

Battery Optimization app status changed

LocationSensor

Location sensor status changed

BluetoothSensor

Bluetooth sensor status changed

The DriveKit SDK will run optimally if the isValid value of each event is true. The table below explains the impact of a status that is not true.

EventCriticalityConsequence if value is false

LocationPermission

πŸ”΄

No trip recording if app has no access to GPS sensor

ActivityPermission

🟑

Poor trip detection

NearbyDevicesPermission

🟑

No trip detection based on beacon or Bluetooth system

NotificationPermission

🟒

Your application cannot send notification to the user if the permission is revoked.

AutoResetPermission

🟒

Revocation of all permissions if app is not opened for 3 months.

AppBatteryOptimization

🟑

Poor trip detection, application may be killed in background

LocationSensor

πŸ”΄

No trip recording if GPS sensor is disabled

BluetoothSensor

🟑

No trip detection based on beacon or Bluetooth system

Get user’s information

To get the user's information (first name, last name and pseudo), call the getUserInfo method. This method will retrieve and save these data locally:

DriveKit.getUserInfo(object : GetUserInfoQueryListener {
   override fun onResponse(status: UserInfoGetStatus, userInfo: UserInfo?) {
       if (status == UserInfoGetStatus.SUCCESS) {
           // Get user's names in userInfo object.
       }
   }
}, synchronizationType = SynchronizationType.DEFAULT)

Update user’s information

You can add information to a user's account such as first name, last name and pseudo. These details are optional and you can choose to make the user's account anonymous. To update the user's information, you must call the updateUserInfo method :

DriveKit.updateUserInfo(pseudo = "New_pseudo", listener = object : UpdateUserInfoQueryListener {
   override fun onResponse(status: Boolean) {
       if (status) {
           // The pseudo has been successfully updated.
       }
   }
})

Or to update all information:

DriveKit.updateUserInfo(
   "New_firstname",
   "New_lastname",
   "New_pseudo",
   object : UpdateUserInfoQueryListener {
       override fun onResponse(status: Boolean) {
           if (status) {
               // The firstname, lastname and pseudo have been successfully updated.
           }
       }
   })

Update UserId

It is possible to update the userId by calling the following method:

DriveKit.updateUserId("newUserId")

To be able to check whenever userId got updated and catch the update status you have to use DriveKitListener listener.

Account deletion

You can delete a driver's account in DriveKit. This action deletes all the data related to the account.

The deletion can be done instantly or with delay.

  • In the first case, when the method is called, the account is instantly deleted.

  • In the second case, the driver has 30 days to log back into the application and reactivate his account.

To delete a driver's account, use the following method:

DriveKit.deleteAccount(instantDeletion: Boolean = false)

instantDeletion can have 2 values:

  • false : Default value, allows the user to recover the deleted account by logging-in again with the same credentials. Users have 30 days starting from the day when the account was deleted.

  • true : Allow to delete an account instantly. The account and all the related data will be immediately deleted and no rollback is possible.

Your team needs to have the deletion feature activated to use this method. Please contact DriveQuant if you need it.

To be able to check whenever the account deletion is complete, you have to use the DriveKitListener interface.

You should restore the DriveKit API key in the onAccountDeleted() callback only when the status value is SUCCESS.

Last updated