1/*
2 * Copyright (c) 2022 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 ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
17import windowManager from '@ohos.window';
18import display from '@ohos.display';
19import Log from '../../../../../../common/src/main/ets/default/Log';
20import Constants from '../../../../../../features/screenshot/src/main/ets/com/ohos/common/constants';
21import ScreenShotModel from '../../../../../../features/screenshot/src/main/ets/com/ohos/model/screenShotModel';
22import Want from '@ohos.app.ability.Want';
23import { BusinessError } from '@ohos.base';
24
25const TAG = 'ScreenShot-ScreenShotServiceAbility';
26const INDEX_PAGE = 'pages/index';
27const ZOOM_RATIO = 0.4;
28const WINDOW_Y = 300;
29
30class ServiceExtAbility extends ServiceExtensionAbility {
31  onCreate(want: Want): void {
32    Log.showInfo(TAG, 'api8New onCreate, want:' + want.abilityName);
33    globalThis.shotScreenContext = this.context;
34    const windowConfig: windowManager.Configuration = {
35      name: Constants.WIN_NAME,
36      windowType: windowManager.WindowType.TYPE_SCREENSHOT,
37      ctx: this.context,
38    };
39    windowManager.createWindow(windowConfig).then((win) => {
40      Log.showInfo(TAG, 'create window finish');
41      win.moveWindowTo(0, WINDOW_Y).then(() => {
42        Log.showInfo(TAG, 'window move finish');
43        const dis = display.getDefaultDisplaySync();
44        Log.showInfo(TAG, 'dis.width = ' + dis.width + ' dis.height = ' + dis.height);
45        win.resize(dis.width * ZOOM_RATIO, dis.height * ZOOM_RATIO).then(() => {
46          Log.showInfo(TAG, 'window reset size finish');
47          win.setUIContent(INDEX_PAGE).then(() => {
48            ScreenShotModel.shotScreen();
49            Log.showInfo(TAG, 'then begin window loadContent in then! ');
50          });
51        });
52      });
53    }, (error: BusinessError) => {
54      Log.showInfo(TAG, 'window createFailed, error.code = ' + error.code);
55    });
56    Log.showInfo(TAG, 'after window create');
57  }
58
59  onDestroy(): void {
60    Log.showInfo(TAG, 'onDestroy');
61  }
62}
63
64export default ServiceExtAbility;