I have a list that is populated by the package manager and it dieplays a list of all system apps. I need to find a way to "filter" that list so only specific apps show. I have a list of apps that I will "allow" the user to manimpulate. Is that possible to show ONLY those apps out of all system apps? I mean filter for their name and if found on device' then show on the list? Thank you so much for any advice! All is appreciated! This is the relevant code that populates the list (the parent is fragment and the list is being created in async task).
private class GearAppsToDebloat extends AsyncTask<Void, Void, Void> {
private ProgressDialog progress = null;
//Using package manager to get all installed apps on the device
final List<PackageInfo> packageList = packageManager
.getInstalledPackages(PackageManager.GET_META_DATA);
final List<PackageInfo> packageList1 = packageManager
.getInstalledPackages(0);
@Override
protected Void doInBackground(Void... params) {
try {
packageList1.clear();
for (int n = 0; n < packageList.size(); n++) {
PackageInfo PackInfo = packageList.get(n);
// List only system apps
if (((PackInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM
) != 0) == true)
{
try {
packageList1.add(packageList.get(n));
// Sort App list on name basis
Collections.sort(packageList1, new Comparator<PackageInfo>()
{
public int compare(PackageInfo o1, PackageInfo o2) {
// Return sorted list of packages
return o1.applicationInfo.loadLabel(getActivity().getPackageManager()).toString()
.compareToIgnoreCase(o2.applicationInfo.loadLabel(getActivity().getPackageManager())
.toString());
}
});
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Sorry, I just did this in C# in Xamarin but I'm sure you can write the equivalent.
I got the needed applications from using a lambda expression on the GetInstalledApplications method. It selects the application if the ClassName is present in the list of apps I am looking for.
This also provided me with a very quick return, using other methods I've come across took about 8 seconds to do the same task.
You should now have the list of apps that you know the user has installed on his/her device.