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 FRAMEWORKS_ANI_SRC_INCLUDE_PHOTO_ACCESS_HELPER_ANI_H 17 #define FRAMEWORKS_ANI_SRC_INCLUDE_PHOTO_ACCESS_HELPER_ANI_H 18 19 #include <ani.h> 20 #include "ani_error.h" 21 #include "fetch_result_ani.h" 22 #include "photo_album_ani.h" 23 #include "medialibrary_ani_utils.h" 24 #include "smart_album_asset.h" 25 26 namespace OHOS { 27 namespace Media { 28 29 class ChangeListenerAni { 30 public: ChangeListenerAni(ani_env * env)31 explicit ChangeListenerAni(ani_env *env) : env_(env) {} 32 33 private: 34 [[maybe_unused]] ani_env *env_ = nullptr; 35 }; 36 37 class ThumbnailBatchGenerateObserver : public DataShare::DataShareObserver { 38 public: 39 ThumbnailBatchGenerateObserver() = default; 40 ~ThumbnailBatchGenerateObserver() = default; 41 42 void OnChange(const ChangeInfo &changeInfo) override; 43 }; 44 45 using ThreadFunciton = std::function<void(ani_env*, ani_object, void*, void*)>; 46 class ThumbnailGenerateHandler { 47 public: ThumbnailGenerateHandler(ani_object ref,ThreadFunciton func)48 ThumbnailGenerateHandler(ani_object ref, ThreadFunciton func) : callbackRef_(ref), threadSafeFunc_(func) {} 49 ~ThumbnailGenerateHandler() = default; 50 51 ani_object callbackRef_; 52 ThreadFunciton threadSafeFunc_; 53 }; 54 55 class MediaLibraryAni { 56 public: 57 static ani_status PhotoAccessHelperInit(ani_env *env); 58 static ani_object Constructor([[maybe_unused]] ani_env *env, ani_object context); 59 static MediaLibraryAni* Unwrap(ani_env *env, ani_object object); 60 61 static ani_object GetPhotoAlbums(ani_env *env, ani_object object, ani_enum_item albumTypeItem, 62 ani_enum_item albumSubtypeItem, ani_object fetchOptions); 63 static ani_status Release(ani_env *env, ani_object object); 64 static ani_status ApplyChanges(ani_env *env, ani_object object); 65 static ani_object createAsset1([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object obj, 66 [[maybe_unused]] ani_string stringObj); 67 static ani_object GetAssetsSync([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object, 68 ani_object options); 69 static ani_object GetFileAssetsInfo([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object, 70 ani_object options); 71 static ani_object GetAssetsInner([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object, 72 ani_object options); 73 static std::mutex sUserFileClientMutex_; 74 static void PhotoAccessStopCreateThumbnailTask([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object, 75 ani_double taskId); 76 static ani_int PhotoAccessStartCreateThumbnailTask([[maybe_unused]] ani_env *env, 77 [[maybe_unused]] ani_object object, ani_object predicate); 78 static void OnThumbnailGenerated(ani_env *env, ani_object callback, void *context, void *data); 79 80 private: 81 ani_env *env_; 82 }; 83 84 struct PickerCallBack { 85 bool ready = false; 86 bool isOrigin; 87 int32_t resultCode; 88 vector<string> uris; 89 }; 90 91 constexpr int32_t DEFAULT_PRIVATEALBUMTYPE = 3; 92 93 struct MediaLibraryAsyncContext : public AniError { 94 bool status; 95 bool isDelete; 96 bool isCreateByComponent; 97 bool isCreateByAgent; 98 AniAssetType assetType; 99 AlbumType albumType; 100 MediaLibraryAni *objectInfo; 101 std::string selection; 102 std::vector<std::string> selectionArgs; 103 std::string order; 104 std::string uri; 105 std::vector<std::string> uriArray; 106 std::string networkId; 107 std::string extendArgs; 108 std::string analysisProgress; 109 int32_t analysisType = AnalysisType::ANALYSIS_INVALID; 110 std::unique_ptr<FetchResult<FileAsset>> fetchFileResult; 111 std::unique_ptr<FetchResult<AlbumAsset>> fetchAlbumResult; 112 std::unique_ptr<FetchResult<PhotoAlbum>> fetchPhotoAlbumResult; 113 std::unique_ptr<FetchResult<SmartAlbumAsset>> fetchSmartAlbumResult; 114 std::unique_ptr<FileAsset> fileAsset; 115 std::unique_ptr<PhotoAlbum> photoAlbumData; 116 std::unique_ptr<SmartAlbumAsset> smartAlbumData; 117 OHOS::DataShare::DataShareValuesBucket valuesBucket; 118 std::vector<OHOS::DataShare::DataShareValuesBucket> valuesBucketArray; 119 unsigned int dirType = 0; 120 int32_t privateAlbumType = DEFAULT_PRIVATEALBUMTYPE; 121 int32_t retVal; 122 std::string directoryRelativePath; 123 std::vector<std::unique_ptr<AlbumAsset>> albumNativeArray; 124 std::vector<std::unique_ptr<SmartAlbumAsset>> smartAlbumNativeArray; 125 std::vector<std::unique_ptr<SmartAlbumAsset>> privateSmartAlbumNativeArray; 126 std::string storeMediaSrc; 127 int32_t imagePreviewIndex; 128 int32_t parentSmartAlbumId = 0; 129 int32_t smartAlbumId = -1; 130 int32_t isLocationAlbum = 0; 131 int32_t isHighlightAlbum = 0; 132 ResultNapiType resultNapiType; 133 std::string tableName; 134 std::vector<uint32_t> mediaTypes; 135 std::vector<string> mediaTypeNames; 136 int32_t photoType; 137 OHOS::DataShare::DataSharePredicates predicates; 138 std::vector<std::string> fetchColumn; 139 std::vector<std::string> uris; 140 bool isForce = false; 141 bool hiddenOnly = false; 142 bool isAnalysisAlbum = false; 143 int32_t hiddenAlbumFetchMode = -1; 144 std::string formId; 145 std::string indexProgress; 146 std::shared_ptr<PickerCallBack> pickerCallBack; 147 std::vector<std::string> analysisDatas; 148 ani_object callback; 149 }; 150 } // namespace Media 151 } // namespace OHOS 152 153 #endif // FRAMEWORKS_ANI_SRC_INCLUDE_PHOTO_ACCESS_HELPER_ANI_H 154