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 CHECK_ERROR_RETURN_RET(appManagerInstance_, appManagerInstance_);
45
46 sptr<OHOS::ISystemAbilityManager> abilityMgr =
47 OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
48 CHECK_ERROR_RETURN_RET_LOG(abilityMgr == nullptr, nullptr, "Failed to get ISystemAbilityManager");
49 sptr<IRemoteObject> remoteObject = abilityMgr->GetSystemAbility(APP_MGR_SERVICE_ID);
50 CHECK_ERROR_RETURN_RET_LOG(remoteObject == nullptr, nullptr,
51 "Failed to get app manager service, id=%{public}u", APP_MGR_SERVICE_ID);
52 sptr<OHOS::AppExecFwk::IAppMgr> appMgrProxy = iface_cast<OHOS::AppExecFwk::IAppMgr>(remoteObject);
53 CHECK_ERROR_RETURN_RET_LOG(appMgrProxy == nullptr || !appMgrProxy->AsObject(), nullptr,
54 "Failed to get app manager proxy");
55 sptr<CameraAppManagerUtilsDeathRecipient> CameraAppManagerUtilsDeathRecipient_ =
56 new CameraAppManagerUtilsDeathRecipient();
57 remoteObject->AddDeathRecipient(CameraAppManagerUtilsDeathRecipient_);
58 appManagerInstance_ = appMgrProxy;
59 return appManagerInstance_;
60 }
61
GetForegroundApplications(std::vector<OHOS::AppExecFwk::AppStateData> & appsData)62 void CameraAppManagerUtils::GetForegroundApplications(std::vector<OHOS::AppExecFwk::AppStateData>& appsData)
63 {
64 auto appMgr = GetAppManagerInstance();
65 if (!appMgr) {
66 return;
67 }
68 int32_t ret = appMgr->GetForegroundApplications(appsData);
69 MEDIA_DEBUG_LOG("GetForegroundApplications, ret: %{public}u, num of apps: %{public}zu", ret, appsData.size());
70 }
71
IsForegroundApplication(const uint32_t tokenId)72 bool CameraAppManagerUtils::IsForegroundApplication(const uint32_t tokenId)
73 {
74 bool IsForeground = false;
75 std::vector<OHOS::AppExecFwk::AppStateData> appsData;
76 GetForegroundApplications(appsData);
77 for (const auto& curApp : appsData) {
78 if (curApp.accessTokenId == tokenId) {
79 IsForeground = true;
80 break;
81 }
82 }
83 MEDIA_DEBUG_LOG("IsForegroundApplication, ret: %{public}u", static_cast<uint32_t>(IsForeground));
84 return IsForeground;
85 }
86
OnRemoveInstance()87 void CameraAppManagerUtils::OnRemoveInstance()
88 {
89 std::lock_guard<std::mutex> lock(g_cameraAppManagerInstanceMutex);
90 appManagerInstance_ = nullptr;
91 }
92 } // namespace PowerMgr
93 } // namespace OHOS