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 / HardcodedValuesDetector.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.Attr;
27
28 import java.util.Arrays;
29 import java.util.Collection;
30
31 /**
32  * Check which looks at the children of ScrollViews and ensures that they fill/match
33  * the parent width instead of setting wrap_content.
34  */
35 public class HardcodedValuesDetector extends LayoutDetector {
36     /** The main issue discovered by this detector */
37     public static final Issue ISSUE = Issue.create(
38             "HardcodedText", //$NON-NLS-1$
39             "Looks for hardcoded text attributes which should be converted to resource lookup",
40             "Hardcoding text attributes directly in layout files is bad for several reasons:\n" +
41             "\n" +
42             "* When creating configuration variations (for example for landscape or portrait)" +
43             "you have to repeat the actual text (and keep it up to date when making changes)\n" +
44             "\n" +
45             "* The application cannot be translated to other languages by just adding new " +
46             "translations for existing string resources.",
47
48             CATEGORY_LAYOUT, 7, Severity.WARNING);
49
50     // TODO: Add additional issues here, such as hardcoded colors, hardcoded sizes, etc
51
52     /** Constructs a new {@link HardcodedValuesDetector} */
53     public HardcodedValuesDetector() {
54     }
55
56     @Override
57     public Issue[] getIssues() {
58         return new Issue[] { ISSUE };
59     }
60
61     @Override
62     public Speed getSpeed() {
63         return Speed.FAST;
64     }
65
66     @Override
67     public Scope getScope() {
68         return Scope.SINGLE_FILE;
69     }
70
71     @Override
72     public Collection<String> getApplicableAttributes() {
73         return Arrays.asList(new String[] {
74                 ATTR_TEXT,
75                 ATTR_CONTENT_DESCRIPTION,
76                 ATTR_HINT,
77                 ATTR_LABEL,
78                 ATTR_PROMPT
79         });
80     }
81
82     @Override
83     public void visitAttribute(Context context, Attr attribute) {
84         String value = attribute.getValue();
85         if (value.length() > 0 && (value.charAt(0) != '@' && value.charAt(0) != '?')) {
86             // Make sure this is really one of the android: attributes
87             if (!ANDROID_URI.equals(attribute.getNamespaceURI())) {
88                 return;
89             }
90
91             context.toolContext.report(ISSUE, context.getLocation(attribute),
92                     String.format("[I18N] Hardcoded string \"%1$s\", should use @string resource",
93                             value));
94         }
95     }
96 }