| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- /**
- * 应用主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 { SpiderMan } from '@simplepeng/spider-man';
- import { WXApi, WXEventHandler } from '../common/util/WXApiWrap';
- import { systemShare } from '@kit.ShareKit';
- /**
- * 主Ability类,继承自UIAbility
- * 负责:
- * - 应用初始化
- * - 窗口管理
- * - 事件分发
- */
- export default class EntryAbility extends UIAbility {
- // UI上下文对象,用于获取窗口信息
- private uiContext?: UIContext;
- /**
- * 窗口尺寸变化回调函数
- * @param windowSize 新的窗口尺寸对象
- * 功能:
- * 1. 获取最新的窗口断点尺寸
- * 2. 更新AppStorage中的尺寸状态
- * 3. 记录尺寸变化日志
- */
- private onWindowSizeChange: (windowSize: window.Size) => void = (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);
- };
- async onCreate(want:Want, launchParam:AbilityConstant.LaunchParam) {
- AppStorage.setOrCreate('context', this.context);
- hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
- // if (!canIUse('SystemCapability.FileManagement.AppFileService.FolderAuthorization')) {
- // console.error('this api is not supported on this device');
- // return;
- // }
- 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<void> {
- 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');
- SpiderMan.init();
- 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));
- })
- })
- 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');
- }
- }
|