1 /* 2 * Copyright (C) 2025 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_MEDIA_CHANGE_INFO_H 17 #define OHOS_MEDIA_CHANGE_INFO_H 18 19 #include <string> 20 #include <list> 21 #include <variant> 22 #include <sstream> 23 #include "parcel.h" 24 #include "media_log.h" 25 #include "userfile_manager_types.h" 26 #include "accurate_common_data.h" 27 #include "album_change_info.h" 28 #include "photo_asset_change_info.h" 29 30 namespace OHOS { 31 namespace Media { 32 namespace Notification { 33 using namespace Media::AccurateRefresh; 34 35 enum NotifyType { 36 NOTIFY_ASSET_ADD, 37 NOTIFY_ASSET_UPDATE, 38 NOTIFY_ASSET_REMOVE, 39 NOTIFY_ALBUM_ADD, 40 NOTIFY_ALBUM_UPDATE, 41 NOTIFY_ALBUM_REMOVE, 42 }; 43 enum NotifyUriType { 44 PHOTO_URI, 45 HIDDEN_PHOTO_URI, 46 TRASH_PHOTO_URI, 47 PHOTO_ALBUM_URI, 48 HIDDEN_ALBUM_URI, 49 TRASH_ALBUM_URI, 50 ANALYSIS_ALBUM_URI, 51 INVALID 52 }; 53 54 class NotifyDetailInfo : public Parcelable { 55 public: 56 NotifyUriType uri; 57 std::vector<int32_t> indexs; 58 NotifyType notifyType; 59 60 public: ToString()61 std::string ToString() const 62 { 63 std::stringstream ss; 64 ss << "uri:" << static_cast<uint16_t>(uri) << ", notifyType:" << static_cast<uint16_t>(notifyType) << " , "; 65 ss << "indexs: {"; 66 for (const auto& item: indexs) { 67 ss << item << " "; 68 } 69 ss << "}"; 70 return ss.str(); 71 } 72 Unmarshalling(Parcel & parcel)73 static std::shared_ptr<NotifyDetailInfo> Unmarshalling(Parcel &parcel) 74 { 75 MEDIA_DEBUG_LOG("unmarshalling debug: std::shared_ptr<NotifyDetailInfo> Unmarshalling"); 76 NotifyDetailInfo* info = new (std::nothrow)NotifyDetailInfo(); 77 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) { 78 delete info; 79 info = nullptr; 80 } 81 return std::shared_ptr<NotifyDetailInfo>(info); 82 } 83 Marshalling(Parcel & parcel)84 bool Marshalling(Parcel &parcel) const override 85 { 86 parcel.WriteUint16(static_cast<uint16_t>(this->uri)); 87 parcel.WriteInt32Vector(this->indexs); 88 parcel.WriteUint16(static_cast<uint16_t>(this->notifyType)); 89 return true; 90 } 91 92 private: ReadFromParcel(Parcel & parcel)93 bool ReadFromParcel(Parcel &parcel) 94 { 95 MEDIA_INFO_LOG("unmarshalling debug: NotifyDetailInfo::ReadFromParcel"); 96 this->uri = static_cast<NotifyUriType>(parcel.ReadUint16()); 97 parcel.ReadInt32Vector(&this->indexs); 98 this->notifyType = static_cast<NotifyType>(parcel.ReadUint16()); 99 return true; 100 } 101 }; 102 103 struct MarshallingVisitor { 104 Parcel &parcel; 105 bool isSystem; operatorMarshallingVisitor106 bool operator()(const PhotoAssetChangeData &data) const 107 { 108 return data.Marshalling(parcel, isSystem); 109 } operatorMarshallingVisitor110 bool operator()(const AlbumChangeData &data) const 111 { 112 return data.Marshalling(parcel, isSystem); 113 } 114 }; 115 116 struct ToStringVisitor { 117 bool isDetail = false; operatorToStringVisitor118 std::string operator()(const PhotoAssetChangeData &data) const 119 { 120 return data.ToString(isDetail); 121 } operatorToStringVisitor122 std::string operator()(const AlbumChangeData &data) const 123 { 124 return data.ToString(isDetail); 125 } 126 }; 127 128 class MediaChangeInfo : public Parcelable { 129 public: 130 std::vector<std::variant<PhotoAssetChangeData, AlbumChangeData>> changeInfos; 131 bool isForRecheck; 132 NotifyUriType notifyUri; 133 NotifyType notifyType; 134 bool isSystem; 135 136 public: 137 std::string ToString(bool isDetail = false) const 138 { 139 std::stringstream ss; 140 ss << "isForRecheck: " << isForRecheck <<", notifyUri:" << static_cast<uint16_t>(notifyUri) 141 << ", notifyType:" << static_cast<uint16_t>(notifyType) << ", isSystem:" << isSystem <<"."; 142 for (size_t i = 0; i < changeInfos.size(); ++i) { 143 ss << "changeInfo[" << i << "]: " << std::visit(ToStringVisitor{isDetail}, changeInfos[i]) << ", "; 144 } 145 return ss.str(); 146 } 147 Unmarshalling(Parcel & parcel)148 static std::shared_ptr<MediaChangeInfo> Unmarshalling(Parcel &parcel) 149 { 150 MediaChangeInfo* info = new (std::nothrow)MediaChangeInfo(); 151 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) { 152 delete info; 153 info = nullptr; 154 } 155 return std::shared_ptr<MediaChangeInfo>(info); 156 } Marshalling(Parcel & parcel,bool isSystem)157 bool Marshalling(Parcel &parcel, bool isSystem) const 158 { 159 size_t photoMaxLimit = 100; 160 size_t photoAlbumMaxLimit = 50; 161 size_t assetChangeDataSize = 0; 162 size_t albumChangeDataSize = 0; 163 size_t changeInfoSize = changeInfos.size(); 164 parcel.WriteUint32(changeInfoSize); 165 for (const auto& item: changeInfos) { 166 if (std::holds_alternative<PhotoAssetChangeData>(item) && assetChangeDataSize < photoMaxLimit) { 167 assetChangeDataSize++; 168 parcel.WriteBool(true); 169 } else if (std::holds_alternative<AlbumChangeData>(item) && albumChangeDataSize < photoAlbumMaxLimit) { 170 albumChangeDataSize++; 171 parcel.WriteBool(false); 172 } else { 173 MEDIA_ERR_LOG("assetChangeData or lbumChangeData size exceeds the maximum limit."); 174 return false; 175 } 176 std::visit(MarshallingVisitor{parcel, isSystem}, item); 177 } 178 parcel.WriteBool(isForRecheck); 179 parcel.WriteUint16(static_cast<uint16_t>(notifyUri)); 180 parcel.WriteUint16(static_cast<uint16_t>(notifyType)); 181 parcel.WriteBool(isSystem); 182 MEDIA_INFO_LOG("parcel size is: %{public}d", (int)parcel.GetDataSize()); 183 return true; 184 } 185 Marshalling(Parcel & parcel)186 bool Marshalling(Parcel &parcel) const override 187 { 188 return Marshalling(parcel, false); 189 } 190 ReadFromParcelInMultiMode(Parcel & parcel)191 bool ReadFromParcelInMultiMode(Parcel &parcel) 192 { 193 this->isForRecheck = parcel.ReadBool(); 194 this->notifyUri = static_cast<NotifyUriType>(parcel.ReadUint16()); 195 this->notifyType = static_cast<NotifyType>(parcel.ReadUint16()); 196 this->isSystem = parcel.ReadBool(); 197 198 bool validFlag = parcel.ReadBool(); 199 while (validFlag) { 200 bool type = parcel.ReadBool(); 201 if (type) { 202 std::shared_ptr<PhotoAssetChangeData> item = PhotoAssetChangeData::Unmarshalling(parcel); 203 if (item == nullptr) { 204 MEDIA_ERR_LOG("item is nullptr"); 205 return false; 206 } 207 this->changeInfos.push_back(*item); 208 } else { 209 std::shared_ptr<AlbumChangeData> item = AlbumChangeData::Unmarshalling(parcel); 210 if (item == nullptr) { 211 MEDIA_ERR_LOG("item is nullptr"); 212 return false; 213 } 214 this->changeInfos.push_back(*item); 215 } 216 validFlag = parcel.ReadBool(); 217 } 218 return true; 219 } 220 221 private: ReadFromParcel(Parcel & parcel)222 bool ReadFromParcel(Parcel &parcel) 223 { 224 uint32_t size = -1; 225 bool ret = parcel.ReadUint32(size); 226 if (!ret) { 227 MEDIA_ERR_LOG("failed to Unmarshalling notifyDetails"); 228 return false; 229 } 230 231 for (uint32_t i = 0; i < size; i++) { 232 bool type = parcel.ReadBool(); 233 if (type) { 234 std::shared_ptr<PhotoAssetChangeData> item = PhotoAssetChangeData::Unmarshalling(parcel); 235 if (item == nullptr) { 236 MEDIA_ERR_LOG("item is nullptr"); 237 return false; 238 } 239 this->changeInfos.push_back(*item); 240 } else { 241 std::shared_ptr<AlbumChangeData> item = AlbumChangeData::Unmarshalling(parcel); 242 if (item == nullptr) { 243 MEDIA_ERR_LOG("item is nullptr"); 244 return false; 245 } 246 this->changeInfos.push_back(*item); 247 } 248 } 249 250 this->isForRecheck = parcel.ReadBool(); 251 this->notifyUri = static_cast<NotifyUriType>(parcel.ReadUint16()); 252 this->notifyType = static_cast<NotifyType>(parcel.ReadUint16()); 253 this->isSystem = parcel.ReadBool(); 254 return true; 255 } 256 }; 257 } // namespace Notification 258 } // namespace Media 259 } // namespace OHOS 260 261 #endif // OHOS_MEDIA_CHANGE_INFO_H