Skip to content

Commit

Permalink
opt code
Browse files Browse the repository at this point in the history
  • Loading branch information
yale8848 committed Jul 8, 2018
1 parent df3c62f commit e594189
Show file tree
Hide file tree
Showing 36 changed files with 758 additions and 706 deletions.
10 changes: 5 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ren.yale.android.cachewebview">
package="ren.yale.android.cachewebview">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<application
android:name=".App"
android:allowBackup="true"
Expand All @@ -15,13 +16,12 @@
android:theme="@android:style/Theme.Black.NoTitleBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity">
</activity>
<activity android:name=".Main2Activity"></activity>
</application>

</manifest>
5 changes: 3 additions & 2 deletions app/src/main/java/ren/yale/android/cachewebview/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@

public class App extends Application {
private static final String CACHE_NAME = "cache_path";

@Override
public void onCreate() {
super.onCreate();

File cacheFile = new File(this.getCacheDir(),CACHE_NAME);
CacheWebView.getCacheConfig().init(this,cacheFile.getAbsolutePath(),1024*1024*100,1024*1024*10)
File cacheFile = new File(this.getCacheDir(), CACHE_NAME);
CacheWebView.getCacheConfig().init(this, cacheFile.getAbsolutePath(), 1024 * 1024 * 100, 1024 * 1024 * 10)
.enableDebug(true);


Expand Down
23 changes: 12 additions & 11 deletions app/src/main/java/ren/yale/android/cachewebview/Main2Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

public class Main2Activity extends Activity {

public static final String KEY_URL = "";
private CacheWebView mWebView;
public static final String KEY_URL="";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -25,12 +26,12 @@ protected void onCreate(Bundle savedInstanceState) {

initSettings();

String html="<html><div class=\"line\"><img src=\"/bbs/upload/1000/2018/03/21/32873_1025080screenshot_2018-03-21-10-22-54-916_com.unicom.woshipin.png\" width=\"400px\" alt=\"screenshot_2018-03-21-10-22-54-916_com.unicom.woshipin\"></div></html>";
mWebView.loadDataWithBaseURL("http://yaohw.com",html,"text/html","utf-8","");
String html = "<html><div class=\"line\"><img src=\"/bbs/upload/1000/2018/03/21/32873_1025080screenshot_2018-03-21-10-22-54-916_com.unicom.woshipin.png\" width=\"400px\" alt=\"screenshot_2018-03-21-10-22-54-916_com.unicom.woshipin\"></div></html>";
mWebView.loadDataWithBaseURL("http://yaohw.com", html, "text/html", "utf-8", "");
}


private void initSettings(){
private void initSettings() {
WebSettings webSettings = mWebView.getSettings();

webSettings.setJavaScriptEnabled(true);
Expand All @@ -51,11 +52,11 @@ private void initSettings(){
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptThirdPartyCookies(mWebView,true);
cookieManager.setAcceptThirdPartyCookies(mWebView, true);
}
if (NetworkUtils.isConnected(mWebView.getContext()) ){
if (NetworkUtils.isConnected(mWebView.getContext())) {
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
} else {
webSettings.setCacheMode(
Expand All @@ -74,17 +75,17 @@ private void initSettings(){
protected void onDestroy() {
super.onDestroy();

mWebView.clearCache();
mWebView.clearCache();

}

private void setCachePath(){
private void setCachePath() {

File cacheFile = new File(mWebView.getContext().getCacheDir(),"appcache_name");
File cacheFile = new File(mWebView.getContext().getCacheDir(), "appcache_name");
String path = cacheFile.getAbsolutePath();

File file = new File(path);
if (!file.exists()){
if (!file.exists()) {
file.mkdirs();
}

Expand Down
49 changes: 26 additions & 23 deletions app/src/main/java/ren/yale/android/cachewebview/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import ren.yale.android.cachewebviewlib.CacheInterceptor;
import ren.yale.android.cachewebviewlib.CacheStatus;
import ren.yale.android.cachewebviewlib.CacheWebView;
import ren.yale.android.cachewebviewlib.config.CacheExtensionConfig;
import ren.yale.android.cachewebviewlib.WebViewCache;
import ren.yale.android.cachewebviewlib.config.CacheExtensionConfig;


public class MainActivity extends Activity {


private String URL ="";
private String URL = "";
private CacheWebView webview;
private long mStart = 0;

Expand All @@ -56,24 +56,25 @@ public void onNothingSelected(AdapterView<?> parent) {
});
webview = testWebView.getDXHWebView();
webview.setCacheStrategy(WebViewCache.CacheStrategy.FORCE);
webview.setWebViewClient(new WebViewClient(){
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
mStart = System.currentTimeMillis();
super.onPageStarted(view, url, favicon);
}

@Override
public void onPageFinished(WebView view, String url) {
Log.d("CacheWebView",(System.currentTimeMillis()-mStart)+" "+url);
Log.d("CacheWebView", (System.currentTimeMillis() - mStart) + " " + url);
super.onPageFinished(view, url);
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

if (url.startsWith("http")){
if (url.startsWith("http")) {
CacheWebView v = (CacheWebView) view;
view.loadUrl(url,getHeaderMap(url));
view.loadUrl(url, getHeaderMap(url));
}

return true;
Expand All @@ -85,7 +86,7 @@ public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError e
}
});
CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox);
webview.setEnableCache(checkBox.isChecked());
webview.setEnableCache(checkBox.isChecked());
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Expand All @@ -111,49 +112,51 @@ public boolean canCache(String url) {

CacheWebView.getCacheConfig().setEncodeBufferSize(1024);
}

@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);

}

private HashMap getHeaderMap(String url){
HashMap<String,String> map = new HashMap<>();
map.put("key","value");
private HashMap getHeaderMap(String url) {
HashMap<String, String> map = new HashMap<>();
map.put("key", "value");
return map;

}
public void onClick(View v){
switch (v.getId()){

public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_load:
// webview.stopLoading();
// webview.stopLoading();
webview.loadUrl(URL);
break;
case R.id.btn_preload:
CacheWebView.servicePreload(MainActivity.this,URL,null);
CacheWebView.servicePreload(MainActivity.this, URL, null);
break;
case R.id.btn_clearcache:
clearCache();

break;
case R.id.btn_get_file:
CacheStatus cacheStatus = webview.getWebViewCache().getCacheFile("https://m.baidu.com/static/search/baiduapp_icon.png");
if (cacheStatus.isExist()){
File file = cacheStatus.getCacheFile();
if (cacheStatus.isExist()) {
File file = cacheStatus.getCacheFile();
String extension = cacheStatus.getExtension();
}

break;
case R.id.btn_start2:{
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra(Main2Activity.KEY_URL,URL);
case R.id.btn_start2: {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra(Main2Activity.KEY_URL, URL);
startActivity(intent);
break;
}
case R.id.btn_stop:{
case R.id.btn_stop: {

webview.stopLoading();
// webview.pauseTimers();
// webview.pauseTimers();

break;
}
Expand All @@ -175,7 +178,7 @@ protected void onResume() {
webview.resumeTimers();
}

private void clearCache(){
private void clearCache() {
webview.clearCache();
}

Expand All @@ -187,7 +190,7 @@ protected void onDestroy() {

@Override
public void onBackPressed() {
if (webview.canGoBack()){
if (webview.canGoBack()) {
webview.goBack();
return;
}
Expand Down
16 changes: 9 additions & 7 deletions app/src/main/java/ren/yale/android/cachewebview/TestWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
public class TestWebView extends RelativeLayout {
private CacheWebView mDXHWebView = null;
private Context mContext;

public TestWebView(Context context) {
super(context);
init(context);
Expand All @@ -35,7 +36,8 @@ public TestWebView(Context context, AttributeSet attrs, int defStyleAttr) {
init(context);

}
public CacheWebView getDXHWebView(){

public CacheWebView getDXHWebView() {
return mDXHWebView;
}

Expand All @@ -44,11 +46,12 @@ protected void onAttachedToWindow() {
super.onAttachedToWindow();
}

private void addWebView(Context context){
private void addWebView(Context context) {
mDXHWebView = new CacheWebView(context);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
this.addView(mDXHWebView,rl);
this.addView(mDXHWebView, rl);
}

private void initSettings() {
WebSettings webSettings = mDXHWebView.getSettings();

Expand All @@ -61,24 +64,22 @@ private void initSettings() {
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);

if (NetworkUtils.isConnected(mContext) ){
if (NetworkUtils.isConnected(mContext)) {
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
} else {
webSettings.setCacheMode(
WebSettings.LOAD_CACHE_ELSE_NETWORK);



}
webSettings.setDatabaseEnabled(true);
webSettings.setAppCacheMaxSize(1024 * 1024 * 8);
webSettings.setAppCacheEnabled(true);



//File fDatabase = new File(mContext.getCacheDir().getAbsolutePath(),"webview_db");
//webSettings.setDatabasePath(fDatabase.getAbsolutePath());
File fAppCache = new File(mContext.getCacheDir().getAbsolutePath(),"webview_cache");
File fAppCache = new File(mContext.getCacheDir().getAbsolutePath(), "webview_cache");
webSettings.setAppCachePath(fAppCache.getAbsolutePath());

webSettings.setBuiltInZoomControls(true);// api-3
Expand All @@ -98,6 +99,7 @@ private void initSettings() {
WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
}
}

private void init(final Context context) {

mContext = context;
Expand Down
Loading

0 comments on commit e594189

Please sign in to comment.