Tizen 2.1 base
[platform/core/api/app-manager.git] / src / app_manager.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include <aul.h>
24 #include <dlog.h>
25
26 #include <app_manager.h>
27 #include <app_manager_private.h>
28
29 #ifdef LOG_TAG
30 #undef LOG_TAG
31 #endif
32
33 #define LOG_TAG "CAPI_APPFW_APP_MANAGER"
34
35
36 static const char* app_manager_error_to_string(app_manager_error_e error)
37 {
38         switch (error)
39         {
40         case APP_MANAGER_ERROR_NONE:
41                 return "NONE";
42
43         case APP_MANAGER_ERROR_INVALID_PARAMETER:
44                 return "INVALID_PARAMETER";
45
46         case APP_MANAGER_ERROR_OUT_OF_MEMORY:
47                 return "OUT_OF_MEMORY";
48
49         case APP_MANAGER_ERROR_IO_ERROR:
50                 return "IO_ERROR";
51
52         case APP_MANAGER_ERROR_NO_SUCH_APP:
53                 return "NO_SUCH_APP";
54
55         case APP_MANAGER_ERROR_DB_FAILED:
56                 return "DB_FAILED";
57
58         case APP_MANAGER_ERROR_INVALID_PACKAGE:
59                 return "INVALID_PACKAGE";
60
61         default :
62                 return "UNKNOWN";
63         }
64 }
65
66 int app_manager_error(app_manager_error_e error, const char* function, const char *description)
67 {
68         if (description)
69         {
70                 LOGE("[%s] %s(0x%08x) : %s", function, app_manager_error_to_string(error), error, description); 
71         }
72         else
73         {
74                 LOGE("[%s] %s(0x%08x)", function, app_manager_error_to_string(error), error);   
75         }
76
77         return error;
78 }
79
80
81 int app_manager_set_app_context_event_cb(app_manager_app_context_event_cb callback, void *user_data)
82 {
83         int retval;
84
85         retval = app_context_set_event_cb(callback, user_data);
86
87         if (retval != APP_MANAGER_ERROR_NONE)
88         {
89                 return app_manager_error(retval, __FUNCTION__, NULL);
90         }
91         else
92         {
93                 return APP_MANAGER_ERROR_NONE;
94         }
95 }
96
97 void app_manager_unset_app_context_event_cb(void)
98 {
99         app_context_unset_event_cb();
100 }
101
102 int app_manager_foreach_app_context(app_manager_app_context_cb callback, void *user_data)
103 {
104         int retval;
105
106         retval = app_context_foreach_app_context(callback, user_data);
107
108         if (retval != APP_MANAGER_ERROR_NONE)
109         {
110                 return app_manager_error(retval, __FUNCTION__, NULL);
111         }
112         else
113         {
114                 return APP_MANAGER_ERROR_NONE;
115         }
116 }
117
118 int app_manager_get_app_context(const char *app_id, app_context_h *app_context)
119 {
120         int retval;
121
122         retval = app_context_get_app_context(app_id, app_context);
123
124         if (retval != APP_MANAGER_ERROR_NONE)
125         {
126                 return app_manager_error(retval, __FUNCTION__, NULL);
127         }
128         else
129         {
130                 return APP_MANAGER_ERROR_NONE;
131         }
132 }
133
134 int app_manager_resume_app(app_context_h app_context)
135 {
136         char *app_id;
137
138         if (app_context == NULL)
139         {
140                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
141         }
142
143         if (app_context_get_app_id(app_context, &app_id) != APP_MANAGER_ERROR_NONE)
144         {
145                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to get the application ID");
146         }
147
148         aul_resume_app(app_id);
149
150         return APP_MANAGER_ERROR_NONE;
151 }
152
153 int app_manager_set_app_info_event_cb(app_manager_app_info_event_cb callback, void *user_data)
154 {
155         int retval;
156
157         retval = app_info_set_event_cb(callback, user_data);
158
159         if (retval != APP_MANAGER_ERROR_NONE)
160         {
161                 return app_manager_error(retval, __FUNCTION__, NULL);
162         }
163         else
164         {
165                 return APP_MANAGER_ERROR_NONE;
166         }
167 }
168
169 void app_manager_unset_app_info_event_cb(void)
170 {
171         app_info_unset_event_cb();
172 }
173
174
175 int app_manager_foreach_app_info(app_manager_app_info_cb callback, void *user_data)
176 {
177         int retval;
178
179         retval = app_info_foreach_app_info(callback, user_data);
180
181         if (retval != APP_MANAGER_ERROR_NONE)
182         {
183                 return app_manager_error(retval, __FUNCTION__, NULL);
184         }
185         else
186         {
187                 return APP_MANAGER_ERROR_NONE;
188         }
189 }
190
191 int app_manager_get_app_info(const char *app_id, app_info_h *app_info)
192 {
193         int retval;
194
195         retval = app_info_get_app_info(app_id, app_info);
196
197         if (retval != APP_MANAGER_ERROR_NONE)
198         {
199                 return app_manager_error(retval, __FUNCTION__, NULL);
200         }
201         else
202         {
203                 return APP_MANAGER_ERROR_NONE;
204         }
205 }
206
207 int app_manager_get_ui_app_info(const char *app_id, ui_app_info_h *ui_app_info)
208 {
209         int retval;
210
211         retval = ui_app_info_get_app_info(app_id, ui_app_info);
212
213         if (retval != APP_MANAGER_ERROR_NONE)
214         {
215                 return app_manager_error(retval, __FUNCTION__, NULL);
216         }
217         else
218         {
219                 return APP_MANAGER_ERROR_NONE;
220         }
221 }
222
223 int app_manager_get_service_app_info(const char *app_id, service_app_info_h *service_app_info)
224 {
225         int retval;
226
227         retval = service_app_info_get_app_info(app_id, service_app_info);
228
229         if (retval != APP_MANAGER_ERROR_NONE)
230         {
231                 return app_manager_error(retval, __FUNCTION__, NULL);
232         }
233         else
234         {
235                 return APP_MANAGER_ERROR_NONE;
236         }
237 }
238
239 int app_manager_get_package(pid_t pid, char **package)
240 {
241         // TODO: this function must be deprecated
242         return app_manager_get_app_id(pid, package);
243 }
244
245 int app_manager_get_app_id(pid_t pid, char **app_id)
246 {
247         char buffer[256] = {0, };
248         char *app_id_dup = NULL;
249
250         if (app_id == NULL)
251         {
252                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
253         }
254
255         if (aul_app_get_appid_bypid(pid, buffer, sizeof(buffer)) != AUL_R_OK)
256         {
257                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "Invalid process ID");
258         }
259
260         app_id_dup = strdup(buffer);
261
262         if (app_id_dup == NULL)
263         {
264                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
265         }
266
267         *app_id = app_id_dup;
268
269         return APP_MANAGER_ERROR_NONE;
270
271 }
272
273 int app_manager_terminate_app(app_context_h app_context)
274 {
275         pid_t pid = 0;
276
277         if (app_context == NULL)
278         {
279                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
280         }
281
282         if (app_context_get_pid(app_context, &pid) != APP_MANAGER_ERROR_NONE) {
283                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to get the process ID");
284         }
285
286         aul_terminate_pid(pid);
287
288         return APP_MANAGER_ERROR_NONE;
289 }
290
291 int app_manager_is_running(const char *app_id, bool *running)
292 {
293         if (app_id == NULL)
294         {
295                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid package", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
296                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
297         }
298
299         if (running == NULL)
300         {
301                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid output param", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
302                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
303         }
304
305         *running = aul_app_is_running(app_id);
306
307         return APP_MANAGER_ERROR_NONE;
308 }