HealthKitApi.ets 16 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
/*
* // 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 } 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 | null> {
  let errorList: Array<Object | null> = new Array<Object | null>(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 HealthAuthorization {
  private status: number;

  public setStatus(status: number): void {
    this.status = status;
  }

  getStatus(): number {
    return this.status;
  }

  private hasData: boolean;

  public setHasData(hasData: boolean): void {
    this.hasData = hasData;
  }

  getHasData(): boolean {
    return this.hasData;
  }

  constructor(status: number, hasData: boolean) {
    this.status = status;
    this.hasData = hasData;
  }

  toList(): Array<Object | null> {
    let arr: Array<Object | null> = new Array<Object | null>();
    if (this.status === undefined || this.status === null) {
      arr.push(null);
    } else {
      arr.push(this.status);
    }
    if (this.hasData === undefined || this.hasData === null) {
      arr.push(null);
    } else {
      arr.push(this.hasData);
    }
    return arr;
  }

  static fromList(arr: Object[]): HealthAuthorization {
    let statusObject: Object = arr[0];
    const status: number = statusObject as number;
    let hasDataObject: Object = arr[1];
    const hasData: boolean = hasDataObject as boolean;
    return new HealthAuthorization(status, hasData);
  }
}

/*
*  The user's current Huawei Health activity-ring goals.
* 
*  Units: move is kcal, exercise is seconds, and stand is hours.
* 
*  Generated class from Pigeon that represents data sent in messages.
*/
export class HealthActivityGoal {
  private move?: number;

  public setMove(move: number | undefined): void {
    this.move = move;
  }

  getMove(): number | undefined {
    return this.move;
  }

  private step?: number;

  public setStep(step: number | undefined): void {
    this.step = step;
  }

  getStep(): number | undefined {
    return this.step;
  }

  private stand?: number;

  public setStand(stand: number | undefined): void {
    this.stand = stand;
  }

  getStand(): number | undefined {
    return this.stand;
  }

  private exercise?: number;

  public setExercise(exercise: number | undefined): void {
    this.exercise = exercise;
  }

  getExercise(): number | undefined {
    return this.exercise;
  }

  constructor(move?: number, step?: number, stand?: number, exercise?: number) {
    this.move = move;
    this.step = step;
    this.stand = stand;
    this.exercise = exercise;
  }

  toList(): Array<Object | null> {
    let arr: Array<Object | null> = new Array<Object | null>();
    if (this.move === undefined || this.move === null) {
      arr.push(null);
    } else {
      arr.push(this.move);
    }
    if (this.step === undefined || this.step === null) {
      arr.push(null);
    } else {
      arr.push(this.step);
    }
    if (this.stand === undefined || this.stand === null) {
      arr.push(null);
    } else {
      arr.push(this.stand);
    }
    if (this.exercise === undefined || this.exercise === null) {
      arr.push(null);
    } else {
      arr.push(this.exercise);
    }
    return arr;
  }

  static fromList(arr: Object[]): HealthActivityGoal {
    let move: number | undefined = undefined;
    if (arr[0] !== null && arr[0] !== undefined) {
      let moveObject: Object = arr[0];
      move = moveObject as number | undefined;
    }
    let step: number | undefined = undefined;
    if (arr[1] !== null && arr[1] !== undefined) {
      let stepObject: Object = arr[1];
      step = stepObject as number | undefined;
    }
    let stand: number | undefined = undefined;
    if (arr[2] !== null && arr[2] !== undefined) {
      let standObject: Object = arr[2];
      stand = standObject as number | undefined;
    }
    let exercise: number | undefined = undefined;
    if (arr[3] !== null && arr[3] !== undefined) {
      let exerciseObject: Object = arr[3];
      exercise = exerciseObject as number | undefined;
    }
    return new HealthActivityGoal(move, step, stand, exercise);
  }
}

/*
*  One workout record read from Health Service Kit.
* 
*  Generated class from Pigeon that represents data sent in messages.
*/
export class HealthWorkoutDataPoint {
  private startTime: number;

  public setStartTime(startTime: number): void {
    this.startTime = startTime;
  }

  getStartTime(): number {
    return this.startTime;
  }

  private endTime: number;

  public setEndTime(endTime: number): void {
    this.endTime = endTime;
  }

  getEndTime(): number {
    return this.endTime;
  }

  private activityType: number;

  public setActivityType(activityType: number): void {
    this.activityType = activityType;
  }

  getActivityType(): number {
    return this.activityType;
  }

  constructor(startTime: number, endTime: number, activityType: number) {
    this.startTime = startTime;
    this.endTime = endTime;
    this.activityType = activityType;
  }

  toList(): Array<Object | null> {
    let arr: Array<Object | null> = new Array<Object | null>();
    if (this.startTime === undefined || this.startTime === null) {
      arr.push(null);
    } else {
      arr.push(this.startTime);
    }
    if (this.endTime === undefined || this.endTime === null) {
      arr.push(null);
    } else {
      arr.push(this.endTime);
    }
    if (this.activityType === undefined || this.activityType === null) {
      arr.push(null);
    } else {
      arr.push(this.activityType);
    }
    return arr;
  }

  static fromList(arr: Object[]): HealthWorkoutDataPoint {
    let startTimeObject: Object = arr[0];
    const startTime: number = startTimeObject as number;
    let endTimeObject: Object = arr[1];
    const endTime: number = endTimeObject as number;
    let activityTypeObject: Object = arr[2];
    const activityType: number = activityTypeObject as number;
    return new HealthWorkoutDataPoint(startTime, endTime, activityType);
  }
}

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 HealthAuthorization.fromList(super.readValue(buffer) as Object[]);
      case this.getByte(130):
        return HealthActivityGoal.fromList(super.readValue(buffer) as Object[]);
      case this.getByte(131):
        return HealthWorkoutDataPoint.fromList(super.readValue(buffer) as Object[]);
      default:
        return super.readValueOfType(type, buffer);
    }
  }

  writeValue(stream: ByteBuffer, value: ESObject): ESObject {
    if (value instanceof HealthAuthorization) {
      stream.writeUint8(this.getByte(129));
      this.writeValue(stream, (value as HealthAuthorization).toList());
    } else if (value instanceof HealthActivityGoal) {
      stream.writeUint8(this.getByte(130));
      this.writeValue(stream, (value as HealthActivityGoal).toList());
    } else if (value instanceof HealthWorkoutDataPoint) {
      stream.writeUint8(this.getByte(131));
      this.writeValue(stream, (value as HealthWorkoutDataPoint).toList());
    } else {
      super.writeValue(stream, value);
    }
  }
}


export interface Result<T> {
  success(result: T): void;

  error(error: Error): void;
}
/* Generated abstract class from Pigeon that represents a handler of messages from Flutter.*/
export abstract class HealthKitHostApi {
  /* Opens Huawei Health client authorization UI. Returns whether user granted.*/
  abstract checkHealthAppAuthorization(result: Result<HealthAuthorization>): void;

  abstract requestHealthClientAuthorization(result: Result<boolean>): void;
  /*
  *  Requests Huawei Health to upload the user's latest data to its cloud.
  * 
  *  This only triggers the Health app's configured device-to-cloud sync; it
  *  does not wait for, or return, the subsequently available cloud data.
  */
  abstract syncHealthDataToCloud(result: Result<boolean>): void;
  /* Reads the current activity-ring goals from Huawei Health on OHOS.*/
  abstract readActivityGoal(result: Result<HealthActivityGoal | undefined>): void;
  /* Reads workout records in the supplied Unix-seconds time range on OHOS.*/
  abstract readWorkoutData(startTime: number, endTime: number, result: Result<Array<HealthWorkoutDataPoint>>): void;
  /** 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, messageChannelSuffix: string = ''): void {
    const separatedMessageChannelSuffix: string = messageChannelSuffix !== '' ? '.' + messageChannelSuffix : '';
    {
      let channel: BasicMessageChannel<Object> =
          new BasicMessageChannel(
              binaryMessenger, 'dev.flutter.pigeon.doublefeel_flutter.HealthKitHostApi.checkHealthAppAuthorization' + separatedMessageChannelSuffix, HealthKitHostApi.getCodec());
      if (api != null) {
        channel.setMessageHandler({
            onMessage(message: Object, reply: Reply<Object>) {
              class ResultImp implements Result<HealthAuthorization>{
                    success(result: HealthAuthorization): void {
                      let res: Array<Object | null> = [];
                      res.push(result);
                      reply.reply(res);
                    }

                    error(error: Error): void {
                      let wrappedError: Array<Object | null> = wrapError(error);
                      reply.reply(wrappedError);
                    }
              }
              let resultCallback: Result<HealthAuthorization> = new ResultImp();

              api!.checkHealthAppAuthorization(resultCallback);
            } });
      } else {
        channel.setMessageHandler(null);
      }
    }
    {
      let channel: BasicMessageChannel<Object> =
          new BasicMessageChannel(
              binaryMessenger, 'dev.flutter.pigeon.doublefeel_flutter.HealthKitHostApi.requestHealthClientAuthorization' + separatedMessageChannelSuffix, HealthKitHostApi.getCodec());
      if (api != null) {
        channel.setMessageHandler({
            onMessage(message: Object, reply: Reply<Object>) {
              class ResultImp implements Result<boolean>{
                    success(result: boolean): void {
                      let res: Array<Object | null> = [];
                      res.push(result);
                      reply.reply(res);
                    }

                    error(error: Error): void {
                      let wrappedError: Array<Object | null> = wrapError(error);
                      reply.reply(wrappedError);
                    }
              }
              let resultCallback: Result<boolean> = new ResultImp();

              api!.requestHealthClientAuthorization(resultCallback);
            } });
      } else {
        channel.setMessageHandler(null);
      }
    }
    {
      let channel: BasicMessageChannel<Object> =
          new BasicMessageChannel(
              binaryMessenger, 'dev.flutter.pigeon.doublefeel_flutter.HealthKitHostApi.syncHealthDataToCloud' + separatedMessageChannelSuffix, HealthKitHostApi.getCodec());
      if (api != null) {
        channel.setMessageHandler({
            onMessage(message: Object, reply: Reply<Object>) {
              class ResultImp implements Result<boolean>{
                    success(result: boolean): void {
                      let res: Array<Object | null> = [];
                      res.push(result);
                      reply.reply(res);
                    }

                    error(error: Error): void {
                      let wrappedError: Array<Object | null> = wrapError(error);
                      reply.reply(wrappedError);
                    }
              }
              let resultCallback: Result<boolean> = new ResultImp();

              api!.syncHealthDataToCloud(resultCallback);
            } });
      } else {
        channel.setMessageHandler(null);
      }
    }
    {
      let channel: BasicMessageChannel<Object> =
          new BasicMessageChannel(
              binaryMessenger, 'dev.flutter.pigeon.doublefeel_flutter.HealthKitHostApi.readActivityGoal' + separatedMessageChannelSuffix, HealthKitHostApi.getCodec());
      if (api != null) {
        channel.setMessageHandler({
            onMessage(message: Object, reply: Reply<Object>) {
              class ResultImp implements Result<HealthActivityGoal | undefined>{
                    success(result: HealthActivityGoal | undefined): void {
                      let res: Array<Object | null> = [];
                      res.push(result);
                      reply.reply(res);
                    }

                    error(error: Error): void {
                      let wrappedError: Array<Object | null> = wrapError(error);
                      reply.reply(wrappedError);
                    }
              }
              let resultCallback: Result<HealthActivityGoal | undefined> = new ResultImp();

              api!.readActivityGoal(resultCallback);
            } });
      } else {
        channel.setMessageHandler(null);
      }
    }
    {
      let channel: BasicMessageChannel<Object> =
          new BasicMessageChannel(
              binaryMessenger, 'dev.flutter.pigeon.doublefeel_flutter.HealthKitHostApi.readWorkoutData' + separatedMessageChannelSuffix, HealthKitHostApi.getCodec());
      if (api != null) {
        channel.setMessageHandler({
            onMessage(message: Object, reply: Reply<Object>) {
              if (!(Array.isArray(message))) {
                  reply.reply(wrapError(new Error('Invalid Pigeon message: expected List.')));
                  return;
              }
              let args: Array<Object> = message as Array<Object>;
              if (args.length < 2) {
                  reply.reply(wrapError(new Error('Invalid Pigeon message: expected at least 2 argument(s).')));
                  return;
              }
              class ResultImp implements Result<Array<HealthWorkoutDataPoint>>{
                    success(result: Array<HealthWorkoutDataPoint>): void {
                      let res: Array<Object | null> = [];
                      res.push(result);
                      reply.reply(res);
                    }

                    error(error: Error): void {
                      let wrappedError: Array<Object | null> = wrapError(error);
                      reply.reply(wrappedError);
                    }
              }
              let resultCallback: Result<Array<HealthWorkoutDataPoint>> = new ResultImp();

              api!.readWorkoutData(args[0] as number, args[1] as number, resultCallback);
            } });
      } else {
        channel.setMessageHandler(null);
      }
    }
  }
}