• 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, AbilityManager, sTimeManager} from '@ohos/common'
20import Constants from '../../../../../../features/screenlock/src/main/ets/com/ohos/common/constants'
21import inputMethod from '@ohos.inputMethod'
22
23const TAG = "ScreenLock-ServiceExtAbility"
24const AUTO_ROTATION_RETRICTED: number = 8
25
26class ServiceExtAbility extends ServiceExtension {
27    private direction :number;
28
29
30    onCreate(want) {
31        Log.showInfo(TAG, 'onCreate, want:' + want.abilityName);
32        AbilityManager.setContext(AbilityManager.ABILITY_NAME_SCREEN_LOCK, this.context)
33        sTimeManager.init(this.context)
34
35        display.on("change", (id) => {
36            Log.showInfo(TAG, "screenlock display change, data: " + JSON.stringify(id))
37            display.getAllDisplay().then(async (arrayDisplay) => {
38                Log.showInfo(TAG, "getAllDisplay : " + JSON.stringify(arrayDisplay))
39                for (let display of arrayDisplay) {
40                    Log.showInfo(TAG, "getAllDisplay start : " + JSON.stringify(display));
41                    if (id == display.id) {
42                        if (display.width > display.height) {
43                            this.direction = 1;
44                        } else {
45                            this.direction = 2;
46                        }
47                        Log.showInfo(TAG, "direction change : " + this.direction)
48                        AppStorage.SetOrCreate('screenlockdirection', this.direction)
49                        this.resetWindow(display.width,display.height)
50                        let inputMethodController = inputMethod.getController();
51                        Log.showInfo(TAG, "inputMethodController: "+inputMethodController)
52                        inputMethodController.hideSoftKeyboard().then(() => {
53                            Log.showInfo(TAG, "Succeeded in hiding softKeyboard")
54                        }).catch((err) => {
55                            Log.showError(TAG, "failed to hideSoftKeyboard: " + JSON.stringify(err));
56                        });
57                    }
58                }
59            })
60        })
61        this.statusBarWindow()
62        this.createWindow(Constants.WIN_NAME)
63    }
64    private async resetWindow(width: number,height:number) {
65        Log.showInfo(TAG, "resetWindow width: " + width +",height:"+height)
66        let window = await windowManager.find(Constants.WIN_NAME);
67        Log.showInfo(TAG, "screenlock window : " + JSON.stringify(window));
68        await  window.resetSize(width,height)
69    }
70
71    private createWindow(name: string) {
72        Log.showDebug(TAG, `createWindow name:${name}`)
73        windowManager.create(this.context, name, windowManager.WindowType.TYPE_KEYGUARD).then((win) => {
74            Log.showInfo(TAG, "before begin " + name + " window show!")
75            win.setPreferredOrientation(AUTO_ROTATION_RETRICTED, (err) => {
76                if (err.code) {
77                    Log.showError(TAG, "failed to set window Orientation: " + JSON.stringify(err));
78                }
79                Log.showInfo(TAG, "succeed to set window Orientation");
80            })
81            win.loadContent("pages/index").then(() => {
82                win.show().then(() => {
83                    Log.showInfo(TAG, "window show in then!");
84                })
85            })
86        }, (error) => {
87            Log.showError(TAG, name + " window createFailed, error.code = " + error.code)
88        })
89    }
90
91    private async statusBarWindow() {
92        Log.showDebug(TAG, `statusBarWindow`);
93        let dis = await display.getDefaultDisplay();
94        while (dis === null) {
95            await new Promise((resolve)=>{setTimeout(resolve, 1000)});
96            dis = await display.getDefaultDisplay();
97        }
98        Log.showInfo(TAG, `getDefaultDisplay, dis: ${JSON.stringify(dis)}`);
99        let rect;
100        if (dis.width > dis.height) { // Pad and PC horizontalScreen Mode
101            rect = {
102                left: 0,
103                top: 0,
104                width: '100%',
105                height: (44 * dis.width) / 1280,
106            }
107            this.direction =1
108        } else { // Phone verticalScreen Mode
109            rect = {
110                left: 0,
111                top: 0,
112                width: '100%',
113                height: (44 * dis.width) / 800
114            }
115            this.direction =2
116        };
117        AppStorage.SetOrCreate('screenlockdirection', this.direction);
118        AbilityManager.setAbilityData(AbilityManager.ABILITY_NAME_STATUS_BAR, "rect", rect);
119        AbilityManager.setAbilityData(AbilityManager.ABILITY_NAME_STATUS_BAR, "dis", {
120            width: dis.width,
121            height: dis.height,
122        });
123    }
124
125    onDestroy() {
126        Log.showInfo(TAG, 'api8New onDestroy');
127        sTimeManager.release()
128    }
129
130    onMemoryLevel(level): void {
131        Log.showInfo(TAG, 'onMemoryLevel, level:' + JSON.stringify(level))
132    }
133}
134
135export default ServiceExtAbility