3 Comparer::Comparer(const std::shared_ptr<UiDevice> device, const std::shared_ptr<UiSelector> selector,
4 const bool &earlyReturn)
5 : mDevice(device), mSelector(selector), mEarlyReturn(earlyReturn)
9 Comparer::~Comparer() {}
11 std::shared_ptr<AccessibleNode> Comparer::findObject(const std::shared_ptr<UiDevice> device,
12 const std::shared_ptr<UiSelector> selector,
13 const std::shared_ptr<AccessibleNode> root)
15 std::vector<std::shared_ptr<AccessibleNode>> ret = findObjects(device, selector, root, true);
17 return std::move(ret[0]);
22 std::vector<std::shared_ptr<AccessibleNode>> Comparer::findObjects(const std::shared_ptr<UiDevice> device,
23 const std::shared_ptr<UiSelector> selector,
24 const std::shared_ptr<AccessibleNode> root, bool earlyReturn)
26 Comparer comparer(device, selector, earlyReturn);
28 LOGI("findObjects selector(%s) from (type:%s style:%s, role:%s, text:%s) earlyReturn:%d", selector->description().c_str(), root->getType().c_str(), root->getStyle().c_str(), root->getRole().c_str(), root->getText().c_str(), earlyReturn);
30 if (selector->mParent) {
31 auto ret = Comparer::findObjects(device, selector->mParent, root);
32 std::vector<std::shared_ptr<AccessibleNode>> merged{};
34 for (const auto &node : ret) {
35 auto tmp = comparer.findObjects(node);
36 std::move(std::begin(tmp), std::end(tmp), std::back_inserter(merged));
41 return comparer.findObjects(root);
44 std::vector<std::shared_ptr<AccessibleNode>> Comparer::findObjects(const std::shared_ptr<AccessibleNode> root)
46 std::list<std::shared_ptr<PartialMatch>> partialList{};
47 std::vector<std::shared_ptr<AccessibleNode>> ret = findObjects(root, 0, 0, partialList);
48 LOGI("%d object(s) found", (int)ret.size());
52 std::vector<std::shared_ptr<AccessibleNode>> Comparer::findObjects(
53 const std::shared_ptr<AccessibleNode> root, const int &index, const int &depth,
54 std::list<std::shared_ptr<PartialMatch>> &partialMatches)
56 std::vector<std::shared_ptr<AccessibleNode>> ret;
59 for (auto &match : partialMatches)
60 match->update(root, index, depth, partialMatches);
62 std::shared_ptr<PartialMatch> currentMatch =
63 PartialMatch::accept(root, mSelector, index, depth);
64 if (currentMatch) partialMatches.push_front(currentMatch);
66 if (!(mSelector->mMaxDepth && (depth+1 > *(mSelector->mMaxDepth)))) {
67 int childCnt = root->getChildCount();
68 for (int i = 0; i < childCnt; i++) {
69 std::shared_ptr<AccessibleNode> childNode = root->getChildAt(i);
70 if (childNode == nullptr) continue;
72 std::vector<std::shared_ptr<AccessibleNode>> childret =
73 findObjects(childNode, i, depth + 1, partialMatches);
74 std::move(std::begin(childret), std::end(childret), std::back_inserter(ret));
76 if (!ret.empty() && mEarlyReturn) {
77 LOGI("Object found and earlyReturn");
82 LOGI("Abort searching! No need to search children(maxDepth limit overflow, %d < %d < %d)", mSelector->mMinDepth? * (mSelector->mMinDepth): -1, depth, mSelector->mMaxDepth?*(mSelector->mMaxDepth):9999999);
85 if (currentMatch && currentMatch->finalizeMatch()){
86 LOGI("Found matched = %s with criteria %s", root->description().c_str(), currentMatch->debugPrint().c_str());