package com.jsxf.jscqmlbmb.View;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.jsxf.jscqmlbmb.Controller.view.BaseFragment;
import com.jsxf.jscqmlbmb.Model.Final.OnlyID;
import com.jsxf.jscqmlbmb.R;
import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;
/**
* Created by Administrator on 2016/11/30 0030.
* Web页面
*/
public class WebFragment extends BaseFragment {
private WebView webView;
private View view;
private ValueCallback<Uri> uploadFile;
private ValueCallback<Uri[]> uploadFiles;
@Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (null != view) {
ViewGroup parent = (ViewGroup) view.getParent();
if (null != parent) {
parent.removeView(view);
}
}
else {
view = inflater.inflate(R.layout.fragment_web, container, false);
if(getResources().getString(R.string.test_url) != null)
{
if(!getResources().getString(R.string.test_url).isEmpty())
{
ViewIni(view,getResources().getString(R.string.test_url));// 控件初始化
}
else
{
ViewIni(view, "http://OnlyID.NTEPARAMETER(getActivity()));
}
}
}
webView.setWebChromeClient(new WebChromeClient() {
// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Log.i("test", "openFileChooser 1");
uploadFile = uploadFile;
openFileChooseProcess();
}
// For Android < 3.0
public void openFileChooser(ValueCallback<Uri> uploadMsgs) {
Log.i("test", "openFileChooser 2");
uploadFile = uploadFile;
openFileChooseProcess();
}
// For Android > 4.1.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Log.i("test", "openFileChooser 3");
uploadFile = uploadFile;
openFileChooseProcess();
}
// For Android >= 5.0
public boolean onShowFileChooser(WebView webView,
ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
Log.i("test", "openFileChooser 4:" + filePathCallback.toString());
uploadFiles = filePathCallback;
openFileChooseProcess();
return true;
}
});
return view;
}
private void ViewIni(View view,String url)
{
//实例化WebView对象
webView = (WebView) view.findViewById(R.id.webview);
//设置WebView属性,能够执行Javascript脚本
webView.getSettings().setJavaScriptEnabled(true);
//加载需要显示的网页
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( url.startsWith("http:") || url.startsWith("https:") ) {
return false;
}
// Otherwise allow the OS to handle things like tel, mailto, etc.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity( intent );
return true;
}
});
webView.loadUrl(url);
//设置Web视图
//setContentView(webView);
}
private void openFileChooseProcess() {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
startActivityForResult(Intent.createChooser(i, "选择文件"), 0);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 0:
if (uploadFile != null) {
Uri result = data == null || resultCode != RESULT_OK ? null
: data.getData();
uploadFile.onReceiveValue(result);
uploadFile = null;
}
if (uploadFiles != null) {
Uri result = data == null || resultCode != RESULT_OK ? null
: data.getData();
uploadFiles.onReceiveValue(new Uri[]{result});
uploadFiles = null;
}
break;
default:
break;
}
} else if (resultCode == RESULT_CANCELED) {
if (uploadFile != null) {
uploadFile.onReceiveValue(null);
uploadFile = null;
}
if (uploadFiles != null) {
uploadFiles.onReceiveValue(null);
uploadFiles = null;
}
}
}
}