3ad20b5fc5544d8e272c7ba7628a3ce8315535d0
[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             bool param_error = false;
80             if (app_control_get_extra_data(app_control, "mode", &mode) != APP_CONTROL_ERROR_NONE) {
81                 LOGE("No given mode");
82                 param_error = true;
83             }
84
85             if (app_control_get_extra_data(app_control, "category", &category) != APP_CONTROL_ERROR_NONE) {
86                 LOGE("No given category");
87                 param_error = true;
88             }
89
90             if (app_control_get_extra_data(app_control, "type", &type) != APP_CONTROL_ERROR_NONE) {
91                 LOGE("No given type");
92                 param_error = true;
93             }
94
95             LOGI("[sync request] mode : %s, category : %s, type : %s", mode, category, type);
96             if (param_error)
97                 goto cleanup;
98
99             if (mode && category && type)
100                 request_sticker_data(mode, category, type);
101         }
102         else
103         {
104             LOGW("Unknown command : %s", request);
105         }
106     }
107
108 cleanup:
109     if (NULL != operation)
110         free(operation);
111
112     if (NULL != request)
113         free(request);
114
115     if (NULL != mode)
116         free(mode);
117
118     if (NULL != category)
119         free(category);
120
121     if (NULL != type)
122         free(type);
123 }
124
125 static void app_terminate(void *data)
126 {
127     /* Release all resources. */
128     deinitialize_sap();
129 }
130
131 int main(int argc, char *argv[])
132 {
133     int ret = 0;
134
135     service_app_lifecycle_callback_s event_callback;
136     memset(&event_callback, 0x0, sizeof(service_app_lifecycle_callback_s));
137
138     event_callback.create = (service_app_create_cb)app_create;
139     event_callback.terminate = (service_app_terminate_cb)app_terminate;
140     event_callback.app_control = (service_app_control_cb)app_control;
141
142     ret = service_app_main(argc, argv, &event_callback, NULL);
143     if (ret != APP_ERROR_NONE) {
144         LOGE("app_main() is failed. err = %d", ret);
145     }
146     return ret;
147 }