Merge "Fix indentation in manifest file" into tizen_5.5
[platform/core/uifw/capi-ui-sticker.git] / receiver / src / main.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_common.h>
18 #include <service_app.h>
19 #include <stdlib.h>
20 #include <app_alarm.h>
21 #include <device/battery.h>
22 #include <vconf.h>
23
24 #include "main.h"
25 #include "ft.h"
26 #include "log.h"
27 #include "config.h"
28 #include "sync_alarm.h"
29
30 static bool app_create(void *data)
31 {
32     /* Hook to take necessary actions before main event loop starts
33        Initialize UI resources and application's data
34        If this function returns true, the main loop of application starts
35        If this function returns false, the application is terminated */
36
37     initialize_sap();
38
39     return true;
40 }
41
42 static void app_control(app_control_h app_control, void *data)
43 {
44     /* Handle the launch request. */
45     char* request = NULL;
46     char* mode = NULL;
47     char* type = NULL;
48     char* operation = NULL;
49     char* alarm_data = NULL;
50
51     int res;
52     int battery_percentage = 0;
53     bool battery_charging = false;
54
55     // operation
56     int ret = app_control_get_operation(app_control, &operation);
57     if (ret == APP_CONTROL_ERROR_NONE) {
58         LOGD("operation: %s", operation);
59
60         if (operation && (strncmp(operation, APP_CONTROL_OPERATION_SYNC_ALARM, strlen(APP_CONTROL_OPERATION_SYNC_ALARM)) == 0)) {
61             ret = app_control_get_extra_data(app_control, APP_CONTROL_DATA_ALARM_ID, &alarm_data);
62             if (ret != APP_CONTROL_ERROR_NONE) {
63                 dlog_print(DLOG_ERROR, LOG_TAG, "Function app_control_get_extra_data() failed.");
64                 goto cleanup;
65             }
66
67             LOGD("alarm data : %s", alarm_data);
68
69             int ret = device_battery_is_charging(&battery_charging);
70             if (ret != DEVICE_ERROR_NONE) {
71                 LOGW("No sync. Can't get battery charging status");
72                 goto cleanup;
73             }
74
75             if (!battery_charging) {
76                 LOGI("No sync due to no battery charging status");
77                 goto cleanup;
78             }
79
80             ret = device_battery_get_percent(&battery_percentage);
81             if (ret != DEVICE_ERROR_NONE) {
82                 LOGW("No sync. Failed to get battery percent. error : %d", ret);
83                 goto cleanup;
84             }
85
86             LOGI("battery percent : %d", battery_percentage);
87             if (battery_percentage >= MINIMUM_BATTERY) {
88                 request_sticker_data("auto", "arsticker", "input");
89                 request_sticker_data("auto", "bitmoji", "input");
90             }
91             else {
92                 LOGI("No sync due to insufficient battery");
93             }
94
95             goto cleanup;
96         }
97     }
98     else {
99         LOGW("Failed to get operation. error : %d", ret);
100     }
101
102     // sync request
103     res = app_control_get_extra_data(app_control, "request", &request);
104     if (APP_CONTROL_ERROR_NONE == res && NULL != request) {
105         if (strcmp(request, "sync") == 0) {
106             bool param_error = false;
107             if (app_control_get_extra_data(app_control, "mode", &mode) != APP_CONTROL_ERROR_NONE) {
108                 LOGE("No given mode");
109                 param_error = true;
110             }
111
112             if (app_control_get_extra_data(app_control, "type", &type) != APP_CONTROL_ERROR_NONE) {
113                 LOGE("No given type");
114                 param_error = true;
115             }
116
117             LOGI("[sync request] mode : %s, type : %s", mode, type);
118             if (param_error)
119                 goto cleanup;
120
121             if (mode && type) {
122 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
123                 int feature_flag = 0;
124                 if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) == 0) {
125                     if (feature_flag & VCONFKEY_STICKER_FEATURE_AREMOJI)
126                         request_sticker_data(mode, "arsticker", type);
127
128                     if (feature_flag & VCONFKEY_STICKER_FEATURE_BITMOJI)
129                         request_sticker_data(mode, "bitmoji", type);
130                 } else
131                     LOGW("Failed to get value of VCONFKEY_STICKER_SUPPORTED_FEATURE");
132 #else
133                 request_sticker_data(mode, "arsticker", type);
134                 request_sticker_data(mode, "bitmoji", type);
135 #endif
136             }
137         }
138         else if (strcmp(request, "oobe") == 0) {
139             LOGI("[OOBE] register sync alarm");
140             sync_alarm_register(APP_CONTROL_OPERATION_SYNC_ALARM, SYNC_ALARM_DELAY, SYNC_ALARM_INTERVAL);
141             request_sticker_feature();
142         }
143         else
144         {
145             LOGW("Unknown command : %s", request);
146         }
147     }
148
149 cleanup:
150     if (NULL != operation)
151         free(operation);
152
153     if (NULL != request)
154         free(request);
155
156     if (NULL != mode)
157         free(mode);
158
159     if (NULL != type)
160         free(type);
161 }
162
163 static void app_terminate(void *data)
164 {
165     /* Release all resources. */
166     deinitialize_sap();
167 }
168
169 int main(int argc, char *argv[])
170 {
171     int ret = 0;
172
173     service_app_lifecycle_callback_s event_callback;
174     memset(&event_callback, 0x0, sizeof(service_app_lifecycle_callback_s));
175
176     event_callback.create = (service_app_create_cb)app_create;
177     event_callback.terminate = (service_app_terminate_cb)app_terminate;
178     event_callback.app_control = (service_app_control_cb)app_control;
179
180     ret = service_app_main(argc, argv, &event_callback, NULL);
181     if (ret != APP_ERROR_NONE) {
182         LOGE("app_main() is failed. err = %d", ret);
183     }
184     return ret;
185 }