Skip to content

Commit

Permalink
更新read文档
Browse files Browse the repository at this point in the history
  • Loading branch information
杨充 committed Jun 8, 2020
1 parent b45d41e commit 3010ba0
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 10 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
2 changes: 1 addition & 1 deletion WebViewLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ group = "cn.yc"
//发布到JCenter上的项目名字,必须填写
def libName = "WebViewLib"
// 版本号,下次更新是只需要更改版本号即可
version = "1.3.0"
version = "1.3.2"

//生成源文件
task sourcesJar(type: Jar) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ private String getUrlPath(String url){
* @return
*/
public InputStream getResByUrl(String url){
//获取url的path路径
String uPath = getUrlPath(url);
if (TextUtils.isEmpty(uPath)){
return null;
Expand All @@ -106,6 +107,7 @@ public InputStream getResByUrl(String url){
}
}
if (mAssetResSet!=null){
//直接遍历集合
for (String p: mAssetResSet) {
if (uPath.endsWith(p)){
if (TextUtils.isEmpty(mDir)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.internal.cache.CacheInterceptor;


/**
Expand Down Expand Up @@ -101,7 +102,9 @@ private boolean isEnableAssets() {
}

private void initAssetsLoader() {
WebAssetsLoader.getInstance().init(mContext).setDir(mAssetsDir).isAssetsSuffixMod(mIsSuffixMod);
WebAssetsLoader.getInstance().init(mContext)
.setDir(mAssetsDir)
.isAssetsSuffixMod(mIsSuffixMod);
}

/**
Expand All @@ -114,6 +117,7 @@ private void initHttpClient() {
.cache(cache)
.connectTimeout(mConnectTimeout, TimeUnit.SECONDS)
.readTimeout(mReadTimeout, TimeUnit.SECONDS)
//.addInterceptor(new CacheInterceptor(client.internalCache())
.addNetworkInterceptor(new HttpCacheInterceptor());
if (mTrustAllHostname) {
builder.hostnameVerifier(new HostnameVerifier() {
Expand Down Expand Up @@ -192,7 +196,7 @@ private boolean checkUrl(String url) {
}

String extension = MimeTypeMapUtils.getFileExtensionFromUrl(url);
X5LogUtils.d("WebViewCacheWrapper---interceptRequest--------checkUrl---" +extension);
X5LogUtils.d("WebViewCacheWrapper---interceptRequest检查url--------checkUrl---" +extension);
if (TextUtils.isEmpty(extension)) {
return false;
}
Expand Down Expand Up @@ -314,6 +318,9 @@ private WebResourceResponse interceptRequest(String url, Map<String, String> hea

//先从缓存中获取数据,也就是本地缓存文件中获取数据
if (isEnableAssets()) {
//这种方式读数据效率不高
//阻塞I/O通信模式:调用InputStream.read()方法时是阻塞的,它会一直等到数据到来时才返回
//NIO通信模式:是一种非阻塞I/O,在Java NIO的服务端由一个专门的线程来处理所有I/O事件,并负责分发;线程之间通讯通过wait和notify等方式
InputStream inputStream = WebAssetsLoader.getInstance().getResByUrl(url);
if (inputStream != null) {
X5LogUtils.d("WebViewCacheWrapper---interceptRequest1--" +String.format("from assets: %s", url));
Expand All @@ -323,7 +330,10 @@ private WebResourceResponse interceptRequest(String url, Map<String, String> hea
}
}

//如果本地缓存没有,则创建OkHttp的Request请求,将资源网络请求交给okHttp来处理,并且用它自带的缓存功能
//创建OkHttp的Request请求,将资源网络请求交给okHttp来处理,并且用它自带的缓存功能
//高效缓存
//1.三级缓存,网络缓存(http),磁盘缓存(file),内存缓存(Lru)
//2.使用okio流,数据进行了分块处理(Segment),提供io流超时处理,对数据的读写都进行了封装和交给Buffer管理
try {
Request.Builder reqBuilder = new Request.Builder().url(url);
String extension = MimeTypeMapUtils.getFileExtensionFromUrl(url);
Expand Down Expand Up @@ -392,7 +402,6 @@ public static class Builder {
private long mReadTimeout = 20;
private CacheExtensionConfig mCacheExtensionConfig;
private Context mContext;
private boolean mDebug = true;
private WebCacheType mCacheType = WebCacheType.FORCE;


Expand All @@ -406,7 +415,7 @@ public static class Builder {
private Dns mDns=null;
public Builder(Context context) {
mContext = context;
mCacheFile = new File(context.getCacheDir().toString(), "CacheWebViewCache");
mCacheFile = new File(context.getCacheDir().toString(), "YcWebViewCache");
mCacheExtensionConfig = new CacheExtensionConfig();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,21 @@ public void onCoreInitFinished() {
}
}

/**
* 初始化缓存
* @param application 上下文
*/
public static void initCache(Application application){
//1.创建委托对象
WebViewCacheDelegate webViewCacheDelegate = WebViewCacheDelegate.getInstance();
//2.创建调用处理器对象,实现类
WebViewCacheWrapper.Builder builder = new WebViewCacheWrapper.Builder(application);
//设置缓存路径,默认getCacheDir,名称CacheWebViewCache
builder.setCachePath(new File(application.getCacheDir(),"CacheWebViewCache"))
builder.setCachePath(new File(application.getCacheDir(),"CacheWebView"))
//设置缓存大小,默认100M
.setCacheSize(1024*1024*100)
//设置本地路径
//.setAssetsDir("yc")
//设置http请求链接超时,默认20秒
.setConnectTimeoutSecond(20)
//设置http请求链接读取超时,默认20秒
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
//官方库
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'cn.yc:WebViewLib:1.3.0'
implementation 'cn.yc:WebViewLib:1.3.2'
implementation 'cn.yc:YCSlideLib:1.1.2'
//okhttp库
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/ycbjie/ycwebview/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void onCreate() {
super.onCreate();
X5WebUtils.init(this);
X5LogUtils.setIsLog(true);

//X5WebUtils.initCache(this);
WebViewCacheDelegate.getInstance().init(new WebViewCacheWrapper.Builder(this));
}
}
28 changes: 28 additions & 0 deletions app/src/main/java/com/ycbjie/ycwebview/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.ycbjie.ycwebview;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
Expand Down Expand Up @@ -41,6 +46,9 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.tv_14_2).setOnClickListener(this);
findViewById(R.id.tv_15).setOnClickListener(this);
findViewById(R.id.tv_16).setOnClickListener(this);

checkReadPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE, 100);
checkReadPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE, 101);
}

@Override
Expand Down Expand Up @@ -140,4 +148,24 @@ public void openLink(Context context, String content) {
}
}


/**
* 判断是否有某项权限
* @param string_permission 权限
* @param request_code 请求码
* @return
*/
public boolean checkReadPermission(Context context, String string_permission, int request_code) {
boolean flag = false;
int permission = ContextCompat.checkSelfPermission(context, string_permission);
if (permission == PackageManager.PERMISSION_GRANTED) {
//已有权限
flag = true;
} else {
//申请权限
ActivityCompat.requestPermissions((Activity) context, new String[]{string_permission}, request_code);
}
return flag;
}

}
2 changes: 1 addition & 1 deletion read/HowToUse.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
implementation 'cn.yc:WebViewLib:1.2.4'
//添加阿里https+dns解析版本
implementation 'cn.yc:WebViewLib:1.3.1'
implementation 'cn.yc:WebViewLib:1.3.2'
```


Expand Down

0 comments on commit 3010ba0

Please sign in to comment.