Skip to content

Commit

Permalink
添加长按webView中图片弹窗,触发下载图片的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
杨充 committed Jul 6, 2020
1 parent 904d591 commit 9e10e86
Show file tree
Hide file tree
Showing 18 changed files with 1,080 additions and 424 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
50 changes: 50 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions WebViewLib/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 27
compileSdkVersion 28

defaultConfig {
minSdkVersion 16
targetSdkVersion 27
versionCode 20
versionName "1.2.0"
targetSdkVersion 28
versionCode 38
versionName "1.3.8"

ndk {
//选择要添加的对应cpu类型的.so库。
Expand All @@ -26,7 +26,7 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:appcompat-v7:28.0.0'
//okhttp库
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
//阿里https+dns开源库
Expand All @@ -38,6 +38,7 @@ dependencies {
//provided files('libs/tbs_sdk_thirdapp_v4.3.0.3_43903_sharewithdownloadwithfile_withoutGame_obfs_20200402_121309.jar')
//implementation 'com.tencent.tbs.tbssdk:sdk:latest.release'
implementation files('libs/tbs_sdk_thirdapp_v4.3.0.3_43903_sharewithdownloadwithfile_withoutGame_obfs_20200402_121309.jar')
implementation 'com.android.support:cardview-v7:28.0.0'
}


Expand All @@ -56,7 +57,7 @@ group = "cn.yc"
//发布到JCenter上的项目名字,必须填写
def libName = "WebViewLib"
// 版本号,下次更新是只需要更改版本号即可
version = "1.3.5"
version = "1.4.0"

//生成源文件
task sourcesJar(type: Jar) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import android.text.TextUtils;

import com.ycbjie.webviewlib.utils.X5WebUtils;

import java.io.IOException;

import okhttp3.CacheControl;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
Expand Down Expand Up @@ -33,11 +37,47 @@ public Response intercept(Chain chain) throws IOException {
//Cache-Control仅指定了max-age所以默认是private。
//缓存时间是31536000,也就是说这个时间段的再次请求这条数据,都会直接获取缓存数据库中的数据,直接使用。
//关于缓存原理,可以重点看一下这个类的源码:CacheInterceptor
//缓存,待测试。在响应头里添加
//Response response = chain.proceed(request);
//Response.Builder responseBuilder = response.newBuilder();
//setCacheBuilder(request,responseBuilder);

return originResponse.newBuilder()
.removeHeader("pragma")
.removeHeader("Cache-Control")
.header("Cache-Control","max-age=3153600000")
.build();
}



private void setCacheBuilder(Request originalRequest, Response.Builder builder) {
//清除头信息,因为服务器如果不支持,会返回一些干扰信息,不清除下面无法生效
builder.removeHeader("Pragma");
if (!X5WebUtils.isConnected(X5WebUtils.getApplication())) {
//无网络下强制使用缓存,无论缓存是否过期,此时该请求实际上不会被发送出去。
originalRequest = originalRequest.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)
.build();
}
if (X5WebUtils.isConnected(X5WebUtils.getApplication())) {
//有网络情况下,根据请求接口的设置,配置缓存。
// 这样在下次请求时,根据缓存决定是否真正发出请求。
String cacheControl = originalRequest.cacheControl().toString();
//当然如果你想在有网络的情况下都直接走网络,那么只需要
//将其超时时间这是为0即可:String cacheControl="Cache-Control:public,max-age=0"
int maxAge = 60 * 60;
// read from cache for 1 minute
builder.addHeader("Cache-Control", cacheControl);
builder.addHeader("Cache-Control", "public, max-age=" + maxAge);
} else {
//无网络
// tolerate 4-weeks stale
int maxStale = 60 * 60 * 24 * 28;
builder.header("Cache-Control", "public,only-if-cached,max-stale=360000");
builder.header("Cache-Control", "public,only-if-cached,max-stale=" + maxStale);
}
}


}
Loading

0 comments on commit 9e10e86

Please sign in to comment.