diff options
author | Tor Norbye <tnorbye@google.com> | 2011-12-13 15:50:45 -0800 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2011-12-13 16:43:45 -0800 |
commit | e61a2749bf3d2509fc6b3472f07f0ecf695d2173 (patch) | |
tree | 237a221b16f43c84db14a11486744276ceb1b5d0 /lint | |
parent | 0c58db6eb2ec57ad9860631b104bd5412077dffd (diff) | |
download | sdk-e61a2749bf3d2509fc6b3472f07f0ecf695d2173.zip sdk-e61a2749bf3d2509fc6b3472f07f0ecf695d2173.tar.gz sdk-e61a2749bf3d2509fc6b3472f07f0ecf695d2173.tar.bz2 |
Fix 22848: Lint: Bogus suggestion and fix for flattening layout
TabHosts etc should not be eligible as parents for an unused layout.
Change-Id: Ic934046530a5c8a4ff84a873e63230819f4fd4a5
Diffstat (limited to 'lint')
3 files changed, 54 insertions, 9 deletions
diff --git a/lint/libs/lint_checks/src/com/android/tools/lint/checks/UselessViewDetector.java b/lint/libs/lint_checks/src/com/android/tools/lint/checks/UselessViewDetector.java index 5fe9119..1404a9a 100644 --- a/lint/libs/lint_checks/src/com/android/tools/lint/checks/UselessViewDetector.java +++ b/lint/libs/lint_checks/src/com/android/tools/lint/checks/UselessViewDetector.java @@ -16,15 +16,21 @@ package com.android.tools.lint.checks; +import static com.android.tools.lint.detector.api.LintConstants.ABSOLUTE_LAYOUT; import static com.android.tools.lint.detector.api.LintConstants.ANDROID_URI; import static com.android.tools.lint.detector.api.LintConstants.ATTR_BACKGROUND; import static com.android.tools.lint.detector.api.LintConstants.ATTR_ID; import static com.android.tools.lint.detector.api.LintConstants.FRAME_LAYOUT; +import static com.android.tools.lint.detector.api.LintConstants.GRID_LAYOUT; import static com.android.tools.lint.detector.api.LintConstants.GRID_VIEW; import static com.android.tools.lint.detector.api.LintConstants.HORIZONTAL_SCROLL_VIEW; import static com.android.tools.lint.detector.api.LintConstants.LINEAR_LAYOUT; import static com.android.tools.lint.detector.api.LintConstants.MERGE; +import static com.android.tools.lint.detector.api.LintConstants.RADIO_GROUP; +import static com.android.tools.lint.detector.api.LintConstants.RELATIVE_LAYOUT; import static com.android.tools.lint.detector.api.LintConstants.SCROLL_VIEW; +import static com.android.tools.lint.detector.api.LintConstants.TABLE_LAYOUT; +import static com.android.tools.lint.detector.api.LintConstants.TABLE_ROW; import com.android.tools.lint.detector.api.Category; import com.android.tools.lint.detector.api.Issue; @@ -82,29 +88,28 @@ public class UselessViewDetector extends LayoutDetector { return Speed.FAST; } - private static final List<String> CONTAINERS = new ArrayList<String>(20); + private static final List<String> CONTAINERS = new ArrayList<String>(18); static { - CONTAINERS.add("android.gesture.GestureOverlayView"); //$NON-NLS-1$ - CONTAINERS.add("AbsoluteLayout"); //$NON-NLS-1$ + CONTAINERS.add(ABSOLUTE_LAYOUT); CONTAINERS.add(FRAME_LAYOUT); - CONTAINERS.add("GridLayout"); //$NON-NLS-1$ + CONTAINERS.add(GRID_LAYOUT); CONTAINERS.add(GRID_VIEW); CONTAINERS.add(HORIZONTAL_SCROLL_VIEW); CONTAINERS.add("ImageSwitcher"); //$NON-NLS-1$ CONTAINERS.add(LINEAR_LAYOUT); - CONTAINERS.add("RadioGroup"); //$NON-NLS-1$ - CONTAINERS.add("RelativeLayout"); //$NON-NLS-1$ + CONTAINERS.add(RADIO_GROUP); + CONTAINERS.add(RELATIVE_LAYOUT); CONTAINERS.add(SCROLL_VIEW); CONTAINERS.add("SlidingDrawer"); //$NON-NLS-1$ CONTAINERS.add("StackView"); //$NON-NLS-1$ - CONTAINERS.add("TabHost"); //$NON-NLS-1$ - CONTAINERS.add("TableLayout"); //$NON-NLS-1$ - CONTAINERS.add("TableRow"); //$NON-NLS-1$ + CONTAINERS.add(TABLE_LAYOUT); + CONTAINERS.add(TABLE_ROW); CONTAINERS.add("TextSwitcher"); //$NON-NLS-1$ CONTAINERS.add("ViewAnimator"); //$NON-NLS-1$ CONTAINERS.add("ViewFlipper"); //$NON-NLS-1$ CONTAINERS.add("ViewSwitcher"); //$NON-NLS-1$ // Available ViewGroups that are not included by this check: + // CONTAINERS.add("android.gesture.GestureOverlayView"); // CONTAINERS.add("AdapterViewFlipper"); // CONTAINERS.add("DialerFilter"); // CONTAINERS.add("ExpandableListView"); @@ -113,6 +118,7 @@ public class UselessViewDetector extends LayoutDetector { // CONTAINERS.add("merge"); // CONTAINERS.add("SearchView"); // CONTAINERS.add("TabWidget"); + // CONTAINERS.add("TabHost"); } @Override public Collection<String> getApplicableElements() { @@ -178,6 +184,12 @@ public class UselessViewDetector extends LayoutDetector { return; } + // Certain parents are special - such as the TabHost and the GestureOverlayView - + // where we want to leave things alone. + if (!CONTAINERS.contains(parentTag)) { + return; + } + boolean hasId = element.hasAttributeNS(ANDROID_URI, ATTR_ID); Location location = context.getLocation(element); String tag = element.getTagName(); diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/UselessViewDetectorTest.java b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/UselessViewDetectorTest.java index 845d6fb..aa45894 100644 --- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/UselessViewDetectorTest.java +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/UselessViewDetectorTest.java @@ -37,4 +37,11 @@ public class UselessViewDetectorTest extends AbstractCheckTest { "no background, no id)", lintFiles("res/layout/useless.xml")); } + + public void testTabHost() throws Exception { + assertEquals( + "No warnings.", + + lintFiles("res/layout/useless2.xml")); + } } diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/useless2.xml b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/useless2.xml new file mode 100644 index 0000000..a819036 --- /dev/null +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/useless2.xml @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="utf-8"?> +<TabHost xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="match_parent" > + + <LinearLayout + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical" > + + <TabWidget + android:layout_width="match_parent" + android:layout_height="wrap_content" /> + + <FrameLayout + android:layout_width="match_parent" + android:layout_height="0px" + android:layout_weight="1" > + + <Button + android:layout_width="wrap_content" + android:layout_height="wrap_content" /> + </FrameLayout> + </LinearLayout> + +</TabHost> |