ContextHelper.ets 3.5 KB

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