Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is resolveInfo.loadLabel () so ridiculously slow?

In my home screen replacement app, I have to get a list of all installed apps to put them inside the app drawer. Hence, the following method gets run on every app;

public static App fromResolveInfo (Context context, PackageManager pacMan, AppManager appManager, ResolveInfo resInf)
{
    String label = resInf.loadLabel (pacMan).toString ();
    String packageName = resInf.activityInfo.applicationInfo.packageName;
    String activityName = resInf.activityInfo.name;

    App app = new App (context, appManager);
    app.setLabel (label);
    app.setPackageName (packageName);
    app.setActivityName (activityName);

    AppIcon icon = null;
    if (appManager.isIconPackLoaded ())
        icon = appManager.getIconPack ().getIconForApp (app);
    if (icon == null)
        icon = appManager.getIconPack ().getFallbackIcon (resInf.loadIcon (pacMan));

    app.setIcon (icon);

    return app;
}

The problem is that there is a bottleneck here, and it's not he loading of the icons as I had originally anticipated. The first line of the method (String label = resInf.loadLabel (pacMan).toString ();) can take up anywhere between 0 and 250 milliseconds (on a relatively high-end device). On older devices, this becomes a real issue.
In my tests, I have noticed that when a slower device is multitasking and for some reason the app drawer has to be reloaded, it can take up to 30 seconds for this action to be completed (on all of the installed apps).

Caching could offer a potential solution for this, but then what if the name of an app changes (which occasionally happens)? I'd have to take the labels from the cache, and then loop over all of the apps in a separate thread and correct the labels where they have changed. This may offer a solution, but it seems more like a dirty hack than an actual good solution.

Is there any faster way to get the label of an app's activity? Also, why does it take so ridiculously long for Android to get an app's label, and/or is there anything that I can do about it?

like image 353
RobinJ Avatar asked Jul 02 '15 15:07

RobinJ


1 Answers

You can get label as:

String label = (String) resInf.activityInfo.applicationInfo.loadLabel(pacMan);

If you compare Android source code for those two methods you will notice the one from applicationInfo has less code to execute. Maybe the bottleneck sits in the extra code. I personally never compared execution times for those as I never observed such issue.

like image 123
mhenryk Avatar answered Oct 30 '22 13:10

mhenryk