harmony-utils

harmony-utils(API12+)

🏆Introduction and Description

harmony-utils A HarmonyOS tool library with rich features and extremely easy to use, with the help of many practical tools, is committed to helping developers quickly build Hongmeng applications. Its encapsulated tools cover APP, device, screen, authorization, notification, inter-thread communication, pop-up frames, toast, biometric authentication, user preferences, taking photos, albums, scanning codes, files, logs, exception capture, characters, strings, numbers, collections, dates, random, base64, encryption, decryption, JSON and other functions, which can meet various development needs.
picker_utils It is a sub-store split by harmony-utils, including PickerUtil, PhotoHelper, and ScanUtil.

📚 Warm reminder: 📕 Harmony-utils is a lightweight framework that integrates more than 50 tool classes, but it is only 130KB in size. Achieve the ultimate balance between tool count and lightweight performance. 📙 Starting from version 1.3.2, the methods in the harmony-utils tool library will no longer be directly discarded. Please feel free to use it in third-party libraries and projects. 📒 In the update record, each version number has the corresponding minimum development tool version, such as: "DevEco Studio 5.1.0 Release". 📗 Please read the documentation carefully and view the use case before using the framework. 🙏 📘 See, please! Follow the official account of "Elder Tong" quickly, and you are waiting for you to unlock it. 📔 Creation is not easy, please give Elder Tong a thumbs up 👍github❤️ gitee❤️ 三方库❤️

🌞Download, installation and use instructions 🙏

ohpm i @pura/harmony-utils
OpenHarmony ohpm environment configuration and more, please refer to如何安装 OpenHarmony ohpm 包

全局初始化方法,从1.2.0版本开始,在UIAbility的onCreate方法中初始化 AppUtil.init()

  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    AppUtil.init(this.context);
  }

📂Module Introduction

ModuleIntroduction
AppUtilAPP-related tool categories
DeviceUtilDevice-related tools
WindowUtilWindow related tool classes
DisplayUtilScreen related tools
PermissionUtilApplication Authorization Tools
AuthUtilMobile phone biometric authentication (fingerprint, face, password) tools
NetworkUtilNetwork related tools
FileUtilFile operation related tool categories
ImageUtilPicture-related tools
PreviewUtilFile Preview Tools
LocationUtilPositioning Tool Class (WGS-84 Coordinate System)
LogUtilLog Tools
CrashUtilGlobal exception capture, crash log collection
EmitterUtilEmitter tool class (for inter-thread communication)
WantUtilWant Tools
KvUtilKey-value database tool class
PreferencesUtilPreferences (User Preferences) Tool Class
CacheUtilCache Tools
LRUCacheUtilLRUCache Cache Tool Class
NotificationUtilNotification Tools
SnapshotUtilComponent screenshot and window screenshot tool class.
KeyboardUtilKeyboard Tools
PasteboardUtilClipboard Tools
AssetUtilKey Asset Storage Service Tools
ResUtilResource Tools
ObjectUtilObject Tool Class
JSONUtilJSON Tool Class
DateUtilDate Tools
Base64UtilBase64 Tools
StrUtilString Tool Class
CharUtilCharacter Tools
NumberUtilnumber tool class
ArrayUtilCollection Tools
RandomUtilRandom Tools
RegexUtilRegular Tools
TypeUtilType Checking Tool Class
FormatUtilFormat Tools
ClickUtilThrottle, anti-shake tool class (used to click events to prevent buttons from being clicked repeatedly)
TempUtilTemperature conversion tool category
DialogUtilPop-up tool class (AlertDialog)
ToastUtilToast Tools (promptAction)
SM2, SM3, SM4,
AES, DES, RSA,
MD5, SHA, ECDSA,
CryptoUtil,
CryptoHelper
Encryption and decryption algorithm tool class
CryptoUtil: Encryption and decryption utility class, used with each encryption module.
CryptoHelper: Encrypt and decrypt data type conversion.
PickerUtilSelection and saving of photos, files (files, pictures, videos, audio), tools.拆分至 picker_utils
PhotoHelperPhoto Album related tool category.拆分至 picker_utils
ScanUtilCode tool category (scan code, code image generation, picture identification).拆分至 picker_utils

Code Example


import { font, KeyboardAvoidMode, router } from '@kit.ArkUI';
import { AppUtil, LogUtil, StrUtil, ToastUtil } from '@pura/harmony-utils';
import { DescribeBean } from '../../model/DescribeBean';
import { MockSetup } from '@ohos/hamock';
import { TitleBarView } from '../../component/TitleBarView';
import {
  AbilityLifecycleCallback,
  ApplicationStateChangeCallback,
  ConfigurationConstant,
  EnvironmentCallback
} from '@kit.AbilityKit';
import { DialogAction, DialogHelper } from '@pura/harmony-dialog';
import { JSON } from '@kit.ArkTS';
import { Utils } from '../../utils/Utils';

/**
 * APP相关工具类
 */
@Entry
@Component
struct Index {
  private scroller: Scroller = new Scroller();
  @State describe: DescribeBean = router.getParams() as DescribeBean;
  @State mode: number = KeyboardAvoidMode.OFFSET; //上抬模式
  @State bright: number = 0.0; //屏幕亮度值
  @State isKeepScreenOn: boolean = false; //屏幕是否为常亮状态
  @State privacyMode: boolean = false; //是否隐私模式
  @State statusBar: boolean = false;
  @State blGray: boolean = false; //置灰状态
  @State colorMode: ConfigurationConstant.ColorMode = ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT; //深浅色模式
  @State blZhCn: boolean = false; //默认中文

  private applicationStateChangeCallback: ApplicationStateChangeCallback = {
    onApplicationForeground() {
      LogUtil.warn('applicationStateChangeCallback onApplicationForeground');
    },
    onApplicationBackground() {
      LogUtil.warn('applicationStateChangeCallback onApplicationBackground');
    }
  };
  @State callback1: number = -1;
  private environmentCallback: EnvironmentCallback = {
    onConfigurationUpdated(config) {
      LogUtil.warn(`onConfigurationUpdated config:\n${JSON.stringify(config, null)}`);
    },
    onMemoryLevel(level) {
      LogUtil.warn(`onMemoryLevel level: ${level}`);
    }
  };
  @State callback2: number = -1;
  private abilityLifecycleCallback: AbilityLifecycleCallback = {
    onAbilityCreate(ability) {
      LogUtil.info(`AbilityLifecycleCallback onAbilityCreate ability: ${ability}`);
    },
    onWindowStageCreate(ability, windowStage) {
      LogUtil.info(`AbilityLifecycleCallback onWindowStageCreate ability: ${ability}`);
      LogUtil.info(`AbilityLifecycleCallback onWindowStageCreate windowStage: ${windowStage}`);
    },
    onWindowStageActive(ability, windowStage) {
      LogUtil.info(`AbilityLifecycleCallback onWindowStageActive ability: ${ability}`);
      LogUtil.info(`AbilityLifecycleCallback onWindowStageActive windowStage: ${windowStage}`);
    },
    onWindowStageInactive(ability, windowStage) {
      LogUtil.info(`AbilityLifecycleCallback onWindowStageInactive ability: ${ability}`);
      LogUtil.info(`AbilityLifecycleCallback onWindowStageInactive windowStage: ${windowStage}`);
    },
    onWindowStageDestroy(ability, windowStage) {
      LogUtil.info(`AbilityLifecycleCallback onWindowStageDestroy ability: ${ability}`);
      LogUtil.info(`AbilityLifecycleCallback onWindowStageDestroy windowStage: ${windowStage}`);
    },
    onAbilityDestroy(ability) {
      LogUtil.info(`AbilityLifecycleCallback onAbilityDestroy ability: ${ability}`);
    },
    onAbilityForeground(ability) {
      LogUtil.info(`AbilityLifecycleCallback onAbilityForeground ability: ${ability}`);
    },
    onAbilityBackground(ability) {
      LogUtil.info(`AbilityLifecycleCallback onAbilityBackground ability: ${ability}`);
    },
    onAbilityContinue(ability) {
      LogUtil.info(`AbilityLifecycleCallback onAbilityContinue ability: ${ability}`);
    }
  };


  @MockSetup
  mock() {
    this.describe = new DescribeBean("AppUtil", "APP相关工具类");
  }

  build() {
    Column() {
      TitleBarView({ describe: this.describe })
      Divider()
      Scroll(this.scroller) {
        Column({ space: 5 }) {
          Button("isApiSupported()")
            .btnStyle()
            .onClick(async () => {
              let api12 = AppUtil.isApiSupported(12);
              let api15 = AppUtil.isApiSupported(15);
              let api17 = AppUtil.isApiSupported(17);
              let api20 = AppUtil.isApiSupported(20);
              let api22 = AppUtil.isApiSupported(22);
              let api31 = AppUtil.isApiSupported(31);
              let infoStr = `API12: ${api12}\nAPI15: ${api15}\nAPI17: ${api17}\nAPI20: ${api20}\nAPI22: ${api22}\nAPI31: ${api31}`;
              Utils.showSheetText(infoStr);
            })
          Button("getApplicationContext()")
            .btnStyle()
            .onClick(() => {
              let applicationContext = AppUtil.getApplicationContext();
              ToastUtil.showToast("成功获取applicationContext");
            })
          Button("getContext()")
            .btnStyle()
            .onClick(() => {
              let context = AppUtil.getContext();
              ToastUtil.showToast("成功获取上下文");
            })
          Button("getUIContext()")
            .btnStyle()
            .onClick(() => {
              let uiContext = AppUtil.getUIContext();
              ToastUtil.showToast("成功获取UIContext");
            })
          Button("getWindowStage()")
            .btnStyle()
            .onClick(() => {
              let windowStage = AppUtil.getWindowStage();
              LogUtil.error(JSON.stringify(windowStage, null, 2));
              ToastUtil.showToast("成功获取WindowStage");
            })
          Button("getMainWindow()")
            .btnStyle()
            .onClick(() => {
              let mainWindow = AppUtil.getMainWindow();
              LogUtil.error(JSON.stringify(mainWindow, null, 2));
              ToastUtil.showToast("成功获取主窗口");
            })
          Button("getConfiguration()")
            .btnStyle()
            .onClick(() => {
              let config = AppUtil.getConfiguration();
              Utils.showSheetText(JSON.stringify(config, null, 2));
            })
          Button("setGrayScale()")
            .btnStyle()
            .onClick(() => {
              if (!this.blGray) {
                this.blGray = true;
                AppUtil.setGrayScale(1);
                ToastUtil.showToast("一键置灰成功");
              } else {
                this.blGray = false;
                AppUtil.setGrayScale(0);
                ToastUtil.showToast("取消置灰成功");
              }
            })
          Button("setColorMode()")
            .btnStyle()
            .onClick(() => {
              if (this.colorMode !== ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT) {
                this.colorMode = ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT;
                AppUtil.setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT);
              } else {
                this.colorMode = ConfigurationConstant.ColorMode.COLOR_MODE_DARK;
                AppUtil.setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_DARK);
              }
            })
          Button("getColorMode()")
            .btnStyle()
            .onClick(() => {
              let colorMode = AppUtil.getColorMode();
              ToastUtil.showToast(`应用的颜色模式:${colorMode}`);
            })
          Button("setFont()")
            .btnStyle()
            .onClick(() => {
              font.registerFont({ familyName: 'WCSF', familySrc: $rawfile('wcsf.ttf') });
              AppUtil.setFont('WCSF');
            })
          Button("setFontSizeScale()")
            .btnStyle()
            .onClick(() => {
              AppUtil.setFontSizeScale(2);
            })
          Button("getFontSizeScale()")
            .btnStyle()
            .onClick(() => {
              let fontSizeScale = AppUtil.getFontSizeScale();
              ToastUtil.showToast(`应用字体大小缩放比例:${fontSizeScale}`);
            })
          Button("setLanguage()")
            .btnStyle()
            .onClick(() => {
              if (this.blZhCn) {
                this.blZhCn = false;
                AppUtil.setLanguage('en-us');
              } else {
                this.blZhCn = true;
                AppUtil.setLanguage('zh-cn');
              }
              ToastUtil.showToast("设置成功!");
            })
          Button("getLanguage()")
            .btnStyle()
            .onClick(() => {
              let language = AppUtil.getLanguage();
              ToastUtil.showToast(`应用的语言:${language}`);
            })
          Button("setSupportedProcessCache()")
            .btnStyle()
            .onClick(() => {
              AppUtil.setSupportedProcessCache(true);
              ToastUtil.showToast("设置成功!");
            })
          Button("clearUpApplicationData()")
            .btnStyle()
            .onClick(() => {
              DialogHelper.showAlertDialog({
                content: '是否清理应用本身的数据?',
                primaryButton:'取消',
                secondaryButton:'清理',
                onAction: (action) => {
                  if (action === DialogAction.SURE) {
                    AppUtil.clearUpApplicationData();
                  }
                }
              });
            })
          Button("killAllProcesses()")
            .btnStyle()
            .onClick(() => {
              DialogHelper.showAlertDialog({
                content: '是否终止应用的所有进程?',
                primaryButton:'取消',
                secondaryButton:'终止',
                onAction: (action) => {
                  if (action === DialogAction.SURE) {
                    AppUtil.killAllProcesses();
                  }
                }
              });
            })
          Button("restartApp()")
            .btnStyle()
            .onClick(() => {
              DialogHelper.showAlertDialog({
                autoCancel: false,
                backCancel: false,
                content: '是否重启应用?',
                primaryButton:'取消',
                secondaryButton:'重启',
                onAction: (action) => {
                  if (action === DialogAction.SURE) {
                    AppUtil.restartApp();
                  }
                }
              });
            })
          Button("exit()")
            .btnStyle()
            .onClick(() => {
              DialogHelper.showAlertDialog({
                content: '是否重退出应用?',
                primaryButton:'取消',
                secondaryButton:'退出',
                onAction: (action) => {
                  if (action === DialogAction.SURE) {
                    AppUtil.exit();
                  }
                }
              });
            })
          Button("getRunningProcessInformation()")
            .btnStyle()
            .onClick(async () => {
              let processInformation = await AppUtil.getRunningProcessInformation();
              let jsonStr = JSON.stringify(processInformation, null, 2);
              Utils.showSheetText(jsonStr);
            })
          Button("onApplicationStateChange()")
            .btnStyle()
            .onClick(() => {
              AppUtil.onApplicationStateChange(this.applicationStateChangeCallback);
              DialogHelper.showToast('添加“applicationStateChange”监听成功!');
            })
          Button("offApplicationStateChange()")
            .btnStyle()
            .onClick(() => {
              AppUtil.offApplicationStateChange(this.applicationStateChangeCallback);
              DialogHelper.showToast('移除“applicationStateChange”监听成功!');
            })
          Button("onEnvironment()")
            .btnStyle()
            .onClick(() => {
              this.callback1 = AppUtil.onEnvironment(this.environmentCallback);
              DialogHelper.showToast('添加“environment”监听成功!');
            })
          Button("offEnvironment()")
            .btnStyle()
            .onClick(() => {
              if (this.callback1 !== -1) {
                AppUtil.offEnvironment(this.callback1);
                DialogHelper.showToast('移除“environment”监听成功!');
              }
            })
          Button("onAbilityLifecycle()")
            .btnStyle()
            .onClick(() => {
              this.callback2 = AppUtil.onAbilityLifecycle(this.abilityLifecycleCallback);
              DialogHelper.showToast('添加“abilityLifecycle”监听成功!');
            })
          Button("offAbilityLifecycle()")
            .btnStyle()
            .onClick(() => {
              if (this.callback2 !== -1) {
                AppUtil.offAbilityLifecycle(this.callback2);
                DialogHelper.showToast('移除“abilityLifecycle”监听成功!');
              }
            })
          Button("getKeyboardAvoidMode()")
            .btnStyle()
            .onClick(() => {
              this.mode = AppUtil.getKeyboardAvoidMode();
              ToastUtil.showToast(`当前模式为: ${StrUtil.equal(this.mode, KeyboardAvoidMode.OFFSET) ? "上抬模式" : "压缩模式"}`)
            })
          Button("setKeyboardAvoidMode()")
            .btnStyle()
            .onClick(() => {
              AppUtil.setKeyboardAvoidMode(StrUtil.equal(this.mode, KeyboardAvoidMode.OFFSET) ?
              KeyboardAvoidMode.RESIZE : KeyboardAvoidMode.OFFSET);
              this.mode = AppUtil.getKeyboardAvoidMode();
              ToastUtil.showToast(`设置模式为: ${StrUtil.equal(this.mode, KeyboardAvoidMode.OFFSET) ? "上抬模式" :
                "压缩模式"}`)
            })
          TextInput({ placeholder: '设置模式后点击输入框查看效果' })
            .width("95%")
            .margin({ bottom: 10 })

          Button("isPortrait()")
            .btnStyle()
            .onClick(() => {
              let isPortrait = AppUtil.isPortrait();
              ToastUtil.showToast(`当前是否竖屏: ${isPortrait}`);
            })
          Button("isLandscape()")
            .btnStyle()
            .onClick(() => {
              let isLandscape = AppUtil.isLandscape();
              ToastUtil.showToast(`当前是否横屏: ${isLandscape}`);
            })
          Button("getStatusBarHeight()")
            .btnStyle()
            .onClick(() => {
              let statusBarHeight = AppUtil.getStatusBarHeight();
              ToastUtil.showToast(`状态栏的高度为:${statusBarHeight}px`)
            })
          Button("getNavigationIndicatorHeight()")
            .btnStyle()
            .onClick(() => {
              let indicatorHeight = AppUtil.getNavigationIndicatorHeight();
              ToastUtil.showToast(`底部导航条的高度为:${indicatorHeight}px`)
            })
          Button("setStatusBar()")
            .btnStyle()
            .onClick(() => {
              this.statusBar = this.statusBar ? false : true;
              if (this.statusBar) {
                AppUtil.setStatusBar();
              } else {
                AppUtil.setStatusBar(false, true);
              }
            })
          Button("getBundleInfo()")
            .btnStyle()
            .onClick(async () => {
              let bundleInfo = await AppUtil.getBundleInfo();
              let infoStr = JSON.stringify(bundleInfo, null, 2);
              Utils.showSheetText(infoStr);
            })
          Button("getAppInfoSync()")
            .btnStyle()
            .onClick(() => {
              let appInfo = AppUtil.getAppInfoSync();
              let infoStr = JSON.stringify(appInfo, null, 2);
              Utils.showSheetText(infoStr);
            })
          Button("getSignatureInfo()")
            .btnStyle()
            .onClick(async () => {
              let signatureInfo = await AppUtil.getSignatureInfo();
              let infoStr = JSON.stringify(signatureInfo, null, 2);
              Utils.showSheetText(infoStr);
            })
          Button("getBundleName()\ngetVersionCode()\ngetVersionName()\ngetTargetVersion()\ngetInstallTime()\ngetUpdateTime()\ngetAppProvisionType()\ndebug()")
            .labelStyle({ maxLines: 10 })
            .type(ButtonType.Normal)
            .borderRadius(10)
            .padding({ top: 10, bottom: 10 })
            .btnStyle()
            .onClick(() => {
              let bundleName = AppUtil.getBundleName();
              let versionCode = AppUtil.getVersionCode();
              let versionName = AppUtil.getVersionName();
              let targetVersion = AppUtil.getTargetVersion();
              let infoStr = `bundleName: ${bundleName}\nversionCode: ${versionCode}\nversionName: ${versionName}\ntargetVersion: ${targetVersion}`;
              let installTime = AppUtil.getInstallTime();
              let updateTime = AppUtil.getUpdateTime();
              infoStr = infoStr + `\ninstallTime: ${installTime}\nupdateTime: ${updateTime}`;
              let appProvisionType = AppUtil.getAppProvisionType();
              let debug = AppUtil.debug();
              infoStr = infoStr + `\nappProvisionType: ${appProvisionType}\ndebug: ${debug}`;
              Utils.showSheetText(infoStr);
            })
        }
        .margin({ top: 5, bottom: 5 })
      }
      .layoutWeight(1)
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Start)
    .backgroundColor($r('app.color.main_background'))
  }
}


@Styles
function btnStyle() {
  .width('90%')
  .margin({ top: 10, bottom: 5 })
}

🍎Contribution code and technology communication

Any problems found during use can be askedIssueGive us; Of course, we also welcome you to send us a messagePR

https://gitee.com/tongyuyan/harmony-utils
https://github.com/787107497

🌏Open Source Protocol

This project is based onApache License 2.0, when copying and borrowing codes, please be sure to indicate the source.

0
Subscribe to my newsletter

Read articles from 桃花镇童长老 directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

桃花镇童长老
桃花镇童长老