diff options
author | Romain Guy <romainguy@android.com> | 2009-10-05 11:58:09 -0700 |
---|---|---|
committer | Romain Guy <romainguy@android.com> | 2009-10-05 11:59:21 -0700 |
commit | 75218d8ec309bc092c169d70a4adaea28c971379 (patch) | |
tree | 5a34f0ddc9e223b7f3a2cfd91dda67cdec30e6f2 /layoutopt/libs/uix | |
parent | 0cbd47a86156fbfc411c5e0990695ffd6568f47f (diff) | |
download | sdk-75218d8ec309bc092c169d70a4adaea28c971379.zip sdk-75218d8ec309bc092c169d70a4adaea28c971379.tar.gz sdk-75218d8ec309bc092c169d70a4adaea28c971379.tar.bz2 |
Add new rules to layoutopt: UseCompoundDrawables and UselessLayout.
Change-Id: I5dfb2567d923da8442b1df1e703e4ebca6e9523b
Diffstat (limited to 'layoutopt/libs/uix')
6 files changed, 39 insertions, 6 deletions
diff --git a/layoutopt/libs/uix/src/com/android/layoutopt/uix/LayoutAnalyzer.java b/layoutopt/libs/uix/src/com/android/layoutopt/uix/LayoutAnalyzer.java index 015bccc..c130782 100644 --- a/layoutopt/libs/uix/src/com/android/layoutopt/uix/LayoutAnalyzer.java +++ b/layoutopt/libs/uix/src/com/android/layoutopt/uix/LayoutAnalyzer.java @@ -230,15 +230,14 @@ public class LayoutAnalyzer { NodeList list = node.getChildNodes(); int count = list.getLength(); - // Depth first + applyRules(analysis, node); + for (int i = 0; i < count; i++) { Node child = list.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { analyze(analysis, child); } } - - applyRules(analysis, node); } private void applyRules(LayoutAnalysis analysis, Node node) { diff --git a/layoutopt/libs/uix/src/resources/rules/MergeRootFrameLayout.rule b/layoutopt/libs/uix/src/resources/rules/MergeRootFrameLayout.rule index c7b2f1a..f92c2b1 100644 --- a/layoutopt/libs/uix/src/resources/rules/MergeRootFrameLayout.rule +++ b/layoutopt/libs/uix/src/resources/rules/MergeRootFrameLayout.rule @@ -12,5 +12,6 @@ if (xml.isRoot() && xml.is("FrameLayout") && !xml.'@android:background' && !xml.'@android:foreground' && ((node.isWidthFillParent() && node.isHeightFillParent()) || !xml.'@android:layout_gravity')) { - analysis << [node: node, description: "The root-level <FrameLayout/> can be replaced with <merge/>"] + analysis << [node: node, description: "The root-level <FrameLayout/> can be " + + "replaced with <merge/>"] } diff --git a/layoutopt/libs/uix/src/resources/rules/TooManyLevels.rule b/layoutopt/libs/uix/src/resources/rules/TooManyLevels.rule index bd8d558..da16c5f 100644 --- a/layoutopt/libs/uix/src/resources/rules/TooManyLevels.rule +++ b/layoutopt/libs/uix/src/resources/rules/TooManyLevels.rule @@ -6,5 +6,5 @@ // - The depth of the layout is > 10 if (xml.isRoot() && (depth = xml.depth()) > 10) { - analysis << "This layout has too many nested layouts: ${depth} levels!" + analysis << "This layout has too many nested layouts: ${depth} levels, it should have <= 10!" } diff --git a/layoutopt/libs/uix/src/resources/rules/TooManyViews.rule b/layoutopt/libs/uix/src/resources/rules/TooManyViews.rule index 466382e..30553e5 100644 --- a/layoutopt/libs/uix/src/resources/rules/TooManyViews.rule +++ b/layoutopt/libs/uix/src/resources/rules/TooManyViews.rule @@ -6,5 +6,5 @@ // - The document contains more than 80 views if (xml.isRoot && (size = xml.'**'.size()) > 80) { - analysis << "This layout has too many views: ${size} views!" + analysis << "This layout has too many views: ${size} views, it should have <= 80!" } diff --git a/layoutopt/libs/uix/src/resources/rules/UseCompoundDrawables.rule b/layoutopt/libs/uix/src/resources/rules/UseCompoundDrawables.rule new file mode 100644 index 0000000..d2421e6 --- /dev/null +++ b/layoutopt/libs/uix/src/resources/rules/UseCompoundDrawables.rule @@ -0,0 +1,15 @@ +// Rule: UseCompoundDrawables +// +// Description: Checks whether the current node can be replaced by a TextView +// using compound drawables. +// +// Conditions: +// - The node is a LinearLayout +// - The node has two children, ImageView and TextView +// - The ImageView does not have a weight + +if (xml.is("LinearLayout") && xml['*'].size() == 2 && xml.'TextView'.size() == 1 && + xml.'ImageView'.size() == 1 && !xml.'ImageView'.'@android:layout_weight') { + analysis << [node: node, description: "This tag and its children can be replaced by one " + + "<TextView/> and a compound drawable for the image"] +} diff --git a/layoutopt/libs/uix/src/resources/rules/UselessLayout.rule b/layoutopt/libs/uix/src/resources/rules/UselessLayout.rule new file mode 100644 index 0000000..d9bd6f0 --- /dev/null +++ b/layoutopt/libs/uix/src/resources/rules/UselessLayout.rule @@ -0,0 +1,18 @@ +// Rule: UselessLayout +// +// Description: Checks whether current node can be removed. +// +// Conditions: +// - The node has children +// - The node does not have siblings +// - The node does not have a background or its parent does not have a +// background or neither the node and its parent have a background +// - The parent is not a <merge/> + +if (!xml.isRoot() && xml['..'].name() != "merge" && xml['..']['*'].size() == 1 && + xml['*'].size() > 0 && ((xml.'@android:background' || xml['..'].'@android:background') || + (!xml.'@android:background' && !xml['..'].'@android:background'))) { + analysis << [node: node, description: "This ${xml.name()} layout or " + + "its ${xml['..'].name()} parent is " + + "${xml['..'].'@android:id' ? "possibly useless" : "useless"}"] +} |