• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include "bundle_resource_rdb.h"
17 
18 #include "app_log_wrapper.h"
19 #include "bundle_resource_constants.h"
20 #include "bundle_util.h"
21 #include "scope_guard.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace {
26 constexpr const char* SYSTEM_RESOURCES_APP = "ohos.global.systemres";
27 constexpr const char* TASK_NAME = "BackUpResourceDbTask";
28 constexpr uint64_t DELAY_TIME_MILLI_SECONDS = 8 * 60 * 1000; // 8min
29 }
30 
BundleResourceRdb()31 BundleResourceRdb::BundleResourceRdb()
32 {
33     APP_LOGI_NOFUNC("BundleResourceRdb create");
34     BmsRdbConfig bmsRdbConfig;
35     bmsRdbConfig.dbName = BundleResourceConstants::BUNDLE_RESOURCE_RDB_NAME;
36     bmsRdbConfig.dbPath = BundleResourceConstants::BUNDLE_RESOURCE_RDB_PATH;
37     bmsRdbConfig.tableName = BundleResourceConstants::BUNDLE_RESOURCE_RDB_TABLE_NAME;
38     bmsRdbConfig.createTableSql = std::string(
39         "CREATE TABLE IF NOT EXISTS "
40         + std::string(BundleResourceConstants::BUNDLE_RESOURCE_RDB_TABLE_NAME)
41         + "(NAME TEXT NOT NULL, UPDATE_TIME INTEGER, LABEL TEXT, ICON TEXT, "
42         + "SYSTEM_STATE TEXT NOT NULL, PRIMARY KEY (NAME));");
43     bmsRdbConfig.insertColumnSql.push_back(std::string("ALTER TABLE " +
44         std::string(BundleResourceConstants::BUNDLE_RESOURCE_RDB_TABLE_NAME) +
45         " ADD FOREGROUND BLOB;"));
46     bmsRdbConfig.insertColumnSql.push_back(std::string("ALTER TABLE " +
47         std::string(BundleResourceConstants::BUNDLE_RESOURCE_RDB_TABLE_NAME) +
48         " ADD BACKGROUND BLOB;"));
49     rdbDataManager_ = std::make_shared<RdbDataManager>(bmsRdbConfig);
50     rdbDataManager_->CreateTable();
51 
52     delayedTaskMgr_ = std::make_shared<SingleDelayedTaskMgr>(TASK_NAME, DELAY_TIME_MILLI_SECONDS);
53 }
54 
~BundleResourceRdb()55 BundleResourceRdb::~BundleResourceRdb()
56 {
57 }
58 
AddResourceInfo(const ResourceInfo & resourceInfo)59 bool BundleResourceRdb::AddResourceInfo(const ResourceInfo &resourceInfo)
60 {
61     if (resourceInfo.bundleName_.empty()) {
62         APP_LOGE("failed, bundleName is empty");
63         return false;
64     }
65     APP_LOGD("insert resource key:%{public}s", resourceInfo.GetKey().c_str());
66     int64_t timeStamp = BundleUtil::GetCurrentTimeMs();
67     NativeRdb::ValuesBucket valuesBucket;
68     valuesBucket.PutString(BundleResourceConstants::NAME, resourceInfo.GetKey());
69     valuesBucket.PutLong(BundleResourceConstants::UPDATE_TIME, timeStamp);
70     valuesBucket.PutString(BundleResourceConstants::LABEL, resourceInfo.label_);
71     valuesBucket.PutString(BundleResourceConstants::ICON, resourceInfo.icon_);
72     valuesBucket.PutString(BundleResourceConstants::SYSTEM_STATE, BundleSystemState::GetInstance().ToString());
73     // used for layered icons
74     valuesBucket.PutBlob(BundleResourceConstants::FOREGROUND, resourceInfo.foreground_);
75     valuesBucket.PutBlob(BundleResourceConstants::BACKGROUND, resourceInfo.background_);
76     APP_LOGD("key:%{public}s foreground: %{public}zu, background: %{public}zu", resourceInfo.GetKey().c_str(),
77         resourceInfo.foreground_.size(), resourceInfo.background_.size());
78 
79     return rdbDataManager_->InsertData(valuesBucket);
80 }
81 
AddResourceInfos(const std::vector<ResourceInfo> & resourceInfos)82 bool BundleResourceRdb::AddResourceInfos(const std::vector<ResourceInfo> &resourceInfos)
83 {
84     if (resourceInfos.empty()) {
85         APP_LOGE("failed, resourceInfos is empty");
86         return false;
87     }
88     int64_t timeStamp = BundleUtil::GetCurrentTimeMs();
89     bool ret = true;
90     std::vector<NativeRdb::ValuesBucket> valuesBuckets;
91     for (const auto &info : resourceInfos) {
92         if (info.bundleName_.empty()) {
93             APP_LOGE("failed, bundleName is empty");
94             ret = false;
95             continue;
96         }
97         NativeRdb::ValuesBucket valuesBucket;
98         valuesBucket.PutString(BundleResourceConstants::NAME, info.GetKey());
99         valuesBucket.PutLong(BundleResourceConstants::UPDATE_TIME, timeStamp);
100         valuesBucket.PutString(BundleResourceConstants::SYSTEM_STATE, BundleSystemState::GetInstance().ToString());
101 
102         if (!info.label_.empty()) {
103             valuesBucket.PutString(BundleResourceConstants::LABEL, info.label_);
104         } else {
105             valuesBucket.PutString(BundleResourceConstants::LABEL, resourceInfos[0].label_);
106         }
107 
108         if (!info.icon_.empty()) {
109             valuesBucket.PutString(BundleResourceConstants::ICON, info.icon_);
110             // used for layered icons
111             valuesBucket.PutBlob(BundleResourceConstants::FOREGROUND, info.foreground_);
112             valuesBucket.PutBlob(BundleResourceConstants::BACKGROUND, info.background_);
113         } else {
114             valuesBucket.PutString(BundleResourceConstants::ICON, resourceInfos[0].icon_);
115             // used for layered icons
116             valuesBucket.PutBlob(BundleResourceConstants::FOREGROUND, resourceInfos[0].foreground_);
117             valuesBucket.PutBlob(BundleResourceConstants::BACKGROUND, resourceInfos[0].background_);
118         }
119         APP_LOGD("key:%{public}s foreground: %{public}zu, background: %{public}zu", info.GetKey().c_str(),
120             info.foreground_.size(), info.background_.size());
121         valuesBuckets.emplace_back(valuesBucket);
122     }
123     int64_t insertNum = 0;
124     bool insertRet = rdbDataManager_->BatchInsert(insertNum, valuesBuckets);
125     if (!insertRet) {
126         APP_LOGE("BatchInsert failed");
127         return false;
128     }
129     if (valuesBuckets.size() != static_cast<uint64_t>(insertNum)) {
130         APP_LOGE("BatchInsert size not expected");
131         return false;
132     }
133     BackupRdb();
134     return ret;
135 }
136 
DeleteResourceInfo(const std::string & key)137 bool BundleResourceRdb::DeleteResourceInfo(const std::string &key)
138 {
139     if (key.empty()) {
140         APP_LOGE("failed, key is empty");
141         return false;
142     }
143     APP_LOGD("need delete resource info, key: %{public}s", key.c_str());
144     /**
145      * begin with bundle name, like:
146      * 1. bundleName
147      * 2. bundleName/moduleName/abilityName
148      */
149     if (key.find(ServiceConstants::PATH_SEPARATOR) == std::string::npos) {
150         NativeRdb::AbsRdbPredicates absRdbPredicates(BundleResourceConstants::BUNDLE_RESOURCE_RDB_TABLE_NAME);
151         // need delete both bundle resource and launcher ability resource
152         absRdbPredicates.BeginsWith(BundleResourceConstants::NAME, key + ServiceConstants::PATH_SEPARATOR);
153         if (!rdbDataManager_->DeleteData(absRdbPredicates)) {
154             APP_LOGW("delete key:%{public}s failed", key.c_str());
155         }
156     }
157     NativeRdb::AbsRdbPredicates absRdbPredicates(BundleResourceConstants::BUNDLE_RESOURCE_RDB_TABLE_NAME);
158     absRdbPredicates.EqualTo(BundleResourceConstants::NAME, key);
159     return rdbDataManager_->DeleteData(absRdbPredicates);
160 }
161 
GetAllResourceName(std::vector<std::string> & keyNames)162 bool BundleResourceRdb::GetAllResourceName(std::vector<std::string> &keyNames)
163 {
164     NativeRdb::AbsRdbPredicates absRdbPredicates(BundleResourceConstants::BUNDLE_RESOURCE_RDB_TABLE_NAME);
165     std::string systemState = BundleSystemState::GetInstance().ToString();
166     APP_LOGI("start get all resource name:%{public}s", systemState.c_str());
167     auto absSharedResultSet = rdbDataManager_->QueryByStep(absRdbPredicates);
168     if (absSharedResultSet == nullptr) {
169         APP_LOGE("QueryByStep failed, systemState:%{public}s", systemState.c_str());
170         return false;
171     }
172     ScopeGuard stateGuard([absSharedResultSet] { absSharedResultSet->Close(); });
173 
174     auto ret = absSharedResultSet->GoToFirstRow();
175     if (ret != NativeRdb::E_OK) {
176         APP_LOGE("GoToFirstRow failed, ret %{public}d, systemState %{public}s", ret, systemState.c_str());
177         return false;
178     }
179     do {
180         std::string name;
181         ret = absSharedResultSet->GetString(BundleResourceConstants::INDEX_NAME, name);
182         if (ret != NativeRdb::E_OK) {
183             APP_LOGE("GetString name failed, ret %{public}d, systemState %{public}s", ret, systemState.c_str());
184             return false;
185         }
186         if (name.find("/") != std::string::npos) {
187             continue;
188         }
189         // icon is invalid, need add again
190         std::vector<uint8_t> foreground;
191         ret = absSharedResultSet->GetBlob(BundleResourceConstants::INDEX_FOREGROUND, foreground);
192         if (ret != NativeRdb::E_OK) {
193             APP_LOGE("GetString foreground failed, ret %{public}d, systemState %{public}s", ret, systemState.c_str());
194             return false;
195         }
196         if (foreground.empty()) {
197             APP_LOGW("keyName %{public}s foreground invalid", name.c_str());
198             continue;
199         }
200         // label is invalid, need add again
201         std::string label;
202         ret = absSharedResultSet->GetString(BundleResourceConstants::INDEX_LABEL, label);
203         if (ret != NativeRdb::E_OK) {
204             APP_LOGE("GetString label failed, ret %{public}d, systemState:%{public}s", ret, systemState.c_str());
205             return false;
206         }
207         if ((label.find('$') == 0) || (label == name) || label.empty()) {
208             APP_LOGW("keyName %{public}s label invalid", name.c_str());
209             continue;
210         }
211         keyNames.push_back(name);
212     } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
213     return true;
214 }
215 
GetResourceNameByBundleName(const std::string & bundleName,const int32_t appIndex,std::vector<std::string> & keyName)216 bool BundleResourceRdb::GetResourceNameByBundleName(
217     const std::string &bundleName,
218     const int32_t appIndex,
219     std::vector<std::string> &keyName)
220 {
221     APP_LOGI_NOFUNC("GetResourceName -n %{public}s -i %{public}d", bundleName.c_str(), appIndex);
222     if (bundleName.empty()) {
223         APP_LOGE("bundleName is empty");
224         return false;
225     }
226     ResourceInfo resourceInfo;
227     resourceInfo.bundleName_ = bundleName;
228     resourceInfo.appIndex_ = appIndex;
229     NativeRdb::AbsRdbPredicates absRdbPredicates(BundleResourceConstants::BUNDLE_RESOURCE_RDB_TABLE_NAME);
230     absRdbPredicates.BeginsWith(BundleResourceConstants::NAME, resourceInfo.GetKey() +
231         BundleResourceConstants::SEPARATOR);
232     std::string systemState = BundleSystemState::GetInstance().ToString();
233 
234     auto absSharedResultSet = rdbDataManager_->QueryByStep(absRdbPredicates);
235     if (absSharedResultSet == nullptr) {
236         APP_LOGE("bundleName:%{public}s failed due rdb QueryByStep failed, systemState:%{public}s",
237             bundleName.c_str(), systemState.c_str());
238         return false;
239     }
240     ScopeGuard stateGuard([absSharedResultSet] { absSharedResultSet->Close(); });
241     auto ret = absSharedResultSet->GoToFirstRow();
242     if (ret != NativeRdb::E_OK) {
243         APP_LOGD("bundleName:%{public}s not exist, ret: %{public}d, systemState:%{public}s",
244             bundleName.c_str(), ret, systemState.c_str());
245         return false;
246     }
247 
248     do {
249         std::string key;
250         auto ret = absSharedResultSet->GetString(BundleResourceConstants::INDEX_NAME, key);
251         CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString name failed, ret: %{public}d");
252         keyName.emplace_back(key);
253     } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
254     return true;
255 }
256 
DeleteAllResourceInfo()257 bool BundleResourceRdb::DeleteAllResourceInfo()
258 {
259     NativeRdb::AbsRdbPredicates absRdbPredicates(BundleResourceConstants::BUNDLE_RESOURCE_RDB_TABLE_NAME);
260     // delete all resource info
261     return rdbDataManager_->DeleteData(absRdbPredicates);
262 }
263 
GetBundleResourceInfo(const std::string & bundleName,const uint32_t flags,BundleResourceInfo & bundleResourceInfo,int32_t appIndex)264 bool BundleResourceRdb::GetBundleResourceInfo(
265     const std::string &bundleName,
266     const uint32_t flags,
267     BundleResourceInfo &bundleResourceInfo,
268     int32_t appIndex)
269 {
270     APP_LOGI_NOFUNC("rdb -n %{public}s -i %{public}d", bundleName.c_str(), appIndex);
271     if (bundleName.empty()) {
272         APP_LOGE("bundleName is empty");
273         return false;
274     }
275     NativeRdb::AbsRdbPredicates absRdbPredicates(BundleResourceConstants::BUNDLE_RESOURCE_RDB_TABLE_NAME);
276     ResourceInfo resourceInfo;
277     resourceInfo.bundleName_ = bundleName;
278     resourceInfo.appIndex_ = appIndex;
279     absRdbPredicates.EqualTo(BundleResourceConstants::NAME, resourceInfo.GetKey());
280     std::string systemState = BundleSystemState::GetInstance().ToString();
281 
282     auto absSharedResultSet = rdbDataManager_->QueryByStep(absRdbPredicates);
283     if (absSharedResultSet == nullptr) {
284         APP_LOGE("rdb QueryByStep failed, bundleName %{public}s appIndex %{public}d, %{public}s",
285             bundleName.c_str(), appIndex, systemState.c_str());
286         return false;
287     }
288 
289     ScopeGuard stateGuard([absSharedResultSet] { absSharedResultSet->Close(); });
290     auto ret = absSharedResultSet->GoToFirstRow();
291     if (ret != NativeRdb::E_OK) {
292         APP_LOGE_NOFUNC("rdb GetBundleResourceInfo -n %{public}s failed:%{public}d systemState:%{public}s",
293             bundleName.c_str(), ret, systemState.c_str());
294         return false;
295     }
296     APP_LOGI_NOFUNC("rdb end");
297     return ConvertToBundleResourceInfo(absSharedResultSet, flags, bundleResourceInfo);
298 }
299 
GetLauncherAbilityResourceInfo(const std::string & bundleName,const uint32_t flags,std::vector<LauncherAbilityResourceInfo> & launcherAbilityResourceInfos,const int32_t appIndex)300 bool BundleResourceRdb::GetLauncherAbilityResourceInfo(
301     const std::string &bundleName,
302     const uint32_t flags,
303     std::vector<LauncherAbilityResourceInfo> &launcherAbilityResourceInfos,
304     const int32_t appIndex)
305 {
306     APP_LOGI_NOFUNC("rdb GetLauncherAbilityResourceInfo -n %{public}s -i %{public}d", bundleName.c_str(), appIndex);
307     if (bundleName.empty()) {
308         APP_LOGE("bundleName is empty");
309         return false;
310     }
311     ResourceInfo resourceInfo;
312     resourceInfo.bundleName_ = bundleName;
313     resourceInfo.appIndex_ = appIndex;
314     NativeRdb::AbsRdbPredicates absRdbPredicates(BundleResourceConstants::BUNDLE_RESOURCE_RDB_TABLE_NAME);
315     absRdbPredicates.BeginsWith(BundleResourceConstants::NAME, resourceInfo.GetKey() +
316         BundleResourceConstants::SEPARATOR);
317     std::string systemState = BundleSystemState::GetInstance().ToString();
318 
319     auto absSharedResultSet = rdbDataManager_->QueryByStep(absRdbPredicates);
320     if (absSharedResultSet == nullptr) {
321         APP_LOGE("QueryByStep failed bundleName %{public}s failed, systemState %{public}s",
322             bundleName.c_str(), systemState.c_str());
323         return false;
324     }
325     ScopeGuard stateGuard([absSharedResultSet] { absSharedResultSet->Close(); });
326     auto ret = absSharedResultSet->GoToFirstRow();
327     if (ret != NativeRdb::E_OK) {
328         APP_LOGE("bundleName %{public}s GoToFirstRow failed, ret %{public}d, systemState:%{public}s",
329             bundleName.c_str(), ret, systemState.c_str());
330         return false;
331     }
332 
333     do {
334         LauncherAbilityResourceInfo resourceInfo;
335         if (ConvertToLauncherAbilityResourceInfo(absSharedResultSet, flags, resourceInfo)) {
336             launcherAbilityResourceInfos.push_back(resourceInfo);
337         }
338     } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
339 
340     if ((flags & static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_SORTED_BY_LABEL)) ==
341         static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_SORTED_BY_LABEL)) {
342         APP_LOGD("need sort by label");
343         std::sort(launcherAbilityResourceInfos.begin(), launcherAbilityResourceInfos.end(),
344             [](LauncherAbilityResourceInfo &resourceA, LauncherAbilityResourceInfo &resourceB) {
345                 return resourceA.label < resourceB.label;
346             });
347     }
348     return !launcherAbilityResourceInfos.empty();
349 }
350 
GetAllBundleResourceInfo(const uint32_t flags,std::vector<BundleResourceInfo> & bundleResourceInfos)351 bool BundleResourceRdb::GetAllBundleResourceInfo(const uint32_t flags,
352     std::vector<BundleResourceInfo> &bundleResourceInfos)
353 {
354     APP_LOGI("start get all bundle resource");
355     NativeRdb::AbsRdbPredicates absRdbPredicates(BundleResourceConstants::BUNDLE_RESOURCE_RDB_TABLE_NAME);
356     std::string systemState = BundleSystemState::GetInstance().ToString();
357 
358     auto absSharedResultSet = rdbDataManager_->QueryByStep(absRdbPredicates);
359     if (absSharedResultSet == nullptr) {
360         APP_LOGE("absSharedResultSet nullptr, systemState %{public}s", systemState.c_str());
361         return false;
362     }
363     ScopeGuard stateGuard([absSharedResultSet] { absSharedResultSet->Close(); });
364     auto ret = absSharedResultSet->GoToFirstRow();
365     if (ret != NativeRdb::E_OK) {
366         APP_LOGE("GoToFirstRow failed, ret %{public}d, systemState %{public}s", ret, systemState.c_str());
367         return false;
368     }
369 
370     do {
371         BundleResourceInfo resourceInfo;
372         if (ConvertToBundleResourceInfo(absSharedResultSet, flags, resourceInfo)) {
373             bundleResourceInfos.push_back(resourceInfo);
374         }
375     } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
376 
377     if ((flags & static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_SORTED_BY_LABEL)) ==
378         static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_SORTED_BY_LABEL)) {
379         APP_LOGD("need sort by label");
380         std::sort(bundleResourceInfos.begin(), bundleResourceInfos.end(),
381             [](BundleResourceInfo &resourceA, BundleResourceInfo &resourceB) {
382                 return resourceA.label < resourceB.label;
383             });
384     }
385     return !bundleResourceInfos.empty();
386 }
387 
GetAllLauncherAbilityResourceInfo(const uint32_t flags,std::vector<LauncherAbilityResourceInfo> & launcherAbilityResourceInfos)388 bool BundleResourceRdb::GetAllLauncherAbilityResourceInfo(const uint32_t flags,
389     std::vector<LauncherAbilityResourceInfo> &launcherAbilityResourceInfos)
390 {
391     APP_LOGI("start get all launcher resource");
392     NativeRdb::AbsRdbPredicates absRdbPredicates(BundleResourceConstants::BUNDLE_RESOURCE_RDB_TABLE_NAME);
393     absRdbPredicates.Contains(BundleResourceConstants::NAME, BundleResourceConstants::SEPARATOR);
394     std::string systemState = BundleSystemState::GetInstance().ToString();
395 
396     auto absSharedResultSet = rdbDataManager_->QueryByStep(absRdbPredicates);
397     if (absSharedResultSet == nullptr) {
398         APP_LOGE("absSharedResultSet nullptr, systemState %{public}s", systemState.c_str());
399         return false;
400     }
401 
402     ScopeGuard stateGuard([absSharedResultSet] { absSharedResultSet->Close(); });
403     auto ret = absSharedResultSet->GoToFirstRow();
404     if (ret != NativeRdb::E_OK) {
405         APP_LOGE("GoToFirstRow failed, ret %{public}d, systemState %{public}s", ret, systemState.c_str());
406         return false;
407     }
408 
409     do {
410         LauncherAbilityResourceInfo resourceInfo;
411         if (ConvertToLauncherAbilityResourceInfo(absSharedResultSet, flags, resourceInfo)) {
412             launcherAbilityResourceInfos.push_back(resourceInfo);
413         }
414     } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
415 
416     if ((flags & static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_SORTED_BY_LABEL)) ==
417         static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_SORTED_BY_LABEL)) {
418         std::sort(launcherAbilityResourceInfos.begin(), launcherAbilityResourceInfos.end(),
419             [](LauncherAbilityResourceInfo &resourceA, LauncherAbilityResourceInfo &resourceB) {
420                 return resourceA.label < resourceB.label;
421             });
422     }
423     return !launcherAbilityResourceInfos.empty();
424 }
425 
ConvertToBundleResourceInfo(const std::shared_ptr<NativeRdb::ResultSet> & absSharedResultSet,const uint32_t flags,BundleResourceInfo & bundleResourceInfo)426 bool BundleResourceRdb::ConvertToBundleResourceInfo(
427     const std::shared_ptr<NativeRdb::ResultSet> &absSharedResultSet,
428     const uint32_t flags,
429     BundleResourceInfo &bundleResourceInfo)
430 {
431     if (absSharedResultSet == nullptr) {
432         APP_LOGE("absSharedResultSet is nullptr");
433         return false;
434     }
435     auto ret = absSharedResultSet->GetString(BundleResourceConstants::INDEX_NAME, bundleResourceInfo.bundleName);
436     CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString name failed, ret: %{public}d");
437     if (bundleResourceInfo.bundleName.find_first_of(BundleResourceConstants::SEPARATOR) != std::string::npos) {
438         APP_LOGD("key:%{public}s not bundle resource info, continue", bundleResourceInfo.bundleName.c_str());
439         return false;
440     }
441     ParseKey(bundleResourceInfo.bundleName, bundleResourceInfo);
442 
443     bool getAll = (flags & static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_ALL)) ==
444         static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_ALL);
445 
446     bool getLabel = (flags & static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL)) ==
447         static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL);
448     if (getAll || getLabel) {
449         ret = absSharedResultSet->GetString(BundleResourceConstants::INDEX_LABEL, bundleResourceInfo.label);
450         CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString label failed, ret: %{public}d");
451     }
452 
453     bool getIcon = (flags & static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_ICON)) ==
454         static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_ICON);
455     if (getAll || getIcon) {
456         ret = absSharedResultSet->GetString(BundleResourceConstants::INDEX_ICON, bundleResourceInfo.icon);
457         CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString label icon, ret: %{public}d");
458     }
459 
460     bool getDrawable = (flags & static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_DRAWABLE_DESCRIPTOR)) ==
461         static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_DRAWABLE_DESCRIPTOR);
462     if (getDrawable) {
463         ret = absSharedResultSet->GetBlob(BundleResourceConstants::INDEX_FOREGROUND, bundleResourceInfo.foreground);
464         CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetBlob foreground, ret: %{public}d");
465 
466         ret = absSharedResultSet->GetBlob(BundleResourceConstants::INDEX_BACKGROUND, bundleResourceInfo.background);
467         CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetBlob background, ret: %{public}d");
468     }
469     return true;
470 }
471 
ConvertToLauncherAbilityResourceInfo(const std::shared_ptr<NativeRdb::ResultSet> & absSharedResultSet,const uint32_t flags,LauncherAbilityResourceInfo & launcherAbilityResourceInfo)472 bool BundleResourceRdb::ConvertToLauncherAbilityResourceInfo(
473     const std::shared_ptr<NativeRdb::ResultSet> &absSharedResultSet,
474     const uint32_t flags,
475     LauncherAbilityResourceInfo &launcherAbilityResourceInfo)
476 {
477     if (absSharedResultSet == nullptr) {
478         APP_LOGE("absSharedResultSet is nullptr");
479         return false;
480     }
481     std::string key;
482     auto ret = absSharedResultSet->GetString(BundleResourceConstants::INDEX_NAME, key);
483     CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString name failed, ret: %{public}d");
484     ParseKey(key, launcherAbilityResourceInfo);
485     if (launcherAbilityResourceInfo.moduleName.empty() || launcherAbilityResourceInfo.abilityName.empty()) {
486         APP_LOGW("key:%{public}s not launcher ability resource info", key.c_str());
487         return false;
488     }
489     bool getAll = (flags & static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_ALL)) ==
490         static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_ALL);
491     bool getLabel = (flags & static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL)) ==
492         static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL);
493     if (getAll || getLabel) {
494         ret = absSharedResultSet->GetString(BundleResourceConstants::INDEX_LABEL, launcherAbilityResourceInfo.label);
495         CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString label failed, ret: %{public}d");
496     }
497 
498     bool getIcon = (flags & static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_ICON)) ==
499         static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_ICON);
500     if (getAll || getIcon) {
501         ret = absSharedResultSet->GetString(BundleResourceConstants::INDEX_ICON, launcherAbilityResourceInfo.icon);
502         CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString label icon, ret: %{public}d");
503     }
504 
505     bool getDrawable = (flags & static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_DRAWABLE_DESCRIPTOR)) ==
506         static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_DRAWABLE_DESCRIPTOR);
507     if (getDrawable) {
508         ret = absSharedResultSet->GetBlob(BundleResourceConstants::INDEX_FOREGROUND,
509             launcherAbilityResourceInfo.foreground);
510         CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetBlob foreground, ret: %{public}d");
511 
512         ret = absSharedResultSet->GetBlob(BundleResourceConstants::INDEX_BACKGROUND,
513             launcherAbilityResourceInfo.background);
514         CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetBlob background, ret: %{public}d");
515     }
516     return true;
517 }
518 
UpdateResourceForSystemStateChanged(const std::vector<ResourceInfo> & resourceInfos)519 bool BundleResourceRdb::UpdateResourceForSystemStateChanged(const std::vector<ResourceInfo> &resourceInfos)
520 {
521     if (resourceInfos.empty()) {
522         APP_LOGE("resourceInfos is empty");
523         return false;
524     }
525     std::string systemState = BundleSystemState::GetInstance().ToString();
526     int64_t timeStamp = BundleUtil::GetCurrentTimeMs();
527     bool ret = true;
528     NativeRdb::AbsRdbPredicates absRdbPredicates(BundleResourceConstants::BUNDLE_RESOURCE_RDB_TABLE_NAME);
529     for (const auto &resourceInfo : resourceInfos) {
530         NativeRdb::ValuesBucket valuesBucket;
531         valuesBucket.PutString(BundleResourceConstants::NAME, resourceInfo.GetKey());
532         valuesBucket.PutString(BundleResourceConstants::SYSTEM_STATE, systemState);
533         // process label
534         if (!resourceInfo.label_.empty()) {
535             valuesBucket.PutString(BundleResourceConstants::LABEL, resourceInfo.label_);
536         } else if (!resourceInfos[0].label_.empty()) {
537             valuesBucket.PutString(BundleResourceConstants::LABEL, resourceInfos[0].label_);
538         }
539         // process icon
540         if (!resourceInfo.icon_.empty()) {
541             valuesBucket.PutString(BundleResourceConstants::ICON, resourceInfo.icon_);
542             valuesBucket.PutBlob(BundleResourceConstants::FOREGROUND, resourceInfo.foreground_);
543             valuesBucket.PutBlob(BundleResourceConstants::BACKGROUND, resourceInfo.background_);
544         } else if (!resourceInfos[0].icon_.empty()) {
545             valuesBucket.PutString(BundleResourceConstants::ICON, resourceInfos[0].icon_);
546             valuesBucket.PutBlob(BundleResourceConstants::FOREGROUND, resourceInfos[0].foreground_);
547             valuesBucket.PutBlob(BundleResourceConstants::BACKGROUND, resourceInfos[0].background_);
548         }
549         valuesBucket.PutLong(BundleResourceConstants::UPDATE_TIME, timeStamp);
550         absRdbPredicates.EqualTo(BundleResourceConstants::NAME, resourceInfo.GetKey());
551         if (!rdbDataManager_->UpdateOrInsertData(valuesBucket, absRdbPredicates)) {
552             APP_LOGE("bundleName: %{public}s UpdateData failed", resourceInfo.GetKey().c_str());
553             ret = false;
554         }
555         absRdbPredicates.Clear();
556     }
557     if (ret) {
558         BackupRdb();
559     }
560     return ret;
561 }
562 
GetCurrentSystemState(std::string & systemState)563 bool BundleResourceRdb::GetCurrentSystemState(std::string &systemState)
564 {
565     NativeRdb::AbsRdbPredicates absRdbPredicates(BundleResourceConstants::BUNDLE_RESOURCE_RDB_TABLE_NAME);
566     absRdbPredicates.EqualTo(BundleResourceConstants::NAME, SYSTEM_RESOURCES_APP);
567     auto absSharedResultSet = rdbDataManager_->QueryByStep(absRdbPredicates);
568     if (absSharedResultSet == nullptr) {
569         APP_LOGW("bundleName:%{public}s failed due rdb QueryByStep failed", SYSTEM_RESOURCES_APP);
570         return false;
571     }
572 
573     ScopeGuard stateGuard([absSharedResultSet] { absSharedResultSet->Close(); });
574     auto ret = absSharedResultSet->GoToFirstRow();
575     if (ret != NativeRdb::E_OK) {
576         APP_LOGW("bundleName:%{public}s GoToFirstRow failed, ret: %{public}d", SYSTEM_RESOURCES_APP, ret);
577         return false;
578     }
579     ret = absSharedResultSet->GetString(BundleResourceConstants::INDEX_SYSTEM_STATE, systemState);
580     CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString name failed, ret: %{public}d");
581     APP_LOGI("current resource rdb systemState:%{public}s", systemState.c_str());
582     return true;
583 }
584 
DeleteNotExistResourceInfo()585 bool BundleResourceRdb::DeleteNotExistResourceInfo()
586 {
587     // need delete not current systemState resource
588     std::string systemState = BundleSystemState::GetInstance().ToString();
589     APP_LOGI_NOFUNC("current systemState:%{public}s", systemState.c_str());
590     NativeRdb::AbsRdbPredicates absRdbPredicates(BundleResourceConstants::BUNDLE_RESOURCE_RDB_TABLE_NAME);
591     absRdbPredicates.NotEqualTo(BundleResourceConstants::SYSTEM_STATE, systemState);
592     return rdbDataManager_->DeleteData(absRdbPredicates);
593 }
594 
ParseKey(const std::string & key,LauncherAbilityResourceInfo & launcherAbilityResourceInfo)595 void BundleResourceRdb::ParseKey(const std::string &key,
596     LauncherAbilityResourceInfo &launcherAbilityResourceInfo)
597 {
598     ResourceInfo info;
599     info.ParseKey(key);
600     launcherAbilityResourceInfo.bundleName = info.bundleName_;
601     launcherAbilityResourceInfo.moduleName = info.moduleName_;
602     launcherAbilityResourceInfo.abilityName = info.abilityName_;
603     launcherAbilityResourceInfo.appIndex = info.appIndex_;
604 }
605 
ParseKey(const std::string & key,BundleResourceInfo & bundleResourceInfo)606 void BundleResourceRdb::ParseKey(const std::string &key,
607     BundleResourceInfo &bundleResourceInfo)
608 {
609     ResourceInfo info;
610     info.ParseKey(key);
611     bundleResourceInfo.bundleName = info.bundleName_;
612     bundleResourceInfo.appIndex = info.appIndex_;
613 }
614 
BackupRdb()615 void BundleResourceRdb::BackupRdb()
616 {
617     std::weak_ptr<BundleResourceRdb> weakPtr = weak_from_this();
618     auto task = [weakPtr] {
619         APP_LOGI("backup resource db begin");
620         auto sharedPtr = weakPtr.lock();
621         if (sharedPtr == nullptr || sharedPtr->rdbDataManager_ == nullptr) {
622             APP_LOGE("backup resource db failed");
623             return;
624         }
625         sharedPtr->rdbDataManager_->BackupRdb();
626         APP_LOGI("backup resource db end");
627     };
628     delayedTaskMgr_->ScheduleDelayedTask(task);
629 }
630 } // AppExecFwk
631 } // OHOS
632