1 /*
2 * Copyright (C) 2024 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 #define MLOG_TAG "MultiStagesCaptureRequestTaskManager"
17
18 #include "media_log.h"
19 #include "multistages_capture_request_task_manager.h"
20
21 using namespace std;
22
23 namespace OHOS {
24 namespace Media {
25 std::unordered_map<int32_t, std::string> MultiStagesCaptureRequestTaskManager::fileId2PhotoId_ = {};
26 std::unordered_map<std::string, std::shared_ptr<LowQualityPhotoInfo>>
27 MultiStagesCaptureRequestTaskManager::photoIdInProcess_ = {};
28 std::mutex MultiStagesCaptureRequestTaskManager::mutex_;
29
AddPhotoInProgress(int32_t fileId,const string & photoId,bool isTrashed)30 void MultiStagesCaptureRequestTaskManager::AddPhotoInProgress(int32_t fileId, const string &photoId, bool isTrashed)
31 {
32 unique_lock<mutex> lock(mutex_, try_to_lock);
33 fileId2PhotoId_.emplace(fileId, photoId);
34 PhotoState state = PhotoState::NORMAL;
35 if (isTrashed) {
36 state = PhotoState::TRASHED;
37 }
38 photoIdInProcess_.emplace(photoId, make_shared<LowQualityPhotoInfo>(fileId, state, 0));
39 }
40
41 // 1. RestoreImage,从回收站恢复,isTrashed=false, state TRASHED => NORMAL
42 // 2. 删除到回收站,isTrashed=false, state NORMAL => TRASHED
UpdatePhotoInProgress(const string & photoId)43 void MultiStagesCaptureRequestTaskManager::UpdatePhotoInProgress(const string &photoId)
44 {
45 unique_lock<mutex> lock(mutex_, try_to_lock);
46 if (photoIdInProcess_.count(photoId) == 0) {
47 MEDIA_INFO_LOG("photo id (%{public}s) not in progress", photoId.c_str());
48 return;
49 }
50 shared_ptr<LowQualityPhotoInfo> photo = photoIdInProcess_.at(photoId);
51 photo->state = (photo->state == PhotoState::NORMAL) ? PhotoState::TRASHED : PhotoState::NORMAL;
52 photoIdInProcess_[photoId] = photo;
53 }
54
RemovePhotoInProgress(const string & photoId,bool isRestorable)55 void MultiStagesCaptureRequestTaskManager::RemovePhotoInProgress(const string &photoId, bool isRestorable)
56 {
57 if (!isRestorable) {
58 unique_lock<mutex> lock(mutex_, try_to_lock);
59 if (photoIdInProcess_.count(photoId) == 0) {
60 MEDIA_INFO_LOG("photo id (%{public}s) not in progress.", photoId.c_str());
61 return;
62 }
63 int32_t fileId = photoIdInProcess_.at(photoId)->fileId;
64 fileId2PhotoId_.erase(fileId);
65 photoIdInProcess_.erase(photoId);
66 return;
67 }
68
69 UpdatePhotoInProgress(photoId);
70 }
71
UpdatePhotoInProcessRequestCount(const std::string & photoId,RequestType requestType)72 int32_t MultiStagesCaptureRequestTaskManager::UpdatePhotoInProcessRequestCount(const std::string &photoId,
73 RequestType requestType)
74 {
75 unique_lock<mutex> lock(mutex_, try_to_lock);
76 if (photoIdInProcess_.count(photoId) == 0) {
77 MEDIA_INFO_LOG("photo id (%{public}s) not in progress.", photoId.c_str());
78 return 0;
79 }
80
81 shared_ptr<LowQualityPhotoInfo> photo = photoIdInProcess_.at(photoId);
82 photo->requestCount += (int32_t) requestType;
83 photoIdInProcess_[photoId] = photo;
84 return photo->requestCount;
85 }
86
IsPhotoInProcess(const string & photoId)87 bool MultiStagesCaptureRequestTaskManager::IsPhotoInProcess(const string &photoId)
88 {
89 unique_lock<mutex> lock(mutex_, try_to_lock);
90 if (photoId.empty() || photoIdInProcess_.find(photoId) == photoIdInProcess_.end()) {
91 return false;
92 }
93 return true;
94 }
95
GetProcessingPhotoId(int32_t fileId)96 std::string MultiStagesCaptureRequestTaskManager::GetProcessingPhotoId(int32_t fileId)
97 {
98 unique_lock<mutex> lock(mutex_, try_to_lock);
99 if (fileId2PhotoId_.find(fileId) == fileId2PhotoId_.end()) {
100 MEDIA_ERR_LOG("photo not in process, id=%{public}d", fileId);
101 return "";
102 }
103 return fileId2PhotoId_[fileId];
104 }
105
106 } // Media
107 } // OHOS