Skip to content

Commit cd0b411

Browse files
committed
ability to check was content fully cached to file or not
1 parent eb67640 commit cd0b411

File tree

17 files changed

+99
-8
lines changed

17 files changed

+99
-8
lines changed

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ Because there is no sense to download video a lot of times while streaming!
1414

1515
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.
1616

17-
## How to use?
17+
## Get started
1818
Just add dependency (`AndroidVideoCache` is available in jcenter):
1919
```
2020
repositories {
2121
jcenter()
2222
}
2323
dependencies {
24-
compile 'com.danikula:videocache:2.3.4'
24+
compile 'com.danikula:videocache:2.4.0'
2525
}
2626
```
2727

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

67-
## Configuration
67+
## Recipes
68+
### Disk cache limit
6869
By default `HttpProxyCacheServer` uses 512Mb for caching files. You can change this value:
6970

7071
```java
@@ -83,9 +84,17 @@ private HttpProxyCacheServer newProxy() {
8384
.maxCacheFilesCount(20)
8485
.build();
8586
}
86-
```
87+
```
88+
89+
### Listen caching progress
90+
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.
91+
92+
Use `HttpProxyCacheServer.isCached(String url)` method to check was url's content fully cached to file or not.
93+
94+
See `sample` app for more details.
8795

88-
See `sample` app for details.
96+
### Sample
97+
See `sample` app.
8998

9099
## Whats new
91100
See Release Notes [here](https://github.com/danikula/AndroidVideoCache/releases)

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ publish {
2929
userOrg = 'alexeydanilov'
3030
groupId = 'com.danikula'
3131
artifactId = 'videocache'
32-
publishVersion = '2.3.4'
32+
publishVersion = '2.4.0'
3333
description = 'Cache support for android VideoView'
3434
website = 'https://github.com/danikula/AndroidVideoCache'
3535
}

library/src/main/java/com/danikula/videocache/HttpProxyCacheServer.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ public void unregisterCacheListener(CacheListener cacheListener) {
173173
}
174174
}
175175

176+
/**
177+
* Checks is cache contains fully cached file for particular url.
178+
*
179+
* @param url an url cache file will be checked for.
180+
* @return {@code true} if cache contains fully cached file for passed in parameters url.
181+
*/
182+
public boolean isCached(String url) {
183+
checkNotNull(url, "Url can't be null!");
184+
File cacheDir = config.cacheRoot;
185+
String fileName = config.fileNameGenerator.generate(url);
186+
File cacheFile = new File(cacheDir, fileName);
187+
return cacheFile.exists();
188+
}
189+
176190
public void shutdown() {
177191
Log.i(LOG_TAG, "Shutdown proxy server");
178192

sample/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ android {
1919
buildToolsVersion '24'
2020

2121
defaultConfig {
22-
applicationId "com.danikula.videocache.sample"
22+
applicationId 'com.danikula.videocache.sample'
2323
minSdkVersion 15
2424
targetSdkVersion 23
2525
versionCode 1
@@ -38,7 +38,7 @@ dependencies {
3838
// compile project(':library')
3939
compile 'com.android.support:support-v4:23.1.0'
4040
compile 'org.androidannotations:androidannotations-api:3.3.2'
41-
compile 'com.danikula:videocache:2.3.3'
41+
compile 'com.danikula:videocache:2.4.0'
4242
compile 'com.viewpagerindicator:library:2.4.2-SNAPSHOT@aar'
4343
apt 'org.androidannotations:androidannotations:3.3.2'
4444
}

sample/src/main/java/com/danikula/videocache/sample/VideoFragment.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.os.Message;
66
import android.support.v4.app.Fragment;
77
import android.util.Log;
8+
import android.widget.ImageView;
89
import android.widget.ProgressBar;
910
import android.widget.VideoView;
1011

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

31+
@ViewById ImageView cacheStatusImageView;
3032
@ViewById VideoView videoView;
3133
@ViewById ProgressBar progressBar;
3234

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

4648
@AfterViews
4749
void afterViewInjected() {
50+
checkCachedState();
4851
startVideo();
4952
}
5053

54+
private void checkCachedState() {
55+
HttpProxyCacheServer proxy = App.getProxy(getActivity());
56+
boolean fullyCached = proxy.isCached(url);
57+
setCachedState(fullyCached);
58+
}
59+
5160
private void startVideo() {
5261
HttpProxyCacheServer proxy = App.getProxy(getActivity());
5362
proxy.registerCacheListener(this, url);
@@ -78,6 +87,7 @@ public void onDestroy() {
7887
@Override
7988
public void onCacheAvailable(File file, String url, int percentsAvailable) {
8089
progressBar.setSecondaryProgress(percentsAvailable);
90+
setCachedState(percentsAvailable == 100);
8191
Log.d(LOG_TAG, String.format("onCacheAvailable. percents: %d, file: %s, url: %s", percentsAvailable, file, url));
8292
}
8393

@@ -92,6 +102,11 @@ void seekVideo() {
92102
videoView.seekTo(videoPosition);
93103
}
94104

105+
private void setCachedState(boolean cached) {
106+
int statusIconId = cached ? R.drawable.ic_cloud_done : R.drawable.ic_cloud_download;
107+
cacheStatusImageView.setImageResource(statusIconId);
108+
}
109+
95110
private final class VideoProgressUpdater extends Handler {
96111

97112
public void start() {
379 Bytes
Loading
353 Bytes
Loading
265 Bytes
Loading
242 Bytes
Loading
449 Bytes
Loading

0 commit comments

Comments
 (0)