diff --git a/tools/layoutopt/libs/Android.mk b/tools/layoutopt/libs/Android.mk index 6af13a8b5..058299a06 100644 --- a/tools/layoutopt/libs/Android.mk +++ b/tools/layoutopt/libs/Android.mk @@ -1,5 +1,5 @@ # Copyright 2009 The Android Open Source Project # -DDMSLIBS_LOCAL_DIR := $(call my-dir) -include $(DDMSLIBS_LOCAL_DIR)/uix/Android.mk +UIX_LOCAL_DIR := $(call my-dir) +include $(UIX_LOCAL_DIR)/uix/Android.mk diff --git a/tools/layoutopt/libs/uix/src/com/android/layoutopt/uix/LayoutAnalyzer.java b/tools/layoutopt/libs/uix/src/com/android/layoutopt/uix/LayoutAnalyzer.java index 015bcccd9..c130782b4 100644 --- a/tools/layoutopt/libs/uix/src/com/android/layoutopt/uix/LayoutAnalyzer.java +++ b/tools/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/tools/layoutopt/libs/uix/src/resources/rules/MergeRootFrameLayout.rule b/tools/layoutopt/libs/uix/src/resources/rules/MergeRootFrameLayout.rule index c7b2f1a55..f92c2b1ca 100644 --- a/tools/layoutopt/libs/uix/src/resources/rules/MergeRootFrameLayout.rule +++ b/tools/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 can be replaced with "] + analysis << [node: node, description: "The root-level can be " + + "replaced with "] } diff --git a/tools/layoutopt/libs/uix/src/resources/rules/TooManyLevels.rule b/tools/layoutopt/libs/uix/src/resources/rules/TooManyLevels.rule index bd8d55890..da16c5f75 100644 --- a/tools/layoutopt/libs/uix/src/resources/rules/TooManyLevels.rule +++ b/tools/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/tools/layoutopt/libs/uix/src/resources/rules/TooManyViews.rule b/tools/layoutopt/libs/uix/src/resources/rules/TooManyViews.rule index 466382e91..30553e58b 100644 --- a/tools/layoutopt/libs/uix/src/resources/rules/TooManyViews.rule +++ b/tools/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/tools/layoutopt/libs/uix/src/resources/rules/UseCompoundDrawables.rule b/tools/layoutopt/libs/uix/src/resources/rules/UseCompoundDrawables.rule new file mode 100644 index 000000000..d2421e675 --- /dev/null +++ b/tools/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 " + + " and a compound drawable for the image"] +} diff --git a/tools/layoutopt/libs/uix/src/resources/rules/UselessLayout.rule b/tools/layoutopt/libs/uix/src/resources/rules/UselessLayout.rule new file mode 100644 index 000000000..d9bd6f0aa --- /dev/null +++ b/tools/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 + +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"}"] +} diff --git a/tools/layoutopt/samples/compound.xml b/tools/layoutopt/samples/compound.xml new file mode 100644 index 000000000..a1c1ee5a1 --- /dev/null +++ b/tools/layoutopt/samples/compound.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + diff --git a/tools/layoutopt/samples/useless.xml b/tools/layoutopt/samples/useless.xml new file mode 100644 index 000000000..1c2d5d8f8 --- /dev/null +++ b/tools/layoutopt/samples/useless.xml @@ -0,0 +1,19 @@ + + + + + + + + + + +