UtilToJS.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package com.haochuan.hciptvbasic.webview;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.net.Uri;
  6. import android.text.TextUtils;
  7. import android.webkit.JavascriptInterface;
  8. import android.webkit.WebView;
  9. import com.haochuan.core.Logger;
  10. import com.haochuan.core.util.HandlerUtil;
  11. import com.haochuan.hciptvbasic.BaseWebActivity;
  12. import com.haochuan.hciptvbasic.BuildConfig;
  13. import com.haochuan.core.util.JSONUtil;
  14. import com.haochuan.core.util.JsUtil;
  15. import com.haochuan.core.util.MacUtil;
  16. import com.haochuan.core.util.MathUtil;
  17. import com.haochuan.core.util.ToolsUtil;
  18. import org.json.JSONObject;
  19. import org.w3c.dom.Text;
  20. import static com.haochuan.core.util.MessageCode.EXCEPTION_ERROR;
  21. import static com.haochuan.core.util.MessageCode.SUCCESS;
  22. public class UtilToJS {
  23. private Context context; //MainActivity 句柄
  24. private WebView webView;
  25. private ToolsUtil toolsUtil;
  26. //将遥控返回按键事件传递给前端
  27. private String JS_EVENT_BACK = "javascript:onBackEvent()";
  28. //将日志传递给js
  29. private String JS_EVENT_LOG = "javascript:onLog('%s')";
  30. //将response传递给js
  31. private String JS_EVENT_RESPONSE ="javascript:onWebRequestResponse('%s','%s')";
  32. /**
  33. * 兼容旧智慧平台接口用
  34. * 前端调用apk http请求回调事件
  35. * <br>
  36. * 形参1:表示前端调用apk方法时传入的code,String字符串
  37. * <br>
  38. * 形参2:表示apk请求过程--(0:开始请求;1:请求成功;-1:请求失败;-2:发生异常;2:请求结束),String字符串
  39. * <br>
  40. * 形参3:表示apk请求过程中的结果,String字符串
  41. */
  42. public static final String JS_EVENT_HTTP_CONNECT = "javascript:onHttpConnect(\"%s\", \"%s\", \"%s\")";
  43. public UtilToJS(Context context, WebView webView){
  44. this.context = context;
  45. this.webView = webView;
  46. toolsUtil = new ToolsUtil();
  47. }
  48. /*------------------------------------功能性函数-----------------------------------------*/
  49. /*
  50. * 将log传递给前端
  51. * */
  52. public void logToJs(String log){
  53. Logger.d("UtilToJS,logToJs(),log:" + log);
  54. JsUtil.evaluateJavascript(context,webView,
  55. String.format(JS_EVENT_LOG,log));
  56. }
  57. /*
  58. * webView对象获取"返回"按键事件
  59. * */
  60. public void onBackPressed(){
  61. Logger.d("UtilToJS,onBackPressed()");
  62. JsUtil.evaluateJavascript(context,webView, JS_EVENT_BACK);
  63. }
  64. /*---------------------------------获取本地参数--------------------------*/
  65. /*
  66. * 获取intent启动参数
  67. * */
  68. @JavascriptInterface
  69. public String getIntentJson(){
  70. Logger.d("UtilToJS,getIntentJson()");
  71. return toolsUtil.getIntentJson(context);
  72. }
  73. @JavascriptInterface
  74. public String getLocalParamsByJson(){
  75. try{
  76. Logger.d("UtilToJS,getLocalParamsByJson()");
  77. JSONObject localParamsJson = new JSONObject();
  78. localParamsJson.put("version_code",BuildConfig.VERSION_CODE);
  79. localParamsJson.put("version_name",BuildConfig.VERSION_NAME);
  80. localParamsJson.put("mac",MacUtil.getMac(context));
  81. localParamsJson.put("user_id",getUserName());
  82. String paramJsonStr = localParamsJson.toString();
  83. Logger.d("UtilToJS,getLocalParamsByJson(),paramJsonStr:" + paramJsonStr);
  84. return paramJsonStr;
  85. }catch (Exception e){
  86. e.printStackTrace();
  87. return "";
  88. }
  89. }
  90. /*-----------------------------操作APK-------------------------------------*/
  91. /*
  92. * 退出app
  93. * */
  94. @JavascriptInterface
  95. public void appExit(){
  96. try{
  97. HandlerUtil.runOnUiThread(() -> {
  98. Logger.d("UtilToJS,appExit()");
  99. Activity activity =(Activity)context;
  100. if(activity instanceof BaseWebActivity){
  101. BaseWebActivity baseWebActivity = (BaseWebActivity)activity;
  102. baseWebActivity.AppExit();
  103. }else{
  104. android.os.Process.killProcess(android.os.Process.myPid()); //获取PID
  105. System.exit(0);
  106. }
  107. });
  108. }catch (Exception e){
  109. e.printStackTrace();
  110. }
  111. }
  112. /*---------------------------通过客户端请求接口------------------------*/
  113. /*
  114. *js 通过apk客户端访问网络接口
  115. *@param contentType 网络类型,详情参考网络请求的content-type
  116. *@param paramJson 请求参数集,格式为json字符串
  117. *@param headJson 请求头部集,格式为json字符串
  118. *@param method 请求方法,1,get;2,post
  119. *@param ignoreResult 是否忽略结果,true,忽略;false,不忽略.
  120. *@param tag 透传参数,将在结果回调中一并返回,主要区别多个并发请求
  121. * */
  122. @JavascriptInterface
  123. public int clientWebRequest(String paramsJson){
  124. try{
  125. Logger.d("clientWebRequest(),paramsJson" + paramsJson);
  126. JSONObject requestParams = new JSONObject(paramsJson);
  127. String url = JSONUtil.getString(requestParams,"url","");
  128. String methodStr = JSONUtil.getString(requestParams,"method","1");
  129. int method = 0;
  130. if(MathUtil.isDigitsOnly(methodStr)){
  131. method = Integer.parseInt(methodStr);
  132. if( method <0 || method > 1 ){
  133. Logger.w("clientWebRequest 请求参数method必须为0或者1,目前重置为0");
  134. method = 0;
  135. }
  136. }else{
  137. Logger.w("clientWebRequest 请求参数method必须为数字,目前重置为0");
  138. }
  139. String contentType = JSONUtil.getString(requestParams,"content_type","application/json");
  140. String headJson = JSONUtil.getString(requestParams,"head_json","{}");
  141. String paramJson = JSONUtil.getString(requestParams,"param_json","{}");
  142. String ignore = JSONUtil.getString(requestParams,"ignore_result","0");
  143. boolean ignoreResult = TextUtils.equals(ignore,"1");
  144. String tag = JSONUtil.getString(requestParams,"tag","");
  145. toolsUtil.clientWebRequest(url, method, contentType, headJson,paramJson, ignoreResult, tag,
  146. (int what,String response,String tag1)->{
  147. Logger.d(String.format("response:%s;tag:%s",response,tag1));
  148. JsUtil.evaluateJavascript(context,webView,
  149. String.format(JS_EVENT_RESPONSE,response,tag1));
  150. });
  151. return SUCCESS;
  152. }catch (Exception e){
  153. e.printStackTrace();
  154. return EXCEPTION_ERROR;
  155. }
  156. }
  157. /*-----------------------------本地盒子参数获取------------------------------------*/
  158. /**
  159. * 获取userName
  160. * @return
  161. */
  162. private String getUserNameFromSTB(){
  163. try{
  164. Uri uri = Uri.parse("content://stbconfig/authentication");
  165. Cursor mCursor = context.getContentResolver().query(uri,null,null,null,null);
  166. if(mCursor != null){
  167. String userName = "";
  168. while (mCursor.moveToNext()){
  169. //获取username
  170. String name = mCursor.getString(mCursor.getColumnIndex("name"));
  171. if("username".equals(name)){
  172. userName = mCursor.getString(mCursor.getColumnIndex("value"));
  173. break;
  174. }
  175. }
  176. return userName;
  177. }else{
  178. return "";
  179. }
  180. }catch (Exception e){
  181. e.printStackTrace();
  182. return "";
  183. }
  184. }
  185. /**
  186. * 返回用户名
  187. * @return
  188. */
  189. private String getUserName(){
  190. String userName = getUserNameFromSTB();
  191. if(TextUtils.isEmpty(userName)){
  192. return MacUtil.getMac(context);
  193. }
  194. return userName;
  195. }
  196. }