Wake up and send sync request when charging 61/229061/8
authorJihoon Kim <jihoon48.kim@samsung.com>
Fri, 27 Mar 2020 10:38:13 +0000 (19:38 +0900)
committerJihoon Kim <jihoon48.kim@samsung.com>
Tue, 7 Apr 2020 07:54:03 +0000 (16:54 +0900)
Change-Id: Iedf39ef144311eac828479f2a9d716bf82b7089e
Signed-off-by: Jihoon Kim <jihoon48.kim@samsung.com>
packaging/capi-ui-sticker.spec
receiver/CMakeLists.txt
receiver/inc/config.h
receiver/inc/ft.h
receiver/inc/sync_alarm.h [deleted file]
receiver/src/ft.cpp
receiver/src/main.cpp
receiver/src/sync_alarm.cpp [deleted file]
receiver/tizen-manifest.xml

index 377419e..7d6cc1d 100644 (file)
@@ -29,11 +29,12 @@ BuildRequires:  pkgconfig(sqlite3)
 BuildRequires:  pkgconfig(capi-base-common)
 BuildRequires:  pkgconfig(capi-appfw-service-application)
 BuildRequires:  pkgconfig(capi-message-port)
-BuildRequires:  pkgconfig(capi-appfw-alarm)
+BuildRequires:  pkgconfig(capi-appfw-preference)
 BuildRequires:  pkgconfig(capi-system-device)
 BuildRequires:  pkgconfig(sap-client-stub-api)
 BuildRequires:  pkgconfig(vconf)
 BuildRequires:  pkgconfig(capi-media-thumbnail-util)
+BuildRequires:  pkgconfig(ecore)
 BuildRequires:  hash-signer
 
 Requires(post): signing-client
index d415064..c9f9fc4 100644 (file)
@@ -7,7 +7,6 @@ SET(SRCS
        src/ft.cpp
        src/sticker_info.cpp
        src/message.cpp
-       src/sync_alarm.cpp
 )
 
 INCLUDE(FindPkgConfig)
@@ -17,13 +16,14 @@ pkg_check_modules(pkgs_test REQUIRED
     capi-appfw-service-application
     capi-message-port
     capi-appfw-app-common
-    capi-appfw-alarm
     capi-appfw-event
+    capi-appfw-preference
     capi-system-device
     sap-client-stub-api
     json-glib-1.0
     vconf
     capi-media-thumbnail-util
+    ecore
 )
 
 INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR}/receiver/inc)
index 454bd5d..10b742f 100644 (file)
@@ -20,8 +20,7 @@
 #define ACCESSORY_SERVICE_PROFILE_ID "/sample/filetransfersender"
 #define ACCESSORY_SERVICE_CHANNEL_ID 107
 
-#define SYNC_ALARM_DELAY 0
-#define SYNC_ALARM_INTERVAL 12*60*60
+#define SYNC_INTERVAL 12*60*60
 
 #define REMOTE_APP_ID "com.samsung.w-input-selector"
 #define MESSAGE_PORT_REMOTE_NAME REMOTE_APP_ID"_msg_port_rcv"
index c20f0af..90d506f 100644 (file)
@@ -29,9 +29,13 @@ void     reject_file(void);
 void     accept_file(void);
 gboolean initialize_sap(void);
 void deinitialize_sap(void);
+gboolean is_init_sap();
 
+void request_all_sticker_data(const char *mode, const char *type);
 bool request_sticker_data(const char *mode, const char *category, const char *type);
 void request_sticker_feature();
 void send_disconnect_message();
 
+bool get_job_progress();
+
 #endif /* __FT_H__ */
diff --git a/receiver/inc/sync_alarm.h b/receiver/inc/sync_alarm.h
deleted file mode 100644 (file)
index 8f025ca..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <glib.h>
-
-#ifndef __ALARM_H__
-#define __ALARM_H__
-
-#define APP_CONTROL_OPERATION_SYNC_ALARM "http://tizen.org/appcontrol/operation/sticker_sync_alarm"
-
-bool sync_alarm_register(const char *operation, int delay, int period);
-bool sync_alarm_exist();
-
-#endif /* __ALARM_H__ */
index 2541bec..aaef4c3 100644 (file)
@@ -33,6 +33,8 @@
 #include <vconf.h>
 #include <queue>
 #include <thumbnail_util.h>
+#include <Ecore.h>
+#include <app_preference.h>
 
 #include "ft.h"
 #include "log.h"
@@ -40,8 +42,8 @@
 #include "../inc/sticker_data.h"
 #include "sticker_request.h"
 #include "message.h"
-#include "sync_alarm.h"
 #include "config.h"
+#include "receiver_preference.h"
 
 #define STICKER_SYNC_FEATURE_REQ "sticker-sync-feature-req"
 #define STICKER_SYNC_FEATURE_RSP "sticker-sync-feature-rsp"
@@ -84,20 +86,37 @@ static int t_id = 0;
 static int rec_file_cnt_in_group = 0;
 static int total_file_count_in_group = 0;
 static int sync_success_cnt = 0;
+static gboolean job_progress = FALSE;
 
-static void set_first_sync_complete()
+static void save_last_sync_time()
+{
+    // save last sync time in preference
+    double current_time = ecore_time_get();
+    if (preference_set_double(LAST_SYNC_TIME, current_time) == PREFERENCE_ERROR_NONE) {
+        LOGI("Succeed to set last sync time : %f", current_time);
+    }
+    else {
+        LOGW("Failed to set last sync time");
+    }
+}
+
+static void set_sync_complete()
 {
     int complete_flags = 0;
     if (vconf_get_int(VCONFKEY_STICKER_SYNC_COMPLETE, &complete_flags) == 0 && complete_flags == 0) {
+        // first sync
         if (vconf_set_int(VCONFKEY_STICKER_SYNC_COMPLETE, VCONFKEY_STICKER_SYNC_COMPLETE_DONE) == 0)
             LOGD("Succeed to set sync complete");
         else
             LOGW("Fail to set sync complete");
     }
+
+    save_last_sync_time();
 }
 
 static void set_sync_progressing(gboolean flag)
 {
+    job_progress = flag;
 #ifdef VCONFKEY_STICKER_SYNC_STATE
     LOGD("sync progressing : %d", flag);
     if (vconf_set_int(VCONFKEY_STICKER_SYNC_STATE, flag ? VCONFKEY_STICKER_SYNC_STATE_IN_PROGRESS :  VCONFKEY_STICKER_SYNC_STATE_WAITING) == 0)
@@ -280,6 +299,11 @@ static void __set_file_transfer_cb(sap_file_transaction_h file_socket)
     sap_file_transfer_set_done_cb(file_socket, _on_transfer_completed, NULL);
 }
 
+bool get_job_progress()
+{
+    return job_progress;
+}
+
 void accept_file()
 {
     int ret;
@@ -338,6 +362,7 @@ bool request_sticker_data(const char *mode, const char *category, const char *ty
     JsonObject *j_object = NULL;
 
     if (!priv_data.socket) {
+        job_progress = TRUE;
         StickerRequest pending_request;
         pending_request.req_type = REQUEST_TYPE_SYNC;
         pending_request.mode = string(mode ? mode : "manual");
@@ -383,11 +408,32 @@ bool request_sticker_data(const char *mode, const char *category, const char *ty
     return result;
 }
 
+void request_all_sticker_data(const char *mode, const char *type)
+{
+#ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
+    int feature_flag = 0;
+    if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) == 0)
+    {
+        if (feature_flag & VCONFKEY_STICKER_FEATURE_AREMOJI)
+            request_sticker_data(mode, "arsticker", type);
+
+        if (feature_flag & VCONFKEY_STICKER_FEATURE_BITMOJI)
+            request_sticker_data(mode, "bitmoji", type);
+    }
+    else
+        LOGW("Failed to get value of VCONFKEY_STICKER_SUPPORTED_FEATURE");
+#else
+    request_sticker_data(mode, "arsticker", type);
+    request_sticker_data(mode, "bitmoji", type);
+#endif
+}
+
 void request_sticker_feature()
 {
     JsonObject *j_object = NULL;
 
     if (!priv_data.socket) {
+        job_progress = TRUE;
         StickerRequest pending_request;
         pending_request.req_type = REQUEST_TYPE_FEATURE_REQ;
         ReqQueue.push(pending_request);
@@ -407,6 +453,9 @@ void request_sticker_feature()
     if (_send_json_data(j_object) == FALSE) {
         LOGE("Failed to send STICKER_SYNC_FEATURE_REQ");
     }
+    else {
+        job_progress = TRUE;
+    }
 
     json_object_unref(j_object);
 
@@ -576,6 +625,7 @@ on_data_received(sap_socket_h socket, unsigned short int channel_id, unsigned in
 #endif
 
         if (!process_request_queue()) {
+            job_progress = FALSE;
             send_disconnect_message();
             service_app_exit();
         }
@@ -604,6 +654,10 @@ on_data_received(sap_socket_h socket, unsigned short int channel_id, unsigned in
         if (result_code != SYNC_START_RSP_SUCCESS) {
             set_sync_progressing(FALSE);
             if (!process_request_queue()) {
+                if (result_code == SYNC_START_RSP_NO_STICKER)
+                    save_last_sync_time();
+
+                job_progress = FALSE;
                 send_disconnect_message();
                 service_app_exit();
             }
@@ -689,7 +743,7 @@ on_data_received(sap_socket_h socket, unsigned short int channel_id, unsigned in
                 {
                     if (current_request.category == string("arsticker"))
                     {
-                        set_first_sync_complete();
+                        set_sync_complete();
                         set_sync_progressing(FALSE);
                     }
                 }
@@ -697,7 +751,7 @@ on_data_received(sap_socket_h socket, unsigned short int channel_id, unsigned in
                 {
                     if (current_request.category == string("bitmoji"))
                     {
-                        set_first_sync_complete();
+                        set_sync_complete();
                         set_sync_progressing(FALSE);
                     }
                 }
@@ -721,13 +775,9 @@ on_data_received(sap_socket_h socket, unsigned short int channel_id, unsigned in
         send_message("sync_stop_result", reason.c_str());
 
         if (!process_request_queue()) {
-            if (sync_success_cnt > 0 && current_request.mode == string("manual")) {
-                if (!sync_alarm_exist())
-                    sync_alarm_register(APP_CONTROL_OPERATION_SYNC_ALARM, SYNC_ALARM_DELAY, SYNC_ALARM_INTERVAL);
-            }
-
             sync_success_cnt = 0;
 
+            job_progress = FALSE;
             send_disconnect_message();
             service_app_exit();
         }
@@ -1012,6 +1062,11 @@ gboolean initialize_sap(void)
 {
     sap_agent_h agent = NULL;
 
+    if (priv_data.agent) {
+        LOGW("duplicate initialize");
+        return FALSE;
+    }
+
     sap_agent_create(&agent);
 
     priv_data.agent = agent;
@@ -1025,6 +1080,7 @@ gboolean initialize_sap(void)
 
 void deinitialize_sap(void)
 {
+    LOGD("deinitialize");
     if (priv_data.agent) {
         int ret = sap_agent_deinitialize(priv_data.agent, on_agent_deinitialized, NULL);
         switch (ret) {
@@ -1046,3 +1102,8 @@ void deinitialize_sap(void)
         priv_data.agent = NULL;
     }
 }
+
+gboolean is_init_sap()
+{
+    return priv_data.agent ? TRUE : FALSE;
+}
\ No newline at end of file
index aa65584..2006985 100644 (file)
 #include <app_common.h>
 #include <service_app.h>
 #include <stdlib.h>
-#include <app_alarm.h>
+#include <app_event.h>
 #include <device/battery.h>
 #include <vconf.h>
+#include <string>
+#include <app_preference.h>
+#include <Ecore.h>
 
 #include "main.h"
 #include "ft.h"
 #include "log.h"
 #include "config.h"
-#include "sync_alarm.h"
+#include "receiver_preference.h"
+
+using namespace std;
 
 static bool app_create(void *data)
 {
@@ -34,11 +39,66 @@ static bool app_create(void *data)
        If this function returns true, the main loop of application starts
        If this function returns false, the application is terminated */
 
-    initialize_sap();
+    LOGD("");
 
     return true;
 }
 
+static bool check_battery_condition()
+{
+    int battery_percentage = 0;
+    int ret;
+
+    // check battery percentage
+    ret = device_battery_get_percent(&battery_percentage);
+    if (ret != DEVICE_ERROR_NONE)
+    {
+        LOGW("No sync. Failed to get battery percent. error : %d", ret);
+        return false;
+    }
+
+    LOGI("battery percent : %d", battery_percentage);
+    if (battery_percentage >= MINIMUM_BATTERY)
+        return true;
+    else
+    {
+        LOGI("No sync due to insufficient battery");
+        return false;
+    }
+}
+
+static bool check_sync_time_condition()
+{
+    double last_sync_time;
+    int feature_flag = 0;
+    bool result = false;
+
+#ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
+    // Check whether oobe has been done
+    if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) == 0 && feature_flag > 0)
+    {
+        if (preference_get_double(LAST_SYNC_TIME, &last_sync_time) != PREFERENCE_ERROR_NONE)
+        {
+            LOGD("Can't get last sync time.");
+            return true;
+        }
+
+        // compare time
+        LOGD("current time : %f, last_sync_time : %f", ecore_time_get(), last_sync_time);
+        if (ecore_time_get() - last_sync_time > SYNC_INTERVAL)
+            result = true;
+        else
+            result = false;
+    }
+    else
+    {
+        result = false;
+    }
+#endif /* VCONFKEY_STICKER_SUPPORTED_FEATURE */
+
+    return result;
+}
+
 static void app_control(app_control_h app_control, void *data)
 {
     /* Handle the launch request. */
@@ -46,53 +106,56 @@ static void app_control(app_control_h app_control, void *data)
     char* mode = NULL;
     char* type = NULL;
     char* operation = NULL;
-    char* alarm_data = NULL;
-
+    char* event_value = NULL;
+    char* uri = NULL;
+    const char *event_uri = "event://tizen.system.event.battery_charger_status";
     int res;
-    int battery_percentage = 0;
-    bool battery_charging = false;
 
     // operation
     int ret = app_control_get_operation(app_control, &operation);
     if (ret == APP_CONTROL_ERROR_NONE) {
         LOGD("operation: %s", operation);
 
-        if (operation && (strncmp(operation, APP_CONTROL_OPERATION_SYNC_ALARM, strlen(APP_CONTROL_OPERATION_SYNC_ALARM)) == 0)) {
-            ret = app_control_get_extra_data(app_control, APP_CONTROL_DATA_ALARM_ID, &alarm_data);
-            if (ret != APP_CONTROL_ERROR_NONE) {
-                dlog_print(DLOG_ERROR, LOG_TAG, "Function app_control_get_extra_data() failed.");
-                goto cleanup;
-            }
-
-            LOGD("alarm data : %s", alarm_data);
-
-            int ret = device_battery_is_charging(&battery_charging);
-            if (ret != DEVICE_ERROR_NONE) {
-                LOGW("No sync. Can't get battery charging status");
-                goto cleanup;
-            }
-
-            if (!battery_charging) {
-                LOGI("No sync due to no battery charging status");
-                goto cleanup;
-            }
-
-            ret = device_battery_get_percent(&battery_percentage);
-            if (ret != DEVICE_ERROR_NONE) {
-                LOGW("No sync. Failed to get battery percent. error : %d", ret);
-                goto cleanup;
-            }
+        if (!operation) {
+            goto cleanup;
+        }
 
-            LOGI("battery percent : %d", battery_percentage);
-            if (battery_percentage >= MINIMUM_BATTERY) {
-                request_sticker_data("auto", "arsticker", "input");
-                request_sticker_data("auto", "bitmoji", "input");
-            }
-            else {
-                LOGI("No sync due to insufficient battery");
+        if (strcmp(operation, APP_CONTROL_OPERATION_LAUNCH_ON_EVENT) == 0)
+        {
+            ret = app_control_get_uri(app_control, &uri);
+            if (ret == APP_CONTROL_ERROR_NONE && uri)
+            {
+                if (strncmp(uri, event_uri, strlen(event_uri) + 1) == 0)
+                {
+                    ret = app_control_get_extra_data(app_control, "battery_charger_status", &event_value);
+                    if (ret == APP_CONTROL_ERROR_NONE && event_value)
+                    {
+                        if (string(event_value) == "connected")
+                        {
+                            if (!is_init_sap()) {
+                                if (check_sync_time_condition()) {
+                                    if (check_battery_condition()) {
+                                        initialize_sap();
+                                        request_all_sticker_data("auto", "input");
+                                    }
+                                }
+                                else {
+                                    if (!get_job_progress())
+                                    {
+                                        LOGD("exit");
+                                        service_app_exit();
+                                    }
+                                }
+                            }
+                            else {
+                                LOGD("continue doing current job");
+                            }
+                        }
+                        free(event_value);
+                    }
+                }
+                free(uri);
             }
-
-            goto cleanup;
         }
     }
     else {
@@ -119,30 +182,23 @@ static void app_control(app_control_h app_control, void *data)
                 goto cleanup;
 
             if (mode && type) {
-#ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
-                int feature_flag = 0;
-                if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) == 0) {
-                    if (feature_flag & VCONFKEY_STICKER_FEATURE_AREMOJI)
-                        request_sticker_data(mode, "arsticker", type);
-
-                    if (feature_flag & VCONFKEY_STICKER_FEATURE_BITMOJI)
-                        request_sticker_data(mode, "bitmoji", type);
-                } else
-                    LOGW("Failed to get value of VCONFKEY_STICKER_SUPPORTED_FEATURE");
-#else
-                request_sticker_data(mode, "arsticker", type);
-                request_sticker_data(mode, "bitmoji", type);
-#endif
+                if (!is_init_sap()) {
+                    initialize_sap();
+                    request_all_sticker_data(mode, type);
+                }
             }
         }
         else if (strcmp(request, "oobe") == 0) {
-            LOGI("[OOBE] register sync alarm");
-            sync_alarm_register(APP_CONTROL_OPERATION_SYNC_ALARM, SYNC_ALARM_DELAY, SYNC_ALARM_INTERVAL);
+            initialize_sap();
             request_sticker_feature();
         }
         else
         {
             LOGW("Unknown command : %s", request);
+            if (!is_init_sap())
+            {
+                service_app_exit();
+            }
         }
     }
 
@@ -163,6 +219,7 @@ cleanup:
 static void app_terminate(void *data)
 {
     /* Release all resources. */
+    LOGD("");
     deinitialize_sap();
 }
 
diff --git a/receiver/src/sync_alarm.cpp b/receiver/src/sync_alarm.cpp
deleted file mode 100644 (file)
index ec7079e..0000000
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <app_alarm.h>
-#include <app_common.h>
-#include <stdlib.h>
-
-#include "log.h"
-#include "config.h"
-
-static int recurring_alarm_id = -1;
-
-static bool
-on_foreach_registered_alarm(int alarm_id, void *user_data)
-{
-    bool *result = (bool *)user_data;
-
-    LOGD("alarm id : %d", alarm_id);
-
-    *result = *result | 0x1;
-
-    return false;
-}
-
-bool sync_alarm_register(const char *operation, int delay, int period)
-{
-    int ret;
-    app_control_h app_control = NULL;
-    char *app_id = NULL;
-    bool result = false;
-
-    ret = app_control_create(&app_control);
-    if (ret != APP_CONTROL_ERROR_NONE)
-    {
-        LOGE("Function app_control_create() failed.");
-        return false;
-    }
-
-    ret = app_control_set_operation(app_control, operation);
-    if (ret != APP_CONTROL_ERROR_NONE)
-    {
-        LOGE("Function app_control_set_operation() failed.");
-        goto cleanup;
-    }
-
-    if (app_get_id(&app_id) != APP_ERROR_NONE) {
-        goto cleanup;
-    }
-
-    LOGD("app id : %s", app_id);
-    if (!app_id) {
-        goto cleanup;
-    }
-
-    ret = app_control_set_app_id(app_control, app_id);
-    if (ret != APP_CONTROL_ERROR_NONE)
-    {
-        LOGE("Function app_control_set_app_id() failed.");
-        goto cleanup;
-    }
-
-    alarm_cancel_all();
-
-    ret = alarm_schedule_after_delay(app_control, delay, period, &recurring_alarm_id);
-    if (ret != ALARM_ERROR_NONE)
-        LOGE("Function alarm_schedule_after_delay() failed.");
-    else
-        LOGD("Function alarm_schedule_after_delay() succeed. delay : %d, interval : %d", delay, period);
-
-    result = true;
-
-cleanup:
-    if (app_id)
-        free(app_id);
-
-    if (app_control) {
-        ret = app_control_destroy(app_control);
-        if (ret != APP_CONTROL_ERROR_NONE)
-            LOGE("Function app_control_destroy() failed.");
-        else
-            LOGD("Set recurring alarm with id: %i", recurring_alarm_id);
-    }
-
-    return result;
-}
-
-bool sync_alarm_exist()
-{
-    bool result = false;
-    int ret = alarm_foreach_registered_alarm(on_foreach_registered_alarm, &result);
-    if (ret != ALARM_ERROR_NONE)
-        LOGD("Listing error : %d", ret);
-
-    return result;
-}
\ No newline at end of file
index 0765a66..402974f 100644 (file)
@@ -8,6 +8,10 @@
             <background-category value="background-network"/>
             <background-category value="iot-communication"/>
             <background-category value="system"/>
+            <app-control>
+                <operation name="http://tizen.org/appcontrol/operation/launch_on_event"/>
+                <uri name="event://tizen.system.event.battery_charger_status"/>
+            </app-control>
         </service-application>
         <privileges>
             <privilege>http://developer.samsung.com/tizen/privilege/accessoryprotocol</privilege>