Support to request sticker sync periodically
[platform/core/uifw/capi-ui-sticker.git] / receiver / src / main.cpp
1 #include <app_common.h>
2 #include <service_app.h>
3 #include <stdlib.h>
4 #include <app_alarm.h>
5 #include <device/battery.h>
6
7 #include "main.h"
8 #include "ft.h"
9 #include "log.h"
10 #include "sync_alarm.h"
11
12 #define MINIMUM_BATTERY 15
13
14 static bool app_create(void *data)
15 {
16     /* Hook to take necessary actions before main event loop starts
17        Initialize UI resources and application's data
18        If this function returns true, the main loop of application starts
19        If this function returns false, the application is terminated */
20
21     initialize_sap();
22
23     return true;
24 }
25
26 static void app_control(app_control_h app_control, void *data)
27 {
28     /* Handle the launch request. */
29     char* request = NULL;
30     char* mode = NULL;
31     char* category = NULL;
32     char* type = NULL;
33     char* operation = NULL;
34     char* alarm_data = NULL;
35
36     int res;
37
38     // operation
39     int ret = app_control_get_operation(app_control, &operation);
40     if (ret == APP_CONTROL_ERROR_NONE) {
41         LOGD("operation: %s", operation);
42
43         if (operation && (strncmp(operation, APP_CONTROL_OPERATION_SYNC_ALARM, strlen(APP_CONTROL_OPERATION_SYNC_ALARM)) == 0)) {
44             ret = app_control_get_extra_data(app_control, APP_CONTROL_DATA_ALARM_ID, &alarm_data);
45             if (ret != APP_CONTROL_ERROR_NONE) {
46                 dlog_print(DLOG_ERROR, LOG_TAG, "Function app_control_get_extra_data() failed.");
47                 goto cleanup;
48             }
49
50             LOGD("alarm data : %s", alarm_data);
51
52             int battery_percentage = 0;
53             int ret = device_battery_get_percent(&battery_percentage);
54             if (ret == DEVICE_ERROR_NONE) {
55                 LOGD("battery percent : %d", battery_percentage);
56                 if (battery_percentage >= MINIMUM_BATTERY) {
57                     request_sticker_data("auto", "arsticker", "input");
58                     request_sticker_data("auto", "bitmoji", "input");
59                 }
60                 else {
61                     LOGD("No sync request due to insufficient battery");
62                 }
63             }
64             else {
65                LOGW("Failed to get battery percent. error : %d", ret);
66             }
67
68             goto cleanup;
69         }
70     }
71     else {
72         LOGW("Failed to get operation. error : %d", ret);
73     }
74
75     // sync request
76     res = app_control_get_extra_data(app_control, "request", &request);
77     if (APP_CONTROL_ERROR_NONE == res && NULL != request) {
78         if (strcmp(request, "sync") == 0) {
79             LOGI("[sync request] mode : %s, category : %s, type : %s", mode, category, type);
80             if (app_control_get_extra_data(app_control, "mode", &mode) != APP_CONTROL_ERROR_NONE) {
81                 LOGE("No given mode");
82                 goto cleanup;
83             }
84
85             if (app_control_get_extra_data(app_control, "category", &category) != APP_CONTROL_ERROR_NONE) {
86                 LOGE("No given category");
87                 goto cleanup;
88             }
89
90             if (app_control_get_extra_data(app_control, "type", &type) != APP_CONTROL_ERROR_NONE) {
91                 LOGE("No given type");
92                 goto cleanup;
93             }
94
95             if (mode && category && type)
96                 request_sticker_data(mode, category, type);
97         }
98         else
99         {
100             LOGW("Unknown command : %s", request);
101         }
102     }
103
104 cleanup:
105     if (NULL != operation)
106         free(operation);
107
108     if (NULL != request)
109         free(request);
110
111     if (NULL != mode)
112         free(mode);
113
114     if (NULL != category)
115         free(category);
116
117     if (NULL != type)
118         free(type);
119 }
120
121 static void app_terminate(void *data)
122 {
123     /* Release all resources. */
124     deinitialize_sap();
125 }
126
127 int main(int argc, char *argv[])
128 {
129     int ret = 0;
130
131     service_app_lifecycle_callback_s event_callback;
132     memset(&event_callback, 0x0, sizeof(service_app_lifecycle_callback_s));
133
134     event_callback.create = (service_app_create_cb)app_create;
135     event_callback.terminate = (service_app_terminate_cb)app_terminate;
136     event_callback.app_control = (service_app_control_cb)app_control;
137
138     ret = service_app_main(argc, argv, &event_callback, NULL);
139     if (ret != APP_ERROR_NONE) {
140         LOGE("app_main() is failed. err = %d", ret);
141     }
142     return ret;
143 }