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.Attr;
28 import java.util.Arrays;
29 import java.util.Collection;
32 * Check which looks at the children of ScrollViews and ensures that they fill/match
33 * the parent width instead of setting wrap_content.
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" +
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" +
45 "* The application cannot be translated to other languages by just adding new " +
46 "translations for existing string resources.",
48 CATEGORY_LAYOUT, 7, Severity.WARNING);
50 // TODO: Add additional issues here, such as hardcoded colors, hardcoded sizes, etc
52 /** Constructs a new {@link HardcodedValuesDetector} */
53 public HardcodedValuesDetector() {
57 public Issue[] getIssues() {
58 return new Issue[] { ISSUE };
62 public Speed getSpeed() {
67 public Scope getScope() {
68 return Scope.SINGLE_FILE;
72 public Collection<String> getApplicableAttributes() {
73 return Arrays.asList(new String[] {
75 ATTR_CONTENT_DESCRIPTION,
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())) {
91 context.toolContext.report(ISSUE, context.getLocation(attribute),
92 String.format("[I18N] Hardcoded string \"%1$s\", should use @string resource",