1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import display from '@ohos.display'; 17import type common from '@ohos.app.ability.common'; 18import type Want from '@ohos.app.ability.Want'; 19import type window from '@ohos.window'; 20import type { PromptAction } from '@ohos.arkui.UIContext'; 21 22const TAG: string = 'GlobalContext'; 23 24export class GlobalContext { 25 26 private constructor() { 27 } 28 29 private static instance: GlobalContext; 30 private _objects = new Map<string, Object>(); 31 private mDisplay: display.Display | undefined = undefined; 32 private mCutoutInfo: display.CutoutInfo | undefined = undefined; 33 private cameraAbilityContext: common.UIAbilityContext; 34 private cameraAbilityStageContext: common.AbilityStageContext; 35 private cameraAbilityWant: Want; 36 private cameraNewWant: Want; 37 private windowStage: window.WindowStage; 38 private cameraWinClass: window.Window; 39 private cameraSettingContext: common.UIAbilityContext; 40 private cameraWindowStageEvent: window.WindowStageEventType; 41 private xComponentController: XComponentController; 42 private promptAction: PromptAction; 43 44 public static get(): GlobalContext { 45 if (!Boolean(GlobalContext.instance).valueOf()) { 46 GlobalContext.instance = new GlobalContext(); 47 } 48 return GlobalContext.instance; 49 } 50 51 getObject(value: string): Object { 52 return this._objects.get(value); 53 } 54 55 getT<T>(value: string): T { 56 return this._objects.get(value) as T; 57 } 58 59 setObject(key: string, objectClass: Object): void { 60 this._objects.set(key, objectClass); 61 } 62 63 apply(value: string): void { 64 const func = this._objects.get(value); 65 if (func) { 66 (func as Function)(); 67 } 68 } 69 70 // 显示实例,在 phone设备上用 display获得的长宽与 State变量中 windowSize一致。 71 public getDisplayInfo(): display.Display { 72 if (!this.mDisplay) { 73 this.mDisplay = display.getDefaultDisplaySync(); 74 } 75 return this.mDisplay; 76 } 77 78 public async getCutoutInfo(): Promise<display.CutoutInfo> { 79 if (!this.mCutoutInfo) { 80 this.mCutoutInfo = await this.getDisplayInfo().getCutoutInfo(); 81 } 82 return this.mCutoutInfo; 83 } 84 85 public getCameraAbilityContext(): common.UIAbilityContext { 86 return this.cameraAbilityContext; 87 } 88 89 public setCameraAbilityContext(context: common.UIAbilityContext): void { 90 this.cameraAbilityContext = context; 91 } 92 93 public getCameraAbilityWant(): Want { 94 return this.cameraAbilityWant; 95 } 96 97 public setCameraAbilityWant(want: Want): void { 98 this.cameraAbilityWant = want; 99 } 100 101 public getCameraNewWant(): Want { 102 return this.cameraNewWant; 103 } 104 105 public setCameraNewWant(want: Want): void { 106 this.cameraNewWant = want; 107 } 108 109 public getWindowStage(): window.WindowStage { 110 return this.windowStage; 111 } 112 113 public setWindowStage(stage: window.WindowStage): void { 114 this.windowStage = stage; 115 } 116 117 public getCameraAbilityStageContext(): common.AbilityStageContext { 118 return this.cameraAbilityStageContext; 119 } 120 121 public setCameraAbilityStageContext(context: common.AbilityStageContext): void { 122 this.cameraAbilityStageContext = context; 123 } 124 125 public getCameraWinClass(): window.Window { 126 return this.cameraWinClass; 127 } 128 129 public setCameraWinClass(win: window.Window): void { 130 this.cameraWinClass = win; 131 } 132 133 public getCameraSettingContext(): common.UIAbilityContext { 134 return this.cameraSettingContext; 135 } 136 137 public setCameraSettingContext(context: common.UIAbilityContext): void { 138 this.cameraSettingContext = context; 139 } 140 141 public setPromptAction(promptAction: PromptAction): void { 142 this.promptAction = promptAction; 143 } 144 145 public getPromptAction(): PromptAction { 146 return this.promptAction; 147 } 148 149 public getCameraWindowStageEvent(): window.WindowStageEventType { 150 return this.cameraWindowStageEvent; 151 } 152 153 public setCameraWindowStageEvent(event: window.WindowStageEventType): void { 154 this.cameraWindowStageEvent = event; 155 } 156 157 public getXComponentController(): XComponentController { 158 return this.xComponentController; 159 } 160 161 public setXComponentController(controller): void { 162 this.xComponentController = controller; 163 } 164}