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 UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 21import { Log } from './Log'; 22 23export class GlobalContext { 24 private static TAG: string = '[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; 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; 42 private cameraFormParam; 43 private isPicker: boolean = false; 44 private cameraUIExtensionContentSession: UIExtensionContentSession; 45 private pickerUri: string; 46 47 public static get(): GlobalContext { 48 if (!Boolean(GlobalContext.instance).valueOf()) { 49 GlobalContext.instance = new GlobalContext(); 50 } 51 return GlobalContext.instance; 52 } 53 54 getObject(value: string): Object { 55 return this._objects.get(value); 56 } 57 58 getT<T>(value: string): T { 59 return this._objects.get(value) as T; 60 } 61 62 setObject(key: string, objectClass: Object): void { 63 this._objects.set(key, objectClass); 64 } 65 66 apply(value: string): void { 67 const func = this._objects.get(value); 68 if (func) { 69 (func as Function)(); 70 } 71 } 72 73 // 显示实例,在 phone设备上用 display获得的长宽与 State变量中 windowSize一致。 74 public getDisplayInfo(): display.Display { 75 if (!this.mDisplay) { 76 this.mDisplay = display.getDefaultDisplaySync(); 77 } 78 return this.mDisplay; 79 } 80 81 public async getCutoutInfo(): Promise<display.CutoutInfo> { 82 if (!this.mCutoutInfo) { 83 this.mCutoutInfo = await this.getDisplayInfo().getCutoutInfo(); 84 } 85 return this.mCutoutInfo; 86 } 87 88 public getCameraAbilityContext() { 89 return this.cameraAbilityContext; 90 } 91 92 public setCameraAbilityContext(context): void { 93 this.cameraAbilityContext = context; 94 } 95 96 public getCameraAbilityWant(): Want { 97 return this.cameraAbilityWant; 98 } 99 100 public setCameraAbilityWant(want: Want): void { 101 this.cameraAbilityWant = want; 102 } 103 104 public getCameraNewWant(): Want { 105 return this.cameraNewWant; 106 } 107 108 public setCameraNewWant(want: Want): void { 109 this.cameraNewWant = want; 110 } 111 112 public getWindowStage(): window.WindowStage { 113 return this.windowStage; 114 } 115 116 public setWindowStage(stage: window.WindowStage): void { 117 this.windowStage = stage; 118 } 119 120 public getCameraAbilityStageContext(): common.AbilityStageContext { 121 return this.cameraAbilityStageContext; 122 } 123 124 public setCameraAbilityStageContext(context: common.AbilityStageContext): void { 125 this.cameraAbilityStageContext = context; 126 } 127 128 public getCameraWinClass(): window.Window { 129 return this.cameraWinClass; 130 } 131 132 public setCameraWinClass(win: window.Window): void { 133 this.cameraWinClass = win; 134 } 135 136 public getCameraSettingContext(): common.UIAbilityContext { 137 return this.cameraSettingContext; 138 } 139 140 public setCameraSettingContext(context: common.UIAbilityContext): void { 141 this.cameraSettingContext = context; 142 } 143 144 public getCameraWindowStageEvent(): window.WindowStageEventType { 145 return this.cameraWindowStageEvent; 146 } 147 148 public setCameraWindowStageEvent(event: window.WindowStageEventType): void { 149 this.cameraWindowStageEvent = event; 150 } 151 152 public getXComponentController() { 153 return this.xComponentController; 154 } 155 156 public setXComponentController(controller): void { 157 this.xComponentController = controller; 158 } 159 160 public getCameraFormParam() { 161 return this.cameraFormParam; 162 } 163 164 public setCameraFormParam(cameraParam): void { 165 this.cameraFormParam = cameraParam; 166 } 167 168 public setIsPicker(isPicker: boolean): void { 169 this.isPicker = isPicker; 170 } 171 172 public getIsPicker(isPicker: boolean): void { 173 this.isPicker = isPicker; 174 } 175 176 public setSession(session: UIExtensionContentSession): void { 177 Log.info('setSession' + session) 178 this.cameraUIExtensionContentSession = session; 179 } 180 181 public getSession(): UIExtensionContentSession { 182 return this.cameraUIExtensionContentSession; 183 } 184 185 public setPickerUri(pickerUri: string): void { 186 if (pickerUri) { 187 this.pickerUri = pickerUri; 188 } else { 189 this.pickerUri = ''; 190 } 191 } 192 193 public getPickerUri(): string { 194 return this.pickerUri; 195 } 196}