1 /*
2  * Copyright (c) 2024 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 "camera_app_manager_utils.h"
17 #include "camera_log.h"
18 
19 #include <app_mgr_interface.h>
20 #include <if_system_ability_manager.h>
21 #include <iservice_registry.h>
22 
23 namespace OHOS {
24 namespace CameraStandard {
25 class CameraAppManagerUtils::CameraAppManagerUtilsDeathRecipient : public IRemoteObject::DeathRecipient {
26     public:
CameraAppManagerUtilsDeathRecipient()27         explicit CameraAppManagerUtilsDeathRecipient() {}
~CameraAppManagerUtilsDeathRecipient()28         ~CameraAppManagerUtilsDeathRecipient() {}
29 
OnRemoteDied(const wptr<IRemoteObject> & remote)30         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
31         {
32             MEDIA_ERR_LOG("Remote died.");
33             CameraAppManagerUtils::OnRemoveInstance();
34         }
35 };
36 
37 static constexpr uint32_t APP_MGR_SERVICE_ID = 501;
38 static std::mutex g_cameraAppManagerInstanceMutex;
39 sptr<OHOS::AppExecFwk::IAppMgr> CameraAppManagerUtils::appManagerInstance_ = nullptr;
40 
GetAppManagerInstance()41 sptr<OHOS::AppExecFwk::IAppMgr> CameraAppManagerUtils::GetAppManagerInstance()
42 {
43     std::lock_guard<std::mutex> lock(g_cameraAppManagerInstanceMutex);
44     if (appManagerInstance_) {
45         return appManagerInstance_;
46     }
47 
48     sptr<OHOS::ISystemAbilityManager> abilityMgr =
49         OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
50     if (abilityMgr == nullptr) {
51         MEDIA_ERR_LOG("Failed to get ISystemAbilityManager");
52         return nullptr;
53     }
54     sptr<IRemoteObject> remoteObject = abilityMgr->GetSystemAbility(APP_MGR_SERVICE_ID);
55     if (remoteObject == nullptr) {
56         MEDIA_ERR_LOG("Failed to get app manager service, id=%{public}u", APP_MGR_SERVICE_ID);
57         return nullptr;
58     }
59     sptr<OHOS::AppExecFwk::IAppMgr> appMgrProxy = iface_cast<OHOS::AppExecFwk::IAppMgr>(remoteObject);
60     if (appMgrProxy == nullptr || !appMgrProxy->AsObject()) {
61         MEDIA_ERR_LOG("Failed to get app manager proxy");
62         return nullptr;
63     }
64     sptr<CameraAppManagerUtilsDeathRecipient> CameraAppManagerUtilsDeathRecipient_ =
65         new CameraAppManagerUtilsDeathRecipient();
66     remoteObject->AddDeathRecipient(CameraAppManagerUtilsDeathRecipient_);
67     appManagerInstance_ = appMgrProxy;
68     return appManagerInstance_;
69 }
70 
GetForegroundApplications(std::vector<OHOS::AppExecFwk::AppStateData> & appsData)71 void CameraAppManagerUtils::GetForegroundApplications(std::vector<OHOS::AppExecFwk::AppStateData>& appsData)
72 {
73     auto appMgr = GetAppManagerInstance();
74     if (!appMgr) {
75         return;
76     }
77     int32_t ret = appMgr->GetForegroundApplications(appsData);
78     MEDIA_INFO_LOG("GetForegroundApplications, ret: %{public}u, num of apps: %{public}zu", ret, appsData.size());
79 }
80 
IsForegroundApplication(const uint32_t tokenId)81 bool CameraAppManagerUtils::IsForegroundApplication(const uint32_t tokenId)
82 {
83     MEDIA_INFO_LOG("check IsForegroundApplication");
84     bool IsForeground = false;
85     std::vector<OHOS::AppExecFwk::AppStateData> appsData;
86     GetForegroundApplications(appsData);
87     for (const auto& curApp : appsData) {
88         if (curApp.accessTokenId == tokenId) {
89             IsForeground = true;
90             break;
91         }
92     }
93     MEDIA_INFO_LOG("IsForegroundApplication, ret: %{public}u", static_cast<uint32_t>(IsForeground));
94     return IsForeground;
95 }
96 
OnRemoveInstance()97 void CameraAppManagerUtils::OnRemoveInstance()
98 {
99     std::lock_guard<std::mutex> lock(g_cameraAppManagerInstanceMutex);
100     appManagerInstance_ = nullptr;
101 }
102 } // namespace PowerMgr
103 } // namespace OHOS