Quick Start

Getting Started / Android

Setup the MOVE SDK

The Android MOVE SDK must be initialized by adding MoveSdk.init() at the first line in your Android Application class (onCreate). This instance can then be used to control MOVE SDK services (check API Interface for more details).

After the MOVE SDK has been initialized you also want to add notifications, listeners or activate additional features.

Also, you have to configure the MOVE SDK before you can call .setup(...).

To successfully initialize and run the MOVE SDK, the user's MoveAuth must already be fetched with the MOVE Admin API and the required permissions must be granted (Permission Handling).

If the provided MoveAuth is invalid, the MOVE SDK will not initialize.

If the required permissions are missing, the MOVE SDK State will change to READY. Additionally, MoveServiceFailure and/or MoveServiceWarning will present the missing permission.

The initialization of the MOVE SDK is asynchronous and you will be notified regarding state changes with the MoveStateListener. On successful initialization the MoveSdkState changes to READY.

To be able to use the MOVE SDK, several permissions need to be granted. This is described in Permission Handling.

Now that there is an instance of the MOVE SDK, the detection may be started with startAutomaticDetection(), which should lead to the state RUNNING of the MOVE SDK.

Since v2.7.1: if the MOVE SDK trip detection was started with startAutomaticDetection() then it is not possible to manual start a trip with startTrip(...).

The MOVE SDK is collecting data only when inRUNNINGstate.

Code example

package com.example.movesdkandroid

import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.util.Log
import androidx.annotation.MainThread
import io.dolphin.move.DrivingService
import io.dolphin.move.MoveAuth
import io.dolphin.move.MoveAuthState
import io.dolphin.move.MoveConfig
import io.dolphin.move.MoveConfigurationError
import io.dolphin.move.MoveDetectionService
import io.dolphin.move.MoveNotification
import io.dolphin.move.MoveSdk
import io.dolphin.move.WalkingService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking


class MoveSDKTest: Application() {

    override fun onCreate() {
        // init the MOVE SDK
        MoveSdk.init(this)

        super.onCreate()

        setupSdk()
    }



    private fun setupSdk() = runBlocking {

        // Use the stored MoveAuth of this user or fetch it from the backend.
        launch(Dispatchers.IO) {
            // TODO: Please replace YourStorage with your preferred secure MoveAuth storage (e.g. database, shared prefs, ...)
            var moveAuthOfThisUser = YourStorage.moveAuth

            if (moveAuthOfThisUser.isNullOrBlank()) {
                // No MoveAuth available -> fetch it from the backend.

                // Source code to fetch MoveAuth object.
                // PLEASE MAKE SURE TO DO THE API REQUEST TO GET THE RESPONSE
                // https://docs.movesdk.com/move-platform/backend/move-backend/move-admin-api

                // Response from the API Request.
                // {
                //  "accessToken": "string",
                //  "refreshToken": "string",
                //  "userId": "string",
                //  "projectId": 0
                //  ...
                // }

                moveAuthOfThisUser = MoveAuth(
                    projectId = projectId ?: 0L,
                    userId = userId ?: "",
                    accessToken = accessToken ?: "",
                    refreshToken = refreshToken ?: ""
                )
            }

            launch(Dispatchers.Main) {
                prepareAndConfigSdk(moveAuthOfThisUser)
            }
        }
    }


    // Now we can prepare the notifications, listeners and config the MOVE SDK.
    @MainThread
    private fun prepareAndConfigSdk(moveAuth: MoveAuth) {

        val channelId = "channel-sdk"

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val sdkChannel = NotificationChannel(channelId, "Dolphin SDK", NotificationManager.IMPORTANCE_DEFAULT)
        notificationManager.createNotificationChannel(sdkChannel)

        // Replace strings and icons as desired.
        MoveSdk.get()?.recognitionNotification(
            MoveNotification(
                channelId,
                drawableId = R.drawable.ic_recognition_notification,
                contentTitle = getString(R.string.app_name),
                contentText = "Active",
                showWhen = true,
            )
        )
        MoveSdk.get()?.tripNotification(
            MoveNotification(
                channelId,
                drawableId = R.drawable.ic_trip_notification,
                contentTitle = getString(R.string.app_name),
                contentText = "Driving",
                showWhen = true,
            )
        )

        // Examples of some listeners
        val initListener = object : MoveSdk.InitializeListener {
            override fun onError(error: MoveConfigurationError) {
                Log.d("MoveConfigurationError", "onError: $error")
            }
        }

        val authStateListener = object : MoveSdk.AuthStateUpdateListener {
            // Triggers whenever the MoveAuthState changes.
            override fun onAuthStateUpdate(state: MoveAuthState) {
                when (state) {
                    is MoveAuthState.INVALID -> {
                        // The SDK authorization state when SDK is uninitialized.
                    }
                    else -> {

                    }
                }
            }
        }

        // Example of adding some notifications, register listener
        // and activate MOVE SDK features.
        MoveSdk.get()?.apply {
            // Please make sure to handle the authentication states.
            authStateUpdateListener(authStateListener)
            initializationListener(initListener)

            // MORE INFORMATION'S ABOUT LISTENERS
            // https://docs.movesdk.com/move-platform/sdk/models/listeners-callbacks

//            tripNotification(drivingNotification)
//            sdkStateListener(sdkStateListener)
//            tripStateListener(tripStateListener)
//            initiateAssistanceCall(assistanceListener)
//            recognitionNotification(recognitionNotification)

            consoleLogging(true)
        }

        // Configuration of the MOVE SDK
        val moveServices: MutableSet<MoveDetectionService> = mutableSetOf()

        val drivingServices: MutableSet<DrivingService> = mutableSetOf()
        drivingServices.add(DrivingService.DrivingBehaviour)
        drivingServices.add(DrivingService.DistractionFreeDriving)
        drivingServices.add(DrivingService.DeviceDiscovery)

        val walkingServices: MutableSet<WalkingService> = mutableSetOf()
        walkingServices.add(WalkingService.Location)

        moveServices.add(MoveDetectionService.Driving(drivingServices = drivingServices.toList()))
        moveServices.add(MoveDetectionService.Walking(walkingServices = walkingServices.toList()))
        moveServices.add(MoveDetectionService.Cycling)
        moveServices.add(MoveDetectionService.AutomaticImpactDetection)
        moveServices.add(MoveDetectionService.AssistanceCall)

        val moveConfig = MoveConfig(moveDetectionServices = moveServices.toList())

        MoveSdk.get()?.setup(
            auth = moveAuth,
            config = moveConfig,
            start = true
        )
    }

}

You must change the following variables with your values: projectId, userId, accessToken, and refreshToken.

Last updated

Dolphin Technologies GmbH