Skip to content

Commit

Permalink
ability to check was content fully cached to file or not
Browse files Browse the repository at this point in the history
  • Loading branch information
danikula committed Jul 27, 2016
1 parent eb67640 commit cd0b411
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 8 deletions.
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ Because there is no sense to download video a lot of times while streaming!

Note `AndroidVideoCache` works only with **direct urls** to media file, it [**doesn't support**](https://github.com/danikula/AndroidVideoCache/issues/19) any streaming technology like DASH, SmoothStreaming, HLS.

## How to use?
## Get started
Just add dependency (`AndroidVideoCache` is available in jcenter):
```
repositories {
jcenter()
}
dependencies {
compile 'com.danikula:videocache:2.3.4'
compile 'com.danikula:videocache:2.4.0'
}
```

Expand Down Expand Up @@ -64,7 +64,8 @@ public class App extends Application {
or use [simple factory](http://pastebin.com/s2fafSYS).
More preferable way is use some dependency injector like [Dagger](http://square.github.io/dagger/).

## Configuration
## Recipes
### Disk cache limit
By default `HttpProxyCacheServer` uses 512Mb for caching files. You can change this value:

```java
Expand All @@ -83,9 +84,17 @@ private HttpProxyCacheServer newProxy() {
.maxCacheFilesCount(20)
.build();
}
```
```

### Listen caching progress
Use `HttpProxyCacheServer.registerCacheListener(CacheListener listener)` method to set listener with callback `onCacheAvailable(File cacheFile, String url, int percentsAvailable)` to be aware of caching progress. Do not forget to to unsubscribe listener with help of `HttpProxyCacheServer.unregisterCacheListener(CacheListener listener)` method to avoid memory leaks.

Use `HttpProxyCacheServer.isCached(String url)` method to check was url's content fully cached to file or not.

See `sample` app for more details.

See `sample` app for details.
### Sample
See `sample` app.

## Whats new
See Release Notes [here](https://github.com/danikula/AndroidVideoCache/releases)
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ publish {
userOrg = 'alexeydanilov'
groupId = 'com.danikula'
artifactId = 'videocache'
publishVersion = '2.3.4'
publishVersion = '2.4.0'
description = 'Cache support for android VideoView'
website = 'https://github.com/danikula/AndroidVideoCache'
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@ public void unregisterCacheListener(CacheListener cacheListener) {
}
}

/**
* Checks is cache contains fully cached file for particular url.
*
* @param url an url cache file will be checked for.
* @return {@code true} if cache contains fully cached file for passed in parameters url.
*/
public boolean isCached(String url) {
checkNotNull(url, "Url can't be null!");
File cacheDir = config.cacheRoot;
String fileName = config.fileNameGenerator.generate(url);
File cacheFile = new File(cacheDir, fileName);
return cacheFile.exists();
}

public void shutdown() {
Log.i(LOG_TAG, "Shutdown proxy server");

Expand Down
4 changes: 2 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ android {
buildToolsVersion '24'

defaultConfig {
applicationId "com.danikula.videocache.sample"
applicationId 'com.danikula.videocache.sample'
minSdkVersion 15
targetSdkVersion 23
versionCode 1
Expand All @@ -38,7 +38,7 @@ dependencies {
// 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.3.3'
compile 'com.danikula:videocache:2.4.0'
compile 'com.viewpagerindicator:library:2.4.2-SNAPSHOT@aar'
apt 'org.androidannotations:androidannotations:3.3.2'
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.VideoView;

Expand All @@ -27,6 +28,7 @@ public class VideoFragment extends Fragment implements CacheListener {
@FragmentArg String url;
@FragmentArg String cachePath;

@ViewById ImageView cacheStatusImageView;
@ViewById VideoView videoView;
@ViewById ProgressBar progressBar;

Expand All @@ -45,9 +47,16 @@ public static Fragment build(String url, String cachePath) {

@AfterViews
void afterViewInjected() {
checkCachedState();
startVideo();
}

private void checkCachedState() {
HttpProxyCacheServer proxy = App.getProxy(getActivity());
boolean fullyCached = proxy.isCached(url);
setCachedState(fullyCached);
}

private void startVideo() {
HttpProxyCacheServer proxy = App.getProxy(getActivity());
proxy.registerCacheListener(this, url);
Expand Down Expand Up @@ -78,6 +87,7 @@ public void onDestroy() {
@Override
public void onCacheAvailable(File file, String url, int percentsAvailable) {
progressBar.setSecondaryProgress(percentsAvailable);
setCachedState(percentsAvailable == 100);
Log.d(LOG_TAG, String.format("onCacheAvailable. percents: %d, file: %s, url: %s", percentsAvailable, file, url));
}

Expand All @@ -92,6 +102,11 @@ void seekVideo() {
videoView.seekTo(videoPosition);
}

private void setCachedState(boolean cached) {
int statusIconId = cached ? R.drawable.ic_cloud_done : R.drawable.ic_cloud_download;
cacheStatusImageView.setImageResource(statusIconId);
}

private final class VideoProgressUpdater extends Handler {

public void start() {
Expand Down
Binary file added sample/src/main/res/drawable-hdpi/ic_cloud_done.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sample/src/main/res/drawable-mdpi/ic_cloud_done.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sample/src/main/res/drawable-xhdpi/ic_cloud_done.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions sample/src/main/res/layout/fragment_video.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />

<ImageView
android:id="@+id/cacheStatusImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:src="@drawable/ic_cloud_download" />

<SeekBar
android:id="@+id/progressBar"
style="@android:style/Widget.Holo.ProgressBar.Horizontal"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,52 @@ public void testMaxFileCacheLimit() throws Exception {
assertThat(file(cacheFolder, HTTP_DATA_URL_6_REDIRECTS)).exists();
}

@Test
public void testCheckFileExistForNotCachedUrl() throws Exception {
HttpProxyCacheServer proxy = newProxy(cacheFolder);
proxy.shutdown();
assertThat(proxy.isCached(HTTP_DATA_URL)).isFalse();
}

@Test
public void testCheckFileExistForFullyCachedUrl() throws Exception {
HttpProxyCacheServer proxy = newProxy(cacheFolder);
readProxyResponse(proxy, HTTP_DATA_URL, 0);
proxy.shutdown();

assertThat(proxy.isCached(HTTP_DATA_URL)).isTrue();
}

@Test
public void testCheckFileExistForPartiallyCachedUrl() throws Exception {
File cacheDir = RuntimeEnvironment.application.getExternalCacheDir();
File file = file(cacheDir, HTTP_DATA_URL);
int partialCacheSize = 1000;
byte[] partialData = ProxyCacheTestUtils.generate(partialCacheSize);
File partialCacheFile = ProxyCacheTestUtils.getTempFile(file);
IoUtils.saveToFile(partialData, partialCacheFile);

HttpProxyCacheServer proxy = newProxy(cacheDir);
assertThat(proxy.isCached(HTTP_DATA_URL)).isFalse();

readProxyResponse(proxy, HTTP_DATA_URL);
proxy.shutdown();

assertThat(proxy.isCached(HTTP_DATA_URL)).isTrue();
}

@Test
public void testCheckFileExistForDeletedCacheFile() throws Exception {
HttpProxyCacheServer proxy = newProxy(cacheFolder);
readProxyResponse(proxy, HTTP_DATA_URL, 0);
proxy.shutdown();
File cacheFile = file(cacheFolder, HTTP_DATA_URL);
boolean deleted = cacheFile.delete();

assertThat(deleted).isTrue();
assertThat(proxy.isCached(HTTP_DATA_URL)).isFalse();
}

private Pair<File, Response> readProxyData(String url, int offset) throws IOException {
File file = file(cacheFolder, url);
HttpProxyCacheServer proxy = newProxy(cacheFolder);
Expand Down

0 comments on commit cd0b411

Please sign in to comment.