Replace int with pid_t where applicable
[platform/core/uifw/multi-assistant-service.git] / src / application_manager_aul.cpp
1 /*
2  * Copyright 2020 Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "application_manager_aul.h"
18 #include "service_common.h"
19
20 #include <aul.h>
21 #include <aul_svc.h>
22
23 CApplicationManagerAul::CApplicationManagerAul()
24 {
25 }
26
27 CApplicationManagerAul::~CApplicationManagerAul()
28 {
29 }
30
31 bool CApplicationManagerAul::is_application_running(pid_t pid)
32 {
33         int status = aul_app_get_status_bypid(pid);
34         if (0 > status) {
35                 MAS_LOGE("The process %d does not exist : %d", pid, status);
36                 return false;
37         }
38         return true;
39 }
40
41 bool CApplicationManagerAul::is_application_running(const std::string& appid)
42 {
43         return (!!aul_app_is_running(appid.c_str()));
44 }
45
46 bool CApplicationManagerAul::bring_app_to_foreground(const std::string& appid)
47 {
48         bool ret = true;
49
50         /* Bring MA client to foreground - is there a better way other than launching? */
51         bundle *b = NULL;
52         b = bundle_create();
53         if (NULL == b) {
54                 MAS_LOGE("Failed creating bundle for aul operation");
55                 return -1;
56         }
57
58         int result = aul_launch_app(appid.c_str(), b);
59         if (result < AUL_R_OK) {
60                 MAS_LOGE("ERROR : aul_launch_app failed. app_id [%s] bundle[%p] result[%d : %s]",
61                         appid.c_str(), b, result, get_error_message(result));
62                 ret = false;
63         }
64
65         if (b) bundle_free(b);
66         b = NULL;
67
68         return ret;
69 }
70
71 bool CApplicationManagerAul::launch_app_async(const std::string& appid, bool background)
72 {
73         bool ret = true;
74
75         bundle *b = NULL;
76         b = bundle_create();
77         if (NULL == b) {
78                 MAS_LOGE("Failed creating bundle for aul operation");
79                 return false;
80         }
81
82         int result = aul_svc_set_background_launch(b, background);
83         if (result < AUL_R_OK) {
84                 MAS_LOGE("ERROR : aul_svc_set_background_launch failed. app_id [%s] bundle[%p] result[%d : %s]",
85                         appid.c_str(), b, result, get_error_message(result));
86         }
87
88         result = aul_launch_app_async(appid.c_str(), b);
89         if (result < AUL_R_OK) {
90                 MAS_LOGE("ERROR : aul_launch_app_async failed. app_id [%s] bundle[%p] result[%d : %s]",
91                         appid.c_str(), b, result, get_error_message(result));
92                 ret = false;
93         }
94
95         if (b) bundle_free(b);
96         b = NULL;
97
98         return ret;
99 }
100
101 boost::optional<std::string> CApplicationManagerAul::get_appid_by_pid(pid_t pid)
102 {
103         boost::optional<std::string> ret;
104
105         char appid[MAX_APPID_LEN] = {'\0', };
106         if (AUL_R_OK == aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
107                 appid[MAX_APPID_LEN - 1] = '\0';
108                 ret = std::string{appid};
109         }
110
111         return ret;
112 }