EntryAbility.ets 8.9 KB

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