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 "app_manager_utils.h"
17 #include "power_log.h"
18 #include <ability_manager_client.h>
19 #include <string>
20 #include <vector>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 using namespace OHOS;
27 using namespace PowerMgr;
28 using namespace AAFwk;
29 
PowerConnectAbility(const Want & want,const sptr<IAbilityConnection> & connect,int32_t userId)30 void PowerConnectAbility(const Want& want, const sptr<IAbilityConnection>& connect, int32_t userId)
31 {
32     auto amsClient = AbilityManagerClient::GetInstance();
33     if (amsClient == nullptr) {
34         POWER_HILOGW(FEATURE_UTIL, "AbilityManagerClient is nullptr");
35         return;
36     }
37     ErrCode result = amsClient->ConnectAbility(want, connect, userId);
38     if (result != ERR_OK) {
39         POWER_HILOGW(FEATURE_UTIL, "ConnectAbility systemui dialog failed, result = %{public}d", result);
40         return;
41     }
42     POWER_HILOGI(FEATURE_UTIL, "ConnectAbility systemui dialog success.");
43 }
44 
PowerStartAbility(const Want & want)45 void PowerStartAbility(const Want& want)
46 {
47     auto amsClient = AbilityManagerClient::GetInstance();
48     if (amsClient == nullptr) {
49         POWER_HILOGW(FEATURE_UTIL, "AbilityManagerClient is nullptr");
50         return;
51     }
52     ErrCode result = amsClient->StartAbility(want);
53     if (result != ERR_OK) {
54         POWER_HILOGW(FEATURE_UTIL, "StartAbility systemui dialog failed, result = %{public}d", result);
55         return;
56     }
57     POWER_HILOGI(FEATURE_UTIL, "StartAbility systemui dialog success.");
58 }
59 
PowerGetForegroundApplications(std::vector<OHOS::AppExecFwk::AppStateData> & appsData)60 void PowerGetForegroundApplications(std::vector<OHOS::AppExecFwk::AppStateData>& appsData)
61 {
62     AppManagerUtils::GetForegroundApplications(appsData);
63 }
64 
PowerIsForegroundApplication(const std::string & appName)65 bool PowerIsForegroundApplication(const std::string& appName)
66 {
67     return AppManagerUtils::IsForegroundApplication(appName);
68 }
69 
70 #ifdef __cplusplus
71 }
72 #endif
73