-1

Im trying to develop an application locker.but for that i need to show the user a list off all the apps installed on the device. the first time i managed to show the user all the apps installed but not the system apps like the camera,dialer,messaging etc. So when modifying the code i managed to show everything as in all the apps but i released that this shows way too much info as it shows all the com.android packages,launcher packages etc. is there any way to filter out and show system apps like contacts,dialer etc and other apps while ignoring the system apps?and also how would i sort this list in alphabetical order? thanks in advance :)

public class ApkList extends Fragment implements OnItemClickListener {
PackageManager packageManager;
ListView apkList;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View rootView = inflater.inflate(R.layout.app_list, container, false);
    return rootView;
}

@Override
public void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    packageManager = getActivity().getPackageManager();
    List<PackageInfo> packageList = packageManager
            .getInstalledPackages(PackageManager.GET_PERMISSIONS);

    List<PackageInfo> packageList1 = new ArrayList<PackageInfo>();

    /* To filter out System apps */
    for (PackageInfo pi : packageList) {
        //boolean b = isSystemPackage(pi);
        //if (!b) {
            packageList1.add(pi);
        //}
    }
    apkList = (ListView) getView().findViewById(R.id.applist);
    apkList.setAdapter(new ApkAdapter(getActivity(), packageList1,
            packageManager));

    apkList.setOnItemClickListener(this);
}

private boolean isSystemPackage(PackageInfo pkgInfo) {
    return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true
            : false;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long row) {
    PackageInfo packageInfo = (PackageInfo) parent
            .getItemAtPosition(position);
    AppData appData = (AppData) getActivity().getApplicationContext();
    appData.setPackageInfo(packageInfo);

    Intent appInfo = new Intent(getActivity().getApplicationContext(),
            ApkInfo.class);
    startActivity(appInfo);
}

}
Clinton Dsouza
  • 330
  • 4
  • 20

1 Answers1

0

You can classify system and none-system apps comparing its flags:

isSystemPackage(PackageInfo pi) {
    ApplicationInfo ai = packageManager.getApplicationInfo(pi.packageName, 0);
    return ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
}

Reference: ApplicationInfo Flags, FLAG_SYSTEM

sam
  • 2,780
  • 1
  • 17
  • 30