/**
* SHealth permission change handler.
* @callback PermissionChangeHandler
* @property {DataType[]} accepted - An array of accepted {@link DataType}.
* @property {DataType[]} denied - An array of denied {@link DataType}.
*/
/**
* SHealth handler for records gathered by Shealth
* @callback SHealthRecordsHandler
* @param {Object} summary - A dictionary of how many records were collected by SHealth keyed by {@link RecordType}
*/
/**
* SHealth error function
* @callback SHealthErrorHandler
* @property {SHealthError} error - The error returned by SHealth
*/
/**
* SHealth History handler
* @callback SHealthHistoryHandler
* @property {Object} summary - a dictionary of how many records were collected by SHealth and keyed by {@link RecordType}
*/
/**
* SHealth listeners for successful events
* @typedef SHealthHandlers
* @property {PermissionChangeHandler} onPermissionChanged
* @property {SHealthRecordsHandler} onSHealthRecords
* @property {SHealthErrorHandler} onSHealthError
* @property {SHealthHistoryHandler} onSHealthHistory
*/
document.addEventListener("deviceReady", () => {
exports.SHealth.setSHealthListener();
});
/**
* @exports SHealth
* Represents Validic Mobile's Samsung SHealth Functionality.
*/
exports.SHealth = {
/**
* Checks whether this device is an Android device and if SHealth is installed.
* @param {AvailableCallback} callback
*/
isSHealthAvailable: function (callback) {
cordova.exec(
callback,
null,
"ValidicMobileSHealth",
"isSHealthAvailable",
[]
);
},
/**
* Starts the SHealth Service if it is not already started. It will report back gathered permissions through
* onPermissionChanged with a {@link PermissionChangeHandler} or OnPermission error with an {@link SHealthError}.
*/
observeCurrentSubscriptions: function () {
cordova.exec(
null,
null,
"ValidicMobileSHealth",
"observeSHealthSubscriptions",
[]
);
},
/**
* Add an array of {@link SubscriptionSet} values to receive records through SHealth for.
* @param {SubscriptionSet[]} subscriptionSet
*/
addSubscriptionSets: function (subscriptionSet) {
cordova.exec(
null,
null,
"ValidicMobileSHealth",
"addSHealthSubscriptionSets",
[subscriptionSet]
);
},
/**
* Add an array of {@link DataType} values to receive records through SHealth for.
* @param {DataType[]} dataTypes - An array of DataType to add a subscription for to receive records through SHealth.
*/
addSubscriptions: function (dataTypes) {
cordova.exec(
null,
null,
"ValidicMobileSHealth",
"addSHealthSubscriptions",
[dataTypes]
);
},
/**
* Remove subscriptions to a group of {@link DataType} from SHealth.
* @param {DataType[]} dataTypes
*/
removeSubscriptions: function (dataTypes) {
cordova.exec(
null,
null,
"ValidicMobileSHealth",
"removeSHealthSubscriptions",
[dataTypes]
);
},
/**
* Remove an array of {@link SubscriptionSet} values from SHealth.
* @param subscriptionSets
*/
removeSubscriptionSets: function (subscriptionSets) {
cordova.exec(
null,
null,
"ValidicMobileSHealth",
"removeSHealthSubscriptionSets",
[subscriptionSets]
);
},
/**
* Set a listener for SHealth events from SHealth.
* @param {SHealthHandlers} listeners - Callbacks for events from SHealth
*/
setSHealthListener: function (listeners) {
var successful = function (obj) {
if (obj.hasOwnProperty("event")) {
cordova.fireWindowEvent(obj.event, obj.payload);
if (
obj.event ==
ValidicMobile.SHealth.EventName.ON_SHEALTH_PERMISSIONS
) {
ValidicMobile.SHealth.onPermissionChanged(obj.payload);
if (
listeners != null &&
listeners.hasOwnProperty("onPermissionChanged")
) {
listeners.onPermissionChanged(obj.payload);
}
} else if (
obj.event ==
ValidicMobile.SHealth.EventName.ON_SHEALTH_RECORDS &&
listeners != null &&
listeners.hasOwnProperty("onSHealthRecords")
) {
listeners.onSHealthRecords(obj.payload);
} else if (
obj.event ==
ValidicMobile.SHealth.EventName.ON_SHEALTH_HISTORY &&
listeners != null &&
listeners.hasOwnProperty("onSHealthHistory")
) {
listeners.onSHealthHistory(obj.payload);
}
}
};
var error = function (obj) {
if (obj.hasOwnProperty("event")) {
cordova.fireWindowEvent(obj.event, obj.payload);
if (
obj.event ==
ValidicMobile.SHealth.EventName.ON_SHEALTH_ERROR
) {
ValidicMobile.SHealth.onPermissionError(obj.payload);
if (
listeners != null &&
listeners.hasOwnProperty("onSHealthError")
) {
listeners.onSHealthError(obj.payload);
}
}
}
};
cordova.exec(
successful,
error,
"ValidicMobileSHealth",
"setSHealthListener",
[]
);
},
/**
* @deprecated
* use {@link setSHealthListener}
* Error callback for SHealth. Overload this function to receive notifications for SHealth Permission Errors.
* @param {SHealthError} err - Error from SHealth.
*/
onPermissionError: function (err) {},
/**
* @deprecated
* use {@link setSHealthListener}
* Success callback for SHealth. Overload this function to receive notifications for SHealth Permission changes.
* @param {PermissionChangeHandler} obj - Callback with gathered permissions.
*/
onPermissionChanged: function (obj) {},
/**
* Returns the set of {@link #DataType} associated with a {@link #SubscriptionSet}
* @param {SubscriptionSet} The subscription set to return data types for
* @param {DataTypeCallback} The success callback for returning the array of datatypes
* @param {FailCallback} The error callback
*/
getSubscriptionsFromSet: function (set, success, err) {
cordova.exec(
success,
err,
"ValidicMobileSHealth",
"getSubscriptionDataTypes",
[set]
);
},
/**
*
* @typedef HistoryOptions
* @param {HistoricalSet[]} historicalSets
* @param {string} from - ISO 8601 Date String in the format yyyy-MM-dd for the start of the history query. Default is 180 days in the past.
* @param {string} to - ISO 8601 Date String in the format yyyy-MM-dd for the end of the history query. Default is today.
*
*/
/**
* Fetch historical data for the specified sets across the specified dates.
* @param {HistoryOptions | HistoricalSet[]} historyOptions - HistoricalSet and Date query options
* {@link HistoricalSet.ROUTINE} and {@link HistoricalSet.FITNESS} are supported. If a listener has been set using
* {@link SHealth.setSHealthListener} a summary will be returned for any records pulled from history.
*/
fetchHistoricalSets: function (historyOptions) {
if (Array.isArray(historyOptions)) {
cordova.exec(null, null, "ValidicMobileSHealth", "fetchHistory", [
{ historicalSets: historyOptions },
]);
} else {
cordova.exec(null, null, "ValidicMobileSHealth", "fetchHistory", [
historyOptions,
]);
}
},
/**
* The window event names for events generated by SHealth
* @enum {string}
*/
EventName: {
ON_SHEALTH_PERMISSIONS: "validicOnSHealthPermissions",
ON_SHEALTH_ERROR: "validicOnSHealthError",
ON_SHEALTH_RECORDS: "validicOnSHealthRecords",
ON_SHEALTH_HISTORY: "validicOnSHealthHistory",
},
/**
* Each Subscription set is a collection of SHealth DataType that when used add subscriptions for multiple DataTypes.
* @enum {number}
*/
SubscriptionSet: {
ROUTINE: 0,
DIABETES: 1,
WEIGHT: 2,
FITNESS: 3,
SLEEP: 4,
NUTRITION: 5,
BLOOD_PRESSURE: 6,
},
/**
* Each Historical set is a collection of SHealth DataType that is used to fetch 6 months of corresponding data from SHealth.
* @enum {string}
*/
HistoricalSet: {
ROUTINE: "ROUTINE",
FITNESS: "FITNESS",
},
/**
* Represents the SHealth HealthConstants string representation.
* @enum {string}
*/
DataType: {
DAILY_STEP_COUNT: "com.samsung.shealth.step_daily_trend",
BLOOD_PRESSURE: "com.samsung.health.blood_pressure",
TEMPERATURE: "com.samsung.health.body_temperature",
HEART_RATE: "com.samsung.health.heart_rate",
SPO2: "com.samsung.health.oxygen_saturation",
GLUCOSE: "com.samsung.health.blood_glucose",
HBA1C: "com.samsung.health.hba1c",
WEIGHT: "com.samsung.health.weight",
FOOD_INFO: "com.samsung.health.food_info",
FOOD_INTAKE: "com.samsung.health.food_intake",
WATER_INTAKE: "com.samsung.health.water_intake",
CAFFEINE_INTAKE: "com.samsung.health.caffeine_intake",
EXERCISE: "com.samsung.health.exercise",
SLEEP: "com.samsung.health.sleep",
SLEEP_STAGE: "com.samsung.health.sleep_stage",
},
/**
* The SHealth errors that can be reported through {@link onPermissionError}.
* @enum {string}
*/
SHealthError: {
MISSING_USER: "MISSING_USER",
RESOLVING_CONNECTION_FAILED: "RESOLVING_CONNECTION_FAILED",
CONNECTION_FAILED: "CONNECTION_FAILED",
INVALID_DATA_TYPE: "INVALID_DATA_TYPE",
INVALID_PERMISSION: "INVALID_PERMISSION",
},
};