Comment on page
Sentiance
The Sentiance class is the main entry point for interacting with the SDK. This class is a singleton, therefore you can obtain an instance as follows:
Sentiance sentianceSDK = Sentiance.getInstance(context);
With the exception of the following methods, all other methods in this class will throw an
SDKException
exception when invoked before SDK initialization is complete. Seeinitialize(SentianceOptions)
.initialize(SentianceOptions)
initialize()
init(SdkConfig, OnInitCallback)
- DeprecateduserExists()
isUserLinked()
reset()
reset(ResetCallback)
- DeprecatedgetInitState()
getVersion()
void addUserMetadataField(String label, String value)Adds a user specific metadata field.
Parameters | Text |
---|---|
label | Metadata label to add. |
value | Metadata value. |
void addUserMetadataFields(Map<String, String> metadata)Adds multiple user specific metadata fields.
Parameters | Text |
---|---|
metadata | The metadata fields. |
boolean addTripMetadata(Map<String, String> metadata)Adds metadata while an external trip is ongoing. This method can be called multiple times during a trip.This method returns false if there is no ongoing external trip ornull
metadata is passed.
Parameters | Text |
---|---|
metadata | A map of metadata pertaining to the ongoing trip, which will be saved along with the trip data. |
PendingOperation<UserCreationResult, UserCreationError> createUser(UserCreationOptions options)Creates a Sentiance user if one does not yet exist.Most SDK functionality requires a Sentiance user to be present on the device. Therefore, create a user first before enabling detections or interacting with other SDK features.
Parameters | Text |
---|---|
options |
void disableBatteryOptimization()Generates a system dialog asking the user to allow disabling battery optimization for the app.
PendingOperation<DisableDetectionsResult, DisableDetectionsError> disableDetections()Disables SDK detections. Currently, this operation always succeeds. However, this behaviour may change in the future.ReturnsDisableDetectionsResult
upon success orDisableDetectionsError
upon failure via PendingOperation.
PendingOperation<EnableDetectionsResult, EnableDetectionsError> enableDetections()Enables SDK detections. Requires a Sentiance user to be present on the device. Enabling detections is persistent across app restarts. Therefore, you do not need to call this method on every app startup. Once detections are enabled, as long as the SDK is properly initialized during startup, detections will be resumed, until you disable them by callingdisableDetections()
.Enabling detections does not mean that the SDK is successfully able to start all detections. There may be unsatisfactory conditions that are blocking some detections. Check the DetectionStatus returned in the result to see if all detections are running, and the SdkStatus to identify possible causes if not.To automatically disable detections after some time, use theenableDetections(Date)
variant of this method.ReturnsEnableDetectionsResult
upon success orEnableDetectionsError
upon failure via PendingOperation.
PendingOperation<EnableDetectionsResult, EnableDetectionsError> enableDetections(@Nonnull Date expirationDate)Enables SDK detections. Requires a Sentiance user to be present on the device. Enabling detections is persistent across app restarts. Therefore, you do not need to call this method on every app startup. Once detections are enabled, as long as the SDK is properly initialized during startup, detections will be resumed, until you disable them by callingdisableDetections()
.Enabling detections does not mean that the SDK is successfully able to start all detections. There may be unsatisfactory conditions that are blocking some detections. Check the DetectionStatus returned in the result to see if all detections are running, and the SdkStatus to identify possible causes if not.This method has the same behaviour asenableDetections()
, except an expiration date can be specified, as an absoluteDate
object, to automatically disable detections some time in the future. After having called this method with a future expiration date, you can cancel the expiration any time by callingenableDetections()
again.ReturnsEnableDetectionsResult
upon success orEnableDetectionsError
upon failure via PendingOperation.
Parameters | Text |
---|---|
expirationDate | An absolute date at which detections should automatically get disabled again. |
DetectionStatus getDetectionStatus()
long getDiskQuotaLimit()Returns the SDK's disk quota limit in bytes.
long getDiskQuotaUsage()Returns the SDK's disk quota usage in bytes.
InitState getInitState()Returns the initialization state of the Sentiance SDK.
static Sentiance getInstance(Context context)This method will callContext.getApplicationContext()
on the passed context to prevent memory leaks when passing an Activity context.
Parameters | Text |
---|---|
context |
long getMobileQuotaLimit()Returns the SDK's mobile data quota limit in bytes.
long getMobileQuotaUsage()Returns the SDK's mobile data quota usage in bytes.
SdkStatus getSdkStatus()
Set<TransmittableDataType> getTransmittableDataTypes();Returns the data types that are allowed to be sent to the Sentiance Cloud Platform.
UserActivity getUserActivity()
void getUserAccessToken(TokenResultCallback callback)
Parameters | Text |
---|---|
callback | A callback to handle the token result. Note that the SDK holds a weak reference to this callback in order to prevent component leaks. Make sure you have a strong reference to it in order to guarantee its execution. |
@Nullable String getUserId()Returns the Sentiance user ID, ornull
if authentication has not yet succeeded.
String getVersion()Returns the SDK version as a String.
long getWiFiQuotaLimit()Returns the SDK's WiFi quota limit in bytes.
long getWiFiQuotaUsage()Returns the SDK's WiFi quota usage in bytes.
InitializationResult initialize()Initializes the Sentiance SDK, thereby preparing it to resume detections if a Sentiance user exists, and if detections were last enabled. If a user does not exist, this method does only minimal work to allow user creation. To create a user, callcreateUser(UserCreationOptions)
.To ensure proper continuity of SDK detections across app restarts, this method must be called inside {@link Application#onCreate()}, on the main application thread, before onCreate returns. The call must not be offloaded to a background thread, as doing so cannot guarantee its invocation prior to the completion of the app startup. For more information about this requirement, see: SDK Initialization.You should initialize the SDK at most once, normally during app startup. Initializing it multiple times will fail with reasonInitializationFailureReason#REINITIALIZATION_NOT_ALLOWED
, unless a Sentiance user does not yet exist (e.g. before user creation, or after an SDK reset), in which case multiple invocations of this method are still allowed.Note that most SDK methods require an initialized SDK before being invoked, otherwise they throw a runtime exception. Methods excluded from this requirement indicate this in their respective documentation.
InitializationResult initialize(SentianceOptions options)Initializes the Sentiance SDK, thereby preparing it to resume detections if a Sentiance user exists, and if detections were last enabled. If a user does not exist, this method does only minimal work to allow user creation. To create a user, callcreateUser(UserCreationOptions)
.To ensure proper continuity of SDK detections across app restarts, this method must be called inside {@link Application#onCreate()}, on the main application thread, before onCreate returns. The call must not be offloaded to a background thread, as doing so cannot guarantee its invocation prior to the completion of the app startup. For more information about this requirement, see: SDK Initialization.You should initialize the SDK at most once, normally during app startup. Initializing it multiple times will fail with reasonInitializationFailureReason#REINITIALIZATION_NOT_ALLOWED
, unless a Sentiance user does not yet exist (e.g. before user creation, or after an SDK reset), in which case multiple invocations of this method are still allowed.Note that most SDK methods require an initialized SDK before being invoked, otherwise they throw a runtime exception. Methods excluded from this requirement indicate this in their respective documentation.
Parameters | Text |
---|---|
SDK initialization options. |
void init(SdkConfig sdkConfig, final OnInitCallback initCallback)Initializes the Sentiance Sdk.This method constructs internal SDK components and prepares the SDK for handling app and system requests. If a Sentiance user does not yet exist on the device, this method triggers user creation on the Sentiance Platform, which requires internet connectivity.During the lifetime of the app process, calling this method again after a successful initialization is not allowed. Doing so is regarded as an error and therefore throws anSdkException
at runtime. To reinitialize the SDK with a different configuration or to create a new Sentiance user, first callreset(ResetCallback)
.All methods in this class (except this method,reset(ResetCallback)
,getInitState()
andgetVersion()
) will throw anSdkException
when called before initialization has completed.
Parameters | Text |
---|---|
sdkConfig | |
initCallback |
boolean isAppSessionDataCollectionEnabled()Returns whether app session data collection is enabled. SeesetAppSessionDataCollectionEnabled(boolean)
.
boolean isTripOngoing(TripType tripType)Indicates whether a trip is in progress.
Parameters | Text |
---|---|
tripType |
boolean userExists()Checks if a Sentiance user exists on the device.You may call this method without having initialized the SDK.
boolean isUserLinked()Checks if a Sentiance user exists on the device, and whether it is linked to your app's user.You may call this method without having initialized the SDK.
PendingOperation<UserLinkingResult, UserLinkingError> linkUser(String authenticationCode)Links the Sentiance user to your app's user, on the Sentiance Platform. Uses the supplied authentication code to find the app user. Therefore, before calling this method, make sure that you have obtained a valid code from the backend Sentiance API (thereby having initiated the first linking step). This code request is authenticated using a Sentiance API key, which must never be transmitted to the app. Hence why the request must come from your backend.Here are the complete steps:
1.Initiate an authentication code request via your backend towards the Sentiance backend. This request will include your app's user identifier, which will be temporarily coupled to the code that you will receive. 2.Retrieve the code in your app and supply it to this method. 3.The SDK will forward the code to the Sentiance backend to complete the link.This method is useful if you want to link a Sentiance user that was created by specifying theUserLinker#NO_OP
orUserLinkerAsync#NO_OP
linker in the user creation options (thereby having created an unlinked user).Note that with this method, a Sentiance user cannot be linked to an app user that already exists on the Sentiance Platform (i.e. with an app user identifier that you already supplied to Sentiance during a past authentication code request). You can only link to an app user that is new to Sentiance. This is because an existing app user will already have a corresponding Sentiance user on the platform, and that Sentiance user cannot be restored here anymore. To restore that user, you must reset the SDK and recreate a linked Sentiance user instead.
Parameters | Text |
---|---|
authenticationCode | A code obtained from the backend Sentiance API. |
PendingOperation<UserLinkingResult, UserLinkingError> linkUser(UserLinkerAsync userLinker)Links the Sentiance user to your app's user, on the Sentiance Platform. During the linking process, the SDK will call the link method of the supplied UserLinkerAsync, and will pass to it an installation ID. Your app must then forward this installation ID to the Sentiance backend (via your backend), and initiate user linking. Finally, when the linking completes, you must invoke the proper linker callback to complete the process.Here are the complete steps:
1.Call this method and supply a linker. 2.Your linker will be invoked during the process, and an installation ID will be supplied. 3.In your linker, forward the installation ID to your backend to initiate a linking request towards the Sentiance backend. This request will include the installation ID and your app's user identifier. 4.Retrieve the response from the Sentiance backend and forward the result to the app. 5.Based on the result, invoke the linker callback's success or failure method. The callback is supplied to your linker along with the installation ID. 6.If it was successful, the SDK will confirm the result with the Sentiance backend to complete the link.This method is useful if you want to link a Sentiance user that was created by specifying theUserLinker#NO_OP
orUserLinkerAsync#NO_OP
linker in the user creation options (thereby having created an unlinked user).Note that with this method, a Sentiance user cannot be linked to an app user that already exists on the Sentiance Platform (i.e. with an app user identifier that you already supplied to Sentiance during a past authentication code request). You can only link to an app user that is new to Sentiance. This is because an existing app user will already have a corresponding Sentiance user on the platform, and that Sentiance user cannot be restored here anymore. To restore that user, you must reset the SDK and recreate a linked Sentiance user instead.
Parameters | Text |
---|---|
userLinker | The linker that the SDK will pass the installation ID to. |
PendingOperation<UserLinkingResult, UserLinkingError> linkUser(UserLinkerAsync userLinker)Links the Sentiance user to your app's user, on the Sentiance Platform. During the linking process, the SDK will call the link method of the suppliedUserLinker
, and will pass to it an installation ID. Your app must then forward this installation ID to the Sentiance backend (via your backend), and initiate user linking. Finally, when the linking completes, you must return a proper result to complete the process.Here are the complete steps:
1.Call this method and supply a linker. 2.Your linker will be invoked during the process, and an installation ID will be supplied. 3.In your linker, forward the installation ID to your backend to initiate a linking request towards the Sentiance backend. This request will include the installation ID and your app's user identifier. 4.Retrieve the response from the Sentiance backend and forward the result to the app. 5.In your linker, returntrue
if the result was successful, and otherwise returnfalse
. 6.If it was successful, the SDK will confirm the result with the Sentiance backend to complete the link.This method is useful if you want to link a Sentiance user that was created by specifying theUserLinker#NO_OP
orUserLinkerAsync#NO_OP
linker in the user creation options (thereby having created an unlinked user).Note that with this method, a Sentiance user cannot be linked to an app user that already exists on the Sentiance Platform (i.e. with an app user identifier that you already supplied to Sentiance during a past authentication code request). You can only link to an app user that is new to Sentiance. This is because an existing app user will already have a corresponding Sentiance user on the platform, and that Sentiance user cannot be restored here anymore. To restore that user, you must reset the SDK and recreate a linked Sentiance user instead.
Parameters | Text |
---|---|
userLinker | The linker that the SDK will pass the installation ID to. |
void removeUserMetadataField(String label)Removes a user specific metadata field.
Parameters | Text |
---|---|
label | Metadata to remove. |
PendingOperation<Token, UserAccessTokenError> requestUserAccessToken()
The reset functionality is intended for removing all data in the device to handle a case such as a user logging out from an app or user requesting data deletion in the local app. It should not be used to reset the internal state of the SDK.
PendingOperation<ResetResult, ResetError> reset()Resets the Sentiance SDK.Calling this method results in stopping of all SDK operations and the deletion of all user data from the device. To avoid losing unsubmitted detection data, you can callsubmitDetections
before resetting the SDK as a final (best effort) attempt. However, keep in mind that this can introduce a delay to your reset process, since it is impacted by the network capability and the number of pending detections.During an ongoing reset operation, calling any other SDK method will be ignored, fail, or will return a default result. After the reset operation completes, you do not have to reinitialize the SDK, but can do so by calling initialize(SentianceOptions) again (e.g. to supply a differentSentianceOptions
).While resetting, make sure that your app is in the foreground to prevent process termination (i.e. an activity is open, or a foreground service is running). The SDK cannot guarantee the completion of this operation otherwise.You may call this method without having initialized the SDK
This method is deprecated. Use
reset()
instead.The reset functionality is intended for removing all data in the device to handle a case such as a user logging out from an app or user requesting data deletion in the local app. It should not be used to reset the internal state of the SDK.
void reset(@Nullable ResetCallback callback)Resets the Sentiance SDK.Calling this method results in stopping of all SDK operations and deleting all SDK user data from the device. Additionally, the SDK will be uninitialized, allowing reinitialization and new Sentiance user creation.The reset operation may take a while, in which case the SDK's initialization state will be set toInitState.RESETTING
until it's complete. Calling any other SDK method during this time will either be ignored or return a default value. While resetting, make sure your app is in the foreground to prevent process termination (i.e. an activity is open, or a foreground service is running).Note that calling this method during intermediate initialization states (i.e.INIT_IN_PROGRESS
andRESETTING
) will fail.
Parameters | Text |
---|---|
callback |
void setAppSessionDataCollectionEnabled(boolean enabled)If enabled, the SDK may collect additional sensor and location data when the app is in the foreground (i.e visible to the user). This data may get uploaded to the Sentiance Platform. By default, this data collection is disabled.
void setSdkStatusUpdateListener(SdkStatusUpdateListener listener)Sets a listener that is invoked when the SDK's status is updated.
Parameters | Text |
---|---|
listener |
void setTripTimeoutListener(@Nullable TripTimeoutListener listener)Sets a listener that is invoked when a trip times out.Currently, only external trips started by callingstartTrip(Map,TransportMode)
time out, and only when your app is configured by Sentiance to run in "triggered trips" mode (where automatic SDK detections are disabled). Therefore, timeouts do not apply to most use-cases.
Parameters | Text |
---|---|
listener |
void setTransmittableDataTypes(@NonNull Set<TransmittableDataType> transmittableDataTypes);Sets the data types that are allowed to be sent to the Sentiance platform.This configuration is persistent across app restarts but will be cleared upon SDK reset. By default all data types are allowed to be sent.Note: this configuration is not applicable to logging and diagnostic data that may get uploaded to the Sentiance Cloud Platform (if enabled for your app).
Parameters | Text |
---|---|
transmittableDataTypes | allowed transmittable data types |
void setUserActivityListener(@Nullable UserActivityListener listener)Sets a listener that is invoked when a new user activity is detected. After calling this method, the current user activity will be immediately delivered to the listener.
Parameters | Text |
---|---|
listener |
void start(OnStartFinishedHandler handler)Starts the SDK detections.When this process has finished,OnStartFinishedHandler.onStartFinished(SdkStatus)
will be called. This does not necessarily mean that the SDK is actually performing detections. You may use theSdkStatus
passed to theonStartFinished(SdkStatus)
method to determine why.To automatically stop detections, call thestart(Date, OnStartFinishedHandler)
method instead. You do not need to callstop()
beforehand as all start calls override each other.
Parameters | Text |
---|---|
handler | A OnStartFinishedHandler used to notify that the SDK start has finished. Note that the SDK holds a weak reference to this handler to in order to prevent component leaks. Make sure you have a strong reference to it in order to guarantee its execution. |
void start(Date stopDate, OnStartFinishedHandler handler)Starts the SDK detections.This method has the same behaviour asstart(OnStartFinishedHandler)
except an absolute date is passed to automatically stop the SDK some time in the future. A typical use-case is when the app provides a free trial period for its users after which the SDK should stop running.To cancel the auto-stop functionality, you can call this method with a null stopDate or just callstart(OnStartFinishedHandler)
. You do not need to callstop()
beforehand as all start calls override each other.
Parameters | Text |
---|---|
stopDate | An absolute date at which the SDK is stopped. If the date is in the past, the SDK will stop immediately. |
handler | A OnStartFinishedHandler used to notify that the SDK start has finished. Note that the SDK holds a weak reference to this handler to in order to prevent component leaks. Make sure you have a strong reference to it in order to guarantee its execution. |
PendingOperation<StartTripResult, StartTripError> startTrip(@Nullable Map<String, String> metadata,@Nullable TransportMode transportModeHint)Starts an external trip. Requires a Sentiance user to be present on the device.Calling this method interrupts any ongoing SDK detection, and forces the SDK to stay in a data collection mode. Trips started with this method may time out, in which case, the TripTimeoutListener set usingsetTripTimeoutListener(TripTimeoutListener)
will be invoked. The timeout duration can be configured for your app by Sentiance.
Parameters | Text |
---|---|
metadata | A map of metadata pertaining to the newly started trip, which will be saved along with the trip data, and made available in backend API query results and offloads. May be null . |
transportModeHint |
void startTrip(@Nullable Map<String, String> metadata,@Nullable TransportMode transportModeHint,@Nullable StartTripCallback callback)Starts an external trip.
Parameters | Text |
---|---|
metadata | A map of metadata pertaining to the newly started trip, which will be saved along with the trip data. |
transportModeHint | |
callback |
void stop()Stops the SDK detections.
PendingOperation<StopTripResult, StopTripError> stopTrip()Stops the currently ongoing external trip (i.e one that was started by callingstartTrip(Map, TransportMode)
.
void stopTrip(@Nullable StopTripCallback callback)
Parameters | Text |
---|---|
callback |
PendingOperation<SubmitDetectionsResult, SubmitDetectionsError> submitDetections()Submits any pending SDK detection to the Sentiance API.This method will bypass any network quota limitation set for the SDK. A typical use case would be when the enclosing app determines (viagetDiskQuotaUsage()
orSdkStatus#diskQuotaStatus)
that there are too many detections on disk that have not yet been submitted.ReturnsSubmitDetectionsResult
upon success orSubmitDetectionsError
upon failure via PendingOperation.
This method is deprecated. Use
submitDetections()
instead.void submitDetections(@Nullable SubmitDetectionsCallback callback)Submits the detections to the Sentiance API. This will bypass any network quota limitation.A typical use case of this method would be when the enclosing app determines (viagetDiskQuotaUsage()
orSdkStatus.diskQuotaStatus
) that there are too many detections on disk that have not yet been submitted.
Parameters | Text |
---|---|
callback |
void updateSdkNotification(Notification notification)Replaces the notification set viaSentianceOptions.Builder#setNotification(Notification, int)
. After calling this method, any service notification shown by the SDK will be updated.Note that the notification update done by this method is valid only during the app's lifetime. After the app restarts, the SDK will default back to the notification set during SDK initialization (or a default one if nothing was specified).
Parameters | Text |
---|---|
notification |
Last modified 8mo ago