4.0版:参考别人的代码终于实现了多窗口
This commit is contained in:
@@ -34,6 +34,7 @@ import android.content.pm.PackageManager;
|
||||
import android.content.res.Configuration;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Matrix;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.media.MediaScannerConnection;
|
||||
@@ -76,23 +77,25 @@ import android.webkit.WebView.FindListener;
|
||||
import android.webkit.WebView.HitTestResult;
|
||||
import android.webkit.WebViewClient;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class MainActivity extends Activity {
|
||||
Button button_title, button_page;
|
||||
TextView findCount;
|
||||
EditText editText1, findText;
|
||||
ImageButton btnGo, btnBack, btnForward, btnMenu, findPrev, findNext, findClose, imageButton_info;
|
||||
WebView webView1;
|
||||
// RelativeLayout RelativeLayout1;
|
||||
LinearLayout LinearLayout1, LinearLayout2;
|
||||
FrameLayout video, findbar;
|
||||
FrameLayout webViewLayout, video, findbar;
|
||||
ImageView imageView1;
|
||||
InputMethodManager IMM;
|
||||
ProgressBar pgb1;
|
||||
@@ -105,6 +108,8 @@ public class MainActivity extends Activity {
|
||||
SharedPreferences sharedPreferences;
|
||||
Thread CU;
|
||||
long downloadIdUpdate;
|
||||
List<WebView> list_webView = new ArrayList<>();
|
||||
int currentPage;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -117,14 +122,12 @@ public class MainActivity extends Activity {
|
||||
dir.mkdirs();
|
||||
}
|
||||
setContentView(R.layout.activity_main);
|
||||
//ptitle = "百度";
|
||||
//urln = "http//www.baidu.com";
|
||||
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
|
||||
|
||||
LinearLayout1 = (LinearLayout) findViewById(R.id.LinearLayout1);
|
||||
LinearLayout2 = (LinearLayout) findViewById(R.id.LinearLayout2);
|
||||
// RelativeLayout1 = (RelativeLayout)
|
||||
// findViewById(R.id.RelativeLayout1);
|
||||
// RelativeLayout1 = (RelativeLayout) findViewById(R.id.RelativeLayout1);
|
||||
webViewLayout = (FrameLayout) findViewById(R.id.webViewLayout);
|
||||
video = (FrameLayout) findViewById(R.id.video);
|
||||
findbar = (FrameLayout) findViewById(R.id.findbar);
|
||||
pgb1 = (ProgressBar) findViewById(R.id.progressBar1);
|
||||
@@ -155,7 +158,10 @@ public class MainActivity extends Activity {
|
||||
findNext = (ImageButton) findViewById(R.id.findNext);
|
||||
findClose = (ImageButton) findViewById(R.id.findClose);
|
||||
imageButton_info = (ImageButton) findViewById(R.id.imageButton_info);
|
||||
// imageView1.setAlpha(100);
|
||||
button_title = (Button) findViewById(R.id.button_title);
|
||||
button_page = (Button) findViewById(R.id.button_page);
|
||||
button_title.setOnClickListener(new ButtonListener());
|
||||
button_page.setOnClickListener(new ButtonListener());
|
||||
btnGo.setOnClickListener(new ButtonListener());
|
||||
btnBack.setOnClickListener(new ButtonListener());
|
||||
btnForward.setOnClickListener(new ButtonListener());
|
||||
@@ -165,218 +171,9 @@ public class MainActivity extends Activity {
|
||||
findNext.setOnClickListener(new ButtonListener());
|
||||
findClose.setOnClickListener(new ButtonListener());
|
||||
editText1 = (EditText) findViewById(R.id.EditText1);
|
||||
webView1 = (WebView) findViewById(R.id.WebView1);
|
||||
editText1.setVisibility(View.GONE);
|
||||
WebIconDatabase.getInstance().open(getDir("icons", MODE_PRIVATE).getPath()); //获取图标数据库路径
|
||||
registerForContextMenu(webView1);
|
||||
// 开启JS
|
||||
webView1.getSettings().setJavaScriptEnabled(true);
|
||||
// 开启JS能打开窗口
|
||||
webView1.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
|
||||
// 开启缓存
|
||||
webView1.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
|
||||
// 自适应屏幕
|
||||
webView1.getSettings().setUseWideViewPort(true);
|
||||
webView1.getSettings().setLoadWithOverviewMode(true);
|
||||
// 支持缩放
|
||||
webView1.getSettings().setSupportZoom(true);
|
||||
// 启用内置缩放控件
|
||||
webView1.getSettings().setBuiltInZoomControls(true);
|
||||
// 隐藏缩放控件
|
||||
webView1.getSettings().setDisplayZoomControls(false);
|
||||
// 开启访问文件
|
||||
webView1.getSettings().setAllowFileAccess(true);
|
||||
// 开启数据库
|
||||
webView1.getSettings().setDatabaseEnabled(true);
|
||||
// 开启localStorage
|
||||
webView1.getSettings().setDomStorageEnabled(true);
|
||||
// 开启定位
|
||||
webView1.getSettings().setGeolocationEnabled(true);
|
||||
// 支持多窗口
|
||||
webView1.getSettings().supportMultipleWindows();
|
||||
// 支持获取手势焦点
|
||||
webView1.requestFocusFromTouch();
|
||||
// 允许跨域
|
||||
webView1.getSettings().setAllowUniversalAccessFromFileURLs(true);
|
||||
// 允许调试
|
||||
if(Build.VERSION.SDK_INT >= 19) {
|
||||
webView1.setWebContentsDebuggingEnabled(true);
|
||||
}
|
||||
|
||||
webView1.setWebViewClient(new WebViewClient() {
|
||||
@Override
|
||||
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
||||
Log.e("OverrideUrlLoading", url);
|
||||
// 协议过滤
|
||||
if(url.startsWith("http") && !url.startsWith("https://cdn-haokanapk.baidu.com/")){
|
||||
view.loadUrl(url);
|
||||
}
|
||||
/*
|
||||
else if(url.startsWith("tbopen://")){
|
||||
Intent intent = new Intent();
|
||||
intent.setAction("android.intent.action.VIEW");
|
||||
Uri uri = Uri.parse(url);
|
||||
intent.setData(uri);
|
||||
intent.setClassName("com.taobao.taobao", "com.taobao.tao.detail.activity.DetailActivity");
|
||||
startActivity(intent);
|
||||
}
|
||||
*/
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageStarted(WebView view, String url, Bitmap favicon) {
|
||||
//super.onPageStarted(view, url, favicon);
|
||||
editText1.setText(url);
|
||||
urln = url;
|
||||
btnBack.setEnabled(true);
|
||||
IMM.hideSoftInputFromWindow(editText1.getWindowToken(), 0);
|
||||
pgb1.setVisibility(View.VISIBLE);
|
||||
imageButton_info.setImageResource(android.R.drawable.ic_menu_info_details);
|
||||
// if(favicon != null) {
|
||||
// imageButton_info.setImageBitmap(favicon);
|
||||
// }
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageFinished(WebView view, String url) {
|
||||
super.onPageFinished(view, url);
|
||||
pgb1.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
|
||||
super.onReceivedError(view, errorCode, description, failingUrl);
|
||||
Toast.makeText(getApplicationContext(), "ReceivedError:" + errorCode, Toast.LENGTH_SHORT).show();
|
||||
if (isNetworkConnected()) {
|
||||
switch(errorCode){
|
||||
case WebViewClient.ERROR_HOST_LOOKUP: // 找不到主机,跳转百度搜索
|
||||
Log.e("ErrorHostLookup", failingUrl);
|
||||
String url = "http://m.baidu.com/s?word=" + urlo;
|
||||
editText1.setText(url);
|
||||
webView1.loadUrl(url);
|
||||
urln = url;
|
||||
break;
|
||||
case WebViewClient.ERROR_UNSUPPORTED_SCHEME:
|
||||
Log.e("ErrorUnsupportedScheme",failingUrl);
|
||||
}
|
||||
} else {
|
||||
webView1.loadDataWithBaseURL(
|
||||
"",
|
||||
"<html><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\"/><style>h1{font-size:10vw;margin:40vh auto;text-align:center;}</style><h1>网络未连接</h1></html>",
|
||||
"text/html", "utf-8", "");
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
webView1.setWebChromeClient(new WebChromeClient() {
|
||||
|
||||
// JS的alert('')提示信息转换成安卓控件提示信息
|
||||
@Override
|
||||
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
|
||||
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
|
||||
result.confirm();
|
||||
return true;
|
||||
}
|
||||
|
||||
// HTML5.input.file转换为安卓文件选择器
|
||||
@Override
|
||||
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> uploadMsg, WebChromeClient.FileChooserParams fileChooserParams) {
|
||||
openFileChooserImplForAndroid5(uploadMsg);
|
||||
return true;
|
||||
}
|
||||
|
||||
// 进度条
|
||||
@Override
|
||||
public void onProgressChanged(WebView view, int newProgress) {
|
||||
super.onProgressChanged(view, newProgress);
|
||||
pgb1.setProgress(newProgress);
|
||||
if(sharedPreferences.getBoolean("switch_adBlock",true)){
|
||||
ADBlock();
|
||||
}
|
||||
if(sharedPreferences.getBoolean("switch_iframeBlock",false)) {
|
||||
if (!view.getUrl().contains("baidu.com")){
|
||||
iframeBlock();
|
||||
}
|
||||
}
|
||||
// 链接关键字屏蔽
|
||||
if(sharedPreferences.getBoolean("switch_filter",false)){
|
||||
String sf = sharedPreferences.getString("filter","");
|
||||
if(!sf.equals("")) {
|
||||
String js = "javascript:var s='"+sf+"';var sl=s.split(';');var a=document.getElementsByTagName('a');for(var i=0;i<a.length;i++){for(var j=0;j<sl.length;j++){if(a[i].textContent.indexOf(sl[j])!=-1){a[i].textContent='';}}}";
|
||||
view.loadUrl(js);
|
||||
}
|
||||
}
|
||||
// 链接关键字高亮
|
||||
if(sharedPreferences.getBoolean("switch_highlight",false)){
|
||||
String shl = sharedPreferences.getString("highlight","");
|
||||
if(!shl.equals("")) {
|
||||
String js = "javascript:var s='"+shl+"';var sl=s.split(';');var a=document.getElementsByTagName('a');for(var i=0;i<a.length;i++){for(var j=0;j<sl.length;j++){if(a[i].textContent.indexOf(sl[j])!=-1){a[i].style.color='white';a[i].style.backgroundColor='#DA3434';}}}";
|
||||
view.loadUrl(js);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取网页标题
|
||||
@Override
|
||||
public void onReceivedTitle(WebView view, String title) {
|
||||
super.onReceivedTitle(view, title);
|
||||
ptitle = title;
|
||||
}
|
||||
|
||||
// 接收网站图标(favicon)
|
||||
public void onReceivedIcon(WebView view, Bitmap icon) {
|
||||
imageButton_info.setImageBitmap(icon);
|
||||
}
|
||||
|
||||
// 播放网络视频时全屏会被调用的方法
|
||||
@Override
|
||||
public void onShowCustomView(View view, CustomViewCallback callback) {
|
||||
Log.e("onShowCustomView", "onShowCustomView");
|
||||
//Toast.makeText(getApplicationContext(), "onShowCustomView", Toast.LENGTH_SHORT).show();
|
||||
customViewCallback = callback;
|
||||
// 将video放到当前视图中
|
||||
video.addView(view);
|
||||
// 设置全屏
|
||||
setFullScreen();
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
}
|
||||
|
||||
// 视频播放退出全屏会被调用的
|
||||
@Override
|
||||
public void onHideCustomView() {
|
||||
Log.e("onHideCustomView", "onHideCustomView");
|
||||
//Toast.makeText(getApplicationContext(), "onHideCustomView", Toast.LENGTH_SHORT).show();
|
||||
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
// 退出全屏
|
||||
// quitFullScreen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
|
||||
Log.d("onConsoleMessage", consoleMessage.message() + " at " + consoleMessage.sourceId() + ":" + consoleMessage.lineNumber());
|
||||
return super.onConsoleMessage(consoleMessage);
|
||||
}
|
||||
|
||||
// 定位权限
|
||||
@Override
|
||||
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
|
||||
callback.invoke(origin, true, false);
|
||||
super.onGeolocationPermissionsShowPrompt(origin, callback);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
webView1.setDownloadListener(new MyWebViewDownLoadListener());
|
||||
|
||||
webView1.setOnTouchListener(new OnTouchListener() {
|
||||
@Override
|
||||
public boolean onTouch(View v, MotionEvent event) {
|
||||
webView1.requestFocus();
|
||||
IMM.hideSoftInputFromWindow(editText1.getWindowToken(), 0);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
newWindow();
|
||||
|
||||
editText1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
||||
@Override
|
||||
@@ -393,7 +190,7 @@ public class MainActivity extends Activity {
|
||||
@Override
|
||||
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
||||
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
||||
webView1.findAllAsync(findText.getText().toString());
|
||||
list_webView.get(currentPage).findAllAsync(findText.getText().toString());
|
||||
IMM.hideSoftInputFromWindow(findText.getWindowToken(), 0);
|
||||
return true;
|
||||
}
|
||||
@@ -408,7 +205,7 @@ public class MainActivity extends Activity {
|
||||
// start 输入框中改变后的字符串的起始位置
|
||||
// before 输入框中改变前的字符串的位置 默认为0
|
||||
// count 输入框中改变后的一共输入字符串的数量
|
||||
webView1.findAllAsync(text.toString());
|
||||
list_webView.get(currentPage).findAllAsync(text.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -433,13 +230,6 @@ public class MainActivity extends Activity {
|
||||
loadPage(sharedPreferences.getString("homepage","http://www.baidu.com"));
|
||||
}
|
||||
|
||||
webView1.setFindListener(new FindListener() {
|
||||
@Override
|
||||
public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches,
|
||||
boolean isDoneCounting) {
|
||||
findCount.setText(activeMatchOrdinal + "/" + numberOfMatches);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
CU = new Thread() {
|
||||
@@ -512,20 +302,51 @@ public class MainActivity extends Activity {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
switch (v.getId()) {
|
||||
case R.id.button_title:
|
||||
button_title.setVisibility(View.GONE);
|
||||
editText1.setVisibility(View.VISIBLE);
|
||||
break;
|
||||
case R.id.button_page:
|
||||
List<String> list_title = new ArrayList<>();
|
||||
for (int i = 0 ; i < list_webView.size() ; i++) {
|
||||
list_title.add(i + 1 + "." + list_webView.get(i).getTitle());
|
||||
}
|
||||
String[] titles = list_title.toArray(new String[list_title.size()]);
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
|
||||
builder.setTitle("窗口");
|
||||
builder.setIcon(android.R.drawable.ic_menu_slideshow);
|
||||
builder.setItems(titles, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
if(which != currentPage) {
|
||||
webViewLayout.removeAllViews();
|
||||
WebView webView = list_webView.get(which);
|
||||
webViewLayout.addView(webView);
|
||||
button_page.setText(which + 1 + "");
|
||||
button_title.setText(webView.getTitle());
|
||||
editText1.setText(webView.getUrl());
|
||||
imageButton_info.setImageBitmap(webView.getFavicon());
|
||||
currentPage = which;
|
||||
}
|
||||
}
|
||||
});
|
||||
builder.create().show();
|
||||
break;
|
||||
case R.id.ImageButtonGo:
|
||||
loadPage(editText1.getText().toString());
|
||||
break;
|
||||
case R.id.ImageButtonBack:
|
||||
if (webView1.canGoBack()) {
|
||||
webView1.goBack();
|
||||
if (list_webView.get(currentPage).canGoBack()) {
|
||||
list_webView.get(currentPage).goBack();
|
||||
btnForward.setEnabled(true);
|
||||
} else {
|
||||
btnBack.setEnabled(false);
|
||||
}
|
||||
break;
|
||||
case R.id.ImageButtonForward:
|
||||
if (webView1.canGoForward()) {
|
||||
webView1.goForward();
|
||||
if (list_webView.get(currentPage).canGoForward()) {
|
||||
list_webView.get(currentPage).goForward();
|
||||
btnBack.setEnabled(true);
|
||||
} else {
|
||||
btnForward.setEnabled(false);
|
||||
@@ -539,10 +360,10 @@ public class MainActivity extends Activity {
|
||||
imageView1.setVisibility(View.GONE);
|
||||
break;
|
||||
case R.id.findPrev:
|
||||
webView1.findNext(false);
|
||||
list_webView.get(currentPage).findNext(false);
|
||||
break;
|
||||
case R.id.findNext:
|
||||
webView1.findNext(true);
|
||||
list_webView.get(currentPage).findNext(true);
|
||||
break;
|
||||
case R.id.findClose:
|
||||
findText.setText("");
|
||||
@@ -589,7 +410,7 @@ public class MainActivity extends Activity {
|
||||
public boolean onContextItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case 0:
|
||||
webView1.loadUrl(HTRE);
|
||||
list_webView.get(currentPage).loadUrl(HTRE);
|
||||
break;
|
||||
case 1:
|
||||
ClipboardManager mClipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
@@ -614,8 +435,8 @@ public class MainActivity extends Activity {
|
||||
break;
|
||||
case 5:
|
||||
String js = "javascript:var imgs=document.getElementsByTagName('img');for(i=0;i<imgs.length;i++){if(imgs[i].src=='" + HTRE +"'){imgs[i].style.display='none';break;}}";
|
||||
webView1.loadUrl(js);
|
||||
break;
|
||||
list_webView.get(currentPage).loadUrl(js);
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -627,8 +448,8 @@ public class MainActivity extends Activity {
|
||||
imageView1.setVisibility(View.GONE);
|
||||
quitFullScreen();
|
||||
} else {
|
||||
if (webView1.canGoBack()) {
|
||||
webView1.goBack();
|
||||
if (list_webView.get(currentPage).canGoBack()) {
|
||||
list_webView.get(currentPage).goBack();
|
||||
} else {
|
||||
// moveTaskToBack(false);
|
||||
// MenuDialog();
|
||||
@@ -637,20 +458,18 @@ public class MainActivity extends Activity {
|
||||
return true;
|
||||
}
|
||||
if (keyCode == KeyEvent.KEYCODE_ENTER) {
|
||||
webView1.findAllAsync(findText.getText().toString());
|
||||
list_webView.get(currentPage).findAllAsync(findText.getText().toString());
|
||||
IMM.hideSoftInputFromWindow(findText.getWindowToken(), 0);
|
||||
return true;
|
||||
}
|
||||
if (keyCode == KeyEvent.KEYCODE_F) {
|
||||
findbar.setVisibility(View.VISIBLE);
|
||||
findText.requestFocus();
|
||||
webView1.findAllAsync(findText.getText().toString());
|
||||
list_webView.get(currentPage).findAllAsync(findText.getText().toString());
|
||||
return true;
|
||||
}
|
||||
if (keyCode == KeyEvent.KEYCODE_ESCAPE) {
|
||||
findbar.setVisibility(View.GONE);
|
||||
// Toast.makeText(getApplicationContext(), "Esc Pressed",
|
||||
// Toast.LENGTH_SHORT).show();
|
||||
return true;
|
||||
}
|
||||
return super.onKeyDown(keyCode, event);
|
||||
@@ -665,7 +484,7 @@ public class MainActivity extends Activity {
|
||||
Log.e("webview", url);
|
||||
IMM.hideSoftInputFromWindow(editText1.getWindowToken(), 0);
|
||||
findText.setText("");
|
||||
webView1.loadUrl(url);
|
||||
list_webView.get(currentPage).loadUrl(url);
|
||||
// btnBack.setEnabled(true);
|
||||
}
|
||||
|
||||
@@ -691,7 +510,7 @@ public class MainActivity extends Activity {
|
||||
}
|
||||
if (requestCode == 0) {
|
||||
if (intent != null)
|
||||
webView1.loadUrl(intent.getStringExtra("url"));
|
||||
list_webView.get(currentPage).loadUrl(intent.getStringExtra("url"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -779,16 +598,16 @@ public class MainActivity extends Activity {
|
||||
|
||||
void pauseVideo() {
|
||||
String js = "javascript:var obj = document.getElementsByTagName('video');for (var i = 0; i < obj.length; i++) { obj[i].pause();}";
|
||||
webView1.loadUrl(js);
|
||||
list_webView.get(currentPage).loadUrl(js);
|
||||
}
|
||||
|
||||
void playVideo() {
|
||||
String js = "javascript:var obj = document.getElementsByTagName('video');for (var i = 0; i < obj.length; i++) { obj[i].play();}";
|
||||
webView1.loadUrl(js);
|
||||
list_webView.get(currentPage).loadUrl(js);
|
||||
}
|
||||
|
||||
void MenuDialog() {
|
||||
String[] items = { "收藏当前页", "收藏夹", "查找", "分享", "视频独立播放", "查看源码", "主页", "全屏", "广告过滤规则", "设置", "检查更新", "关于", "退出" };
|
||||
String[] items = { "新建窗口", "关闭当前窗口", "收藏当前页", "收藏夹", "查找", "分享", "视频独立播放", "查看源码", "主页", "全屏", "广告过滤规则", "设置", "检查更新", "关于", "退出" };
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
builder.setTitle("菜单");
|
||||
builder.setIcon(R.drawable.ic_launcher);
|
||||
@@ -798,6 +617,24 @@ public class MainActivity extends Activity {
|
||||
dialog.dismiss();
|
||||
switch (which) {
|
||||
case 0:
|
||||
newWindow();
|
||||
break;
|
||||
case 1:
|
||||
list_webView.remove(currentPage);
|
||||
if(list_webView.size() == 0){
|
||||
newWindow();
|
||||
}else{
|
||||
currentPage--;
|
||||
button_page.setText(currentPage + 1 + "");
|
||||
webViewLayout.removeAllViews();
|
||||
WebView webView = list_webView.get(currentPage);
|
||||
webViewLayout.addView(webView);
|
||||
button_title.setText(webView.getTitle());
|
||||
editText1.setText(webView.getUrl());
|
||||
imageButton_info.setImageBitmap(webView.getFavicon());
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
LinearLayout layout = new LinearLayout(MainActivity.this);
|
||||
layout.setOrientation(LinearLayout.VERTICAL);
|
||||
final EditText ET_title = new EditText(MainActivity.this);
|
||||
@@ -809,7 +646,8 @@ public class MainActivity extends Activity {
|
||||
ET_url.setText(urln);
|
||||
layout.addView(ET_url);
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
|
||||
Bitmap icon = webView1.getFavicon();
|
||||
Bitmap icon = list_webView.get(currentPage).getFavicon();
|
||||
if(icon == null) icon = BitmapFactory.decodeResource(getResources(), R.drawable.network);
|
||||
Matrix matrix = new Matrix();
|
||||
matrix.postScale((float)100/icon.getWidth(), (float)100/icon.getHeight());
|
||||
Bitmap bitmap = Bitmap.createBitmap(icon, 0, 0, icon.getWidth(), icon.getHeight(), matrix, true);
|
||||
@@ -884,53 +722,53 @@ public class MainActivity extends Activity {
|
||||
});
|
||||
builder.create().show();
|
||||
break;
|
||||
case 1:
|
||||
case 3:
|
||||
Intent intent = new Intent(MainActivity.this, FavoriteActivity.class);
|
||||
startActivityForResult(intent, 0);
|
||||
break;
|
||||
case 2:
|
||||
case 4:
|
||||
findbar.setVisibility(View.VISIBLE);
|
||||
findText.requestFocus();
|
||||
webView1.findAllAsync(findText.getText().toString());
|
||||
list_webView.get(currentPage).findAllAsync(findText.getText().toString());
|
||||
break;
|
||||
case 3:
|
||||
case 5:
|
||||
intent = new Intent();
|
||||
intent.setAction(Intent.ACTION_SEND);
|
||||
intent.putExtra(Intent.EXTRA_TEXT, webView1.getUrl());
|
||||
intent.putExtra(Intent.EXTRA_TEXT, list_webView.get(currentPage).getUrl());
|
||||
intent.setType("text/plain");
|
||||
startActivity(Intent.createChooser(intent, "分享"));
|
||||
break;
|
||||
case 4:
|
||||
String js = "javascript:var videos=document.getElementsByTagName('video'); if(videos.length!=0){ var url=videos[0].src; document.body.innerHTML=''; var video=document.createElement('video'); video.style.width='100%'; video.style.height='auto'; video.src=url; video.controls=true; document.body.appendChild(video); var a=document.createElement('a'); a.textContent=url; a.href=url; document.body.appendChild(a); } else { var iframes=document.getElementsByTagName('iframe'); if(iframes.length!=0) window.location.href=iframes[0].src; }";
|
||||
webView1.loadUrl(js);
|
||||
break;
|
||||
case 5:
|
||||
js = "javascript:var s='<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>'; document.body.innerHTML=''; var pre=document.createElement('pre'); document.body.appendChild(pre); pre.textContent=s;";
|
||||
webView1.loadUrl(js);
|
||||
break;
|
||||
case 6:
|
||||
webView1.loadUrl(sharedPreferences.getString("homepage",""));
|
||||
String js = "javascript:var videos=document.getElementsByTagName('video'); if(videos.length!=0){ var url=videos[0].src; document.body.innerHTML=''; var video=document.createElement('video'); video.style.width='100%'; video.style.height='auto'; video.src=url; video.controls=true; document.body.appendChild(video); var a=document.createElement('a'); a.textContent=url; a.href=url; document.body.appendChild(a); } else { var iframes=document.getElementsByTagName('iframe'); if(iframes.length!=0) window.location.href=iframes[0].src; }";
|
||||
list_webView.get(currentPage).loadUrl(js);
|
||||
break;
|
||||
case 7:
|
||||
js = "javascript:var s='<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>'; document.body.innerHTML=''; var pre=document.createElement('pre'); document.body.appendChild(pre); pre.textContent=s;";
|
||||
list_webView.get(currentPage).loadUrl(js);
|
||||
break;
|
||||
case 8:
|
||||
list_webView.get(currentPage).loadUrl(sharedPreferences.getString("homepage",""));
|
||||
break;
|
||||
case 9:
|
||||
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||
LinearLayout2.setVisibility(View.GONE);
|
||||
pgb1.setVisibility(View.GONE);
|
||||
isFullScreen = true;
|
||||
break;
|
||||
case 8:
|
||||
case 10:
|
||||
DialogBlockList();
|
||||
break;
|
||||
case 9:
|
||||
case 11:
|
||||
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
|
||||
break;
|
||||
case 10:
|
||||
case 12:
|
||||
isManualCheckUpdate = true;
|
||||
new Thread(CU).start();
|
||||
break;
|
||||
case 11:
|
||||
webView1.loadUrl("file:///android_asset/about.htm");
|
||||
case 13:
|
||||
list_webView.get(currentPage).loadUrl("file:///android_asset/about.htm");
|
||||
break;
|
||||
case 12:
|
||||
case 14:
|
||||
MainActivity.this.finish();
|
||||
break;
|
||||
}
|
||||
@@ -1090,6 +928,7 @@ public class MainActivity extends Activity {
|
||||
ostream.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return "";
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -1104,13 +943,13 @@ public class MainActivity extends Activity {
|
||||
rules = rules.substring(0, rules.length() - 1);
|
||||
}
|
||||
String js = "javascript:var rules='" + rules + "';var rule=new Array();rule=rules.split(',');var img=document.getElementsByTagName('img');for(i=0;i<img.length;i++){for(j=0;j<rule.length;j++){if(img[i].src.indexOf(rule[j])!=-1){img[i].style.display='none';}}}";
|
||||
webView1.loadUrl(js);
|
||||
list_webView.get(currentPage).loadUrl(js);
|
||||
}
|
||||
}
|
||||
|
||||
void iframeBlock(){
|
||||
String js = "javascript:var iframes=document.getElementsByTagName('iframe');for(i=0;i<iframes.length;i++){iframes[i].style.display='none';}document.getElementById('win-pop-foot').style.display='none';document.getElementById('win-pop-foot1').style.display='none';";
|
||||
webView1.loadUrl(js);
|
||||
list_webView.get(currentPage).loadUrl(js);
|
||||
}
|
||||
|
||||
// 调用系统下载,https://www.jianshu.com/p/6e38e1ef203a
|
||||
@@ -1124,7 +963,7 @@ public class MainActivity extends Activity {
|
||||
// 设置通知栏的标题,如果不设置,默认使用文件名
|
||||
// request.setTitle("This is title");
|
||||
// 设置通知栏的描述
|
||||
request.setDescription(surl);
|
||||
request.setDescription(surl);
|
||||
// 允许在计费流量下下载
|
||||
request.setAllowedOverMetered(false);
|
||||
// 允许该记录在下载管理界面可见
|
||||
@@ -1163,8 +1002,8 @@ public class MainActivity extends Activity {
|
||||
Log.e("Version", versionS + " > " + versionL + " ?");
|
||||
String[] AVersionS = versionS.split("\\.");
|
||||
String[] AVersionL = versionL.split("\\.");
|
||||
//Log.e("Version: ", AVersionS[0] + " > " + AVersionL[0] + " ? " + AVersionS[1] + " > " + AVersionL[1] + " ?");
|
||||
if ((Integer.parseInt(AVersionS[0]) > Integer.parseInt(AVersionL[0])) || (Integer.parseInt(AVersionS[1]) > Integer.parseInt(AVersionL[1]))) {
|
||||
Log.e("Version", "(" + AVersionS[0] + " > " + AVersionL[0] + ") && (" + AVersionS[1] + " > " + AVersionL[1] + ")");
|
||||
if ((Integer.parseInt(AVersionS[0]) > Integer.parseInt(AVersionL[0])) || (Integer.parseInt(AVersionS[0]) == Integer.parseInt(AVersionL[0]) && Integer.parseInt(AVersionS[1]) > Integer.parseInt(AVersionL[1]))) {
|
||||
Looper.prepare();
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
|
||||
builder.setIcon(R.drawable.ic_launcher);
|
||||
@@ -1221,4 +1060,240 @@ public class MainActivity extends Activity {
|
||||
}
|
||||
}
|
||||
|
||||
void newWindow(){
|
||||
WebView webView = new WebView(MainActivity.this);
|
||||
setWebView(webView);
|
||||
webView.loadUrl(sharedPreferences.getString("homepage","http://www.baidu.com"));
|
||||
webViewLayout.removeAllViews();
|
||||
webViewLayout.addView(webView);
|
||||
list_webView.add(webView);
|
||||
currentPage = list_webView.size() - 1;
|
||||
button_page.setText(currentPage + 1 + "");
|
||||
list_webView.get(currentPage).setFindListener(new FindListener() {
|
||||
@Override
|
||||
public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) {
|
||||
findCount.setText(activeMatchOrdinal + "/" + numberOfMatches);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void setWebView(final WebView webView){
|
||||
// 支持获取手势焦点
|
||||
webView.requestFocusFromTouch();
|
||||
// 允许调试
|
||||
if(Build.VERSION.SDK_INT >= 19) {
|
||||
webView.setWebContentsDebuggingEnabled(true);
|
||||
}
|
||||
WebSettings webSettings = webView.getSettings();
|
||||
// 开启JS
|
||||
webSettings.setJavaScriptEnabled(true);
|
||||
// 开启JS能打开窗口
|
||||
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
|
||||
// 开启缓存
|
||||
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
|
||||
// 自适应屏幕
|
||||
webSettings.setUseWideViewPort(true);
|
||||
webSettings.setLoadWithOverviewMode(true);
|
||||
// 支持缩放
|
||||
webSettings.setSupportZoom(true);
|
||||
// 启用内置缩放控件
|
||||
webSettings.setBuiltInZoomControls(true);
|
||||
// 隐藏缩放控件
|
||||
webSettings.setDisplayZoomControls(false);
|
||||
// 开启访问文件
|
||||
webSettings.setAllowFileAccess(true);
|
||||
// 开启数据库
|
||||
webSettings.setDatabaseEnabled(true);
|
||||
// 开启localStorage
|
||||
webSettings.setDomStorageEnabled(true);
|
||||
// 开启定位
|
||||
webSettings.setGeolocationEnabled(true);
|
||||
// 支持多窗口
|
||||
webSettings.supportMultipleWindows();
|
||||
// 允许跨域
|
||||
webSettings.setAllowUniversalAccessFromFileURLs(true);
|
||||
|
||||
webView.setWebViewClient(new WebViewClient() {
|
||||
@Override
|
||||
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
||||
Log.e("OverrideUrlLoading", url);
|
||||
// 协议过滤
|
||||
if(url.startsWith("http") && !url.startsWith("https://cdn-haokanapk.baidu.com/")){
|
||||
view.loadUrl(url);
|
||||
}
|
||||
/*
|
||||
else if(url.startsWith("tbopen://")){
|
||||
Intent intent = new Intent();
|
||||
intent.setAction("android.intent.action.VIEW");
|
||||
Uri uri = Uri.parse(url);
|
||||
intent.setData(uri);
|
||||
intent.setClassName("com.taobao.taobao", "com.taobao.tao.detail.activity.DetailActivity");
|
||||
startActivity(intent);
|
||||
}
|
||||
*/
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageStarted(WebView view, String url, Bitmap favicon) {
|
||||
//super.onPageStarted(view, url, favicon);
|
||||
button_title.setText(url);
|
||||
button_title.setVisibility(View.VISIBLE);
|
||||
editText1.setText(url);
|
||||
editText1.setVisibility(View.GONE);
|
||||
urln = url;
|
||||
btnBack.setEnabled(true);
|
||||
IMM.hideSoftInputFromWindow(editText1.getWindowToken(), 0);
|
||||
pgb1.setVisibility(View.VISIBLE);
|
||||
imageButton_info.setImageResource(android.R.drawable.ic_menu_info_details);
|
||||
// if(favicon != null) {
|
||||
// imageButton_info.setImageBitmap(favicon);
|
||||
// }
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageFinished(WebView view, String url) {
|
||||
super.onPageFinished(view, url);
|
||||
pgb1.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
|
||||
super.onReceivedError(view, errorCode, description, failingUrl);
|
||||
Toast.makeText(getApplicationContext(), "ReceivedError:" + errorCode, Toast.LENGTH_SHORT).show();
|
||||
if (isNetworkConnected()) {
|
||||
switch(errorCode){
|
||||
case WebViewClient.ERROR_HOST_LOOKUP: // 找不到主机,跳转百度搜索
|
||||
Log.e("ErrorHostLookup", failingUrl);
|
||||
String url = "http://m.baidu.com/s?word=" + urlo;
|
||||
editText1.setText(url);
|
||||
webView.loadUrl(url);
|
||||
urln = url;
|
||||
break;
|
||||
case WebViewClient.ERROR_UNSUPPORTED_SCHEME:
|
||||
Log.e("ErrorUnsupportedScheme",failingUrl);
|
||||
}
|
||||
} else {
|
||||
webView.loadDataWithBaseURL(
|
||||
"",
|
||||
"<html><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\"/><style>h1{font-size:10vw;margin:40vh auto;text-align:center;}</style><h1>网络未连接</h1></html>",
|
||||
"text/html", "utf-8", "");
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
webView.setWebChromeClient(new WebChromeClient() {
|
||||
|
||||
// JS的alert('')提示信息转换成安卓控件提示信息
|
||||
@Override
|
||||
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
|
||||
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
|
||||
result.confirm();
|
||||
return true;
|
||||
}
|
||||
|
||||
// HTML5.input.file转换为安卓文件选择器
|
||||
@Override
|
||||
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> uploadMsg, WebChromeClient.FileChooserParams fileChooserParams) {
|
||||
openFileChooserImplForAndroid5(uploadMsg);
|
||||
return true;
|
||||
}
|
||||
|
||||
// 进度条
|
||||
@Override
|
||||
public void onProgressChanged(WebView view, int newProgress) {
|
||||
super.onProgressChanged(view, newProgress);
|
||||
pgb1.setProgress(newProgress);
|
||||
if(sharedPreferences.getBoolean("switch_adBlock",true)){
|
||||
ADBlock();
|
||||
}
|
||||
if(sharedPreferences.getBoolean("switch_iframeBlock",false)) {
|
||||
if (!view.getUrl().contains("baidu.com")){
|
||||
iframeBlock();
|
||||
}
|
||||
}
|
||||
// 链接关键字屏蔽
|
||||
if(sharedPreferences.getBoolean("switch_filter",false)){
|
||||
String sf = sharedPreferences.getString("filter","");
|
||||
if(!sf.equals("")) {
|
||||
String js = "javascript:var s='"+sf+"';var sl=s.split(';');var a=document.getElementsByTagName('a');for(var i=0;i<a.length;i++){for(var j=0;j<sl.length;j++){if(a[i].textContent.indexOf(sl[j])!=-1){a[i].textContent='';}}}";
|
||||
view.loadUrl(js);
|
||||
}
|
||||
}
|
||||
// 链接关键字高亮
|
||||
if(sharedPreferences.getBoolean("switch_highlight",false)){
|
||||
String shl = sharedPreferences.getString("highlight","");
|
||||
if(!shl.equals("")) {
|
||||
String js = "javascript:var s='"+shl+"';var sl=s.split(';');var a=document.getElementsByTagName('a');for(var i=0;i<a.length;i++){for(var j=0;j<sl.length;j++){if(a[i].textContent.indexOf(sl[j])!=-1){a[i].style.color='white';a[i].style.backgroundColor='#DA3434';}}}";
|
||||
view.loadUrl(js);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取网页标题
|
||||
@Override
|
||||
public void onReceivedTitle(WebView view, String title) {
|
||||
super.onReceivedTitle(view, title);
|
||||
button_title.setText(title);
|
||||
ptitle = title;
|
||||
}
|
||||
|
||||
// 接收网站图标(favicon)
|
||||
public void onReceivedIcon(WebView view, Bitmap icon) {
|
||||
imageButton_info.setImageBitmap(icon);
|
||||
}
|
||||
|
||||
// 播放网络视频时全屏会被调用的方法
|
||||
@Override
|
||||
public void onShowCustomView(View view, CustomViewCallback callback) {
|
||||
Log.e("onShowCustomView", "onShowCustomView");
|
||||
//Toast.makeText(getApplicationContext(), "onShowCustomView", Toast.LENGTH_SHORT).show();
|
||||
customViewCallback = callback;
|
||||
// 将video放到当前视图中
|
||||
video.addView(view);
|
||||
// 设置全屏
|
||||
setFullScreen();
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
}
|
||||
|
||||
// 视频播放退出全屏会被调用的
|
||||
@Override
|
||||
public void onHideCustomView() {
|
||||
Log.e("onHideCustomView", "onHideCustomView");
|
||||
//Toast.makeText(getApplicationContext(), "onHideCustomView", Toast.LENGTH_SHORT).show();
|
||||
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
// 退出全屏
|
||||
// quitFullScreen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
|
||||
Log.d("onConsoleMessage", consoleMessage.message() + " at " + consoleMessage.sourceId() + ":" + consoleMessage.lineNumber());
|
||||
return super.onConsoleMessage(consoleMessage);
|
||||
}
|
||||
|
||||
// 定位权限
|
||||
@Override
|
||||
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
|
||||
callback.invoke(origin, true, false);
|
||||
super.onGeolocationPermissionsShowPrompt(origin, callback);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
webView.setDownloadListener(new MyWebViewDownLoadListener());
|
||||
|
||||
webView.setOnTouchListener(new OnTouchListener() {
|
||||
@Override
|
||||
public boolean onTouch(View v, MotionEvent event) {
|
||||
button_title.setVisibility(View.VISIBLE);
|
||||
editText1.setVisibility(View.GONE);
|
||||
webView.requestFocus();
|
||||
IMM.hideSoftInputFromWindow(editText1.getWindowToken(), 0);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user