• 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 #include "insight_intent_event_mgr.h"
17 
18 #include "insight_intent_sys_event_receiver.h"
19 #include "insight_intent_db_cache.h"
20 #include "ability_manager_errors.h"
21 #include "bundle_mgr_helper.h"
22 #include "hilog_tag_wrapper.h"
23 #include "in_process_call_wrapper.h"
24 #include "extract_insight_intent_profile.h"
25 #include "os_account_manager_wrapper.h"
26 #include "common_event_manager.h"
27 #include "common_event_support.h"
28 
29 namespace OHOS {
30 namespace AbilityRuntime {
DeleteInsightIntent(const std::string & bundleName,const std::string & moduleName,int32_t userId)31 void InsightIntentEventMgr::DeleteInsightIntent(const std::string &bundleName, const std::string &moduleName,
32     int32_t userId)
33 {
34     std::vector<ExtractInsightIntentInfo> intentInfos;
35     DelayedSingleton<InsightIntentDbCache>::GetInstance()->GetInsightIntentInfoByName(
36         bundleName, userId, intentInfos);
37     if (!intentInfos.empty()) {
38         TAG_LOGI(AAFwkTag::INTENT, "update bundleName: %{public}s to no insight intent", bundleName.c_str());
39         DelayedSingleton<AbilityRuntime::InsightIntentDbCache>::GetInstance()->DeleteInsightIntentTotalInfo(
40             bundleName, moduleName, userId);
41     }
42 
43     return;
44 }
45 
UpdateInsightIntentEvent(const AppExecFwk::ElementName & elementName,int32_t userId)46 void InsightIntentEventMgr::UpdateInsightIntentEvent(const AppExecFwk::ElementName &elementName, int32_t userId)
47 {
48     ErrCode ret;
49     std::vector<std::string> moduleNameVec;
50     std::string profile;
51     AbilityRuntime::ExtractInsightIntentProfileInfoVec infos = {};
52     auto bundleName = elementName.GetBundleName();
53     auto moduleName = elementName.GetModuleName();
54     if (bundleName.empty() || moduleName.empty()) {
55         TAG_LOGW(AAFwkTag::INTENT, "input param empty, bundleName: %{public}s, moduleName: %{public}s",
56             bundleName.c_str(), moduleName.c_str());
57         return;
58     }
59     if (userId < 0) {
60         TAG_LOGW(AAFwkTag::INTENT, "invalid userId: %{public}d", userId);
61         return;
62     }
63 
64     auto bundleMgrHelper = DelayedSingleton<AppExecFwk::BundleMgrHelper>::GetInstance();
65     if (bundleMgrHelper == nullptr) {
66         TAG_LOGE(AAFwkTag::INTENT, "get bundleMgrHelper instance failed");
67         return;
68     }
69 
70     OHOS::SplitStr(moduleName, ",", moduleNameVec);
71     for (std::string moduleNameLocal : moduleNameVec) {
72         // Get json profile firstly
73         ret = IN_PROCESS_CALL(bundleMgrHelper->GetJsonProfile(AppExecFwk::INTENT_PROFILE, bundleName, moduleNameLocal,
74             profile, userId));
75         if (ret != ERR_OK) {
76             TAG_LOGI(AAFwkTag::INTENT, "get json failed code: %{public}d, bundleName: %{public}s, "
77                 "moduleName: %{public}s, userId: %{public}d", ret, bundleName.c_str(), moduleNameLocal.c_str(), userId);
78             DeleteInsightIntent(bundleName, moduleNameLocal, userId);
79             continue;
80         }
81 
82         // Transform json string
83         if (!AbilityRuntime::ExtractInsightIntentProfile::TransformTo(profile, infos) ||
84             infos.insightIntents.size() == 0) {
85             TAG_LOGW(AAFwkTag::INTENT, "transform profile failed, profile:%{public}s", profile.c_str());
86             DeleteInsightIntent(bundleName, moduleNameLocal, userId);
87             continue;
88         }
89 
90         // save database
91         ret = DelayedSingleton<AbilityRuntime::InsightIntentDbCache>::GetInstance()->SaveInsightIntentTotalInfo(
92             bundleName, moduleNameLocal, userId, infos);
93         if (ret != ERR_OK) {
94             TAG_LOGW(AAFwkTag::INTENT, "update intent info failed, bundleName: %{public}s, "
95                 "moduleName: %{public}s, userId: %{public}d", bundleName.c_str(), moduleNameLocal.c_str(), userId);
96             continue;
97         }
98 
99         TAG_LOGI(AAFwkTag::INTENT, "update intent info success, bundleName: %{public}s, "
100             "moduleName: %{public}s, userId: %{public}d", bundleName.c_str(), moduleNameLocal.c_str(), userId);
101     }
102 }
103 
DeleteInsightIntentEvent(const AppExecFwk::ElementName & elementName,int32_t userId,int32_t appIndex)104 void InsightIntentEventMgr::DeleteInsightIntentEvent(const AppExecFwk::ElementName &elementName, int32_t userId,
105     int32_t appIndex)
106 {
107     ErrCode ret;
108     auto bundleName = elementName.GetBundleName();
109     auto moduleName = elementName.GetModuleName();
110     if (bundleName.empty()) {
111         TAG_LOGE(AAFwkTag::INTENT, "input bundleName empty, bundleName: %{public}s, moduleName: %{public}s",
112             bundleName.c_str(), moduleName.c_str());
113         return;
114     }
115 
116     if (appIndex > 0) {
117         TAG_LOGI(AAFwkTag::INTENT, "this application is a simulation, not support to delete intent info, "
118             "bundleName: %{public}s, appIndex: %{public}d", bundleName.c_str(), appIndex);
119         return;
120     }
121 
122     if (userId < 0) {
123         TAG_LOGI(AAFwkTag::INTENT, "invalid userId: %{public}d", userId);
124         return;
125     }
126 
127     ret = DelayedSingleton<AbilityRuntime::InsightIntentDbCache>::GetInstance()->DeleteInsightIntentTotalInfo(
128         bundleName, moduleName, userId);
129     if (ret != ERR_OK) {
130         TAG_LOGW(AAFwkTag::INTENT, "delete intent info failed, bundleName: %{public}s, "
131             "moduleName: %{public}s, userId: %{public}d", bundleName.c_str(), moduleName.c_str(), userId);
132         return;
133     }
134 
135     TAG_LOGI(AAFwkTag::INTENT, "delete intent info success, bundleName: %{public}s, "
136         "moduleName: %{public}s, userId: %{public}d", bundleName.c_str(), moduleName.c_str(), userId);
137 }
138 
SubscribeSysEventReceiver()139 void InsightIntentEventMgr::SubscribeSysEventReceiver()
140 {
141     EventFwk::MatchingSkills matchingSkills;
142     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BUNDLE_SCAN_FINISHED);
143     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
144     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
145     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
146     subscribeInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
147     insightIntentSysEventReceiver_ = std::make_shared<AbilityRuntime::InsightIntentSysEventReceiver>(subscribeInfo);
148     bool subResult = EventFwk::CommonEventManager::SubscribeCommonEvent(insightIntentSysEventReceiver_);
149     if (!subResult) {
150         TAG_LOGE(AAFwkTag::INTENT, "subscribe common event failed");
151         return;
152     }
153 
154     TAG_LOGI(AAFwkTag::INTENT, "subscribe common event success");
155 }
156 
157 } // namespace AbilityRuntime
158 } // namespace OHOS
159