diff --git a/frameworks/services/media_mtp/include/ptp_media_sync_observer.h b/frameworks/services/media_mtp/include/ptp_media_sync_observer.h index 360f5cb74b031696f27594c6050d6a7a9651d98c..1f16e962433eb83c0e7489a17913c320e17a30fe 100644 --- a/frameworks/services/media_mtp/include/ptp_media_sync_observer.h +++ b/frameworks/services/media_mtp/include/ptp_media_sync_observer.h @@ -48,7 +48,9 @@ private: void SendPhotoEvent(ChangeType changeType, std::string suffixString); std::vector GetHandlesFromPhotosInfoBurstKeys(int32_t handle); void SendEventToPTP(int32_t suff_int, ChangeType changeType); + std::vector GetAllDeleteHandles(); std::shared_ptr GetAlbumInfo(); + void SendPhotoRemoveEvent(std::string suffixString); }; class MediaSyncNotifyData : public AsyncTaskData { diff --git a/frameworks/services/media_mtp/src/ptp_media_sync_observer.cpp b/frameworks/services/media_mtp/src/ptp_media_sync_observer.cpp index bedd0250c354e539e3c17915e4b7ffe24589156b..dac4e46d01304b0c7c1811a4cba517a8484ff4a8 100644 --- a/frameworks/services/media_mtp/src/ptp_media_sync_observer.cpp +++ b/frameworks/services/media_mtp/src/ptp_media_sync_observer.cpp @@ -13,9 +13,12 @@ * limitations under the License. */ +#include "ptp_media_sync_observer.h" + +#include + #include "media_log.h" #include "ptp_album_handles.h" -#include "ptp_media_sync_observer.h" #include "photo_album_column.h" #include "datashare_predicates.h" #include "datashare_abs_result_set.h" @@ -27,10 +30,12 @@ namespace OHOS { namespace Media { constexpr int32_t RESERVE_ALBUM = 10; constexpr int32_t PARENT_ID = 0; +constexpr int32_t DELETE_LIMIT_TIME = 5000; const string BURST_COVER_LEVEL = "1"; const string BURST_NOT_COVER_LEVEL = "2"; const string IS_LOCAL = "2"; const std::string HIDDEN_ALBUM = ".hiddenAlbum"; + bool startsWith(const std::string& str, const std::string& prefix) { if (prefix.size() > str.size() || prefix.empty() || str.empty()) { @@ -57,6 +62,7 @@ void MediaSyncObserver::SendEventPackets(uint32_t objectHandle, uint16_t eventCo CHECK_AND_RETURN_LOG(context_ != nullptr, "Mtp Ptp context is nullptr"); MtpPacketTool::PutUInt32(outBuffer, context_->transactionID); MtpPacketTool::PutUInt32(outBuffer, objectHandle); + MEDIA_DEBUG_LOG("MtpMediaLibrary album [%{public}d]", objectHandle); event.data = outBuffer; CHECK_AND_RETURN_LOG(context_->mtpDriver != nullptr, "Mtp Ptp mtpDriver is nullptr"); @@ -125,16 +131,68 @@ vector MediaSyncObserver::GetHandlesFromPhotosInfoBurstKeys(int32_t han return handlesResult; } -void MediaSyncObserver::SendPhotoEvent(ChangeType changeType, string suffixString) +vector MediaSyncObserver::GetAllDeleteHandles() { - if (stoi(suffixString) <= 0) { - return; + vector handlesResult; + if (dataShareHelper_ == nullptr) { + MEDIA_ERR_LOG("MtpMedialibraryManager::GetAllDeleteHandles fail to get datasharehelper"); + return handlesResult; } Uri uri(PAH_QUERY_PHOTO); vector columns; + columns.push_back(MediaColumn::MEDIA_ID); DataShare::DataSharePredicates predicates; - shared_ptr resultSet; + auto now = std::chrono::system_clock::now(); + auto now_milliseconds = std::chrono::duration_cast(now.time_since_epoch()).count(); + auto new_timestamp = now_milliseconds - DELETE_LIMIT_TIME; + predicates.GreaterThan(MediaColumn::MEDIA_DATE_TRASHED, to_string(new_timestamp)); + shared_ptr resultSet = dataShareHelper_->Query(uri, predicates, columns); + + CHECK_AND_RETURN_RET_LOG(resultSet != nullptr, + handlesResult, "MtpMedialibraryManager fail to get PHOTO_ALL_DELETE_KEY"); + CHECK_AND_RETURN_RET_LOG(resultSet->GoToFirstRow() == NativeRdb::E_OK, + handlesResult, "MtpMedialibraryManager have no PHOTO_ALL_DELETE_KEY"); + do { + string file_id = GetStringVal(PhotoColumn::MEDIA_ID, resultSet); + handlesResult.push_back(stoi(file_id)); + } while (resultSet->GoToNextRow()==NativeRdb::E_OK); + return handlesResult; +} + +void MediaSyncObserver::SendPhotoRemoveEvent(std::string suffixString) +{ + vector allDeletedHandles; vector handles; + if (suffixString.empty()) { + allDeletedHandles = GetAllDeleteHandles(); + for (const auto deletehandle: allDeletedHandles) { + SendEventPackets(deletehandle + COMMON_PHOTOS_OFFSET, MTP_EVENT_OBJECT_REMOVED_CODE); + SendEventPackets(deletehandle + EDITED_PHOTOS_OFFSET, MTP_EVENT_OBJECT_REMOVED_CODE); + SendEventPackets(deletehandle + COMMON_MOVING_OFFSET, MTP_EVENT_OBJECT_REMOVED_CODE); + SendEventPackets(deletehandle + EDITED_MOVING_OFFSET, MTP_EVENT_OBJECT_REMOVED_CODE); + handles = GetHandlesFromPhotosInfoBurstKeys(deletehandle); + for (const auto handle : handles) { + SendEventPackets(handle + COMMON_PHOTOS_OFFSET, MTP_EVENT_OBJECT_REMOVED_CODE); + } + } + } else { + SendEventPackets(stoi(suffixString) + COMMON_PHOTOS_OFFSET, MTP_EVENT_OBJECT_REMOVED_CODE); + SendEventPackets(stoi(suffixString) + EDITED_PHOTOS_OFFSET, MTP_EVENT_OBJECT_REMOVED_CODE); + SendEventPackets(stoi(suffixString) + COMMON_MOVING_OFFSET, MTP_EVENT_OBJECT_REMOVED_CODE); + SendEventPackets(stoi(suffixString) + EDITED_MOVING_OFFSET, MTP_EVENT_OBJECT_REMOVED_CODE); + handles = GetHandlesFromPhotosInfoBurstKeys(stoi(suffixString)); + for (const auto handle : handles) { + SendEventPackets(handle+COMMON_PHOTOS_OFFSET, MTP_EVENT_OBJECT_REMOVED_CODE); + } + } +} + +void MediaSyncObserver::SendPhotoEvent(ChangeType changeType, string suffixString) +{ + if (!suffixString.empty() && stoi(suffixString) <= 0) { + return; + } + switch (changeType) { case static_cast(NotifyType::NOTIFY_ADD): MEDIA_DEBUG_LOG("MtpMediaLibrary PHOTO ADD"); @@ -146,14 +204,7 @@ void MediaSyncObserver::SendPhotoEvent(ChangeType changeType, string suffixStrin break; case static_cast(NotifyType::NOTIFY_REMOVE): MEDIA_DEBUG_LOG("MtpMediaLibrary PHOTO REMOVE"); - SendEventPackets(stoi(suffixString) + COMMON_PHOTOS_OFFSET, MTP_EVENT_OBJECT_REMOVED_CODE); - SendEventPackets(stoi(suffixString) + EDITED_PHOTOS_OFFSET, MTP_EVENT_OBJECT_REMOVED_CODE); - SendEventPackets(stoi(suffixString) + COMMON_MOVING_OFFSET, MTP_EVENT_OBJECT_REMOVED_CODE); - SendEventPackets(stoi(suffixString) + EDITED_MOVING_OFFSET, MTP_EVENT_OBJECT_REMOVED_CODE); - handles = GetHandlesFromPhotosInfoBurstKeys(stoi(suffixString)); - for (const auto handle: handles) { - SendEventPackets(handle+COMMON_PHOTOS_OFFSET, MTP_EVENT_OBJECT_REMOVED_CODE); - } + SendPhotoRemoveEvent(suffixString); break; default: break; @@ -230,7 +281,7 @@ void MediaSyncObserver::OnChangeEx(const ChangeInfo &changeInfo) MEDIA_DEBUG_LOG("MtpMediaLibrary uris [%{public}s]", uri.c_str()); if (startsWith(uri, PhotoPrefix)) { std::string suffixString = uri.substr(PhotoPrefix.size()); - if (suffixString.empty()) { + if (suffixString.empty() && changeInfo.changeType_ != static_cast(NotifyType::NOTIFY_REMOVE)) { MEDIA_ERR_LOG("MtpMediaLibrary suffixString is empty"); continue; }