if device storage is really low, delete the entire cache

This commit is contained in:
Hans-Christoph Steiner 2018-04-05 23:02:23 +02:00
parent af32e4ac85
commit c1656f61a7
3 changed files with 56 additions and 1 deletions

View File

@ -267,6 +267,9 @@
<service
android:name=".CleanCacheService"
android:exported="false"/>
<service
android:name=".DeleteCacheService"
android:exported="false"/>
<service android:name=".net.WifiStateChangeService"/>
<service android:name=".localrepo.SwapService"/>
<service

View File

@ -0,0 +1,44 @@
package org.fdroid.fdroid;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Process;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import org.apache.commons.io.FileUtils;
import java.io.File;
/**
* An {@link IntentService} subclass for deleting the full cache for this app.
*/
public class DeleteCacheService extends IntentService {
public static final String TAG = "DeleteCacheService";
public DeleteCacheService() {
super("DeleteCacheService");
}
public static void deleteAll(Context context) {
Intent intent = new Intent(context, DeleteCacheService.class);
context.startService(intent);
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent == null) {
return;
}
Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
Log.w(TAG, "Deleting all cached contents!");
try {
FileUtils.deleteDirectory(getCacheDir());
for (File dir : ContextCompat.getExternalCacheDirs(this)) {
FileUtils.deleteDirectory(dir);
}
} catch (Exception e) {
// ignored
}
}
}

View File

@ -4,6 +4,8 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import org.fdroid.fdroid.CleanCacheService;
import org.fdroid.fdroid.DeleteCacheService;
import org.fdroid.fdroid.Utils;
public class DeviceStorageReceiver extends BroadcastReceiver {
@Override
@ -13,7 +15,13 @@ public class DeviceStorageReceiver extends BroadcastReceiver {
}
String action = intent.getAction();
if (Intent.ACTION_DEVICE_STORAGE_LOW.equals(action)) {
CleanCacheService.schedule(context);
int percentageFree = Utils.getPercent(Utils.getImageCacheDirAvailableMemory(context),
Utils.getImageCacheDirTotalMemory(context));
if (percentageFree > 2) {
CleanCacheService.start(context);
} else {
DeleteCacheService.deleteAll(context);
}
}
}
}