Last active
August 29, 2015 14:17
-
-
Save MinceMan/dd9a8b69c093d4b009c7 to your computer and use it in GitHub Desktop.
This will tell you if the give package currently has the URI permissions that you granted. It will fail it the app has been killed or uninstalled. This may not work for persistent permissions if the app is not running.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class App extends Application { | |
// Bunches of application code | |
/** | |
* This will tell you if the give package currently has the URI permissions that you granted. | |
* It will fail it the app has been killed or uninstalled. | |
* This may not work for persistent permissions if the app is not running. | |
* @return PackageManager.PERMISSION_DENIED if it does not have permission and | |
* PackageManager.PERMISSION_GRANTED if it does have permission. | |
*/ | |
public int checkUriPermissions(Uri uri, String packageName, int modeFlags) { | |
ActivityManager activityService = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); | |
List<RunningAppProcessInfo> processes = activityService.getRunningAppProcesses(); | |
int uid = myUid(); | |
if (processes == null) return PackageManager.PERMISSION_DENIED; | |
for(RunningAppProcessInfo process : processes) { | |
if (process.processName.equals(packageName) && process.uid == uid) { | |
// We found what we're looking for! | |
return checkUriPermission(uri, process.pid, process.uid, modeFlags); | |
} | |
} | |
// Didn't find the process, if it had permissions they are now revoked. | |
return PackageManager.PERMISSION_DENIED; | |
} | |
/** | |
* This will tell you the packages which currently have the URI permissions that you granted. | |
* Packages will fail if they have been killed or uninstalled. | |
* This may not work for persistent permissions if the app is not running. | |
* @return a list of packageNames that have the permission which are queried for. | |
* This will never return null; | |
*/ | |
@NonNull | |
public List<String> checkUriPermissions(Uri uri, int modeFlags) { | |
ActivityManager activityService = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); | |
List<RunningAppProcessInfo> processes = activityService.getRunningAppProcesses(); | |
List<String> packages = new ArrayList<>(); | |
int uid = myUid(); | |
if (processes == null) return packages; | |
for(RunningAppProcessInfo process : processes) { | |
if (process.uid == uid) { | |
int result = checkUriPermission(uri, process.pid, process.uid, modeFlags); | |
if (result == PackageManager.PERMISSION_GRANTED) { | |
packages.add(process.processName); | |
} | |
} | |
} | |
return packages; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment