Track Resources.getColorStateList calls
Bug: 147874909
Test: ./gradlew :lint-demo:lint-demo-appcompat:lint
Change-Id: I9984fe61c5a93ad7a1492becab869a52972f7984
diff --git a/appcompat/appcompat-lint/build.gradle b/appcompat/appcompat-lint/build.gradle
index 9b14d99..da60f47 100644
--- a/appcompat/appcompat-lint/build.gradle
+++ b/appcompat/appcompat-lint/build.gradle
@@ -28,6 +28,10 @@
dependencies {
compileOnly LINT_API_LATEST
compileOnly KOTLIN_STDLIB
+
+ testImplementation KOTLIN_STDLIB
+ testImplementation LINT_CORE
+ testImplementation LINT_TESTS
}
androidx {
diff --git a/appcompat/appcompat-lint/src/main/java/androidx/appcompat/AppCompatIssueRegistry.kt b/appcompat/appcompat-lint/src/main/java/androidx/appcompat/AppCompatIssueRegistry.kt
index d4411aa..bdc7ed8 100644
--- a/appcompat/appcompat-lint/src/main/java/androidx/appcompat/AppCompatIssueRegistry.kt
+++ b/appcompat/appcompat-lint/src/main/java/androidx/appcompat/AppCompatIssueRegistry.kt
@@ -17,6 +17,7 @@
package androidx.appcompat
import androidx.appcompat.res.ColorStateListAlphaDetector
+import androidx.appcompat.res.ColorStateListLoadingDetector
import androidx.appcompat.res.ImageViewTintDetector
import com.android.tools.lint.client.api.IssueRegistry
import com.android.tools.lint.detector.api.CURRENT_API
@@ -25,6 +26,7 @@
override val api = CURRENT_API
override val issues get() = listOf(
ColorStateListAlphaDetector.NOT_USING_ANDROID_ALPHA,
+ ColorStateListLoadingDetector.NOT_USING_COMPAT_LOADING,
ImageViewTintDetector.USING_ANDROID_TINT
)
}
diff --git a/appcompat/appcompat-lint/src/main/java/androidx/appcompat/res/ColorStateListLoadingDetector.kt b/appcompat/appcompat-lint/src/main/java/androidx/appcompat/res/ColorStateListLoadingDetector.kt
new file mode 100644
index 0000000..c584ccf
--- /dev/null
+++ b/appcompat/appcompat-lint/src/main/java/androidx/appcompat/res/ColorStateListLoadingDetector.kt
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2020 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 androidx.appcompat.res
+
+import com.android.tools.lint.detector.api.Category
+import com.android.tools.lint.detector.api.Detector
+import com.android.tools.lint.detector.api.Implementation
+import com.android.tools.lint.detector.api.Issue
+import com.android.tools.lint.detector.api.JavaContext
+import com.android.tools.lint.detector.api.Scope
+import com.android.tools.lint.detector.api.Severity
+import com.android.tools.lint.detector.api.SourceCodeScanner
+import com.intellij.psi.PsiMethod
+import org.jetbrains.uast.UCallExpression
+
+class ColorStateListLoadingDetector : Detector(), SourceCodeScanner {
+ companion object {
+ internal val NOT_USING_COMPAT_LOADING: Issue = Issue.create(
+ "UseCompatLoading",
+ "Should not call `Resources.getColorStateList` directly",
+ "Use Compat loading of color state lists",
+ Category.CORRECTNESS,
+ 1,
+ Severity.WARNING,
+ Implementation(ColorStateListLoadingDetector::class.java, Scope.JAVA_FILE_SCOPE)
+ )
+ }
+
+ override fun getApplicableMethodNames(): List<String>? = listOf("getColorStateList")
+
+ override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) {
+ // Only flag Resources.getColorStateList calls with one argument (parameter)
+ if (context.evaluator.isMemberInClass(method, "android.content.res.Resources") &&
+ (node.valueArgumentCount == 1)
+ ) {
+ val message = if (context.mainProject.minSdkVersion.featureLevel > 23)
+ "Use ContextCompat.getColorStateList()" else
+ "Use AppCompatResources.getColorStateList()"
+ context.report(
+ NOT_USING_COMPAT_LOADING,
+ context.getLocation(node),
+ message
+ )
+ }
+ }
+}
\ No newline at end of file
diff --git a/appcompat/appcompat-lint/src/test/java/androidx/appcompat/lint/Stubs.kt b/appcompat/appcompat-lint/src/test/java/androidx/appcompat/lint/Stubs.kt
new file mode 100644
index 0000000..d7aae48
--- /dev/null
+++ b/appcompat/appcompat-lint/src/test/java/androidx/appcompat/lint/Stubs.kt
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2020 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 androidx.appcompat.lint
+
+import com.android.tools.lint.checks.infrastructure.LintDetectorTest
+import com.android.tools.lint.checks.infrastructure.LintDetectorTest.xml
+import com.android.tools.lint.checks.infrastructure.TestFile
+
+object Stubs {
+ val APPCOMPAT_ACTIVITY: TestFile = LintDetectorTest.kotlin(
+ "androidx/appcompat/app/AppCompatActivity.kt",
+ """
+ package androidx.appcompat.app
+ import android.app.Activity
+ open class AppCompatActivity: Activity()
+ """
+ )
+ .indented().within("src")
+
+ val APPCOMPAT_RESOURCES: TestFile = LintDetectorTest.kotlin(
+ "androidx/appcompat/content/res/AppCompatActivity.kt",
+ """
+ package androidx.appcompat.content.res
+ import android.content.Context
+ import android.content.res.ColorStateList
+ class AppCompatResources {
+ companion object {
+ fun getColorStateList(content: Context, resId: Int) : ColorStateList {
+ val result: ColorStateList = TODO("Stub")
+ return result
+ }
+ }
+ }
+ """
+ )
+ .indented().within("src")
+
+ val COLOR_STATE_LIST: TestFile = xml(
+ "color/color_state_list.xml",
+ """
+<?xml version="1.0" encoding="utf-8"?>
+<selector xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:app="http://schemas.android.com/apk/res-auto">
+ <item app:alpha="?android:disabledAlpha"
+ android:color="#FF0000"
+ android:state_enabled="false"/>
+ <item android:color="#FF0000"/>
+</selector>
+ """
+ ).indented().within("res")
+}
diff --git a/appcompat/appcompat-lint/src/test/java/androidx/appcompat/lint/res/ColorStateListLoadingDetectorTest.kt b/appcompat/appcompat-lint/src/test/java/androidx/appcompat/lint/res/ColorStateListLoadingDetectorTest.kt
new file mode 100644
index 0000000..3ded86f
--- /dev/null
+++ b/appcompat/appcompat-lint/src/test/java/androidx/appcompat/lint/res/ColorStateListLoadingDetectorTest.kt
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2020 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 androidx.appcompat.lint.res
+
+import androidx.appcompat.lint.Stubs
+import androidx.appcompat.res.ColorStateListLoadingDetector
+import com.android.tools.lint.checks.infrastructure.LintDetectorTest.kotlin
+import com.android.tools.lint.checks.infrastructure.TestLintTask.lint
+import org.junit.Test
+
+class ColorStateListLoadingDetectorTest {
+ @Test
+ fun testCustomGetColorStateList() {
+ val customActivity = kotlin(
+ "com/example/CustomActivity.kt",
+ """
+ package com.example
+
+ import android.content.res.ColorStateList
+ import androidx.appcompat.app.AppCompatActivity
+ import androidx.appcompat.content.res.AppCompatResources
+
+ class ResourceLoader {
+ private fun getColorStateList(resourceId: Int): ColorStateList {
+ return AppCompatResources.getColorStateList(CustomActivity.this, resourceId)
+ }
+ }
+
+ class CustomActivity: AppCompatActivity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ ResourceLoader().getColorStateList(R.color.color_state_list)
+ }
+ }
+ """
+ ).indented().within("src")
+
+ // We expect a clean Lint run since the call to getColorStateList in activity's onCreate
+ // is on our own custom inner class
+ lint().files(
+ Stubs.APPCOMPAT_ACTIVITY,
+ Stubs.APPCOMPAT_RESOURCES,
+ Stubs.COLOR_STATE_LIST,
+ customActivity
+ ).issues(ColorStateListLoadingDetector.NOT_USING_COMPAT_LOADING)
+ .run()
+ .expectClean()
+ }
+
+ @Test
+ fun testCoreGetColorStateList() {
+ val customActivity = kotlin(
+ "com/example/CustomActivity.kt",
+ """
+ package com.example
+
+ import android.os.Bundle
+ import androidx.appcompat.app.AppCompatActivity
+
+ class CustomActivity: AppCompatActivity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ getResources().getColorStateList(R.color.color_state_list)
+ }
+ }
+ """
+ ).indented().within("src")
+
+ // We expect the call to Resources.getColorStateList to be flagged
+ lint().files(
+ Stubs.APPCOMPAT_ACTIVITY,
+ Stubs.COLOR_STATE_LIST,
+ customActivity
+ ).issues(ColorStateListLoadingDetector.NOT_USING_COMPAT_LOADING)
+ .run()
+ .expect("""
+src/com/example/CustomActivity.kt:8: Warning: Use AppCompatResources.getColorStateList() [UseCompatLoading]
+ getResources().getColorStateList(R.color.color_state_list)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+0 errors, 1 warnings
+ """.trimIndent())
+ }
+}
diff --git a/lint-demos/lint-demo-appcompat/src/main/java/com/example/android/appcompat/AppCompatLintDemo.java b/lint-demos/lint-demo-appcompat/src/main/java/com/example/android/appcompat/AppCompatLintDemo.java
index fae81c3..87642ca 100644
--- a/lint-demos/lint-demo-appcompat/src/main/java/com/example/android/appcompat/AppCompatLintDemo.java
+++ b/lint-demos/lint-demo-appcompat/src/main/java/com/example/android/appcompat/AppCompatLintDemo.java
@@ -16,16 +16,38 @@
package com.example.android.appcompat;
+import android.content.res.ColorStateList;
import android.os.Bundle;
+import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
+import androidx.appcompat.content.res.AppCompatResources;
/**
* Dummy activity for the AppCompat Lint demo
*/
public class AppCompatLintDemo extends AppCompatActivity {
+ private class ResourceLoader {
+ private ColorStateList getColorStateList(int resourceId) {
+ return AppCompatResources.getColorStateList(AppCompatLintDemo.this, resourceId);
+ }
+ }
+
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
+
+ TextView dummy = findViewById(R.id.dummy);
+ // The following call to getColorStateList should be flagged by our Lint rule, since
+ // it's on the core Android Resources class
+ ColorStateList csl =
+ getResources().getColorStateList(R.color.color_state_list_missing_android_alpha);
+ dummy.setTextColor(csl);
+
+ // The following call to getColorStateList should not be flagged by our Lint rule, since
+ // it's on our own custom inner class
+ ColorStateList csl2 = new ResourceLoader().getColorStateList(
+ R.color.color_state_list_missing_android_alpha);
+ dummy.setTextColor(csl2);
}
}
diff --git a/lint-demos/lint-demo-appcompat/src/main/res/layout/image_view_using_android_tint.xml b/lint-demos/lint-demo-appcompat/src/main/res/layout/image_view_using_android_tint.xml
index fb79632..ccbd975 100644
--- a/lint-demos/lint-demo-appcompat/src/main/res/layout/image_view_using_android_tint.xml
+++ b/lint-demos/lint-demo-appcompat/src/main/res/layout/image_view_using_android_tint.xml
@@ -20,6 +20,11 @@
android:layout_height="match_parent"
android:orientation="vertical">
+ <TextView
+ android:id="@+id/dummy"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content" />
+
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"