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 Ability from '@ohos.app.ability.UIAbility';
17import window from '@ohos.window';
18import { CameraBasicFunction } from '@ohos/common/src/main/ets/default/function/CameraBasicFunction';
19import { CameraNeedStatus } from '@ohos/common/src/main/ets/default/utils/Constants';
20import { FeatureManager } from '@ohos/common/src/main/ets/default/featureservice/FeatureManager';
21import { Log } from '@ohos/common/src/main/ets/default/utils/Log';
22import { PreferencesService } from '@ohos/common/src/main/ets/default/featurecommon/preferences/PreferencesService';
23import { ModeMap } from '../common/ModeMap';
24import wantConstant from '@ohos.ability.wantConstant';
25import { GlobalContext } from '@ohos/common/src/main/ets/default/utils/GlobalContext';
26
27export default class MainAbility extends Ability {
28  private cameraBasicFunction: any = null;
29
30  onCreate(want, launchParam) {
31    // Ability is creating, initialize resources for this ability
32    Log.start(Log.ABILITY_WHOLE_LIFE);
33    Log.info('Camera MainAbility onCreate.e');
34    GlobalContext.get().setCameraAbilityContext(this.context);
35    GlobalContext.get().setCameraAbilityWant(this.launchWant);
36    GlobalContext.get().setObject('permissionFlag', false);
37    GlobalContext.get().setObject('cameraStartTime', new Date().getTime());
38    GlobalContext.get().setObject('cameraStartFlag', true);
39    GlobalContext.get().setObject('stopRecordingFlag', false);
40    GlobalContext.get().setObject('doOnForeground', false);
41    GlobalContext.get().setObject('doOnForeground', false);
42    this.cameraBasicFunction = CameraBasicFunction.getInstance();
43    this.cameraBasicFunction.initCamera({
44      cameraId: 'BACK', mode: 'PHOTO'
45    }, 'onCreate');
46    Log.info('Camera MainAbility onCreate.x');
47    if (GlobalContext.get().getCameraFormParam() != undefined) {
48      new FeatureManager(GlobalContext.get().getCameraFormParam().mode, new ModeMap());
49    } else {
50      new FeatureManager('PHOTO', new ModeMap());
51    }
52  }
53
54  onDestroy() {
55    // Ability is creating, release resources for this ability
56    Log.end(Log.ABILITY_WHOLE_LIFE);
57    Log.end(Log.APPLICATION_WHOLE_LIFE);
58    this.cameraBasicFunction.startIdentification = false;
59    PreferencesService.getInstance().flush();
60    Log.info('Camera MainAbility onDestroy.');
61  }
62
63  async onWindowStageCreate(windowStage) {
64    // Main window is created, set main page for this ability
65    Log.start(Log.ABILITY_VISIBLE_LIFE);
66    Log.info('Camera MainAbility onWindowStageCreate.');
67    windowStage.on('windowStageEvent', (event) => {
68      Log.info('Camera MainAbility onWindowStageEvent: ' + JSON.stringify(event));
69      GlobalContext.get().setCameraWindowStageEvent(event);
70      if (event === window.WindowStageEventType.INACTIVE || event === window.WindowStageEventType.HIDDEN) {
71        GlobalContext.get().setObject('stopRecordingFlag', true);
72        GlobalContext.get().apply('stopCameraRecording');
73      } else {
74        GlobalContext.get().setObject('stopRecordingFlag', false);
75      }
76    })
77
78    windowStage.getMainWindow().then((win) => {
79      try {
80        win.setLayoutFullScreen(true).then(() => {
81          Log.info('Camera setFullScreen finished.');
82          win.setSystemBarEnable(['navigation']).then(() => {
83            win.setSystemBarProperties({
84              navigationBarColor: '#00000000', navigationBarContentColor: '#B3B3B3'
85            }).then(() => {
86              Log.info('Camera setSystemBarProperties.');
87            })
88            Log.info('Camera setSystemBarEnable finished.');
89          })
90        })
91        GlobalContext.get().setCameraWinClass(win);
92      } catch (err) {
93        Log.error('Camera setFullScreen err: ' + err);
94      }
95    })
96
97    if (this.launchWant?.action === wantConstant.Action.ACTION_IMAGE_CAPTURE ||
98    this.launchWant?.parameters?.action === wantConstant.Action.ACTION_IMAGE_CAPTURE) {
99      GlobalContext.get().setCameraFormParam({
100        action: 'capture',
101        cameraPosition: 'PHOTO',
102        mode: 'PHOTO'
103      });
104    } else if (this.launchWant?.action === wantConstant.Action.ACTION_VIDEO_CAPTURE ||
105    this.launchWant?.parameters?.action === wantConstant.Action.ACTION_VIDEO_CAPTURE) {
106      GlobalContext.get().setCameraFormParam({
107        action: 'video',
108        cameraPosition: 'VIDEO',
109        mode: 'VIDEO'
110      });
111    }
112    windowStage.setUIContent(this.context, 'pages/index', null);
113  }
114
115  onWindowStageDestroy() {
116    Log.end(Log.ABILITY_VISIBLE_LIFE);
117    Log.info('Camera MainAbility onWindowStageDestroy.');
118  }
119
120  onForeground() {
121    Log.start(Log.ABILITY_FOREGROUND_LIFE);
122    GlobalContext.get().setObject('cameraNeedStatus', CameraNeedStatus.CAMERA_NEED_INIT)
123    if (GlobalContext.get().getT<boolean>('doOnForeground')) {
124      Log.info('Camera MainAbility onForeground.');
125      GlobalContext.get().apply('updateCameraStatus')
126    } else {
127      GlobalContext.get().setObject('doOnForeground', true);
128    }
129    Log.info('Camera MainAbility onForeground');
130  }
131
132  onBackground() {
133    Log.end(Log.ABILITY_FOREGROUND_LIFE);
134    Log.info('Camera MainAbility onBackground.');
135    this.cameraBasicFunction.startIdentification = false;
136    GlobalContext.get().setObject('cameraNeedStatus', CameraNeedStatus.CAMERA_NEED_RELEASE)
137    GlobalContext.get().apply('updateCameraStatus');
138  }
139
140  onNewWant(want) {
141    Log.info('Camera MainAbility E onNewWant.');
142    GlobalContext.get().setCameraAbilityWant(want);
143    Log.info(`Camera MainAbility X newWantAction: ${JSON.stringify(GlobalContext.get().getCameraAbilityWant())}`);
144  }
145}