EntryAbility.ets 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * 应用主Ability入口文件
  3. * 功能:
  4. * 1. 管理应用生命周期
  5. * 2. 处理窗口创建和尺寸变化
  6. * 3. 响应外部调用请求
  7. * 4. 全局状态管理
  8. */
  9. import UIAbility from '@ohos.app.ability.UIAbility';
  10. import hilog from '@ohos.hilog';
  11. import window from '@ohos.window';
  12. import { AbilityConstant, Want } from '@kit.AbilityKit';
  13. import { BusinessError, emitter } from '@kit.BasicServicesKit';
  14. import { AppUtil, ArrayUtil, GlobalContext, LogUtil, StrUtil } from '@pura/harmony-utils';
  15. import { DemoConstants } from './DemoConstants';
  16. import { Utility } from '../common/util/Utility';
  17. import { SpiderMan } from '@simplepeng/spider-man';
  18. import { WXApi, WXEventHandler } from '../common/util/WXApiWrap';
  19. import { systemShare } from '@kit.ShareKit';
  20. /**
  21. * 主Ability类,继承自UIAbility
  22. * 负责:
  23. * - 应用初始化
  24. * - 窗口管理
  25. * - 事件分发
  26. */
  27. export default class EntryAbility extends UIAbility {
  28. // UI上下文对象,用于获取窗口信息
  29. private uiContext?: UIContext;
  30. /**
  31. * 窗口尺寸变化回调函数
  32. * @param windowSize 新的窗口尺寸对象
  33. * 功能:
  34. * 1. 获取最新的窗口断点尺寸
  35. * 2. 更新AppStorage中的尺寸状态
  36. * 3. 记录尺寸变化日志
  37. */
  38. private onWindowSizeChange: (windowSize: window.Size) => void = (windowSize: window.Size) => {
  39. // 获取宽度断点并更新全局状态
  40. let widthBp: WidthBreakpoint = this.uiContext!.getWindowWidthBreakpoint();
  41. AppStorage.setOrCreate('currentWidthBreakpoint', widthBp);
  42. // 获取高度断点并更新全局状态
  43. let heightBp: HeightBreakpoint = this.uiContext!.getWindowHeightBreakpoint();
  44. AppStorage.setOrCreate('currentHeightBreakpoint', heightBp);
  45. // 记录尺寸变化日志
  46. LogUtil.info('pura onWindowSizeChange currentHeightBreakpoint= '+heightBp);
  47. LogUtil.info( 'pura onWindowSizeChange currentWidthBreakpoint= '+widthBp);
  48. };
  49. async onCreate(want:Want, launchParam:AbilityConstant.LaunchParam) {
  50. AppStorage.setOrCreate('context', this.context);
  51. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  52. // if (!canIUse('SystemCapability.FileManagement.AppFileService.FolderAuthorization')) {
  53. // console.error('this api is not supported on this device');
  54. // return;
  55. // }
  56. let timeout = 5000
  57. if(Utility.isNoble())
  58. timeout = 3000
  59. setTimeout(async ()=>{
  60. this.loadDoWant(want)
  61. // await this.handleParam(want)
  62. },timeout)
  63. this.handleWeChatCallIfNeed(want)
  64. }
  65. async onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): Promise<void> {
  66. hilog.info(0x0000, 'testTag', `onNewWant, want=${JSON.stringify(want)}`);
  67. super.onNewWant(want, launchParam);
  68. this.loadDoWant(want)
  69. // await this.handleParam(want)
  70. this.handleWeChatCallIfNeed(want)
  71. }
  72. private handleWeChatCallIfNeed(want: Want) {
  73. WXApi.handleWant(want, WXEventHandler)
  74. }
  75. //处理其他app点击其他应用打开播放器播放视频或者音频
  76. loadDoWant(want: Want){
  77. let uri = want.uri;
  78. if (uri == null || uri == undefined|| StrUtil.isEmpty(uri)) {
  79. console.info('uri is invalid');
  80. return;
  81. }
  82. hilog.info(0x0000, 'testTag', `onCreate or onNewWant, uri=${uri}`);
  83. const eventData: emitter.EventData = {
  84. data: {
  85. message: uri
  86. }
  87. };
  88. if(Utility.isMeidaByExtension(uri)){
  89. emitter.emit({ eventId: 2 }, eventData); // 发送音频打开广播事件
  90. }else{
  91. emitter.emit({ eventId: 1 }, eventData); // 发送视频打开广播事件
  92. }
  93. }
  94. // 华为分享拉起接收
  95. // 1. 改造 handleParam 为异步函数,让其返回 Promise
  96. async handleParam(want: Want) {
  97. try {
  98. // 通过 await 等待异步操作完成
  99. const data = await systemShare.getSharedData(want);
  100. const records = data.getRecords();
  101. let uri = want.uri;
  102. for (const record of records) {
  103. if (record.uri) {
  104. uri = record.uri;
  105. break;
  106. }
  107. }
  108. } catch (error) {
  109. const businessError = error as BusinessError;
  110. console.error(`Failed: Code ${businessError.code}, ${businessError.message}`);
  111. }
  112. }
  113. onDestroy() {
  114. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  115. }
  116. onWindowStageCreate(windowStage: window.WindowStage) {
  117. // Main window is created, set main page for this ability
  118. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
  119. SpiderMan.init();
  120. AppUtil.init(this.context);
  121. //1.获取应用主窗口。
  122. let windowClass: window.Window | null = null;
  123. windowStage.getMainWindow((err: BusinessError, data) => {
  124. windowClass = data;
  125. // LogUtil.info( 'getMainWindow = ');
  126. globalThis.windowClass = data // 赋值给全局变量windowClass
  127. windowClass.setWindowLayoutFullScreen(true).then(() => {
  128. console.info('Succeeded in setting the window layout to full-screen mode.');
  129. }).catch((e: BusinessError) => {
  130. console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(e));
  131. })
  132. })
  133. AppStorage.setOrCreate('windowStage',windowStage);
  134. GlobalContext.getContext().setObject('windowClass',windowClass)
  135. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
  136. windowStage.loadContent('pages/SplashIndex', (err, data) => {
  137. if (err.code) {
  138. hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
  139. return;
  140. }
  141. //一多断点开发
  142. windowStage.getMainWindow().then((data: window.Window) => {
  143. this.uiContext = data.getUIContext();
  144. let widthBp: WidthBreakpoint = this.uiContext.getWindowWidthBreakpoint();
  145. let heightBp: HeightBreakpoint = this.uiContext.getWindowHeightBreakpoint();
  146. AppStorage.setOrCreate('currentWidthBreakpoint', widthBp);
  147. AppStorage.setOrCreate('currentHeightBreakpoint', heightBp);
  148. LogUtil.info( 'getMainWindow currentHeightBreakpoint= '+heightBp);
  149. LogUtil.info( 'getMainWindow currentWidthBreakpoint= '+widthBp);
  150. data.on('windowSizeChange', this.onWindowSizeChange);
  151. }).catch((err: BusinessError) => {
  152. console.error(`Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}`);
  153. });
  154. hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
  155. });
  156. DemoConstants.windowStage = windowStage
  157. }
  158. onWindowStageDestroy() {
  159. // Main window is destroyed, release UI related resources
  160. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  161. }
  162. onForeground() {
  163. // Ability has brought to foreground
  164. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  165. }
  166. onBackground() {
  167. // Ability has back to background
  168. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  169. }
  170. }