import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; import { window } from '@kit.ArkUI'; import { systemShare } from '@kit.ShareKit'; import { BusinessError, emitter } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { AppUtil, GlobalContext, LogUtil, StrUtil } from '@pura/harmony-utils'; import { Utility } from '../common/util/Utility'; import { DemoConstants } from './DemoConstants'; import { WXApi, WXEventHandler } from '../common/util/WXApiWrap'; export default class ShareUIAbility extends UIAbility { //一多断点开发 private uiContext?: UIContext; 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); }; onAvoidAreaChange = (data: window.AvoidAreaOptions) => { let topRectHeight = px2vp(data.area.topRect.height); AppStorage.setOrCreate('topRectHeight', topRectHeight); LogUtil.info('onecold onAvoidAreaChange topRectHeight = '+topRectHeight); let bottomRectHeight = px2vp(data.area.bottomRect.height); AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight); } onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { AppStorage.setOrCreate('context', this.context); let timeout = 2000 // if(Utility.isNoble()) // timeout = 2000 setTimeout(()=>{ this.handleShare(want)//分享打开的音频 },timeout) this.handleWeChatCallIfNeed(want) } onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { hilog.info(0x0000, 'ShareUIAbility', `onNewWant, want=${JSON.stringify(want)}`); super.onNewWant(want, launchParam); this.handleShare(want) this.handleWeChatCallIfNeed(want) } private handleWeChatCallIfNeed(want: Want) { WXApi.handleWant(want, WXEventHandler) } //处理其他app点击分享的视频或音频的打开本app播放器播放视频或者音频 handleShare(want: Want){ systemShare.getSharedData(want) .then((data: systemShare.SharedData) => { data.getRecords().forEach((record: systemShare.SharedRecord) => { let uri = record.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); // 发送视频打开广播事件 } // 处理分享数据 }); }) .catch((error: BusinessError) => { console.error(`Failed to getSharedData. Code: ${error.code}, message: ${error.message}`); this.context.terminateSelf(); }); } onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability AppUtil.init(this.context); // PreferencesUtil.init(CommonConstants.PLAY_STORE) // Main window is created, set main page for this ability //1.获取应用主窗口。 let windowClass: window.Window | null = null; windowStage.getMainWindow((err: BusinessError, data) => { windowClass = data; globalThis.windowClass = data // 赋值给全局变量windowClass let avoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM) 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); 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 } }