1/* 2 * Copyright (c) 2022-2023 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 */ 15import { Log } from './Log'; 16import window from '@ohos.window'; 17import { Router, UIContext } from '@ohos.arkui.UIContext'; 18import common from '@ohos.app.ability.common'; 19 20const TAG: string = 'common_WindowUtil'; 21 22export class WindowUtil { 23 static setWindowKeepScreenOn(context: common.UIAbilityContext, isKeepScreenOn: boolean): void { 24 try { 25 window.getLastWindow(context).then((windows) => { 26 windows.setWindowKeepScreenOn(isKeepScreenOn).then(() => { 27 Log.info(TAG, 'Photos succeeded in setting the screen to be always on.'); 28 }).catch((err) => { 29 Log.error(TAG, 'Photos failed to set the screen to be always on. Cause: ' + JSON.stringify(err)); 30 }); 31 }).catch((err) => { 32 Log.error(TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(err)); 33 }); 34 } catch (exception) { 35 Log.error(TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(exception)); 36 } 37 } 38 39 static setPreferredOrientation(context: common.UIAbilityContext, orientation: number): void { 40 try { 41 window.getLastWindow(context, (err, data) => { 42 if (err.code || !data) { 43 Log.error(TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(err)); 44 return; 45 } 46 let windowClass = data; 47 windowClass.setPreferredOrientation(orientation, (err) => { 48 if (err.code) { 49 Log.error(TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(err)); 50 return; 51 } 52 Log.info(TAG, 'Succeeded in setting window orientation.'); 53 }); 54 }); 55 } catch (exception) { 56 Log.error(TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(exception)); 57 } 58 } 59 60 static prepareWinRouter(): void { 61 Log.debug(TAG, `prepareWinRouter AppStorage.get<Router>('router')=${AppStorage.get<Router>('router')}`); 62 if (AppStorage.get('router')) { 63 return; 64 } 65 try { 66 AppStorage.setOrCreate('uiContext', (AppStorage.get<window.Window>('mainWindow') as window.Window).getUIContext()); 67 } catch (error) { 68 Log.error(TAG, `Failed to get UIContext, error: ${error}`); 69 return; 70 } 71 if (AppStorage.get('uiContext')) { 72 Log.info(TAG, `prepareWinRouter AppStorage.get<window.Window>('uiContext')=${AppStorage.get<window.Window>('uiContext')}`); 73 AppStorage.setOrCreate('router', (AppStorage.get<UIContext>('uiContext')).getRouter()); 74 Log.info(TAG, `prepareWinRouter localRouter=${AppStorage.get<Router>('router')}`); 75 } 76 } 77}