SDK
Table of contents
Repository
You can browse the Sahha Android SDK on Github.
Installation
Step 1) Add Jitpack
Add the Jitpack repository to your project’s Settings.gradle.
Settings.gradle
repositories {
mavenCentral()
maven { url "https://jitpack.io" }
}
Step 2) Add Implementation
Add the Sahha SDK implementation to your project’s dependencies in Build.gradle.
Build.gradle
dependencies {
...
implementation 'com.github.sahha-ai:sahha-android:+'
...
}
Step 3) Add Permissions
Add the required permissions to your project’s AndroidManifest.xml.
AndroidManifest.xml
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
Step 4) Import Module
Import the Sahha module into any files inside your project that use the SDK.
Kotlin
import sdk.sahha.android.source.*
Configure
Configure Sahha inside onCreate
of your app’s MainActivity
.
MainActivity.kt
import sdk.sahha.android.source.*
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val config = SahhaSettings(
environment = SahhaEnvironment.development
)
Sahha.configure(application, config)
}
}
Settings
You may pass an optional list of settings to the configure
method.
The SahhaEnvironment
determines if the SDK connects to the development
or production
server of the API. Setting this incorrectly will send data to the wrong server.
SahhaEnvironment | Description |
---|---|
development | For development and testing |
production | For submission to the App store |
enum class SahhaEnvironment {
development,
production
}
Which sensors do you want to enable? Passing an empty array will enable all sensors by default.
SahhaSensor | Description | Android | iOS |
---|---|---|---|
device | For tracking device usage patterns | ✓ | X |
sleep | For tracking sleep patterns | ✓ | ✓ |
pedometer | For tracking walking patterns | ✓ | ✓ |
enum class SahhaSensor {
device,
sleep,
pedometer
}
If you would like to handle posting of sensor data to the Sahha API manually, set postSensorDataManually
to true
. You will then need to post sensor data to the Sahha API manually by calling postSensorData
.
If you would like the Sahha SDK to handle posting of sensor data to the Sahha API automatically, set postSensorDataManually
to false
.
postSensorDataManually
defaults to false
.
postSensorDataManually | Description |
---|---|
true | You will post sensor data to the Sahha API manually by calling postSensorData |
false | The Sahha SDK will post sensor data to the Sahha API automatically |
Profile
Each user of your app will be assigned a Sahha Profile for analyzation. This is not handled by the Sahha SDK. Please refer to the separate Sahha API documentation for steps to generate profiles and profile tokens.
Authenticate
The Sahha SDK must be authenticated in order to connect to the Sahha API. Do this once per user profile. Once the user is successfully authenticated, the SDK will take care of automatically refreshing expired tokens.
Sahha.authenticate(profileToken: "PROFILE_TOKEN", refreshToken: "REFRESH_TOKEN") { error, success ->
if (success) greeting = "Successful"
else greeting = error ?: "Failed"
}
Demographic
Each authenticated profile includes an optional demographic which can be used to increase the accuracy of analyzation. This data is not collected automatically. Your app can choose to GET or POST this demographic to the Sahha API.
class SahhaDemographic {
public var age: Int?
public var gender: String? // "Male", "Female", "Gender Diverse"
public var country: String? // ISO 2 digit code, i.e. "US", "UK", "AU", etc.
public var birthCountry: String? // ISO 2 digit code, i.e. "US", "UK", "AU", etc.
}
var demographic = SahhaDemographic(age, gender, country, birthCountry)
Sahha.postDemographic(demographic) { error, success ->
if (error != null) {
println(error)
} else {
println(success.toString())
}
}
Sahha.getDemographic() { error, demographic ->
if (error != null) {
println(error)
} else if (demographic != null) {
println(demographic)
}
}
Sensors
The Sahha SDK acts as a bridge between your app and the device sensors. You must enable each sensor that you want to start collecting data from. To optimize your analysis result, we suggest enabling all available sensors.
enum class SahhaSensor {
device,
sleep,
pedometer
}
Sensor Status
Each sensor has multiple possible statuses.
enum class SahhaSensorStatus {
pending, /// Sensor is pending User permission
unavailable, /// Sensor is not supported by the User's device
disabled, /// Sensor has been disabled by the User
enabled /// Sensor has been enabled by the User
}
You can check the current status of each sensor by calling getSensorStatus
. This method is asynchronous and will return the updated SahhaSensorStatus
in its callback.
Sahha.getSensorStatus(this@MainActivity, SahhaSensor.pedometer) { error, sensorStatus ->
if (error != null) {
println(error)
} else {
println(sensorStatus.name)
}
}
Enable Sensor
You will need to manually enable each sensor by calling enableSensor
. This method is asynchronous and will return the updated SahhaSensorStatus
in its callback.
Sahha.enableSensor(this@MainActivity, SahhaSensor.pedometer) { error, sensorStatus ->
if (error != null) {
println(error)
} else {
println(sensorStatus.name)
}
}
Open App Settings
It’s possible for your app user to disable a sensor. In this case, you must send the user to the app settings to manually enable the sensor.
Sahha.openAppSettings(this@MainActivity)
Post Sensor Data
By default, the Sahha SDK will post sensor data automatically. However, if you set postSensorDataManually
to true
, you will need to post sensor data manutally to the Sahha API by calling postSensorData
at a regular interval of your choosing.
You have two options:
- A) Include a list of specific sensor data you want to post
- B) Post all sensor data
Option A) Post specific sensor data
// Include a list of sensors
Sahha.postSensorData(setOf(SahhaSensor.device, SahhaSensor.sleep)) { error, success ->
if (success) manualPost = "Successful"
else manualPost = error ?: "Failed"
}
Option B) Post all sensor data
// Leave sensor list empty
Sahha.postSensorData { error, success ->
if (success) manualPost = "Successful"
else manualPost = error ?: "Failed"
}
Analyze
You can analyze a user’s activities over a period of time and receive a mental health personalized report which you can display or action within your app.
Sahha.analyze { response ->
println(response)
}
The response will be in JSON format. An example response includes these fields:
{
"inferences": [
{
"createdAt": "2022-05-20T00:30:00+00:00",
"modelName": "automl_toolkit_randomForest",
"predictionState": "not_depressed",
"predictionSubState": "",
"predictionRange": -1,
"predictionConfidence": 0.8,
"dataSource": ["sleep", "screenTime"],
"dataSourceSummary": [
{
"type": "sleep",
"amount": "0"
},
{
"type": "screenTime",
"amount": "0"
}
]
}
]
}