优化地址栏和错误信息
This commit is contained in:
@@ -2,11 +2,13 @@ package com.hty.browser;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
@@ -15,6 +17,7 @@ import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -128,6 +131,7 @@ public class MainActivity extends Activity {
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE };
|
||||
private static int REQUEST_PERMISSION_CODE = 1;
|
||||
Boolean isExit = false;
|
||||
ClipboardManager clipboardManager;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -139,6 +143,7 @@ public class MainActivity extends Activity {
|
||||
}
|
||||
}
|
||||
|
||||
clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
|
||||
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
IMM = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "HTYBrowser";
|
||||
@@ -245,6 +250,8 @@ public class MainActivity extends Activity {
|
||||
IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
|
||||
registerReceiver(receiver, intentFilter);
|
||||
|
||||
Log.e(Thread.currentThread().getStackTrace()[2] + "", "Intent(" + getIntent() + ")");
|
||||
}
|
||||
|
||||
private void setFullScreen() {
|
||||
@@ -319,7 +326,7 @@ public class MainActivity extends Activity {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
if(which != currentPage) {
|
||||
if (which != currentPage) {
|
||||
webViewLayout.removeAllViews();
|
||||
WebView webView = list_webView.get(which);
|
||||
webViewLayout.addView(webView);
|
||||
@@ -394,16 +401,18 @@ public class MainActivity extends Activity {
|
||||
if (result.getType() == HitTestResult.IMAGE_TYPE || result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
|
||||
menu.setHeaderIcon(android.R.drawable.ic_menu_gallery);
|
||||
menu.add(0, 0, 0, "查看图片");
|
||||
//menu.add(0, 1, 1, "复制图片");
|
||||
//menu.add(0, 1, 1, "复制图片"); // 未实现
|
||||
menu.add(0, 2, 2, "保存图片").setIcon(android.R.drawable.ic_menu_save); // Context menu items do not support icons
|
||||
menu.add(0, 3, 3, "复制链接");
|
||||
menu.add(0, 4, 4, "屏蔽图片");
|
||||
menu.add(0, 5, 5, "隐藏图片");
|
||||
menu.add(0, 4, 4, "分享");
|
||||
menu.add(0, 5, 5, "屏蔽图片");
|
||||
menu.add(0, 6, 6, "隐藏图片");
|
||||
} else if (result.getType() == HitTestResult.SRC_ANCHOR_TYPE) {
|
||||
menu.setHeaderIcon(R.drawable.link);
|
||||
menu.add(0, 0, 0, "在新窗口打开链接");
|
||||
menu.add(0, 2, 2, "下载");
|
||||
menu.add(0, 3, 3, "复制链接");
|
||||
menu.add(0, 4, 4, "分享");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -414,29 +423,34 @@ public class MainActivity extends Activity {
|
||||
newWindow(HTRE);
|
||||
break;
|
||||
case 1:
|
||||
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
ContentValues values = new ContentValues(2);
|
||||
values.put(MediaStore.Images.Media.MIME_TYPE, "image/*");
|
||||
values.put(MediaStore.Images.Media.DATA, HTRE);
|
||||
Uri imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
|
||||
ClipData clipData = ClipData.newUri(getContentResolver(), "Image", imageUri);
|
||||
Bitmap bitmap = null;
|
||||
// HTRE -> bitmap
|
||||
Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(), bitmap, "HTYBrowser", HTRE));
|
||||
ClipData clipData = ClipData.newUri(getContentResolver(), "Image", uri);
|
||||
clipboardManager.setPrimaryClip(clipData);
|
||||
break;
|
||||
case 2:
|
||||
String mime = URLConnection.getFileNameMap().getContentTypeFor(HTRE);
|
||||
Log.e(Thread.currentThread().getStackTrace()[2] + " ", "" + mime);
|
||||
if (mime == null) mime = "";
|
||||
if (mime == null)
|
||||
mime = "";
|
||||
dialog_download(HTRE, "", mime, 0);
|
||||
break;
|
||||
case 3:
|
||||
clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
|
||||
clipboardManager.setPrimaryClip(ClipData.newPlainText("link", HTRE));
|
||||
Toast.makeText(getApplicationContext(), "链接已复制", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case 4:
|
||||
DialogBlock(HTRE);
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(Intent.ACTION_SEND);
|
||||
intent.putExtra(Intent.EXTRA_TEXT, HTRE);
|
||||
intent.setType("text/plain");
|
||||
startActivity(Intent.createChooser(intent, "分享"));
|
||||
break;
|
||||
case 5:
|
||||
DialogBlock(HTRE);
|
||||
break;
|
||||
case 6:
|
||||
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;}}";
|
||||
list_webView.get(currentPage).loadUrl(js);
|
||||
break;
|
||||
@@ -454,6 +468,19 @@ public class MainActivity extends Activity {
|
||||
} else {
|
||||
if (list_webView.get(currentPage).canGoBack()) {
|
||||
list_webView.get(currentPage).goBack();
|
||||
} else if (currentPage > 0) {
|
||||
list_webView.remove(currentPage);
|
||||
Log.e(Thread.currentThread().getStackTrace()[2] + "", currentPage + "");
|
||||
if (currentPage == list_webView.size())
|
||||
currentPage--;
|
||||
button_page.setText(currentPage + 1 + "");
|
||||
webViewLayout.removeAllViews();
|
||||
Log.e(Thread.currentThread().getStackTrace()[2] + "", currentPage + "");
|
||||
WebView webView = list_webView.get(currentPage);
|
||||
webViewLayout.addView(webView);
|
||||
button_title.setText(webView.getTitle());
|
||||
editText_url.setText(webView.getUrl());
|
||||
button_info.setImageBitmap(webView.getFavicon());
|
||||
} else {
|
||||
exitBy2Click();
|
||||
}
|
||||
@@ -511,12 +538,17 @@ public class MainActivity extends Activity {
|
||||
}
|
||||
mUploadMessageForAndroid5 = null;
|
||||
}
|
||||
if (requestCode == 0) {
|
||||
if (intent != null)
|
||||
if (intent.getBooleanExtra("newWindow", false))
|
||||
newWindow(intent.getStringExtra("url"));
|
||||
else
|
||||
list_webView.get(currentPage).loadUrl(intent.getStringExtra("url"));
|
||||
if (requestCode == 0) { //收藏夹返回
|
||||
if (intent != null) {
|
||||
String surl = intent.getStringExtra("url");
|
||||
if (intent.getBooleanExtra("newWindow", false)) {
|
||||
newWindow(surl);
|
||||
} else {
|
||||
list_webView.get(currentPage).loadUrl(surl);
|
||||
button_title.setText(surl);
|
||||
editText_url.setText(surl);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (requestCode == FILECHOOSER_DOWNLOAD_PATH) {
|
||||
if (resultCode == Activity.RESULT_OK) { //是否选择,没选择就不会继续
|
||||
@@ -641,7 +673,7 @@ public class MainActivity extends Activity {
|
||||
}
|
||||
|
||||
private void MenuDialog() {
|
||||
String[] items = { "新建窗口", "关闭当前窗口", "收藏当前页", "收藏夹", "查找", "分享", "视频独立播放", "视频截图", "视频在播放器中打开", "查看源码", "主页", "全屏", "广告过滤规则", "设置", "检查更新", "关于", "退出", "清除缓存" };
|
||||
String[] items = { "新建窗口", "关闭当前窗口", "收藏当前页", "收藏夹", "查找", "分享", "视频独立播放", "视频截图", "视频在播放器中打开", "查看源码", "保存", "全屏", "广告过滤规则", "设置", "检查更新", "关于", "退出", "清除缓存" };
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
builder.setTitle("菜单");
|
||||
//builder.setIcon(android.R.drawable.ic_menu_preferences);
|
||||
@@ -659,8 +691,8 @@ public class MainActivity extends Activity {
|
||||
if (list_webView.size() == 0) {
|
||||
newWindow(sharedPreferences.getString("homepage","http://www.baidu.com"));
|
||||
} else {
|
||||
currentPage--;
|
||||
if (currentPage < 0) currentPage = 0;
|
||||
if (currentPage == list_webView.size())
|
||||
currentPage--;
|
||||
button_page.setText(currentPage + 1 + "");
|
||||
webViewLayout.removeAllViews();
|
||||
WebView webView = list_webView.get(currentPage);
|
||||
@@ -827,7 +859,7 @@ public class MainActivity extends Activity {
|
||||
@Override
|
||||
public void onReceiveValue(String value) {
|
||||
Log.e(Thread.currentThread().getStackTrace()[2] + "", "" + value);
|
||||
if(!value.equals("null")) {
|
||||
if (!value.equals("null")) {
|
||||
try {
|
||||
Intent intent1 = new Intent(Intent.ACTION_VIEW);
|
||||
String type = "video/*";
|
||||
@@ -842,12 +874,20 @@ public class MainActivity extends Activity {
|
||||
}
|
||||
});
|
||||
break;
|
||||
case 9://改多标签后失效
|
||||
case 9://查看源码改多标签后失效
|
||||
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 10:
|
||||
list_webView.get(currentPage).loadUrl(sharedPreferences.getString("homepage",""));
|
||||
js = "javascript:function save(){var s='<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>'; return s;}";
|
||||
list_webView.get(currentPage).loadUrl(js);
|
||||
list_webView.get(currentPage).evaluateJavascript("javascript:save()", new ValueCallback<String>() {
|
||||
@Override
|
||||
public void onReceiveValue(String value) {
|
||||
Log.e(Thread.currentThread().getStackTrace()[2] + "", "" + value);
|
||||
writeFile(button_title.getText().toString() + ".htm", value);
|
||||
}
|
||||
});
|
||||
break;
|
||||
case 11:
|
||||
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||
@@ -900,7 +940,7 @@ public class MainActivity extends Activity {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!exist) {
|
||||
if (!exist) {
|
||||
addRule(rule);
|
||||
dialogRule.dismiss();
|
||||
}
|
||||
@@ -949,7 +989,7 @@ public class MainActivity extends Activity {
|
||||
void addRule(String content){
|
||||
FileOutputStream outStream = null;
|
||||
try {
|
||||
outStream = this.openFileOutput("blockrules", Context.MODE_APPEND);
|
||||
outStream = openFileOutput("blockrules", Context.MODE_APPEND);
|
||||
outStream.write((content+"\n").getBytes());
|
||||
outStream.close();
|
||||
ADBlock();
|
||||
@@ -1170,6 +1210,8 @@ public class MainActivity extends Activity {
|
||||
list_webView.add(webView);
|
||||
currentPage = list_webView.size() - 1;
|
||||
button_page.setText(currentPage + 1 + "");
|
||||
button_title.setText(surl);
|
||||
editText_url.setText(surl);
|
||||
}
|
||||
|
||||
void settingWebView(WebView webView) {
|
||||
@@ -1207,16 +1249,12 @@ public class MainActivity extends Activity {
|
||||
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
||||
Log.e(Thread.currentThread().getStackTrace()[2] + "", url);
|
||||
// 协议过滤
|
||||
if (url.startsWith("http") || url.startsWith("ftp://") && !url.startsWith("https://cdn-haokanapk.baidu.com/")) {
|
||||
if (url.startsWith("https://baiduyunbo.com/?id=")) {
|
||||
String id = url.replace("https://baiduyunbo.com/?id=","");
|
||||
view.loadUrl("https://m3u8.cdnpan.com/"+ id + ".m3u8");
|
||||
} else {
|
||||
view.loadUrl(url);
|
||||
}
|
||||
return false;
|
||||
if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("ftp://") && !url.startsWith("https://cdn-haokanapk.baidu.com/")) {
|
||||
view.loadUrl(url);
|
||||
return false; // continue load
|
||||
} else {
|
||||
return true; // abort load
|
||||
}
|
||||
return true; //不跳转
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1246,35 +1284,32 @@ public class MainActivity extends Activity {
|
||||
@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()) {
|
||||
//Toast.makeText(getApplicationContext(), "ReceivedError:" + errorCode, Toast.LENGTH_SHORT).show();
|
||||
//if (isNetworkConnected()) {
|
||||
switch (errorCode) {
|
||||
case WebViewClient.ERROR_HOST_LOOKUP: // 找不到主机,跳转百度搜索
|
||||
// case WebViewClient.ERROR_HOST_LOOKUP: // 找不到主机,跳转百度搜索
|
||||
// Log.e(Thread.currentThread().getStackTrace()[2] + "", failingUrl);
|
||||
// String url = "http://m.baidu.com/s?word=" + urlo;
|
||||
// editText_url.setText(url);
|
||||
// view.loadUrl(url);
|
||||
// urln = url;
|
||||
// break;
|
||||
default:
|
||||
Log.e(Thread.currentThread().getStackTrace()[2] + "", failingUrl);
|
||||
String url = "http://m.baidu.com/s?word=" + urlo;
|
||||
editText_url.setText(url);
|
||||
view.loadUrl(url);
|
||||
urln = url;
|
||||
break;
|
||||
case WebViewClient.ERROR_UNSUPPORTED_SCHEME:
|
||||
Log.e(Thread.currentThread().getStackTrace()[2] + "",failingUrl);
|
||||
view.loadDataWithBaseURL(failingUrl, "<html><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\"/><title>" + failingUrl + "</title><body><h1>无法访问此网站</h1><p>" + failingUrl + " 无法访问,因为:<br>" + description + "</p></body></html>", "text/html", "utf-8", "");
|
||||
}
|
||||
} else {
|
||||
view.loadDataWithBaseURL(
|
||||
"",
|
||||
"<html><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\"/><title>网络未连接</title><style>h1{font-size:10vw;margin:40vh auto;text-align:center;}</style><body><h1>网络未连接</h1></body></html>",
|
||||
"text/html", "utf-8", "");
|
||||
}
|
||||
//} else {
|
||||
// view.loadDataWithBaseURL("", "<html><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\"/><title>网络未连接</title><style>h1{font-size:10vw;margin:40vh auto;text-align:center;}</style><body><h1>网络未连接</h1></body></html>", "text/html", "utf-8", "");
|
||||
//}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
webView.setFindListener(new FindListener() {
|
||||
@Override
|
||||
public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) {
|
||||
if(numberOfMatches == 0){
|
||||
if (numberOfMatches == 0) {
|
||||
textView_searchCount.setText(activeMatchOrdinal + "/" + numberOfMatches);
|
||||
}else{
|
||||
} else {
|
||||
textView_searchCount.setText(activeMatchOrdinal + 1 + "/" + numberOfMatches);
|
||||
}
|
||||
}
|
||||
@@ -1330,7 +1365,7 @@ public class MainActivity extends Activity {
|
||||
}
|
||||
}
|
||||
// 链接关键字屏蔽
|
||||
if(sharedPreferences.getBoolean("switch_filter", false)){
|
||||
if (sharedPreferences.getBoolean("switch_filter", false)) {
|
||||
String sf = sharedPreferences.getString("filter", "");
|
||||
//Log.e(Thread.currentThread().getStackTrace()[2] + "", "" + sf);
|
||||
if(!sf.equals("")) {
|
||||
@@ -1343,7 +1378,7 @@ public class MainActivity extends Activity {
|
||||
String shl = sharedPreferences.getString("highlight", "");
|
||||
//Log.e(Thread.currentThread().getStackTrace()[2] + "", "" + shl);
|
||||
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';}}}";
|
||||
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';a[i].innerHTML=a[i].textContent;}}}";
|
||||
view.loadUrl(js);
|
||||
}
|
||||
}
|
||||
@@ -1403,10 +1438,10 @@ public class MainActivity extends Activity {
|
||||
super.onGeolocationPermissionsShowPrompt(origin, callback);
|
||||
}
|
||||
|
||||
// target="_blank" 处理
|
||||
// target="_blank" 处理,Android 11 崩溃无Log
|
||||
@Override
|
||||
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
|
||||
if(isUserGesture) {
|
||||
if (isUserGesture) {
|
||||
WebView webView = new WebView(MainActivity.this);
|
||||
settingWebView(webView);
|
||||
webViewLayout.removeAllViews();
|
||||
@@ -1417,8 +1452,10 @@ public class MainActivity extends Activity {
|
||||
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
|
||||
transport.setWebView(webView);
|
||||
resultMsg.sendToTarget();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1429,20 +1466,16 @@ public class MainActivity extends Activity {
|
||||
urln = intent.getDataString();
|
||||
newWindow(urln);
|
||||
} else {
|
||||
newWindow(sharedPreferences.getString("homepage","http://www.baidu.com"));
|
||||
newWindow(sharedPreferences.getString("homepage", "http://www.baidu.com"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onNewIntent(Intent intent) {
|
||||
//Log.e(Thread.currentThread().getStackTrace()[2] + "", "onNewIntent(" + getIntent() + ")");
|
||||
Log.e(Thread.currentThread().getStackTrace()[2] + "", "onNewIntent(" + getIntent() + ")");
|
||||
super.onNewIntent(intent);
|
||||
setIntent(intent);
|
||||
if (getIntent().getDataString() != null) {
|
||||
urln = getIntent().getDataString();
|
||||
Log.e(Thread.currentThread().getStackTrace()[2] + "", "onNewIntent(" + urln + ")");
|
||||
newWindow(urln);
|
||||
}
|
||||
getDataFromIntent(intent);
|
||||
}
|
||||
|
||||
void dialog_download(final String surl, String contentDisposition, String mimeType, long contentLength){
|
||||
@@ -1452,7 +1485,7 @@ public class MainActivity extends Activity {
|
||||
final EditText editText_download_filename = (EditText) view.findViewById(R.id.editText_download_filename);
|
||||
String filename = surl.substring(surl.lastIndexOf("/") + 1);
|
||||
Log.e(Thread.currentThread().getStackTrace()[2] + " ", "FileName: " + filename);
|
||||
if (filename.contains("&f=JPEG?")) {//百度网页图片重命名
|
||||
if (filename.contains("&f=JPEG?")) { //百度网页图片重命名
|
||||
SimpleDateFormat SDF = new SimpleDateFormat("yyyyMMddHHmmss");
|
||||
Date date = new Date();
|
||||
String stime = SDF.format(date);
|
||||
@@ -1561,4 +1594,19 @@ public class MainActivity extends Activity {
|
||||
}
|
||||
}
|
||||
|
||||
void writeFile(String filename, String s) {
|
||||
File file = new File(MainActivity.dir, filename);
|
||||
try {
|
||||
BufferedWriter BW = new BufferedWriter(new FileWriter(file, false)); //false覆盖
|
||||
BW.write(s);
|
||||
BW.flush();
|
||||
String filepath = MainActivity.dir + File.separator + filename;
|
||||
MediaScannerConnection.scanFile(MainActivity.this, new String[] { filepath }, null, null);
|
||||
Toast.makeText(MainActivity.this, "写文件 " + MainActivity.dir + File.separator + filename + " 成功", Toast.LENGTH_SHORT).show();
|
||||
} catch (Exception e) {
|
||||
Log.e(Thread.currentThread().getStackTrace()[2] + "", e.toString());
|
||||
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user