platform channel added.
parent
183bcce003
commit
4912f686f9
@ -0,0 +1,125 @@
|
||||
package com.ejada.hmg.samsung_watch
|
||||
|
||||
|
||||
|
||||
import com.ejada.hmg.MainActivity
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import androidx.annotation.RequiresApi
|
||||
import com.ejada.hmg.penguin.PenguinView
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
import io.flutter.plugin.common.MethodCall
|
||||
import com.ejada.hmg.PermissionManager.HostNotificationPermissionManager
|
||||
import com.ejada.hmg.PermissionManager.HostBgLocationManager
|
||||
import com.ejada.hmg.PermissionManager.HostGpsStateManager
|
||||
import com.samsung.android.sdk.health.data.HealthDataService
|
||||
import com.samsung.android.sdk.health.data.HealthDataStore
|
||||
import com.samsung.android.sdk.health.data.permission.AccessType
|
||||
import com.samsung.android.sdk.health.data.permission.Permission
|
||||
import com.samsung.android.sdk.health.data.request.DataTypes
|
||||
import com.samsung.android.sdk.health.data.request.LocalTimeFilter
|
||||
import com.samsung.android.sdk.health.data.request.Ordering
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.launch
|
||||
import java.time.LocalDateTime
|
||||
import java.time.LocalTime
|
||||
|
||||
class SamsungWatch(
|
||||
private var flutterEngine: FlutterEngine,
|
||||
private var mainActivity: MainActivity
|
||||
) {
|
||||
|
||||
private lateinit var channel: MethodChannel
|
||||
private lateinit var dataStore: HealthDataStore
|
||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
private val TAG = "SamsungWatch"
|
||||
companion object {
|
||||
private const val CHANNEL = "samsung_watch"
|
||||
|
||||
}
|
||||
init{
|
||||
create()
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
fun create() {
|
||||
Log.d(TAG, "create: is called")
|
||||
// openTok = OpenTok(mainActivity, flutterEngine)
|
||||
channel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
|
||||
channel.setMethodCallHandler { call: MethodCall, result: MethodChannel.Result ->
|
||||
when (call.method) {
|
||||
"init" -> {
|
||||
Log.d(TAG, "onMethodCall: init called")
|
||||
dataStore = HealthDataService.getStore(mainActivity)
|
||||
result.success("initialized")
|
||||
}
|
||||
|
||||
"getPermission"->{
|
||||
if(!this::dataStore.isInitialized)
|
||||
result.error("DataStoreNotInitialized", "Please call init before requesting permissions", null)
|
||||
val permSet = setOf(
|
||||
Permission.of(DataTypes.HEART_RATE, AccessType.READ),
|
||||
Permission.of(DataTypes.STEPS, AccessType.READ),
|
||||
Permission.of(DataTypes.BLOOD_OXYGEN, AccessType.READ),
|
||||
Permission.of(DataTypes.ACTIVITY_SUMMARY, AccessType.READ),
|
||||
Permission.of(DataTypes.SLEEP, AccessType.READ),
|
||||
// Permission.of(DataTypes.SKIN_TEMPERATURE, AccessType.READ),
|
||||
// Permission.of(DataTypes.NUTRITION, AccessType.READ),
|
||||
|
||||
)
|
||||
scope.launch {
|
||||
try {
|
||||
var granted = dataStore.getGrantedPermissions(permSet)
|
||||
|
||||
if(granted.containsAll(granted))
|
||||
result.success("Permission Granted") // adapt result as needed
|
||||
|
||||
granted = dataStore.requestPermissions(permSet, mainActivity)
|
||||
|
||||
if(granted.containsAll(granted))
|
||||
result.success("Permission Granted") // adapt result as needed
|
||||
result.error("PermissionError", "Permission Not Granted", null) // adapt result as needed
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "create: getPermission failed", e)
|
||||
result.error("PermissionError", e.message, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"getHeartRate"->{
|
||||
val dateTime = LocalDateTime.now().with(LocalTime.MIDNIGHT)
|
||||
val localTimeFilter = LocalTimeFilter.of(dateTime, dateTime.minusDays(365))
|
||||
val readRequest = DataTypes.HEART_RATE.readDataRequestBuilder
|
||||
.setLocalTimeFilter(localTimeFilter)
|
||||
.setOrdering(Ordering.DESC)
|
||||
.build()
|
||||
|
||||
scope.launch {
|
||||
val heartRateList = dataStore.readData(readRequest).dataList
|
||||
Log.d(TAG, "create: heart rate data count: ${heartRateList}")
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
"closeCoroutineScope"->{
|
||||
destroy()
|
||||
result.success("Coroutine Scope Cancelled")
|
||||
}
|
||||
|
||||
else -> {
|
||||
result.notImplemented()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun destroy() {
|
||||
scope.cancel()
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
|
||||
import 'package:async/async.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
class SamsungPlatformChannel {
|
||||
final MethodChannel _channel = MethodChannel('samsung_watch');
|
||||
Future<Result<bool>> initDevice() async {
|
||||
try{
|
||||
await _channel.invokeMethod('init');
|
||||
return Result.value(true);
|
||||
}catch(e){
|
||||
return Result.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Result<bool>> getRequestedPermission() async {
|
||||
try{
|
||||
await _channel.invokeMethod('getPermission');
|
||||
return Result.value(true);
|
||||
}catch(e){
|
||||
return Result.error(e);
|
||||
}
|
||||
}
|
||||
Future<Result<bool>> getHeartRate() async {
|
||||
try{
|
||||
await _channel.invokeMethod('getHeartRate');
|
||||
return Result.value(true);
|
||||
}catch(e){
|
||||
return Result.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
import 'package:hmg_patient_app_new/core/common_models/smart_watch.dart';
|
||||
import 'package:hmg_patient_app_new/features/smartwatch_health_data/watch_connectors/samsung_health.dart';
|
||||
import 'package:hmg_patient_app_new/features/smartwatch_health_data/watch_connectors/watch_helper.dart';
|
||||
|
||||
class CreateWatchHelper {
|
||||
static WatchHelper getWatchName(SmartWatchTypes watchType) {
|
||||
switch(watchType){
|
||||
case SmartWatchTypes.samsung:
|
||||
return SamsungHealth();
|
||||
default:
|
||||
return SamsungHealth();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:async/src/result/result.dart';
|
||||
import 'package:hmg_patient_app_new/features/smartwatch_health_data/platform_channel/samsung_platform_channel.dart';
|
||||
import 'package:hmg_patient_app_new/features/smartwatch_health_data/watch_connectors/watch_helper.dart' show WatchHelper;
|
||||
|
||||
class SamsungHealth extends WatchHelper {
|
||||
|
||||
final SamsungPlatformChannel platformChannel = SamsungPlatformChannel();
|
||||
|
||||
@override
|
||||
FutureOr<void> getHeartRate() async {
|
||||
await platformChannel.getHeartRate();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Result<bool>> initDevice() async {
|
||||
var result = await platformChannel.initDevice();
|
||||
if(result.isError){
|
||||
return result;
|
||||
}
|
||||
return await platformChannel.getRequestedPermission();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
import 'dart:async';
|
||||
import 'package:async/async.dart';
|
||||
abstract class WatchHelper {
|
||||
Future<Result<bool>> initDevice();
|
||||
FutureOr<void> getHeartRate();
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue