HealthKitApi.ets 8.98 KB
/*
* // Copyright 2013 The Flutter Authors. All rights reserved.
*/
import StandardMessageCodec from '@ohos/flutter_ohos/src/main/ets/plugin/common/StandardMessageCodec';
import BasicMessageChannel, { Reply } from '@ohos/flutter_ohos/src/main/ets/plugin/common/BasicMessageChannel';
import { BinaryMessenger,TaskQueue } from '@ohos/flutter_ohos/src/main/ets/plugin/common/BinaryMessenger';
import MessageCodec from '@ohos/flutter_ohos/src/main/ets/plugin/common/MessageCodec';
import { ByteBuffer } from '@ohos/flutter_ohos/src/main/ets/util/ByteBuffer';


/** Error class for passing custom error details to Flutter via a thrown PlatformException. */
export class FlutterError implements Error {

  /** The error code. */
  public code: string;

  /** The error name. */
  public name: string;

  /** The error message. */
  public message: string;
  /** The error stack. */
  public stack?: string;

  constructor(code: string, name: string,  message: string, stack?: string) 
  {
    this.code = code;
    this.name = name;
    this.message = message;
    this.stack = stack;
  }
}

function wrapError(error: Error): Array<Object> {
  let errorList: Array<Object> = new Array<Object>(3);
  if (error instanceof FlutterError) {
    let err: FlutterError = error as FlutterError;
    errorList[0] = err.code;
    errorList[1] = err.name;
    errorList[2] = err.message;
  } else {
    errorList[0] = error.toString();
    errorList[1] = error.name;
    errorList[2] = "Cause: " + error.message + ", Stacktrace: " + error.stack;
  }
  return errorList;
}

/* Generated class from Pigeon that represents data sent in messages.*/
export class HealthUploadResult {
  private commonUploadSuccess?: boolean;

  public setCommonUploadSuccess(commonUploadSuccess:boolean):void {
    this.commonUploadSuccess = commonUploadSuccess;
  }

  getCommonUploadSuccess(): boolean | undefined{
    return this.commonUploadSuccess;
  }

  private sleepUploadSuccess?: boolean;

  public setSleepUploadSuccess(sleepUploadSuccess:boolean):void {
    this.sleepUploadSuccess = sleepUploadSuccess;
  }

  getSleepUploadSuccess(): boolean | undefined{
    return this.sleepUploadSuccess;
  }

  private errorMessage?: string;

  public setErrorMessage(errorMessage:string):void {
    this.errorMessage = errorMessage;
  }

  getErrorMessage(): string | undefined{
    return this.errorMessage;
  }

  constructor(commonUploadSuccess?: boolean, sleepUploadSuccess?: boolean, errorMessage?: string) {
    this.commonUploadSuccess = commonUploadSuccess;
    this.sleepUploadSuccess = sleepUploadSuccess;
    this.errorMessage = errorMessage;
  }

  toList(): Object[] {
    let arr: Object[] = new Array();
    if(this.commonUploadSuccess !==undefined){
      arr.push(this.commonUploadSuccess);
    }
    if(this.sleepUploadSuccess !==undefined){
      arr.push(this.sleepUploadSuccess);
    }
    if(this.errorMessage !==undefined){
      arr.push(this.errorMessage);
    }
    return arr;
  }

  static fromList(arr: Object[]): HealthUploadResult {
    let pigeonResult: HealthUploadResult = new HealthUploadResult();
    let commonUploadSuccess: Object = arr[0];
    pigeonResult.setCommonUploadSuccess(commonUploadSuccess as boolean);
    let sleepUploadSuccess: Object = arr[1];
    pigeonResult.setSleepUploadSuccess(sleepUploadSuccess as boolean);
    let errorMessage: Object = arr[2];
    pigeonResult.setErrorMessage(errorMessage as string);
    return pigeonResult;
  }
}

export class PigeonCodec extends StandardMessageCodec {
  static readonly INSTANCE: PigeonCodec  = new PigeonCodec();

  getByte(n: number): number {
    let byteArray = new Uint8Array(1);
    byteArray[0] = n;
    return byteArray[0] as number;
  }
  private constructor() {
    super();
  }

  readValueOfType(type: number,  buffer: ByteBuffer): ESObject {
    switch (type) {
      case this.getByte(129):
        return HealthUploadResult.fromList(super.readValue(buffer) as Object[]);
      default:
        return super.readValueOfType(type, buffer);
    }
  }

  writeValue(stream: ByteBuffer , value: ESObject): ESObject{
    if (value instanceof HealthUploadResult) {
      stream.writeUint8(this.getByte(129));
      this.writeValue(stream, (value as HealthUploadResult).toList());
    } else {
      super.writeValue(stream, value);
    }
  }
}

/* Generated abstract class from Pigeon that represents a handler of messages from Flutter.*/
export abstract class HealthKitHostApi {

  abstract checkHealthAppAuthorization(): boolean;

  abstract getHealthServerAuthUrl(): string;
  /* Opens Huawei Health client authorization UI. Returns whether user granted.*/
  abstract requestHealthClientAuthorization(): boolean;

  abstract cancelHealthAppAuthorization(): boolean;
  /* Runs native health read and server upload pipeline.*/
  abstract performHealthUpload(): HealthUploadResult;
  /** The codec used by HealthKitHostApi. */
  static getCodec(): MessageCodec<Object>{
    return PigeonCodec.INSTANCE;
  }
  /*Sets up an instance of `HealthKitHostApi` to handle messages through the `binaryMessenger`.*/
  static setup(binaryMessenger: BinaryMessenger, api: HealthKitHostApi | null): void {
    {
      let channel: BasicMessageChannel<Object> =
          new BasicMessageChannel(
              binaryMessenger, "dev.flutter.pigeon.doublefeel_flutter.HealthKitHostApi.checkHealthAppAuthorization", HealthKitHostApi.getCodec());
      if (api != null) {
        channel.setMessageHandler({
            onMessage(message: Object ,reply: Reply<Object> ) {
              let res: Array<Object> = [];
              try {
                let output: boolean = api!.checkHealthAppAuthorization();
                res.push(output);
              }
 catch (error) {
                let wrappedError: Array<Object> = wrapError(error);
                res = wrappedError;
              }
              reply.reply(res);
            } });
      } else {
        channel.setMessageHandler(null);
      }
    }
    {
      let channel: BasicMessageChannel<Object> =
          new BasicMessageChannel(
              binaryMessenger, "dev.flutter.pigeon.doublefeel_flutter.HealthKitHostApi.getHealthServerAuthUrl", HealthKitHostApi.getCodec());
      if (api != null) {
        channel.setMessageHandler({
            onMessage(message: Object ,reply: Reply<Object> ) {
              let res: Array<Object> = [];
              try {
                let output: string = api!.getHealthServerAuthUrl();
                res.push(output);
              }
 catch (error) {
                let wrappedError: Array<Object> = wrapError(error);
                res = wrappedError;
              }
              reply.reply(res);
            } });
      } else {
        channel.setMessageHandler(null);
      }
    }
    {
      let channel: BasicMessageChannel<Object> =
          new BasicMessageChannel(
              binaryMessenger, "dev.flutter.pigeon.doublefeel_flutter.HealthKitHostApi.requestHealthClientAuthorization", HealthKitHostApi.getCodec());
      if (api != null) {
        channel.setMessageHandler({
            onMessage(message: Object ,reply: Reply<Object> ) {
              let res: Array<Object> = [];
              try {
                let output: boolean = api!.requestHealthClientAuthorization();
                res.push(output);
              }
 catch (error) {
                let wrappedError: Array<Object> = wrapError(error);
                res = wrappedError;
              }
              reply.reply(res);
            } });
      } else {
        channel.setMessageHandler(null);
      }
    }
    {
      let channel: BasicMessageChannel<Object> =
          new BasicMessageChannel(
              binaryMessenger, "dev.flutter.pigeon.doublefeel_flutter.HealthKitHostApi.cancelHealthAppAuthorization", HealthKitHostApi.getCodec());
      if (api != null) {
        channel.setMessageHandler({
            onMessage(message: Object ,reply: Reply<Object> ) {
              let res: Array<Object> = [];
              try {
                let output: boolean = api!.cancelHealthAppAuthorization();
                res.push(output);
              }
 catch (error) {
                let wrappedError: Array<Object> = wrapError(error);
                res = wrappedError;
              }
              reply.reply(res);
            } });
      } else {
        channel.setMessageHandler(null);
      }
    }
    {
      let channel: BasicMessageChannel<Object> =
          new BasicMessageChannel(
              binaryMessenger, "dev.flutter.pigeon.doublefeel_flutter.HealthKitHostApi.performHealthUpload", HealthKitHostApi.getCodec());
      if (api != null) {
        channel.setMessageHandler({
            onMessage(message: Object ,reply: Reply<Object> ) {
              let res: Array<Object> = [];
              try {
                let output: HealthUploadResult = api!.performHealthUpload();
                res.push(output);
              }
 catch (error) {
                let wrappedError: Array<Object> = wrapError(error);
                res = wrappedError;
              }
              reply.reply(res);
            } });
      } else {
        channel.setMessageHandler(null);
      }
    }
  }
}