2 * Copyright (C) 2011 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.tools.lint.checks;
19 import com.android.tools.lint.detector.api.Context;
20 import com.android.tools.lint.detector.api.Issue;
21 import com.android.tools.lint.detector.api.LayoutDetector;
22 import com.android.tools.lint.detector.api.Scope;
23 import com.android.tools.lint.detector.api.Severity;
24 import com.android.tools.lint.detector.api.Speed;
26 import org.w3c.dom.Element;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.List;
33 * Checks whether the current node can be replaced by a TextView using compound
36 public class UseCompoundDrawableDetector extends LayoutDetector {
37 /** The main issue discovered by this detector */
38 public static final Issue ISSUE = Issue.create(
39 "UseCompoundDrawables", //$NON-NLS-1$
40 "Checks whether the current node can be replaced by a TextView using compound drawables.",
41 // TODO: OFFER MORE HELP!
42 "A LinearLayout which contains an ImageView and a TextView can be more efficiently " +
43 "handled as a compound drawable",
44 CATEGORY_PERFORMANCE, 6, Severity.WARNING);
46 /** Constructs a new {@link UseCompoundDrawableDetector} */
47 public UseCompoundDrawableDetector() {
51 public Issue[] getIssues() {
52 return new Issue[] { ISSUE };
56 public Speed getSpeed() {
61 public Scope getScope() {
62 return Scope.SINGLE_FILE;
66 public Collection<String> getApplicableElements() {
67 return Arrays.asList(new String[] {
73 public void visitElement(Context context, Element element) {
74 int childCount = getChildCount(element);
75 if (childCount == 2) {
76 List<Element> children = getChildren(element);
77 Element first = children.get(0);
78 Element second = children.get(1);
79 if ((first.getTagName().equals(IMAGE_VIEW) &&
80 second.getTagName().equals(TEXT_VIEW) &&
81 !first.hasAttributeNS(ANDROID_URI, ATTR_LAYOUT_WEIGHT)) ||
82 ((second.getTagName().equals(IMAGE_VIEW) &&
83 first.getTagName().equals(TEXT_VIEW) &&
84 !second.hasAttributeNS(ANDROID_URI, ATTR_LAYOUT_WEIGHT)))) {
85 context.toolContext.report(ISSUE, context.getLocation(element),
86 "This tag and its children can be replaced by one <TextView/> and " +
87 "a compound drawable");