Add a cache for application display types 48/110548/1 accepted/tizen/3.0/common/20170118.131024 accepted/tizen/3.0/ivi/20170118.041320 accepted/tizen/3.0/mobile/20170118.041234 accepted/tizen/3.0/wearable/20170118.041304 submit/tizen_3.0/20170117.044420
authorMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 17 Jan 2017 04:13:30 +0000 (13:13 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 17 Jan 2017 04:13:30 +0000 (13:13 +0900)
Change-Id: I4e49ae6e9161ac4d4db6f539499de249bb998243
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/app-stats/ActiveWindowMonitor.cpp

index 107d598..97a5b48 100644 (file)
@@ -16,6 +16,8 @@
 
 #include <sys/types.h>
 #include <time.h>
+#include <list>
+#include <utility>
 #include <app_manager.h>
 
 #include <Json.h>
 /* 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<std::pair<std::string, bool>> __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()