Send request to sync after receiving sticker feature
[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_event.h>
21 #include <device/battery.h>
22 #include <vconf.h>
23 #include <string>
24 #include <app_preference.h>
25 #include <Ecore.h>
26
27 #include "main.h"
28 #include "ft.h"
29 #include "log.h"
30 #include "config.h"
31 #include "receiver_preference.h"
32 #include "sticker_info.h"
33
34 using namespace std;
35
36 static bool app_create(void *data)
37 {
38     /* Hook to take necessary actions before main event loop starts
39        Initialize UI resources and application's data
40        If this function returns true, the main loop of application starts
41        If this function returns false, the application is terminated */
42
43     LOGD("");
44
45     char log_path[PATH_MAX];
46     char *data_path = NULL;
47     data_path = app_get_shared_data_path();
48     snprintf(log_path, sizeof(log_path), "%s/log", data_path);
49
50     if (data_path)
51         free(data_path);
52
53     if (access(log_path, F_OK) != 0) {
54         if (mkdir(log_path, 0755) == -1) {
55             LOGE("directory create error");
56         }
57     }
58
59     create_sticker_provider_handle();
60
61     return true;
62 }
63
64 static bool check_battery_condition()
65 {
66     int battery_percentage = 0;
67     int ret;
68
69     // check battery percentage
70     ret = device_battery_get_percent(&battery_percentage);
71     if (ret != DEVICE_ERROR_NONE)
72     {
73         LOGW("No sync. Failed to get battery percent. error : %d", ret);
74         return false;
75     }
76
77     LOGI("battery percent : %d", battery_percentage);
78     if (battery_percentage >= MINIMUM_BATTERY)
79         return true;
80     else
81     {
82         LOGI("No sync due to insufficient battery");
83         return false;
84     }
85 }
86
87 static bool check_sync_time_condition()
88 {
89     double last_sync_time;
90     int feature_flag = 0;
91     bool result = false;
92
93 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
94     // Check whether oobe has been done
95     if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) == 0 && feature_flag > 0)
96     {
97         if (preference_get_double(LAST_SYNC_TIME, &last_sync_time) != PREFERENCE_ERROR_NONE)
98         {
99             LOGD("Can't get last sync time.");
100             return true;
101         }
102
103         // compare time
104         double timediff = ecore_time_unix_get() - last_sync_time;
105         LOGD("current time : %.3f, last_sync_time : %.3f, diff : %.3f", ecore_time_unix_get(), last_sync_time, timediff);
106
107         if (timediff > MAX_WAIT_TIME) {
108             LOGD("Starting manual synchronization");
109             initialize_sap();
110             request_show_sync_notification();
111             result = false;
112         } else {
113             if (timediff > SYNC_INTERVAL)
114                 result = true;
115             else
116                 result = false;
117         }
118     }
119     else
120     {
121         result = false;
122     }
123 #endif /* VCONFKEY_STICKER_SUPPORTED_FEATURE */
124
125     return result;
126 }
127
128 bool check_rw_mount()
129 {
130     char *data_path = NULL;
131     bool rw_mount = false;
132     data_path = app_get_shared_data_path();
133     if (!data_path) return false;
134
135     if (access(data_path, R_OK) == 0)
136         rw_mount = true;
137
138     free(data_path);
139
140     if (!rw_mount) {
141         SECURE_LOGE("Can't access rw storage\n");
142     }
143
144     return rw_mount;
145 }
146
147 static void app_control(app_control_h app_control, void *data)
148 {
149     /* Handle the launch request. */
150     char* request = NULL;
151     char* mode = NULL;
152     char* type = NULL;
153     char* operation = NULL;
154     char* event_value = NULL;
155     char* uri = NULL;
156     const char *event_uri = "event://tizen.system.event.battery_charger_status";
157     int res;
158
159     if (!check_rw_mount())
160         service_app_exit();
161
162     // operation
163     int ret = app_control_get_operation(app_control, &operation);
164     if (ret == APP_CONTROL_ERROR_NONE) {
165         LOGD("operation: %s", operation);
166
167         if (!operation) {
168             goto cleanup;
169         }
170
171         if (strcmp(operation, APP_CONTROL_OPERATION_LAUNCH_ON_EVENT) == 0)
172         {
173             ret = app_control_get_uri(app_control, &uri);
174             if (ret == APP_CONTROL_ERROR_NONE && uri)
175             {
176                 if (strncmp(uri, event_uri, strlen(event_uri) + 1) == 0)
177                 {
178                     ret = app_control_get_extra_data(app_control, "battery_charger_status", &event_value);
179                     if (ret == APP_CONTROL_ERROR_NONE && event_value)
180                     {
181                         if (string(event_value) == "connected")
182                         {
183                             if (!is_init_sap()) {
184                                 if (check_sync_time_condition()) {
185                                     if (check_battery_condition()) {
186                                         LOGD("Starting auto synchronization");
187                                         initialize_sap();
188                                         request_sticker_feature();
189                                         request_auto_sync();
190                                     }
191                                 }
192                                 else {
193                                     if (!get_job_progress())
194                                     {
195                                         LOGD("exit");
196                                         service_app_exit();
197                                     }
198                                 }
199                             }
200                             else {
201                                 LOGD("continue doing current job");
202                             }
203                         }
204                         free(event_value);
205                     }
206                 }
207                 free(uri);
208             }
209         }
210     }
211     else {
212         LOGW("Failed to get operation. error : %d", ret);
213     }
214
215     // sync request
216     res = app_control_get_extra_data(app_control, "request", &request);
217     if (APP_CONTROL_ERROR_NONE == res && NULL != request) {
218         if (strcmp(request, "sync") == 0) {
219             bool param_error = false;
220             if (app_control_get_extra_data(app_control, "mode", &mode) != APP_CONTROL_ERROR_NONE) {
221                 LOGE("No given mode");
222                 param_error = true;
223             }
224
225             if (app_control_get_extra_data(app_control, "type", &type) != APP_CONTROL_ERROR_NONE) {
226                 LOGE("No given type");
227                 param_error = true;
228             }
229
230             LOGI("[sync request] mode : %s, type : %s", mode, type);
231             if (param_error)
232                 goto cleanup;
233
234             if (mode && type) {
235                 if (!is_init_sap()) {
236                     initialize_sap();
237                     request_all_sticker_data(mode, type);
238                 }
239             }
240         }
241         else if (strcmp(request, "oobe") == 0) {
242             initialize_sap();
243             request_sticker_feature();
244         }
245         else
246         {
247             LOGW("Unknown command : %s", request);
248             if (!is_init_sap())
249             {
250                 service_app_exit();
251             }
252         }
253     }
254
255 cleanup:
256     if (NULL != operation)
257         free(operation);
258
259     if (NULL != request)
260         free(request);
261
262     if (NULL != mode)
263         free(mode);
264
265     if (NULL != type)
266         free(type);
267 }
268
269 static void app_terminate(void *data)
270 {
271     /* Release all resources. */
272     LOGD("");
273     destroy_sticker_provider_handle();
274     deinitialize_sap();
275 }
276
277 int main(int argc, char *argv[])
278 {
279     int ret = 0;
280
281     service_app_lifecycle_callback_s event_callback;
282     memset(&event_callback, 0x0, sizeof(service_app_lifecycle_callback_s));
283
284     event_callback.create = (service_app_create_cb)app_create;
285     event_callback.terminate = (service_app_terminate_cb)app_terminate;
286     event_callback.app_control = (service_app_control_cb)app_control;
287
288     ret = service_app_main(argc, argv, &event_callback, NULL);
289     if (ret != APP_ERROR_NONE) {
290         LOGE("app_main() is failed. err = %d", ret);
291     }
292     return ret;
293 }