Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
polysoft-nafmii
/
polysoft-android
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 07964e42
authored
Jul 19, 2021
by
amateur
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
1
1 parent
8fb78afe
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
73 additions
and
28 deletions
app/src/main/java/com/polysoft/nafmii/webview/HomeWebviewHandler.java
app/src/main/java/com/polysoft/nafmii/webview/IWebview.java
app/src/main/java/com/polysoft/nafmii/webview/JSApi.java
app/src/main/java/com/polysoft/nafmii/webview/JsInterfaceHandler.java
app/src/main/java/com/polysoft/nafmii/webview/JsToJavaInterface.java → app/src/main/java/com/polysoft/nafmii/webview/NativeApi.java
app/src/main/java/com/polysoft/nafmii/webview/WebviewHandler.java
app/src/main/java/com/polysoft/nafmii/webview/HomeWebviewHandler.java
View file @
07964e4
package
com
.
polysoft
.
nafmii
.
webview
;
package
com
.
polysoft
.
nafmii
.
webview
;
import
androidx.fragment.app.Fragment
;
import
androidx.fragment.app.Fragment
;
import
androidx.fragment.app.FragmentManager
;
import
com.polysoft.nafmii.bean.MenuBean
;
import
com.polysoft.nafmii.bean.MenuBean
;
import
com.polysoft.nafmii.fragment.WebViewFragment
;
import
com.tencent.smtt.sdk.WebView
;
import
com.tencent.smtt.sdk.WebView
;
import
com.whaty.nafmii.activity.HomeActivity
;
import
com.whaty.nafmii.activity.HomeActivity
;
import
java.util.List
;
public
class
HomeWebviewHandler
extends
WebviewHandler
{
public
class
HomeWebviewHandler
extends
WebviewHandler
{
private
final
JsToJavaInterface
js2java
;
public
HomeWebviewHandler
(
Fragment
fragment
,
WebView
webView
)
{
public
HomeWebviewHandler
(
Fragment
fragment
,
WebView
webView
)
{
super
(
fragment
,
webView
);
super
(
fragment
,
webView
);
this
.
js2java
=
new
JsToJavaInterface
(
this
);
webView
.
addJavascriptInterface
(
new
NativeApi
(
this
),
"$NativeApi"
);
webView
.
addJavascriptInterface
(
this
.
js2java
,
"$NativeApi"
);
}
}
public
void
setButtomMenu
(
MenuBean
bean
)
{
public
void
setButtomMenu
(
MenuBean
bean
)
{
HomeActivity
activity
=
(
HomeActivity
)
this
.
fragment
.
getActivity
();
HomeActivity
activity
=
(
HomeActivity
)
this
.
fragment
.
getActivity
();
activity
.
setModuleStatus
(
bean
);
activity
.
setModuleStatus
(
bean
);
}
}
public
void
setTextZoom
(
int
zoom
)
{
public
void
setTextZoom
(
int
zoom
)
{
super
.
webView
.
getSettings
().
setTextZoom
(
zoom
);
FragmentManager
manager
=
super
.
getFragmentManager
();
List
<
Fragment
>
fragments
=
manager
.
getFragments
();
for
(
Fragment
fmt
:
fragments
)
{
String
tag
=
fmt
.
getTag
();
WebView
webview
=
((
WebViewFragment
)
fmt
).
getWebview
().
getWebview
();
webview
.
getSettings
().
setTextZoom
(
zoom
);
}
}
}
}
}
app/src/main/java/com/polysoft/nafmii/webview/IWebview.java
View file @
07964e4
package
com
.
polysoft
.
nafmii
.
webview
;
package
com
.
polysoft
.
nafmii
.
webview
;
import
androidx.fragment.app.Fragment
;
import
com.tencent.smtt.sdk.WebView
;
public
interface
IWebview
{
public
interface
IWebview
{
void
loadUrl
(
String
url
);
void
loadUrl
(
String
url
);
...
@@ -8,6 +13,8 @@ public interface IWebview {
...
@@ -8,6 +13,8 @@ public interface IWebview {
boolean
canGoBack
();
boolean
canGoBack
();
JavaToJsinterface
getJsInterface
();
WebView
getWebview
();
Fragment
getFragment
();
}
}
app/src/main/java/com/polysoft/nafmii/webview/JSApi.java
0 → 100644
View file @
07964e4
package
com
.
polysoft
.
nafmii
.
webview
;
import
android.util.Log
;
import
com.alibaba.fastjson.JSON
;
import
com.tencent.smtt.sdk.WebView
;
public
class
JSApi
{
private
static
final
String
TAG
=
JSApi
.
class
.
getName
();
public
static
void
callOnBack
(
WebView
webView
,
String
json
)
{
callJs
(
webView
,
"onBack"
,
json
);
}
public
static
void
callOnRouteChange
(
WebView
webView
,
String
json
)
{
callJs
(
webView
,
"onRouteChange"
,
json
);
}
public
static
void
callOnPushMessage
(
WebView
webView
,
String
json
)
{
callJs
(
webView
,
"onPushMessage"
,
json
);
}
public
static
void
callJs
(
WebView
webView
,
String
methodName
,
String
json
)
{
String
param
=
JSON
.
toJSONString
(
json
);
String
script
=
String
.
format
(
"javascript:$api.%s(%s)"
,
methodName
,
param
);
webView
.
evaluateJavascript
(
script
,
value
->
Log
.
d
(
TAG
,
String
.
format
(
"JavaToJsinterface: callJs is %s result : %s"
,
methodName
,
value
)));
}
}
app/src/main/java/com/polysoft/nafmii/webview/JsInterfaceHandler.java
View file @
07964e4
...
@@ -5,16 +5,17 @@ import android.text.TextUtils;
...
@@ -5,16 +5,17 @@ import android.text.TextUtils;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
com.polysoft.nafmii.bean.Result
;
import
com.polysoft.nafmii.bean.Result
;
import
com.tencent.smtt.sdk.WebView
;
public
class
JsInterfaceHandler
{
public
class
JsInterfaceHandler
{
private
final
Activity
mActivity
;
private
final
Activity
mActivity
;
private
final
JavaToJsinterface
javaTojs
;
private
final
WebView
mWebview
;
private
String
jsMethodName
;
private
String
jsMethodName
;
public
JsInterfaceHandler
(
WebviewHandler
handler
)
{
public
JsInterfaceHandler
(
WebviewHandler
handler
)
{
this
.
mActivity
=
handler
.
getFragment
().
getActivity
();
this
.
mActivity
=
handler
.
getFragment
().
getActivity
();
this
.
javaTojs
=
handler
.
getJsInterface
();
this
.
mWebview
=
handler
.
getWebview
();
}
}
public
void
parse
(
String
params
,
JsHandle
jsHandle
)
{
public
void
parse
(
String
params
,
JsHandle
jsHandle
)
{
...
@@ -34,10 +35,10 @@ public class JsInterfaceHandler {
...
@@ -34,10 +35,10 @@ public class JsInterfaceHandler {
return
;
return
;
}
}
if
(
"'main'"
.
equals
(
Thread
.
currentThread
().
getName
()))
{
if
(
"'main'"
.
equals
(
Thread
.
currentThread
().
getName
()))
{
javaTojs
.
callJs
(
jsMethodName
,
json
);
JSApi
.
callJs
(
this
.
mWebview
,
jsMethodName
,
json
);
return
;
return
;
}
}
this
.
mActivity
.
runOnUiThread
(()
->
javaTojs
.
callJs
(
jsMethodName
,
json
));
this
.
mActivity
.
runOnUiThread
(()
->
JSApi
.
callJs
(
this
.
mWebview
,
jsMethodName
,
json
));
}
}
...
...
app/src/main/java/com/polysoft/nafmii/webview/
JsToJavaInterface
.java
→
app/src/main/java/com/polysoft/nafmii/webview/
NativeApi
.java
View file @
07964e4
...
@@ -29,11 +29,11 @@ import java.util.HashMap;
...
@@ -29,11 +29,11 @@ import java.util.HashMap;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
public
class
JsToJavaInterface
{
public
class
NativeApi
{
private
final
HomeWebviewHandler
handler
;
private
final
HomeWebviewHandler
handler
;
private
final
Activity
mActivity
;
private
final
Activity
mActivity
;
public
JsToJavaInterface
(
HomeWebviewHandler
handler
)
{
public
NativeApi
(
HomeWebviewHandler
handler
)
{
this
.
handler
=
handler
;
this
.
handler
=
handler
;
this
.
mActivity
=
handler
.
getFragment
().
getActivity
();
this
.
mActivity
=
handler
.
getFragment
().
getActivity
();
}
}
...
...
app/src/main/java/com/polysoft/nafmii/webview/WebviewHandler.java
View file @
07964e4
...
@@ -3,31 +3,27 @@ package com.polysoft.nafmii.webview;
...
@@ -3,31 +3,27 @@ package com.polysoft.nafmii.webview;
import
android.text.TextUtils
;
import
android.text.TextUtils
;
import
androidx.fragment.app.Fragment
;
import
androidx.fragment.app.Fragment
;
import
androidx.fragment.app.FragmentManager
;
import
com.polysoft.nafmii.bean.MenuBean
;
import
com.polysoft.nafmii.enums.JSMethodsEnum
;
import
com.tencent.smtt.sdk.WebView
;
import
com.tencent.smtt.sdk.WebView
;
import
com.whaty.nafmii.activity.HomeActivity
;
public
class
WebviewHandler
implements
IWebview
{
public
class
WebviewHandler
implements
IWebview
{
private
static
final
String
TAG
=
WebviewHandler
.
class
.
getName
();
private
static
final
String
TAG
=
WebviewHandler
.
class
.
getName
();
protected
final
Fragment
fragment
;
protected
final
Fragment
fragment
;
protected
final
WebView
webView
;
protected
final
WebView
webView
;
protected
final
JavaToJsinterface
java2js
;
protected
boolean
backIntercept
=
false
;
protected
boolean
backIntercept
=
false
;
public
WebviewHandler
(
Fragment
fragment
,
WebView
webView
)
{
public
WebviewHandler
(
Fragment
fragment
,
WebView
webView
)
{
this
.
fragment
=
fragment
;
this
.
fragment
=
fragment
;
this
.
webView
=
webView
;
this
.
webView
=
webView
;
this
.
java2js
=
new
JavaToJsinterface
(
webView
);
}
}
@Override
@Override
public
boolean
canGoBack
()
{
public
boolean
canGoBack
()
{
if
(
this
.
backIntercept
)
{
if
(
this
.
backIntercept
)
{
this
.
java2js
.
callJs
(
JSMethodsEnum
.
CALL_BACK
,
""
);
JSApi
.
callOnBack
(
this
.
webView
,
""
);
return
true
;
return
true
;
}
}
if
(
this
.
webView
.
canGoBack
())
{
if
(
this
.
webView
.
canGoBack
())
{
...
@@ -38,6 +34,16 @@ public class WebviewHandler implements IWebview {
...
@@ -38,6 +34,16 @@ public class WebviewHandler implements IWebview {
}
}
@Override
@Override
public
WebView
getWebview
()
{
return
this
.
webView
;
}
@Override
public
Fragment
getFragment
()
{
return
this
.
fragment
;
}
@Override
public
void
loadUrl
(
String
url
)
{
public
void
loadUrl
(
String
url
)
{
if
(
TextUtils
.
isEmpty
(
url
)
||
url
.
equals
(
this
.
getUrl
()))
{
if
(
TextUtils
.
isEmpty
(
url
)
||
url
.
equals
(
this
.
getUrl
()))
{
return
;
return
;
...
@@ -50,20 +56,13 @@ public class WebviewHandler implements IWebview {
...
@@ -50,20 +56,13 @@ public class WebviewHandler implements IWebview {
return
this
.
webView
.
getUrl
();
return
this
.
webView
.
getUrl
();
}
}
@Override
public
JavaToJsinterface
getJsInterface
()
{
return
this
.
java2js
;
}
public
void
setBackIntercept
(
boolean
intercept
)
{
public
void
setBackIntercept
(
boolean
intercept
)
{
this
.
backIntercept
=
intercept
;
this
.
backIntercept
=
intercept
;
}
}
p
ublic
WebView
getWebView
()
{
p
rotected
FragmentManager
getFragmentManager
()
{
return
this
.
webView
;
return
this
.
fragment
.
getActivity
().
getSupportFragmentManager
()
;
}
}
public
Fragment
getFragment
()
{
return
this
.
fragment
;
}
}
}
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment