summaryrefslogtreecommitdiff
path: root/app_common/app_event.c
blob: fe32e892d1ca4dbb5e3cdfe687eef56d45163775 (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
/*
 * Copyright (c) 2011 - 2016 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 <string.h>

#include <vconf-internal-keys.h>
#include <app_common.h>
#include <appcore_base.h>
#include <app_internal.h>

struct app_event_info {
	app_event_type_e type;
	void *value;
};

enum appcore_rm {
	APPCORE_RM_UNKNOWN,
	APPCORE_RM_PORTRAIT_NORMAL,
	APPCORE_RM_PORTRAIT_REVERSE,
	APPCORE_RM_LANDSCAPE_NORMAL,
	APPCORE_RM_LANDSCAPE_REVERSE,
};

app_device_orientation_e app_convert_appcore_rm(enum appcore_rm rm)
{
	app_device_orientation_e dev_orientation;

	switch (rm) {
	case APPCORE_RM_PORTRAIT_NORMAL:
		dev_orientation = APP_DEVICE_ORIENTATION_0;
		break;
	case APPCORE_RM_PORTRAIT_REVERSE:
		dev_orientation = APP_DEVICE_ORIENTATION_180;
		break;
	case APPCORE_RM_LANDSCAPE_NORMAL:
		dev_orientation = APP_DEVICE_ORIENTATION_270;
		break;
	case APPCORE_RM_LANDSCAPE_REVERSE:
		dev_orientation = APP_DEVICE_ORIENTATION_90;
		break;
	default:
		dev_orientation = APP_DEVICE_ORIENTATION_0;
		break;
	}

	return dev_orientation;
}

static int __app_convert_low_memory(void *val)
{
	switch (*(int *)val) {
	case VCONFKEY_SYSMAN_LOW_MEMORY_NORMAL:
		return APP_EVENT_LOW_MEMORY_NORMAL;
	case VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING:
		return APP_EVENT_LOW_MEMORY_SOFT_WARNING;
	case VCONFKEY_SYSMAN_LOW_MEMORY_HARD_WARNING:
		return APP_EVENT_LOW_MEMORY_HARD_WARNING;
	default:
		return -1;
	}
}

static int __app_convert_low_battery(void *val)
{
	switch (*(int *)val) {
	case VCONFKEY_SYSMAN_BAT_POWER_OFF:
		return APP_EVENT_LOW_BATTERY_POWER_OFF;
	case VCONFKEY_SYSMAN_BAT_CRITICAL_LOW:
		return APP_EVENT_LOW_BATTERY_CRITICAL_LOW;
	default:
		return -1;
	}
}

int app_event_get_low_memory_status(app_event_info_h event_info, app_event_low_memory_status_e *status)
{
	int ret;

	if (event_info == NULL || status == NULL)
		return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "null parameter");

	if (event_info->type != APP_EVENT_LOW_MEMORY)
		return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "event type mismatching");

	ret = __app_convert_low_memory(event_info->value);
	if (ret < 0)
		return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "invalid event info");

	*status = ret;

	return APP_ERROR_NONE;
}

int app_event_get_low_battery_status(app_event_info_h event_info, app_event_low_battery_status_e *status)
{
	int ret;

	if (event_info == NULL || status == NULL)
		return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "null parameter");

	if (event_info->type != APP_EVENT_LOW_BATTERY)
		return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "event type mismatching");

	ret = __app_convert_low_battery(event_info->value);
	if (ret < 0)
		return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "invalid event info");

	*status = ret;

	return APP_ERROR_NONE;
}

int app_event_get_language(app_event_info_h event_info, char **lang)
{
	if (event_info == NULL || lang == NULL)
		return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "null parameter");

	if (event_info->type != APP_EVENT_LANGUAGE_CHANGED)
		return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "event type mismatching");

	*lang = strdup(event_info->value);

	return APP_ERROR_NONE;
}

int app_event_get_region_format(app_event_info_h event_info, char **region)
{
	if (event_info == NULL || region == NULL)
		return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "null parameter");

	if (event_info->type != APP_EVENT_REGION_FORMAT_CHANGED)
		return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "event type mismatching");

	*region = strdup(event_info->value);

	return APP_ERROR_NONE;
}

int app_event_get_device_orientation(app_event_info_h event_info, app_device_orientation_e *orientation)
{
	if (event_info == NULL || orientation == NULL)
		return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "null parameter");

	if (event_info->type != APP_EVENT_DEVICE_ORIENTATION_CHANGED)
		return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "event type mismatching");

	*orientation = app_convert_appcore_rm(*(enum appcore_base_rm *)(event_info->value));

	return APP_ERROR_NONE;
}

int app_event_get_suspended_state(app_event_info_h event_info, app_suspended_state_e *state)
{
	if (event_info == NULL || state == NULL)
		return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "null parameter");

	if (event_info->type != APP_EVENT_SUSPENDED_STATE_CHANGED)
		return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "event type mismatching");

	if (*(enum appcore_base_suspended_state *)(event_info->value) == APPCORE_BASE_SUSPENDED_STATE_WILL_ENTER_SUSPEND)
		*state = APP_SUSPENDED_STATE_WILL_ENTER;
	else if (*(enum appcore_base_suspended_state *)(event_info->value) == APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND)
		*state = APP_SUSPENDED_STATE_DID_EXIT;

	return APP_ERROR_NONE;
}