Support to request sticker sync periodically
[platform/core/uifw/capi-ui-sticker.git] / receiver / src / sync_alarm.cpp
1 /*
2  * Copyright (c) 2020 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 #include <app_alarm.h>
18 #include <app_common.h>
19 #include <stdlib.h>
20
21 #include "log.h"
22
23 static int recurring_alarm_id = -1;
24
25 static bool
26 on_foreach_registered_alarm(int alarm_id, void *user_data)
27 {
28     bool *result = (bool *)user_data;
29
30     LOGD("alarm id : %d", alarm_id);
31
32     *result = *result | 0x1;
33
34     return false;
35 }
36
37 bool sync_alarm_register(const char *operation, int delay, int period)
38 {
39     int ret;
40     app_control_h app_control = NULL;
41     char *app_id = NULL;
42     bool result = false;
43
44     ret = app_control_create(&app_control);
45     if (ret != APP_CONTROL_ERROR_NONE)
46     {
47         LOGE("Function app_control_create() failed.");
48         return false;
49     }
50
51     ret = app_control_set_operation(app_control, operation);
52     if (ret != APP_CONTROL_ERROR_NONE)
53     {
54         LOGE("Function app_control_set_operation() failed.");
55         goto cleanup;
56     }
57
58     if (app_get_id(&app_id) != APP_ERROR_NONE) {
59         goto cleanup;
60     }
61
62     LOGD("app id : %s", app_id);
63     if (!app_id) {
64         goto cleanup;
65     }
66
67     ret = app_control_set_app_id(app_control, app_id);
68     if (ret != APP_CONTROL_ERROR_NONE)
69     {
70         LOGE("Function app_control_set_app_id() failed.");
71         goto cleanup;
72     }
73
74     alarm_cancel_all();
75
76     ret = alarm_schedule_after_delay(app_control, delay, period, &recurring_alarm_id);
77     if (ret != ALARM_ERROR_NONE)
78         LOGE("Function alarm_schedule_after_delay() failed.");
79     else
80         LOGD("Function alarm_schedule_after_delay() succeed. delay : %d, interval : %d", delay, period);
81
82     result = true;
83
84 cleanup:
85     if (app_id)
86         free(app_id);
87
88     if (app_control) {
89         ret = app_control_destroy(app_control);
90         if (ret != APP_CONTROL_ERROR_NONE)
91             LOGE("Function app_control_destroy() failed.");
92         else
93             LOGD("Set recurring alarm with id: %i", recurring_alarm_id);
94     }
95
96     return result;
97 }
98
99 bool sync_alarm_exist()
100 {
101     bool result = false;
102     int ret = alarm_foreach_registered_alarm(on_foreach_registered_alarm, &result);
103     if (ret != ALARM_ERROR_NONE)
104         LOGD("Listing error : %d", ret);
105
106     return result;
107 }