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!
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.