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