Skip to content

Commit 96e2d95

Browse files
committed
添加一个浏览器控制类
1 parent de69cc7 commit 96e2d95

8 files changed

Lines changed: 266 additions & 13 deletions

File tree

build.gradle

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
buildscript {
44
repositories {
55
jcenter()
6+
mavenCentral()
67
}
78
dependencies {
89
classpath 'com.android.tools.build:gradle:2.2.3'
10+
// classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
11+
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
912

1013
// NOTE: Do not place your application dependencies here; they belong
1114
// in the individual module build.gradle files
@@ -15,9 +18,23 @@ buildscript {
1518
allprojects {
1619
repositories {
1720
jcenter()
21+
mavenCentral()
1822
}
1923
}
2024

2125
task clean(type: Delete) {
2226
delete rootProject.buildDir
2327
}
28+
29+
ext {
30+
android = [
31+
compileSdkVersion : 25,
32+
buildToolsVersion : "25.0.2",
33+
minSdkVersion : 15,
34+
targetSdkVersion : 25,
35+
versionCode : 100,
36+
versionName : "1.0.0",
37+
androidSupportSdkVersion: "25.2.0",
38+
espressoSdkVersion : "2.2.2"
39+
]
40+
}

listrelated/build.gradle

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
apply plugin: 'com.android.library'
2+
apply plugin: 'com.github.dcendents.android-maven'
3+
group = 'com.github.qyai'
24

35
android {
4-
compileSdkVersion 25
5-
buildToolsVersion "25.0.2"
6+
compileSdkVersion rootProject.ext.android.compileSdkVersion
7+
buildToolsVersion rootProject.ext.android.buildToolsVersion
68

79
defaultConfig {
8-
minSdkVersion 15
9-
targetSdkVersion 25
10+
minSdkVersion rootProject.ext.android.minSdkVersion
11+
targetSdkVersion rootProject.ext.android.targetSdkVersion
1012
versionCode 100
1113
versionName "1.0.0"
1214
}
15+
16+
compileOptions {
17+
sourceCompatibility JavaVersion.VERSION_1_7
18+
targetCompatibility JavaVersion.VERSION_1_7
19+
}
20+
1321
buildTypes {
1422
release {
1523
minifyEnabled false
1624
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
1725
}
1826
}
27+
1928
}
2029

2130
dependencies {
@@ -28,15 +37,13 @@ dependencies {
2837
compile 'com.android.support:support-compat:25.1.0@aar'
2938
compile 'com.android.support:support-v13:25.1.0@aar'
3039

31-
3240
// v7
3341
compile 'com.android.support:appcompat-v7:25.1.0@aar'
3442
compile 'com.android.support:recyclerview-v7:25.1.0@aar'
3543
compile 'com.android.support:support-annotations:25.1.0@jar'
3644
compile 'com.android.support:support-vector-drawable:25.1.0@aar'
3745
compile 'com.android.support:animated-vector-drawable:25.1.0@aar'
3846

39-
4047
// logger
4148
// compile 'com.orhanobut:logger:1.15@aar'
4249
compile 'com.github.zhaokaiqiang.klog:library:1.6.0'
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.ai.listrelated.ui.fragment;
22

3+
import android.support.v4.app.Fragment;
4+
35
/**
46
* <b>Project:</b> ListRelated <br>
57
* <b>Create Date:</b> 2017/1/4 <br>
68
* <b>Author:</b> qingyong <br>
79
* <b>Address:</b> qingyongai@gmail.com <br>
810
* <b>Description:</b> BaseFragment <br>
911
*/
10-
public class BaseFragment extends LifecycleFragment {
12+
public class BaseFragment extends Fragment {
1113
}

listrelated/src/main/java/com/ai/listrelated/ui/fragment/BaseLazyFragment.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public abstract class BaseLazyFragment extends BaseFragment
2929

3030
@Override
3131
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
32-
logLifeCycle("onCreateView");
32+
// logLifeCycle("onCreateView");
3333
mView = onInflaterRootView(inflater, container, savedInstanceState);
3434
return mView;
3535
}
@@ -49,12 +49,12 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
4949

5050
@Override
5151
public void onFragmentVisible() {
52-
logLifeCycle("onFragmentVisible");
52+
// logLifeCycle("onFragmentVisible");
5353
}
5454

5555
@Override
5656
public void onFragmentInvisible() {
57-
logLifeCycle("onFragmentInvisible");
57+
// logLifeCycle("onFragmentInvisible");
5858
}
5959

6060
@Override

listrelated/src/main/java/com/ai/listrelated/ui/fragment/LifecycleFragment.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import android.view.View;
88
import android.view.ViewGroup;
99

10-
import com.socks.library.KLog;
11-
1210
/**
1311
* <b>Project:</b> ListRelated <br>
1412
* <b>Create Date:</b> 2017/1/10 <br>
@@ -20,7 +18,7 @@ public abstract class LifecycleFragment extends Fragment {
2018

2119
protected void logLifeCycle(String msg) {
2220
// Logger.t("Lifecycle").i(msg);
23-
KLog.i("Lifecycle", msg);
21+
// KLog.i("Lifecycle", msg);
2422
}
2523

2624
@Override
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.ai.listrelated.web;
2+
3+
import android.webkit.JsPromptResult;
4+
import android.webkit.JsResult;
5+
import android.webkit.WebChromeClient;
6+
import android.webkit.WebView;
7+
8+
public class MWebChromeClient extends WebChromeClient {
9+
10+
@Override
11+
public void onProgressChanged(WebView view, int newProgress) {
12+
super.onProgressChanged(view, newProgress);
13+
}
14+
15+
@Override
16+
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
17+
return super.onJsAlert(view, url, message, result);
18+
}
19+
20+
@Override
21+
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
22+
return super.onJsPrompt(view, url, message, defaultValue, result);
23+
}
24+
25+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.ai.listrelated.web;
2+
3+
import android.annotation.TargetApi;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.content.pm.PackageManager;
7+
import android.content.pm.ResolveInfo;
8+
import android.net.Uri;
9+
import android.webkit.WebResourceRequest;
10+
import android.webkit.WebView;
11+
import android.webkit.WebViewClient;
12+
13+
import java.util.List;
14+
15+
public class MWebViewClient extends WebViewClient {
16+
17+
public static final String TAG = MWebViewClient.class.getSimpleName();
18+
19+
private Context mContext;
20+
21+
public MWebViewClient(Context context) {
22+
mContext = context;
23+
}
24+
25+
@Override
26+
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
27+
return handle(webView, url);
28+
}
29+
30+
@TargetApi(21)
31+
@Override
32+
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request) {
33+
return handle(webView, request.getUrl().toString());
34+
}
35+
36+
public boolean handle(WebView webView, String url) {
37+
// KLog.i(TAG, "shouldOverrideUrlLoading 地址 -------------> " + url);
38+
// https://play.google.com/store/apps/details?id=org.getlantern.lantern&hl=zh-CN
39+
if (url.endsWith(".apk")) {
40+
if (!goSystemBrowser(mContext, url)) {
41+
return false;
42+
}
43+
} else if (!url.startsWith("http")) {
44+
// 不是http开头的抛出让系统处理先
45+
Intent intent = new Intent(Intent.ACTION_VIEW);
46+
intent.setData(Uri.parse(url));
47+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
48+
try {
49+
mContext.startActivity(intent);
50+
} catch (Exception e) {
51+
return false;
52+
}
53+
} else {
54+
// 返回false表示继续让此浏览器打开页面
55+
return false;
56+
}
57+
return true;
58+
}
59+
60+
/**
61+
* 用系统的浏览器打开
62+
*
63+
* @param context 上下文
64+
* @param url 地址
65+
* @return 返回打开是否成功
66+
*/
67+
public static boolean goSystemBrowser(Context context, String url) {
68+
Intent intent = new Intent(Intent.ACTION_VIEW);
69+
intent.setData(Uri.parse(url));
70+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
71+
Intent i = new Intent(intent);
72+
try {
73+
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
74+
i.setPackage("com.android.browser");
75+
context.startActivity(i);
76+
return true;
77+
} catch (Exception ignored) {
78+
}
79+
try {
80+
List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(
81+
intent, PackageManager.MATCH_DEFAULT_ONLY);
82+
if (list == null || list.size() <= 0) {
83+
return false;
84+
}
85+
ResolveInfo info = list.get(0);
86+
intent.setPackage(info.activityInfo.packageName);
87+
context.startActivity(intent);
88+
return true;
89+
} catch (Exception e) {
90+
return false;
91+
}
92+
}
93+
94+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.ai.listrelated.web;
2+
3+
import android.content.Context;
4+
import android.os.Build;
5+
import android.webkit.WebSettings;
6+
import android.webkit.WebView;
7+
8+
/**
9+
* <b>Project:</b> ListRelated <br>
10+
* <b>Create Date:</b> 2017/1/19 <br>
11+
* <b>Author:</b> qy <br>
12+
* <b>Address:</b> qingyongai@gmail.com <br>
13+
* <b>Description:</b> 通用的WebView的一些设置,方便直接使用以及扩展 <br>
14+
*/
15+
public class WebViewController {
16+
17+
protected static final String TAG = WebViewController.class.getSimpleName();
18+
19+
protected Context mContext;
20+
protected WebView mWebView;
21+
protected WebSettings mSettings;
22+
protected MWebViewClient mWebViewClient;
23+
protected MWebChromeClient mWebChromeClient;
24+
25+
public WebViewController(WebView webview) {
26+
mWebView = webview;
27+
mContext = mWebView.getContext();
28+
mSettings = mWebView.getSettings();
29+
}
30+
31+
/**
32+
* 初始化WebView
33+
*/
34+
public void setupWebView() {
35+
mSettings.setSaveFormData(false);
36+
mSettings.setAllowFileAccess(true);
37+
mSettings.setDatabaseEnabled(true);
38+
mSettings.setJavaScriptEnabled(true);
39+
mSettings.setUseWideViewPort(true);
40+
mSettings.setAppCacheEnabled(true);
41+
mSettings.setDomStorageEnabled(true);
42+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
43+
mSettings.setDisplayZoomControls(false);
44+
}
45+
mSettings.setLoadWithOverviewMode(true);
46+
mSettings.setPluginState(WebSettings.PluginState.ON);
47+
// WebView自适应屏幕大小
48+
mSettings.setDefaultTextEncodingName("UTF-8");
49+
mSettings.setLoadsImagesAutomatically(true);
50+
// 设置可以支持缩放
51+
mSettings.setSupportZoom(true);
52+
// 设置默认缩放方式尺寸是far
53+
mSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
54+
// 设置出现缩放工具
55+
mSettings.setBuiltInZoomControls(true);
56+
// 设置WebView的缓存模式
57+
mSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
58+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
59+
// 不然5.0以后http和https混合的页面会加载不出来
60+
mSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
61+
}
62+
}
63+
64+
/**
65+
* 设置ua,需要注意的是我已经把原来的ua追加了,调用这个方法,只需要添加你想要添加的字符串即可
66+
*
67+
* @param userAgent ua
68+
*/
69+
public void setUserAgent(String userAgent) {
70+
String origin = mSettings.getUserAgentString();
71+
mSettings.setUserAgentString(origin + " " + userAgent);
72+
// KLog.i(TAG, "UA------------------>" + mSettings.getUserAgentString());
73+
}
74+
75+
/**
76+
* 设置webViewClient
77+
*
78+
* @param webViewClient webViewClient必须继承{@link MWebViewClient}
79+
*/
80+
public void setWebViewClient(MWebViewClient webViewClient) {
81+
if (webViewClient != null) {
82+
mWebViewClient = webViewClient;
83+
mWebView.setWebViewClient(webViewClient);
84+
}
85+
}
86+
87+
/**
88+
* 设置webChromeClient
89+
*
90+
* @param webChromeClient webChromeClient必须继承{@link MWebChromeClient}
91+
*/
92+
public void setWebChromeClient(MWebChromeClient webChromeClient) {
93+
if (webChromeClient != null) {
94+
mWebChromeClient = webChromeClient;
95+
mWebView.setWebChromeClient(webChromeClient);
96+
}
97+
}
98+
99+
/**
100+
* 添加js调用java的接口
101+
*
102+
* @param object
103+
* @param name
104+
*/
105+
@SuppressWarnings("All")
106+
public void addJavascriptInterface(Object object, String name) {
107+
mWebView.addJavascriptInterface(object, name);
108+
}
109+
110+
}

0 commit comments

Comments
 (0)