/*---------------------------------------------------------------------------- * Copyright (c) TJD Technologies Co., Ltd. 2024. All rights reserved. * * Description: Common UI setting center, such as low power mode, screen brightness, etc. * * Author: luziquan@ss-tjd.com * * Create: 2024-10-16 *--------------------------------------------------------------------------*/ #include "TjdUiSettingCenter.h" #include "NativeAbility.h" #include "TjdUiAppPlayerModel.h" #include "TjdUiMsgCenter.h" #include "alipay_feature.h" #include "ble_api.h" #include "common_pm.h" #include "display_action.h" #include "graphic_service.h" #include "motor.h" #include "notification_manager.h" #include "power_display_service.h" #include "rtc_api.h" #include "service_ancillary.h" #include "soc_lcd_api.h" #include "sql_setting.h" #include "sys_config.h" #include "sys_restart.h" #include #include #include #include #include #include "TjdUiAppAlbumModel.h" extern "C" { #include "hal_reboot.h" #include "task_ancillary.h" #include } #define ENABLE_PRINT_INFO 1 #if ENABLE_PRINT_INFO #define static_print_info(...) sys_ui_log_i(__VA_ARGS__) // 一般信息打印宏控制 #define static_print_warn(...) sys_ui_log_w(__VA_ARGS__) // 警告信息打印一般常开 #define static_print_error(...) sys_ui_log_e(__VA_ARGS__) // 错误信息打印一般常开 #define static_print_debug(...) sys_ui_log_d(__VA_ARGS__) // 调试信息打印一般常开 #else #define static_print_info(...) #define static_print_warn(...) #define static_print_error(...) #define static_print_debug(...) #endif namespace TJD { typedef void (*PostEventHandleFunction)(void); static PostEventHandleFunction g_postEventCallback = nullptr; static signed int EventHandler(void *param) { if (g_postEventCallback != nullptr) { g_postEventCallback(); } return 0; } struct SettingCenterEventInfo { signed int (*func_event_handler)(void *param); // 事件处理函数 void *param; // 事件处理函数入参 void (*func_event_debug)(char *str, ...); // 打印 void *payload; }; static void PostGraphicEvent(PostEventHandleFunction event) { SettingCenterEventInfo g_msg_data = { .func_event_handler = EventHandler, .param = NULL, .func_event_debug = NULL, .payload = NULL, }; g_postEventCallback = event; static unsigned int g_msg_size = sizeof(SettingCenterEventInfo); osal_msg_queue_write_copy(tjd_task_ancillary_get_queue_id(), &g_msg_data, g_msg_size, 0); } static void DeleteDirectoryContents(const char *dirPath) { DIR *dir = opendir(dirPath); if (dir == nullptr) { static_print_debug("Failed to open directory: %s", dirPath); return; } // 获取目录名 std::string dirName = std::string(dirPath).substr(std::string(dirPath).find_last_of("/") + 1); std::string skipFileName = dirName + ".txt"; struct dirent *entry; while ((entry = readdir(dir)) != nullptr) { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } std::string fullPath = std::string(dirPath) + "/" + entry->d_name; struct stat statbuf; if (stat(fullPath.c_str(), &statbuf) == 0) { if (S_ISDIR(statbuf.st_mode)) { DeleteDirectoryContents(fullPath.c_str()); rmdir(fullPath.c_str()); } else { // 检查文件名是否为与目录同名的 .txt 文件 if (strcmp(entry->d_name, skipFileName.c_str()) != 0) { remove(fullPath.c_str()); } } } } closedir(dir); } void TjdUiAlbumRecovery(void) { static_print_debug("TjdUiAlbumRecovery:"); struct dirent *direntp; const std::vector keep_files = { "panda001.bin", "panda001.mp4", "sea_wave.bin", "sea_wave.mp4", "tjd_album.txt" }; // 打开目录 DIR *dirp = opendir(TJD_FS_DIR_ALBUM); // 遍历文件 if (dirp != nullptr) { std::string albumDirPath(TJD_FS_DIR_ALBUM); while ((direntp = readdir(dirp)) != nullptr) { bool isKeep = false; std::string filename(direntp->d_name); for (const auto &file : keep_files) { if (file == filename) { isKeep = true; break; } } if(!isKeep) { std::string fullPath = albumDirPath + "/" + filename; remove(fullPath.c_str()); } } } else { static_print_error("%s opendir err!", __func__); } // 关闭目录 closedir(dirp); } void TjdUiSetDefaultWf(void) { //恢复默认表盘 std::string filename; TjdUiAppAlbumModel::GetInstance().UpdateWfJson(ALBUM_VIDEO, filename); } void TjdUiMusicRecovery(void) { static_print_debug("TjdUiMusicRecovery:"); const std::vector keep_files = { "Color Your Night.lrc", "Color Your Night.mp3", "Night Voyager.lrc", "Night Voyager.mp3", "tjd_music.txt" }; // 打开目录 DIR *dirp = opendir(TJD_FS_DIR_MUSIC); struct dirent *direntp; if(dirp != nullptr){ while((direntp = readdir(dirp)) != nullptr){ bool isKeep = false; std::string dirpath(TJD_FS_DIR_MUSIC); std::string filename(direntp->d_name); for(const auto &file : keep_files){ if(file == filename){ isKeep = true; break; } } if(!isKeep){ std::string fullPath = dirpath + "/" + filename; remove(fullPath.c_str()); } } }else { static_print_error("%s opendir err!", __func__); } // 关闭目录 closedir(dirp); } extern void TjdUiWfRecovery(void); void tjd_recovery_user_partition(void) { TjdUiWfRecovery(); TjdUiSetDefaultWf(); TjdUiAlbumRecovery(); DeleteDirectoryContents(TJD_FS_DIR_BOOK); TjdUiMusicRecovery(); } void TjdUiSettingCenter::Init() { register_gap_connect_state_callback_t([](int val) { TjdUiSettingCenter::GetInstance().BleConnectStateEvent(val); }); } TjdUiSettingCenter &TJD::TjdUiSettingCenter::GetInstance() { static TjdUiSettingCenter instance; return instance; } void TjdUiSettingCenter::BleConnectStateEvent(int val) { if (val == GAP_ACL_STATE_CONNECTED) { for (auto &callback : bleConnectStateCallbackList_) { callback(true); } } else if (val == GAP_ACL_STATE_DISCONNECTED) { for (auto &callback : bleConnectStateCallbackList_) { callback(false); } } } void TjdUiSettingCenter::SetLowPowerMode(bool enable) { if (OHOS::NotificationManager::GetInstance()->HasNotifyShowing()) { const uint16_t curTopic = OHOS::NotificationManager::GetInstance()->GetShowingNotifyId(); if (curTopic == TJDUI_TOPIC_EVENT_CHARING || curTopic == TJDUI_TOPIC_EVENT_SHUTDOWN || curTopic == TJDUI_TOPIC_EVENT_LOW_POWER) { OHOS::NotificationManager::GetInstance()->StopNotify(); } } sql_setting_set_power_saving_mode(enable); if (enable) { BatterySaverProcess(); } else { power_display_svr_get_api()->set_brightness(sql_setting_get_brightness()); uapi_lcd_exit_idle_mode(); } } bool TjdUiSettingCenter::IsConnectApp() { return tjd_get_ble_is_connect(); } void TjdUiSettingCenter::ClearExcFile() { DeleteDirectoryContents("/user/exc"); } void TjdUiSettingCenter::NotifShakeEvent() { if (sql_setting_get_notification() == false) { return; } motor_param_t motorParam = {.cycle1_on = 250, .cycle1_off = 750, .cycle1_cnt = 1, .duty = 100}; tjd_driver_motor_user_start(&motorParam); } void TjdUiSettingCenter::PhoneShakeEvent() { if (sql_setting_get_notification() == false) { return; } motor_param_t motorParam = {.cycle1_on = 500, .cycle1_off = 500, .cycle1_cnt = 60, .duty = 100}; tjd_driver_motor_user_start(&motorParam); } void TjdUiSettingCenter::ShakeEndEvent() { tjd_driver_motor_user_stop(); } void TjdUiSettingCenter::KeepScreenMaxOn() { const power_display_svr_api_t *screenApi = power_display_svr_get_api(); screenApi->set_auto_timeout_function(false); screenApi->set_brightness(0xff); } void TjdUiSettingCenter::KeepScreenMaxOff() { uint8_t brightness = sql_setting_get_brightness(); const power_display_svr_api_t *screenApi = power_display_svr_get_api(); screenApi->set_auto_timeout_function(true); screenApi->set_brightness(brightness); } void TjdUiSettingCenter::ResetSystem() { PostGraphicEvent([]() { static_print_debug("ResetSystem thread run"); tjd_action_display_lcd_off(); osDelay(500); OHOS::NativeAbility::GetInstance().StopAbility(); sql_setting_set_save_flag(true); tjd_driver_rtc_save_time(); tjd_driver_pm_shutdown_event(); tjd_service_ancillary_device_restart(); }); } void TjdUiSettingCenter::ShutdownSystem() { PostGraphicEvent([]() { static_print_debug("ShutdownSystem thread run"); tjd_action_display_lcd_off(); osDelay(500); OHOS::NativeAbility::GetInstance().StopAbility(); /* 关机前的资源保存 */ tjd_driver_rtc_save_time(); /* 保存资源 */ sql_setting_set_save_flag(true); tjd_sys_restart_data_save(); tjd_driver_pm_shutdown_event(); tjd_driver_pm_power_down_event(); uapi_sys_shipmode(0); }); } void TjdUiSettingCenter::RecoverySystem() { PostGraphicEvent([]() { static_print_debug("RecoverySystem thread run"); tjd_action_display_lcd_off(); osDelay(500); OHOS::NativeAbility::GetInstance().StopAbility(); alipay_svr_get_api()->exit_lpm(); uapi_alipay_svr_reset(); /* 删除内容 */ DeleteDirectoryContents("/system"); DeleteDirectoryContents("/user/tjd_record"); DeleteDirectoryContents("/user/tjd_phone"); DeleteDirectoryContents("/user/exc"); DeleteDirectoryContents("/update"); tjd_recovery_user_partition(); tjd_driver_pm_recover_event(); static_print_debug("Deleted all contents"); hal_reboot_chip(); }); } void TjdUiSettingCenter::BatterySaverProcess() { TjdUiAppPlayerModel *playerModel = TjdUiAppPlayerModel::GetInstance(); /* 屏幕亮度将降到最低,震动、铃声将关闭 */ auto playCtr = playerModel->GetPlayerCtr(); if (playCtr.get()) { int32_t state = 0; playCtr->GetPlayerState(state); playerModel->PlayerStop(); // if (state == OHOS::Media::PlayerStates::PLAYER_STARTED || state == OHOS::Media::PlayerStates::PLAYER_PAUSED) // { // playCtr->Stop(); // } } power_display_svr_get_api()->set_brightness(lowPowerBrightness_); uapi_lcd_enter_idle_mode(); } } // namespace TJD