1 /* 2 * Copyright (c) 2021 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 16 #ifndef OHOS_ABILITY_RUNTIME_MISSION_DATA_STORAGE_H 17 #define OHOS_ABILITY_RUNTIME_MISSION_DATA_STORAGE_H 18 19 #include <list> 20 #include <mutex> 21 #include <queue> 22 23 #include "event_handler.h" 24 #include "inner_mission_info.h" 25 #include "mission_snapshot.h" 26 27 namespace OHOS { 28 namespace AAFwk { 29 constexpr const char* TASK_DATA_FILE_BASE_PATH = "/data/service/el1/public/AbilityManagerService"; 30 constexpr const char* MISSION_DATA_FILE_PATH = "MissionInfo"; 31 constexpr const char* MISSION_JSON_FILE_PREFIX = "mission"; 32 constexpr const char* LOW_RESOLUTION_FLAG = "little"; 33 constexpr const char* JSON_FILE_SUFFIX = ".json"; 34 constexpr const char* JPEG_FILE_SUFFIX = ".jpg"; 35 constexpr const char* FILE_SEPARATOR = "/"; 36 constexpr const char* UNDERLINE_SEPARATOR = "_"; 37 const int32_t SCALE = 2; 38 39 class MissionDataStorage : public std::enable_shared_from_this<MissionDataStorage> { 40 public: 41 MissionDataStorage() = default; 42 MissionDataStorage(int userId); 43 virtual ~MissionDataStorage(); 44 45 void SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> &handler); 46 47 /** 48 * @brief GeT all mission info. 49 * @return Returns true if this function is successfully called; returns false otherwise. 50 */ 51 bool LoadAllMissionInfo(std::list<InnerMissionInfo> &missionInfoList); 52 53 /** 54 * @brief Save the mission data. 55 * @param missionInfo Indicates the missionInfo object to be save. 56 */ 57 void SaveMissionInfo(const InnerMissionInfo &missionInfo); 58 59 /** 60 * @brief Delete the bundle data corresponding to the mission Id. 61 * @param missionId Indicates this mission id. 62 */ 63 void DeleteMissionInfo(int missionId); 64 65 /** 66 * @brief Save mission snapshot 67 * @param missionId Indicates this mission id. 68 * @param missionSnapshot the mission snapshot to save 69 */ 70 void SaveMissionSnapshot(int32_t missionId, const MissionSnapshot& missionSnapshot); 71 72 /** 73 * @brief Delete mission snapshot 74 * @param missionId Indicates this mission id. 75 */ 76 void DeleteMissionSnapshot(int32_t missionId); 77 78 /** 79 * @brief Get the Mission Snapshot object 80 * @param missionId id of mission. 81 * @param missionSnapshot snapshot of target mission id. 82 * @param isLowResolution low resolution. 83 * @return Returns true if this function is successfully called; returns false otherwise. 84 */ 85 bool GetMissionSnapshot(int32_t missionId, MissionSnapshot& missionSnapshot, bool isLowResolution); 86 87 #ifdef SUPPORT_GRAPHICS 88 /** 89 * Get low resoultion pixelmap of source. 90 * 91 * @param source source pixelmap. 92 * @return return reduced pixel map. 93 */ 94 static std::shared_ptr<OHOS::Media::PixelMap> GetReducedPixelMap( 95 const std::shared_ptr<OHOS::Media::PixelMap>& source); 96 97 /** 98 * @brief Get the Snapshot object 99 * @param missionId Indicates this mission id. 100 * @return Returns PixelMap of snapshot. 101 */ 102 std::shared_ptr<Media::PixelMap> GetSnapshot(int missionId, bool isLowResolution = false) const; 103 104 std::unique_ptr<Media::PixelMap> GetPixelMap(int missionId, bool isLowResolution) const; 105 #endif 106 107 private: 108 std::string GetMissionDataDirPath() const; 109 110 std::string GetMissionDataFilePath(int missionId); 111 112 std::string GetMissionSnapshotPath(int32_t missionId, bool isLowResolution) const; 113 114 bool CheckFileNameValid(const std::string &fileName); 115 116 #ifdef SUPPORT_GRAPHICS 117 void WriteRgb888ToJpeg(const char* fileName, uint32_t width, uint32_t height, const uint8_t* data); 118 119 bool GetCachedSnapshot(int32_t missionId, MissionSnapshot& missionSnapshot); 120 121 bool SaveCachedSnapshot(int32_t missionId, const MissionSnapshot& missionSnapshot); 122 123 bool DeleteCachedSnapshot(int32_t missionId); 124 void DeleteMissionSnapshot(int32_t missionId, bool isLowResolution); 125 126 void SaveSnapshotFile(int32_t missionId, const MissionSnapshot& missionSnapshot); 127 128 void SaveSnapshotFile(int32_t missionId, const std::shared_ptr<OHOS::Media::PixelMap>& snapshot, 129 bool isPrivate, bool isLowResolution); 130 131 bool RGB565ToRGB888(const uint16_t *rgb565Buf, int32_t rgb565Size, uint8_t *rgb888Buf, int32_t rgb888Size); 132 bool RGBA8888ToRGB888(const uint32_t *rgba8888Buf, int32_t rgba8888Size, uint8_t *rgb888Buf, int32_t rgb888Size); 133 void SaveRGB565Image(const std::shared_ptr<Media::PixelMap> &frame, const char* fileName); 134 void SaveRGBA8888Image(const std::shared_ptr<Media::PixelMap> &frame, const char* fileName); 135 136 std::map<int32_t, std::shared_ptr<Media::PixelMap>> cachedPixelMap_; 137 #endif 138 139 int userId_ = 0; 140 std::shared_ptr<AppExecFwk::EventHandler> handler_; 141 std::mutex cachedPixelMapMutex_; 142 }; 143 } // namespace AAFwk 144 } // namespace OHOS 145 #endif // OHOS_ABILITY_RUNTIME_MISSION_DATA_STORAGE_H 146