2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <app_common.h>
18 #include <service_app.h>
20 #include <app_event.h>
21 #include <device/battery.h>
24 #include <app_preference.h>
31 #include "receiver_preference.h"
32 #include "sticker_info.h"
36 static bool check_rw_mount();
38 static bool app_create(void *data)
40 /* Hook to take necessary actions before main event loop starts
41 Initialize UI resources and application's data
42 If this function returns true, the main loop of application starts
43 If this function returns false, the application is terminated */
45 if (!check_rw_mount()) {
52 char log_path[PATH_MAX];
53 char *data_path = NULL;
54 data_path = app_get_shared_data_path();
55 snprintf(log_path, sizeof(log_path), "%s/log", data_path);
60 if (access(log_path, F_OK) != 0) {
61 if (mkdir(log_path, 0755) == -1) {
62 STLOGE("directory create error");
66 create_sticker_provider_handle();
71 static bool check_battery_condition()
73 int battery_percentage = 0;
76 // check battery percentage
77 ret = device_battery_get_percent(&battery_percentage);
78 if (ret != DEVICE_ERROR_NONE)
80 STLOGW("No sync. Failed to get battery percent. error : %d", ret);
84 STLOGI("battery percent : %d", battery_percentage);
85 if (battery_percentage >= MINIMUM_BATTERY)
89 STLOGI("No sync due to insufficient battery");
94 static bool check_sync_time_condition()
96 double last_sync_time;
100 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
101 // Check whether oobe has been done
102 if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) == 0 && feature_flag > 0)
104 if (preference_get_double(LAST_SYNC_TIME, &last_sync_time) != PREFERENCE_ERROR_NONE)
106 STLOGD("Can't get last sync time.");
111 double timediff = ecore_time_unix_get() - last_sync_time;
112 STLOGD("current time : %.3f, last_sync_time : %.3f, diff : %.3f", ecore_time_unix_get(), last_sync_time, timediff);
114 if (timediff > MAX_WAIT_TIME) {
115 STLOGD("Starting manual synchronization");
117 request_show_sync_notification();
120 if (timediff > SYNC_INTERVAL)
130 #endif /* VCONFKEY_STICKER_SUPPORTED_FEATURE */
135 bool check_rw_mount()
137 char *data_path = NULL;
138 bool rw_mount = false;
139 data_path = app_get_shared_data_path();
140 if (!data_path) return false;
142 if (access(data_path, R_OK) == 0)
148 SECURE_LOGE("Can't access rw storage\n");
154 static void process_request(app_control_h app_control, char *request)
159 if (strcmp(request, "sync") == 0) {
160 bool param_error = false;
161 if (app_control_get_extra_data(app_control, "mode", &mode) != APP_CONTROL_ERROR_NONE) {
162 STLOGE("No given mode");
166 if (app_control_get_extra_data(app_control, "type", &type) != APP_CONTROL_ERROR_NONE) {
167 STLOGE("No given type");
171 STLOGI("[sync request] mode : %s, type : %s", mode, type);
176 if (check_battery_condition()) {
177 if (check_sync_time_condition()) {
178 if (preference_set_int(LAST_SYNC_STATUS, LAST_SYNC_STATUS_SYNC_NEEDED) != PREFERENCE_ERROR_NONE)
179 STLOGE("Failed to set sync status as NEEDED");
183 request_all_sticker_data(mode, type);
184 } else { // Under the sync interval time. Need to check whether last sync was succeded or not.
185 int last_sync_status = 0;
187 if (preference_get_int(LAST_SYNC_STATUS, &last_sync_status) != PREFERENCE_ERROR_NONE) {
188 STLOGE("Failed to get sync status. Default action is exit.");
192 if (last_sync_status == LAST_SYNC_STATUS_SYNC_NEEDED) {
193 STLOGD("Last sync status is NEEDED(code: %d). Retrying to sync.", last_sync_status);
196 request_all_sticker_data(mode, type);
198 STLOGD("Last sync status is SUCCESS(code: %d). Don't have to retrying sync", last_sync_status);
202 STLOGD("Not enough battery.");
206 else if (strcmp(request, "oobe") == 0) {
208 request_sticker_feature();
211 STLOGW("Unknown command : %s", request);
212 if (!is_init_sap()) {
225 static void process_auto_sync()
228 STLOGD("continue doing current job");
232 if (check_sync_time_condition()) {
233 if (check_battery_condition()) {
234 STLOGD("Starting auto synchronization");
235 if (preference_set_int(LAST_SYNC_STATUS, LAST_SYNC_STATUS_SYNC_NEEDED) != PREFERENCE_ERROR_NONE)
236 STLOGE("Failed to set sync status as NEEDED");
238 request_sticker_feature();
242 STLOGI("The status of battery is low");
243 if (!get_job_progress()) {
250 if (!get_job_progress()) {
251 int last_sync_status = 0;
252 if (preference_get_int(LAST_SYNC_STATUS, &last_sync_status) != PREFERENCE_ERROR_NONE) {
253 STLOGE("Failed to get sync status. Default action is exit.");
257 if (last_sync_status == LAST_SYNC_STATUS_SYNC_NEEDED) {
258 STLOGD("Last sync status is NEEDED(code: %d). Retrying to sync.", last_sync_status);
260 request_sticker_feature();
263 STLOGD("Last sync status is SUCCESS(code: %d). Don't have to retrying sync", last_sync_status);
270 static void get_sticker_feature()
272 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
273 // Check whether oobe has been done
274 int feature_flag = 0;
275 if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) == 0 && feature_flag == 0) {
276 STLOGD("Request to get sticker feature");
278 request_sticker_feature();
287 static void app_control(app_control_h app_control, void *data)
289 /* Handle the launch request. */
290 char* request = NULL;
291 char* operation = NULL;
292 char* event_value = NULL;
294 const char *event_uri = "event://tizen.system.event.battery_charger_status";
297 if (!check_rw_mount())
301 int ret = app_control_get_operation(app_control, &operation);
302 if (ret != APP_CONTROL_ERROR_NONE) {
303 STLOGW("Failed to get operation. error : %d", ret);
307 STLOGD("operation: %s", operation);
313 if (strcmp(operation, APP_CONTROL_OPERATION_LAUNCH_ON_EVENT) == 0)
315 ret = app_control_get_uri(app_control, &uri);
316 if (ret == APP_CONTROL_ERROR_NONE && uri)
318 if (strncmp(uri, event_uri, strlen(event_uri) + 1) == 0)
320 ret = app_control_get_extra_data(app_control, "battery_charger_status", &event_value);
321 if (ret == APP_CONTROL_ERROR_NONE && event_value)
323 if (string(event_value) == "connected")
325 STLOGI("The charger state is connected");
334 else if (strcmp(operation, APP_CONTROL_OPERATION_DEFAULT) == 0) {
335 res = app_control_get_extra_data(app_control, "request", &request);
336 STLOGD("get extra data result : %d, request : %s", res, request);
337 if (APP_CONTROL_ERROR_NONE == res && NULL != request) {
338 process_request(app_control, request);
342 get_sticker_feature();
347 if (NULL != operation)
354 static void app_terminate(void *data)
356 /* Release all resources. */
358 destroy_sticker_provider_handle();
362 int main(int argc, char *argv[])
366 service_app_lifecycle_callback_s event_callback;
367 memset(&event_callback, 0x0, sizeof(service_app_lifecycle_callback_s));
369 event_callback.create = (service_app_create_cb)app_create;
370 event_callback.terminate = (service_app_terminate_cb)app_terminate;
371 event_callback.app_control = (service_app_control_cb)app_control;
373 ret = service_app_main(argc, argv, &event_callback, NULL);
374 if (ret != APP_ERROR_NONE) {
375 STLOGE("app_main() is failed. err = %d", ret);