From b726d52e139cedac80a5c67d6a52860f54a5906d Mon Sep 17 00:00:00 2001 From: gome Date: Tue, 9 Apr 2019 12:47:04 +0800 Subject: [PATCH 1/2] add interface to change http request this will be useful when your url is need sign every time you request --- build.gradle | 4 ++- gradle/wrapper/gradle-wrapper.properties | 4 +-- library/build.gradle | 36 ++++++------------- .../java/com/danikula/videocache/Config.java | 5 ++- .../videocache/HttpProxyCacheServer.java | 15 +++++++- .../HttpProxyCacheServerClients.java | 10 +++++- .../danikula/videocache/HttpUrlSource.java | 20 +++++++---- .../com/danikula/videocache/SourceInfo.java | 4 +++ .../extend/IHttpUrlSourceMaker.java | 11 ++++++ sample/build.gradle | 17 ++++----- settings.gradle | 4 ++- test/src/main/AndroidManifest.xml | 2 -- 12 files changed, 82 insertions(+), 50 deletions(-) create mode 100644 library/src/main/java/com/danikula/videocache/extend/IHttpUrlSourceMaker.java diff --git a/build.gradle b/build.gradle index 8ccd9c9..5634595 100644 --- a/build.gradle +++ b/build.gradle @@ -1,14 +1,16 @@ buildscript { repositories { jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.1' + classpath 'com.android.tools.build:gradle:3.3.2' } } allprojects { repositories { + google() jcenter() } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 3c11526..84e1ddd 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Tue Apr 18 11:58:38 MSK 2017 +#Tue Apr 09 10:31:28 CST 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip diff --git a/library/build.gradle b/library/build.gradle index 15dbe3d..3bb10d0 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -1,23 +1,14 @@ -buildscript { - repositories { - jcenter() - } - dependencies { - classpath 'com.novoda:bintray-release:0.4.0' - } -} apply plugin: 'com.android.library' -apply plugin: 'idea' -apply plugin: 'bintray-release' +//apply plugin: 'idea' android { - compileSdkVersion 23 + compileSdkVersion 26 buildToolsVersion '25.0.2' defaultConfig { minSdkVersion 9 - targetSdkVersion 23 + targetSdkVersion 26 versionCode 22 versionName '2.7.1' } @@ -28,22 +19,15 @@ android { } } -idea { - module { - downloadJavadoc = true - downloadSources = true - } -} +//idea { +// module { +// downloadJavadoc = true +// downloadSources = true +// } +//} dependencies { compile 'org.slf4j:slf4j-android:1.7.21' } -publish { - userOrg = 'alexeydanilov' - groupId = 'com.danikula' - artifactId = 'videocache' - publishVersion = android.defaultConfig.versionName - description = 'Cache support for android VideoView' - website = 'https://github.com/danikula/AndroidVideoCache' -} + diff --git a/library/src/main/java/com/danikula/videocache/Config.java b/library/src/main/java/com/danikula/videocache/Config.java index 29ab95c..25add15 100644 --- a/library/src/main/java/com/danikula/videocache/Config.java +++ b/library/src/main/java/com/danikula/videocache/Config.java @@ -1,5 +1,6 @@ package com.danikula.videocache; +import com.danikula.videocache.extend.IHttpUrlSourceMaker; import com.danikula.videocache.file.DiskUsage; import com.danikula.videocache.file.FileNameGenerator; import com.danikula.videocache.headers.HeaderInjector; @@ -19,13 +20,15 @@ class Config { public final DiskUsage diskUsage; public final SourceInfoStorage sourceInfoStorage; public final HeaderInjector headerInjector; + public final IHttpUrlSourceMaker sourceMaker; - Config(File cacheRoot, FileNameGenerator fileNameGenerator, DiskUsage diskUsage, SourceInfoStorage sourceInfoStorage, HeaderInjector headerInjector) { + Config(File cacheRoot, FileNameGenerator fileNameGenerator, DiskUsage diskUsage, SourceInfoStorage sourceInfoStorage, HeaderInjector headerInjector,IHttpUrlSourceMaker sourceMaker) { this.cacheRoot = cacheRoot; this.fileNameGenerator = fileNameGenerator; this.diskUsage = diskUsage; this.sourceInfoStorage = sourceInfoStorage; this.headerInjector = headerInjector; + this.sourceMaker = sourceMaker; } File generateCacheFile(String url) { diff --git a/library/src/main/java/com/danikula/videocache/HttpProxyCacheServer.java b/library/src/main/java/com/danikula/videocache/HttpProxyCacheServer.java index 1f34ae8..31920bb 100644 --- a/library/src/main/java/com/danikula/videocache/HttpProxyCacheServer.java +++ b/library/src/main/java/com/danikula/videocache/HttpProxyCacheServer.java @@ -3,6 +3,7 @@ import android.content.Context; import android.net.Uri; +import com.danikula.videocache.extend.IHttpUrlSourceMaker; import com.danikula.videocache.file.DiskUsage; import com.danikula.videocache.file.FileNameGenerator; import com.danikula.videocache.file.Md5FileNameGenerator; @@ -353,6 +354,7 @@ public static final class Builder { private DiskUsage diskUsage; private SourceInfoStorage sourceInfoStorage; private HeaderInjector headerInjector; + private IHttpUrlSourceMaker sourceMaker; public Builder(Context context) { this.sourceInfoStorage = SourceInfoStorageFactory.newSourceInfoStorage(context); @@ -419,6 +421,17 @@ public Builder maxCacheFilesCount(int count) { return this; } + /** + * Set a http source maker which can build your defined http source.This give you a change to + * change the http request,such as sign url at so on. + * @param sourceMaker + * @return + */ + public Builder sourceMaker(IHttpUrlSourceMaker sourceMaker) { + this.sourceMaker = sourceMaker; + return this; + } + /** * Set custom DiskUsage logic for handling when to keep or clean cache. * @@ -452,7 +465,7 @@ public HttpProxyCacheServer build() { } private Config buildConfig() { - return new Config(cacheRoot, fileNameGenerator, diskUsage, sourceInfoStorage, headerInjector); + return new Config(cacheRoot, fileNameGenerator, diskUsage, sourceInfoStorage, headerInjector,sourceMaker); } } diff --git a/library/src/main/java/com/danikula/videocache/HttpProxyCacheServerClients.java b/library/src/main/java/com/danikula/videocache/HttpProxyCacheServerClients.java index df12622..0061c10 100644 --- a/library/src/main/java/com/danikula/videocache/HttpProxyCacheServerClients.java +++ b/library/src/main/java/com/danikula/videocache/HttpProxyCacheServerClients.java @@ -79,7 +79,15 @@ public int getClientsCount() { } private HttpProxyCache newHttpProxyCache() throws ProxyCacheException { - HttpUrlSource source = new HttpUrlSource(url, config.sourceInfoStorage, config.headerInjector); + HttpUrlSource source = null; + if(null != config.sourceMaker) + { + source = config.sourceMaker.createHttpUrlSource(url, config.sourceInfoStorage, config.headerInjector); + } + else + { + source = new HttpUrlSource(url, config.sourceInfoStorage, config.headerInjector); + } FileCache cache = new FileCache(config.generateCacheFile(url), config.diskUsage); HttpProxyCache httpProxyCache = new HttpProxyCache(source, cache); httpProxyCache.registerCacheListener(uiCacheListener); diff --git a/library/src/main/java/com/danikula/videocache/HttpUrlSource.java b/library/src/main/java/com/danikula/videocache/HttpUrlSource.java index c7fb8ad..af8da19 100644 --- a/library/src/main/java/com/danikula/videocache/HttpUrlSource.java +++ b/library/src/main/java/com/danikula/videocache/HttpUrlSource.java @@ -33,14 +33,14 @@ */ public class HttpUrlSource implements Source { - private static final Logger LOG = LoggerFactory.getLogger("HttpUrlSource"); + protected static final Logger LOG = LoggerFactory.getLogger("HttpUrlSource"); - private static final int MAX_REDIRECTS = 5; - private final SourceInfoStorage sourceInfoStorage; - private final HeaderInjector headerInjector; - private SourceInfo sourceInfo; - private HttpURLConnection connection; - private InputStream inputStream; + protected static final int MAX_REDIRECTS = 5; + protected final SourceInfoStorage sourceInfoStorage; + protected final HeaderInjector headerInjector; + protected SourceInfo sourceInfo; + protected HttpURLConnection connection; + protected InputStream inputStream; public HttpUrlSource(String url) { this(url, SourceInfoStorageFactory.newEmptySourceInfoStorage()); @@ -58,6 +58,12 @@ public HttpUrlSource(String url, SourceInfoStorage sourceInfoStorage, HeaderInje new SourceInfo(url, Integer.MIN_VALUE, ProxyCacheUtils.getSupposablyMime(url)); } + public HttpUrlSource(SourceInfoStorage sourceInfoStorage, HeaderInjector headerInjector) + { + this.sourceInfoStorage = checkNotNull(sourceInfoStorage); + this.headerInjector = checkNotNull(headerInjector); + } + public HttpUrlSource(HttpUrlSource source) { this.sourceInfo = source.sourceInfo; this.sourceInfoStorage = source.sourceInfoStorage; diff --git a/library/src/main/java/com/danikula/videocache/SourceInfo.java b/library/src/main/java/com/danikula/videocache/SourceInfo.java index 8079428..dd89ab6 100644 --- a/library/src/main/java/com/danikula/videocache/SourceInfo.java +++ b/library/src/main/java/com/danikula/videocache/SourceInfo.java @@ -10,6 +10,10 @@ public class SourceInfo { public final String url; public final long length; public final String mime; + /** + * the base url + */ + public String baseUrl; public SourceInfo(String url, long length, String mime) { this.url = url; diff --git a/library/src/main/java/com/danikula/videocache/extend/IHttpUrlSourceMaker.java b/library/src/main/java/com/danikula/videocache/extend/IHttpUrlSourceMaker.java new file mode 100644 index 0000000..e58ffed --- /dev/null +++ b/library/src/main/java/com/danikula/videocache/extend/IHttpUrlSourceMaker.java @@ -0,0 +1,11 @@ +package com.danikula.videocache.extend; + +import com.danikula.videocache.HttpUrlSource; +import com.danikula.videocache.Source; +import com.danikula.videocache.headers.HeaderInjector; +import com.danikula.videocache.sourcestorage.SourceInfoStorage; + +public interface IHttpUrlSourceMaker +{ + HttpUrlSource createHttpUrlSource(String url, SourceInfoStorage sourceInfoStorage, HeaderInjector headerInjector); +} diff --git a/sample/build.gradle b/sample/build.gradle index 2e33790..25617b7 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -12,7 +12,7 @@ repositories { } apply plugin: 'com.android.application' -apply plugin: 'com.neenbedankt.android-apt' +//apply plugin: 'com.neenbedankt.android-apt' android { compileSdkVersion 23 @@ -27,12 +27,12 @@ android { } } -apt { - arguments { - androidManifestFile variant.outputs[0].processResources.manifestFile - resourcePackageName android.defaultConfig.applicationId - } -} +//apt { +// arguments { +// androidManifestFile variant.outputs[0].processResources.manifestFile +// resourcePackageName android.defaultConfig.applicationId +// } +//} dependencies { // compile project(':library') @@ -40,5 +40,6 @@ dependencies { compile 'org.androidannotations:androidannotations-api:3.3.2' compile 'com.danikula:videocache:2.7.1' compile 'com.viewpagerindicator:library:2.4.2-SNAPSHOT@aar' - apt 'org.androidannotations:androidannotations:3.3.2' +// apt 'org.androidannotations:androidannotations:3.3.2' + annotationProcessor 'com.google.dagger:dagger-compiler:2.12' } diff --git a/settings.gradle b/settings.gradle index c83799c..4c32e20 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,3 @@ -include ':sample', ':library', ':test' +include ':sample' +include ':library' +include ':test' diff --git a/test/src/main/AndroidManifest.xml b/test/src/main/AndroidManifest.xml index d667938..acc5c6f 100644 --- a/test/src/main/AndroidManifest.xml +++ b/test/src/main/AndroidManifest.xml @@ -3,8 +3,6 @@ xmlns:android="http://schemas.android.com/apk/res/android" package="com.danikula.videocache.test"> - - From 4632e8b649df0c908e4fcd8f5b24a82295336be5 Mon Sep 17 00:00:00 2001 From: gome Date: Tue, 9 Apr 2019 14:03:44 +0800 Subject: [PATCH 2/2] add interface to change http request this will be useful when your url is need sign every time you request --- .../java/com/danikula/videocache/HttpProxyCache.java | 2 +- .../java/com/danikula/videocache/HttpUrlSource.java | 11 +++++------ .../main/java/com/danikula/videocache/SourceInfo.java | 4 ---- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/library/src/main/java/com/danikula/videocache/HttpProxyCache.java b/library/src/main/java/com/danikula/videocache/HttpProxyCache.java index 010354f..37e9816 100644 --- a/library/src/main/java/com/danikula/videocache/HttpProxyCache.java +++ b/library/src/main/java/com/danikula/videocache/HttpProxyCache.java @@ -84,7 +84,7 @@ private void responseWithCache(OutputStream out, long offset) throws ProxyCacheE } private void responseWithoutCache(OutputStream out, long offset) throws ProxyCacheException, IOException { - HttpUrlSource newSourceNoCache = new HttpUrlSource(this.source); + HttpUrlSource newSourceNoCache = this.source.copy(); try { newSourceNoCache.open((int) offset); byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; diff --git a/library/src/main/java/com/danikula/videocache/HttpUrlSource.java b/library/src/main/java/com/danikula/videocache/HttpUrlSource.java index af8da19..e193d98 100644 --- a/library/src/main/java/com/danikula/videocache/HttpUrlSource.java +++ b/library/src/main/java/com/danikula/videocache/HttpUrlSource.java @@ -58,18 +58,17 @@ public HttpUrlSource(String url, SourceInfoStorage sourceInfoStorage, HeaderInje new SourceInfo(url, Integer.MIN_VALUE, ProxyCacheUtils.getSupposablyMime(url)); } - public HttpUrlSource(SourceInfoStorage sourceInfoStorage, HeaderInjector headerInjector) - { - this.sourceInfoStorage = checkNotNull(sourceInfoStorage); - this.headerInjector = checkNotNull(headerInjector); - } - public HttpUrlSource(HttpUrlSource source) { this.sourceInfo = source.sourceInfo; this.sourceInfoStorage = source.sourceInfoStorage; this.headerInjector = source.headerInjector; } + public HttpUrlSource copy() + { + return new HttpUrlSource(this); + } + @Override public synchronized long length() throws ProxyCacheException { if (sourceInfo.length == Integer.MIN_VALUE) { diff --git a/library/src/main/java/com/danikula/videocache/SourceInfo.java b/library/src/main/java/com/danikula/videocache/SourceInfo.java index dd89ab6..8079428 100644 --- a/library/src/main/java/com/danikula/videocache/SourceInfo.java +++ b/library/src/main/java/com/danikula/videocache/SourceInfo.java @@ -10,10 +10,6 @@ public class SourceInfo { public final String url; public final long length; public final String mime; - /** - * the base url - */ - public String baseUrl; public SourceInfo(String url, long length, String mime) { this.url = url;