package com.tasomaniac.openwith.resolver;

import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import timber.log.Timber;

public class IconLoader {

    private final PackageManager packageManager;
    private final int iconDpi;

    public IconLoader(PackageManager packageManager, int iconDpi) {
        this.packageManager = packageManager;
        this.iconDpi = iconDpi;
    }

    public Drawable loadFor(ActivityInfo activity) {
        try {
            String packageName = activity.packageName;
            if (activity.icon != 0) {
                return getIcon(packageName, activity.icon);
            }
            final int iconRes = activity.getIconResource();
            if (iconRes != 0) {
                return getIcon(packageName, iconRes);
            }
        } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
            Timber.e(e, "Couldn't find resources for package");
        }
        return activity.loadIcon(packageManager);
    }

    private Drawable getIcon(String packageName, int resId) throws PackageManager.NameNotFoundException {
        Resources res = packageManager.getResourcesForApplication(packageName);
        return res.getDrawableForDensity(resId, iconDpi);
    }
}

@@ -199,16 +199,52 @@
 awable;%0A
+%0Aimport javax.annotation.Nullable;%0A%0A
 import t
@@ -519,24 +519,38 @@
 Dpi;%0A    %7D%0A%0A
+    @Nullable%0A
     public D
@@ -1147,24 +1147,38 @@
 er);%0A    %7D%0A%0A
+    @Nullable%0A
     private 
@@ -1406,16 +1406,22 @@
  iconDpi
+, null
 );%0A    %7D

Fix warnings