Init/Deinit sync when BT attached/detached
[platform/core/uifw/capi-ui-sticker.git] / receiver / src / main.cpp
index aa196e1..6ba29ed 100644 (file)
@@ -173,9 +173,33 @@ static void process_request(app_control_h app_control, char *request)
             goto cleanup;
 
         if (mode && type) {
-            if (!is_init_sap()) {
-                initialize_sap();
-                request_all_sticker_data(mode, type);
+            if (check_battery_condition()) {
+                if (check_sync_time_condition()) {
+                    if (preference_set_int(LAST_SYNC_STATUS, LAST_SYNC_STATUS_SYNC_NEEDED) != PREFERENCE_ERROR_NONE)
+                        STLOGE("Failed to set sync status as NEEDED");
+
+                    if (!is_init_sap())
+                        initialize_sap();
+                    request_all_sticker_data(mode, type);
+                } else { // Under the sync interval time. Need to check whether last sync was succeded or not.
+                    int last_sync_status = 0;
+
+                    if (preference_get_int(LAST_SYNC_STATUS, &last_sync_status) != PREFERENCE_ERROR_NONE) {
+                        STLOGE("Failed to get sync status. Default action is exit.");
+                        goto cleanup;
+                    }
+
+                    if (last_sync_status == LAST_SYNC_STATUS_SYNC_NEEDED) {
+                        STLOGD("Last sync status is NEEDED(code: %d). Retrying to sync.", last_sync_status);
+                        if (!is_init_sap())
+                            initialize_sap();
+                        request_all_sticker_data(mode, type);
+                    } else {
+                        STLOGD("Last sync status is SUCCESS(code: %d). Don't have to retrying sync", last_sync_status);
+                    }
+                }
+            } else {
+                STLOGD("Not enough battery.");
             }
         }
     }
@@ -208,6 +232,8 @@ static void process_auto_sync()
     if (check_sync_time_condition()) {
         if (check_battery_condition()) {
             STLOGD("Starting auto synchronization");
+            if (preference_set_int(LAST_SYNC_STATUS, LAST_SYNC_STATUS_SYNC_NEEDED) != PREFERENCE_ERROR_NONE)
+                STLOGE("Failed to set sync status as NEEDED");
             initialize_sap();
             request_sticker_feature();
             request_auto_sync();
@@ -222,8 +248,21 @@ static void process_auto_sync()
     }
     else {
         if (!get_job_progress()) {
-            STLOGD("exit");
-            service_app_exit();
+            int last_sync_status = 0;
+            if (preference_get_int(LAST_SYNC_STATUS, &last_sync_status) != PREFERENCE_ERROR_NONE) {
+                STLOGE("Failed to get sync status. Default action is exit.");
+                service_app_exit();
+            }
+
+            if (last_sync_status == LAST_SYNC_STATUS_SYNC_NEEDED) {
+                STLOGD("Last sync status is NEEDED(code: %d). Retrying to sync.", last_sync_status);
+                initialize_sap();
+                request_sticker_feature();
+                request_auto_sync();
+            } else {
+                STLOGD("Last sync status is SUCCESS(code: %d). Don't have to retrying sync", last_sync_status);
+                service_app_exit();
+            }
         }
     }
 }