• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-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 ServiceExtension 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/screenlock/src/main/ets/com/ohos/common/constants'
21import AbilityManager from '../../../../../../common/src/main/ets/default/abilitymanager/abilityManager'
22import sTimeManager from '../../../../../../common/src/main/ets/default/TimeManager'
23
24const TAG = "ScreenLock-ServiceExtAbility"
25
26class ServiceExtAbility extends ServiceExtension {
27    onCreate(want) {
28        Log.showInfo(TAG, 'onCreate, want:' + want.abilityName);
29        AbilityManager.setContext(AbilityManager.ABILITY_NAME_SCREEN_LOCK, this.context)
30        sTimeManager.init(this.context)
31        this.statusBarWindow()
32        this.createWindow(Constants.WIN_NAME)
33    }
34
35    private createWindow(name: string) {
36        Log.showDebug(TAG, `createWindow name:${name}`)
37        windowManager.create(this.context, name, 2110).then((win) => {
38            win.loadContent("pages/index").then(() => {
39                Log.showInfo(TAG, name + " window loadContent in then! ")
40                win.show().then(() => {
41                    Log.showInfo(TAG, "then begin " + name + " window show in then! ");
42                })
43            })
44        }, (error) => {
45            Log.showError(TAG, name + " window createFailed, error.code = " + error.code)
46        })
47    }
48
49    private async statusBarWindow() {
50        let dis = await display.getDefaultDisplay();
51        Log.showDebug(TAG, `api8New onCreate, dis: ${JSON.stringify(dis)}`);
52        let rect;
53        if (dis.width > dis.height) { // Pad、PC horizontalScreen Mode
54            rect = {
55                left: 0,
56                top: 0,
57                width: '100%',
58                height: (48 * dis.width) / 1280,
59            }
60        } else { // Phone verticalScreen Mode
61            rect = {
62                left: 0,
63                top: 0,
64                width: '100%',
65                height: (48 * dis.width) / 720
66            }
67        }
68        AbilityManager.setAbilityData(AbilityManager.ABILITY_NAME_STATUS_BAR, "rect", rect);
69        AbilityManager.setAbilityData(AbilityManager.ABILITY_NAME_STATUS_BAR, "dis", {
70            width: dis.width,
71            height: dis.height,
72        });
73        Log.showInfo(TAG, `createWindow success.`);
74    }
75
76    onDestroy() {
77        Log.showInfo(TAG, 'api8New onDestroy');
78        sTimeManager.release()
79
80    }
81}
82
83export default ServiceExtAbility