/** * 应用主Ability入口文件 * 功能: * 1. 管理应用生命周期 * 2. 处理窗口创建和尺寸变化 * 3. 响应外部调用请求 * 4. 全局状态管理 */ import UIAbility from '@ohos.app.ability.UIAbility'; import hilog from '@ohos.hilog'; import window from '@ohos.window'; import { AbilityConstant, Want } from '@kit.AbilityKit'; import { BusinessError, emitter } from '@kit.BasicServicesKit'; import { AppUtil, ArrayUtil, GlobalContext, LogUtil, StrUtil } from '@pura/harmony-utils'; import { DemoConstants } from './DemoConstants'; import { Utility } from '../common/util/Utility'; import { WXApi, WXEventHandler } from '../common/util/WXApiWrap'; import { systemShare } from '@kit.ShareKit'; import Logger from '../common/util/Logger'; import statusBarManager from '@hms.pcService.statusBarManager'; import StatusBarViewExtensionAbility from '@hms.pcService.StatusBarViewExtensionAbility'; /** * 主Ability类,继承自UIAbility * 负责: * - 应用初始化 * - 窗口管理 * - 事件分发 */ export default class EntryAbility extends UIAbility { // UI上下文对象,用于获取窗口信息 private uiContext?: UIContext; /** * 窗口尺寸变化回调函数 * @param windowSize 新的窗口尺寸对象 * 功能: * 1. 获取最新的窗口断点尺寸 * 2. 更新AppStorage中的尺寸状态 * 3. 记录尺寸变化日志 */ private onWindowSizeChange: (windowSize: window.Size) => void = async (windowSize: window.Size) => { // 获取宽度断点并更新全局状态 let widthBp: WidthBreakpoint = this.uiContext!.getWindowWidthBreakpoint(); AppStorage.setOrCreate('currentWidthBreakpoint', widthBp); // 获取高度断点并更新全局状态 let heightBp: HeightBreakpoint = this.uiContext!.getWindowHeightBreakpoint(); AppStorage.setOrCreate('currentHeightBreakpoint', heightBp); // 记录尺寸变化日志 LogUtil.info('pura onWindowSizeChange currentHeightBreakpoint= '+heightBp); LogUtil.info( 'pura onWindowSizeChange currentWidthBreakpoint= '+widthBp); }; onAvoidAreaChange = (data: window.AvoidAreaOptions) => { if (data.type === window.AvoidAreaType.TYPE_SYSTEM) { let topRectHeight = px2vp(data.area.topRect.height); AppStorage.setOrCreate('topRectHeight', topRectHeight); } else if (data.type === window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) { let bottomRectHeight = px2vp(data.area.bottomRect.height); AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight); } } async onCreate(want:Want, launchParam:AbilityConstant.LaunchParam) { AppStorage.setOrCreate('context', this.context); hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); let timeout = 5000 if(Utility.isNoble()) timeout = 3000 setTimeout(async ()=>{ this.loadDoWant(want) // await this.handleParam(want) },timeout) this.handleWeChatCallIfNeed(want) } async onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): Promise { hilog.info(0x0000, 'testTag', `onNewWant, want=${JSON.stringify(want)}`); super.onNewWant(want, launchParam); this.loadDoWant(want) // await this.handleParam(want) this.handleWeChatCallIfNeed(want) } private handleWeChatCallIfNeed(want: Want) { WXApi.handleWant(want, WXEventHandler) } //处理其他app点击其他应用打开播放器播放视频或者音频 loadDoWant(want: Want){ let uri = want.uri; if (uri == null || uri == undefined|| StrUtil.isEmpty(uri)) { console.info('uri is invalid'); return; } hilog.info(0x0000, 'testTag', `onCreate or onNewWant, uri=${uri}`); const eventData: emitter.EventData = { data: { message: uri } }; if(Utility.isMeidaByExtension(uri)){ emitter.emit({ eventId: 2 }, eventData); // 发送音频打开广播事件 }else{ emitter.emit({ eventId: 1 }, eventData); // 发送视频打开广播事件 } } // 华为分享拉起接收 // 1. 改造 handleParam 为异步函数,让其返回 Promise async handleParam(want: Want) { try { // 通过 await 等待异步操作完成 const data = await systemShare.getSharedData(want); const records = data.getRecords(); let uri = want.uri; for (const record of records) { if (record.uri) { uri = record.uri; break; } } } catch (error) { const businessError = error as BusinessError; console.error(`Failed: Code ${businessError.code}, ${businessError.message}`); } } onDestroy() { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); } onWindowStageCreate(windowStage: window.WindowStage) { // Main window is created, set main page for this ability hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');; AppUtil.init(this.context); //1.获取应用主窗口。 let windowClass: window.Window | null = null; windowStage.getMainWindow((err: BusinessError, data) => { windowClass = data; // LogUtil.info( 'getMainWindow = '); globalThis.windowClass = data // 赋值给全局变量windowClass windowClass.setWindowLayoutFullScreen(true).then(() => { console.info('Succeeded in setting the window layout to full-screen mode.'); }).catch((e: BusinessError) => { console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(e)); }) // let avoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM) // let topRectHeight = px2vp(avoidArea.topRect.height); // AppStorage.setOrCreate('topRectHeight', topRectHeight); let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; // 以导航条避让为例 let avoidArea = windowClass.getWindowAvoidArea(type); let bottomRectHeight = px2vp(avoidArea.bottomRect.height); // 获取到导航条区域的高度 AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight); type = window.AvoidAreaType.TYPE_SYSTEM; // 以状态栏避让为例 avoidArea = windowClass.getWindowAvoidArea(type); let topRectHeight = px2vp(avoidArea.topRect.height) // 获取状态栏区域高度 AppStorage.setOrCreate('topRectHeight', topRectHeight); LogUtil.info('onecold topRectHeight = '+topRectHeight); windowClass.on('avoidAreaChange', this.onAvoidAreaChange) }) AppStorage.setOrCreate('windowStage',windowStage); GlobalContext.getContext().setObject('windowClass',windowClass) hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); windowStage.loadContent('pages/SplashIndex', (err, data) => { if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; } //一多断点开发 windowStage.getMainWindow().then((data: window.Window) => { this.uiContext = data.getUIContext(); let widthBp: WidthBreakpoint = this.uiContext.getWindowWidthBreakpoint(); let heightBp: HeightBreakpoint = this.uiContext.getWindowHeightBreakpoint(); AppStorage.setOrCreate('currentWidthBreakpoint', widthBp); AppStorage.setOrCreate('currentHeightBreakpoint', heightBp); LogUtil.info( 'getMainWindow currentHeightBreakpoint= '+heightBp); LogUtil.info( 'getMainWindow currentWidthBreakpoint= '+widthBp); data.on('windowSizeChange', this.onWindowSizeChange); }).catch((err: BusinessError) => { console.error(`Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}`); }); hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); }); DemoConstants.windowStage = windowStage } onWindowStageDestroy() { // Main window is destroyed, release UI related resources hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); } onForeground() { // Ability has brought to foreground hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); } onBackground() { // Ability has back to background hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); } }