1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| public class AppInfoManager { public static String TAG = "APPInfoManager";
public static void saveInstalledAppsInfo(Context context) throws ClassNotFoundException, NoSuchMethodException { PackageManager packageManager = context.getPackageManager(); List<ApplicationInfo> apps = packageManager.getInstalledApplications(0);
DatabaseHelper dbHelper = new DatabaseHelper(context); SQLiteDatabase db = dbHelper.getWritableDatabase(); for (ApplicationInfo app : apps) { String packageName = app.packageName; Log.d(TAG, app.packageName); String name = (String) app.loadLabel(packageManager); if (dbHelper.appNameExists(name)) { Log.d(TAG, String.format("%s exists", name)); continue; } int flags = app.flags; boolean isSystemApp = (flags & ApplicationInfo.FLAG_SYSTEM) != 0; int compileSdkVersion = app.compileSdkVersion; int targetSdkVersion = app.targetSdkVersion; String processName = app.processName;
String launcherActivity = ""; Intent intent = packageManager.getLaunchIntentForPackage(packageName); if (intent != null) { ComponentName componentName = intent.getComponent(); launcherActivity = componentName.getClassName(); } Drawable icon = app.loadIcon(packageManager); Bitmap bitmap = getBitmapFromDrawable(icon); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] iconBytes = baos.toByteArray();
ContentValues values = new ContentValues(); values.put("packageName", packageName); values.put("appName", name); values.put("isSystemApp", isSystemApp); values.put("launcherActivity", launcherActivity); values.put("processName", processName); values.put("compileSdkVersion", compileSdkVersion); values.put("targetSdkVersion", targetSdkVersion); values.put("icon", iconBytes); db.insert("apps", null, values); Log.d(TAG, String.format("%s saved", name)); }
db.close(); writeFinishedFlag(context); }
private static Bitmap getBitmapFromDrawable(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof AdaptiveIconDrawable) { Drawable backgroundDr = ((AdaptiveIconDrawable) drawable).getBackground(); Drawable foregroundDr = ((AdaptiveIconDrawable) drawable).getForeground(); if (backgroundDr != null && foregroundDr != null) { Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); backgroundDr.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); foregroundDr.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); backgroundDr.draw(canvas); foregroundDr.draw(canvas); return bitmap; } else{ return Bitmap.createBitmap(64, 64, Bitmap.Config.ARGB_8888); } } else { Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } }
private static void writeFinishedFlag(Context context) { File file = new File(Objects.requireNonNull(context.getExternalFilesDir(null)).getAbsolutePath(), "result.txt"); try (FileWriter writer = new FileWriter(file)) { writer.write("1"); } catch (IOException e) { Log.d(TAG, "writeFinishedFlag: ", e); } } }
|