From c23f2a71d425888cf3d0edde1f51505fcb20f8d6 Mon Sep 17 00:00:00 2001 From: hyunho Date: Thu, 12 Apr 2018 19:20:38 +0900 Subject: [PATCH] Add periodic update feature Change-Id: Id79bd7c5ed869a1f2c8e03b1dc136702f1888bc7 Signed-off-by: hyunho --- .../complication-implementation.h | 2 ++ watchface-complication/complication.cc | 24 ++++++++++++++ watchface-complication/complication.h | 1 + watchface-complication/db-manager.cc | 37 ++++++++++++++++++++++ watchface-complication/db-manager.h | 1 + 5 files changed, 65 insertions(+) diff --git a/watchface-complication/complication-implementation.h b/watchface-complication/complication-implementation.h index fb81b9d..e04ecd7 100644 --- a/watchface-complication/complication-implementation.h +++ b/watchface-complication/complication-implementation.h @@ -62,6 +62,7 @@ class Complication::Impl : ComplicationConnector::IEventListener { int AddCandidateList(std::string provider_id, int types); int AddCandidateList(int types); int MakeCandidatesList(); + void SetPeriod(); private: Complication* parent_; @@ -88,6 +89,7 @@ class Complication::Impl : ComplicationConnector::IEventListener { static const std::string provider_id_key_; static const std::string provider_type_key_; IEditable::EditableState ed_state_; + guint periodic_timer_; }; } // namespace watchface_complication diff --git a/watchface-complication/complication.cc b/watchface-complication/complication.cc index d674fff..0e845ab 100644 --- a/watchface-complication/complication.cc +++ b/watchface-complication/complication.cc @@ -59,6 +59,7 @@ Complication::Impl::Impl(Complication* parent, int id, geo_(geo) { MakeCandidatesList(); RestoreStateOrSetDefault(); + SetPeriod(); subscribe_id_ = ComplicationConnector::GetInst().SubscribeSignal( ComplicationConnector::Complication, cur_provider_id_, -1, this); @@ -255,6 +256,28 @@ int Complication::GetLastDataIdx() { return impl_->last_data_idx_; } +void Complication::Impl::SetPeriod() { + if (periodic_timer_) { + LOGI("Remove timer!"); + g_source_remove(periodic_timer_); + periodic_timer_ = 0; + } + + int period; + int ret = DBManager::GetProviderPeriod(cur_provider_id_, &period); + if (ret == COMPLICATION_ERROR_NONE && period != -1) { + periodic_timer_ = g_timeout_add_seconds(period, PeriodCallback, parent_); + LOGI("New timer set : %d(%d)", period, periodic_timer_); + } +} + +gboolean Complication::PeriodCallback(gpointer user_data) { + Complication* comp = static_cast(user_data); + comp->SendDataUpdateRequest(); + LOGI("send periodic update request !!"); + return G_SOURCE_CONTINUE; +} + int Complication::UpdateLastDataIdx() { int ret; @@ -263,6 +286,7 @@ int Complication::UpdateLastDataIdx() { if (ret != COMPLICATION_ERROR_NONE) return ret; + impl_->SetPeriod(); impl_->last_provider_id_ = impl_->cur_provider_id_; impl_->last_type_ = impl_->cur_type_; impl_->last_data_idx_ = impl_->cur_data_idx_; diff --git a/watchface-complication/complication.h b/watchface-complication/complication.h index 6dcfbf6..3fe4bd3 100644 --- a/watchface-complication/complication.h +++ b/watchface-complication/complication.h @@ -96,6 +96,7 @@ class EXPORT_API Complication : public IEditable public: static const char* GetProviderIdKey(); static const char* GetProviderTypeKey(); + static gboolean PeriodCallback(gpointer user_data); private: class Impl; diff --git a/watchface-complication/db-manager.cc b/watchface-complication/db-manager.cc index 088a48b..bae8643 100644 --- a/watchface-complication/db-manager.cc +++ b/watchface-complication/db-manager.cc @@ -104,6 +104,43 @@ std::unique_ptr DBManager::GetDefaultData(const char* provider_id, return std::move(default_data); } +int DBManager::GetProviderPeriod(std::string& provider_id, int* period) { + int ret; + sqlite3_stmt *stmt = NULL; + sqlite3* db; + + static const char query[] = + "SELECT period FROM complication_provider " + "WHERE provider_id=?"; + + db = OpenDB(); + if (db == NULL) { + LOGE("parser db not exist"); + return COMPLICATION_ERROR_DB; + } + + ret = sqlite3_prepare_v2(db, query, strlen(query), + &stmt, NULL); + if (ret != SQLITE_OK) { + LOGE("prepare error: %s", sqlite3_errmsg(db)); + CloseDB(db); + return COMPLICATION_ERROR_DB; + } + + sqlite3_bind_text(stmt, 1, provider_id.c_str(), -1, SQLITE_TRANSIENT); + + /* complication_provider table's default value of period is -1. + * So, if nothing specified in period, it will return -1 + */ + if (sqlite3_step(stmt) == SQLITE_ROW) + *period = sqlite3_column_int(stmt, 0); + + sqlite3_finalize(stmt); + CloseDB(db); + + return COMPLICATION_ERROR_NONE; +} + std::string DBManager::GetProviderAppId(const char* provider_id) { int ret; std::string appid; diff --git a/watchface-complication/db-manager.h b/watchface-complication/db-manager.h index 9a86ead..68d06f0 100644 --- a/watchface-complication/db-manager.h +++ b/watchface-complication/db-manager.h @@ -52,6 +52,7 @@ class EXPORT_API DBManager { static std::list> GetProviderListWithTypes( int support_types); static int GetSupportTypes(std::string& provider_id, int* types); + static int GetProviderPeriod(std::string& provider_id, int* period); private: DBManager(); -- 2.7.4