if device storage is really low, delete the entire cache
This commit is contained in:
parent
af32e4ac85
commit
c1656f61a7
@ -267,6 +267,9 @@
|
|||||||
<service
|
<service
|
||||||
android:name=".CleanCacheService"
|
android:name=".CleanCacheService"
|
||||||
android:exported="false"/>
|
android:exported="false"/>
|
||||||
|
<service
|
||||||
|
android:name=".DeleteCacheService"
|
||||||
|
android:exported="false"/>
|
||||||
<service android:name=".net.WifiStateChangeService"/>
|
<service android:name=".net.WifiStateChangeService"/>
|
||||||
<service android:name=".localrepo.SwapService"/>
|
<service android:name=".localrepo.SwapService"/>
|
||||||
<service
|
<service
|
||||||
|
44
app/src/main/java/org/fdroid/fdroid/DeleteCacheService.java
Normal file
44
app/src/main/java/org/fdroid/fdroid/DeleteCacheService.java
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,8 @@ import android.content.BroadcastReceiver;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import org.fdroid.fdroid.CleanCacheService;
|
import org.fdroid.fdroid.CleanCacheService;
|
||||||
|
import org.fdroid.fdroid.DeleteCacheService;
|
||||||
|
import org.fdroid.fdroid.Utils;
|
||||||
|
|
||||||
public class DeviceStorageReceiver extends BroadcastReceiver {
|
public class DeviceStorageReceiver extends BroadcastReceiver {
|
||||||
@Override
|
@Override
|
||||||
@ -13,7 +15,13 @@ public class DeviceStorageReceiver extends BroadcastReceiver {
|
|||||||
}
|
}
|
||||||
String action = intent.getAction();
|
String action = intent.getAction();
|
||||||
if (Intent.ACTION_DEVICE_STORAGE_LOW.equals(action)) {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user