summaryrefslogtreecommitdiff
path: root/src/app_manager_deprecated.c
blob: b13551e77abb3dd5ac31d0e3d8138f4e5d6e8a6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <aul.h>
#include <aul_service.h>
#include <vconf.h>
#include <ail.h>
#include <package-manager.h>
#include <dlog.h>

#include <app_manager.h>
#include <app_manager_private.h>

#ifdef LOG_TAG
#undef LOG_TAG
#endif

#define LOG_TAG "CAPI_APPFW_APP_MANAGER"

typedef struct {
	app_manager_app_running_cb cb;
	void *user_data;
	bool foreach_break;
} running_apps_foreach_cb_context;

typedef struct {
	app_manager_app_installed_cb cb;
	void *user_data;
} installed_apps_foreach_cb_context;

static pkgmgr_client *package_manager = NULL;
static app_manager_app_list_changed_cb app_list_changed_cb = NULL;
static void *app_list_changed_cb_data = NULL;

static int foreach_running_app_cb_broker(const aul_app_info * appcore_app_info, void *appcore_user_data)
{
	ail_appinfo_h handle;
	ail_error_e ret;
	bool task_manage = false;
	running_apps_foreach_cb_context *foreach_cb_context = NULL;

	if (appcore_app_info == NULL || appcore_user_data == NULL) 
	{
		LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid callback context", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
		return 0;
	}

	ret = ail_get_appinfo(appcore_app_info->pkg_name, &handle);
	if (ret != AIL_ERROR_OK)
	{
		LOGE("[%s] DB_FAILED(0x%08x) : failed to get the app-info", __FUNCTION__, APP_MANAGER_ERROR_DB_FAILED);
		return 0;
	}

	// do not call callback function when X-SLP-TaskManage is set to false
	ret = ail_appinfo_get_bool(handle, AIL_PROP_X_SLP_TASKMANAGE_BOOL, &task_manage);

	ail_destroy_appinfo(handle);

	if (ret != AIL_ERROR_OK || task_manage == false)
	{
		return 0;
	}

	foreach_cb_context = (running_apps_foreach_cb_context *)appcore_user_data;

	if (foreach_cb_context->cb != NULL && foreach_cb_context->foreach_break == false)
	{
		if (foreach_cb_context->cb(appcore_app_info->pkg_name, foreach_cb_context->user_data) == false)
		{
			foreach_cb_context->foreach_break = true;
		}
	}

	return 0;
}

static ail_cb_ret_e foreach_installed_app_cb_broker(const ail_appinfo_h appinfo, void *ail_user_data)
{
	installed_apps_foreach_cb_context *foreach_cb_context = NULL;
	char *package;

	if (appinfo == NULL || ail_user_data == NULL)
	{
		return AIL_CB_RET_CANCEL;
	}

	foreach_cb_context = (installed_apps_foreach_cb_context *)ail_user_data;

	ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &package);

	if (foreach_cb_context->cb(package, foreach_cb_context->user_data)  == false)
	{
		return AIL_CB_RET_CANCEL;
	}
	
	return AIL_CB_RET_CONTINUE;

}


static int app_manager_ail_error_handler(ail_error_e ail_error, const char *func)
{
	int error_code;
	char *error_msg;

	switch(ail_error)
	{
		case AIL_ERROR_FAIL:
			error_code = APP_MANAGER_ERROR_INVALID_PARAMETER;
			error_msg = "INVALID_PARAMETER";
			break;

		case AIL_ERROR_DB_FAILED:
			error_code = APP_MANAGER_ERROR_DB_FAILED;
			error_msg = "DB_FAILED";
			break;

		case AIL_ERROR_OUT_OF_MEMORY:
			error_code = APP_MANAGER_ERROR_OUT_OF_MEMORY;
			error_msg = "OUT_OF_MEMORY";
			break;

		case AIL_ERROR_INVALID_PARAMETER:
			error_code = APP_MANAGER_ERROR_INVALID_PARAMETER;
			error_msg = "INVALID_PARAMETER";
			break;
		
		case AIL_ERROR_OK:
			error_code = APP_MANAGER_ERROR_NONE;
			break;
			
		default:
			error_code = APP_MANAGER_ERROR_INVALID_PARAMETER;
			error_msg = "INVALID_PARAMETER";
	}

	if (error_code != APP_MANAGER_ERROR_NONE)
	{
		LOGE("[%s] %s(0x%08x)", func, error_msg, error_code);
	}

	return error_code;
}


int app_manager_foreach_app_running(app_manager_app_running_cb callback, void *user_data)
{
	running_apps_foreach_cb_context foreach_cb_context = {
		.cb = callback,
		.user_data = user_data,
		.foreach_break = false
	};

	if (callback == NULL)
	{
		LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid callback", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
		return APP_MANAGER_ERROR_INVALID_PARAMETER;
	}

	aul_app_get_running_app_info(foreach_running_app_cb_broker, &foreach_cb_context);

	return APP_MANAGER_ERROR_NONE;
}

 int app_manager_foreach_app_installed(app_manager_app_installed_cb callback, void *user_data)
{
	ail_filter_h filter;
	ail_error_e ret;

	if (callback == NULL)
	{
		LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid callback", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
		return APP_MANAGER_ERROR_INVALID_PARAMETER;
	}

	ret = ail_filter_new(&filter);
	if (ret != AIL_ERROR_OK)
	{
		return app_manager_ail_error_handler(ret, __FUNCTION__);
	}

	installed_apps_foreach_cb_context foreach_cb_context = {
		.cb = callback,
		.user_data = user_data,
	};

	ail_filter_list_appinfo_foreach(filter, foreach_installed_app_cb_broker, &foreach_cb_context);

	ail_filter_destroy(filter);
	
	return APP_MANAGER_ERROR_NONE;
}

static int app_manager_get_appinfo(const char *package, const char *property, char **value)
{
	ail_error_e ail_error;
	ail_appinfo_h appinfo;
	char *appinfo_value;
	char *appinfo_value_dup;

	ail_error = ail_get_appinfo(package, &appinfo);
	if (ail_error != AIL_ERROR_OK)
	{
		return app_manager_ail_error_handler(ail_error, __FUNCTION__);
	}

	ail_error = ail_appinfo_get_str(appinfo, property, &appinfo_value);
	if (ail_error != AIL_ERROR_OK)
	{
		ail_destroy_appinfo(appinfo);
		return app_manager_ail_error_handler(ail_error, __FUNCTION__);
	}

	appinfo_value_dup = strdup(appinfo_value);

	ail_destroy_appinfo(appinfo);

	if (appinfo_value_dup == NULL)
	{
		LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, APP_MANAGER_ERROR_OUT_OF_MEMORY);
		return APP_MANAGER_ERROR_OUT_OF_MEMORY;
	}

	*value = appinfo_value_dup;
	
	return APP_MANAGER_ERROR_NONE;
}

int app_manager_get_app_name(const char *package, char** name)
{
	if (package == NULL)
	{
		LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid package", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
		return APP_MANAGER_ERROR_INVALID_PARAMETER;
	}

	return app_manager_get_appinfo(package, AIL_PROP_NAME_STR, name);
}
 
int app_manager_get_app_icon_path(const char *package, char** icon_path)
{
	if (package == NULL)
	{
		LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid package", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
		return APP_MANAGER_ERROR_INVALID_PARAMETER;
	}

	return app_manager_get_appinfo(package, AIL_PROP_ICON_STR, icon_path);
}

int app_manager_get_app_version(const char *package, char** version)
{
	if (package == NULL)
	{
		LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid package", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
		return APP_MANAGER_ERROR_INVALID_PARAMETER;
	}

	return app_manager_get_appinfo(package, AIL_PROP_VERSION_STR, version);
}

static app_manger_event_type_e app_manager_app_list_pkgmgr_event(const char *value)
{
	if (!strcasecmp(value, "install"))
	{
		return APP_MANAGER_EVENT_INSTALLED;
	}
	else if (!strcasecmp(value, "uninstall"))
	{
		return APP_MANAGER_EVENT_UNINSTALLED;	
	}
	else if (!strcasecmp(value, "update"))
	{
		return APP_MANAGER_EVENT_UPDATED;		
	}
	else
	{
		return APP_MANAGER_ERROR_INVALID_PARAMETER;
	}
}

static int app_manager_app_list_changed_cb_broker(int id, const char *type, const char *package, const char *key, const char *val, const void *msg, void *data)
{
	static int event_id = -1;
	static app_manger_event_type_e event_type;

	if (!strcasecmp(key, "start"))
	{
		event_id = id;
		event_type = app_manager_app_list_pkgmgr_event(val);
	}
	else if (!strcasecmp(key, "end") && !strcasecmp(val, "ok") && id == event_id)
	{
		if (app_list_changed_cb != NULL && event_type >= 0)
		{
			app_list_changed_cb(event_type, package, app_list_changed_cb_data);
		}

		event_id = -1;
		event_type = -1;
	}

	return APP_MANAGER_ERROR_NONE;
}

int app_manager_set_app_list_changed_cb(app_manager_app_list_changed_cb callback, void* user_data)
{
       if (callback == NULL) 
	{
		LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
		return APP_MANAGER_ERROR_INVALID_PARAMETER;
	}

	if (app_list_changed_cb == NULL)
	{
		package_manager = pkgmgr_client_new(PC_LISTENING);

		if (package_manager == NULL)
		{
			LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, APP_MANAGER_ERROR_OUT_OF_MEMORY);
			return APP_MANAGER_ERROR_OUT_OF_MEMORY;
		}

		pkgmgr_client_listen_status(package_manager, app_manager_app_list_changed_cb_broker, NULL);
	}

	app_list_changed_cb = callback;
	app_list_changed_cb_data = user_data;

	return APP_MANAGER_ERROR_NONE;
}

int app_manager_unset_app_list_changed_cb()
{	
	if (app_list_changed_cb != NULL)
	{
		pkgmgr_client_free(package_manager);
		package_manager = NULL;
	}

	app_list_changed_cb = NULL;
	app_list_changed_cb_data = NULL;

	return APP_MANAGER_ERROR_NONE;
}