|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 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.example.google.lint; |
| 18 | + |
| 19 | +import com.android.annotations.NonNull; |
| 20 | +import com.android.tools.lint.detector.api.Category; |
| 21 | +import com.android.tools.lint.detector.api.Context; |
| 22 | +import com.android.tools.lint.detector.api.Detector; |
| 23 | +import com.android.tools.lint.detector.api.Implementation; |
| 24 | +import com.android.tools.lint.detector.api.Issue; |
| 25 | +import com.android.tools.lint.detector.api.LintUtils; |
| 26 | +import com.android.tools.lint.detector.api.Location; |
| 27 | +import com.android.tools.lint.detector.api.ResourceXmlDetector; |
| 28 | +import com.android.tools.lint.detector.api.Scope; |
| 29 | +import com.android.tools.lint.detector.api.Severity; |
| 30 | +import com.android.tools.lint.detector.api.XmlContext; |
| 31 | + |
| 32 | +import org.w3c.dom.Element; |
| 33 | +import org.w3c.dom.Node; |
| 34 | + |
| 35 | +import java.util.Collection; |
| 36 | +import java.util.Collections; |
| 37 | +import java.util.EnumSet; |
| 38 | + |
| 39 | +import static com.android.SdkConstants.ANDROID_MANIFEST_XML; |
| 40 | +import static com.android.SdkConstants.ANDROID_URI; |
| 41 | +import static com.android.SdkConstants.ATTR_NAME; |
| 42 | +import static com.android.SdkConstants.TAG_ACTIVITY; |
| 43 | +import static com.android.SdkConstants.TAG_APPLICATION; |
| 44 | +import static com.android.SdkConstants.TAG_INTENT_FILTER; |
| 45 | +import static com.android.xml.AndroidManifest.NODE_ACTION; |
| 46 | +import static com.android.xml.AndroidManifest.NODE_CATEGORY; |
| 47 | +import static com.example.google.lint.ManifestConstants.ACTION_NAME_MAIN; |
| 48 | +import static com.example.google.lint.ManifestConstants.CATEGORY_NAME_LAUNCHER; |
| 49 | + |
| 50 | +/** |
| 51 | + * Checks for an activity with a launcher intent in <code>AndroidManifest.xml</code>. |
| 52 | + * <p/> |
| 53 | + * <b>NOTE: This is not a final API; if you rely on this be prepared |
| 54 | + * to adjust your code for the next tools release.</b> |
| 55 | + */ |
| 56 | +public class MainActivityDetector extends ResourceXmlDetector implements Detector.XmlScanner { |
| 57 | + public static final Issue ISSUE = Issue.create( |
| 58 | + "MainActivityDetector", |
| 59 | + "Missing Launcher Activities", |
| 60 | + "This app should have an activity with a launcher intent.", |
| 61 | + Category.CORRECTNESS, |
| 62 | + 8, |
| 63 | + Severity.ERROR, |
| 64 | + new Implementation( |
| 65 | + MainActivityDetector.class, |
| 66 | + Scope.MANIFEST_SCOPE)); |
| 67 | + |
| 68 | + /** |
| 69 | + * This will be <code>true</code> if the current file we're checking has at least one activity. |
| 70 | + */ |
| 71 | + private boolean mHasActivity; |
| 72 | + /** |
| 73 | + * This will be <code>true</code> if the file has an activity with a launcher intent. |
| 74 | + */ |
| 75 | + private boolean mHasLauncherActivity; |
| 76 | + /** |
| 77 | + * The manifest file location for the main project, <code>null</code> if there is no manifest. |
| 78 | + */ |
| 79 | + private Location mManifestLocation; |
| 80 | + |
| 81 | + /** |
| 82 | + * No-args constructor used by the lint framework to instantiate the detector. |
| 83 | + */ |
| 84 | + public MainActivityDetector() { |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public Collection<String> getApplicableElements() { |
| 89 | + java.util.List<String> s = new java.util.ArrayList<String>(2); |
| 90 | + s.add(TAG_ACTIVITY); |
| 91 | + s.add("manifest"); |
| 92 | + return s; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void beforeCheckProject(@NonNull Context context) { |
| 97 | + mHasActivity = false; |
| 98 | + mHasLauncherActivity = false; |
| 99 | + mManifestLocation = null; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void afterCheckProject(@NonNull Context context) { |
| 104 | + // Don't report issues on libraries that may not have a launcher activity |
| 105 | + if (context.getProject() == context.getMainProject() |
| 106 | + && !context.getMainProject().isLibrary() |
| 107 | + && mManifestLocation != null) { |
| 108 | + if (!mHasActivity) { |
| 109 | + context.report(ISSUE, mManifestLocation, |
| 110 | + "Expecting " + ANDROID_MANIFEST_XML + " to have an <" + TAG_ACTIVITY + |
| 111 | + "> tag."); |
| 112 | + } else if (!mHasLauncherActivity) { |
| 113 | + // Report the issue if the manifest file has no activity with a launcher intent. |
| 114 | + context.report(ISSUE, mManifestLocation, |
| 115 | + "Expecting " + ANDROID_MANIFEST_XML + |
| 116 | + " to have an activity with a launcher intent."); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void afterCheckFile(@NonNull Context context) { |
| 123 | + // Store a reference to the manifest file in case we need to report it's location. |
| 124 | + if (context.getProject() == context.getMainProject() |
| 125 | + && mManifestLocation == null) { |
| 126 | + mManifestLocation = Location.create(context.file); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void visitElement(XmlContext context, Element activityElement) { |
| 132 | + // Checks every activity and reports an error if there is no activity with a launcher |
| 133 | + // intent. |
| 134 | + mHasActivity = true; |
| 135 | + if (activityElement.getNodeName().equals("manifest")) { |
| 136 | + System.out.println("setting manifest"); |
| 137 | + mManifestLocation = context.getLocation(activityElement); |
| 138 | + } |
| 139 | + if (isMainActivity(activityElement)) { |
| 140 | + mHasLauncherActivity = true; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Returns true if the XML node is an activity with a launcher intent. |
| 146 | + * |
| 147 | + * @param activityNode The node to check. |
| 148 | + * @return <code>true</code> if the node is an activity with a launcher intent. |
| 149 | + */ |
| 150 | + private boolean isMainActivity(Node activityNode) { |
| 151 | + if (TAG_ACTIVITY.equals(activityNode.getNodeName())) { |
| 152 | + // Loop through all <intent-filter> tags |
| 153 | + for (Element activityChild : LintUtils.getChildren(activityNode)) { |
| 154 | + if (TAG_INTENT_FILTER.equals(activityChild.getNodeName())) { |
| 155 | + // Check for these children nodes: |
| 156 | + // |
| 157 | + // <category android:name="android.intent.category.LAUNCHER" /> |
| 158 | + // <action android:name="android.intent.action.MAIN" /> |
| 159 | + boolean hasLauncherCategory = false; |
| 160 | + boolean hasMainAction = false; |
| 161 | + |
| 162 | + for (Element intentFilterChild : LintUtils.getChildren(activityChild)) { |
| 163 | + // Check for category tag) |
| 164 | + if (NODE_CATEGORY.equals(intentFilterChild.getNodeName()) |
| 165 | + && CATEGORY_NAME_LAUNCHER.equals( |
| 166 | + intentFilterChild.getAttributeNS(ANDROID_URI, ATTR_NAME))) { |
| 167 | + hasLauncherCategory = true; |
| 168 | + } |
| 169 | + // Check for action tag |
| 170 | + if (NODE_ACTION.equals(intentFilterChild.getNodeName()) |
| 171 | + && ACTION_NAME_MAIN.equals( |
| 172 | + intentFilterChild.getAttributeNS(ANDROID_URI, ATTR_NAME))) { |
| 173 | + hasMainAction = true; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + if (hasLauncherCategory && hasMainAction) { |
| 178 | + return true; |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + return false; |
| 184 | + } |
| 185 | +} |
0 commit comments