1 /*
2 * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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 #ifndef APP_DATA_PARSER_H
16 #define APP_DATA_PARSER_H
17 #include <vector>
18 #include "ability_info.h"
19 #include "bundle_mgr_interface.h"
20 #include "bundle_mgr_proxy.h"
21 #include "common_event_subscriber.h"
22 #include "common_event_support.h"
23 #include "element_name.h"
24 #ifdef VENDOR_APPLICATIONS_ENABLED
25 #include "ion_card_emulation_notify_cb.h"
26 #include "iquery_app_info_callback.h"
27 #endif
28 #include "nfc_sdk_common.h"
29 #include "want.h"
30 
31 namespace OHOS {
32 namespace NFC {
33 using AppExecFwk::AbilityInfo;
34 using AppExecFwk::ExtensionAbilityInfo;
35 using OHOS::AppExecFwk::ElementName;
36 class BundleMgrDeathRecipient : public IRemoteObject::DeathRecipient {
37     void OnRemoteDied([[maybe_unused]] const wptr<IRemoteObject> &remote) override;
38 };
39 class AppDataParser {
40 public:
41     explicit AppDataParser();
42     ~AppDataParser();
43 
44     struct AidInfo {
45         std::string name; // the type, payment-aid, or other-aid
46         std::string value; // the aid value
47     };
48 
49     struct TagAppTechInfo {
50         ElementName element;
51         std::vector<std::string> tech;
52     };
53 
54     struct HceAppAidInfo {
55         ElementName element;
56         int32_t labelId;
57         int32_t iconId;
58         std::vector<AidInfo> customDataAid;
59     };
60 
61     std::vector<TagAppTechInfo> g_tagAppAndTechMap;
62     std::vector<HceAppAidInfo> g_hceAppAndAidMap;
63     std::vector<HceAppAidInfo> g_offHostAppAndAidMap;
64 
65     static AppDataParser& GetInstance();
66 
67     bool HandleAppAddOrChangedEvent(std::shared_ptr<EventFwk::CommonEventData> data);
68     bool HandleAppRemovedEvent(std::shared_ptr<EventFwk::CommonEventData> data);
69     void InitAppList();
70     std::vector<ElementName> GetDispatchTagAppsByTech(std::vector<int> discTechList);
71 #ifdef VENDOR_APPLICATIONS_ENABLED
72     std::vector<ElementName> GetVendorDispatchTagAppsByTech(std::vector<int>& discTechList);
73     void RegQueryApplicationCb(sptr<IQueryAppInfoCallback> callback);
74     void RegCardEmulationNotifyCb(sptr<IOnCardEmulationNotifyCb> callback);
75     sptr<IOnCardEmulationNotifyCb> GetNotifyCardEmulationCallback() const;
76 #endif
77     void GetHceAppsByAid(const std::string &aid, std::vector<AppDataParser::HceAppAidInfo>& hceApps);
78     bool IsBundleInstalled(const std::string &bundleName);
79     void GetHceApps(std::vector<HceAppAidInfo> &hceApps);
80     void GetPaymentAbilityInfos(std::vector<AbilityInfo> &paymentAbilityInfos);
81     bool GetBundleInfo(AppExecFwk::BundleInfo &bundleInfo, const std::string &bundleName);
82     bool IsSystemApp(uint32_t uid);
83     bool IsHceApp(const ElementName &elementName);
84     std::string GetBundleNameByUid(uint32_t uid);
85 private:
86     static sptr<AppExecFwk::IBundleMgr> GetBundleMgrProxy();
87     ElementName GetMatchedTagKeyElement(ElementName &element);
88     ElementName GetMatchedHceKeyElement(ElementName &element);
89     bool IsMatchedByBundleName(ElementName &src, ElementName &target);
90     bool InitAppListByAction(const std::string action);
91     void QueryAbilityInfos(const std::string action, std::vector<AbilityInfo> &abilityInfos,
92         std::vector<ExtensionAbilityInfo> &extensionInfos);
93     bool VerifyHapPermission(const std::string bundleName, const std::string action);
94     bool UpdateAppListInfo(ElementName &element, const std::string action);
95     void UpdateTagAppList(AbilityInfo &abilityInfo, ElementName &element);
96     void UpdateHceAppList(AbilityInfo &abilityInfo, ElementName &element);
97     void UpdateOffHostAppList(AbilityInfo &abilityInfo, ElementName &element);
98     bool HaveMatchedOffHostKeyElement(ElementName &element);
99     bool RemoveTagAppInfo(ElementName &element);
100     bool RemoveHceAppInfo(ElementName &element);
101     bool RemoveOffHostAppInfo(ElementName &element);
102     bool IsPaymentApp(const AppDataParser::HceAppAidInfo &hceAppInfo);
103 #ifdef VENDOR_APPLICATIONS_ENABLED
104     void GetHceAppsFromVendor(std::vector<HceAppAidInfo> &hceApps);
105     void GetPaymentAbilityInfosFromVendor(std::vector<AbilityInfo> &paymentAbilityInfos);
106     bool IsHceAppFromVendor(const ElementName &elementName);
107     sptr<IQueryAppInfoCallback> queryApplicationByVendor_ {};
108     sptr<IOnCardEmulationNotifyCb> onCardEmulationNotify_ {};
109 #endif
110     bool appListInitDone_ = false;
111 };
112 }  // namespace NFC
113 }  // namespace OHOS
114 #endif  // NFC_EVENT_HANDLER_H
115