Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cd0b4111a4 | ||
|
|
eb67640212 | ||
|
|
1d50807fe0 | ||
|
|
6da9650030 | ||
|
|
a1d00fec7b | ||
|
|
f5a779266d | ||
|
|
ca6f36ada3 |
28
README.md
@@ -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.3'
|
||||
compile 'com.danikula:videocache:2.4.0'
|
||||
}
|
||||
```
|
||||
|
||||
@@ -64,6 +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/).
|
||||
|
||||
## Recipes
|
||||
### Disk cache limit
|
||||
By default `HttpProxyCacheServer` uses 512Mb for caching files. You can change this value:
|
||||
|
||||
```java
|
||||
@@ -82,9 +84,17 @@ private HttpProxyCacheServer newProxy() {
|
||||
.maxCacheFilesCount(20)
|
||||
.build();
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
See `sample` app for details.
|
||||
### 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.
|
||||
|
||||
### Sample
|
||||
See `sample` app.
|
||||
|
||||
## Whats new
|
||||
See Release Notes [here](https://github.com/danikula/AndroidVideoCache/releases)
|
||||
@@ -94,9 +104,11 @@ If it's a feature that you think would need to be discussed please open an issue
|
||||
|
||||
1. [Fork the project](http://help.github.com/fork-a-repo/)
|
||||
2. Create a feature branch (git checkout -b my_branch)
|
||||
3. Push your changes to your new branch (git push origin my_branch)
|
||||
4. Initiate a [pull request](http://help.github.com/send-pull-requests/) on github
|
||||
5. Your pull request will be reviewed and hopefully merged :)
|
||||
3. Fix a problem. Your code **must** contain test for reproducing problem. Your tests **must be passed** with help of your fix
|
||||
4. Push your changes to your new branch (git push origin my_branch)
|
||||
5. Initiate a [pull request](http://help.github.com/send-pull-requests/) on github
|
||||
6. Rebase [master branch](https://github.com/danikula/AndroidVideoCache) if your local branch is not actual. Merging is not acceptable, only rebase
|
||||
6. Your pull request will be reviewed and hopefully merged :)
|
||||
|
||||
## Where published?
|
||||
[Here](https://bintray.com/alexeydanilov/maven/videocache/view)
|
||||
@@ -106,7 +118,7 @@ If it's a feature that you think would need to be discussed please open an issue
|
||||
|
||||
## License
|
||||
|
||||
Copyright 2014-2015 Alexey Danilov
|
||||
Copyright 2014-2016 Alexey Danilov
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -3,7 +3,7 @@ buildscript {
|
||||
mavenCentral()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:1.3.0'
|
||||
classpath 'com.android.tools.build:gradle:2.1.2'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
BIN
files/android
Normal file
|
After Width: | Height: | Size: 4.7 KiB |
BIN
files/android.jpg
Normal file
|
After Width: | Height: | Size: 4.7 KiB |
BIN
files/devbytes.mp4
Normal file
0
files/empty.txt
Normal file
BIN
files/orange1.mp4
Normal file
BIN
files/orange2.mp4
Normal file
BIN
files/orange3.mp4
Normal file
BIN
files/orange4.mp4
Normal file
BIN
files/orange5.mp4
Normal file
BIN
files/phones.jpg
Normal file
|
After Width: | Height: | Size: 92 KiB |
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-all.zip
|
||||
|
||||
@@ -3,7 +3,7 @@ buildscript {
|
||||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.novoda:bintray-release:0.2.10'
|
||||
classpath 'com.novoda:bintray-release:0.3.4'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,9 @@ idea {
|
||||
}
|
||||
}
|
||||
|
||||
targetCompatibility = '1.7'
|
||||
sourceCompatibility = '1.7'
|
||||
|
||||
dependencies {
|
||||
compile 'com.google.android:android:1.6_r2'
|
||||
}
|
||||
@@ -26,7 +29,7 @@ publish {
|
||||
userOrg = 'alexeydanilov'
|
||||
groupId = 'com.danikula'
|
||||
artifactId = 'videocache'
|
||||
publishVersion = '2.3.3'
|
||||
publishVersion = '2.4.0'
|
||||
description = 'Cache support for android VideoView'
|
||||
website = 'https://github.com/danikula/AndroidVideoCache'
|
||||
}
|
||||
|
||||
@@ -173,6 +173,20 @@ public class HttpProxyCacheServer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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");
|
||||
|
||||
|
||||
@@ -16,10 +16,10 @@ apply plugin: 'com.neenbedankt.android-apt'
|
||||
|
||||
android {
|
||||
compileSdkVersion 23
|
||||
buildToolsVersion '23.0.1'
|
||||
buildToolsVersion '24'
|
||||
|
||||
defaultConfig {
|
||||
applicationId "com.danikula.videocache.sample"
|
||||
applicationId 'com.danikula.videocache.sample'
|
||||
minSdkVersion 15
|
||||
targetSdkVersion 23
|
||||
versionCode 1
|
||||
@@ -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'
|
||||
}
|
||||
|
||||
@@ -6,11 +6,11 @@ import java.io.File;
|
||||
|
||||
public enum Video {
|
||||
|
||||
ORANGE_1("https://dl.dropboxusercontent.com/u/15506779/persistent/proxycache/orange1.mp4"),
|
||||
ORANGE_2("https://dl.dropboxusercontent.com/u/15506779/persistent/proxycache/orange2.mp4"),
|
||||
ORANGE_3("https://dl.dropboxusercontent.com/u/15506779/persistent/proxycache/orange3.mp4"),
|
||||
ORANGE_4("https://dl.dropboxusercontent.com/u/15506779/persistent/proxycache/orange4.mp4"),
|
||||
ORANGE_5("https://dl.dropboxusercontent.com/u/15506779/persistent/proxycache/orange5.mp4");
|
||||
ORANGE_1(Config.ROOT + "orange1.mp4"),
|
||||
ORANGE_2(Config.ROOT + "orange2.mp4"),
|
||||
ORANGE_3(Config.ROOT + "orange3.mp4"),
|
||||
ORANGE_4(Config.ROOT + "orange4.mp4"),
|
||||
ORANGE_5(Config.ROOT + "orange5.mp4");
|
||||
|
||||
public final String url;
|
||||
|
||||
@@ -21,4 +21,8 @@ public enum Video {
|
||||
public File getCacheFile(Context context) {
|
||||
return new File(context.getExternalCacheDir(), name());
|
||||
}
|
||||
|
||||
private class Config {
|
||||
private static final String ROOT = "https://raw.githubusercontent.com/danikula/AndroidVideoCache/master/files/";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.os.Handler;
|
||||
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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -45,9 +47,16 @@ public class VideoFragment extends Fragment implements CacheListener {
|
||||
|
||||
@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);
|
||||
@@ -78,6 +87,7 @@ public class VideoFragment extends Fragment implements CacheListener {
|
||||
@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));
|
||||
}
|
||||
|
||||
@@ -92,6 +102,11 @@ public class VideoFragment extends Fragment implements CacheListener {
|
||||
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() {
|
||||
|
||||
BIN
sample/src/main/res/drawable-hdpi/ic_cloud_done.png
Normal file
|
After Width: | Height: | Size: 379 B |
BIN
sample/src/main/res/drawable-hdpi/ic_cloud_download.png
Normal file
|
After Width: | Height: | Size: 353 B |
BIN
sample/src/main/res/drawable-mdpi/ic_cloud_done.png
Normal file
|
After Width: | Height: | Size: 265 B |
BIN
sample/src/main/res/drawable-mdpi/ic_cloud_download.png
Normal file
|
After Width: | Height: | Size: 242 B |
BIN
sample/src/main/res/drawable-xhdpi/ic_cloud_done.png
Normal file
|
After Width: | Height: | Size: 449 B |
BIN
sample/src/main/res/drawable-xhdpi/ic_cloud_download.png
Normal file
|
After Width: | Height: | Size: 417 B |
BIN
sample/src/main/res/drawable-xxhdpi/ic_cloud_done.png
Normal file
|
After Width: | Height: | Size: 672 B |
BIN
sample/src/main/res/drawable-xxhdpi/ic_cloud_download.png
Normal file
|
After Width: | Height: | Size: 610 B |
BIN
sample/src/main/res/drawable-xxxhdpi/ic_cloud_done.png
Normal file
|
After Width: | Height: | Size: 855 B |
BIN
sample/src/main/res/drawable-xxxhdpi/ic_cloud_download.png
Normal file
|
After Width: | Height: | Size: 789 B |
@@ -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"
|
||||
|
||||
@@ -82,7 +82,7 @@ public class HttpProxyCacheServerTest {
|
||||
|
||||
@Test
|
||||
public void testMimeFromResponse() throws Exception {
|
||||
Pair<File, Response> response = readProxyData("https://dl.dropboxusercontent.com/u/15506779/persistent/proxycache/android");
|
||||
Pair<File, Response> response = readProxyData("https://raw.githubusercontent.com/danikula/AndroidVideoCache/master/files/android");
|
||||
assertThat(response.second.contentType).isEqualTo("application/octet-stream");
|
||||
}
|
||||
|
||||
@@ -210,6 +210,52 @@ public class HttpProxyCacheServerTest {
|
||||
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);
|
||||
|
||||
@@ -78,7 +78,7 @@ public class HttpProxyCacheTest {
|
||||
|
||||
@Test
|
||||
public void testLoadEmptyFile() throws Exception {
|
||||
String zeroSizeUrl = "https://dl.dropboxusercontent.com/u/15506779/persistent/proxycache/empty.txt";
|
||||
String zeroSizeUrl = "https://raw.githubusercontent.com/danikula/AndroidVideoCache/master/files/empty.txt";
|
||||
HttpUrlSource source = new HttpUrlSource(zeroSizeUrl);
|
||||
HttpProxyCache proxyCache = new HttpProxyCache(source, new FileCache(ProxyCacheTestUtils.newCacheFile()));
|
||||
GetRequest request = new GetRequest("GET /" + HTTP_DATA_URL + " HTTP/1.1");
|
||||
|
||||
@@ -11,6 +11,7 @@ import org.robolectric.annotation.Config;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static com.danikula.videocache.ProxyCacheUtils.DEFAULT_BUFFER_SIZE;
|
||||
import static com.danikula.videocache.support.ProxyCacheTestUtils.ASSETS_DATA_BIG_NAME;
|
||||
import static com.danikula.videocache.support.ProxyCacheTestUtils.ASSETS_DATA_NAME;
|
||||
import static com.danikula.videocache.support.ProxyCacheTestUtils.HTTP_DATA_BIG_SIZE;
|
||||
@@ -83,7 +84,7 @@ public class HttpUrlSourceTest {
|
||||
HttpUrlSource source = new HttpUrlSource(HTTP_DATA_URL_ONE_REDIRECT);
|
||||
source.open(0);
|
||||
byte[] readData = new byte[HTTP_DATA_SIZE];
|
||||
source.read(readData);
|
||||
readSource(source, readData);
|
||||
source.close();
|
||||
|
||||
byte[] expectedData = Arrays.copyOfRange(loadAssetFile(ASSETS_DATA_NAME), 0, HTTP_DATA_SIZE);
|
||||
@@ -96,7 +97,7 @@ public class HttpUrlSourceTest {
|
||||
HttpUrlSource source = new HttpUrlSource(HTTP_DATA_URL_ONE_REDIRECT);
|
||||
source.open(offset);
|
||||
byte[] readData = new byte[HTTP_DATA_SIZE - offset];
|
||||
source.read(readData);
|
||||
readSource(source, readData);
|
||||
source.close();
|
||||
|
||||
byte[] expectedData = Arrays.copyOfRange(loadAssetFile(ASSETS_DATA_NAME), offset, HTTP_DATA_SIZE);
|
||||
@@ -109,7 +110,7 @@ public class HttpUrlSourceTest {
|
||||
HttpUrlSource source = new HttpUrlSource(HTTP_DATA_URL_3_REDIRECTS);
|
||||
source.open(offset);
|
||||
byte[] readData = new byte[HTTP_DATA_SIZE - offset];
|
||||
source.read(readData);
|
||||
readSource(source, readData);
|
||||
source.close();
|
||||
|
||||
byte[] expectedData = Arrays.copyOfRange(loadAssetFile(ASSETS_DATA_NAME), offset, HTTP_DATA_SIZE);
|
||||
@@ -130,4 +131,14 @@ public class HttpUrlSourceTest {
|
||||
assertThat(new HttpUrlSource("http://mysite.by/video.mp4").getMime()).isEqualTo("video/mp4");
|
||||
assertThat(new HttpUrlSource(HTTP_DATA_URL).getMime()).isEqualTo("image/jpeg");
|
||||
}
|
||||
|
||||
private void readSource(Source source, byte[] target) throws ProxyCacheException {
|
||||
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
|
||||
int totalRead = 0;
|
||||
int readBytes;
|
||||
while ((readBytes = source.read(buffer)) != -1) {
|
||||
System.arraycopy(buffer, 0, target, totalRead, readBytes);
|
||||
totalRead += readBytes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,12 +20,12 @@ import java.util.UUID;
|
||||
*/
|
||||
public class ProxyCacheTestUtils {
|
||||
|
||||
public static final String HTTP_DATA_URL = "https://dl.dropboxusercontent.com/u/15506779/persistent/proxycache/android.jpg";
|
||||
public static final String HTTP_DATA_URL_ONE_REDIRECT = "http://bit.ly/1V5PeY5";
|
||||
public static final String HTTP_DATA_URL_3_REDIRECTS = "http://bit.ly/1KvVmgZ";
|
||||
public static final String HTTP_DATA_URL_6_REDIRECTS = "http://ow.ly/SugRH";
|
||||
public static final String HTTP_DATA_BIG_URL = "https://dl.dropboxusercontent.com/u/15506779/persistent/proxycache/phones.jpg";
|
||||
public static final String HTTP_DATA_BIG_URL_ONE_REDIRECT = "http://bit.ly/1iJ69yA";
|
||||
public static final String HTTP_DATA_URL = "https://raw.githubusercontent.com/danikula/AndroidVideoCache/master/files/android.jpg";
|
||||
public static final String HTTP_DATA_URL_ONE_REDIRECT = "http://bit.ly/1LAJKAy";
|
||||
public static final String HTTP_DATA_URL_3_REDIRECTS = "http://bit.ly/1QtKJiB";
|
||||
public static final String HTTP_DATA_URL_6_REDIRECTS = "http://ow.ly/Z17wz";
|
||||
public static final String HTTP_DATA_BIG_URL = "https://raw.githubusercontent.com/danikula/AndroidVideoCache/master/files/phones.jpg";
|
||||
public static final String HTTP_DATA_BIG_URL_ONE_REDIRECT = "http://bit.ly/24DdZ06";
|
||||
public static final String ASSETS_DATA_NAME = "android.jpg";
|
||||
public static final String ASSETS_DATA_BIG_NAME = "phones.jpg";
|
||||
public static final int HTTP_DATA_SIZE = 4768;
|
||||
|
||||