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 prompt from '@ohos.prompt' 17import window from '@ohos.window' 18import Logger from '../util/Logger' 19 20const TAG: string = 'WindowController' 21 22let windowClass = null 23 24export default class WindowController { 25 26 // 窗口唤醒屏幕,对应FA的setWakeUpScreen() 27 setWakeUpScreen(context) { 28 window.getTopWindow(context, (err, data) => { 29 if (err.code) { 30 Logger.info(TAG, `Failed to obtain the top window. Cause: ${JSON.stringify(err)}`) 31 prompt.showToast({ 32 message: `Failed to obtain the top window. Cause: ${JSON.stringify(err)}` 33 }) 34 return 35 } 36 windowClass = data 37 const wakeUp = true 38 windowClass.setWakeUpScreen(wakeUp) 39 Logger.info(TAG, `Succeeded in setWakeUpScreen. Data: ${JSON.stringify(data)}`) 40 prompt.showToast({ 41 message: `Succeeded in setWakeUpScreen` 42 }) 43 }) 44 } 45 46 // 获取当前应用内最后显示的窗口,使用callback异步回调。对应FA的getWindow 47 getTopWindow(context) { 48 window.getTopWindow(context, (err, data) => { 49 if (err.code) { 50 Logger.info(TAG, `Failed to obtain the top window. Cause: ${JSON.stringify(err)}`) 51 prompt.showToast({ 52 message: `Failed to obtain the top window. Cause: ${JSON.stringify(err)}` 53 }) 54 return 55 } 56 Logger.info(TAG, `Succeeded in obtaining the top window. Data: ${JSON.stringify(data)}`) 57 prompt.showToast({ 58 message: `Succeeded in obtaining the top window` 59 }) 60 }) 61 } 62 63 // 设置当前能力的显示方向,对应FA模型的setDisplayOrientation() 64 setPreferredOrientation() { 65 let orientation = window.Orientation.AUTO_ROTATION 66 if (windowClass === null) { 67 return 68 } 69 let promise = windowClass.setPreferredOrientation(orientation) 70 promise.then((data) => { 71 Logger.info(TAG, `Succeeded in setting the window orientation. Data: ${JSON.stringify(data)}`) 72 prompt.showToast({ 73 message: `Succeeded in setting the window orientation` 74 }) 75 }).catch((error) => { 76 Logger.error(TAG, `Failed to set the window orientation. Cause: ${JSON.stringify(error)}`) 77 prompt.showToast({ 78 message: `Failed to set the window orientation. Cause: ${JSON.stringify(error)}` 79 }) 80 }) 81 } 82}