EntryAbility.ets 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 { IBestInit } from '@ibestservices/ibest-ui';
  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 = 2000
  59. setTimeout(()=>{
  60. this.loadDoWant(want)
  61. },timeout)
  62. this.handleWeChatCallIfNeed(want)
  63. }
  64. onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
  65. hilog.info(0x0000, 'testTag', `onNewWant, want=${JSON.stringify(want)}`);
  66. super.onNewWant(want, launchParam);
  67. this.loadDoWant(want)
  68. this.handleWeChatCallIfNeed(want)
  69. }
  70. private handleWeChatCallIfNeed(want: Want) {
  71. WXApi.handleWant(want, WXEventHandler)
  72. }
  73. //处理其他app点击其他应用打开播放器播放视频或者音频
  74. loadDoWant(want: Want){
  75. let uri = want.uri;
  76. if (uri == null || uri == undefined|| StrUtil.isEmpty(uri)) {
  77. console.info('uri is invalid');
  78. return;
  79. }
  80. hilog.info(0x0000, 'testTag', `onCreate or onNewWant, uri=${uri}`);
  81. const eventData: emitter.EventData = {
  82. data: {
  83. message: uri
  84. }
  85. };
  86. if(Utility.isMeidaByExtension(uri)){
  87. emitter.emit({ eventId: 2 }, eventData); // 发送音频打开广播事件
  88. }else{
  89. emitter.emit({ eventId: 1 }, eventData); // 发送视频打开广播事件
  90. }
  91. }
  92. onDestroy() {
  93. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  94. }
  95. onWindowStageCreate(windowStage: window.WindowStage) {
  96. // Main window is created, set main page for this ability
  97. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
  98. SpiderMan.init();
  99. AppUtil.init(this.context);
  100. //1.获取应用主窗口。
  101. let windowClass: window.Window | null = null;
  102. windowStage.getMainWindow((err: BusinessError, data) => {
  103. windowClass = data;
  104. // LogUtil.info( 'getMainWindow = ');
  105. globalThis.windowClass = data // 赋值给全局变量windowClass
  106. windowClass.setWindowLayoutFullScreen(true).then(() => {
  107. console.info('Succeeded in setting the window layout to full-screen mode.');
  108. }).catch((e: BusinessError) => {
  109. console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(e));
  110. })
  111. })
  112. AppStorage.setOrCreate('windowStage',windowStage);
  113. GlobalContext.getContext().setObject('windowClass',windowClass)
  114. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
  115. windowStage.loadContent('pages/SplashIndex', (err, data) => {
  116. IBestInit(windowStage, this.context)
  117. if (err.code) {
  118. hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
  119. return;
  120. }
  121. //一多断点开发
  122. windowStage.getMainWindow().then((data: window.Window) => {
  123. this.uiContext = data.getUIContext();
  124. let widthBp: WidthBreakpoint = this.uiContext.getWindowWidthBreakpoint();
  125. let heightBp: HeightBreakpoint = this.uiContext.getWindowHeightBreakpoint();
  126. AppStorage.setOrCreate('currentWidthBreakpoint', widthBp);
  127. AppStorage.setOrCreate('currentHeightBreakpoint', heightBp);
  128. LogUtil.info( 'getMainWindow currentHeightBreakpoint= '+heightBp);
  129. LogUtil.info( 'getMainWindow currentWidthBreakpoint= '+widthBp);
  130. data.on('windowSizeChange', this.onWindowSizeChange);
  131. }).catch((err: BusinessError) => {
  132. console.error(`Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}`);
  133. });
  134. hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
  135. });
  136. DemoConstants.windowStage = windowStage
  137. }
  138. onWindowStageDestroy() {
  139. // Main window is destroyed, release UI related resources
  140. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  141. }
  142. onForeground() {
  143. // Ability has brought to foreground
  144. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  145. }
  146. onBackground() {
  147. // Ability has back to background
  148. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  149. }
  150. }