(#) Wrong system service casts !!! ERROR: Wrong system service casts This is an error. Id : `ServiceCast` Summary : Wrong system service casts Severity : Error Category : Correctness Platform : Android Vendor : Android Open Source Project Feedback : https://issuetracker.google.com/issues/new?component=192708 Since : Initial Affects : Kotlin and Java files Editing : This check runs on the fly in the IDE editor Implementation : [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/ServiceCastDetector.kt) Tests : [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/ServiceCastDetectorTest.kt) Copyright Year : 2013 When you call `Context#getSystemService()`, the result is typically cast to a specific interface. This lint check ensures that the cast is compatible with the expected type of the return value. (##) Example Here is an example of lint warnings produced by this check: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text src/test/pkg/SystemServiceTest.java:13:Error: Suspicious cast to DisplayManager for a DEVICE_POLICY_SERVICE: expected DevicePolicyManager [ServiceCast] DisplayManager displayServiceWrong = (DisplayManager) getSystemService( ^ src/test/pkg/SystemServiceTest.java:16:Error: Suspicious cast to WallpaperService for a WALLPAPER_SERVICE: expected WallpaperManager [ServiceCast] WallpaperService wallPaperWrong = (WallpaperService) getSystemService(WALLPAPER_SERVICE); ------------------------------------------------------ src/test/pkg/SystemServiceTest.java:22:Error: Suspicious cast to DisplayManager for a DEVICE_POLICY_SERVICE: expected DevicePolicyManager [ServiceCast] DisplayManager displayServiceWrong = (DisplayManager) context ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Here is the source file referenced above: `src/test/pkg/SystemServiceTest.java`: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers package test.pkg; import android.content.ClipboardManager; import android.app.Activity; import android.app.WallpaperManager; import android.content.Context; import android.hardware.display.DisplayManager; import android.service.wallpaper.WallpaperService; public class SystemServiceTest extends Activity { public void test1() { DisplayManager displayServiceOk = (DisplayManager) getSystemService(DISPLAY_SERVICE); DisplayManager displayServiceWrong = (DisplayManager) getSystemService( DEVICE_POLICY_SERVICE); WallpaperManager wallPaperOk = (WallpaperManager) getSystemService(WALLPAPER_SERVICE); WallpaperService wallPaperWrong = (WallpaperService) getSystemService(WALLPAPER_SERVICE); } public void test2(Context context) { DisplayManager displayServiceOk = (DisplayManager) context .getSystemService(DISPLAY_SERVICE); DisplayManager displayServiceWrong = (DisplayManager) context .getSystemService(DEVICE_POLICY_SERVICE); } public void clipboard(Context context) { ClipboardManager clipboard = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE); android.content.ClipboardManager clipboard1 = (android.content.ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE); android.text.ClipboardManager clipboard2 = (android.text.ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE); } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can also visit the [source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/ServiceCastDetectorTest.kt) for the unit tests for this check to see additional scenarios. The above example was automatically extracted from the first unit test found for this lint check, `ServiceCastDetector.testServiceCast`. To report a problem with this extracted sample, visit https://issuetracker.google.com/issues/new?component=192708. (##) Suppressing You can suppress false positives using one of the following mechanisms: * Using a suppression annotation like this on the enclosing element: ```kt // Kotlin @Suppress("ServiceCast") fun method() { getSystemService(...) } ``` or ```java // Java @SuppressWarnings("ServiceCast") void method() { getSystemService(...); } ``` * Using a suppression comment like this on the line above: ```kt //noinspection ServiceCast problematicStatement() ``` * Using a special `lint.xml` file in the source tree which turns off the check in that folder and any sub folder. A simple file might look like this: ```xml <?xml version="1.0" encoding="UTF-8"?> <lint> <issue id="ServiceCast" severity="ignore" /> </lint> ``` Instead of `ignore` you can also change the severity here, for example from `error` to `warning`. You can find additional documentation on how to filter issues by path, regular expression and so on [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html). * In Gradle projects, using the DSL syntax to configure lint. For example, you can use something like ```gradle lintOptions { disable 'ServiceCast' } ``` In Android projects this should be nested inside an `android { }` block. * For manual invocations of `lint`, using the `--ignore` flag: ``` $ lint --ignore ServiceCast ...` ``` * Last, but not least, using baselines, as discussed [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).