EntryAbility.ets 3.92 KB

import { FlutterAbility, FlutterEngine } from '@ohos/flutter_ohos';
import { GeneratedPluginRegistrant } from '../plugins/GeneratedPluginRegistrant';
import { NativePigeonRegistrar } from '../native/NativePigeonRegistrar';
import { WeChatLoginBridge } from '../native/WeChatLoginBridge';
import { WeChatHostApi } from '../pigeon/WeChatApi';
import Want from '@ohos.app.ability.Want';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import Any from '@ohos/flutter_ohos/src/main/ets/plugin/common/Any';
import MethodCall from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodCall';
import MethodChannel, { MethodCallHandler, MethodResult } from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodChannel';
import { BusinessError } from '@kit.BasicServicesKit';
import { pushService } from '@kit.PushKit';

export default class EntryAbility extends FlutterAbility implements MethodCallHandler {
  private static readonly FLUTTER_MAIN_CHANNEL = 'doublefeel_flutter_main_channel';
  private static readonly OHOS_PUSH_CHANNEL = 'doublefeel_flutter/ohos_push';
  private flutterUrlChannel?: MethodChannel;
  private pushTokenChannel?: MethodChannel;
  private weChatLoginBridge?: WeChatLoginBridge;

  configureFlutterEngine(flutterEngine: FlutterEngine) {
    super.configureFlutterEngine(flutterEngine)
    GeneratedPluginRegistrant.registerWith(flutterEngine)
    NativePigeonRegistrar.register(
      flutterEngine.getDartExecutor()!.getBinaryMessenger()!,
      this.context,
    )
    this.flutterUrlChannel = new MethodChannel(
      flutterEngine.getDartExecutor()!.getBinaryMessenger()!,
      EntryAbility.FLUTTER_MAIN_CHANNEL,
    )
    this.pushTokenChannel = new MethodChannel(
      flutterEngine.getDartExecutor()!.getBinaryMessenger()!,
      EntryAbility.OHOS_PUSH_CHANNEL,
    )
    this.pushTokenChannel.setMethodCallHandler(this)
    NativePigeonRegistrar.setAppForeground(true)
    NativePigeonRegistrar.handleWant(this.getWant())
    this.weChatLoginBridge = new WeChatLoginBridge(this.context)
    WeChatHostApi.setup(
      flutterEngine.getDartExecutor()!.getBinaryMessenger()!,
      this.weChatLoginBridge,
    )
    this.weChatLoginBridge.handleWant(this.getWant())
  }

  onMethodCall(call: MethodCall, result: MethodResult): void {
    if (call.method !== 'getPushToken') {
      result.notImplemented()
      return
    }

    // Push Kit obtains a token asynchronously. Do not request notification
    // permission here: token issuance and the user's notification preference
    // are independent, and permission is requested by the product flow.
    pushService.getToken()
      .then((token: string) => result.success(token))
      .catch((error: BusinessError) => {
        result.error('push_token_unavailable', error.message, error.code)
      })
  }

  onNewWant(want: Want, launchParams: AbilityConstant.LaunchParam): void {
    super.onNewWant(want, launchParams)
    NativePigeonRegistrar.handleWant(want)
    this.forwardFlutterUrl(want)
    this.weChatLoginBridge?.handleWant(want)
  }

  /**
   * Mirrors iOS notification/deep-link handling for an existing Flutter
   * engine. Keep the URL cached until Flutter confirms it handled the route,
   * so a not-yet-ready engine can still consume it during startup.
   */
  private forwardFlutterUrl(want: Want): void {
    const url = want.uri;
    if (url === undefined || url.length === 0) {
      return;
    }
    const result: MethodResult = {
      success: (handled: Any): void => {
        if (handled === true) {
          NativePigeonRegistrar.clearUnhandedUrl(url)
        }
      },
      error: (_code: string, _message: string, _details: Any): void => {},
      notImplemented: (): void => {},
    }
    this.flutterUrlChannel?.invokeMethod('handleFlutterUrl', { url }, result)
  }

  onForeground(): void {
    super.onForeground()
    NativePigeonRegistrar.setAppForeground(true)
  }

  onBackground(): void {
    super.onBackground()
    NativePigeonRegistrar.setAppForeground(false)
  }
}