• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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';
22
23const TAG = 'ScreenShot-ScreenShotServiceAbility';
24const INDEX_PAGE = 'pages/index';
25const ZOOM_RATIO = 0.4;
26const WINDOW_Y = 300;
27
28class ServiceExtAbility extends ServiceExtensionAbility {
29  onCreate(want): void {
30    Log.showInfo(TAG, 'api8New onCreate, want:' + want.abilityName);
31    globalThis.shotScreenContext = this.context;
32    const windowConfig = {
33      name: Constants.WIN_NAME,
34      windowType: windowManager.WindowType.TYPE_SCREENSHOT,
35      ctx: this.context,
36    };
37    windowManager.createWindow(windowConfig).then((win) => {
38      Log.showInfo(TAG, 'create window finish');
39      win.moveWindowTo(0, WINDOW_Y).then(() => {
40        Log.showInfo(TAG, 'window move finish');
41        const dis = display.getDefaultDisplaySync();
42        Log.showInfo(TAG, 'dis.width = ' + dis.width + ' dis.height = ' + dis.height);
43        win.resize(dis.width * ZOOM_RATIO, dis.height * ZOOM_RATIO).then(() => {
44          Log.showInfo(TAG, 'window reset size finish');
45          win.setUIContent(INDEX_PAGE).then(() => {
46            ScreenShotModel.shotScreen();
47            Log.showInfo(TAG, 'then begin window loadContent in then! ');
48          });
49        });
50      });
51    }, (error) => {
52      Log.showInfo(TAG, 'window createFailed, error.code = ' + error.code);
53    });
54    Log.showInfo(TAG, 'after window create');
55  }
56
57  onDestroy(): void {
58    Log.showInfo(TAG, 'onDestroy');
59  }
60}
61
62export default ServiceExtAbility;