0

I am trying to clean /cache folder of my android tv.

In the beginning, when I do tv boots I call service which use this method to define space e valuable in this folder.

  public static long getCacheFreeSize() {
    StatFs stat = new StatFs("/cache");
    DebugUtils.logV(TAG, "all space of cache " + stat.getTotalBytes() / 1024 + "kb");
    DebugUtils.logV(TAG, "free cache " + stat.getBlockSizeLong() * stat.getAvailableBlocksLong() / 1024 + "kb");
    return stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
}

So when tv starts I see this logs:

V/CommonUtils: all space of cache 1015704kb
V/CommonUtils: free cache 999084kb

And after some time other apps that on tv creates new

file
/cache/update_s.zip

screen from android studio

then I have called new method, that should clean cache folder.

public static Completable deleteAppsCache(Context context, long spaceToClear) {
    return Completable.create(emitter -> {

        boolean executed = execCommand("chmod", "775", "/cache");

        StatFs stat = new StatFs("/cache");

        DebugUtils.logV(TAG, "all space of cache " + stat.getTotalBytes() / 1024 / 1024 + " mb "  + "chmod 775 executed? "+ executed);
        DebugUtils.logV(TAG, "trying to clear some space in cache ");

        if (CommonUtils.removeFile(CommonUtils.getTVCacheFile())) {
            DebugUtils.logV(TAG, " tv cache removed, free space: " + CommonUtils.getCacheFreeSize());
        }
        if (CommonUtils.removeFile(CommonUtils.getCacheFile())) {
            DebugUtils.logV(TAG, "cache signed removed, free space: " + CommonUtils.getCacheFreeSize());
        }

        long size = CommonUtils.getCacheFreeSize();

        DebugUtils.logV(TAG, "cache cleared now size is " + size / 1024 + " kb");

        if (CommonUtils.isFreeSpaceCache(spaceToClear)) {
            emitter.onComplete();
        }

        List<File> files = (List<File>) FileUtils.listFiles(new File("cache/"), TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);

        for (int i = 0; i < files.size(); i++) {
            CommonUtils.removeFile(files.get(i));
        }

        if (size > spaceToClear) {
            emitter.onComplete();
        } else {
            final PackageManager pm = context.getPackageManager();
            Method[] methods = pm.getClass().getDeclaredMethods();
            for (Method m : methods) {
                if (m.getName().equals("freeStorage")) {
                    Class[] params = m.getParameterTypes();
                    if (params.length == 2) {
                        // Found the method I want to use
                        try {
                            DebugUtils.logV(TAG, "trying free " + spaceToClear + " bytes from cache");

                            long desiredFreeStorage = spaceToClear;
                            m.invoke(pm, desiredFreeStorage, null);
                        } catch (Exception e) {
                            DebugUtils.logV(TAG, "exception during cleaning cache 1 " + e.getMessage());

                        }
                        break;
                    }
                }
            }
        }
        emitter.onComplete();
    });
}

now I can see from android studio and from adb when call is, that files was removed.

enter image description here

but, when I'm trying to define how many space I have I still get old information.

here is the logs:

V: all space of cache 991 mb chmod 775 executed? true
V: trying to clear some space in cache 
V: not removed: /cache/funtvupgrade/update.zip
V: removed: /cache/update_signed.zip
V: all space of cache 1015704kb
V: free cache 496400kb
V: cache signed removed, free space: 508313600
V: all space of cache 1015704kb
V: free cache 496400kb
V: cache cleared now size is 496400 kb
V: all space of cache 1015704kb
V: free cache 496400kb
V: removed: /cache/update_s.zip
V: cache cleaned
V: all space of cache 1015704kb
V: free cache 496400kb

So I'm trying to clean cache with adb.

I call

adb root 
adb connect -my ip
adb shell
su
mount -o rw,remount,rw /cache
cd /cache
rm -Rr -f * 

this code cleans all cache folder, I see from android studio all folder is empty, no files, no directories.

Then I can call my method and I see that I have no space:

all space of cache 1015704kb
V: free cache 496408kb

I have feeling like some data wasn't invalidated after cache cleaning. and android.os.StatFs gives me wrong available space.

So here is the question:

What I am doing wrong and what is the right way to clean cache programmatically?

links that I used: Clear Cache in Android Application programmatically

Abhinav Gupta
  • 2,225
  • 1
  • 14
  • 30
Anton Kogan
  • 153
  • 2
  • 10
  • found some linux methods, that may be useful https://android.stackexchange.com/questions/5232/how-can-i-view-the-android-internal-partition-table – Anton Kogan Mar 07 '18 at 14:06
  • I found that file is called update.zip or update_s.zip and belongs to com.google.android.gms as I can't manage this process, I will wait until app will do this update and clear cache. And after that allocate space with one of this methods: link: https://stackoverflow.com/questions/18031841/pre-allocating-drive-space-for-file-storage – Anton Kogan Mar 13 '18 at 10:12

0 Answers0