I am currently working on a project that is going to filter out some specific query from a specific app to android contact books. I am wondering how can I figure out which app is accessing the contacts provider? I've looked through the API references, but it looks like there's only methods on acquiring the content provider's client instead of acquiring the app that is asking for access to the data. If anyone could explain this a bit, it will be great help! Thanks in advance!
Asked
Active
Viewed 213 times
1

user7153368
- 15
- 3
-
A `ContentResolver` does not have a "client", at least in terms of how I would use the term. – CommonsWare Nov 13 '16 at 16:10
-
I was trying to say the app is calling content resolver. Is there any name for that? – user7153368 Nov 13 '16 at 16:16
-
I would call that the client of the `ContentProvider`. – CommonsWare Nov 13 '16 at 16:18
-
Isn't the client of ContentProvider the database a query is trying to access? – user7153368 Nov 13 '16 at 16:22
-
Not by how I would use the term "client". That would be akin to calling MySQL a Web browser, on the grounds that the client of the Web server is a database. – CommonsWare Nov 13 '16 at 16:25
1 Answers
1
From your ContentProvider
you can get the package name of the app calling you like this :
@NonNull
private static Collection<String> getCallingPackages(Context context) {
int callingUid = Binder.getCallingUid();
if (callingUid == 0) {
return Collections.emptyList();
}
String[] packages = context.getPackageManager().getPackagesForUid(callingUid);
return new ArrayList<>(Arrays.asList(packages));
}
You can then check that the package(s) are on a whitelist/blacklist that you maintain.

XGouchet
- 10,002
- 10
- 48
- 83