Stop AndroidFrameworkContextUserId on UserManager

For older APIs, UserManager uses the method getContextUserIfAppropriate() to get the context user (if a newer API level) or the calling user (if an older API level). We had to gate on API level so that we could convert old API usage to the new context-user-based style.

In this cl, we recognize this method as a legit context-user-style method, so that we can stop getting AndroidFrameworkContextUserId lint warnings on UserManager.

Change-Id: I95ee95645314ab9105a48a800de06d9b8c7ecb85
Fixes: 268129776
Test: atest error_prone_android_framework_test
This commit is contained in:
Adam Bookatz
2023-02-07 04:16:33 +00:00
parent fd4c8b0282
commit c80cb2eb9d
2 changed files with 29 additions and 0 deletions

View File

@@ -68,6 +68,8 @@ public final class ContextUserIdChecker extends BugChecker implements MethodInvo
.named("myUserId"),
instanceMethod().onExactClass("android.content.pm.ShortcutManager")
.named("injectMyUserId"),
instanceMethod().onExactClass("android.os.UserManager")
.named("getContextUserIfAppropriate"),
instanceMethod().onDescendantOf("android.content.Context")
.named("getUserId")));

View File

@@ -148,4 +148,31 @@ public class ContextUserIdCheckerTest {
"}")
.doTest();
}
@Test
public void testUserManager() {
compilationHelper
.addSourceFile("/android/annotation/SystemService.java")
.addSourceFile("/android/content/Context.java")
.addSourceFile("/android/content/Intent.java")
.addSourceFile("/android/foo/IFooService.java")
.addSourceFile("/android/os/IInterface.java")
.addSourceFile("/android/os/UserHandle.java")
.addSourceFile("/android/os/RemoteException.java")
.addSourceLines("UserManager.java",
"package android.os;",
"import android.annotation.SystemService;",
"import android.content.Context;",
"import android.foo.IFooService;",
"import android.os.UserHandle;",
"import android.os.RemoteException;",
"@SystemService(\"user\") public class UserManager {",
" IFooService mService;",
" int getContextUserIfAppropriate() { return 0; }",
" void bar() throws RemoteException {",
" mService.baz(null, getContextUserIfAppropriate());",
" }",
"}")
.doTest();
}
}