From 52e0ca954149321fe6d976e4d3a116ba627880ba Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Tue, 17 Jan 2017 13:13:30 +0900 Subject: [PATCH] Add a cache for application display types Change-Id: I4e49ae6e9161ac4d4db6f539499de249bb998243 Signed-off-by: Mu-Woong Lee --- src/app-stats/ActiveWindowMonitor.cpp | 65 +++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/src/app-stats/ActiveWindowMonitor.cpp b/src/app-stats/ActiveWindowMonitor.cpp index 107d598..97a5b48 100644 --- a/src/app-stats/ActiveWindowMonitor.cpp +++ b/src/app-stats/ActiveWindowMonitor.cpp @@ -16,6 +16,8 @@ #include #include +#include +#include #include #include @@ -27,6 +29,51 @@ /* Active window changes frequently. * We thus consider the apps being foregrounded at least 3 secs */ #define ONE_DAY_IN_SEC 86400 +#define DEFAULT_CACHE_SIZE 10 +#define TOP_TIER_LIMIT 2 + +class AppDisplayTypeCache { +private: + unsigned int __size; + std::list> __cache; + +public: + AppDisplayTypeCache(unsigned int size) : + __size(size) + { + } + + void put(const std::string& appId, bool noDisp) + { + __cache.push_front(std::make_pair(appId, noDisp)); + + if (__cache.size() > __size) + __cache.pop_back(); + } + + bool get(const std::string& appId, bool* noDisp) + { + int index = 0; + + for (auto it = __cache.begin(); it != __cache.end(); ++it) { + if ((*it).first != appId) { + ++index; + continue; + } + + *noDisp = (*it).second; + + if (index > TOP_TIER_LIMIT) { + __cache.erase(it); + __cache.push_front(std::make_pair(appId, *noDisp)); + } + + return true; + } + + return false; + } +}; ctx::AppUseMonitor::AppUseMonitor() : __signalId(-1), @@ -117,13 +164,20 @@ void ctx::AppUseMonitor::__finishRecord(std::string appId) bool ctx::AppUseMonitor::__isSkippable(std::string appId) { - /* TODO: circular cache */ + static AppDisplayTypeCache __cache(DEFAULT_CACHE_SIZE); + + bool noDisp = false; + + if (__cache.get(appId, &noDisp)) + return noDisp; + + _D("No cache for '%s'", appId.c_str()); + app_info_h appInfo = NULL; int err = app_manager_get_app_info(appId.c_str(), &appInfo); IF_FAIL_RETURN_TAG(err == APP_MANAGER_ERROR_NONE && appInfo, true, _E, "app_manager_get_app_info() failed"); - bool nodisp = false; - err = app_info_is_nodisplay(appInfo, &nodisp); + err = app_info_is_nodisplay(appInfo, &noDisp); if (err != APP_MANAGER_ERROR_NONE) { app_info_destroy(appInfo); _E("app_info_is_nodisplay() failed"); @@ -131,7 +185,10 @@ bool ctx::AppUseMonitor::__isSkippable(std::string appId) } app_info_destroy(appInfo); - return nodisp; + + __cache.put(appId, noDisp); + + return noDisp; } void ctx::AppUseMonitor::__removeExpired() -- 2.34.1