Merge change I5dfb2567 into eclair
* changes: Add new rules to layoutopt: UseCompoundDrawables and UselessLayout.
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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/>"]
|
||||
}
|
||||
|
||||
@@ -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!"
|
||||
}
|
||||
|
||||
@@ -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!"
|
||||
}
|
||||
|
||||
@@ -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"]
|
||||
}
|
||||
@@ -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"}"]
|
||||
}
|
||||
23
tools/layoutopt/samples/compound.xml
Normal file
23
tools/layoutopt/samples/compound.xml
Normal file
@@ -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>
|
||||
19
tools/layoutopt/samples/useless.xml
Normal file
19
tools/layoutopt/samples/useless.xml
Normal file
@@ -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>
|
||||
Reference in New Issue
Block a user