• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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 */
15
16import vibrator from '@ohos.vibrator'
17import Window from '@ohos.window'
18import Logger from './Logger'
19import resourceManager from '@ohos.resourceManager'
20import { BusinessError } from '@ohos.base'
21
22const TAG = '[Util]'
23
24export function getDynamicStates(dis: boolean[], id: number): boolean[] {
25  for (let i = 0; i < dis.length; i++) {
26    i === id ? dis[i] = true : dis[i] = false
27    Logger.info(TAG, dis[i].toString())
28  }
29  return dis
30}
31
32export async function playVibrateCustom(fileName: string) {
33  let rawFd: resourceManager.RawFileDescriptor | undefined = undefined
34  await getContext().resourceManager.getRawFd(fileName).then(value => {
35    rawFd = value
36    Logger.info(TAG, "rawFd of vibration:" + " fd:" + rawFd.fd + ", offset:" + rawFd.offset + ", length: " + rawFd.length)
37  })
38  try {
39    vibrator.startVibration({
40      type: "file",
41      hapticFd: { fd: rawFd!.fd, offset: rawFd!.offset, length: rawFd!.length }
42    }, {
43      usage: "unknown"
44    }, (error: BusinessError) => {
45      if (error) {
46        Logger.info(TAG, 'startVibration failed, type is file, in callback');
47      } else {
48        Logger.info(TAG, 'startVibration successful, type is file, in callback');
49      }
50    });
51  } catch (error) {
52    let err: BusinessError = error as BusinessError;
53    Logger.info(TAG, 'startVibration failed, type is file, in catch, err code:' + err.code + ' err msg:' + err.message)
54  }
55}
56
57export function changeOrientationInternal(lastWindow: Window.Window, portrait: boolean): void {
58  if (portrait) {
59    lastWindow.setPreferredOrientation(Window.Orientation.LANDSCAPE).then(() => {
60      Logger.info(TAG, "setPreferredOrientation success")
61    }).catch((error: BusinessError) => {
62      Logger.info(TAG, "setPreferredOrientation failure" + JSON.stringify(error))
63    })
64  } else {
65    lastWindow.setPreferredOrientation(Window.Orientation.PORTRAIT).then(() => {
66      Logger.info(TAG, "setPreferredOrientation success")
67    }).catch((error: BusinessError) => {
68      Logger.info(TAG, "setPreferredOrientation failure: " + JSON.stringify(error))
69    })
70  }
71}
72
73export function changeOrientation(portrait: boolean): void {
74  Window.getLastWindow(getContext()).then((lastWindow: Window.Window) => {
75    changeOrientationInternal(lastWindow, portrait)
76  }).catch((error: BusinessError) => {
77    Logger.info(TAG, "getLastWindow error: " + JSON.stringify(error))
78  })
79}
80
81export function hideTitleBar(): void {
82  Window.getLastWindow(getContext()).then((lastWindow: Window.Window) => {
83    if (lastWindow) {
84      lastWindow.setWindowSystemBarEnable([], (err: BusinessError) => {
85        if (err) {
86          Logger.info(TAG, "hideTitleBar failed")
87        } else {
88          Logger.info(TAG, "hideTitleBar successful")
89        }
90      })
91    }
92  }).catch((error: BusinessError) => {
93    Logger.info(TAG, "getLastWindow error: " + JSON.stringify(error))
94  })
95}
96
97export function showTitleBar(): void {
98  Window.getLastWindow(getContext()).then((lastWindow: Window.Window) => {
99    if (lastWindow) {
100      lastWindow.setWindowSystemBarEnable(['status', 'navigation'], (err: BusinessError) => {
101        if (err) {
102          Logger.info(TAG, "showTitleBar failed")
103        } else {
104          Logger.info(TAG, "showTitleBar successful")
105        }
106      })
107    }
108  }).catch((error: BusinessError) => {
109    Logger.info(TAG, "getLastWindow error: " + JSON.stringify(error))
110  })
111}
112