import { Context } from '@ohos.abilityAccessCtrl' import { common } from '@kit.AbilityKit' import { permissionController } from './PermissionControl' import { AppStorageKeyCollect, jHStartAd, pushMessageUtil, registerFontUtil, YTLog, yTPreferences, YTToast, YTUserRequest } from '../../../../Index' import { identifier } from '@kit.AdsKit' import { BusinessError } from '@kit.BasicServicesKit' export class ContextHelper { private static declare _UIContext: UIContext static get UIContext() { if (!ContextHelper._UIContext) { throw new Error('UIContext is not set') } return ContextHelper._UIContext } static set UIContext(value: UIContext) { ContextHelper._UIContext = value const context = value.getHostContext() if (context && !ContextHelper._context) { ContextHelper.context = context ContextHelper.UIAbilityContext = context as common.UIAbilityContext } } private static declare _UIAbilityContext: common.UIAbilityContext static get UIAbilityContext() { if (!ContextHelper._UIAbilityContext) { throw new Error('UIAbilityContext is not set') } return ContextHelper._UIAbilityContext } static set UIAbilityContext(value: common.UIAbilityContext) { ContextHelper._UIAbilityContext = value if (!ContextHelper._context) { ContextHelper.context = value } } private static declare _context: Context static get context() { if (!ContextHelper._context) { throw new Error('context is not set') } return ContextHelper._context } static set context(value: Context) { ContextHelper._context = value } //依赖上下文的工具类初始化 需要在第一个页面中设置UIContext后调用 static init(context: UIContext) { ContextHelper.UIContext = context PersistentStorage.persistProp(AppStorageKeyCollect.TOKEN, '') YTUserRequest.refreshUserInfo() permissionController.init(ContextHelper.context) .then(() => { permissionController .askPermissions(["ohos.permission.APP_TRACKING_CONSENT"], (res, err) => { if (err) { YTLog.error(err) } else { if (res?.authResults[0] === 0) { YTLog.info('succeeded in requesting permission'); identifier.getOAID((err: BusinessError, data: string) => { if (err.code) { YTLog.error(`get oaid failed, error: ${err.code} ${err.message}`); } else { const oaid: string = data; jHStartAd.init(ContextHelper.UIAbilityContext, oaid) AppStorage.setOrCreate('OAID', oaid) YTLog.info(`succeeded in getting oaid by callback , oaid: ${oaid}`); } }); } else { YTLog.error('user rejected'); } } }) }) yTPreferences.init(ContextHelper.context) pushMessageUtil.init(ContextHelper.UIAbilityContext) registerFontUtil.registerFont([ { familyName: 'MiSans VF', familySrc: $rawfile('MiSansVF.ttf') }, { familyName: 'Source Han Sans SC', familySrc: $rawfile('SourceHanSansSC.otf') }, { familyName: 'Source Han Sans CN', familySrc: $rawfile('SourceHanSansSC.otf') } ]) new YTToast({ key: 'loginRemind', options: { autoCancel: false, alignment: DialogAlignment.Bottom, maskColor: Color.Transparent, isModal: false, offset: { dy: -84, dx: 0 } } }) } }