aboutsummaryrefslogtreecommitdiffstats
path: root/lint/libs/lint_checks/src/com/android/tools/lint/checks/TooManyViewsDetector.java
blob: 3256b91f4d48a49b86a4e8c69a4e657b4becd32a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tools.lint.checks;

import com.android.annotations.NonNull;
import com.android.tools.lint.detector.api.Category;
import com.android.tools.lint.detector.api.Context;
import com.android.tools.lint.detector.api.Issue;
import com.android.tools.lint.detector.api.LayoutDetector;
import com.android.tools.lint.detector.api.Scope;
import com.android.tools.lint.detector.api.Severity;
import com.android.tools.lint.detector.api.Speed;
import com.android.tools.lint.detector.api.XmlContext;

import org.w3c.dom.Element;

import java.util.Collection;

/**
 * Checks whether a root FrameLayout can be replaced with a {@code <merge>} tag.
 */
public class TooManyViewsDetector extends LayoutDetector {
    /** Issue of having too many views in a single layout */
    public static final Issue TOO_MANY = Issue.create(
            "TooManyViews", //$NON-NLS-1$
            "Checks whether a layout has too many views",
            "Using too many views in a single layout in a layout is bad for " +
            "performance. Consider using compound drawables or other tricks for " +
            "reducing the number of views in this layout.\n\n" +
            "The maximum view count defaults to 80 but can be configured with the " +
            "environment variable `ANDROID_LINT_MAX_VIEW_COUNT`.",
            Category.PERFORMANCE,
            1,
            Severity.WARNING,
            TooManyViewsDetector.class,
            Scope.RESOURCE_FILE_SCOPE);

    /** Issue of having too deep hierarchies in layouts */
    public static final Issue TOO_DEEP = Issue.create(
            "TooDeepLayout", //$NON-NLS-1$
            "Checks whether a layout hierarchy is too deep",
            "Layouts with too much nesting is bad for performance. " +
            "Consider using a flatter layout (such as `RelativeLayout` or `GridLayout`)." +
            "The default maximum depth is 10 but can be configured with the environment " +
            "variable `ANDROID_LINT_MAX_DEPTH`.",
            Category.PERFORMANCE,
            1,
            Severity.WARNING,
            TooManyViewsDetector.class,
            Scope.RESOURCE_FILE_SCOPE);

    private static final int MAX_VIEW_COUNT;
    private static final int MAX_DEPTH;
    static {
        int maxViewCount = 0;
        int maxDepth = 0;

        String countValue = System.getenv("ANDROID_LINT_MAX_VIEW_COUNT"); //$NON-NLS-1$
        if (countValue != null) {
            try {
                maxViewCount = Integer.parseInt(countValue);
            } catch (NumberFormatException nufe) {
            }
        }
        String depthValue = System.getenv("ANDROID_LINT_MAX_DEPTH"); //$NON-NLS-1$
        if (depthValue != null) {
            try {
                maxDepth = Integer.parseInt(depthValue);
            } catch (NumberFormatException nufe) {
            }
        }
        if (maxViewCount == 0) {
            maxViewCount = 80;
        }
        if (maxDepth == 0) {
            maxDepth = 10;
        }

        MAX_VIEW_COUNT = maxViewCount;
        MAX_DEPTH = maxDepth;
    }

    private int mViewCount;
    private int mDepth;
    private boolean mWarnedAboutDepth;

    /** Constructs a new {@link TooManyViewsDetector} */
    public TooManyViewsDetector() {
    }

    @Override
    public @NonNull Speed getSpeed() {
        return Speed.FAST;
    }

    @Override
    public void beforeCheckFile(@NonNull Context context) {
        mViewCount = mDepth = 0;
        mWarnedAboutDepth = false;
    }

    @Override
    public Collection<String> getApplicableElements() {
        return ALL;
    }

    @Override
    public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
        mViewCount++;
        mDepth++;

        if (mDepth == MAX_DEPTH && !mWarnedAboutDepth) {
            // Have to record whether or not we've warned since we could have many siblings
            // at the max level and we'd warn for each one. No need to do the same thing
            // for the view count error since we'll only have view count exactly equal the
            // max just once.
            mWarnedAboutDepth = true;
            String msg = String.format("%1$s has more than %2$d levels, bad for performance",
                    context.file.getName(), MAX_DEPTH);
            context.report(TOO_DEEP, element, context.getLocation(element), msg, null);
        }
        if (mViewCount == MAX_VIEW_COUNT) {
            String msg = String.format("%1$s has more than %2$d views, bad for performance",
                    context.file.getName(), MAX_VIEW_COUNT);
            context.report(TOO_MANY, element, context.getLocation(element), msg, null);
        }
    }

    @Override
    public void visitElementAfter(@NonNull XmlContext context, @NonNull Element element) {
        mDepth--;
    }
}