diff options
Diffstat (limited to 'layoutopt')
10 files changed, 85 insertions, 10 deletions
diff --git a/layoutopt/app/src/com/android/layoutopt/cli/Main.java b/layoutopt/app/src/com/android/layoutopt/cli/Main.java index 9b3b9aa..abfd5ea 100644 --- a/layoutopt/app/src/com/android/layoutopt/cli/Main.java +++ b/layoutopt/app/src/com/android/layoutopt/cli/Main.java @@ -49,7 +49,7 @@ public class Main { private static void analyzeFiles(File[] files) { LayoutAnalyzer analyzer = new LayoutAnalyzer(); for (File file : files) { - if (file.isFile()) { + if (file.isFile() && file.getName().endsWith(".xml")) { analyze(analyzer, file); } else { analyzeFiles(file.listFiles()); @@ -95,7 +95,7 @@ public class Main { List<File> files = new ArrayList<File>(); for (String path : args) { File file = new File(path); - if (file.exists()) { + if (file.exists() && (file.isDirectory() || file.getName().endsWith(".xml"))) { files.add(file); } } diff --git a/layoutopt/libs/Android.mk b/layoutopt/libs/Android.mk index 6af13a8..058299a 100644 --- a/layoutopt/libs/Android.mk +++ b/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/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"}"] +} diff --git a/layoutopt/samples/compound.xml b/layoutopt/samples/compound.xml new file mode 100644 index 0000000..a1c1ee5 --- /dev/null +++ b/layoutopt/samples/compound.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="utf-8"?> + +<FrameLayout + xmlns:android="http://schemas.android.com/apk/res/android" + + android:layout_width="fill_parent" + android:layout_height="fill_parent"> + + <LinearLayout + android:layout_width="fill_parent" + android:layout_height="fill_parent"> + + <ImageView + android:layout_width="wrap_content" + android:layout_height="wrap_content" /> + + <TextView + android:layout_width="wrap_content" + android:layout_height="wrap_content" /> + + </LinearLayout> + +</FrameLayout> diff --git a/layoutopt/samples/useless.xml b/layoutopt/samples/useless.xml new file mode 100644 index 0000000..1c2d5d8 --- /dev/null +++ b/layoutopt/samples/useless.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="utf-8"?> + +<FrameLayout + xmlns:android="http://schemas.android.com/apk/res/android" + + android:layout_width="fill_parent" + android:layout_height="fill_parent"> + + <LinearLayout + android:layout_width="fill_parent" + android:layout_height="fill_parent"> + + <TextView + android:layout_width="wrap_content" + android:layout_height="wrap_content" /> + + </LinearLayout> + +</FrameLayout> |