Skip to content

Clear cache #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ apply plugin: 'bintray-release'

android {
compileSdkVersion 23
buildToolsVersion '25.0.2'
buildToolsVersion '25.0.3'

defaultConfig {
minSdkVersion 9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,26 @@ public void shutdown() {
}
}

public void cleanCache() {
StorageUtils.cleanDirectory(config.cacheRoot);
config.sourceInfoStorage.clean();
}

public void removeFromCache(String url) {
if(!isCached(url)) return;

File cacheFile = getCacheFile(url);
if(!cacheFile.exists()) return;

boolean isDeleted = cacheFile.delete();
if (!isDeleted) {
LOG.warn(String.format("File %s can't be deleted", cacheFile.getAbsolutePath()));
return;
}

config.sourceInfoStorage.remove(url);
}

private boolean isAlive() {
return pinger.ping(3, 70); // 70+140+280=max~500ms
}
Expand Down
36 changes: 36 additions & 0 deletions library/src/main/java/com/danikula/videocache/StorageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;

import static android.os.Environment.MEDIA_MOUNTED;

Expand Down Expand Up @@ -80,4 +81,39 @@ private static File getExternalCacheDir(Context context) {
}
return appCacheDir;
}

public static void cleanDirectory(File file) {
if (!file.exists()) {
return;
}
File[] contentFiles = file.listFiles();
if (contentFiles != null) {
for (File contentFile : contentFiles) {
try {
delete(contentFile);
} catch (IOException e) {
LOG.warn(e.getMessage());
}
}
}
}

private static void delete(File file) throws IOException {
if (file.isFile() && file.exists()) {
deleteOrThrow(file);
} else {
cleanDirectory(file);
deleteOrThrow(file);
}
}

private static void deleteOrThrow(File file) throws IOException {
if (file.exists()) {
boolean isDeleted = file.delete();
if (!isDeleted) {
throw new IOException(String.format("File %s can't be deleted", file.getAbsolutePath()));
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ public void release() {
close();
}

@Override
public void clean() {
getWritableDatabase().delete(TABLE, null, null);
}

@Override
public void remove(String url) {
checkNotNull(url);
getWritableDatabase().delete(TABLE, COLUMN_URL + "=?", new String[]{url});
}

private SourceInfo convert(Cursor cursor) {
return new SourceInfo(
cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_URL)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ public void put(String url, SourceInfo sourceInfo) {
@Override
public void release() {
}

@Override
public void clean() {
}

@Override
public void remove(String url) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ public interface SourceInfoStorage {
void put(String url, SourceInfo sourceInfo);

void release();

void clean();

void remove(String url);
}
6 changes: 3 additions & 3 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ apply plugin: 'com.neenbedankt.android-apt'

android {
compileSdkVersion 23
buildToolsVersion '25.0.2'
buildToolsVersion '25.0.3'

defaultConfig {
applicationId 'com.danikula.videocache.sample'
Expand All @@ -35,10 +35,10 @@ apt {
}

dependencies {
// compile project(':library')
compile project(':library')
compile 'com.android.support:support-v4:23.1.0'
compile 'org.androidannotations:androidannotations-api:3.3.2'
compile 'com.danikula:videocache:2.7.0'
// compile 'com.danikula:videocache:2.7.0'
compile 'com.viewpagerindicator:library:2.4.2-SNAPSHOT@aar'
apt 'org.androidannotations:androidannotations:3.3.2'
}
4 changes: 3 additions & 1 deletion sample/src/main/java/com/danikula/videocache/sample/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import com.danikula.videocache.HttpProxyCacheServer;

import java.io.File;

/**
* @author Alexey Danilov (danikula@gmail.com).
*/
Expand All @@ -19,7 +21,7 @@ public static HttpProxyCacheServer getProxy(Context context) {

private HttpProxyCacheServer newProxy() {
return new HttpProxyCacheServer.Builder(this)
.cacheDirectory(Utils.getVideoCacheDir(this))
.cacheDirectory(new File(getExternalCacheDir(), "video-cache"))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -48,13 +45,7 @@ void onListItemClicked(int position) {

@Click(R.id.cleanCacheButton)
void onClearCacheButtonClick() {
try {

Utils.cleanVideoCacheDir(this);
} catch (IOException e) {
Log.e(null, "Error cleaning cache", e);
Toast.makeText(this, "Error cleaning cache", Toast.LENGTH_LONG).show();
}
App.getProxy(this).cleanCache();
}

private static final class ListEntry {
Expand Down
53 changes: 0 additions & 53 deletions sample/src/main/java/com/danikula/videocache/sample/Utils.java

This file was deleted.

2 changes: 1 addition & 1 deletion test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion '25.0.2'
buildToolsVersion '25.0.3'

defaultConfig {
applicationId 'com.danikula.proxycache.test'
Expand Down