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