libaurum: core quality improvement 17/288917/3
authorWoochan Lee <wc0917.lee@samsung.com>
Fri, 24 Feb 2023 09:38:02 +0000 (18:38 +0900)
committerwoochan lee <wc0917.lee@samsung.com>
Thu, 9 Mar 2023 08:11:15 +0000 (08:11 +0000)
Remove unnecessary flag, remove multiple loop statements.

Change-Id: I6a0a97ceaa8f592f0252ed6c778db30c97a30162

libaurum/src/UiDevice.cc

index c266122..0d710c8 100644 (file)
@@ -30,6 +30,7 @@
 #include <algorithm>
 #include <iostream>
 #include <gio/gio.h>
+#include <unordered_set>
 
 using namespace Aurum;
 using namespace AurumInternal;
@@ -188,11 +189,11 @@ out:
 
 std::vector<std::shared_ptr<AccessibleNode>> UiDevice::getWindowRoot() const
 {
-    bool dup;
     LOGI("Request window info");
     getTizenWindowInfo();
 
     std::vector<std::shared_ptr<AccessibleNode>> ret{};
+    std::unordered_map<int, std::shared_ptr<AccessibleApplication>> pidToAppNode{};
 
     auto apps = AccessibleWatcher::getInstance()->getApplications();
     for (auto app : apps)
@@ -200,6 +201,7 @@ std::vector<std::shared_ptr<AccessibleNode>> UiDevice::getWindowRoot() const
         app->getAccessibleNode()->updateName();
         app->getAccessibleNode()->updatePid();
         LOGI("App(%s) Pid(%d)", app->getPackageName().c_str(), app->getAccessibleNode()->getPid());
+        pidToAppNode[app->getAccessibleNode()->getPid()] = app;
     }
 
     for (auto tWin : mTizenWindows)
@@ -207,35 +209,18 @@ std::vector<std::shared_ptr<AccessibleNode>> UiDevice::getWindowRoot() const
         LOGI("Visible win (%d) (%d %d %d %d) (%s)", tWin->getPid(), tWin->getWindowGeometry().mTopLeft.x, tWin->getWindowGeometry().mTopLeft.y, tWin->getWindowGeometry().width(),
             tWin->getWindowGeometry().height(), tWin->getName().c_str());
 
-        for (auto app : apps)
-        {
-            dup = false;
-            if (app->getAccessibleNode()->getPid() == tWin->getPid())
-            {
-                for (const auto &retWin : ret)
-                {
-                    retWin->updatePid();
-                    LOGI("Pid Dup check in vector (%d) target (%d)", retWin->getPid(), tWin->getPid());
-                    if (retWin->getPid() == tWin->getPid())
-                    {
-                        dup = true;
-                        break;
-                    }
-                }
-
-                if (!dup)
-                {
-                    LOGI("Actvie App : (%s) (%d)", tWin->getName().c_str(), tWin->getPid());
-                    auto wins = app->getWindows();
-                    std::transform(wins.begin(), wins.end(), std::back_inserter(ret),
-                        [&](std::shared_ptr<AccessibleWindow> window){
-                            LOGI("Target window add pkg: (%s), name (%s)", window->getAccessibleNode()->getPkg().c_str(), window->getTitle().c_str());
-                            return window->getAccessibleNode();
-                        }
-                    );
-                }
-            }
-        }
+        if (pidToAppNode.count(tWin->getPid() == 0)) continue;
+
+        LOGI("Actvie App : (%s) (%d)", tWin->getName().c_str(), tWin->getPid());
+        auto wins = pidToAppNode[tWin->getPid()]->getWindows();
+        std::transform(wins.begin(), wins.end(), std::back_inserter(ret),
+             [&](std::shared_ptr<AccessibleWindow> window) {
+                 LOGI("Target window add pkg: (%s), name (%s)", window->getAccessibleNode()->getPkg().c_str(), window->getTitle().c_str());
+                 return window->getAccessibleNode();
+             }
+        );
+
+        pidToAppNode.erase(tWin->getPid());
     }
 
     return ret;