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 #ifndef AV_SESSION_BUNDLE_STATUS_ADAPTER_H
17 #define AV_SESSION_BUNDLE_STATUS_ADAPTER_H
18 
19 #include <mutex>
20 
21 #include "bundle_mgr_proxy.h"
22 #include "bundle_status_callback_host.h"
23 
24 namespace OHOS::AVSession {
25 class BundleStatusAdapter {
26 public:
27     static BundleStatusAdapter& GetInstance();
28 
29     ~BundleStatusAdapter();
30 
31     void Init();
32 
33     bool SubscribeBundleStatusEvent(const std::string bundleName,
34         const std::function<void(const std::string, const int32_t userId)>& callback, int32_t userId = DEFAULT_USER_ID);
35 
36     bool IsAudioPlayback(const std::string& bundleName, const std::string& abilityName);
37 
38     std::string GetBundleNameFromUid(const int32_t uid);
39 
40     __attribute__((no_sanitize("cfi"))) bool IsSupportPlayIntent(const std::string& bundleName,
41         std::string& supportModule, std::string& profile);
42 
43 private:
44     BundleStatusAdapter();
45 
46     void NotifyBundleRemoved(const std::string bundleName, const int32_t userId);
47 
48     bool CheckBundleSupport(std::string& profile);
49 
50     sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy;
51 
52     std::map<std::pair<std::string, int32_t>, std::function<void(const std::string, const int32_t)>>
53         bundleStatusListeners_;
54 
55     const int32_t backgroundModeDemand = 2;
56 
57     const int32_t getBundleInfoWithHapModule = 0x00000002;
58 
59     const int32_t startUserId = 100;
60 
61     const std::string PLAY_MUSICLIST = "PlayMusicList";
62 
63     const std::string PLAY_AUDIO = "PlayAudio";
64 
65     std::recursive_mutex bundleMgrProxyLock_;
66 
67     static const int32_t DEFAULT_USER_ID = 100;
68 };
69 
70 class BundleStatusCallbackImpl : public AppExecFwk::BundleStatusCallbackHost {
71 public:
72     explicit BundleStatusCallbackImpl(const std::function<void(const std::string, const int32_t)>& callback,
73         int32_t userId);
74     ~BundleStatusCallbackImpl() override;
75     void OnBundleStateChanged(const uint8_t installType, const int32_t resultCode,
76         const std::string &resultMsg, const std::string &bundleName) override;
OnBundleAdded(const std::string & bundleName,const int userId)77     void OnBundleAdded(const std::string &bundleName, const int userId) override {};
OnBundleUpdated(const std::string & bundleName,const int userId)78     void OnBundleUpdated(const std::string &bundleName, const int userId) override {};
OnBundleRemoved(const std::string & bundleName,const int userId)79     void OnBundleRemoved(const std::string &bundleName, const int userId) override {};
80 private:
81     DISALLOW_COPY_AND_MOVE(BundleStatusCallbackImpl);
82     std::function<void(const std::string, const int32_t userId)> callback_;
83     int32_t userId_;
84 };
85 }
86 #endif // AV_SESSION_BUNDLE_STATUS_ADAPTER_H
87 
88