1 /* 2 * Copyright (C) 2022 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_MEDIALIBRARY_RDBSTORE_H 17 #define OHOS_MEDIALIBRARY_RDBSTORE_H 18 19 #include "medialibrary_unistore.h" 20 #include "timer.h" 21 22 namespace OHOS { 23 namespace Media { 24 class MediaLibraryRdbStoreObserver; 25 class MediaLibraryDataCallBack; 26 27 class MediaLibraryRdbStore final : public MediaLibraryUnistore { 28 public: 29 explicit MediaLibraryRdbStore(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context); 30 virtual ~MediaLibraryRdbStore(); 31 32 virtual void Init() override; 33 virtual void Stop() override; 34 35 virtual int32_t Insert(MediaLibraryCommand &cmd, int64_t &rowId) override; 36 virtual int32_t Delete(MediaLibraryCommand &cmd, int32_t &rowId) override; 37 virtual int32_t Update(MediaLibraryCommand &cmd, int32_t &rowId) override; 38 std::shared_ptr<NativeRdb::AbsSharedResultSet> Query(MediaLibraryCommand &cmd, 39 const std::vector<std::string> &columns) override; 40 41 bool SyncPullAllTableByDeviceId(const std::string &bundleName, std::vector<std::string> &devices) override; 42 bool SyncPullTable(const std::string &bundleName, const std::string &tableName, 43 const std::vector<std::string> &devices, bool isLast = false) override; 44 bool SyncPushTable(const std::string &bundleName, const std::string &tableName, 45 const std::vector<std::string> &devices, bool isLast = false) override; 46 int32_t ExecuteSql(const std::string &sql) override; 47 std::shared_ptr<NativeRdb::AbsSharedResultSet> QuerySql(const std::string &sql) override; 48 49 std::shared_ptr<NativeRdb::RdbStore> GetRaw() const; 50 std::string ObtainTableName(MediaLibraryCommand &cmd) override; 51 52 private: 53 bool SubscribeRdbStoreObserver(); 54 bool UnSubscribeRdbStoreObserver(); 55 56 std::shared_ptr<NativeRdb::RdbStore> rdbStore_; 57 std::shared_ptr<MediaLibraryRdbStoreObserver> rdbStoreObs_; 58 std::string bundleName_{BUNDLE_NAME}; 59 NativeRdb::RdbStoreConfig config_{""}; 60 }; 61 62 class MediaLibraryDataCallBack : public NativeRdb::RdbOpenCallback { 63 public: 64 struct DirValuesBucket { 65 int32_t directoryType; 66 std::string dirValues; 67 std::string typeValues; 68 std::string extensionValues; 69 }; 70 71 struct SmartAlbumValuesBucket { 72 int32_t albumId; 73 std::string albumName; 74 int32_t albumType; 75 }; 76 77 int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override; 78 int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override; 79 bool HasDistributedTables(); 80 81 private: 82 int32_t PrepareDir(NativeRdb::RdbStore &store); 83 int32_t PrepareSmartAlbum(NativeRdb::RdbStore &store); 84 int32_t InsertDirValues(const DirValuesBucket &dirValuesBucket, NativeRdb::RdbStore &store); 85 int32_t InsertSmartAlbumValues(const SmartAlbumValuesBucket &smartAlbum, NativeRdb::RdbStore &store); 86 bool isDistributedTables = false; 87 }; 88 89 class MediaLibraryRdbStoreObserver : public NativeRdb::RdbStore::RdbStoreObserver { 90 public: 91 explicit MediaLibraryRdbStoreObserver(const std::string &bundleName); 92 virtual ~MediaLibraryRdbStoreObserver(); 93 void OnChange(const std::vector<std::string> &devices) override; 94 95 private: 96 void NotifyDeviceChange(); 97 static constexpr int NOTIFY_TIME_INTERVAL = 10000; 98 std::unique_ptr<OHOS::Utils::Timer> timer_; 99 uint32_t timerId_{0}; 100 std::string bundleName_; 101 bool isNotifyDeviceChange_; 102 }; 103 } // namespace Media 104 } // namespace OHOS 105 106 #endif // OHOS_MEDIALIBRARY_RDBSTORE_OPERATIONS_H 107