ShareUIAbility.ets 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, 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. onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
  20. AppStorage.setOrCreate('context', this.context);
  21. let timeout = 5000
  22. if(Utility.isNoble())
  23. timeout = 2000
  24. setTimeout(()=>{
  25. this.handleShare(want)//分享打开的音频
  26. },timeout)
  27. this.handleWeChatCallIfNeed(want)
  28. }
  29. onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
  30. hilog.info(0x0000, 'ShareUIAbility', `onNewWant, want=${JSON.stringify(want)}`);
  31. super.onNewWant(want, launchParam);
  32. this.handleShare(want)
  33. this.handleWeChatCallIfNeed(want)
  34. }
  35. private handleWeChatCallIfNeed(want: Want) {
  36. WXApi.handleWant(want, WXEventHandler)
  37. }
  38. //处理其他app点击分享的视频或音频的打开本app播放器播放视频或者音频
  39. handleShare(want: Want){
  40. systemShare.getSharedData(want)
  41. .then((data: systemShare.SharedData) => {
  42. data.getRecords().forEach((record: systemShare.SharedRecord) => {
  43. let uri = record.uri
  44. if (uri == null || uri == undefined|| StrUtil.isEmpty(uri)) {
  45. console.info('uri is invalid');
  46. return;
  47. }
  48. hilog.info(0x0000, 'testTag', `onCreate or onNewWant, uri=${uri}`);
  49. const eventData: emitter.EventData = {
  50. data: {
  51. message: uri
  52. }
  53. };
  54. if(Utility.isMeidaByExtension(uri)){
  55. emitter.emit({ eventId: 2 }, eventData); // 发送音频打开广播事件
  56. }else{
  57. emitter.emit({ eventId: 1 }, eventData); // 发送视频打开广播事件
  58. }
  59. // 处理分享数据
  60. });
  61. })
  62. .catch((error: BusinessError) => {
  63. console.error(`Failed to getSharedData. Code: ${error.code}, message: ${error.message}`);
  64. this.context.terminateSelf();
  65. });
  66. }
  67. onWindowStageCreate(windowStage: window.WindowStage): void {
  68. // Main window is created, set main page for this ability
  69. AppUtil.init(this.context);
  70. // PreferencesUtil.init(CommonConstants.PLAY_STORE)
  71. // Main window is created, set main page for this ability
  72. //1.获取应用主窗口。
  73. let windowClass: window.Window | null = null;
  74. windowStage.getMainWindow((err: BusinessError, data) => {
  75. windowClass = data;
  76. globalThis.windowClass = data // 赋值给全局变量windowClass
  77. })
  78. AppStorage.setOrCreate('windowStage',windowStage);
  79. GlobalContext.getContext().setObject('windowClass',windowClass)
  80. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
  81. windowStage.loadContent('pages/SplashIndex', (err, data) => {
  82. if (err.code) {
  83. hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
  84. return;
  85. }
  86. //一多断点开发
  87. windowStage.getMainWindow().then((data: window.Window) => {
  88. this.uiContext = data.getUIContext();
  89. let widthBp: WidthBreakpoint = this.uiContext.getWindowWidthBreakpoint();
  90. let heightBp: HeightBreakpoint = this.uiContext.getWindowHeightBreakpoint();
  91. AppStorage.setOrCreate('currentWidthBreakpoint', widthBp);
  92. AppStorage.setOrCreate('currentHeightBreakpoint', heightBp);
  93. data.on('windowSizeChange', this.onWindowSizeChange);
  94. }).catch((err: BusinessError) => {
  95. console.error(`Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}`);
  96. });
  97. hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
  98. });
  99. DemoConstants.windowStage = windowStage
  100. }
  101. }