ContextHelper.ets 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { Context } from '@ohos.abilityAccessCtrl'
  2. import { common } from '@kit.AbilityKit'
  3. import { permissionController } from './PermissionControl'
  4. import {
  5. AppStorageKeyCollect,
  6. jHStartAd,
  7. pushMessageUtil,
  8. registerFontUtil,
  9. YTLog,
  10. yTPreferences,
  11. YTToast,
  12. YTUserRequest
  13. } from '../../../../Index'
  14. import { identifier } from '@kit.AdsKit'
  15. import { BusinessError } from '@kit.BasicServicesKit'
  16. export class ContextHelper {
  17. private static declare _UIContext: UIContext
  18. static get UIContext() {
  19. if (!ContextHelper._UIContext) {
  20. throw new Error('UIContext is not set')
  21. }
  22. return ContextHelper._UIContext
  23. }
  24. static set UIContext(value: UIContext) {
  25. ContextHelper._UIContext = value
  26. const context = value.getHostContext()
  27. if (context && !ContextHelper._context) {
  28. ContextHelper.context = context
  29. ContextHelper.UIAbilityContext = context as common.UIAbilityContext
  30. }
  31. }
  32. private static declare _UIAbilityContext: common.UIAbilityContext
  33. static get UIAbilityContext() {
  34. if (!ContextHelper._UIAbilityContext) {
  35. throw new Error('UIAbilityContext is not set')
  36. }
  37. return ContextHelper._UIAbilityContext
  38. }
  39. static set UIAbilityContext(value: common.UIAbilityContext) {
  40. ContextHelper._UIAbilityContext = value
  41. if (!ContextHelper._context) {
  42. ContextHelper.context = value
  43. }
  44. }
  45. private static declare _context: Context
  46. static get context() {
  47. if (!ContextHelper._context) {
  48. throw new Error('context is not set')
  49. }
  50. return ContextHelper._context
  51. }
  52. static set context(value: Context) {
  53. ContextHelper._context = value
  54. }
  55. //依赖上下文的工具类初始化 需要在第一个页面中设置UIContext后调用
  56. static init(context: UIContext) {
  57. ContextHelper.UIContext = context
  58. PersistentStorage.persistProp(AppStorageKeyCollect.TOKEN, '')
  59. YTUserRequest.refreshUserInfo()
  60. permissionController.init(ContextHelper.context)
  61. .then(() => {
  62. permissionController
  63. .askPermissions(["ohos.permission.APP_TRACKING_CONSENT"], (res, err) => {
  64. if (err) {
  65. YTLog.error(err)
  66. } else {
  67. if (res?.authResults[0] === 0) {
  68. YTLog.info('succeeded in requesting permission');
  69. identifier.getOAID((err: BusinessError, data: string) => {
  70. if (err.code) {
  71. YTLog.error(`get oaid failed, error: ${err.code} ${err.message}`);
  72. } else {
  73. const oaid: string = data;
  74. jHStartAd.init(ContextHelper.UIAbilityContext, oaid)
  75. AppStorage.setOrCreate('OAID', oaid)
  76. YTLog.info(`succeeded in getting oaid by callback , oaid: ${oaid}`);
  77. }
  78. });
  79. } else {
  80. YTLog.error('user rejected');
  81. }
  82. }
  83. })
  84. })
  85. yTPreferences.init(ContextHelper.context)
  86. pushMessageUtil.init(ContextHelper.UIAbilityContext)
  87. registerFontUtil.registerFont([
  88. {
  89. familyName: 'MiSans VF',
  90. familySrc: $rawfile('MiSansVF.ttf')
  91. },
  92. {
  93. familyName: 'Source Han Sans SC',
  94. familySrc: $rawfile('SourceHanSansSC.otf')
  95. },
  96. {
  97. familyName: 'Source Han Sans CN',
  98. familySrc: $rawfile('SourceHanSansSC.otf')
  99. }
  100. ])
  101. new YTToast({
  102. key: 'loginRemind',
  103. options: {
  104. autoCancel: false,
  105. alignment: DialogAlignment.Bottom,
  106. maskColor: Color.Transparent,
  107. isModal: false,
  108. offset: { dy: -84, dx: 0 }
  109. }
  110. })
  111. }
  112. }