100aff185Sopenharmony_ci/*
200aff185Sopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
300aff185Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
400aff185Sopenharmony_ci * you may not use this file except in compliance with the License.
500aff185Sopenharmony_ci * You may obtain a copy of the License at
600aff185Sopenharmony_ci *
700aff185Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
800aff185Sopenharmony_ci *
900aff185Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1000aff185Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1100aff185Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1200aff185Sopenharmony_ci * See the License for the specific language governing permissions and
1300aff185Sopenharmony_ci * limitations under the License.
1400aff185Sopenharmony_ci */
1500aff185Sopenharmony_ciimport { Log } from './Log';
1600aff185Sopenharmony_ciimport window from '@ohos.window';
1700aff185Sopenharmony_ciimport { Router, UIContext } from '@ohos.arkui.UIContext';
1800aff185Sopenharmony_ciimport common from '@ohos.app.ability.common';
1900aff185Sopenharmony_ci
2000aff185Sopenharmony_ciconst TAG: string = 'common_WindowUtil';
2100aff185Sopenharmony_ci
2200aff185Sopenharmony_ciexport class WindowUtil {
2300aff185Sopenharmony_ci  static setWindowKeepScreenOn(context: common.UIAbilityContext, isKeepScreenOn: boolean): void {
2400aff185Sopenharmony_ci    try {
2500aff185Sopenharmony_ci      window.getLastWindow(context).then((windows) => {
2600aff185Sopenharmony_ci        windows.setWindowKeepScreenOn(isKeepScreenOn).then(() => {
2700aff185Sopenharmony_ci          Log.info(TAG, 'Photos succeeded in setting the screen to be always on.');
2800aff185Sopenharmony_ci        }).catch((err) => {
2900aff185Sopenharmony_ci          Log.error(TAG, 'Photos failed to set the screen to be always on. Cause:  ' + JSON.stringify(err));
3000aff185Sopenharmony_ci        });
3100aff185Sopenharmony_ci      }).catch((err) => {
3200aff185Sopenharmony_ci        Log.error(TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(err));
3300aff185Sopenharmony_ci      });
3400aff185Sopenharmony_ci    } catch (exception) {
3500aff185Sopenharmony_ci      Log.error(TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(exception));
3600aff185Sopenharmony_ci    }
3700aff185Sopenharmony_ci  }
3800aff185Sopenharmony_ci
3900aff185Sopenharmony_ci  static setPreferredOrientation(context: common.UIAbilityContext, orientation: number): void {
4000aff185Sopenharmony_ci    try {
4100aff185Sopenharmony_ci      window.getLastWindow(context, (err, data) => {
4200aff185Sopenharmony_ci        if (err.code || !data) {
4300aff185Sopenharmony_ci          Log.error(TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(err));
4400aff185Sopenharmony_ci          return;
4500aff185Sopenharmony_ci        }
4600aff185Sopenharmony_ci        let windowClass = data;
4700aff185Sopenharmony_ci        windowClass.setPreferredOrientation(orientation, (err) => {
4800aff185Sopenharmony_ci          if (err.code) {
4900aff185Sopenharmony_ci            Log.error(TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(err));
5000aff185Sopenharmony_ci            return;
5100aff185Sopenharmony_ci          }
5200aff185Sopenharmony_ci          Log.info(TAG, 'Succeeded in setting window orientation.');
5300aff185Sopenharmony_ci        });
5400aff185Sopenharmony_ci      });
5500aff185Sopenharmony_ci    } catch (exception) {
5600aff185Sopenharmony_ci      Log.error(TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(exception));
5700aff185Sopenharmony_ci    }
5800aff185Sopenharmony_ci  }
5900aff185Sopenharmony_ci
6000aff185Sopenharmony_ci  static prepareWinRouter(): void {
6100aff185Sopenharmony_ci    Log.debug(TAG, `prepareWinRouter AppStorage.get<Router>('router')=${AppStorage.get<Router>('router')}`);
6200aff185Sopenharmony_ci    if (AppStorage.get('router')) {
6300aff185Sopenharmony_ci      return;
6400aff185Sopenharmony_ci    }
6500aff185Sopenharmony_ci    try {
6600aff185Sopenharmony_ci      AppStorage.setOrCreate('uiContext', (AppStorage.get<window.Window>('mainWindow') as window.Window).getUIContext());
6700aff185Sopenharmony_ci    } catch (error) {
6800aff185Sopenharmony_ci      Log.error(TAG, `Failed to get UIContext, error: ${error}`);
6900aff185Sopenharmony_ci      return;
7000aff185Sopenharmony_ci    }
7100aff185Sopenharmony_ci    if (AppStorage.get('uiContext')) {
7200aff185Sopenharmony_ci      Log.info(TAG, `prepareWinRouter AppStorage.get<window.Window>('uiContext')=${AppStorage.get<window.Window>('uiContext')}`);
7300aff185Sopenharmony_ci      AppStorage.setOrCreate('router', (AppStorage.get<UIContext>('uiContext')).getRouter());
7400aff185Sopenharmony_ci      Log.info(TAG, `prepareWinRouter localRouter=${AppStorage.get<Router>('router')}`);
7500aff185Sopenharmony_ci    }
7600aff185Sopenharmony_ci  }
7700aff185Sopenharmony_ci}