ShareUIAbility.ets 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
  2. import { window } from '@kit.ArkUI';
  3. import { systemShare } from '@kit.ShareKit';
  4. import { BusinessError, emitter } from '@kit.BasicServicesKit';
  5. import { hilog } from '@kit.PerformanceAnalysisKit';
  6. import { AppUtil, GlobalContext, LogUtil, StrUtil } from '@pura/harmony-utils';
  7. import { Utility } from '../common/util/Utility';
  8. import { DemoConstants } from './DemoConstants';
  9. import { WXApi, WXEventHandler } from '../common/util/WXApiWrap';
  10. export default class ShareUIAbility extends UIAbility {
  11. //一多断点开发
  12. private uiContext?: UIContext;
  13. private onWindowSizeChange: (windowSize: window.Size) => void = (windowSize: window.Size) => {
  14. let widthBp: WidthBreakpoint = this.uiContext!.getWindowWidthBreakpoint();
  15. AppStorage.setOrCreate('currentWidthBreakpoint', widthBp);
  16. let heightBp: HeightBreakpoint = this.uiContext!.getWindowHeightBreakpoint();
  17. AppStorage.setOrCreate('currentHeightBreakpoint', heightBp);
  18. };
  19. onAvoidAreaChange = (data: window.AvoidAreaOptions) => {
  20. let topRectHeight = px2vp(data.area.topRect.height);
  21. AppStorage.setOrCreate('topRectHeight', topRectHeight);
  22. LogUtil.info('onecold onAvoidAreaChange topRectHeight = '+topRectHeight);
  23. let bottomRectHeight = px2vp(data.area.bottomRect.height);
  24. AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight);
  25. }
  26. onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
  27. AppStorage.setOrCreate('context', this.context);
  28. let timeout = 2000
  29. // if(Utility.isNoble())
  30. // timeout = 2000
  31. setTimeout(()=>{
  32. this.handleShare(want)//分享打开的音频
  33. },timeout)
  34. this.handleWeChatCallIfNeed(want)
  35. }
  36. onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
  37. hilog.info(0x0000, 'ShareUIAbility', `onNewWant, want=${JSON.stringify(want)}`);
  38. super.onNewWant(want, launchParam);
  39. this.handleShare(want)
  40. this.handleWeChatCallIfNeed(want)
  41. }
  42. private handleWeChatCallIfNeed(want: Want) {
  43. WXApi.handleWant(want, WXEventHandler)
  44. }
  45. //处理其他app点击分享的视频或音频的打开本app播放器播放视频或者音频
  46. handleShare(want: Want){
  47. systemShare.getSharedData(want)
  48. .then((data: systemShare.SharedData) => {
  49. data.getRecords().forEach((record: systemShare.SharedRecord) => {
  50. let uri = record.uri
  51. if (uri == null || uri == undefined|| StrUtil.isEmpty(uri)) {
  52. console.info('uri is invalid');
  53. return;
  54. }
  55. hilog.info(0x0000, 'testTag', `onCreate or onNewWant, uri=${uri}`);
  56. const eventData: emitter.EventData = {
  57. data: {
  58. message: uri
  59. }
  60. };
  61. if(Utility.isMeidaByExtension(uri)){
  62. emitter.emit({ eventId: 2 }, eventData); // 发送音频打开广播事件
  63. }else{
  64. emitter.emit({ eventId: 1 }, eventData); // 发送视频打开广播事件
  65. }
  66. // 处理分享数据
  67. });
  68. })
  69. .catch((error: BusinessError) => {
  70. console.error(`Failed to getSharedData. Code: ${error.code}, message: ${error.message}`);
  71. this.context.terminateSelf();
  72. });
  73. }
  74. onWindowStageCreate(windowStage: window.WindowStage): void {
  75. // Main window is created, set main page for this ability
  76. AppUtil.init(this.context);
  77. // PreferencesUtil.init(CommonConstants.PLAY_STORE)
  78. // Main window is created, set main page for this ability
  79. //1.获取应用主窗口。
  80. let windowClass: window.Window | null = null;
  81. windowStage.getMainWindow((err: BusinessError, data) => {
  82. windowClass = data;
  83. globalThis.windowClass = data // 赋值给全局变量windowClass
  84. let avoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)
  85. let topRectHeight = px2vp(avoidArea.topRect.height);
  86. AppStorage.setOrCreate('topRectHeight', topRectHeight);
  87. LogUtil.info('onecold topRectHeight = '+topRectHeight);
  88. windowClass.on('avoidAreaChange', this.onAvoidAreaChange)
  89. })
  90. AppStorage.setOrCreate('windowStage',windowStage);
  91. GlobalContext.getContext().setObject('windowClass',windowClass)
  92. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
  93. windowStage.loadContent('pages/SplashIndex', (err, data) => {
  94. if (err.code) {
  95. hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
  96. return;
  97. }
  98. //一多断点开发
  99. windowStage.getMainWindow().then((data: window.Window) => {
  100. this.uiContext = data.getUIContext();
  101. let widthBp: WidthBreakpoint = this.uiContext.getWindowWidthBreakpoint();
  102. let heightBp: HeightBreakpoint = this.uiContext.getWindowHeightBreakpoint();
  103. AppStorage.setOrCreate('currentWidthBreakpoint', widthBp);
  104. AppStorage.setOrCreate('currentHeightBreakpoint', heightBp);
  105. data.on('windowSizeChange', this.onWindowSizeChange);
  106. }).catch((err: BusinessError) => {
  107. console.error(`Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}`);
  108. });
  109. hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
  110. });
  111. DemoConstants.windowStage = windowStage
  112. }
  113. }