com.android.tools.lint.detector.api.Context Java Examples
The following examples show how to use
com.android.tools.lint.detector.api.Context.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LauncherActivityDetector.java From lewis with Apache License 2.0 | 6 votes |
@Override public void afterCheckProject(@NonNull Context context) { // if it's not a library, it's an application if (context.getProject() == context.getMainProject() && !context.getMainProject().isLibrary() && mApplicationTagLocation != null) { if (!mHasActivity) { context.report(ISSUE_MISSING_LAUNCHER, mApplicationTagLocation, "Expecting " + ANDROID_MANIFEST_XML + " to have an <" + TAG_ACTIVITY + "> tag."); } else if (!mHasLauncherActivity) { context.report(ISSUE_MISSING_LAUNCHER, mApplicationTagLocation, "Expecting " + ANDROID_MANIFEST_XML + " to have an activity with a launcher intent."); } } }
Example #2
Source File: GradleDetector.java From javaide with GNU General Public License v3.0 | 6 votes |
private void checkPlayServices(Context context, GradleCoordinate dependency, Object cookie) { String groupId = dependency.getGroupId(); String artifactId = dependency.getArtifactId(); assert groupId != null && artifactId != null; File sdkHome = context.getClient().getSdkHome(); File repository = SdkMavenRepository.GOOGLE.getRepositoryLocation(sdkHome, true); if (repository == null) { report(context, cookie, DEPENDENCY, "Dependency on Play Services, but the SDK installation does not " + "have the \"Extras > Google Repository\" installed. " + "Open the SDK manager and install it."); } else { checkLocalMavenVersions(context, dependency, cookie, groupId, artifactId, repository); } }
Example #3
Source File: PrivateResourceDetector.java From javaide with GNU General Public License v3.0 | 6 votes |
/** Pick a suitable name to describe the library defining the private resource */ @Nullable private static String getLibraryName(@NonNull Context context, @NonNull ResourceType type, @NonNull String name) { ResourceVisibilityLookup lookup = context.getProject().getResourceVisibility(); AndroidLibrary library = lookup.getPrivateIn(type, name); if (library != null) { String libraryName = library.getProject(); if (libraryName != null) { return libraryName; } MavenCoordinates coordinates = library.getResolvedCoordinates(); if (coordinates != null) { return coordinates.getGroupId() + ':' + coordinates.getArtifactId(); } } return "the library"; }
Example #4
Source File: PropertyFileDetector.java From javaide with GNU General Public License v3.0 | 6 votes |
@Override public void run(@NonNull Context context) { String contents = context.getContents(); if (contents == null) { return; } int offset = 0; Iterator<String> iterator = Splitter.on('\n').split(contents).iterator(); String line; for (; iterator.hasNext(); offset += line.length() + 1) { line = iterator.next(); if (line.startsWith("#") || line.startsWith(" ")) { continue; } if (line.indexOf('\\') == -1 && line.indexOf(':') == -1) { continue; } int valueStart = line.indexOf('=') + 1; if (valueStart == 0) { continue; } checkLine(context, contents, line, offset, valueStart); } }
Example #5
Source File: ViewTypeDetector.java From javaide with GNU General Public License v3.0 | 6 votes |
@Nullable private Multimap<String, String> getIdToTagsIn(@NonNull Context context, @NonNull File file) { if (!file.getPath().endsWith(DOT_XML)) { return null; } if (mFileIdMap == null) { mFileIdMap = Maps.newHashMap(); } Multimap<String, String> map = mFileIdMap.get(file); if (map == null) { map = ArrayListMultimap.create(); mFileIdMap.put(file, map); String xml = context.getClient().readFile(file); // TODO: Use pull parser instead for better performance! Document document = XmlUtils.parseDocumentSilently(xml, true); if (document != null && document.getDocumentElement() != null) { addViewTags(map, document.getDocumentElement()); } } return map; }
Example #6
Source File: GradleDetector.java From javaide with GNU General Public License v3.0 | 6 votes |
protected void checkOctal( @NonNull Context context, @NonNull String value, @NonNull Object cookie) { if (value.length() >= 2 && value.charAt(0) == '0' && (value.length() > 2 || value.charAt(1) >= '8' && isInteger(value)) && context.isEnabled(ACCIDENTAL_OCTAL)) { String message = "The leading 0 turns this number into octal which is probably " + "not what was intended"; try { long numericValue = Long.decode(value); message += " (interpreted as " + numericValue + ")"; } catch (NumberFormatException nufe) { message += " (and it is not a valid octal number)"; } report(context, cookie, ACCIDENTAL_OCTAL, message); } }
Example #7
Source File: ManifestDetector.java From javaide with GNU General Public License v3.0 | 6 votes |
@Override public void afterCheckFile(@NonNull Context context) { XmlContext xmlContext = (XmlContext) context; Element element = xmlContext.document.getDocumentElement(); if (element != null) { checkDocumentElement(xmlContext, element); } if (mSeenUsesSdk == 0 && context.isEnabled(USES_SDK) // Not required in Gradle projects; typically defined in build.gradle instead // and inserted at build time && !context.getMainProject().isGradleProject()) { context.report(USES_SDK, Location.create(context.file), "Manifest should specify a minimum API level with " + "`<uses-sdk android:minSdkVersion=\"?\" />`; if it really supports " + "all versions of Android set it to 1."); } }
Example #8
Source File: LintDriver.java From javaide with GNU General Public License v3.0 | 6 votes |
private void checkBuildScripts(Project project, Project main) { List<Detector> detectors = mScopeDetectors.get(Scope.GRADLE_FILE); if (detectors != null) { List<File> files = project.getSubset(); if (files == null) { files = project.getGradleBuildScripts(); } for (File file : files) { Context context = new Context(this, project, main, file); fireEvent(EventType.SCANNING_FILE, context); for (Detector detector : detectors) { if (detector.appliesTo(context, file)) { detector.beforeCheckFile(context); detector.visitBuildScript(context, Maps.<String, Object>newHashMap()); detector.afterCheckFile(context); } } } } }
Example #9
Source File: GradleDetector.java From javaide with GNU General Public License v3.0 | 6 votes |
@Nullable private static GradleCoordinate resolveCoordinate(@NonNull Context context, @NonNull GradleCoordinate gc) { assert gc.getFullRevision().contains("$") : gc.getFullRevision(); Variant variant = context.getProject().getCurrentVariant(); if (variant != null) { Dependencies dependencies = variant.getMainArtifact().getDependencies(); for (AndroidLibrary library : dependencies.getLibraries()) { MavenCoordinates mc = library.getResolvedCoordinates(); if (mc != null && mc.getGroupId().equals(gc.getGroupId()) && mc.getArtifactId().equals(gc.getArtifactId())) { List<RevisionComponent> revisions = GradleCoordinate.parseRevisionNumber(mc.getVersion()); if (!revisions.isEmpty()) { return new GradleCoordinate(mc.getGroupId(), mc.getArtifactId(), revisions, null); } break; } } } return null; }
Example #10
Source File: PrivateKeyDetector.java From javaide with GNU General Public License v3.0 | 6 votes |
@Override public void run(@NonNull Context context) { if (!context.getProject().getReportIssues()) { // If this is a library project not being analyzed, ignore it return; } File file = context.file; if (isPrivateKeyFile(file)) { String fileName = file.getParentFile().getName() + File.separator + file.getName(); String message = String.format( "The `%1$s` file seems to be a private key file. " + "Please make sure not to embed this in your APK file.", fileName); context.report(ISSUE, Location.create(file), message); } }
Example #11
Source File: UnusedResourceDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
@Override public void beforeCheckProject(@NonNull Context context) { if (context.getPhase() == 1) { mDeclarations = new HashSet<String>(300); mReferences = new HashSet<String>(300); } }
Example #12
Source File: MainActivityDetector.java From android-custom-lint-rules with Apache License 2.0 | 5 votes |
@Override public void afterCheckFile(@NonNull Context context) { // Store a reference to the manifest file in case we need to report it's location. if (context.getProject() == context.getMainProject()) { mManifestLocation = Location.create(context.file); } }
Example #13
Source File: GradleDetectorTest.java From javaide with GNU General Public License v3.0 | 5 votes |
@Override protected void checkReportedError(@NonNull Context context, @NonNull Issue issue, @NonNull Severity severity, @Nullable Location location, @NonNull String message) { if (issue == DEPENDENCY && message.startsWith("Using the appcompat library when ")) { // No data embedded in this specific message return; } // Issues we're supporting getOldFrom if (issue == DEPENDENCY || issue == STRING_INTEGER || issue == DEPRECATED || issue == PLUS) { assertNotNull("Could not extract message tokens from " + message, GradleDetector.getOldValue(issue, message, TEXT)); } if (issue == DEPENDENCY || issue == STRING_INTEGER || issue == DEPRECATED) { assertNotNull("Could not extract message tokens from " + message, GradleDetector.getNewValue(issue, message, TEXT)); } if (issue == COMPATIBILITY) { if (message.startsWith("Version ")) { assertNotNull("Could not extract message tokens from " + message, GradleDetector.getNewValue(issue, message, TEXT)); } } }
Example #14
Source File: ManifestDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
@Nullable private Location getMainApplicationTagLocation(@NonNull Context context) { if (mApplicationTagHandle != null) { return mApplicationTagHandle.resolve(); } List<File> manifestFiles = context.getMainProject().getManifestFiles(); if (!manifestFiles.isEmpty()) { return Location.create(manifestFiles.get(0)); } return null; }
Example #15
Source File: TypographyDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
@Override public void beforeCheckProject(@NonNull Context context) { mCheckDashes = context.isEnabled(DASHES); mCheckQuotes = context.isEnabled(QUOTES); mCheckFractions = context.isEnabled(FRACTIONS); mCheckEllipsis = context.isEnabled(ELLIPSIS); mCheckMisc = context.isEnabled(OTHER); }
Example #16
Source File: DefaultConfiguration.java From javaide with GNU General Public License v3.0 | 5 votes |
private void formatError(String message, Object... args) { if (args != null && args.length > 0) { message = String.format(message, args); } message = "Failed to parse `lint.xml` configuration file: " + message; LintDriver driver = new LintDriver(new IssueRegistry() { @Override @NonNull public List<Issue> getIssues() { return Collections.emptyList(); } }, mClient); mClient.report(new Context(driver, mProject, mProject, mConfigFile), IssueRegistry.LINT_ERROR, mProject.getConfiguration(driver).getSeverity(IssueRegistry.LINT_ERROR), Location.create(mConfigFile), message, TextFormat.RAW); }
Example #17
Source File: GroovyGradleDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
@Override public void visitBuildScript(@NonNull final Context context, Map<String, Object> sharedData) { try { visitQuietly(context, sharedData); } catch (Throwable t) { // ignore // Parsing the build script can involve class loading that we sometimes can't // handle. This happens for example when running lint in build-system/tests/api/. // This is a lint limitation rather than a user error, so don't complain // about these. Consider reporting a Issue#LINT_ERROR. } }
Example #18
Source File: LintDriver.java From javaide with GNU General Public License v3.0 | 5 votes |
/** Notifies listeners, if any, that the given event has occurred */ private void fireEvent(@NonNull LintListener.EventType type, @Nullable Context context) { if (mListeners != null) { for (LintListener listener : mListeners) { listener.update(this, type, context); } } }
Example #19
Source File: LintCliClient.java From javaide with GNU General Public License v3.0 | 5 votes |
protected void reportNonExistingIssueId(@Nullable Project project, @NonNull String id) { String message = String.format("Unknown issue id \"%1$s\"", id); if (mDriver != null && project != null) { Location location = Location.create(project.getDir()); if (!isSuppressed(IssueRegistry.LINT_ERROR)) { report(new Context(mDriver, project, project, project.getDir()), IssueRegistry.LINT_ERROR, project.getConfiguration(mDriver).getSeverity(IssueRegistry.LINT_ERROR), location, message, TextFormat.RAW); } } else { log(Severity.ERROR, null, "Lint: %1$s", message); } }
Example #20
Source File: GradleDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
private void checkSupportLibraries(Context context, GradleCoordinate dependency, Object cookie) { String groupId = dependency.getGroupId(); String artifactId = dependency.getArtifactId(); assert groupId != null && artifactId != null; // See if the support library version is lower than the targetSdkVersion if (mTargetSdkVersion > 0 && dependency.getMajorVersion() < mTargetSdkVersion && dependency.getMajorVersion() != GradleCoordinate.PLUS_REV_VALUE && // The multidex library doesn't follow normal supportlib numbering scheme !dependency.getArtifactId().startsWith("multidex") && context.isEnabled(COMPATIBILITY)) { String message = "This support library should not use a lower version (" + dependency.getMajorVersion() + ") than the `targetSdkVersion` (" + mTargetSdkVersion + ")"; report(context, cookie, COMPATIBILITY, message); } // Check to make sure you have the Android support repository installed File sdkHome = context.getClient().getSdkHome(); File repository = SdkMavenRepository.ANDROID.getRepositoryLocation(sdkHome, true); if (repository == null) { report(context, cookie, DEPENDENCY, "Dependency on a support library, but the SDK installation does not " + "have the \"Extras > Android Support Repository\" installed. " + "Open the SDK manager and install it."); } else { checkLocalMavenVersions(context, dependency, cookie, groupId, artifactId, repository); } }
Example #21
Source File: OverdrawDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
private void scanBitmap(Context context, Element element) { String tileMode = element.getAttributeNS(ANDROID_URI, ATTR_TILE_MODE); if (!(tileMode.equals(VALUE_DISABLED) || tileMode.isEmpty())) { if (mValidDrawables != null) { String resource = getDrawableResource(context.file); mValidDrawables.remove(resource); } } }
Example #22
Source File: GradleDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
private boolean checkGradlePluginDependency(Context context, GradleCoordinate dependency, Object cookie) { GradleCoordinate latestPlugin = GradleCoordinate.parseCoordinateString( SdkConstants.GRADLE_PLUGIN_NAME + GRADLE_PLUGIN_MINIMUM_VERSION); if (GradleCoordinate.COMPARE_PLUS_HIGHER.compare(dependency, latestPlugin) < 0) { String message = "You must use a newer version of the Android Gradle plugin. The " + "minimum supported version is " + GRADLE_PLUGIN_MINIMUM_VERSION + " and the recommended version is " + GRADLE_PLUGIN_RECOMMENDED_VERSION; report(context, cookie, GRADLE_PLUGIN_COMPATIBILITY, message); return true; } return false; }
Example #23
Source File: RequiredAttributeDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
@Override public void afterCheckProject(@NonNull Context context) { // Process checks in two phases: // Phase 1: Gather styles and includes (styles are encountered after the layouts // so we can't do it in a single phase, and includes can be affected by includes from // layouts we haven't seen yet) // Phase 2: Process layouts, using gathered style and include data, and mark layouts // not known. // if (context.getPhase() == 1) { checkSizeSetInTheme(); context.requestRepeat(this, Scope.RESOURCE_FILE_SCOPE); } }
Example #24
Source File: DuplicateResourceDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
@Override public void beforeCheckFile(@NonNull Context context) { File parent = context.file.getParentFile(); if (!parent.equals(mParent)) { mParent = parent; mTypeMap = Maps.newEnumMap(ResourceType.class); mLocations = Maps.newEnumMap(ResourceType.class); } }
Example #25
Source File: ApiDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
protected int getMinSdk(Context context) { if (mMinApi == -1) { AndroidVersion minSdkVersion = context.getMainProject().getMinSdkVersion(); mMinApi = minSdkVersion.getFeatureLevel(); } return mMinApi; }
Example #26
Source File: ApiDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
@Override public void afterCheckProject(@NonNull Context context) { if (mPendingFields != null) { for (List<Pair<String, Location>> list : mPendingFields.values()) { for (Pair<String, Location> pair : list) { String message = pair.getFirst(); Location location = pair.getSecond(); context.report(INLINED, location, message); } } } super.afterCheckProject(context); }
Example #27
Source File: LintDetectorTest.java From javaide with GNU General Public License v3.0 | 5 votes |
protected void checkReportedError( @NonNull Context context, @NonNull Issue issue, @NonNull Severity severity, @Nullable Location location, @NonNull String message) { }
Example #28
Source File: StringFormatDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
@Override public boolean appliesTo(@NonNull Context context, @NonNull File file) { if (LintUtils.endsWith(file.getName(), DOT_JAVA)) { return mFormatStrings != null; } return super.appliesTo(context, file); }
Example #29
Source File: MinSdkDetector.java From linette with Apache License 2.0 | 5 votes |
@Override public void afterCheckProject(Context context) { super.afterCheckProject(context); int minSdk = context.getProject().getMinSdk(); if (minSdk != -1 && minSdk < SUGGESTED_MIN_SDK_VERSION) { context.report(ISSUE, Location.create(context.file), ISSUE.getBriefDescription(TextFormat.TEXT)); } }
Example #30
Source File: AlwaysShowActionDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
@Override public void afterCheckProject(@NonNull Context context) { if (mAlwaysFields != null && !mHasIfRoomRefs) { for (Location location : mAlwaysFields) { context.report(ISSUE, location, "Prefer \"`SHOW_AS_ACTION_IF_ROOM`\" instead of \"`SHOW_AS_ACTION_ALWAYS`\""); } } }