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"
33 #include "sticker_request.h"
37 static bool check_rw_mount();
39 static bool app_create(void *data)
41 /* Hook to take necessary actions before main event loop starts
42 Initialize UI resources and application's data
43 If this function returns true, the main loop of application starts
44 If this function returns false, the application is terminated */
46 if (!check_rw_mount()) {
53 char log_path[PATH_MAX];
54 char *data_path = NULL;
55 data_path = app_get_shared_data_path();
56 snprintf(log_path, sizeof(log_path), "%s/log", data_path);
61 if (access(log_path, F_OK) != 0) {
62 if (mkdir(log_path, 0755) == -1) {
63 STLOGE("directory create error");
67 create_sticker_provider_handle();
72 static bool check_battery_condition()
74 int battery_percentage = 0;
77 // check battery percentage
78 ret = device_battery_get_percent(&battery_percentage);
79 if (ret != DEVICE_ERROR_NONE)
81 STLOGW("No sync. Failed to get battery percent. error : %d", ret);
85 STLOGI("battery percent : %d", battery_percentage);
86 if (battery_percentage >= MINIMUM_BATTERY)
90 STLOGI("No sync due to insufficient battery");
95 static bool check_sync_time_condition()
97 double last_sync_time;
101 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
102 // Check whether oobe has been done
103 if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) == 0 && feature_flag > 0)
105 if (preference_get_double(LAST_SYNC_TIME, &last_sync_time) != PREFERENCE_ERROR_NONE)
107 STLOGD("Can't get last sync time.");
112 double timediff = ecore_time_unix_get() - last_sync_time;
113 STLOGD("current time : %.3f, last_sync_time : %.3f, diff : %.3f", ecore_time_unix_get(), last_sync_time, timediff);
115 if (timediff > MAX_WAIT_TIME) {
116 STLOGD("Starting manual synchronization");
118 if (false == request_show_sync_notification())
119 push_sticker_request(REQUEST_TYPE_SHOW_NOTIFICATION, NULL, NULL, NULL);
122 if (timediff > SYNC_INTERVAL)
132 #endif /* VCONFKEY_STICKER_SUPPORTED_FEATURE */
137 bool check_rw_mount()
139 char *data_path = NULL;
140 bool rw_mount = false;
141 data_path = app_get_shared_data_path();
142 if (!data_path) return false;
144 if (access(data_path, R_OK) == 0)
150 SECURE_LOGE("Can't access rw storage\n");
156 static void process_request(app_control_h app_control, char *request)
161 if (strcmp(request, "sync") == 0) {
162 bool param_error = false;
163 if (app_control_get_extra_data(app_control, "mode", &mode) != APP_CONTROL_ERROR_NONE) {
164 STLOGE("No given mode");
168 if (app_control_get_extra_data(app_control, "type", &type) != APP_CONTROL_ERROR_NONE) {
169 STLOGE("No given type");
173 STLOGI("[sync request] mode : %s, type : %s", mode, type);
178 if (check_battery_condition()) {
179 if (check_sync_time_condition()) {
180 if (preference_set_int(LAST_SYNC_STATUS, LAST_SYNC_STATUS_SYNC_NEEDED) != PREFERENCE_ERROR_NONE)
181 STLOGE("Failed to set sync status as NEEDED");
185 request_all_sticker_data(mode, type);
186 } else { // Under the sync interval time. Need to check whether last sync was succeded or not.
187 int last_sync_status = 0;
189 if (preference_get_int(LAST_SYNC_STATUS, &last_sync_status) != PREFERENCE_ERROR_NONE) {
190 STLOGE("Failed to get sync status. Default action is exit.");
194 if (last_sync_status == LAST_SYNC_STATUS_SYNC_NEEDED) {
195 STLOGD("Last sync status is NEEDED(code: %d). Retrying to sync.", last_sync_status);
198 request_all_sticker_data(mode, type);
200 STLOGD("Last sync status is SUCCESS(code: %d). Don't have to retrying sync", last_sync_status);
204 STLOGD("Not enough battery.");
208 else if (strcmp(request, "oobe") == 0) {
211 if (false == request_sticker_feature())
212 push_sticker_request(REQUEST_TYPE_FEATURE_REQ, NULL, NULL, NULL);
215 STLOGW("Unknown command : %s", request);
216 if (!is_init_sap()) {
229 static void process_auto_sync()
232 STLOGD("continue doing current job");
236 if (check_sync_time_condition()) {
237 if (check_battery_condition()) {
238 STLOGD("Starting auto synchronization");
239 if (preference_set_int(LAST_SYNC_STATUS, LAST_SYNC_STATUS_SYNC_NEEDED) != PREFERENCE_ERROR_NONE)
240 STLOGE("Failed to set sync status as NEEDED");
243 if (false == request_sticker_feature())
244 push_sticker_request(REQUEST_TYPE_FEATURE_REQ, NULL, NULL, NULL);
246 push_sticker_request(REQUEST_TYPE_AUTOSYNC, NULL, NULL, NULL);
249 STLOGI("The status of battery is low");
250 if (!get_job_progress()) {
257 if (!get_job_progress()) {
258 int last_sync_status = 0;
259 if (preference_get_int(LAST_SYNC_STATUS, &last_sync_status) != PREFERENCE_ERROR_NONE) {
260 STLOGE("Failed to get sync status. Default action is exit.");
264 if (last_sync_status == LAST_SYNC_STATUS_SYNC_NEEDED) {
265 STLOGD("Last sync status is NEEDED(code: %d). Retrying to sync.", last_sync_status);
268 if (false == request_sticker_feature())
269 push_sticker_request(REQUEST_TYPE_FEATURE_REQ, NULL, NULL, NULL);
271 push_sticker_request(REQUEST_TYPE_AUTOSYNC, NULL, NULL, NULL);
273 STLOGD("Last sync status is SUCCESS(code: %d). Don't have to retrying sync", last_sync_status);
280 static void get_sticker_feature()
282 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
283 // Check whether oobe has been done
284 int feature_flag = 0;
285 if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) == 0 && feature_flag == 0) {
286 STLOGD("Request to get sticker feature");
289 if (false == request_sticker_feature())
290 push_sticker_request(REQUEST_TYPE_FEATURE_REQ, NULL, NULL, NULL);
299 static void app_control(app_control_h app_control, void *data)
301 /* Handle the launch request. */
302 char* request = NULL;
303 char* operation = NULL;
304 char* event_value = NULL;
306 const char *event_uri = "event://tizen.system.event.battery_charger_status";
309 if (!check_rw_mount())
313 int ret = app_control_get_operation(app_control, &operation);
314 if (ret != APP_CONTROL_ERROR_NONE) {
315 STLOGW("Failed to get operation. error : %d", ret);
319 STLOGD("operation: %s", operation);
325 if (strcmp(operation, APP_CONTROL_OPERATION_LAUNCH_ON_EVENT) == 0)
327 ret = app_control_get_uri(app_control, &uri);
328 if (ret == APP_CONTROL_ERROR_NONE && uri)
330 if (strncmp(uri, event_uri, strlen(event_uri) + 1) == 0)
332 ret = app_control_get_extra_data(app_control, "battery_charger_status", &event_value);
333 if (ret == APP_CONTROL_ERROR_NONE && event_value)
335 if (string(event_value) == "connected")
337 STLOGI("The charger state is connected");
346 else if (strcmp(operation, APP_CONTROL_OPERATION_DEFAULT) == 0) {
347 res = app_control_get_extra_data(app_control, "request", &request);
348 STLOGD("get extra data result : %d, request : %s", res, request);
349 if (APP_CONTROL_ERROR_NONE == res && NULL != request) {
350 process_request(app_control, request);
354 get_sticker_feature();
359 if (NULL != operation)
366 static void app_terminate(void *data)
368 /* Release all resources. */
370 destroy_sticker_provider_handle();
374 int main(int argc, char *argv[])
378 service_app_lifecycle_callback_s event_callback;
379 memset(&event_callback, 0x0, sizeof(service_app_lifecycle_callback_s));
381 event_callback.create = (service_app_create_cb)app_create;
382 event_callback.terminate = (service_app_terminate_cb)app_terminate;
383 event_callback.app_control = (service_app_control_cb)app_control;
385 ret = service_app_main(argc, argv, &event_callback, NULL);
386 if (ret != APP_ERROR_NONE) {
387 STLOGE("app_main() is failed. err = %d", ret);