fix :bug: available cache percents callback · sunriver/AndroidVideoCache@6125478 · GitHub
Skip to content

Commit 6125478

Browse files
committed
fix 🐛 available cache percents callback
1 parent e135bf0 commit 6125478

8 files changed

Lines changed: 65 additions & 23 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ repositories {
1212
maven { url 'https://dl.bintray.com/alexeydanilov/maven' }
1313
}
1414
dependencies {
15-
compile 'com.danikula:videocache:2.1.3'
15+
compile 'com.danikula:videocache:2.1.4'
1616
}
1717
```
1818

@@ -59,14 +59,17 @@ More preferable way is use some dependency injector like [Dagger](http://square.
5959
See `sample` app for details.
6060

6161
## Whats new
62+
### 2.1.4
63+
- [fix](https://github.com/danikula/AndroidVideoCache/issues/18) available cache percents callback
64+
6265
### 2.1.3
6366
- ping proxy after starting to make sure it works fine
6467

6568
### 2.1.2
66-
- fix offline work
69+
- [fix](https://github.com/danikula/AndroidVideoCache/issues/13) offline work
6770

6871
### 2.1.1
69-
- fix for too long cache file name
72+
- [fix](https://github.com/danikula/AndroidVideoCache/issues/14) for too long cache file name
7073
- url redirects support (thanks [ongakuer](https://github.com/ongakuer) for [PR](https://github.com/danikula/AndroidVideoCache/pull/12))
7174

7275
### 2.0

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ publish {
2626
userOrg = 'alexeydanilov'
2727
groupId = 'com.danikula'
2828
artifactId = 'videocache'
29-
publishVersion = '2.1.3'
29+
publishVersion = '2.1.4'
3030
description = 'Cache support for android VideoView'
3131
website = 'https://github.com/danikula/AndroidVideoCache'
3232
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ public void processRequest(GetRequest request, Socket socket) throws IOException
4343
}
4444
out.write(buffer, 0, readBytes);
4545
offset += readBytes;
46-
if (cache.isCompleted()) {
47-
onCacheAvailable(100);
48-
}
4946
}
5047
out.flush();
5148
}
@@ -68,7 +65,7 @@ private String newResponseHeaders(GetRequest request) throws IOException, ProxyC
6865
}
6966

7067
@Override
71-
protected void onCacheAvailable(int percents) {
68+
protected void onCachePercentsAvailableChanged(int percents) {
7269
if (listener != null) {
7370
listener.onCacheAvailable(cache.file, source.url, percents);
7471
}

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ class ProxyCache {
2424
private final Cache cache;
2525
private final Object wc = new Object();
2626
private final Object stopLock = new Object();
27+
private final AtomicInteger readSourceErrorsCount;
2728
private volatile Thread sourceReaderThread;
2829
private volatile boolean stopped;
29-
private final AtomicInteger readSourceErrorsCount;
30+
private volatile int percentsAvailable = -1;
3031

3132
public ProxyCache(Source source, Cache cache) {
3233
this.source = checkNotNull(source);
@@ -42,7 +43,12 @@ public int read(byte[] buffer, long offset, int length) throws ProxyCacheExcepti
4243
waitForSourceData();
4344
checkReadSourceErrorsCount();
4445
}
45-
return cache.read(buffer, offset, length);
46+
int read = cache.read(buffer, offset, length);
47+
if (cache.isCompleted() && percentsAvailable != 100) {
48+
percentsAvailable = 100;
49+
onCachePercentsAvailableChanged(100);
50+
}
51+
return read;
4652
}
4753

4854
private void checkReadSourceErrorsCount() throws ProxyCacheException {
@@ -86,22 +92,34 @@ private void waitForSourceData() throws ProxyCacheException {
8692
}
8793
}
8894

89-
private void notifyNewCacheDataAvailable(int cachePercentage) {
90-
onCacheAvailable(cachePercentage);
95+
private void notifyNewCacheDataAvailable(long cacheAvailable, long sourceAvailable) {
96+
onCacheAvailable(cacheAvailable, sourceAvailable);
9197

9298
synchronized (wc) {
9399
wc.notifyAll();
94100
}
95101
}
96102

97-
protected void onCacheAvailable(int percents) {
103+
protected void onCacheAvailable(long cacheAvailable, long sourceAvailable) {
104+
int percents = (int) (cacheAvailable * 100 / sourceAvailable);
105+
boolean percentsChanged = percents != percentsAvailable;
106+
boolean sourceLengthKnown = sourceAvailable >= 0;
107+
if (sourceLengthKnown && percentsChanged) {
108+
onCachePercentsAvailableChanged(percents);
109+
}
110+
percentsAvailable = percents;
111+
}
112+
113+
protected void onCachePercentsAvailableChanged(int percentsAvailable) {
98114
}
99115

100116
private void readSource() {
101-
int cachePercentage = 0;
117+
int sourceAvailable = -1;
118+
int offset = 0;
102119
try {
103-
int offset = cache.available();
120+
offset = cache.available();
104121
source.open(offset);
122+
sourceAvailable = source.available();
105123
byte[] buffer = new byte[ProxyCacheUtils.DEFAULT_BUFFER_SIZE];
106124
int readBytes;
107125
while ((readBytes = source.read(buffer)) != -1) {
@@ -112,17 +130,15 @@ private void readSource() {
112130
cache.append(buffer, readBytes);
113131
}
114132
offset += readBytes;
115-
cachePercentage = offset * 100 / source.available();
116-
117-
notifyNewCacheDataAvailable(cachePercentage);
133+
notifyNewCacheDataAvailable(offset, sourceAvailable);
118134
}
119135
tryComplete();
120136
} catch (Throwable e) {
121137
readSourceErrorsCount.incrementAndGet();
122138
onError(e);
123139
} finally {
124140
closeSource();
125-
notifyNewCacheDataAvailable(cachePercentage);
141+
notifyNewCacheDataAvailable(offset, sourceAvailable);
126142
}
127143
}
128144

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,34 @@
77
*/
88
public interface Source {
99

10-
int available() throws ProxyCacheException;
11-
10+
/**
11+
* Opens source. Source should be open before using {@link #read(byte[])}
12+
*
13+
* @param offset offset in bytes for source.
14+
* @throws ProxyCacheException if error occur while opening source.
15+
*/
1216
void open(int offset) throws ProxyCacheException;
1317

14-
void close() throws ProxyCacheException;
18+
/**
19+
* Returns available bytes or <b>negative value</b> if available bytes count is unknown.
20+
*
21+
* @return bytes available
22+
* @throws ProxyCacheException if error occur while fetching source data.
23+
*/
24+
int available() throws ProxyCacheException;
1525

26+
/**
27+
* Read data to byte buffer from source with current offset.
28+
*
29+
* @param buffer a buffer to be used for reading data.
30+
* @throws ProxyCacheException if error occur while reading source.
31+
*/
1632
int read(byte[] buffer) throws ProxyCacheException;
33+
34+
/**
35+
* Closes source and release resources. Every opened source should be closed.
36+
*
37+
* @throws ProxyCacheException if error occur while closing source.
38+
*/
39+
void close() throws ProxyCacheException;
1740
}

sample/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ dependencies {
3939
// compile project(':library')
4040
compile 'com.android.support:support-v4:23.0.1'
4141
compile 'org.androidannotations:androidannotations-api:3.3.2'
42-
compile 'com.danikula:videocache:2.1.3'
42+
compile 'com.danikula:videocache:2.1.4'
4343
compile 'com.viewpagerindicator:library:2.4.2-SNAPSHOT@aar'
4444
apt 'org.androidannotations:androidannotations:3.3.2'
4545
}

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

Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)