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