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 app_create(void *data)
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 */
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);
53 if (access(log_path, F_OK) != 0) {
54 if (mkdir(log_path, 0755) == -1) {
55 LOGE("directory create error");
59 create_sticker_provider_handle();
64 static bool check_battery_condition()
66 int battery_percentage = 0;
69 // check battery percentage
70 ret = device_battery_get_percent(&battery_percentage);
71 if (ret != DEVICE_ERROR_NONE)
73 LOGW("No sync. Failed to get battery percent. error : %d", ret);
77 LOGI("battery percent : %d", battery_percentage);
78 if (battery_percentage >= MINIMUM_BATTERY)
82 LOGI("No sync due to insufficient battery");
87 static bool check_sync_time_condition()
89 double last_sync_time;
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)
97 if (preference_get_double(LAST_SYNC_TIME, &last_sync_time) != PREFERENCE_ERROR_NONE)
99 LOGD("Can't get last sync time.");
104 LOGD("current time : %f, last_sync_time : %f", ecore_time_unix_get(), last_sync_time);
106 if (ecore_time_unix_get() - last_sync_time > MAX_WAIT_TIME) {
107 LOGD("Starting manual synchronization");
109 request_show_sync_notification();
112 if (ecore_time_unix_get() - last_sync_time > SYNC_INTERVAL)
122 #endif /* VCONFKEY_STICKER_SUPPORTED_FEATURE */
127 static void app_control(app_control_h app_control, void *data)
129 /* Handle the launch request. */
130 char* request = NULL;
133 char* operation = NULL;
134 char* event_value = NULL;
136 const char *event_uri = "event://tizen.system.event.battery_charger_status";
140 int ret = app_control_get_operation(app_control, &operation);
141 if (ret == APP_CONTROL_ERROR_NONE) {
142 LOGD("operation: %s", operation);
148 if (strcmp(operation, APP_CONTROL_OPERATION_LAUNCH_ON_EVENT) == 0)
150 ret = app_control_get_uri(app_control, &uri);
151 if (ret == APP_CONTROL_ERROR_NONE && uri)
153 if (strncmp(uri, event_uri, strlen(event_uri) + 1) == 0)
155 ret = app_control_get_extra_data(app_control, "battery_charger_status", &event_value);
156 if (ret == APP_CONTROL_ERROR_NONE && event_value)
158 if (string(event_value) == "connected")
160 if (!is_init_sap()) {
161 if (check_sync_time_condition()) {
162 if (check_battery_condition()) {
163 LOGD("Starting auto synchronization");
165 request_all_sticker_data("auto", "input");
169 if (!get_job_progress())
177 LOGD("continue doing current job");
188 LOGW("Failed to get operation. error : %d", ret);
192 res = app_control_get_extra_data(app_control, "request", &request);
193 if (APP_CONTROL_ERROR_NONE == res && NULL != request) {
194 if (strcmp(request, "sync") == 0) {
195 bool param_error = false;
196 if (app_control_get_extra_data(app_control, "mode", &mode) != APP_CONTROL_ERROR_NONE) {
197 LOGE("No given mode");
201 if (app_control_get_extra_data(app_control, "type", &type) != APP_CONTROL_ERROR_NONE) {
202 LOGE("No given type");
206 LOGI("[sync request] mode : %s, type : %s", mode, type);
211 if (!is_init_sap()) {
213 request_all_sticker_data(mode, type);
217 else if (strcmp(request, "oobe") == 0) {
219 request_sticker_feature();
223 LOGW("Unknown command : %s", request);
232 if (NULL != operation)
245 static void app_terminate(void *data)
247 /* Release all resources. */
249 destroy_sticker_provider_handle();
253 int main(int argc, char *argv[])
257 service_app_lifecycle_callback_s event_callback;
258 memset(&event_callback, 0x0, sizeof(service_app_lifecycle_callback_s));
260 event_callback.create = (service_app_create_cb)app_create;
261 event_callback.terminate = (service_app_terminate_cb)app_terminate;
262 event_callback.app_control = (service_app_control_cb)app_control;
264 ret = service_app_main(argc, argv, &event_callback, NULL);
265 if (ret != APP_ERROR_NONE) {
266 LOGE("app_main() is failed. err = %d", ret);