diff options
16 files changed, 255 insertions, 44 deletions
diff --git a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/BindServiceOnMainThreadDetector.kt b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/BindServiceOnMainThreadDetector.kt index 1d808ba7ee16..74e6d85f5374 100644 --- a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/BindServiceOnMainThreadDetector.kt +++ b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/BindServiceOnMainThreadDetector.kt @@ -59,11 +59,11 @@ class BindServiceOnMainThreadDetector : Detector(), SourceCodeScanner { !hasWorkerThreadAnnotation(context, node.getParentOfType(UClass::class.java)) ) { context.report( - ISSUE, - method, - context.getLocation(node), - "This method should be annotated with `@WorkerThread` because " + - "it calls ${method.name}", + issue = ISSUE, + location = context.getLocation(node), + message = + "This method should be annotated with `@WorkerThread` because " + + "it calls ${method.name}", ) } } diff --git a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/BroadcastSentViaContextDetector.kt b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/BroadcastSentViaContextDetector.kt index 112992913661..344d0a3f3187 100644 --- a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/BroadcastSentViaContextDetector.kt +++ b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/BroadcastSentViaContextDetector.kt @@ -52,10 +52,9 @@ class BroadcastSentViaContextDetector : Detector(), SourceCodeScanner { val evaluator = context.evaluator if (evaluator.isMemberInSubClassOf(method, CLASS_CONTEXT)) { context.report( - ISSUE, - method, - context.getNameLocation(node), - "`Context.${method.name}()` should be replaced with " + + issue = ISSUE, + location = context.getNameLocation(node), + message = "`Context.${method.name}()` should be replaced with " + "`BroadcastSender.${method.name}()`" ) } diff --git a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/NonInjectedMainThreadDetector.kt b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/NonInjectedMainThreadDetector.kt index bab76ab4bce2..14099ebef56c 100644 --- a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/NonInjectedMainThreadDetector.kt +++ b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/NonInjectedMainThreadDetector.kt @@ -38,10 +38,9 @@ class NonInjectedMainThreadDetector : Detector(), SourceCodeScanner { override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) { if (context.evaluator.isMemberInSubClassOf(method, CLASS_CONTEXT)) { context.report( - ISSUE, - method, - context.getNameLocation(node), - "Replace with injected `@Main Executor`." + issue = ISSUE, + location = context.getNameLocation(node), + message = "Replace with injected `@Main Executor`." ) } } diff --git a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/NonInjectedServiceDetector.kt b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/NonInjectedServiceDetector.kt index b62290025437..aa4b2f766bf0 100644 --- a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/NonInjectedServiceDetector.kt +++ b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/NonInjectedServiceDetector.kt @@ -44,11 +44,11 @@ class NonInjectedServiceDetector : Detector(), SourceCodeScanner { method.containingClass?.qualifiedName == CLASS_CONTEXT ) { context.report( - ISSUE, - method, - context.getNameLocation(node), - "Use `@Inject` to get system-level service handles instead of " + - "`Context.getSystemService()`" + issue = ISSUE, + location = context.getNameLocation(node), + message = + "Use `@Inject` to get system-level service handles instead of " + + "`Context.getSystemService()`" ) } else if ( evaluator.isStatic(method) && @@ -56,10 +56,10 @@ class NonInjectedServiceDetector : Detector(), SourceCodeScanner { method.containingClass?.qualifiedName == "android.accounts.AccountManager" ) { context.report( - ISSUE, - method, - context.getNameLocation(node), - "Replace `AccountManager.get()` with an injected instance of `AccountManager`" + issue = ISSUE, + location = context.getNameLocation(node), + message = + "Replace `AccountManager.get()` with an injected instance of `AccountManager`" ) } } diff --git a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/RegisterReceiverViaContextDetector.kt b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/RegisterReceiverViaContextDetector.kt index 4ba3afc7f7e2..5840e8f8dfb6 100644 --- a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/RegisterReceiverViaContextDetector.kt +++ b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/RegisterReceiverViaContextDetector.kt @@ -38,10 +38,10 @@ class RegisterReceiverViaContextDetector : Detector(), SourceCodeScanner { override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) { if (context.evaluator.isMemberInSubClassOf(method, CLASS_CONTEXT)) { context.report( - ISSUE, - method, - context.getNameLocation(node), - "Register `BroadcastReceiver` using `BroadcastDispatcher` instead of `Context`" + issue = ISSUE, + location = context.getNameLocation(node), + message = "Register `BroadcastReceiver` using `BroadcastDispatcher` instead " + + "of `Context`" ) } } diff --git a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/SlowUserQueryDetector.kt b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/SlowUserQueryDetector.kt index 7be21a512f89..b15a41b226df 100644 --- a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/SlowUserQueryDetector.kt +++ b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/SlowUserQueryDetector.kt @@ -46,10 +46,10 @@ class SlowUserQueryDetector : Detector(), SourceCodeScanner { method.containingClass?.qualifiedName == "android.app.ActivityManager" ) { context.report( - ISSUE_SLOW_USER_ID_QUERY, - method, - context.getNameLocation(node), - "Use `UserTracker.getUserId()` instead of `ActivityManager.getCurrentUser()`" + issue = ISSUE_SLOW_USER_ID_QUERY, + location = context.getNameLocation(node), + message = + "Use `UserTracker.getUserId()` instead of `ActivityManager.getCurrentUser()`" ) } if ( @@ -58,10 +58,9 @@ class SlowUserQueryDetector : Detector(), SourceCodeScanner { method.containingClass?.qualifiedName == "android.os.UserManager" ) { context.report( - ISSUE_SLOW_USER_INFO_QUERY, - method, - context.getNameLocation(node), - "Use `UserTracker.getUserInfo()` instead of `UserManager.getUserInfo()`" + issue = ISSUE_SLOW_USER_INFO_QUERY, + location = context.getNameLocation(node), + message = "Use `UserTracker.getUserInfo()` instead of `UserManager.getUserInfo()`" ) } } diff --git a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/SoftwareBitmapDetector.kt b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/SoftwareBitmapDetector.kt index 4b9aa13c0240..bf025894d66f 100644 --- a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/SoftwareBitmapDetector.kt +++ b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/SoftwareBitmapDetector.kt @@ -44,10 +44,9 @@ class SoftwareBitmapDetector : Detector(), SourceCodeScanner { val evaluator = context.evaluator if (evaluator.isMemberInClass(referenced as? PsiField, "android.graphics.Bitmap.Config")) { context.report( - ISSUE, - referenced, - context.getNameLocation(reference), - "Replace software bitmap with `Config.HARDWARE`" + issue = ISSUE, + location = context.getNameLocation(reference), + message = "Replace software bitmap with `Config.HARDWARE`" ) } } diff --git a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/StaticSettingsProviderDetector.kt b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/StaticSettingsProviderDetector.kt index 1db072548a76..22f15bdcb5bd 100644 --- a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/StaticSettingsProviderDetector.kt +++ b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/StaticSettingsProviderDetector.kt @@ -66,10 +66,9 @@ class StaticSettingsProviderDetector : Detector(), SourceCodeScanner { val subclassName = className.substring(CLASS_SETTINGS.length + 1) context.report( - ISSUE, - method, - context.getNameLocation(node), - "`@Inject` a ${subclassName}Settings instead" + issue = ISSUE, + location = context.getNameLocation(node), + message = "`@Inject` a ${subclassName}Settings instead" ) } diff --git a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/BindServiceOnMainThreadDetectorTest.kt b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/BindServiceOnMainThreadDetectorTest.kt index c35ac61a6543..426211e0f327 100644 --- a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/BindServiceOnMainThreadDetectorTest.kt +++ b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/BindServiceOnMainThreadDetectorTest.kt @@ -126,6 +126,32 @@ class BindServiceOnMainThreadDetectorTest : SystemUILintDetectorTest() { } @Test + fun testSuppressUnbindService() { + lint() + .files( + TestFiles.java( + """ + package test.pkg; + import android.content.Context; + import android.content.ServiceConnection; + + @SuppressLint("BindServiceOnMainThread") + public class TestClass { + public void unbind(Context context, ServiceConnection connection) { + context.unbindService(connection); + } + } + """ + ) + .indented(), + *stubs + ) + .issues(BindServiceOnMainThreadDetector.ISSUE) + .run() + .expectClean() + } + + @Test fun testWorkerMethod() { lint() .files( diff --git a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/BroadcastSentViaContextDetectorTest.kt b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/BroadcastSentViaContextDetectorTest.kt index 376acb56fac9..30b68f7e7a75 100644 --- a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/BroadcastSentViaContextDetectorTest.kt +++ b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/BroadcastSentViaContextDetectorTest.kt @@ -129,6 +129,34 @@ class BroadcastSentViaContextDetectorTest : SystemUILintDetectorTest() { } @Test + fun testSuppressSendBroadcastInActivity() { + lint() + .files( + TestFiles.java( + """ + package test.pkg; + import android.app.Activity; + import android.os.UserHandle; + + public class TestClass { + @SuppressWarnings("BroadcastSentViaContext") + public void send(Activity activity) { + Intent intent = new Intent(Intent.ACTION_VIEW); + activity.sendBroadcastAsUser(intent, UserHandle.ALL, "permission"); + } + + } + """ + ) + .indented(), + *stubs + ) + .issues(BroadcastSentViaContextDetector.ISSUE) + .run() + .expectClean() + } + + @Test fun testSendBroadcastInBroadcastSender() { lint() .files( diff --git a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/NonInjectedMainThreadDetectorTest.kt b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/NonInjectedMainThreadDetectorTest.kt index 301c338f9b42..ed3d14a1f33f 100644 --- a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/NonInjectedMainThreadDetectorTest.kt +++ b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/NonInjectedMainThreadDetectorTest.kt @@ -61,6 +61,32 @@ class NonInjectedMainThreadDetectorTest : SystemUILintDetectorTest() { } @Test + fun testSuppressGetMainThreadHandler() { + lint() + .files( + TestFiles.java( + """ + package test.pkg; + import android.content.Context; + import android.os.Handler; + + @SuppressWarnings("NonInjectedMainThread") + public class TestClass { + public void test(Context context) { + Handler mainThreadHandler = context.getMainThreadHandler(); + } + } + """ + ) + .indented(), + *stubs + ) + .issues(NonInjectedMainThreadDetector.ISSUE) + .run() + .expectClean() + } + + @Test fun testGetMainLooper() { lint() .files( diff --git a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/NonInjectedServiceDetectorTest.kt b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/NonInjectedServiceDetectorTest.kt index 0a74bfcfee57..846129aa12c1 100644 --- a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/NonInjectedServiceDetectorTest.kt +++ b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/NonInjectedServiceDetectorTest.kt @@ -91,6 +91,32 @@ class NonInjectedServiceDetectorTest : SystemUILintDetectorTest() { } @Test + fun testSuppressGetServiceWithClass() { + lint() + .files( + TestFiles.java( + """ + package test.pkg; + import android.content.Context; + import android.os.UserManager; + + public class TestClass { + @SuppressLint("NonInjectedService") + public void getSystemServiceWithoutDagger(Context context) { + context.getSystemService(UserManager.class); + } + } + """ + ) + .indented(), + *stubs + ) + .issues(NonInjectedServiceDetector.ISSUE) + .run() + .expectClean() + } + + @Test fun testGetAccountManager() { lint() .files( diff --git a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/RegisterReceiverViaContextDetectorTest.kt b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/RegisterReceiverViaContextDetectorTest.kt index 9ed7aa029b1d..0ac8f8e7c672 100644 --- a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/RegisterReceiverViaContextDetectorTest.kt +++ b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/RegisterReceiverViaContextDetectorTest.kt @@ -63,6 +63,34 @@ class RegisterReceiverViaContextDetectorTest : SystemUILintDetectorTest() { } @Test + fun testSuppressRegisterReceiver() { + lint() + .files( + TestFiles.java( + """ + package test.pkg; + import android.content.BroadcastReceiver; + import android.content.Context; + import android.content.IntentFilter; + + @SuppressWarnings("RegisterReceiverViaContext") + public class TestClass { + public void bind(Context context, BroadcastReceiver receiver, + IntentFilter filter) { + context.registerReceiver(receiver, filter, 0); + } + } + """ + ) + .indented(), + *stubs + ) + .issues(RegisterReceiverViaContextDetector.ISSUE) + .run() + .expectClean() + } + + @Test fun testRegisterReceiverAsUser() { lint() .files( diff --git a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/SlowUserQueryDetectorTest.kt b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/SlowUserQueryDetectorTest.kt index 54cac7b35598..34a424918a79 100644 --- a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/SlowUserQueryDetectorTest.kt +++ b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/SlowUserQueryDetectorTest.kt @@ -76,7 +76,7 @@ class SlowUserQueryDetectorTest : SystemUILintDetectorTest() { import android.os.UserManager; public class TestClass { - public void slewlyGetUserInfo(UserManager userManager) { + public void slowlyGetUserInfo(UserManager userManager) { userManager.getUserInfo(); } } @@ -101,6 +101,34 @@ class SlowUserQueryDetectorTest : SystemUILintDetectorTest() { } @Test + fun testSuppressGetUserInfo() { + lint() + .files( + TestFiles.java( + """ + package test.pkg; + import android.os.UserManager; + + public class TestClass { + @SuppressWarnings("SlowUserInfoQuery") + public void slowlyGetUserInfo(UserManager userManager) { + userManager.getUserInfo(); + } + } + """ + ) + .indented(), + *stubs + ) + .issues( + SlowUserQueryDetector.ISSUE_SLOW_USER_ID_QUERY, + SlowUserQueryDetector.ISSUE_SLOW_USER_INFO_QUERY + ) + .run() + .expectClean() + } + + @Test fun testUserTrackerGetUserId() { lint() .files( diff --git a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/SoftwareBitmapDetectorTest.kt b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/SoftwareBitmapDetectorTest.kt index c632636eb9c8..34becc6a5b04 100644 --- a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/SoftwareBitmapDetectorTest.kt +++ b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/SoftwareBitmapDetectorTest.kt @@ -63,6 +63,31 @@ class SoftwareBitmapDetectorTest : SystemUILintDetectorTest() { } @Test + fun testSuppressSoftwareBitmap() { + lint() + .files( + TestFiles.java( + """ + import android.graphics.Bitmap; + + @SuppressWarnings("SoftwareBitmap") + public class TestClass { + public void test() { + Bitmap.createBitmap(300, 300, Bitmap.Config.RGB_565); + Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888); + } + } + """ + ) + .indented(), + *stubs + ) + .issues(SoftwareBitmapDetector.ISSUE) + .run() + .expectClean() + } + + @Test fun testHardwareBitmap() { lint() .files( diff --git a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/StaticSettingsProviderDetectorTest.kt b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/StaticSettingsProviderDetectorTest.kt index b83ed7067bc3..efe4c90ec44f 100644 --- a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/StaticSettingsProviderDetectorTest.kt +++ b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/StaticSettingsProviderDetectorTest.kt @@ -28,7 +28,7 @@ class StaticSettingsProviderDetectorTest : SystemUILintDetectorTest() { override fun getIssues(): List<Issue> = listOf(StaticSettingsProviderDetector.ISSUE) @Test - fun testGetServiceWithString() { + fun testSuppressGetServiceWithString() { lint() .files( TestFiles.java( @@ -204,5 +204,34 @@ class StaticSettingsProviderDetectorTest : SystemUILintDetectorTest() { ) } + @Test + fun testGetServiceWithString() { + lint() + .files( + TestFiles.java( + """ + package test.pkg; + + import android.provider.Settings; + import android.provider.Settings.Global; + import android.provider.Settings.Secure; + + public class TestClass { + @SuppressWarnings("StaticSettingsProvider") + public void getSystemServiceWithoutDagger(Context context) { + final ContentResolver cr = mContext.getContentResolver(); + Global.getFloat(cr, Settings.Global.UNLOCK_SOUND); + } + } + """ + ) + .indented(), + *stubs + ) + .issues(StaticSettingsProviderDetector.ISSUE) + .run() + .expectClean() + } + private val stubs = androidStubs } |