HealthDataStore
samsung health data sdk/com samsung android sdk health data/healthdatastore healthdatastore interface healthdatastorehealthdatastore is a proxy that provides a way to access the data stored in the samsung health app an app can access the data through the operations provided by healthdatastore, such as reading, aggregating, inserting, updating and deleting data use the healthdataservice getstore method to retrieve an instance from healthdatastore the below examples show how apps can retrieve appropriate permissions and read data in two styles of programming all requests are processed immediately after moving to a dedicated thread pool, so the caller can invoke the method on any thread however, of the various manners of waiting for the result of asynccompletablefuture or asyncsinglefuture, do not use the sync manner on the main thread synchronous request the below sample code uses suspend functions, showing how to retrieve a step count for a defined time period be careful not to use the synchronous request on the main thread @throws interruptedexception class suspend fun showsteps date localdate, context context { val store = healthdataservice getstore context val requiredpermissions mutableset<permission> = hashset listof permission of datatypes steps, accesstype read // this checks if the app has the user's permission for the target data types if areallpermissionsobtained store, requiredpermissions { val stepsresponse = getaggregateresult store, date stepsresponse datalist foreach { val steps = it value // this processes the value } } } @throws interruptedexception class private suspend fun areallpermissionsobtained store healthdatastore, permissions set<permission> boolean { try { // an api call to check the granted permissions val initialresult set<permission> = store getgrantedpermissions permissions if !initialresult containsall permissions { val requiredpermissions = permissions tomutableset apply { removeall initialresult } // an api call for a permission request val obtainedresult = store requestpermissions requiredpermissions, this context if obtainedresult containsall requiredpermissions { return true } } else { return true } } catch error healthdataexception { if error is resolvableplatformexception && error hasresolution { log i tag, error tostring error resolve this context // an exception to indicate that the samsung health app is not ready to serve the specified operation } else if error is authorizationexception { log i tag, error tostring // an exception to indicate that the app is not authorized to perform the specified operation } else if error is invalidrequestexception { log i tag, error tostring // an exception to indicate that the app requests the specified operation with invalid conditions } else if error is platforminternalexception { log i tag, error tostring // an exception to indicate that the samsung health app experienced an internal error // that cannot be resolved by the app } } return false } private suspend fun getaggregateresult store healthdatastore, date localdate dataresponse<aggregateddata<long>> { val stepsrequest aggregaterequest<long> = datatype stepstype total requestbuilder setlocaltimefilterwithgroup localtimefilter of date atstartofday , date plusdays 1 atstartofday , localtimegroup of localtimegroupunit minutely, 30 build // an api call for an aggregate request return store aggregatedata stepsrequest }content copied to clipboard asynchronous request this manner returns `asynccompletablefuture` or `asyncsinglefuture` the actual requests are processed immediately after moving to a dedicated thread pool, so the method can be invoked on any thread to get a query result, processing `futures` using sync manner on the main thread is not recommended @throws interruptedexception class fun showsteps date localdate, context activity { log i tag, "showsteps async" this context = context val store = healthdataservice getstore context val requiredpermissions mutableset<permission> = hashset listof permission of datatypes steps, accesstype read // this checks the user's permissions for the target data types runonallpermissionsobtained store, requiredpermissions { val stepsresponse = getaggregateresult store, date stepsresponse datalist foreach { val steps = it value // this processes the value log i tag, "step count $steps" } } } private fun runonallpermissionsobtained store healthdatastore, permissions set<permission>, runnable runnable { // an asynchronous api call to check if the correct permissions are granted store getgrantedpermissionsasync permissions setcallback looper getmainlooper , consumer { initialresult set<permission> -> if !initialresult containsall permissions { val requiredpermissions = permissions tomutableset apply { removeall initialresult } // an asynchronous api call for a permission request store requestpermissionsasync permissions, context setcallback looper getmainlooper , consumer { obtainedresult -> if !obtainedresult containsall requiredpermissions { return@consumer } runnable run } { error -> if error is resolvableplatformexception && error hasresolution { error resolve context } } } else { runnable run ; } } { error -> if error is resolvableplatformexception && error hasresolution { error resolve context // an exception to indicate that the samsung health app is not ready to serve the specified operation } else if error is authorizationexception { // an exception to indicate that the app is not authorized to perform given operation } else if error is invalidrequestexception { // an exception to indicate that the app requests the given operation with invalid conditions } else if error is platforminternalexception { // an exception to indicate that the samsung health app experienced an internal error // that cannot be resolved by the app } } } private fun getaggregateresult store healthdatastore, date localdate dataresponse<aggregateddata<long>> { val stepsrequest = datatype stepstype total requestbuilder setlocaltimefilterwithgroup localtimefilter of date atstartofday , date plusdays 1 atstartofday , localtimegroup of localtimegroupunit minutely, 30 build // an asynchronous api call for an aggregate request return store aggregatedataasync stepsrequest get }content copied to clipboardsince1 0 0 membersmembers & extensions functions aggregate link copied to clipboard suspend fun <t any, s aggregaterequest builder<t>> healthdatastore aggregate aggregateoperation aggregateoperation<t, s>, builder suspend s -> s? = null dataresponse<aggregateddata<t>>aggregates a set of data in the samsung health app with simplified code it enables you to request an aggregate operation for a specific data type with building an aggregaterequest easily to aggregate data asynchronously, use healthdatastore aggregatedataasync aggregatedata link copied to clipboard abstract suspend fun <t any> aggregatedata request aggregaterequest<t> dataresponse<aggregateddata<t>>aggregates a set of the samsung health app's data according to the specified request aggregatedataasync link copied to clipboard abstract fun <t any> aggregatedataasync request aggregaterequest<t> asyncsinglefuture<dataresponse<aggregateddata<t>>>aggregates a set of samsung health data according to the specified request delete link copied to clipboard suspend fun <t datapoint> healthdatastore delete type datatype writeable<t>, builder suspend deletedatarequest basicbuilder -> deletedatarequest basicbuilder deletes a set of data from the samsung health app with simplified code it enables you to delete health data with a specific data type and building a deletedatarequest easily to delete data asynchronously, use healthdatastore deletedataasync deletedata link copied to clipboard abstract suspend fun deletedata request deletedatarequest delete a set of data in the request into the samsung health app deletedataasync link copied to clipboard abstract fun deletedataasync request deletedatarequest asynccompletablefuturedelete a set of data in the request into the samsung health getdevicemanager link copied to clipboard abstract fun getdevicemanager devicemanagerretrieves the devicemanager object for information about the devices that provide the health data stored in the samsung health getgrantedpermissions link copied to clipboard abstract suspend fun getgrantedpermissions permissions set<permission> set<permission>returns the specified set of granted permissions getgrantedpermissionsasync link copied to clipboard abstract fun getgrantedpermissionsasync permissions set<permission> asyncsinglefuture<set<permission>>returns the specified set of granted permissions insert link copied to clipboard suspend fun <t datapoint> healthdatastore insert type datatype writeable<t>, builder suspend insertdatarequest basicbuilder<t> -> insertdatarequest basicbuilder<t> inserts a set of data into the samsung health app with simplified code it enables you to insert health data with a specific data type and building an insertdatarequest easily to insert data asynchronously, use healthdatastore insertdataasync insertdata link copied to clipboard abstract suspend fun <t datapoint> insertdata request insertdatarequest<t> inserts a set of data in the request into the samsung health app insertdataasync link copied to clipboard abstract fun <t datapoint> insertdataasync request insertdatarequest<t> asynccompletablefutureinserts a set of data in the request into the samsung health read link copied to clipboard suspend fun <t datapoint, s readdatarequest builder<t>> healthdatastore read type datatype readable<t, s>, builder suspend s -> s? = null dataresponse<t>reads a set of data in the samsung health app with simplified code it enables you to read health data with a specific data type and building a readdatarequest for that data type easily to read data asynchronously, use healthdatastore readdataasync readassociateddata link copied to clipboard abstract suspend fun readassociateddata request associatedreadrequest dataresponse<associateddatapoints>reads a set of associated data for a certain target data according to the given request readassociateddataasync link copied to clipboard abstract fun readassociateddataasync request associatedreadrequest asyncsinglefuture<dataresponse<associateddatapoints>>reads asynchronously a set of associated data for a certain target data according to the given request readchange link copied to clipboard suspend fun <t datapoint> healthdatastore readchange type datatype changereadable<t>, builder suspend changeddatarequest basicbuilder<t> -> changeddatarequest basicbuilder<t> dataresponse<change<t>>reads a set of changed data in the samsung health app with simplified code it enables you to read changed health data with a specific data type and building a changeddatarequest for that data type easily to read changed data asynchronously, use healthdatastore readchangesasync readchanges link copied to clipboard abstract suspend fun <t datapoint> readchanges request changeddatarequest<t> dataresponse<change<t>>reads a set of changes made in the samsung health app's data according to the specified request readchangesasync link copied to clipboard abstract fun <t datapoint> readchangesasync request changeddatarequest<t> asyncsinglefuture<dataresponse<change<t>>>reads a set of changes made in the samsung health data according to the specified request readdata link copied to clipboard abstract suspend fun <t datapoint> readdata request readdatarequest<t> dataresponse<t>reads a set of the samsung health app's data according to the specified request readdataasync link copied to clipboard abstract fun <t datapoint> readdataasync request readdatarequest<t> asyncsinglefuture<dataresponse<t>>reads a set of the samsung health app's data according to the specified request requestpermissions link copied to clipboard abstract suspend fun requestpermissions permissions set<permission>, activity activity set<permission>requests a data permission popup to be displayed for the user to grant a specified set of permissions requestpermissionsasync link copied to clipboard abstract fun requestpermissionsasync permissions set<permission>, activity activity asyncsinglefuture<set<permission>>requests a data permission popup to be displayed for the user to grant a specified set of permissions update link copied to clipboard suspend fun <t datapoint> healthdatastore update type datatype writeable<t>, builder suspend updatedatarequest basicbuilder<t> -> updatedatarequest basicbuilder<t> updates a set of data in the samsung health app with simplified code it enables you to update health data with a specific data type and building a updatedatarequest easily to update data asynchronously, use healthdatastore updatedataasync updatedata link copied to clipboard abstract suspend fun <t datapoint> updatedata request updatedatarequest<t> updates a set of data, represented by the request into the samsung health app's database updatedataasync link copied to clipboard abstract fun <t datapoint> updatedataasync request updatedatarequest<t> asynccompletablefutureupdates a set of data in the request into the samsung health