Skip to content

Commit

Permalink
feat: trim the icon cache after refreshing
Browse files Browse the repository at this point in the history
  • Loading branch information
SebiderSushi committed Oct 29, 2022
1 parent 43e3af7 commit ac59ace
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ protected Void doInBackground(Void... params) {
@Override
protected void onPostExecute(Void result) {
App.getPackageChangedListener().onPackageChange(mChangedPackageNames, appInfoList);
//TODO trim icon cache
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public void onReceive(Context context, Intent intent) {
if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
App.getPackageChangedListener().onPackageChange(Collections.singletonList(packageName), null);
}
//TODO trim icon cache
break;
}
}
Expand Down
33 changes: 33 additions & 0 deletions android/src/main/java/org/ligi/fast/model/AppIconCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;

import org.ligi.fast.App;
import org.ligi.tracedroid.logging.Log;
Expand All @@ -14,6 +15,8 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.ref.SoftReference;
import java.util.Iterator;
import java.util.List;

public class AppIconCache {
private static final Bitmap.CompressFormat COMPRESS_FORMAT = Bitmap.CompressFormat.PNG;
Expand Down Expand Up @@ -56,6 +59,36 @@ public static void invalidateIconCache() {
}
}

/**
* Delete all icon cache files that are not referenced in the current app info list.
* If no app info list is provided all currently cached icons will be deleted.
*
* @param currentAppInfo The complete and up-to-date app info list. Treated as empty if null.
*/
public static void trimIconCache(@Nullable List<AppInfo> currentAppInfo) {
if (App.getBaseDir().exists()) {
File[] iconFiles = App.getBaseDir().listFiles(pathname -> pathname.getName().endsWith(CACHE_FILE_ENDING));
if (iconFiles != null) {
next_file:
for (File iconFile : iconFiles) {
if (currentAppInfo != null) {
for (Iterator<AppInfo> iterator = currentAppInfo.iterator(); iterator.hasNext(); ) {
String referencedPath = getIconCacheFile(iterator.next()).getAbsolutePath();
String cachedPath = iconFile.getAbsolutePath();
if (cachedPath.equals(referencedPath)) {
iterator.remove();
continue next_file;
}
}
}
if (!iconFile.delete()) {
Log.w("Unable to delete " + iconFile.getAbsolutePath());
}
}
}
}
}

public void cacheIcon(ResolveInfo ri) {
final PackageManager packageManager = ctx.getPackageManager();

Expand Down
1 change: 1 addition & 0 deletions android/src/main/java/org/ligi/fast/model/AppInfoList.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@ public void onPackageChange(List<String> changedPackageNames, List<AppInfo> newA

this.clear();
this.addAll(updatedAppInfoList);
new Thread(() -> AppIconCache.trimIconCache(updatedAppInfoList)).start();
}
}
1 change: 0 additions & 1 deletion android/src/main/java/org/ligi/fast/ui/SearchActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ private String getHomePackageName() {

@Override
public void onPackageChange(List<String> changedPackages, List<AppInfo> list) {
// TODO we should also do a cleanup of cached icons here
runOnUiThread(() -> {
appInfoList.getBackingAppInfoList().onPackageChange(changedPackages, list);
configureAdapter();
Expand Down

0 comments on commit ac59ace

Please sign in to comment.