Lao Li Recommendation: 14th Chapter 8 "Monkeyrunner Source Analysis" Hierarchyviewer implementation principle-get the control list and build the control tree 3

Source: Internet
Author: User

The file returned by the entire dump can be viewed as a multi-fork tree consisting of controls, each representing a control, and the number of spaces before the start of each row (a control) represents the control's hierarchy in this tree, as no spaces represent the root node, which is the decorview of the top of the window we often call.

The algorithm of the above method understands the meaning of the several variables we need to understand first:

    • Depth: Represents the current row of the parsed control information in the first layer of the control tree, that is, the number of spaces before this line of information
    • Currentdepth: The last created Viewnode control node in the hierarchy of the control tree
    • CurrentNode: The last created Viewnode control node, which defaults to the parent node of the current row control, but adjusts to the actual situation

As for the Viewnode control, we're going to parse it down and now we need to know that the entire control tree is composed of it and its constructor Viewnode (Window window, viewnode parent, String data) The three parameters accepted were:

    • window instance of the activity window that represents the topmost focus of the screen
    • The parent node of the node
    • Viewserver returns a row of control information (you will see that the preceding space is actually removed)

against 430 rows "new Viewnode (window,  CurrentNode , line.substring ( Depth)) "You can see that the whole algorithm is focused on how to determine the parent node of the Viewnode control in a control tree based on a line of control string information! because the other two parameters are obvious. Knowing the focus of the algorithm is a good description, in a loop is mainly 421-429 rows to determine the parent control node, and then 430-431 rows based on the parent control node to create the Viewnode node, so the entire algorithm is:

    • 421-423 rows: The control's hierarchy is obtained based on the number of spaces in front of the current row of the control string depth
    • 424-429 rows: Compare the hierarchy of the currently parsed row controls that should be in the hierarchy of the control tree depth and last created Viewnode control nodes
      • Less than or equal to: Then the last created CurrentNode node is definitely not its parent control node, so it backtracking on the control tree, directly found 1 less than the level of the control, that is, its parent node
      • Greater than: Then the last created CurrentNode node is its parent control
    • 430-431 rows: Create a control node by calling Viewnode's constructor immediately after the parent control has been determined
    • Go to the next loop to get the next/row Viewserver control information returned for analysis
    • 440-442 rows: Build a control tree back to the root control and return it to the caller so that the caller can traverse the entire control tree to find the desired control based on the root control.

From the above algorithm we can know that the viewserver return of the spatial information string should be a certain constraint, in fact, from the 13th chapter 6th of the output "figure 13-6-1 Noteslist Control List" can also confirm:

    • The root control should be in the first row
    • Root controls, it is guaranteed that the parent control of any row must have been parsed and already exists in the control tree. That is, the next line of space should not be two more space than the current row, that is, the next row of the control should not be the current row of the sun control, otherwise there is no way to find the next row of the control of the parent control is who, because the control tree is here to break down

After the above steps, hierarchyviewer a tree starting from the root node Decorview, which means you can find any node you want from the tree root.

So in the end, let's see what's going on with this viewnode class:

    • It represents a control that has all the properties that a control should have
    • It represents a node of a control tree that has both a parent member variable pointing to a child control and a children member variable

So let's first look at what it does as a property owned by a control:

public class Viewnode

{

...

public String ID;

public String name;

Public String hashcode;

...

Public list<property> properties = new ArrayList ();

Public map<string, property> namedproperties = new HashMap ();

...

public int left;

public int top;

public int width;

public int height;

public int protocolversion;

...

}

Code 14-8-5 Viewnode Class-control properties

From the above code we see a large number of control properties owned by Viewnode. As to what each property is, I believe it is clear that there is no need to waste time here to give you all the analysis, here you notice the properties and namedproperties this two attributes, where properties is a list of Save control property While Namedproperties is also a property of a control, it is not a list, but rather a set of key-value pairs consisting of a control property name as a key and a value for a control property, which makes it easy for the caller to find the property of the control by the name of a control property.

Let's look at the Viewnode as a node of the control tree to join the corresponding variable that makes up the entire control tree:

Public class Viewnode

23 {

...

Public Viewnode Parent;

53

list<viewnode> children = New ArrayList ();

...

}

Code 14-8-6 Viewnode Class-As a control tree node

Lao Li Recommendation: 14th Chapter 8, "Monkeyrunner Source Analysis" Hierarchyviewer implementation principle-Get a list of controls and build a control tree 3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.