Search in sources :

Example 1 with AddTargetVersionCheckQuickFix

use of com.android.tools.idea.lint.AddTargetVersionCheckQuickFix in project android by JetBrains.

the class ResourceTypeInspection method checkApiLevel.

private static void checkApiLevel(@NotNull PsiCall methodCall, @NotNull PsiMethod method, @NotNull ProblemsHolder holder, @NotNull PsiAnnotation annotation, @NotNull PsiAnnotation[] allMethodAnnotations, @NonNull PsiAnnotation[] allClassAnnotations) {
    // Don't inherit these annotations; see for example b/32952309
    PsiAnnotationOwner owner = annotation.getOwner();
    if (owner == null || !(method.getModifierList().equals(owner) || method.getContainingClass() != null && owner.equals(method.getContainingClass().getModifierList()))) {
        return;
    }
    // if there is another annotation specified on the method.
    if (containsAnnotation(allClassAnnotations, annotation) && containsAnnotation(allMethodAnnotations, REQUIRES_API_ANNOTATION)) {
        return;
    }
    AndroidFacet facet = AndroidFacet.getInstance(methodCall);
    // already checked early on in the inspection visitor
    assert facet != null;
    AndroidVersion minSdkVersion = AndroidModuleInfo.get(facet).getMinSdkVersion();
    PsiAnnotationMemberValue apiValue = annotation.findAttributeValue(ATTR_VALUE);
    if (apiValue == null || (int) getLongValue(apiValue, 1) == 1) {
        // Look for both value= and api= (they are aliases, though  value of 1 is meaningless)
        apiValue = annotation.findAttributeValue("api");
    }
    int api = (int) getLongValue(apiValue, 1);
    if (api <= 1) {
        return;
    }
    int minSdk = minSdkVersion.getFeatureLevel();
    if (api <= minSdk) {
        return;
    }
    int target = ApiDetector.getTargetApi(methodCall);
    if (target != -1) {
        if (api <= target) {
            return;
        }
    }
    if (LintIdeUtils.isSuppressed(methodCall, UNSUPPORTED)) {
        return;
    }
    if (VersionChecks.isWithinVersionCheckConditional(methodCall, api)) {
        return;
    }
    if (VersionChecks.isPrecededByVersionCheckExit(methodCall, api)) {
        return;
    }
    PsiClass containingClass = method.getContainingClass();
    String fqcn = containingClass != null ? containingClass.getQualifiedName() : "";
    // Keep in sync with guessLintIssue
    String message = String.format("Call requires API level %1$d (current min is %2$d): %3$s", api, minSdk, fqcn + '#' + method.getName());
    LocalQuickFix versionCheck = new AndroidLintQuickFix.LocalFixWrapper(new AddTargetVersionCheckQuickFix(api), methodCall, methodCall);
    LocalQuickFix addTargetApiQuickFix = new AndroidLintQuickFix.LocalFixWrapper(new AddTargetApiQuickFix(api, false, methodCall), methodCall, methodCall);
    LocalQuickFix addRequiresApiQuickFix = new AndroidLintQuickFix.LocalFixWrapper(new AddTargetApiQuickFix(api, true, methodCall), methodCall, methodCall);
    registerProblem(holder, UNSUPPORTED, methodCall, message, versionCheck, addRequiresApiQuickFix, addTargetApiQuickFix);
}
Also used : AddTargetVersionCheckQuickFix(com.android.tools.idea.lint.AddTargetVersionCheckQuickFix) AddTargetApiQuickFix(com.android.tools.idea.lint.AddTargetApiQuickFix) AndroidVersion(com.android.sdklib.AndroidVersion) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Aggregations

AndroidVersion (com.android.sdklib.AndroidVersion)1 AddTargetApiQuickFix (com.android.tools.idea.lint.AddTargetApiQuickFix)1 AddTargetVersionCheckQuickFix (com.android.tools.idea.lint.AddTargetVersionCheckQuickFix)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1