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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd.
*
* 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 <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <dlog.h>
#include <aul.h>
#include <aul_app_com.h>
#include <app_common_internal.h>
#include <alarm.h>
#include "ui_app_ambient.h"
#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "CAPI_UI_APP_AMBIENT"
#define APPID_BUFFER_MAX 512
#define SECOND 1
#define ONE_MINUTE_IN_SEC 60
#define MILLION 1000000 /* for calculating micro seconds */
typedef struct
{
int alarm_id;
int period_type;
ui_app_ambient_update_frame_cb frame_updated;
ui_app_ambient_changed_cb ambient_changed;
void *user_data;
} ambient_lifecycle_s;
static ambient_lifecycle_s ambient_lifecycle = {
.alarm_id = 0,
.period_type = UI_APP_AMBIENT_UPDATE_NONE,
.frame_updated = NULL,
.ambient_changed = NULL,
.user_data = NULL
};
static struct ambient_tick_type_info {
int interval;
int minute_base;
int hour_base;
} ambient_tick_type_infos[] = {
{0, 0, 0},
{ONE_MINUTE_IN_SEC, 1, 0},
{ONE_MINUTE_IN_SEC * 5, 5, 0},
{ONE_MINUTE_IN_SEC * 15, 15, 0},
{ONE_MINUTE_IN_SEC * 30, 30, 0}
};
static aul_app_com_connection_h __conn;
static bool __initialized = false;
static void __alarm_init(void)
{
int r = 0;
int pid = getpid();
char appid[APPID_BUFFER_MAX] = {0,};
r = aul_app_get_appid_bypid(pid, appid, APPID_BUFFER_MAX);
if (r != AUL_R_OK) {
LOGE("fail to get the appid from the pid : %d", pid);
return;
}
r = alarmmgr_init(appid);
if (r != ALARMMGR_RESULT_SUCCESS) {
LOGE("fail to alarmmgr_init : error_code : %d", r);
}
}
static int __alarm_cb(alarm_id_t id, void *data)
{
LOGD("Ambient Tick callback");
if (id == ambient_lifecycle.alarm_id)
ambient_lifecycle.frame_updated(data);
return 0;
}
static alarm_entry_t* __get_ambient_tick_alarm(ui_app_ambient_update_period_e period)
{
struct timespec current_time;
struct tm cur_tm;
struct tm next_tm;
int offset = 0;
int remain_min;
int minute_base;
int ret;
time_t next;
alarm_entry_t *alarm_info = NULL;
alarm_date_t date = {0};
alarm_info = alarmmgr_create_alarm();
if (!alarm_info)
return NULL;
clock_gettime(CLOCK_REALTIME, ¤t_time);
tzset();
localtime_r(¤t_time.tv_sec, &cur_tm);
switch(period) {
case UI_APP_AMBIENT_UPDATE_MINUTE:
offset = ONE_MINUTE_IN_SEC - cur_tm.tm_sec;
break;
case UI_APP_AMBIENT_UPDATE_FIVE_MINUTES:
case UI_APP_AMBIENT_UPDATE_FIFTEEN_MINUTES:
case UI_APP_AMBIENT_UPDATE_THIRTY_MINUTES:
/* To make a gap minutes */
minute_base = ambient_tick_type_infos[period].minute_base;
remain_min = minute_base - 1 - (cur_tm.tm_min % minute_base);
offset = (remain_min * ONE_MINUTE_IN_SEC) +
(ONE_MINUTE_IN_SEC - cur_tm.tm_sec);
break;
default:
break;
}
next = current_time.tv_sec + offset;
localtime_r(&next, &next_tm);
date.year = next_tm.tm_year + 1900;
date.month = next_tm.tm_mon + 1;
date.day = next_tm.tm_mday;
date.hour = next_tm.tm_hour;
date.min = next_tm.tm_min;
date.sec = next_tm.tm_sec;
alarmmgr_set_type(alarm_info, ALARM_TYPE_VOLATILE);
ret = alarmmgr_set_time(alarm_info, date);
if (ret != ALARMMGR_RESULT_SUCCESS) {
LOGE("Failed to alarmmgr_set_time : %d", ret);
alarmmgr_free_alarm(alarm_info);
return NULL;
}
ret = alarmmgr_set_repeat_mode(alarm_info, ALARM_REPEAT_MODE_REPEAT,
ambient_tick_type_infos[period].interval);
if (ret != ALARMMGR_RESULT_SUCCESS) {
LOGE("Failed to alarmmgr_set_time : %d", ret);
alarmmgr_free_alarm(alarm_info);
return NULL;
}
LOGD("Next alarm tick [%d:%d:%d]", date.hour, date.min, date.sec);
return alarm_info;
}
static int __set_ambient_tick_cb()
{
int r;
char appid_buf[APPID_BUFFER_MAX] = {0, };
alarm_entry_t *alarm_info = NULL;
if (ambient_lifecycle.period_type == UI_APP_AMBIENT_UPDATE_NONE &&
ambient_lifecycle.alarm_id == 0) {
LOGI("DEFAULT period is set [FIFTEEN_MINUTES]");
ambient_lifecycle.period_type = UI_APP_AMBIENT_UPDATE_FIFTEEN_MINUTES;
}
alarm_info = __get_ambient_tick_alarm(ambient_lifecycle.period_type);
if (!alarm_info)
return -1;
alarmmgr_set_cb(__alarm_cb, ambient_lifecycle.user_data);
__alarm_init();
if (aul_app_get_appid_bypid(
getpid(), appid_buf, sizeof(appid_buf)) != AUL_R_OK) {
LOGE("Failed to get appid (%d)", getpid());
alarmmgr_free_alarm(alarm_info);
return -1;
}
r = alarmmgr_add_alarm_with_localtime(alarm_info, appid_buf,
&ambient_lifecycle.alarm_id);
if (r < 0)
LOGE("fail to alarmmgr_add_alarm_with_localtime : error_code : %d", r);
ambient_lifecycle.frame_updated(ambient_lifecycle.user_data);
alarmmgr_free_alarm(alarm_info);
return r;
}
int __on_change_signal(const char *endpoint, aul_app_com_result_e e,
bundle *envelope, void *user_data)
{
LOGE("__on_change_signal");
bundle *b = NULL;
char *extra;
char *mode;
bundle_get_str(envelope, "__AMBIENT_MODE__", &mode);
bundle_get_str(envelope, "__AMBIENT_EXTRA__", &extra);
b = bundle_decode((bundle_raw *)extra, strlen(extra));
if (ambient_lifecycle.ambient_changed)
ambient_lifecycle.ambient_changed((bool)atoi(mode), b, user_data);
if (b)
bundle_free(b);
return 0;
}
static int __set_ambient_changed_cb()
{
int r;
if (__conn) {
LOGD("Already set ambient changed cb");
return 0;
}
LOGD("Set ambient changed cb");
r = aul_app_com_create("ui.ambientchange", NULL, __on_change_signal,
ambient_lifecycle.user_data, &__conn);
if (r != AUL_R_OK) {
LOGE("Failed to listen 'ui.ambientchange' signal");
}
return r;
}
int ui_app_ambient_set_lifecycle(ui_app_ambient_lifecycle_callback_s *lifecycle,
void *user_data)
{
int ret = -1;
if (!lifecycle) {
LOGE("Invalid parameter");
return ret;
}
if (__initialized) {
LOGW("Lifecycle is already set");
return 0;
}
ambient_lifecycle.frame_updated = lifecycle->frame_updated;
ambient_lifecycle.ambient_changed = lifecycle->ambient_changed;
ambient_lifecycle.user_data = user_data;
ret = __set_ambient_tick_cb();
if (ret != 0) {
LOGE("Failed to set ambient tick cb : %d", ret);
return ret;
}
ret = __set_ambient_changed_cb();
if (ret != 0) {
LOGE("Failed to set ambient changed cb : %d", ret);
ui_app_ambient_unset_lifecycle();
return ret;
}
__initialized = true;
return 0;
}
void ui_app_ambient_unset_lifecycle()
{
if (__conn) {
if (aul_app_com_leave(__conn) < 0)
LOGE("failed to leave app com disable");
__conn = NULL;
}
if (ambient_lifecycle.alarm_id) {
alarmmgr_remove_alarm(ambient_lifecycle.alarm_id);
ambient_lifecycle.alarm_id = 0;
}
if (ambient_lifecycle.ambient_changed)
ambient_lifecycle.ambient_changed = NULL;
if (ambient_lifecycle.frame_updated)
ambient_lifecycle.frame_updated = NULL;
if (ambient_lifecycle.user_data)
ambient_lifecycle.user_data = NULL;
__initialized = false;
}
int ui_app_ambient_set_update_period(ui_app_ambient_update_period_e period)
{
int ret = 0;
LOGD("set update period : %d", period);
ambient_lifecycle.period_type = period;
if (ambient_lifecycle.alarm_id) {
alarmmgr_remove_alarm(ambient_lifecycle.alarm_id);
ambient_lifecycle.alarm_id = 0;
if (ambient_lifecycle.period_type != UI_APP_AMBIENT_UPDATE_NONE)
ret = __set_ambient_tick_cb();
}
return ret;
}
int ui_app_ambient_get_update_period(ui_app_ambient_update_period_e *period)
{
if (!period) {
LOGE("Invalid parameter");
return -1;
}
*period = ambient_lifecycle.period_type;
return 0;
}
int ui_app_ambient_notify_event(ui_app_ambient_event_e event, bundle *extra)
{
char buf[32];
char appid_buf[APPID_BUFFER_MAX] = {0, };
int ret;
bundle *envelope;
if (extra == NULL) {
LOGE("extra data is null");
envelope = bundle_create();
} else {
envelope = bundle_dup(extra);
}
if (envelope == NULL) {
LOGE("out of memory");
return -1;
}
snprintf(buf, sizeof(buf), "%d", event);
ret = bundle_add_str(envelope, "__APP_AMBIENT_EVENT__", buf);
if (ret != BUNDLE_ERROR_NONE) {
LOGE("bundle_add_str returns false");
bundle_free(envelope);
return -1;
}
if (aul_app_get_appid_bypid(
getpid(), appid_buf, sizeof(appid_buf)) != AUL_R_OK) {
LOGE("Failed to get appid (%d)", getpid());
bundle_free(envelope);
return -1;
}
ret = bundle_add_str(envelope, "__APP_AMBIENT_SENDER__", appid_buf);
if (ret != BUNDLE_ERROR_NONE) {
LOGE("bundle_add_str returns false");
bundle_free(envelope);
return -1;
}
ret = aul_app_com_send("aod.ambientevent", envelope);
if (ret != AUL_R_OK) {
LOGE("Failed aul_app_com_send");
bundle_free(envelope);
return -1;
}
LOGI("Send AOD Event(%d) done", event);
bundle_free(envelope);
return 0;
}
|