Home | History | Annotate | Download | only in checks
      1 /*
      2  * Copyright (C) 2012 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 static com.android.tools.lint.detector.api.LintConstants.ANDROID_URI;
     20 import static com.android.tools.lint.detector.api.LintConstants.ATTR_AUTO_TEXT;
     21 import static com.android.tools.lint.detector.api.LintConstants.ATTR_BUFFER_TYPE;
     22 import static com.android.tools.lint.detector.api.LintConstants.ATTR_CAPITALIZE;
     23 import static com.android.tools.lint.detector.api.LintConstants.ATTR_CURSOR_VISIBLE;
     24 import static com.android.tools.lint.detector.api.LintConstants.ATTR_DIGITS;
     25 import static com.android.tools.lint.detector.api.LintConstants.ATTR_EDITABLE;
     26 import static com.android.tools.lint.detector.api.LintConstants.ATTR_EDITOR_EXTRAS;
     27 import static com.android.tools.lint.detector.api.LintConstants.ATTR_IME_ACTION_ID;
     28 import static com.android.tools.lint.detector.api.LintConstants.ATTR_IME_ACTION_LABEL;
     29 import static com.android.tools.lint.detector.api.LintConstants.ATTR_IME_OPTIONS;
     30 import static com.android.tools.lint.detector.api.LintConstants.ATTR_INPUT_METHOD;
     31 import static com.android.tools.lint.detector.api.LintConstants.ATTR_INPUT_TYPE;
     32 import static com.android.tools.lint.detector.api.LintConstants.ATTR_NUMERIC;
     33 import static com.android.tools.lint.detector.api.LintConstants.ATTR_PASSWORD;
     34 import static com.android.tools.lint.detector.api.LintConstants.ATTR_PHONE_NUMBER;
     35 import static com.android.tools.lint.detector.api.LintConstants.ATTR_PRIVATE_IME_OPTIONS;
     36 import static com.android.tools.lint.detector.api.LintConstants.BUTTON;
     37 import static com.android.tools.lint.detector.api.LintConstants.CHECKED_TEXT_VIEW;
     38 import static com.android.tools.lint.detector.api.LintConstants.CHECK_BOX;
     39 import static com.android.tools.lint.detector.api.LintConstants.RADIO_BUTTON;
     40 import static com.android.tools.lint.detector.api.LintConstants.SWITCH;
     41 import static com.android.tools.lint.detector.api.LintConstants.TEXT_VIEW;
     42 import static com.android.tools.lint.detector.api.LintConstants.TOGGLE_BUTTON;
     43 import static com.android.tools.lint.detector.api.LintConstants.VALUE_EDITABLE;
     44 import static com.android.tools.lint.detector.api.LintConstants.VALUE_NONE;
     45 import static com.android.tools.lint.detector.api.LintConstants.VALUE_TRUE;
     46 
     47 import com.android.tools.lint.detector.api.Category;
     48 import com.android.tools.lint.detector.api.Issue;
     49 import com.android.tools.lint.detector.api.LayoutDetector;
     50 import com.android.tools.lint.detector.api.Location;
     51 import com.android.tools.lint.detector.api.Scope;
     52 import com.android.tools.lint.detector.api.Severity;
     53 import com.android.tools.lint.detector.api.Speed;
     54 import com.android.tools.lint.detector.api.XmlContext;
     55 
     56 import org.w3c.dom.Attr;
     57 import org.w3c.dom.Element;
     58 import org.w3c.dom.NamedNodeMap;
     59 
     60 import java.util.Arrays;
     61 import java.util.Collection;
     62 
     63 /**
     64  * Checks for cases where a TextView should probably be an EditText instead
     65  */
     66 public class TextViewDetector extends LayoutDetector {
     67     /** The main issue discovered by this detector */
     68     public static final Issue ISSUE = Issue.create(
     69             "TextViewEdits", //$NON-NLS-1$
     70             "Looks for TextViews being used for input",
     71 
     72             "Using a <TextView> to input text is generally an error, you should be " +
     73             "using <EditText> instead.  EditText is a subclass of TextView, and some " +
     74             "of the editing support is provided by TextView, so it's possible to set " +
     75             "some input-related properties on a TextView. However, using a TextView " +
     76             "along with input attributes is usually a cut & paste error. To input " +
     77             "text you should be using <EditText>." +
     78             "\n" +
     79             "This check also checks subclasses of TextView, such as Button and CheckBox, " +
     80             "since these have the same issue: they should not be used with editable " +
     81             "attributes.",
     82 
     83             Category.CORRECTNESS,
     84             7,
     85             Severity.WARNING,
     86             TextViewDetector.class,
     87             Scope.RESOURCE_FILE_SCOPE);
     88 
     89     /** Constructs a new {@link TextViewDetector} */
     90     public TextViewDetector() {
     91     }
     92 
     93     @Override
     94     public Speed getSpeed() {
     95         return Speed.FAST;
     96     }
     97 
     98     @Override
     99     public Collection<String> getApplicableElements() {
    100         return Arrays.asList(
    101                 TEXT_VIEW,
    102                 BUTTON,
    103                 TOGGLE_BUTTON,
    104                 CHECK_BOX,
    105                 RADIO_BUTTON,
    106                 CHECKED_TEXT_VIEW,
    107                 SWITCH
    108         );
    109     }
    110 
    111     @Override
    112     public void visitElement(XmlContext context, Element element) {
    113         NamedNodeMap attributes = element.getAttributes();
    114         for (int i = 0, n = attributes.getLength(); i < n; i++) {
    115             Attr attribute = (Attr) attributes.item(i);
    116             String name = attribute.getLocalName();
    117             if (name == null) {
    118                 // Attribute not in a namespace; we only care about the android: ones
    119                 continue;
    120             }
    121 
    122             boolean isEditAttribute = false;
    123             switch (name.charAt(0)) {
    124                 case 'a': {
    125                     isEditAttribute = name.equals(ATTR_AUTO_TEXT);
    126                     break;
    127                 }
    128                 case 'b': {
    129                     isEditAttribute = name.equals(ATTR_BUFFER_TYPE) &&
    130                             attribute.getValue().equals(VALUE_EDITABLE);
    131                     break;
    132                 }
    133                 case 'p': {
    134                     isEditAttribute = name.equals(ATTR_PASSWORD)
    135                             || name.equals(ATTR_PHONE_NUMBER)
    136                             || name.equals(ATTR_PRIVATE_IME_OPTIONS);
    137                     break;
    138                 }
    139                 case 'c': {
    140                     isEditAttribute = name.equals(ATTR_CAPITALIZE)
    141                             || name.equals(ATTR_CURSOR_VISIBLE);
    142                     break;
    143                 }
    144                 case 'd': {
    145                     isEditAttribute = name.equals(ATTR_DIGITS);
    146                     break;
    147                 }
    148                 case 'e': {
    149                     if (name.equals(ATTR_EDITABLE)) {
    150                         isEditAttribute = attribute.getValue().equals(VALUE_TRUE);
    151                     } else {
    152                         isEditAttribute = name.equals(ATTR_EDITOR_EXTRAS);
    153                     }
    154                     break;
    155                 }
    156                 case 'i': {
    157                     if (name.equals(ATTR_INPUT_TYPE)) {
    158                         String value = attribute.getValue();
    159                         isEditAttribute = !value.isEmpty() && !value.equals(VALUE_NONE);
    160                     } else {
    161                         isEditAttribute = name.equals(ATTR_INPUT_TYPE)
    162                                 || name.equals(ATTR_IME_OPTIONS)
    163                                 || name.equals(ATTR_IME_ACTION_LABEL)
    164                                 || name.equals(ATTR_IME_ACTION_ID)
    165                                 || name.equals(ATTR_INPUT_METHOD);
    166                     }
    167                     break;
    168                 }
    169                 case 'n': {
    170                     isEditAttribute = name.equals(ATTR_NUMERIC);
    171                     break;
    172                 }
    173             }
    174 
    175             if (isEditAttribute && ANDROID_URI.equals(attribute.getNamespaceURI())) {
    176                 Location location = context.getLocation(attribute);
    177                 String message;
    178                 String view = element.getTagName();
    179                 if (view.equals(TEXT_VIEW)) {
    180                     message = String.format(
    181                             "Attribute %1$s should not be used with <TextView>: " +
    182                             "Change element type to <EditText> ?", attribute.getName());
    183                 } else {
    184                     message = String.format(
    185                             "Attribute %1$s should not be used with <%2$s>: " +
    186                             "intended for editable text widgets",
    187                             attribute.getName(), view);
    188                 }
    189                 context.report(ISSUE, attribute, location, message, null);
    190             }
    191         }
    192     }
    193 }
    194