Commit 52b2bd29 by zhengnw@sobot.com

widget 1.1.8 webview

parent 2c13748a
......@@ -27,6 +27,7 @@ import com.sobot.widget.ui.SobotMarkConfig;
import com.sobot.widget.ui.base.SobotBaseActivity;
import com.sobot.widget.ui.rich.HtmlToolUtils;
import com.sobot.widget.ui.toast.SobotToastUtil;
import com.sobot.widget.ui.webview.SobotWebViewActivity;
import java.io.File;
import java.util.HashMap;
......@@ -247,6 +248,13 @@ public class MainActivity extends SobotBaseActivity {
// SobotLogUtils.i(SobotLoginTools.getInstance().getServiceInfo().getTimezone()+"----------------------------------"+SobotLoginTools.getInstance().getServiceInfo().getTimezoneId());
// }
// });
// Intent intent = new Intent(MainActivity.this, SobotWebViewActivity.class);
// intent.putExtra("url", "https://www.sobot.com/ticketclient/index.html?sysNum=75574e5fa29a48458d1f57ab5489a4c5&deployId=442eb27d9c5c4ab9a3a21023f82207a9");
//// 是否隐藏头部导航栏 默认false显示, true 隐藏
// intent.putExtra("isHideTitleBar", true);
//// 是否隐藏底部工具栏 默认false显示, true 隐藏
// intent.putExtra("isHideToolBar", true);
// startActivity(intent);
}
@Override
......
......@@ -12,7 +12,7 @@ task androidSourcesJar(type: Jar) {
ext {
PUBLISH_GROUP_ID = "com.sobot.library" //项目包名
PUBLISH_ARTIFACT_ID = 'widget_x' //项目名
PUBLISH_VERSION = '1.1.5' //版本号
PUBLISH_VERSION = '1.1.8' //版本号
}
......
package com.sobot.widget.ui.webview;
import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
/**
* 解决WebView中输入时弹出软键盘,挡住输入框的问题
*/
public class AndroidBug5497Workaround {
// For more information, see https://issuetracker.google.com/issues/36911528
// To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
public static void assistActivity (Activity activity) {
new AndroidBug5497Workaround(activity);
}
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private AndroidBug5497Workaround(Activity activity) {
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}
private int frameLayoutHeight = 0;
private void possiblyResizeChildOfContent() {
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard/4)) {
// keyboard probably just became visible
frameLayoutHeight = frameLayoutParams.height;// !!!修改前保存原有高度
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
} else {
// keyboard probably just became hidden
if(0 != frameLayoutHeight) {
frameLayoutParams.height = frameLayoutHeight;// !!!收起键盘恢复原有高度
}
}
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);
}
}
\ No newline at end of file
package com.sobot.widget.ui.webview;
import static com.sobot.widget.ui.SobotBaseConstant.REQUEST_CODE_MAKEPICTUREFROMCAMERA;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
......@@ -22,11 +24,15 @@ import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.sobot.utils.SobotLogUtils;
import com.sobot.utils.SobotNetUtils;
import com.sobot.utils.SobotStringUtils;
import com.sobot.widget.R;
import com.sobot.widget.ui.base.SobotBaseActivity;
import com.sobot.widget.ui.toast.SobotToastUtil;
import com.sobot.widget.ui.utils.SobotWidgetUtils;
@SuppressLint("SetJavaScriptEnabled")
......@@ -47,10 +53,16 @@ public class SobotWebViewActivity extends SobotBaseActivity implements View.OnCl
private ImageView sobot_webview_reload;
private ImageView sobot_webview_copy;
//根据入的url判断是否url true:是;false:不是
//根据入的url判断是否url true:是;false:不是
private boolean isUrlOrText = true;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidBug5497Workaround.assistActivity(SobotWebViewActivity.this);
}
@Override
protected int getContentViewResId() {
return R.layout.sobot_activity_webview;
}
......@@ -81,13 +93,6 @@ public class SobotWebViewActivity extends SobotBaseActivity implements View.OnCl
@Override
protected void initView() {
setTitle("");
showLeftMenu(getResDrawableId("sobot_icon_back"), "", true, new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
mWebView = (WebView) findViewById(R.id.sobot_mWebView);
mProgressBar = (ProgressBar) findViewById(R.id.sobot_loadProgress);
sobot_rl_net_error = (RelativeLayout) findViewById(R.id.sobot_rl_net_error);
......@@ -108,37 +113,17 @@ public class SobotWebViewActivity extends SobotBaseActivity implements View.OnCl
sobot_webview_copy.setOnClickListener(this);
sobot_webview_goback.setEnabled(false);
sobot_webview_forward.setEnabled(false);
displayInNotch(mWebView);
resetViewDisplay();
boolean isHideTitleBar = getIntent().getBooleanExtra("isHideTitleBar", false);
boolean isHideToolBar = getIntent().getBooleanExtra("isHideToolBar", false);
if (isHideTitleBar && getToolBar() != null) {
getToolBar().setVisibility(View.GONE);
}
initWebView();
if (isUrlOrText) {
//加载url
mWebView.loadUrl(mUrl);
sobot_webview_copy.setVisibility(View.VISIBLE);
} else {
//修改图片高度为自适应宽度
mUrl = "<!DOCTYPE html>\n" +
"<html>\n" +
" <head>\n" +
" <meta charset=\"utf-8\">\n" +
" <title></title>\n" +
" <style>\n" +
" img{\n" +
" width: auto;\n" +
" height:auto;\n" +
" max-height: 100%;\n" +
" max-width: 100%;\n" +
" }\n" +
" </style>\n" +
" </head>\n" +
" <body>" + mUrl + " </body>\n" +
"</html>";
//显示文本内容
mWebView.loadDataWithBaseURL("about:blank", mUrl.replace("</p>", "<br/>").replace("<P>", "").replace("</P>", "<br/>"), "text/html", "utf-8", null);
resetViewDisplay();
if (isHideToolBar && sobot_webview_toolsbar != null) {
sobot_webview_toolsbar.setVisibility(View.GONE);
}
SobotLogUtils.i("webViewActivity---" + mUrl);
}
@Override
......@@ -196,6 +181,32 @@ public class SobotWebViewActivity extends SobotBaseActivity implements View.OnCl
mWebView.setVisibility(View.VISIBLE);
sobot_webview_toolsbar.setVisibility(View.VISIBLE);
sobot_rl_net_error.setVisibility(View.GONE);
if (isUrlOrText) {
//加载url
mWebView.loadUrl(mUrl);
sobot_webview_copy.setVisibility(View.VISIBLE);
} else {
//修改图片高度为自适应宽度
mUrl = "<!DOCTYPE html>\n" +
"<html>\n" +
" <head>\n" +
" <meta charset=\"utf-8\">\n" +
" <title></title>\n" +
" <style>\n" +
" img{\n" +
" width: auto;\n" +
" height:auto;\n" +
" max-height: 100%;\n" +
" max-width: 100%;\n" +
" }\n" +
" </style>\n" +
" </head>\n" +
" <body>" + mUrl + " </body>\n" +
"</html>";
//显示文本内容
mWebView.loadDataWithBaseURL("about:blank", mUrl.replace("</p>", "<br/>").replace("<P>", "").replace("</P>", "<br/>"), "text/html", "utf-8", null);
}
SobotLogUtils.i("webViewActivity---" + mUrl);
} else {
mWebView.setVisibility(View.GONE);
sobot_webview_toolsbar.setVisibility(View.GONE);
......@@ -303,10 +314,34 @@ public class SobotWebViewActivity extends SobotBaseActivity implements View.OnCl
}
}
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
uploadMessageAboveL = filePathCallback;
chooseAlbumPic();
if (fileChooserParams != null && fileChooserParams.getAcceptTypes() != null) {
String[] acceptTypes = fileChooserParams.getAcceptTypes();
String acceptType = "*/*";
StringBuilder sb = new StringBuilder();
if (acceptTypes.length > 0) {
for (String type : acceptTypes) {
sb.append(type).append(';');
}
}
if (sb.length() > 0) {
String typeStr = sb.toString();
acceptType = typeStr.substring(0, typeStr.length() - 1);
}
SobotLogUtils.i("input file 标签的accept类型:" + acceptType + fileChooserParams.getMode());
//根据判断,触发相关的操作,如文件选择,拍照等
if ("image/*".equals(SobotStringUtils.checkStringIsNull(acceptType))) {
openCapture();
} else {
chooseFile();
}
} else {
}
return true;
}
......@@ -360,7 +395,6 @@ public class SobotWebViewActivity extends SobotBaseActivity implements View.OnCl
private static final int REQUEST_CODE_ALBUM = 0x0111;
private ValueCallback<Uri> uploadMessage;
private ValueCallback<Uri[]> uploadMessageAboveL;
public Context getContext() {
......@@ -368,53 +402,52 @@ public class SobotWebViewActivity extends SobotBaseActivity implements View.OnCl
}
/**
* 选择相册照片
* 打开相机拍照
*/
private void chooseAlbumPic() {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
// i.setType("image/*");
i.setType("video/*;image/*");
startActivityForResult(Intent.createChooser(i, "Image Chooser"), REQUEST_CODE_ALBUM);
private void openCapture() {
openCamera();
}
/**
* 打开文件系统选取文件上传
*/
private void chooseFile() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");//设置类型,我这里是任意类型,任意后缀的可以这样写。
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE_ALBUM);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_ALBUM) {
if (uploadMessage == null && uploadMessageAboveL == null) {
if (requestCode == REQUEST_CODE_ALBUM || requestCode == REQUEST_CODE_MAKEPICTUREFROMCAMERA) {
if (uploadMessageAboveL == null) {
return;
}
if (resultCode != RESULT_OK) {
//一定要返回null,否则<input file> 就是没有反应
if (uploadMessage != null) {
uploadMessage.onReceiveValue(null);
uploadMessage = null;
}
if (uploadMessageAboveL != null) {
uploadMessageAboveL.onReceiveValue(null);
uploadMessageAboveL = null;
}
}
} else {
if (resultCode == RESULT_OK) {
Uri imageUri = null;
switch (requestCode) {
case REQUEST_CODE_ALBUM:
if (data != null) {
imageUri = data.getData();
}
break;
case REQUEST_CODE_MAKEPICTUREFROMCAMERA:
if (cameraFile != null && cameraFile.exists()) {
imageUri = SobotWidgetUtils.getUri(SobotWebViewActivity.this, cameraFile);
}
//上传文件
if (uploadMessage != null) {
uploadMessage.onReceiveValue(imageUri);
uploadMessage = null;
break;
}
if (imageUri != null) {
//上传文件
if (uploadMessageAboveL != null) {
uploadMessageAboveL.onReceiveValue(new Uri[]{imageUri});
uploadMessageAboveL = null;
......@@ -422,4 +455,6 @@ public class SobotWebViewActivity extends SobotBaseActivity implements View.OnCl
}
}
}
}
}
}
\ No newline at end of file
......@@ -5,7 +5,10 @@
android:background="@color/sobot_common_gray6"
android:orientation="vertical">
<include layout="@layout/sobot_common_layout_titlebar" />
<include
layout="@layout/sobot_common_layout_titlebar"
android:layout_height="@dimen/sobot_webview_titlebar_height"
android:layout_width="match_parent" />
<LinearLayout
android:layout_width="match_parent"
......@@ -19,11 +22,11 @@
android:layout_width="fill_parent"
android:layout_height="5dip"
android:max="100"
android:paddingStart="1dip"
android:paddingLeft="1dip"
android:paddingRight="1dip"
android:progress="0"
android:paddingEnd="1dip"
android:paddingStart="1dip" />
android:paddingRight="1dip"
android:progress="0" />
<com.sobot.widget.ui.webview.CustomWebview
android:id="@+id/sobot_mWebView"
......@@ -34,8 +37,8 @@
<LinearLayout
android:id="@+id/sobot_webview_toolsbar"
android:layout_height="@dimen/sobot_webview_toolsbar_height"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="vertical">
<View
......@@ -48,10 +51,10 @@
android:layout_height="43dp"
android:background="@color/sobot_white"
android:orientation="horizontal"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingStart="15dp"
android:paddingEnd="15dp">
android:paddingLeft="15dp"
android:paddingEnd="15dp"
android:paddingRight="15dp">
<ImageView
android:id="@+id/sobot_webview_goback"
......
......@@ -3,5 +3,7 @@
<dimen name="sobot_titlebar_height">44dp</dimen>
<dimen name="sobot_titlebar_title_size">16sp</dimen>
<dimen name="sobot_webview_titlebar_height">@dimen/sobot_titlebar_height</dimen>
<dimen name="sobot_webview_toolsbar_height">@dimen/sobot_titlebar_height</dimen>
</resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment