├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── cc │ │ └── solart │ │ └── openweb │ │ └── simple │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── cc │ │ │ └── solart │ │ │ └── openweb │ │ │ └── simple │ │ │ ├── BaseActivity.java │ │ │ ├── BaseWebActivity.java │ │ │ ├── MyOpenWebFragment.java │ │ │ └── OpenWebActivity.java │ └── res │ │ ├── layout │ │ ├── openweb_activity.xml │ │ └── openweb_fragment.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── cc │ └── solart │ └── openweb │ └── simple │ └── ExampleUnitTest.java ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── openweb ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── cc │ │ └── solart │ │ └── openweb │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── cc │ │ └── solart │ │ └── openweb │ │ ├── OnRefreshStatusListener.java │ │ ├── OpenWebEvent.java │ │ ├── OpenWebFragment.java │ │ ├── base │ │ ├── BaseWebChromeClient.java │ │ ├── BaseWebEvent.java │ │ ├── BaseWebFragment.java │ │ ├── BaseWebViewClient.java │ │ └── WebUrl.java │ │ ├── utils │ │ ├── Logger.java │ │ ├── NetworkUtil.java │ │ ├── ObjEnsureUtil.java │ │ └── WebSettingsUtil.java │ │ └── widget │ │ ├── OpenWebLayout.java │ │ └── OpenWebView.java │ └── res │ ├── drawable │ └── bg_progress_horizontal.xml │ ├── layout │ └── open_web_layout.xml │ └── values │ ├── attrs.xml │ ├── dimens.xml │ └── strings.xml ├── preview └── openweb.gif └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/ 5 | .DS_Store 6 | /build 7 | /captures 8 | 9 | gradle.properties 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | OpenWeb project 2 | =============== 3 | 4 | Help you build a web framework quickly. 5 | It can manages the BackForward stack automatically. 6 | 7 | 8 | 9 | Gradle 10 | ------ 11 | ``` 12 | dependencies { 13 | ... 14 | compile 'cc.solart:openweb:1.1.1' 15 | } 16 | ``` 17 | 18 | Usage 19 | ----- 20 | ```java 21 | public class MyOpenWebFragment extends OpenWebFragment { 22 | 23 | private SwipeRefreshLayout mSwipeRefreshLayout; 24 | 25 | /** 26 | * if you need pull to refresh, you can coding like this, otherwise return null. 27 | * Careful use of pull down refresh, it may lead to a sliding conflict with the web page.(fixed in 1.1.1) 28 | * You can also disable and open the drop-down refresh to circumvent this problem by JavaScript 29 | * @return 30 | */ 31 | @Override 32 | protected OnRefreshStatusListener getOnRefreshStatusListener() { 33 | return new OnRefreshStatusListener() { 34 | 35 | @Override 36 | public boolean isRefreshing() { 37 | return mSwipeRefreshLayout.isRefreshing(); 38 | } 39 | 40 | @Override 41 | public void refreshComplete() { 42 | mSwipeRefreshLayout.setRefreshing(false); 43 | } 44 | }; 45 | } 46 | 47 | 48 | @Override 49 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 50 | View view = super.onCreateView(inflater, container, savedInstanceState); 51 | mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh_layout); 52 | OpenWebLayout webLayout = (OpenWebLayout) view.findViewById(R.id.webview); 53 | webLayout.setRefreshView(mSwipeRefreshLayout); //add in 1.1.1 54 | mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 55 | @Override 56 | public void onRefresh() { 57 | mWebView.reload(); 58 | } 59 | }); 60 | return view; 61 | } 62 | 63 | @Override 64 | protected int loadLayoutRes() { 65 | return R.layout.openweb_fragment; 66 | } 67 | 68 | @Override 69 | protected int getWebViewId() { 70 | return R.id.webview; 71 | } 72 | } 73 | ``` 74 | 75 | ```xml 76 | 77 | 81 | 82 | 86 | 87 | 93 | 94 | 95 | 96 | 97 | ``` 98 | 99 | Changelog 100 | --------- 101 | * **1.0.0** 102 | * Initial release 103 | * **1.1.0** 104 | * Add OpenWebLayout widget, instead of WebView 105 | * **1.1.1** 106 | * Fix pull to refresh conflict bug 107 | 108 | License 109 | ------- 110 | 111 | Copyright 2015 - 2016 solartisan/imilk 112 | 113 | Licensed under the Apache License, Version 2.0 (the "License"); 114 | you may not use this file except in compliance with the License. 115 | You may obtain a copy of the License at 116 | 117 | http://www.apache.org/licenses/LICENSE-2.0 118 | 119 | Unless required by applicable law or agreed to in writing, software 120 | distributed under the License is distributed on an "AS IS" BASIS, 121 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 122 | See the License for the specific language governing permissions and 123 | limitations under the License. -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.2" 6 | 7 | defaultConfig { 8 | applicationId "cc.solart.openweb.simple" 9 | minSdkVersion 14 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.1.1' 26 | compile project(':openweb') 27 | } 28 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/imilk/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/cc/solart/openweb/simple/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb.simple; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 19 | 20 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /app/src/main/java/cc/solart/openweb/simple/BaseActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * ----------------------------------------------------------------- 3 | * Copyright (C) 2012-2015, by www.dianhua.cn, Beijing, All rights reserved. 4 | * ----------------------------------------------------------------- 5 | * Author: peng.wang 6 | * Create: 2015-7-10 7 | * 8 | * Changes (from 2015-7-10) 9 | * ----------------------------------------------------------------- 10 | * 2015-7-10 : 创建 BaseActivity.java (作者:peng.wang); 11 | * ----------------------------------------------------------------- 12 | */ 13 | package cc.solart.openweb.simple; 14 | 15 | import android.app.ActionBar; 16 | import android.app.Activity; 17 | import android.app.Fragment; 18 | import android.os.Bundle; 19 | import android.os.Handler; 20 | import android.os.Message; 21 | import android.support.v7.app.AppCompatActivity; 22 | import android.view.MenuItem; 23 | 24 | 25 | 26 | import java.lang.ref.WeakReference; 27 | 28 | import cc.solart.openweb.utils.Logger; 29 | 30 | /** 31 | * Activity基类 32 | * Created by imilk on 15/7/13. 33 | */ 34 | public abstract class BaseActivity extends AppCompatActivity{ 35 | private static final String TAG = "BaseActivity"; 36 | 37 | protected ActionBar mActionBar; 38 | 39 | protected Handler mHandler = new SafeHandler(this){ 40 | @Override 41 | public void handleMessage(Message msg) { 42 | super.handleMessage(msg); 43 | BaseActivity.this.handleMessage(msg); 44 | } 45 | }; 46 | 47 | /** 48 | * 防止内部Handler类引起内存泄露 49 | * 50 | */ 51 | static class SafeHandler extends Handler{ 52 | private final WeakReference mActivity; 53 | 54 | public SafeHandler(Activity activity) { 55 | mActivity = new WeakReference(activity); 56 | } 57 | 58 | @Override 59 | public void handleMessage(Message msg) { 60 | if(mActivity.get() == null) { 61 | return; 62 | } 63 | } 64 | } 65 | 66 | 67 | @Override 68 | protected void onCreate(Bundle savedInstanceState) { 69 | super.onCreate(savedInstanceState); 70 | Logger.d(TAG, "onCreate " + this.toString()); 71 | initActionBar(); 72 | } 73 | 74 | @Override 75 | public void onAttachFragment(Fragment fragment) { 76 | super.onAttachFragment(fragment); 77 | } 78 | 79 | 80 | /** 81 | * 初始化ActionBar called onCreate(Bundle); 82 | */ 83 | protected void initActionBar(){ 84 | this.mActionBar = getActionBar(); 85 | if (mActionBar != null) { 86 | mActionBar.setDisplayHomeAsUpEnabled(true); 87 | mActionBar.setDisplayShowTitleEnabled(true); 88 | mActionBar.setDisplayShowHomeEnabled(false); 89 | } 90 | } 91 | 92 | 93 | protected final void sendCallbackEmptyMessage(int what){ 94 | sendCallbackMessage(what,null); 95 | } 96 | 97 | /** 98 | * Handler消息发送 99 | * @param what 100 | * @param obj 101 | */ 102 | protected final void sendCallbackMessage(int what, Object obj) { 103 | sendCallbackMessageDelay(what, obj, 0); 104 | } 105 | 106 | 107 | /** 108 | *Handler消息延时发送 109 | * @param what 110 | * @param obj 111 | * @param delay 112 | */ 113 | protected final void sendCallbackMessageDelay(int what, Object obj,long delay) { 114 | if (mHandler != null) { 115 | Message msg = mHandler.obtainMessage(); 116 | msg.what = what; 117 | msg.obj = obj; 118 | mHandler.sendMessageDelayed(msg,delay); 119 | } 120 | } 121 | 122 | /** 123 | * Handler消息处理 124 | * @param msg 125 | */ 126 | protected abstract void handleMessage(Message msg); 127 | 128 | /** 129 | * ActionBar 返回键监听处理 130 | */ 131 | protected abstract void onMenuHome(); 132 | 133 | 134 | @Override 135 | public boolean onOptionsItemSelected(MenuItem item) { 136 | switch (item.getItemId()) { 137 | case android.R.id.home: 138 | onMenuHome(); 139 | break; 140 | 141 | default: 142 | break; 143 | } 144 | return super.onOptionsItemSelected(item); 145 | } 146 | 147 | } 148 | -------------------------------------------------------------------------------- /app/src/main/java/cc/solart/openweb/simple/BaseWebActivity.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb.simple; 2 | 3 | import android.os.Bundle; 4 | import android.text.TextUtils; 5 | 6 | import cc.solart.openweb.base.BaseWebFragment; 7 | 8 | /** 9 | * Created by imilk on 15/6/9. 10 | */ 11 | public abstract class BaseWebActivity extends BaseActivity { 12 | 13 | public static final String LINK = "link"; 14 | public static final String TITLE = "title"; 15 | private static final String TAG = "BaseWebActivity"; 16 | private String mTitle; 17 | private String mUrl; 18 | protected BaseWebFragment mWebFragment; 19 | 20 | private void initTitleBar() { 21 | mActionBar.setHomeButtonEnabled(true); 22 | if (!TextUtils.isEmpty(mTitle)) 23 | setTitle(mTitle); 24 | } 25 | 26 | /** 27 | * 定制actionbar 28 | */ 29 | protected abstract void customizeActionBar(); 30 | 31 | /** 32 | * 33 | * @return ContentViewID 34 | */ 35 | protected abstract int getContentViewResId(); 36 | 37 | /** 38 | * 39 | * @return WebFragmentID 40 | */ 41 | protected abstract int getWebFragmentResId(); 42 | 43 | 44 | @Override 45 | protected void onCreate(Bundle savedInstanceState) { 46 | super.onCreate(savedInstanceState); 47 | setContentView(getContentViewResId()); 48 | this.mWebFragment = ((BaseWebFragment) getFragmentManager().findFragmentById(getWebFragmentResId())); 49 | 50 | this.mWebFragment.loadUrl("http://news.163.com/"); 51 | } 52 | 53 | 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/java/cc/solart/openweb/simple/MyOpenWebFragment.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb.simple; 2 | 3 | import android.os.Bundle; 4 | import android.support.v4.widget.SwipeRefreshLayout; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | 9 | import cc.solart.openweb.OnRefreshStatusListener; 10 | import cc.solart.openweb.OpenWebFragment; 11 | import cc.solart.openweb.simple.R; 12 | import cc.solart.openweb.widget.OpenWebLayout; 13 | 14 | /** 15 | * ------------------------------------------------------------------------- 16 | * Author: imilk 17 | * Create: 19:32 18 | * ------------------------------------------------------------------------- 19 | * Describe: 20 | * ------------------------------------------------------------------------- 21 | * Changes: 22 | * ------------------------------------------------------------------------- 23 | * 19 : Create by imilk 24 | * ------------------------------------------------------------------------- 25 | */ 26 | public class MyOpenWebFragment extends OpenWebFragment { 27 | 28 | private SwipeRefreshLayout mSwipeRefreshLayout; 29 | 30 | /** 31 | * if you need pull to refresh, you can coding like this, otherwise return null. 32 | * You can also disable and enable the pull to refresh to circumvent this problem by JavaScript 33 | * override {@link #enablePullToRefresh()} and {@link #disablePullToRefresh()} 34 | * @return 35 | */ 36 | @Override 37 | protected OnRefreshStatusListener getOnRefreshStatusListener() { 38 | return new OnRefreshStatusListener() { 39 | 40 | @Override 41 | public boolean isRefreshing() { 42 | return mSwipeRefreshLayout.isRefreshing(); 43 | } 44 | 45 | @Override 46 | public void refreshComplete() { 47 | mSwipeRefreshLayout.setRefreshing(false); 48 | } 49 | }; 50 | } 51 | 52 | 53 | @Override 54 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 55 | View view = super.onCreateView(inflater, container, savedInstanceState); 56 | mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh_layout); 57 | OpenWebLayout webLayout = (OpenWebLayout) view.findViewById(R.id.webview); 58 | webLayout.setRefreshView(mSwipeRefreshLayout); 59 | mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 60 | @Override 61 | public void onRefresh() { 62 | mWebView.reload(); 63 | } 64 | }); 65 | return view; 66 | } 67 | 68 | @Override 69 | protected int loadLayoutRes() { 70 | return R.layout.openweb_fragment; 71 | } 72 | 73 | @Override 74 | protected int getWebViewId() { 75 | return R.id.webview; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /app/src/main/java/cc/solart/openweb/simple/OpenWebActivity.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb.simple; 2 | 3 | import android.os.Bundle; 4 | import android.os.Message; 5 | import android.view.Menu; 6 | 7 | import cc.solart.openweb.simple.R; 8 | 9 | 10 | /** 11 | * Created by imilk on 15/6/9. 12 | */ 13 | public class OpenWebActivity extends BaseWebActivity { 14 | 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | } 20 | 21 | 22 | @Override 23 | public boolean onCreateOptionsMenu(Menu menu) { 24 | if (mWebFragment != null) { 25 | ((MyOpenWebFragment) this.mWebFragment).allowCreateWebMenu(); 26 | } 27 | return super.onCreateOptionsMenu(menu); 28 | } 29 | 30 | @Override 31 | protected void customizeActionBar() { 32 | if (mActionBar == null) { 33 | mActionBar = getActionBar(); 34 | } 35 | mActionBar.setDisplayHomeAsUpEnabled(true); 36 | mActionBar.setDisplayShowCustomEnabled(true); 37 | mActionBar.setDisplayShowTitleEnabled(true); 38 | mActionBar.setDisplayShowHomeEnabled(false); 39 | } 40 | 41 | 42 | @Override 43 | public void onBackPressed() { 44 | if (!this.mWebFragment.onBackPressed()) { 45 | super.onBackPressed(); 46 | } 47 | } 48 | 49 | @Override 50 | protected int getContentViewResId() { 51 | return R.layout.openweb_activity; 52 | } 53 | 54 | @Override 55 | protected int getWebFragmentResId() { 56 | return R.id.yellowpage_fragment_container; 57 | } 58 | 59 | @Override 60 | protected void onMenuHome() { 61 | if (!this.mWebFragment.onMenuHome()) { 62 | finish(); 63 | } 64 | } 65 | 66 | 67 | @Override 68 | protected void handleMessage(Message msg) { 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /app/src/main/res/layout/openweb_activity.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/layout/openweb_fragment.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solartisan/OpenWeb/a00215df6de513956a934594c9cd7033d1ffc98d/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solartisan/OpenWeb/a00215df6de513956a934594c9cd7033d1ffc98d/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solartisan/OpenWeb/a00215df6de513956a934594c9cd7033d1ffc98d/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solartisan/OpenWeb/a00215df6de513956a934594c9cd7033d1ffc98d/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solartisan/OpenWeb/a00215df6de513956a934594c9cd7033d1ffc98d/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | OpenWeb 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/cc/solart/openweb/simple/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb.simple; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.0.0' 9 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' 10 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6' 11 | // NOTE: Do not place your application dependencies here; they belong 12 | // in the individual module build.gradle files 13 | } 14 | } 15 | 16 | allprojects { 17 | repositories { 18 | jcenter() 19 | } 20 | } 21 | 22 | task clean(type: Delete) { 23 | delete rootProject.buildDir 24 | } 25 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solartisan/OpenWeb/a00215df6de513956a934594c9cd7033d1ffc98d/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Apr 02 21:06:36 CST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /openweb/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /openweb/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | apply plugin: 'com.jfrog.bintray' 4 | 5 | 6 | android { 7 | compileSdkVersion 23 8 | buildToolsVersion "23.0.2" 9 | 10 | defaultConfig { 11 | minSdkVersion 14 12 | targetSdkVersion 23 13 | versionCode 1 14 | versionName version 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | compile fileTree(dir: 'libs', include: ['*.jar']) 26 | compile 'com.android.support:support-v4:23.1.1' 27 | } 28 | 29 | version = "1.1.1" 30 | 31 | // 根节点添加 32 | def siteUrl = 'https://github.com/Solartisan/OpenWeb' // project homepage 33 | def gitUrl = 'https://github.com/Solartisan/OpenWeb.git' // project git 34 | 35 | group = "cc.solart" 36 | 37 | install { 38 | repositories.mavenInstaller { 39 | // This generates POM.xml with proper parameters 40 | pom { 41 | project { 42 | packaging 'aar' 43 | name 'OpenWeb For Android' // #CONFIG# // project title 44 | url siteUrl 45 | // Set your license 46 | licenses { 47 | license { 48 | name 'The Apache Software License, Version 2.0' 49 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt' 50 | } 51 | } 52 | developers { 53 | developer { 54 | id 'solart' // #CONFIG# // your user id (you can write your nickname) 55 | name 'solartisan' // #CONFIG# // your user name 56 | email 'imilko7@gmail.com' // #CONFIG# // your email 57 | } 58 | } 59 | scm { 60 | connection gitUrl 61 | developerConnection gitUrl 62 | url siteUrl 63 | } 64 | } 65 | } 66 | } 67 | } 68 | 69 | task sourcesJar(type: Jar) { 70 | from android.sourceSets.main.java.srcDirs 71 | classifier = 'sources' 72 | } 73 | 74 | task javadoc(type: Javadoc) { 75 | source = android.sourceSets.main.java.srcDirs 76 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 77 | } 78 | 79 | task javadocJar(type: Jar, dependsOn: javadoc) { 80 | classifier = 'javadoc' 81 | from javadoc.destinationDir 82 | } 83 | 84 | artifacts { 85 | archives javadocJar 86 | archives sourcesJar 87 | } 88 | 89 | Properties properties = new Properties() 90 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 91 | bintray { 92 | user = properties.getProperty("bintray.user") 93 | key = properties.getProperty("bintray.key") 94 | configurations = ['archives'] 95 | pkg { 96 | repo = "maven" 97 | name = "android-openweb" // #CONFIG# project name in jcenter 98 | websiteUrl = siteUrl 99 | vcsUrl = gitUrl 100 | licenses = ["Apache-2.0"] 101 | publish = true 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /openweb/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/imilk/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /openweb/src/androidTest/java/cc/solart/openweb/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /openweb/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /openweb/src/main/java/cc/solart/openweb/OnRefreshStatusListener.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb; 2 | 3 | /** 4 | * ------------------------------------------------------------------------- 5 | * Author: imilk 6 | * Create: 18:16 7 | * ------------------------------------------------------------------------- 8 | * Describe: 9 | * ------------------------------------------------------------------------- 10 | * Changes: 11 | * ------------------------------------------------------------------------- 12 | * 18 : Create by imilk 13 | * ------------------------------------------------------------------------- 14 | */ 15 | public interface OnRefreshStatusListener { 16 | 17 | boolean isRefreshing(); 18 | 19 | void refreshComplete(); 20 | } 21 | -------------------------------------------------------------------------------- /openweb/src/main/java/cc/solart/openweb/OpenWebEvent.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb; 2 | 3 | import android.content.Context; 4 | import android.os.Handler; 5 | import android.util.Log; 6 | import android.webkit.JavascriptInterface; 7 | import android.widget.Toast; 8 | 9 | import cc.solart.openweb.base.BaseWebEvent; 10 | import cc.solart.openweb.base.WebUrl; 11 | import cc.solart.openweb.utils.NetworkUtil; 12 | 13 | 14 | /** 15 | * Created by imilk on 15/6/9. 16 | */ 17 | public class OpenWebEvent extends BaseWebEvent { 18 | private static final String TAG = "OpenWebEvent"; 19 | public OpenWebEvent(Context context, Handler handler, WebUrl url) { 20 | super(context, handler, url); 21 | } 22 | 23 | @JavascriptInterface 24 | public void logHTML(String message) { 25 | Log.i(TAG, "html: " +message); 26 | } 27 | 28 | 29 | /** 30 | * 判断当前网络是否为Wifi 31 | * 32 | * @return 33 | */ 34 | @JavascriptInterface 35 | public boolean isWifiDataEnable() { 36 | return NetworkUtil.isWifiConnected(mContext); 37 | } 38 | 39 | 40 | /** 41 | * 通过toast显示信息 42 | * 43 | * @param message 44 | */ 45 | @JavascriptInterface 46 | public void toastMessage(final String message) { 47 | mHandler.post(new Runnable() { 48 | @Override 49 | public void run() { 50 | Toast.makeText(mContext,message,Toast.LENGTH_SHORT).show(); 51 | } 52 | }); 53 | } 54 | 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /openweb/src/main/java/cc/solart/openweb/OpenWebFragment.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.app.AlertDialog; 5 | import android.content.Context; 6 | import android.content.DialogInterface; 7 | import android.content.Intent; 8 | import android.content.res.Resources; 9 | import android.location.LocationManager; 10 | import android.net.Uri; 11 | import android.net.http.SslError; 12 | import android.os.Bundle; 13 | import android.os.Handler; 14 | import android.os.Message; 15 | import android.provider.Settings; 16 | import android.text.TextUtils; 17 | import android.view.LayoutInflater; 18 | import android.view.Menu; 19 | import android.view.MenuInflater; 20 | import android.view.View; 21 | import android.view.ViewGroup; 22 | import android.webkit.GeolocationPermissions; 23 | import android.webkit.SslErrorHandler; 24 | import android.webkit.WebChromeClient; 25 | import android.webkit.WebView; 26 | import android.webkit.WebViewClient; 27 | import android.widget.ProgressBar; 28 | 29 | import java.lang.ref.WeakReference; 30 | 31 | import cc.solart.openweb.base.BaseWebChromeClient; 32 | import cc.solart.openweb.base.BaseWebEvent; 33 | import cc.solart.openweb.base.BaseWebFragment; 34 | import cc.solart.openweb.base.BaseWebViewClient; 35 | import cc.solart.openweb.base.WebUrl; 36 | import cc.solart.openweb.utils.Logger; 37 | import cc.solart.openweb.utils.ObjEnsureUtil; 38 | 39 | /** 40 | * Created by imilk on 15/6/9. 41 | */ 42 | public abstract class OpenWebFragment extends BaseWebFragment { 43 | 44 | private static final int REQUEST_CODE_SSL_ERROR = 101; 45 | private static final int REQUEST_CODE_GPS_JUMP = 102; 46 | private static final String TAG = "OpenWebFragment"; 47 | 48 | 49 | private WebFragmentHandler mHandler = new WebFragmentHandler(this); 50 | 51 | 52 | private String backPressedCallback; 53 | private String topBackCallback; 54 | 55 | 56 | class WebFragmentHandler extends Handler { 57 | private final WeakReference wrFragment; 58 | 59 | WebFragmentHandler(OpenWebFragment openWebFragment) { 60 | this.wrFragment = new WeakReference(openWebFragment); 61 | } 62 | 63 | public void clearReference() { 64 | this.wrFragment.clear(); 65 | } 66 | 67 | @Override 68 | public void handleMessage(Message msg) { 69 | super.handleMessage(msg); 70 | switch (msg.what) { 71 | case BaseWebEvent.MESSAGE_GO_HOME: 72 | if (mActivity != null) { 73 | mActivity.finish(); 74 | } 75 | break; 76 | case BaseWebEvent.MESSAGE_GO_BACK: 77 | goBack(); 78 | break; 79 | case BaseWebEvent.MESSAGE_SET_TITLE: 80 | String title = (String) msg.obj; 81 | if (mActivity != null) { 82 | mActivity.setTitle(title); 83 | } 84 | break; 85 | } 86 | } 87 | } 88 | 89 | 90 | /** 91 | * if you need pull to refresh, you can coding like this, otherwise return null. 92 | * Careful use of pull down refresh, it may lead to a sliding conflict with the web page. 93 | * You can also disable and enable the pull to refresh to circumvent this problem by JavaScript 94 | * override {@link #enablePullToRefresh()} and {@link #disablePullToRefresh()} 95 | * @return 96 | */ 97 | protected OnRefreshStatusListener getOnRefreshStatusListener(){ 98 | return null; 99 | } 100 | 101 | 102 | @Override 103 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 104 | View view = super.onCreateView(inflater, container, savedInstanceState); 105 | 106 | return view; 107 | } 108 | 109 | @Override 110 | public void onResume() { 111 | super.onResume(); 112 | } 113 | 114 | @Override 115 | public void onPause() { 116 | super.onPause(); 117 | } 118 | 119 | @Override 120 | public void onStop() { 121 | super.onStop(); 122 | } 123 | 124 | @Override 125 | public void onDestroy() { 126 | super.onDestroy(); 127 | //清理掉fragment的引用 128 | mHandler.clearReference(); 129 | } 130 | 131 | 132 | /** 133 | * 检测 134 | * 135 | * @return 136 | */ 137 | private boolean ensureNonNull() { 138 | return ObjEnsureUtil.ensureNonNull(new Object[]{this.mActivity, this.mWebView, this.mWebEvent, this.mHandler}); 139 | } 140 | 141 | 142 | @Override 143 | public boolean onMenuHome() { 144 | if (topBackCallback != null) { 145 | mWebView.loadUrl( 146 | "javascript:" + topBackCallback + "()"); 147 | Logger.d(TAG,"load js: javascript:" + topBackCallback + "()"); 148 | return true; 149 | } 150 | return super.onMenuHome(); 151 | } 152 | 153 | @Override 154 | public boolean onBackPressed() { 155 | if (!ensureNonNull()) 156 | return false; 157 | if (backPressedCallback != null) { 158 | mWebView.loadUrl( 159 | "javascript:" + backPressedCallback + "()"); 160 | Logger.d(TAG, "load js: javascript:" + backPressedCallback + "()"); 161 | return true; 162 | } 163 | return super.onBackPressed(); 164 | } 165 | 166 | 167 | protected void setCookie() { 168 | 169 | } 170 | 171 | protected void disablePullToRefresh(){ 172 | 173 | } 174 | 175 | protected void enablePullToRefresh(){ 176 | 177 | } 178 | 179 | 180 | @Override 181 | public void loadUrl(String url) { 182 | setCookie(); 183 | super.loadUrl(url); 184 | if (mProgressBar.getVisibility() == View.GONE) { 185 | mProgressBar.setVisibility(View.VISIBLE); 186 | } 187 | } 188 | 189 | @Override 190 | public void reload() { 191 | super.reload(); 192 | if (mProgressBar.getVisibility() == View.GONE) { 193 | mProgressBar.setVisibility(View.VISIBLE); 194 | } 195 | } 196 | 197 | @SuppressLint("AddJavascriptInterface") 198 | @Override 199 | protected void onInitWebViewSettings() { 200 | super.onInitWebViewSettings(); 201 | this.mHandler = new WebFragmentHandler(this); 202 | this.mWebEvent = new OpenWebEvent(this.mActivity, this.mHandler, new WebUrl() { 203 | @Override 204 | public String getCurrentUrl() { 205 | return mWebView.getUrl(); 206 | } 207 | }); 208 | this.mWebView.addJavascriptInterface(this.mWebEvent, "MyJsBridge"); 209 | } 210 | 211 | @Override 212 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 213 | if (!ensureNonNull()) 214 | return; 215 | Logger.d(TAG,"onCreateOptionsMenu"); 216 | } 217 | 218 | public void allowCreateWebMenu() { 219 | if (!ensureNonNull()) 220 | return; 221 | setHasOptionsMenu(true); 222 | } 223 | 224 | @Override 225 | protected WebChromeClient onCreateWebChromeClient() { 226 | return new OpenWebChromeClient(this); 227 | } 228 | 229 | @Override 230 | protected WebViewClient onCreateWebViewClient() { 231 | return new OpenWebViewClient(this); 232 | } 233 | 234 | @Override 235 | public void onActivityResult(int requestCode, int resultCode, Intent data) { 236 | Logger.i(TAG, "onActivityResult requestCode = " + requestCode + " resultCode = " + resultCode); 237 | 238 | switch (requestCode) { 239 | case REQUEST_CODE_SSL_ERROR: 240 | case REQUEST_CODE_GPS_JUMP: 241 | reload(); 242 | break; 243 | 244 | } 245 | 246 | super.onActivityResult(requestCode, resultCode, data); 247 | } 248 | 249 | class OpenWebViewClient extends BaseWebViewClient { 250 | 251 | private OpenWebViewClient(BaseWebFragment webFragment) { 252 | super(webFragment); 253 | } 254 | 255 | @Override 256 | public boolean shouldOverrideUrlLoading(WebView view, String url) { 257 | boolean flag = super.shouldOverrideUrlLoading(view, url); 258 | if (!flag) { 259 | Uri uri = Uri.parse(url); 260 | String scheme = uri.getScheme(); 261 | if (!TextUtils.isEmpty(scheme)) { 262 | if (TextUtils.equals(scheme, "http") || TextUtils.equals(scheme, "https")) { 263 | mProgressBar.setVisibility(View.VISIBLE); 264 | disablePullToRefresh(); 265 | backPressedCallback = null; 266 | topBackCallback = null; 267 | } 268 | } 269 | return false; 270 | } else { 271 | return true; 272 | } 273 | 274 | } 275 | 276 | 277 | @Override 278 | public void onPageFinished(WebView view, String url) { 279 | super.onPageFinished(view, url); 280 | if (getOnRefreshStatusListener()!=null && getOnRefreshStatusListener().isRefreshing()) { 281 | getOnRefreshStatusListener().refreshComplete(); 282 | } 283 | if (mProgressBar.getVisibility() == View.VISIBLE) { 284 | mProgressBar.setVisibility(View.GONE); 285 | enablePullToRefresh(); 286 | } 287 | 288 | } 289 | 290 | @Override 291 | public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { 292 | super.onReceivedSslError(view, handler, error); 293 | } 294 | } 295 | 296 | 297 | class OpenWebChromeClient extends BaseWebChromeClient { 298 | 299 | private OpenWebChromeClient(BaseWebFragment webFragment) { 300 | super(webFragment); 301 | } 302 | 303 | @Override 304 | public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { 305 | LocationManager locationManager = (LocationManager) mActivity. 306 | getSystemService(Context.LOCATION_SERVICE); 307 | boolean enable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 308 | if (!enable) { 309 | AlertDialog dialog = new AlertDialog.Builder(mActivity).setTitle("温馨提示"). 310 | setMessage("检测到您的GPS已经关闭,是否需要打开?"). 311 | setNegativeButton("取消", new DialogInterface.OnClickListener() { 312 | 313 | @Override 314 | public void onClick(DialogInterface dialog, int which) { 315 | dialog.dismiss(); 316 | } 317 | }).setPositiveButton("打开", new DialogInterface.OnClickListener() { 318 | 319 | @Override 320 | public void onClick(DialogInterface dialog, int which) { 321 | dialog.dismiss(); 322 | Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 323 | startActivityForResult(intent, REQUEST_CODE_GPS_JUMP); 324 | } 325 | }).create(); 326 | dialog.show(); 327 | } else { 328 | super.onGeolocationPermissionsShowPrompt(origin, callback); 329 | } 330 | 331 | } 332 | 333 | @Override 334 | public void onProgressChanged(WebView view, int newProgress) { 335 | super.onProgressChanged(view, newProgress); 336 | if (getOnRefreshStatusListener() == null || (getOnRefreshStatusListener() !=null && !getOnRefreshStatusListener().isRefreshing())) { 337 | mProgressBar.setProgress(newProgress); 338 | } 339 | 340 | } 341 | 342 | @Override 343 | public void onReceivedTitle(WebView view, String title) { 344 | super.onReceivedTitle(view, title); 345 | mActivity.setTitle(title); 346 | } 347 | } 348 | } -------------------------------------------------------------------------------- /openweb/src/main/java/cc/solart/openweb/base/BaseWebChromeClient.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb.base; 2 | 3 | import android.webkit.GeolocationPermissions; 4 | import android.webkit.JsPromptResult; 5 | import android.webkit.WebChromeClient; 6 | import android.webkit.WebView; 7 | 8 | import cc.solart.openweb.utils.Logger; 9 | 10 | 11 | /** 12 | * Created by imilk on 15/6/8. 13 | */ 14 | public class BaseWebChromeClient extends WebChromeClient { 15 | 16 | protected static final String TAG = "BaseWebChromeClient"; 17 | private BaseWebFragment mWebFragment; 18 | public BaseWebChromeClient(BaseWebFragment webFragment) { 19 | mWebFragment = webFragment; 20 | } 21 | 22 | @Override 23 | public void onGeolocationPermissionsShowPrompt(String origin,GeolocationPermissions.Callback callback) { 24 | super.onGeolocationPermissionsShowPrompt(origin, callback); 25 | callback.invoke(origin, true, false); 26 | 27 | } 28 | 29 | @Override 30 | public void onProgressChanged(WebView view, int newProgress) { 31 | Logger.i(TAG, "onProgressChanged newProgress=" + newProgress); 32 | 33 | } 34 | 35 | @Override 36 | public boolean onJsPrompt(WebView view, String url, String message, 37 | String defaultValue, JsPromptResult result) { 38 | Logger.i(TAG, "onJsPrompt message=" + message); 39 | return true; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /openweb/src/main/java/cc/solart/openweb/base/BaseWebEvent.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb.base; 2 | 3 | import android.content.ClipData; 4 | import android.content.ClipboardManager; 5 | import android.content.Context; 6 | import android.os.Handler; 7 | import android.os.Looper; 8 | import android.os.Message; 9 | import android.text.TextUtils; 10 | import android.webkit.JavascriptInterface; 11 | 12 | 13 | import java.util.ArrayList; 14 | import java.util.HashMap; 15 | import java.util.List; 16 | import java.util.Map; 17 | 18 | import cc.solart.openweb.utils.Logger; 19 | 20 | /** 21 | * web页面基类js定义 22 | * Created by imilk on 15/6/8. 23 | */ 24 | public abstract class BaseWebEvent { 25 | private static final String TAG = "BaseWebEvent"; 26 | public static final String BACK_PRESSED_LISTENER = "backPressed"; 27 | public static final String HOME_PRESSED_LISTENER = "homePressed"; 28 | 29 | /** 30 | * 返回主页 31 | */ 32 | public static final int MESSAGE_GO_HOME = 10; 33 | /** 34 | * 返回上一页 35 | */ 36 | public static final int MESSAGE_GO_BACK = 11; 37 | /** 38 | * 设置页面标题 39 | */ 40 | public static final int MESSAGE_SET_TITLE = 12; 41 | 42 | protected Context mContext; 43 | protected Handler mHandler; 44 | private Map mListener; 45 | private WebUrl mUrlSource; 46 | 47 | public BaseWebEvent(Context context, Handler handler, WebUrl url) { 48 | this.mContext = context; 49 | this.mHandler = handler; 50 | this.mListener = new HashMap(); 51 | this.mUrlSource = url; 52 | } 53 | 54 | private String getCurrentUrl() { 55 | return this.mUrlSource.getCurrentUrl(); 56 | } 57 | 58 | 59 | private static void ensureOnMainThread() { 60 | if (Looper.myLooper() != Looper.getMainLooper()) 61 | throw new RuntimeException("handleWebEvent can only be called from main thread"); 62 | } 63 | 64 | public boolean handleWebEvent(String paramString) { 65 | ensureOnMainThread(); 66 | return handleWebEvent(paramString, null); 67 | } 68 | 69 | public boolean handleWebEvent(String event, Object data) { 70 | List callbacks = (List) mListener.get(getCurrentUrl()); 71 | int what = 0; 72 | if (callbacks != null) { 73 | if (callbacks.contains(event)) { 74 | if (BACK_PRESSED_LISTENER.equals(event)) { 75 | what = MESSAGE_GO_BACK; 76 | } else if (HOME_PRESSED_LISTENER.equals(event)) { 77 | what = MESSAGE_GO_HOME; 78 | } else { 79 | Logger.d(TAG, "cannot handle event:" + event); 80 | return false; 81 | } 82 | sendAsyncCallbackMessage(what, data); 83 | return true; 84 | } 85 | } 86 | return false; 87 | } 88 | 89 | public void sendAsyncCallbackMessage(int what, Object obj) { 90 | sendAsyncCallbackMessageDelay(what,obj,0); 91 | } 92 | 93 | 94 | /** 95 | * 96 | * @param what 97 | * @param obj 98 | * @param delay 99 | */ 100 | public void sendAsyncCallbackMessageDelay(int what, Object obj,long delay) { 101 | if (mHandler != null) { 102 | Message msg = mHandler.obtainMessage(); 103 | msg.what = what; 104 | msg.obj = obj; 105 | mHandler.sendMessageDelayed(msg,delay); 106 | } 107 | } 108 | 109 | @JavascriptInterface 110 | public void setListener(final String listener) { 111 | if (!TextUtils.isEmpty(listener)) { 112 | this.mHandler.post(new Runnable() { 113 | @Override 114 | public void run() { 115 | //TODO mListener put 116 | if (mListener.get(getCurrentUrl()) == null) { 117 | ArrayList callbacks = new ArrayList(); 118 | mListener.put(getCurrentUrl(), callbacks); 119 | }else { 120 | ((List) mListener.get(getCurrentUrl())).add(listener); 121 | } 122 | } 123 | }); 124 | } 125 | } 126 | 127 | 128 | @JavascriptInterface 129 | public void goHome() { 130 | sendAsyncCallbackMessage(MESSAGE_GO_HOME, null); 131 | } 132 | 133 | @JavascriptInterface 134 | public void goBack() { 135 | this.mHandler.post(new Runnable() { 136 | 137 | @Override 138 | public void run() { 139 | mListener.remove(getCurrentUrl()); 140 | sendAsyncCallbackMessage(MESSAGE_GO_BACK, null); 141 | } 142 | }); 143 | } 144 | 145 | @JavascriptInterface 146 | public void setTitle(String title) { 147 | if (TextUtils.isEmpty(title)) { 148 | return; 149 | } 150 | sendAsyncCallbackMessage(MESSAGE_SET_TITLE, title); 151 | } 152 | 153 | /** 154 | * 155 | * @param label 156 | * @param text 157 | */ 158 | @JavascriptInterface 159 | public void copyText(String label, String text) { 160 | ClipboardManager copy = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE); 161 | copy.setPrimaryClip(ClipData.newPlainText(label, text)); 162 | } 163 | 164 | 165 | } 166 | -------------------------------------------------------------------------------- /openweb/src/main/java/cc/solart/openweb/base/BaseWebFragment.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb.base; 2 | 3 | import android.app.Activity; 4 | import android.app.Fragment; 5 | import android.content.BroadcastReceiver; 6 | import android.content.Context; 7 | import android.content.Intent; 8 | import android.content.IntentFilter; 9 | import android.net.Uri; 10 | import android.os.Build; 11 | import android.os.Bundle; 12 | import android.text.TextUtils; 13 | import android.view.LayoutInflater; 14 | import android.view.View; 15 | import android.view.ViewGroup; 16 | import android.webkit.DownloadListener; 17 | import android.webkit.WebBackForwardList; 18 | import android.webkit.WebChromeClient; 19 | import android.webkit.WebView; 20 | import android.webkit.WebViewClient; 21 | import android.widget.ProgressBar; 22 | 23 | import cc.solart.openweb.utils.Logger; 24 | import cc.solart.openweb.utils.NetworkUtil; 25 | import cc.solart.openweb.utils.ObjEnsureUtil; 26 | import cc.solart.openweb.utils.WebSettingsUtil; 27 | import cc.solart.openweb.widget.OpenWebLayout; 28 | 29 | 30 | /** 31 | * Created by imilk on 15/6/8. 32 | */ 33 | public abstract class BaseWebFragment extends Fragment { 34 | protected static final String TAG = "BaseWebFragment"; 35 | protected static final String BLANK_SCREEN_URL = "about:blank"; 36 | protected BaseWebEvent mWebEvent; 37 | protected WebView mWebView; 38 | protected ProgressBar mProgressBar; 39 | protected boolean mNetworkConnected; 40 | private NetworkConnectivityReceiver mNetworkConnectivityReceiver; 41 | 42 | /** 43 | * TargetApi{@link Build.VERSION_CODES.KITKAT} 44 | */ 45 | static { 46 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 47 | WebView.setWebContentsDebuggingEnabled(true); 48 | } 49 | } 50 | 51 | protected Activity mActivity; 52 | 53 | 54 | @Override 55 | public void onCreate(Bundle paramBundle) { 56 | super.onCreate(paramBundle); 57 | } 58 | 59 | protected abstract int loadLayoutRes(); 60 | 61 | protected abstract int getWebViewId(); 62 | 63 | @Override 64 | public void onAttach(Context context) { 65 | super.onAttach(context); 66 | } 67 | 68 | @Override 69 | public void onAttach(Activity activity) { 70 | super.onAttach(activity); 71 | this.mActivity = activity; 72 | registerConnectivityReceiver(); 73 | mNetworkConnected = NetworkUtil.isNetConnected(mActivity); 74 | } 75 | 76 | @Override 77 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 78 | int i = loadLayoutRes(); 79 | if (i <= 0) 80 | throw new IllegalArgumentException("You must provide a valid category_more"); 81 | View view = inflater.inflate(i, container, false); 82 | 83 | OpenWebLayout openWebLayout = (OpenWebLayout) view.findViewById(getWebViewId()); 84 | if (openWebLayout == null) 85 | throw new IllegalArgumentException("As a web view fragment, you must provide a OpenWebLayout"); 86 | 87 | mWebView = openWebLayout.getWebView(); 88 | 89 | mProgressBar = openWebLayout.getProgressBar(); 90 | onInitWebViewSettings(); 91 | return view; 92 | } 93 | 94 | public void onResume() { 95 | super.onResume(); 96 | if (mWebView != null) { 97 | mWebView.onResume(); 98 | } 99 | } 100 | 101 | @Override 102 | public void onPause() { 103 | super.onPause(); 104 | if (mWebView != null) { 105 | mWebView.onPause(); 106 | } 107 | } 108 | 109 | @Override 110 | public void onDetach() { 111 | super.onDetach(); 112 | unregisterConnectivityReceiver(); 113 | } 114 | 115 | @Override 116 | public void onDestroy() { 117 | super.onDestroy(); 118 | if (mWebView != null) { 119 | ((ViewGroup) mWebView.getParent()).removeView(mWebView); 120 | mWebView.destroy(); 121 | } 122 | } 123 | 124 | protected void onInitWebViewSettings() { 125 | WebSettingsUtil.initSettings(this.mActivity, this.mWebView); 126 | WebChromeClient webChromeClient = onCreateWebChromeClient(); 127 | if (webChromeClient != null) { 128 | mWebView.setWebChromeClient(webChromeClient); 129 | } 130 | WebViewClient webViewClient = onCreateWebViewClient(); 131 | if (webViewClient != null) { 132 | mWebView.setWebViewClient(webViewClient); 133 | } 134 | mWebView.setOverScrollMode(View.OVER_SCROLL_NEVER); 135 | mWebView.requestFocus(); 136 | mWebView.setDownloadListener(new WebDownloadListener(this)); 137 | } 138 | 139 | private boolean ensureNonNull() { 140 | return ObjEnsureUtil.ensureNonNull(new Object[]{this.mActivity, this.mWebView, this.mWebEvent}); 141 | } 142 | 143 | public void loadUrl(String url) { 144 | if (!ensureNonNull()) 145 | return; 146 | if (TextUtils.isEmpty(url)) { 147 | Logger.e(TAG, "The url should not be null, load nothing"); 148 | return; 149 | } 150 | Logger.d(TAG, "loadUrl: " + url); 151 | this.mWebView.loadUrl(url); 152 | } 153 | 154 | /** 155 | * webview reload 156 | */ 157 | public void reload() { 158 | if (!ensureNonNull()) 159 | return; 160 | Logger.d("BaseWebFragment", "webview reload"); 161 | this.mWebView.reload(); 162 | } 163 | 164 | protected void onNetworkConnected() { 165 | reload(); 166 | } 167 | 168 | 169 | protected WebChromeClient onCreateWebChromeClient() { 170 | return new BaseWebChromeClient(this); 171 | } 172 | 173 | protected WebViewClient onCreateWebViewClient() { 174 | return new BaseWebViewClient(this); 175 | } 176 | 177 | private int stepsToGoBack() { 178 | int j = 1; 179 | WebBackForwardList webBackForwardList = mWebView.copyBackForwardList(); 180 | int k = webBackForwardList.getCurrentIndex(); 181 | 182 | Logger.d(TAG, "k = " + k); 183 | for (int i = 0; i <= k; i++) { 184 | String url = webBackForwardList.getItemAtIndex(k - i).getUrl(); 185 | if ((!BLANK_SCREEN_URL.equalsIgnoreCase(url)) && (TextUtils.equals(mWebView.getUrl(), url))) 186 | break; 187 | j += 1; 188 | Logger.d(TAG, "j = " + j); 189 | } 190 | return j; 191 | } 192 | 193 | /** 194 | * 回退H5 195 | * 196 | * @return 197 | */ 198 | protected boolean goBack() { 199 | int i; 200 | int j; 201 | if (mWebView.canGoBack()) { 202 | WebBackForwardList backForwardList = mWebView.copyBackForwardList(); 203 | i = stepsToGoBack(); 204 | j = backForwardList.getCurrentIndex(); 205 | Logger.d(TAG, "i = " + i); 206 | Logger.d(TAG, "j = " + j); 207 | if (i <= j) { 208 | String title = backForwardList.getItemAtIndex(j - i).getTitle(); 209 | if (!TextUtils.isEmpty(title)) { 210 | mActivity.setTitle(title); 211 | } 212 | mWebView.goBackOrForward(-i); 213 | return true; 214 | } 215 | } 216 | return false; 217 | } 218 | 219 | 220 | /** 221 | * 前进H5页面 222 | * 223 | * @return 224 | */ 225 | protected boolean goForward() { 226 | int i; 227 | int j; 228 | if (mWebView.canGoForward()) { 229 | WebBackForwardList backForwardList = mWebView.copyBackForwardList(); 230 | i = 1; 231 | j = backForwardList.getCurrentIndex(); 232 | if (backForwardList.getSize() > j) { 233 | String title = backForwardList.getItemAtIndex(j + i).getTitle(); 234 | if (!TextUtils.isEmpty(title)) { 235 | mActivity.setTitle(title); 236 | } 237 | mWebView.goBackOrForward(i); 238 | return true; 239 | } 240 | } 241 | return false; 242 | } 243 | 244 | /** 245 | * ActionBar返回监听处理,子类应该覆写该方法 246 | * @return 247 | */ 248 | public boolean onMenuHome(){ 249 | return false; 250 | } 251 | 252 | 253 | public boolean onBackPressed() { 254 | if (!ensureNonNull()) 255 | return false; 256 | return goBack(); 257 | } 258 | 259 | public boolean onForwardPressed() { 260 | if (!ensureNonNull()) 261 | return false; 262 | return goForward(); 263 | } 264 | 265 | 266 | private void registerConnectivityReceiver() { 267 | Logger.d(TAG, "Register network connectivity changed receiver"); 268 | if (mNetworkConnectivityReceiver == null) { 269 | mNetworkConnectivityReceiver = new NetworkConnectivityReceiver(); 270 | } 271 | IntentFilter intentFilter = new IntentFilter(); 272 | intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); 273 | mActivity.registerReceiver(mNetworkConnectivityReceiver, intentFilter); 274 | } 275 | 276 | private void unregisterConnectivityReceiver() { 277 | Logger.d(TAG, "Unregister network connectivity changed receiver"); 278 | mActivity.unregisterReceiver(mNetworkConnectivityReceiver); 279 | } 280 | 281 | /** 282 | * DownloadListener 283 | */ 284 | class WebDownloadListener implements DownloadListener { 285 | private BaseWebFragment mWebFragment; 286 | 287 | private WebDownloadListener(BaseWebFragment mWebFragment) { 288 | this.mWebFragment = mWebFragment; 289 | } 290 | 291 | 292 | @Override 293 | public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { 294 | try { 295 | Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 296 | mWebFragment.startActivity(intent); 297 | }catch (Exception e){ 298 | e.printStackTrace(); 299 | } 300 | } 301 | } 302 | 303 | class NetworkConnectivityReceiver extends BroadcastReceiver { 304 | @Override 305 | public void onReceive(Context context, Intent intent) { 306 | boolean bool = NetworkUtil.isNetConnected(mActivity); 307 | if ((!mNetworkConnected) && (bool)) { 308 | onNetworkConnected(); 309 | } 310 | mNetworkConnected = bool; 311 | } 312 | } 313 | } 314 | -------------------------------------------------------------------------------- /openweb/src/main/java/cc/solart/openweb/base/BaseWebViewClient.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb.base; 2 | 3 | import android.content.Intent; 4 | import android.net.MailTo; 5 | import android.net.Uri; 6 | import android.text.TextUtils; 7 | import android.webkit.WebView; 8 | import android.webkit.WebViewClient; 9 | 10 | 11 | import java.util.regex.Matcher; 12 | import java.util.regex.Pattern; 13 | 14 | import cc.solart.openweb.utils.Logger; 15 | 16 | /** 17 | * Created by imilk on 15/6/8. 18 | */ 19 | public class BaseWebViewClient extends WebViewClient { 20 | 21 | private BaseWebFragment mWebFragment; 22 | 23 | public BaseWebViewClient(BaseWebFragment webFragment) { 24 | mWebFragment = webFragment; 25 | } 26 | 27 | @Override 28 | public boolean shouldOverrideUrlLoading(WebView view, String url) { 29 | Logger.i("BaseWebFragment", "shouldOverrideUrlLoading url=" + url); 30 | if (TextUtils.isEmpty(url)) { 31 | return true; 32 | } 33 | 34 | if (url.startsWith("sms:")) { 35 | String regex = "sms:([\\d]*?)\\?body=([\\w\\W]*)"; 36 | Pattern p = Pattern.compile(regex); 37 | Matcher m = p.matcher(Uri.decode(url).replaceAll(" ", "")); 38 | if (m.find()) { 39 | String tel = m.group(1); 40 | String body = m.group(2); 41 | 42 | Uri smsto = Uri.parse("smsto:" + tel); 43 | Intent sendIntent = new Intent(Intent.ACTION_VIEW, smsto); 44 | sendIntent.putExtra("sms_body", body); 45 | mWebFragment.startActivity(sendIntent); 46 | return true; 47 | } 48 | } else if (url.startsWith("tel:")) { 49 | Intent telIntent = new Intent(Intent.ACTION_DIAL, Uri.parse(url)); 50 | telIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 51 | mWebFragment.startActivity(telIntent); 52 | return true; 53 | } else if (url.startsWith("mailto:")) { 54 | MailTo mailTo = MailTo.parse(url); 55 | Intent intent = new Intent(Intent.ACTION_SEND); 56 | intent.putExtra(Intent.EXTRA_EMAIL, new String[]{mailTo.getTo()}); 57 | intent.putExtra(Intent.EXTRA_CC, mailTo.getCc()); 58 | intent.putExtra(Intent.EXTRA_TEXT, mailTo.getBody()); 59 | intent.putExtra(Intent.EXTRA_SUBJECT, mailTo.getSubject()); 60 | intent.setPackage("com.android.email"); 61 | intent.setType("text/plain"); 62 | intent.addCategory(Intent.CATEGORY_BROWSABLE); 63 | mWebFragment.startActivity(intent); 64 | return true; 65 | } else if (url.startsWith("intent:")) { 66 | 67 | try { 68 | Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); 69 | mWebFragment.startActivity(intent); 70 | } catch (Exception e) { 71 | e.printStackTrace(); 72 | } 73 | return true; 74 | } 75 | 76 | return false; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /openweb/src/main/java/cc/solart/openweb/base/WebUrl.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb.base; 2 | 3 | /** 4 | * Created by imilk on 15/6/8. 5 | */ 6 | public interface WebUrl { 7 | 8 | /** 9 | * 获取当前Webview的url地址 10 | * @return 11 | */ 12 | String getCurrentUrl(); 13 | } 14 | -------------------------------------------------------------------------------- /openweb/src/main/java/cc/solart/openweb/utils/Logger.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb.utils; 2 | 3 | import android.util.Log; 4 | 5 | 6 | /** 7 | * ------------------------------------------------------------------------- 8 | * Author: imilk 9 | * Create: 15/10/14 14:10 10 | * ------------------------------------------------------------------------- 11 | * Describe:Logging helper class. Logger is a wrapper of {@link Log} 12 | * But more pretty, simple and powerful. 13 | *

14 | * 15 | * {@code /platform-tools/adb shell setprop log.tag.OpenWeb VERBOSE} 16 | * ------------------------------------------------------------------------- 17 | * Changes: 18 | * ------------------------------------------------------------------------- 19 | * 15/10/14 14 : Create by imilk 20 | * ------------------------------------------------------------------------- 21 | */ 22 | public final class Logger { 23 | private static String TAG = "OpenWeb"; 24 | 25 | private static boolean DEBUG = Log.isLoggable(TAG, Log.VERBOSE); 26 | 27 | 28 | /** 29 | * Customize the log tag for your application, so that other apps 30 | * using Volley don't mix their logs with yours. 31 | *
32 | * Enable the log property for your tag before starting your app: 33 | *
34 | * {@code adb shell setprop log.tag.<tag>} 35 | */ 36 | static { 37 | Log.d(TAG, "LOGGER DEBUG = " + DEBUG); 38 | } 39 | 40 | //no instance 41 | private Logger() { 42 | } 43 | 44 | 45 | public static void v(String tag, String msg){ 46 | if (DEBUG) 47 | Log.v(tag,msg); 48 | } 49 | 50 | public static void d(String tag, String msg){ 51 | if (DEBUG) 52 | Log.d(tag, msg); 53 | } 54 | 55 | public static void i(String tag, String msg){ 56 | if (DEBUG) 57 | Log.i(tag, msg); 58 | } 59 | 60 | public static void w(String tag, String msg){ 61 | if (DEBUG) 62 | Log.w(tag, msg); 63 | } 64 | 65 | public static void e(String tag, String msg){ 66 | if (DEBUG) 67 | Log.e(tag, msg); 68 | } 69 | 70 | public static void e(String tag, String msg,Throwable tr){ 71 | if (DEBUG) 72 | Log.e(tag, msg,tr); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /openweb/src/main/java/cc/solart/openweb/utils/NetworkUtil.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb.utils; 2 | 3 | import android.content.Context; 4 | import android.net.ConnectivityManager; 5 | import android.net.NetworkInfo; 6 | import android.os.Build; 7 | 8 | /** 9 | * Created by imilk on 15/6/19. 10 | */ 11 | public class NetworkUtil { 12 | 13 | public static boolean isNetConnected(Context context) { 14 | if(context == null) { 15 | return false; 16 | } 17 | ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)); 18 | NetworkInfo workinfo = connectivityManager.getActiveNetworkInfo(); 19 | return (workinfo != null) && (workinfo.isConnectedOrConnecting()); 20 | } 21 | 22 | public static boolean isMobileConnected(Context context) { 23 | if(context == null) { 24 | return false; 25 | } 26 | ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)); 27 | NetworkInfo workinfo = connectivityManager.getActiveNetworkInfo(); 28 | return (workinfo != null) && (workinfo.getType() == ConnectivityManager.TYPE_MOBILE); 29 | } 30 | 31 | public static int getNetworkType(Context context) { 32 | if(context == null) { 33 | return -1; 34 | } 35 | ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)); 36 | NetworkInfo workinfo = connectivityManager.getActiveNetworkInfo(); 37 | if (workinfo != null) 38 | return workinfo.getType(); 39 | return -1; 40 | } 41 | 42 | 43 | public static String getNetworkStringType(Context context) { 44 | if(context == null) { 45 | return null; 46 | } 47 | ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)); 48 | NetworkInfo workinfo = connectivityManager.getActiveNetworkInfo(); 49 | if (workinfo != null) { 50 | if (workinfo.getType() == ConnectivityManager.TYPE_BLUETOOTH) { 51 | return "b"; 52 | } 53 | if (workinfo.getType() == ConnectivityManager.TYPE_MOBILE) { 54 | return "m"; 55 | } 56 | if (workinfo.getType() == ConnectivityManager.TYPE_WIFI) { 57 | return "w"; 58 | } 59 | } 60 | return null; 61 | } 62 | 63 | public static boolean isWifiConnected(Context context) { 64 | if(context == null) { 65 | return false; 66 | } 67 | ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)); 68 | NetworkInfo workinfo = connectivityManager.getActiveNetworkInfo(); 69 | if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){ 70 | return (!connectivityManager.isActiveNetworkMetered()) && (workinfo != null) && (workinfo.isConnected()); 71 | }else{ 72 | return (workinfo != null) && (workinfo.isConnected()); 73 | } 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /openweb/src/main/java/cc/solart/openweb/utils/ObjEnsureUtil.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb.utils; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * Created by imilk on 15/6/9. 7 | */ 8 | public class ObjEnsureUtil { 9 | 10 | /** 11 | * 检测Object数组中是否有值为空 12 | * @param arrayOfObject 13 | * @return 14 | */ 15 | public static boolean ensureNonNull(Object[] arrayOfObject){ 16 | if (arrayOfObject == null) { 17 | return false; 18 | } 19 | 20 | for(Object obj:arrayOfObject){ 21 | if(obj==null){ 22 | return false; 23 | } 24 | } 25 | return true; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /openweb/src/main/java/cc/solart/openweb/utils/WebSettingsUtil.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb.utils; 2 | 3 | import android.content.Context; 4 | import android.os.Build; 5 | import android.webkit.WebSettings; 6 | import android.webkit.WebView; 7 | 8 | import java.io.File; 9 | 10 | /** 11 | * Created by imilk on 15/6/8. 12 | */ 13 | public class WebSettingsUtil { 14 | 15 | 16 | public static void initSettings(Context paramContext, WebView webView) 17 | { 18 | WebSettings webSettings = webView.getSettings(); 19 | webSettings.setJavaScriptEnabled(true); 20 | webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); 21 | if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) { 22 | webSettings.setAllowFileAccessFromFileURLs(true); 23 | webSettings.setAllowUniversalAccessFromFileURLs(true); 24 | } 25 | webSettings.setJavaScriptCanOpenWindowsAutomatically(true); 26 | webSettings.setUseWideViewPort(true); 27 | webSettings.setLoadWithOverviewMode(true); 28 | webSettings.setTextZoom(100); 29 | webSettings.setLoadsImagesAutomatically(true); 30 | webSettings.setBlockNetworkImage(false); 31 | if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){ 32 | webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); 33 | } 34 | setGeolocation(paramContext, webSettings); 35 | setAppCache(paramContext, webSettings); 36 | setStorage(paramContext, webSettings); 37 | setUserAgent(paramContext, webSettings); 38 | } 39 | 40 | private static void setGeolocation(Context paramContext, WebSettings paramWebSettings) 41 | { 42 | paramWebSettings.setGeolocationEnabled(true); 43 | paramWebSettings.setGeolocationDatabasePath(paramContext.getApplicationContext().getDir("geodatabase", 0).getPath()); 44 | } 45 | 46 | private static void setUserAgent(Context paramContext, WebSettings paramWebSettings) 47 | { 48 | StringBuilder localStringBuilder = new StringBuilder(); 49 | localStringBuilder.append(paramWebSettings.getUserAgentString()); 50 | paramWebSettings.setUserAgentString(localStringBuilder.toString()); 51 | } 52 | 53 | private static void setAppCache(Context paramContext, WebSettings paramWebSettings) 54 | { 55 | paramWebSettings.setAppCacheEnabled(true); 56 | String path = paramContext.getApplicationContext().getDir("cache", 0).getPath(); 57 | mkdirs(path); 58 | paramWebSettings.setAppCachePath(path); 59 | } 60 | 61 | private static void mkdirs(String paramString) 62 | { 63 | File file = new File(paramString); 64 | if (!file.exists()) 65 | file.mkdirs(); 66 | } 67 | 68 | private static void setStorage(Context paramContext, WebSettings paramWebSettings) 69 | { 70 | paramWebSettings.setDomStorageEnabled(true); 71 | paramWebSettings.setDatabaseEnabled(true); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /openweb/src/main/java/cc/solart/openweb/widget/OpenWebLayout.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb.widget; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.drawable.BitmapDrawable; 6 | import android.graphics.drawable.Drawable; 7 | import android.graphics.drawable.LayerDrawable; 8 | import android.os.Build; 9 | import android.util.AttributeSet; 10 | import android.view.ViewGroup; 11 | import android.webkit.WebView; 12 | import android.widget.FrameLayout; 13 | import android.widget.ProgressBar; 14 | 15 | import cc.solart.openweb.R; 16 | 17 | /** 18 | * ------------------------------------------------------------------------- 19 | * Author: imilk 20 | * Create: 14:38 21 | * ------------------------------------------------------------------------- 22 | * Describe: 23 | * ------------------------------------------------------------------------- 24 | * Changes: 25 | * ------------------------------------------------------------------------- 26 | * 14: Create by imilk 27 | * ------------------------------------------------------------------------- 28 | */ 29 | public class OpenWebLayout extends FrameLayout { 30 | 31 | private ProgressBar mProgressBar; 32 | private OpenWebView mWebView; 33 | private int mProgressHeight; 34 | private Drawable mProgressDrawable; 35 | private ViewGroup mRefreshView; 36 | private OpenWebView.OnWebScrollListener mOnWebScrollListener = new OpenWebView.OnWebScrollListener() { 37 | @Override 38 | public void onScrollChanged(int l, int t, int oldl, int oldt) { 39 | if (mRefreshView != null) { 40 | if (mWebView.getScrollY() == 0) { 41 | mRefreshView.setEnabled(true); 42 | } else { 43 | mRefreshView.setEnabled(false); 44 | } 45 | } 46 | } 47 | }; 48 | 49 | public OpenWebLayout(Context context) { 50 | this(context, null); 51 | } 52 | 53 | public OpenWebLayout(Context context, AttributeSet attrs) { 54 | this(context, attrs, 0); 55 | } 56 | 57 | public OpenWebLayout(Context context, AttributeSet attrs, int defStyleAttr) { 58 | super(context, attrs, defStyleAttr); 59 | 60 | inflate(context, R.layout.open_web_layout, this); 61 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.OpenWebLayout); 62 | mProgressHeight = ta.getDimensionPixelOffset(R.styleable.OpenWebLayout_progressHeight, context.getResources().getDimensionPixelOffset(R.dimen.dp_progress_height)); 63 | mProgressDrawable = ta.getDrawable(R.styleable.OpenWebLayout_progressDrawable); 64 | ta.recycle(); 65 | } 66 | 67 | @Override 68 | protected void onFinishInflate() { 69 | super.onFinishInflate(); 70 | mWebView = (OpenWebView) findViewById(R.id.open_webview); 71 | mWebView.setOnWebScrollListener(mOnWebScrollListener); 72 | mProgressBar = (ProgressBar) findViewById(R.id.open_progress_bar); 73 | LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mProgressHeight); 74 | mProgressBar.setLayoutParams(lp); 75 | if (mProgressDrawable != null) { 76 | if (needsTileify(mProgressDrawable) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 77 | mProgressBar.setProgressDrawableTiled(mProgressDrawable); 78 | } else { 79 | mProgressBar.setProgressDrawable(mProgressDrawable); 80 | } 81 | } 82 | mProgressBar.setIndeterminate(false); 83 | 84 | } 85 | 86 | public WebView getWebView() { 87 | return mWebView; 88 | } 89 | 90 | public ProgressBar getProgressBar() { 91 | return mProgressBar; 92 | } 93 | 94 | private static boolean needsTileify(Drawable dr) { 95 | if (dr instanceof LayerDrawable) { 96 | final LayerDrawable orig = (LayerDrawable) dr; 97 | final int N = orig.getNumberOfLayers(); 98 | for (int i = 0; i < N; i++) { 99 | if (needsTileify(orig.getDrawable(i))) { 100 | return true; 101 | } 102 | } 103 | return false; 104 | } 105 | 106 | // If there's a bitmap that's not wrapped with a ClipDrawable or 107 | // ScaleDrawable, we'll need to wrap it and apply tiling. 108 | if (dr instanceof BitmapDrawable) { 109 | return true; 110 | } 111 | 112 | return false; 113 | } 114 | 115 | public void setRefreshView(ViewGroup refreshView) { 116 | mRefreshView = refreshView; 117 | } 118 | 119 | 120 | public void setOnWebScrollListener(OpenWebView.OnWebScrollListener listener) { 121 | this.mOnWebScrollListener = listener; 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /openweb/src/main/java/cc/solart/openweb/widget/OpenWebView.java: -------------------------------------------------------------------------------- 1 | package cc.solart.openweb.widget; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.webkit.WebView; 6 | 7 | /** 8 | * ------------------------------------------------------------------------- 9 | * Author: imilk 10 | * Create: 20:53 11 | * ------------------------------------------------------------------------- 12 | * Describe: 13 | * ------------------------------------------------------------------------- 14 | * Changes: 15 | * ------------------------------------------------------------------------- 16 | * 20 : Create by imilk 17 | * ------------------------------------------------------------------------- 18 | */ 19 | public class OpenWebView extends WebView { 20 | 21 | private OnWebScrollListener mListener; 22 | 23 | public OpenWebView(Context context) { 24 | super(context); 25 | } 26 | 27 | public OpenWebView(Context context, AttributeSet attrs) { 28 | super(context, attrs); 29 | } 30 | 31 | public OpenWebView(Context context, AttributeSet attrs, int defStyleAttr) { 32 | super(context, attrs, defStyleAttr); 33 | } 34 | 35 | @Override 36 | protected void onScrollChanged(int l, int t, int oldl, int oldt) { 37 | super.onScrollChanged(l, t, oldl, oldt); 38 | if(mListener!=null){ 39 | mListener.onScrollChanged(l,t,oldl,oldt); 40 | } 41 | } 42 | 43 | 44 | public void setOnWebScrollListener(OnWebScrollListener listener) { 45 | this.mListener = listener; 46 | } 47 | 48 | public interface OnWebScrollListener { 49 | 50 | void onScrollChanged(int l, int t, int oldl, int oldt); 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /openweb/src/main/res/drawable/bg_progress_horizontal.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /openweb/src/main/res/layout/open_web_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /openweb/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /openweb/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 3dp 4 | -------------------------------------------------------------------------------- /openweb/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | openweb 3 | 4 | -------------------------------------------------------------------------------- /preview/openweb.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solartisan/OpenWeb/a00215df6de513956a934594c9cd7033d1ffc98d/preview/openweb.gif -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':openweb' 2 | --------------------------------------------------------------------------------