• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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';
20
21export class GlobalContext {
22  private static TAG: string = '[GlobalContext]:';
23
24  private constructor() {
25  }
26
27  private static instance: GlobalContext;
28  private _objects = new Map<string, Object>();
29  private mDisplay: display.Display | undefined = undefined;
30  private mCutoutInfo: display.CutoutInfo | undefined = undefined;
31  private cameraAbilityContext;
32  private cameraAbilityStageContext: common.AbilityStageContext;
33  private cameraAbilityWant: Want;
34  private cameraNewWant: Want;
35  private windowStage: window.WindowStage;
36  private cameraWinClass: window.Window;
37  private cameraSettingContext: common.UIAbilityContext;
38  private cameraWindowStageEvent: window.WindowStageEventType;
39  private xComponentController;
40  private cameraFormParam;
41
42  public static get(): GlobalContext {
43    if (!Boolean(GlobalContext.instance).valueOf()) {
44      GlobalContext.instance = new GlobalContext();
45    }
46    return GlobalContext.instance;
47  }
48
49  getObject(value: string): Object {
50    return this._objects.get(value);
51  }
52
53  getT<T>(value: string): T {
54    return this._objects.get(value) as T;
55  }
56
57  setObject(key: string, objectClass: Object): void {
58    this._objects.set(key, objectClass);
59  }
60
61  apply(value: string): void {
62    const func = this._objects.get(value);
63    if (func) {
64      (func as Function)();
65    }
66  }
67
68  // 显示实例,在 phone设备上用 display获得的长宽与 State变量中 windowSize一致。
69  public getDisplayInfo(): display.Display {
70    if (!this.mDisplay) {
71      this.mDisplay = display.getDefaultDisplaySync();
72    }
73    return this.mDisplay;
74  }
75
76  public async getCutoutInfo(): Promise<display.CutoutInfo> {
77    if (!this.mCutoutInfo) {
78      this.mCutoutInfo = await this.getDisplayInfo().getCutoutInfo();
79    }
80    return this.mCutoutInfo;
81  }
82
83  public getCameraAbilityContext() {
84    return this.cameraAbilityContext;
85  }
86
87  public setCameraAbilityContext(context): void {
88    this.cameraAbilityContext = context;
89  }
90
91  public getCameraAbilityWant(): Want {
92    return this.cameraAbilityWant;
93  }
94
95  public setCameraAbilityWant(want: Want): void {
96    this.cameraAbilityWant = want;
97  }
98
99  public getCameraNewWant(): Want {
100    return this.cameraNewWant;
101  }
102
103  public setCameraNewWant(want: Want): void {
104    this.cameraNewWant = want;
105  }
106
107  public getWindowStage(): window.WindowStage {
108    return this.windowStage;
109  }
110
111  public setWindowStage(stage: window.WindowStage): void {
112    this.windowStage = stage;
113  }
114
115  public getCameraAbilityStageContext(): common.AbilityStageContext {
116    return this.cameraAbilityStageContext;
117  }
118
119  public setCameraAbilityStageContext(context: common.AbilityStageContext): void {
120    this.cameraAbilityStageContext = context;
121  }
122
123  public getCameraWinClass(): window.Window {
124    return this.cameraWinClass;
125  }
126
127  public setCameraWinClass(win: window.Window): void {
128    this.cameraWinClass = win;
129  }
130
131  public getCameraSettingContext(): common.UIAbilityContext {
132    return this.cameraSettingContext;
133  }
134
135  public setCameraSettingContext(context: common.UIAbilityContext): void {
136    this.cameraSettingContext = context;
137  }
138
139  public getCameraWindowStageEvent(): window.WindowStageEventType {
140    return this.cameraWindowStageEvent;
141  }
142
143  public setCameraWindowStageEvent(event: window.WindowStageEventType): void {
144    this.cameraWindowStageEvent = event;
145  }
146
147  public getXComponentController() {
148    return this.xComponentController;
149  }
150
151  public setXComponentController(controller): void {
152    this.xComponentController = controller;
153  }
154
155  public getCameraFormParam() {
156    return this.cameraFormParam;
157  }
158
159  public setCameraFormParam(cameraParam): void {
160    this.cameraFormParam = cameraParam;
161  }
162}