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 #ifndef PHOTO_ACCESS_HELPER_IMPL_H 17 #define PHOTO_ACCESS_HELPER_IMPL_H 18 19 #include <mutex> 20 #include <string> 21 #include <vector> 22 23 #include "ability_context.h" 24 #include "ability_runtime/cj_ability_context.h" 25 #include "abs_shared_result_set.h" 26 #include "cj_lambda.h" 27 #include "data_ability_helper.h" 28 #include "data_ability_observer_stub.h" 29 #include "data_ability_predicates.h" 30 #include "datashare_helper.h" 31 #include "datashare_observer.h" 32 #include "datashare_predicates.h" 33 #include "fetch_result_impl.h" 34 #include "modal_ui_callback.h" 35 #include "photo_accesshelper_utils.h" 36 #include "photo_album_impl.h" 37 #include "photo_asset_impl.h" 38 #include "values_bucket.h" 39 #include "uv.h" 40 41 namespace OHOS { 42 namespace Media { 43 struct FfiMediaChangeListener { 44 MediaType mediaType; 45 OHOS::DataShare::DataShareObserver::ChangeInfo changeInfo; 46 std::string strUri; 47 std::function<void(ChangeData)> callbackRef; 48 }; 49 50 class FfiMediaOnNotifyObserver; 51 class ChangeListener { 52 public: 53 class UvChangeMsg { 54 public: UvChangeMsg(std::function<void (ChangeData)> callbackRef_,DataShare::DataShareObserver::ChangeInfo & changeInfo,std::string strUri)55 UvChangeMsg(std::function<void(ChangeData)> callbackRef_, 56 DataShare::DataShareObserver::ChangeInfo &changeInfo, std::string strUri) 57 { 58 callbackRef = callbackRef_; 59 changeInfo_ = changeInfo; 60 strUri_ = std::move(strUri); 61 } ~UvChangeMsg()62 ~UvChangeMsg() {} 63 std::function<void(ChangeData)> callbackRef; 64 DataShare::DataShareObserver::ChangeInfo changeInfo_; 65 std::string strUri_; 66 uint8_t *data_ {nullptr}; 67 }; 68 ChangeListener()69 explicit ChangeListener() {} 70 ChangeListener(const ChangeListener & listener)71 ChangeListener(const ChangeListener &listener) 72 { 73 funcId = listener.funcId; 74 } 75 76 ChangeListener& operator=(const ChangeListener &listener) 77 { 78 funcId = listener.funcId; 79 return *this; 80 } 81 ~ChangeListener()82 ~ChangeListener() {} 83 84 void OnChange(FfiMediaChangeListener &listener); 85 void UvQueueWork(UvChangeMsg *msg); 86 void SolveOnChange(UvChangeMsg *msg, ChangeData &changeData); 87 int64_t funcId = 0; 88 std::function<void(ChangeData)> callbackRef = nullptr; 89 90 sptr<AAFwk::IDataAbilityObserver> audioDataObserver_ = nullptr; 91 sptr<AAFwk::IDataAbilityObserver> videoDataObserver_ = nullptr; 92 sptr<AAFwk::IDataAbilityObserver> imageDataObserver_ = nullptr; 93 sptr<AAFwk::IDataAbilityObserver> fileDataObserver_ = nullptr; 94 sptr<AAFwk::IDataAbilityObserver> smartAlbumDataObserver_ = nullptr; 95 sptr<AAFwk::IDataAbilityObserver> deviceDataObserver_ = nullptr; 96 sptr<AAFwk::IDataAbilityObserver> remoteFileDataObserver_ = nullptr; 97 sptr<AAFwk::IDataAbilityObserver> albumDataObserver_ = nullptr; 98 std::vector<std::shared_ptr<FfiMediaOnNotifyObserver>> observers_; 99 }; 100 101 class FfiMediaObserver : public AAFwk::DataAbilityObserverStub { 102 public: FfiMediaObserver(const ChangeListener & listObj,MediaType mediaType)103 FfiMediaObserver(const ChangeListener &listObj, MediaType mediaType) : listObj_(listObj) 104 { 105 mediaType_ = mediaType; 106 } 107 108 ~FfiMediaObserver() = default; 109 OnChange()110 void OnChange() override 111 { 112 FfiMediaChangeListener listener; 113 listener.mediaType = mediaType_; 114 listener.callbackRef = listObj_.callbackRef; 115 listObj_.OnChange(listener); 116 } 117 118 ChangeListener listObj_; 119 MediaType mediaType_; 120 }; 121 122 class FfiMediaOnNotifyObserver : public DataShare::DataShareObserver { 123 public: FfiMediaOnNotifyObserver(const ChangeListener & listObj,std::string uri,int64_t funcId_,std::function<void (ChangeData)> callbackRef_)124 FfiMediaOnNotifyObserver(const ChangeListener &listObj, std::string uri, 125 int64_t funcId_, std::function<void(ChangeData)> callbackRef_) : listObj_(listObj) 126 { 127 uri_ = uri; 128 funcId = funcId_; 129 callbackRef = callbackRef_; 130 } 131 132 ~FfiMediaOnNotifyObserver() = default; OnChange(const DataShare::DataShareObserver::ChangeInfo & changeInfo)133 void OnChange(const DataShare::DataShareObserver::ChangeInfo &changeInfo) override 134 { 135 FfiMediaChangeListener listener; 136 listener.changeInfo = changeInfo; 137 listener.strUri = uri_; 138 listener.callbackRef = callbackRef; 139 listObj_.OnChange(listener); 140 } 141 ChangeListener listObj_; 142 std::string uri_; 143 int64_t funcId; 144 std::function<void(ChangeData)> callbackRef; 145 }; 146 147 class PhotoAccessHelperImpl : public OHOS::FFI::FFIData { DECL_TYPE(PhotoAccessHelperImpl,OHOS::FFI::FFIData)148 DECL_TYPE(PhotoAccessHelperImpl, OHOS::FFI::FFIData) 149 public: 150 PhotoAccessHelperImpl() {} 151 bool GetPhotoAccessHelper(int64_t id); 152 std::shared_ptr<FetchResult<FileAsset>> GetAssets(COptions options, int32_t &errCode); 153 std::shared_ptr<FetchResult<FileAsset>> GetBurstAssets(char* cBurstKey, COptions options, int32_t &errCode); 154 void ParseAlbumTypes(int32_t albumType, int32_t albumSubType, 155 DataShare::DataSharePredicates &predicates, std::vector<std::string> &fetchColumn, int32_t &errCode); 156 std::shared_ptr<FetchResult<PhotoAlbum>> GetAlbums(int32_t type, int32_t subtype, 157 COptions options, int32_t &errCode); 158 bool CheckRef(ChangeListener &listObj, bool isOff, const string &uri, int64_t funcId); 159 void RegisterNotifyChange(const std::string &uri, bool isDerived, 160 int64_t funcId, ChangeListener &listObj, int32_t &errCode); 161 void RegisterChange(char* uri, bool forChildUris, int64_t funcId, int32_t &errCode); 162 void UnRegisterChange(const string &type, ChangeListener &listObj); 163 void UnRegisterNotifyChange(const std::string &uri, int64_t funcId, ChangeListener &listObj); 164 void UnRegisterChange(char* uri, int64_t funcId); 165 void ShowAssetsCreationDialog(CArrString &srcFileUris, PhotoCreationConfigs &photoCreationConfigs, 166 int64_t funcId, FfiBundleInfo &cBundleInfo, int32_t &errCode); 167 void Release(); 168 void ParseArgsGetPhotoAlbum(int32_t type, int32_t subtype, COptions options, 169 DataShare::DataSharePredicates &predicates, std::vector<std::string> &fetchColumn, 170 ExtraInfo &extraInfo, int32_t &errCode); 171 static bool CheckWhetherInitSuccess(const sptr<IRemoteObject> &token); 172 static PhotoSelectResult StartPhotoPicker(int64_t id, PhotoSelectOptions &option, int32_t &errCode); 173 174 static std::mutex sUserFileClientMutex_; 175 static int64_t contextId; 176 static std::mutex sOnOffMutex_; 177 178 private: 179 bool hiddenOnly = false; 180 bool isAnalysisAlbum = false; 181 int32_t hiddenAlbumFetchMode = -1; 182 int32_t isLocationAlbum = 0; 183 int32_t isHighlightAlbum = 0; 184 }; 185 } 186 } 187 188 #endif