package tv.luma.launcher; import android.content.*; import android.content.pm.*; import android.graphics.*; import android.graphics.drawable.Drawable; import android.util.LruCache; import java.util.*; final class AppRepository { static final class App { final String pkg, label; final ComponentName component; final ResolveInfo info; App(ResolveInfo r, PackageManager pm) { info = r; pkg = r.activityInfo.packageName; component = new ComponentName(pkg, r.activityInfo.name); label = r.loadLabel(pm).toString(); } } private final Context context; final LruCache icons = new LruCache(3 * 1024 * 1024) { protected int sizeOf(String k, Bitmap b) { return b.getAllocationByteCount(); } }; AppRepository(Context c) { context = c.getApplicationContext(); } List discover() { PackageManager pm = context.getPackageManager(); Map map = new HashMap<>(); for (String cat : new String[] {Intent.CATEGORY_LEANBACK_LAUNCHER, Intent.CATEGORY_LAUNCHER}) { Intent i = new Intent(Intent.ACTION_MAIN).addCategory(cat); for (ResolveInfo r : pm.queryIntentActivities(i, 0)) { if (!r.activityInfo.enabled || r.activityInfo.packageName.equals(context.getPackageName())) continue; if (!map.containsKey(r.activityInfo.packageName)) map.put(r.activityInfo.packageName, new App(r, pm)); } } List apps = new ArrayList<>(map.values()); apps.sort((a, b) -> a.label.compareToIgnoreCase(b.label)); return apps; } Bitmap icon(App app) { Bitmap old = icons.get(app.pkg); if (old != null) return old; try { PackageManager pm = context.getPackageManager(); Drawable d = app.info.activityInfo.loadBanner(pm); if (d == null) d = pm.getApplicationBanner(app.pkg); boolean banner = d != null; if (d == null) d = app.info.loadIcon(pm); Bitmap b = Bitmap.createBitmap(288, 162, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); c.drawColor(color(app.pkg)); if (banner) { d.setBounds(0, 0, 288, 162); } else { d.setBounds(97, 34, 191, 128); } d.draw(c); icons.put(app.pkg, b); return b; } catch (Exception e) { return null; } } static int color(String pkg) { String p = pkg.toLowerCase(Locale.ROOT); if (p.contains("plex") || p.contains("plezy")) return 0xFF5B4125; if (p.contains("netflix")) return 0xFF681B2C; if (p.contains("spotify")) return 0xFF164F45; if (p.contains("disney")) return 0xFF193C6A; if (p.contains("youtube") || p.contains("smarttube")) return 0xFF62352D; if (p.contains("twitch")) return 0xFF4A3267; if (p.contains("moon") || p.contains("limelight")) return 0xFF244E68; return Color.HSVToColor(new float[] {Math.abs(pkg.hashCode() % 360), .42f, .31f}); } }