OSDN Git Service

More lint checks: translation, i18n, proguard, gridlayout, "px"
[android-x86/sdk.git] / lint / libs / lint_checks / src / com / android / tools / lint / checks / UseCompoundDrawableDetector.java
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.tools.lint.checks;
18
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;
25
26 import org.w3c.dom.Element;
27
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.List;
31
32 /**
33  * Checks whether the current node can be replaced by a TextView using compound
34  * drawables.
35  */
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);
45
46     /** Constructs a new {@link UseCompoundDrawableDetector} */
47     public UseCompoundDrawableDetector() {
48     }
49
50     @Override
51     public Issue[] getIssues() {
52         return new Issue[] { ISSUE };
53     }
54
55     @Override
56     public Speed getSpeed() {
57         return Speed.FAST;
58     }
59
60     @Override
61     public Scope getScope() {
62         return Scope.SINGLE_FILE;
63     }
64
65     @Override
66     public Collection<String> getApplicableElements() {
67         return Arrays.asList(new String[] {
68                 LINEAR_LAYOUT
69         });
70     }
71
72     @Override
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");
88             }
89         }
90     }
91 }