• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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';
17
18const TAG: string = 'common_WindowUtil';
19
20export class WindowUtil {
21  static setWindowKeepScreenOn(context, isKeepScreenOn: boolean) {
22    try {
23      window.getLastWindow(context).then((windows) => {
24        windows.setWindowKeepScreenOn(isKeepScreenOn).then(() => {
25          Log.info(TAG, 'Photos succeeded in setting the screen to be always on.');
26        }).catch((err) => {
27          Log.error(TAG, 'Photos failed to set the screen to be always on. Cause:  ' + JSON.stringify(err));
28        });
29      }).catch((err) => {
30        Log.error(TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(err));
31      });
32    } catch (exception) {
33      Log.error(TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(exception));
34    }
35  }
36
37  static setPreferredOrientation(context, orientation: number) {
38    try {
39      window.getLastWindow(context, (err, data) => {
40        if (err.code || !data) {
41          Log.error(TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(err));
42          return;
43        }
44        let windowClass = data;
45        windowClass.setPreferredOrientation(orientation, (err) => {
46          if (err.code) {
47            Log.error(TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(err));
48            return;
49          }
50          Log.info(TAG, 'Succeeded in setting window orientation.');
51        });
52      });
53    } catch (exception) {
54      Log.error(TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(exception));
55    }
56  }
57}