• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-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 window from '@ohos.window'
17import prompt from '@ohos.prompt'
18import { getResourceString } from './Tools'
19import { MILLISECOND, FILENAME_REGEXP } from '../constants/Constant'
20import Logger from '../log/Logger'
21import MediaLibrary from '@ohos.multimedia.mediaLibrary'
22
23const TAG = 'commonUtil'
24
25let mediaLibrary: MediaLibrary.MediaLibrary = null
26
27export const toast = (text: string | Resource, time= MILLISECOND.ONE_SECOND) => {
28  let resourceString = undefined
29  if (typeof text !== 'string') {
30    resourceString = getResourceString(text)
31  } else {
32    resourceString = text
33  }
34  prompt.showToast({ message: resourceString, duration: time })
35}
36
37/**
38 * 设置导航栏、状态栏背景、文字颜色
39 *
40 * @param navigationBarColor 导航栏背景颜色
41 * @param statusBarColor 状态栏背景颜色
42 * @param navigationBarContentColor 导航栏文字颜色
43 * @param statusBarContentColor 状态栏文字颜色
44 */
45export const setSystemBar = async (navigationBarColor?: string, statusBarColor?: string, navigationBarContentColor?: string, statusBarContentColor?: string) => {
46  let w = await window.getTopWindow(getContext(this))
47  await w.setSystemBarProperties({
48    navigationBarColor,
49    statusBarColor,
50    navigationBarContentColor,
51    statusBarContentColor
52  })
53}
54
55/**
56 * 设置沉浸式
57 *
58 * @param isImmersion 是否沉浸式
59 */
60export const setImmersion = (isImmersion: boolean) => {
61  let TAG = 'setImmersion';
62  if (!globalThis.windowClass) {
63    return;
64  }
65  globalThis.windowClass.setFullScreen(isImmersion, (err, data) => {
66    if (err.code) {
67      Logger.e(TAG, 'Failed to enable the full-screen mode. Cause:' + JSON.stringify(err))
68      return
69    }
70    Logger.i(TAG, 'Succeeded in enabling the full-screen mode. Data: ' + JSON.stringify(data))
71  })
72}
73
74/**
75 * 校验文件名合法性
76 *
77 * @param fileName 文件名
78 * @return 是否合法
79 */
80export function isValidFileName(fileName): boolean {
81  return FILENAME_REGEXP.test(fileName)
82}
83
84