Merge change I5dfb2567 into eclair

* changes:
  Add new rules to layoutopt: UseCompoundDrawables and UselessLayout.
This commit is contained in:
Android (Google) Code Review
2009-10-05 14:59:48 -04:00
9 changed files with 83 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
# Copyright 2009 The Android Open Source Project # Copyright 2009 The Android Open Source Project
# #
DDMSLIBS_LOCAL_DIR := $(call my-dir) UIX_LOCAL_DIR := $(call my-dir)
include $(DDMSLIBS_LOCAL_DIR)/uix/Android.mk include $(UIX_LOCAL_DIR)/uix/Android.mk

View File

@@ -230,15 +230,14 @@ public class LayoutAnalyzer {
NodeList list = node.getChildNodes(); NodeList list = node.getChildNodes();
int count = list.getLength(); int count = list.getLength();
// Depth first applyRules(analysis, node);
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
Node child = list.item(i); Node child = list.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) { if (child.getNodeType() == Node.ELEMENT_NODE) {
analyze(analysis, child); analyze(analysis, child);
} }
} }
applyRules(analysis, node);
} }
private void applyRules(LayoutAnalysis analysis, Node node) { private void applyRules(LayoutAnalysis analysis, Node node) {

View File

@@ -12,5 +12,6 @@
if (xml.isRoot() && xml.is("FrameLayout") && !xml.'@android:background' && if (xml.isRoot() && xml.is("FrameLayout") && !xml.'@android:background' &&
!xml.'@android:foreground' && ((node.isWidthFillParent() && !xml.'@android:foreground' && ((node.isWidthFillParent() &&
node.isHeightFillParent()) || !xml.'@android:layout_gravity')) { 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/>"]
} }

View File

@@ -6,5 +6,5 @@
// - The depth of the layout is > 10 // - The depth of the layout is > 10
if (xml.isRoot() && (depth = xml.depth()) > 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!"
} }

View File

@@ -6,5 +6,5 @@
// - The document contains more than 80 views // - The document contains more than 80 views
if (xml.isRoot && (size = xml.'**'.size()) > 80) { 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!"
} }

View File

@@ -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"]
}

View File

@@ -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"}"]
}

View 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>

View 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>