• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define MLOG_TAG "CustomRestore"
17 
18 #include "media_library_custom_restore.h"
19 
20 #include "datashare_helper.h"
21 #include "iservice_registry.h"
22 #include "media_log.h"
23 #include "medialibrary_custom_restore_observer_manager.h"
24 #include "medialibrary_errno.h"
25 #include "system_ability_definition.h"
26 #include "userfilemgr_uri.h"
27 
28 namespace OHOS {
29 namespace Media {
30 using ChangeInfo = DataShare::DataShareObserver::ChangeInfo;
31 const std::string CustomRestore::NOTIFY_URI_PREFIX = "file://media/custom_restore/";
32 const std::string ALBUM_PATH_PREFIX = "/Pictures/";
33 
CustomRestore(string keyPath,bool isDeduplication)34 CustomRestore::CustomRestore(string keyPath, bool isDeduplication)
35 {
36     albumLpath_ = ALBUM_PATH_PREFIX + keyPath;
37     keyPath_ = keyPath;
38     isDeduplication_ = isDeduplication;
39 }
40 
CustomRestore(string albumLpath,string keyPath,bool isDeduplication)41 CustomRestore::CustomRestore(string albumLpath, string keyPath, bool isDeduplication)
42 {
43     albumLpath_ = albumLpath;
44     keyPath_ = keyPath;
45     isDeduplication_ = isDeduplication;
46 }
47 
Init(string bundleName,string appName,string appId,int32_t tokenId)48 void CustomRestore::Init(string bundleName, string appName, string appId, int32_t tokenId)
49 {
50     MEDIA_DEBUG_LOG("CustomRestore init");
51     bundleName_ = bundleName;
52     appName_ = appName;
53     appId_ = appId;
54     tokenId_ = tokenId;
55     InitDataShareHelper();
56 }
57 
InitDataShareHelper()58 void CustomRestore::InitDataShareHelper()
59 {
60     auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
61     if (saManager == nullptr) {
62         MEDIA_ERR_LOG("get system ability mgr failed.");
63         return;
64     }
65     auto remoteObj = saManager->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
66     if (remoteObj == nullptr) {
67         MEDIA_ERR_LOG("GetSystemAbility Service failed.");
68         return;
69     }
70     if (sDataShareHelper_ == nullptr && remoteObj != nullptr) {
71         sDataShareHelper_ = DataShare::DataShareHelper::Creator(remoteObj, MEDIALIBRARY_DATA_URI);
72     }
73 }
74 
Restore()75 int32_t CustomRestore::Restore()
76 {
77     MEDIA_DEBUG_LOG("CustomRestore Restore");
78     if (sDataShareHelper_ == nullptr) {
79         MEDIA_ERR_LOG("sDataShareHelper_ is null.");
80         return E_DATASHARE_IS_NULL;
81     }
82     CHECK_AND_RETURN_RET_LOG(!albumLpath_.empty(), E_INVALID_VALUES, "albumLpath is empty.");
83     CHECK_AND_RETURN_RET_LOG(!keyPath_.empty(), E_INVALID_VALUES, "keyPath is empty.");
84     CHECK_AND_RETURN_RET_LOG(!bundleName_.empty(), E_INVALID_VALUES, "bundleName is empty.");
85     CHECK_AND_RETURN_RET_LOG(!appName_.empty(), E_INVALID_VALUES, "appName is empty.");
86 
87     Uri customRestoreUri(PAH_CUSTOM_RESTORE);
88     DataShareValuesBucket valuesBucket;
89     valuesBucket.Put("albumLpath", albumLpath_);
90     valuesBucket.Put("keyPath", keyPath_);
91     std::string isDeduplicationStr = isDeduplication_ ? "true" : "false";
92     valuesBucket.Put("isDeduplication", isDeduplicationStr);
93     valuesBucket.Put("bundleName", bundleName_);
94     valuesBucket.Put("appName", appName_);
95     valuesBucket.Put("appId", appId_);
96     int32_t result = sDataShareHelper_->Insert(customRestoreUri, valuesBucket);
97     MEDIA_DEBUG_LOG("CustomRestore Restore end. %{public}d", result);
98     return result;
99 }
100 
StopRestore()101 int32_t CustomRestore::StopRestore()
102 {
103     MEDIA_DEBUG_LOG("CustomRestore StopRestore");
104     CHECK_AND_RETURN_RET_LOG(sDataShareHelper_ != nullptr, E_DATASHARE_IS_NULL, "sDataShareHelper_ is null.");
105     Uri cancelUri(PAH_CUSTOM_RESTORE_CANCEL);
106     DataShareValuesBucket valuesBucket;
107     valuesBucket.Put("keyPath", keyPath_);
108     int32_t result = sDataShareHelper_->Insert(cancelUri, valuesBucket);
109     MEDIA_DEBUG_LOG("CustomRestore StopRestore end. %{public}d", result);
110     return result;
111 }
112 
RegisterCustomRestoreCallback(std::shared_ptr<CustomRestoreCallback> callback)113 int32_t CustomRestore::RegisterCustomRestoreCallback(std::shared_ptr<CustomRestoreCallback> callback)
114 {
115     MEDIA_DEBUG_LOG("CustomRestore RegisterCallback");
116     if (callback == nullptr) {
117         MEDIA_ERR_LOG("CustomRestore RegisterCallback callback is null.");
118         return E_CALLBACK_IS_NULL;
119     }
120     if (sDataShareHelper_ == nullptr) {
121         MEDIA_ERR_LOG("CustomRestore RegisterCallback sDataShareHelper_ is null.");
122         return E_DATASHARE_IS_NULL;
123     }
124     std::shared_ptr<CustomRestoreNotifyObserver> notifyObserver =
125             std::make_shared<CustomRestoreNotifyObserver>(callback);
126     CHECK_AND_RETURN_RET_LOG(notifyObserver != nullptr, E_OBSERVER_IS_NULL,
127         "CustomRestore RegisterCallback notifyObserver is null.");
128 
129     Uri customRestoreUri(NOTIFY_URI_PREFIX + keyPath_);
130     sDataShareHelper_->RegisterObserverExt(customRestoreUri, notifyObserver, true);
131     CustomRestoreObserverManager::GetInstance().AttachObserver(callback, notifyObserver);
132     return E_OK;
133 }
134 
UnregisterCustomRestoreCallback(std::shared_ptr<CustomRestoreCallback> callback)135 int32_t CustomRestore::UnregisterCustomRestoreCallback(std::shared_ptr<CustomRestoreCallback> callback)
136 {
137     MEDIA_DEBUG_LOG("CustomRestore UnRegisterCallback");
138     if (callback == nullptr) {
139         MEDIA_ERR_LOG("CustomRestore UnRegisterCallback callback is null.");
140         return E_CALLBACK_IS_NULL;
141     }
142 
143     CHECK_AND_RETURN_RET_LOG(sDataShareHelper_ != nullptr, E_DATASHARE_IS_NULL,
144         "CustomRestore UnregisterCallback sDataShareHelper_ is null.");
145     std::shared_ptr<CustomRestoreNotifyObserver> notifyObserver =
146         CustomRestoreObserverManager::GetInstance().QueryObserver(callback);
147     CHECK_AND_RETURN_RET_LOG(notifyObserver != nullptr, E_OBSERVER_IS_NULL,
148         "CustomRestore UnRegisterCallback notifyObserver is null.");
149 
150     Uri customRestoreUri(NOTIFY_URI_PREFIX + keyPath_);
151     sDataShareHelper_->UnregisterObserverExt(customRestoreUri, notifyObserver);
152     CustomRestoreObserverManager::GetInstance().DetachObserver(callback);
153     return E_OK;
154 }
155 
156 }  // namespace Media
157 }  // namespace OHOS