123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- package com.haochuan.hciptvbasic.webview;
- import android.app.Activity;
- import android.content.Context;
- import android.database.Cursor;
- import android.net.Uri;
- import android.text.TextUtils;
- import android.webkit.JavascriptInterface;
- import android.webkit.WebView;
- import com.haochuan.core.Logger;
- import com.haochuan.core.util.HandlerUtil;
- import com.haochuan.hciptvbasic.BaseWebActivity;
- import com.haochuan.hciptvbasic.BuildConfig;
- import com.haochuan.core.util.JSONUtil;
- import com.haochuan.core.util.JsUtil;
- import com.haochuan.core.util.MacUtil;
- import com.haochuan.core.util.MathUtil;
- import com.haochuan.core.util.ToolsUtil;
- import org.json.JSONObject;
- import org.w3c.dom.Text;
- import static com.haochuan.core.util.MessageCode.EXCEPTION_ERROR;
- import static com.haochuan.core.util.MessageCode.SUCCESS;
- public class UtilToJS {
- private Context context; //MainActivity 句柄
- private WebView webView;
- private ToolsUtil toolsUtil;
- //将遥控返回按键事件传递给前端
- private String JS_EVENT_BACK = "javascript:onBackEvent()";
- //将日志传递给js
- private String JS_EVENT_LOG = "javascript:onLog('%s')";
- //将response传递给js
- private String JS_EVENT_RESPONSE ="javascript:onWebRequestResponse('%s','%s')";
- /**
- * 兼容旧智慧平台接口用
- * 前端调用apk http请求回调事件
- * <br>
- * 形参1:表示前端调用apk方法时传入的code,String字符串
- * <br>
- * 形参2:表示apk请求过程--(0:开始请求;1:请求成功;-1:请求失败;-2:发生异常;2:请求结束),String字符串
- * <br>
- * 形参3:表示apk请求过程中的结果,String字符串
- */
- public static final String JS_EVENT_HTTP_CONNECT = "javascript:onHttpConnect(\"%s\", \"%s\", \"%s\")";
- public UtilToJS(Context context, WebView webView){
- this.context = context;
- this.webView = webView;
- toolsUtil = new ToolsUtil();
- }
- /*------------------------------------功能性函数-----------------------------------------*/
- /*
- * 将log传递给前端
- * */
- public void logToJs(String log){
- Logger.d("UtilToJS,logToJs(),log:" + log);
- JsUtil.evaluateJavascript(context,webView,
- String.format(JS_EVENT_LOG,log));
- }
- /*
- * webView对象获取"返回"按键事件
- * */
- public void onBackPressed(){
- Logger.d("UtilToJS,onBackPressed()");
- JsUtil.evaluateJavascript(context,webView, JS_EVENT_BACK);
- }
- /*---------------------------------获取本地参数--------------------------*/
- /*
- * 获取intent启动参数
- * */
- @JavascriptInterface
- public String getIntentJson(){
- Logger.d("UtilToJS,getIntentJson()");
- return toolsUtil.getIntentJson(context);
- }
- @JavascriptInterface
- public String getLocalParamsByJson(){
- try{
- Logger.d("UtilToJS,getLocalParamsByJson()");
- JSONObject localParamsJson = new JSONObject();
- localParamsJson.put("version_code",BuildConfig.VERSION_CODE);
- localParamsJson.put("version_name",BuildConfig.VERSION_NAME);
- localParamsJson.put("mac",MacUtil.getMac(context));
- localParamsJson.put("user_id",getUserName());
- String paramJsonStr = localParamsJson.toString();
- Logger.d("UtilToJS,getLocalParamsByJson(),paramJsonStr:" + paramJsonStr);
- return paramJsonStr;
- }catch (Exception e){
- e.printStackTrace();
- return "";
- }
- }
- /*-----------------------------操作APK-------------------------------------*/
- /*
- * 退出app
- * */
- @JavascriptInterface
- public void appExit(){
- try{
- HandlerUtil.runOnUiThread(() -> {
- Logger.d("UtilToJS,appExit()");
- Activity activity =(Activity)context;
- if(activity instanceof BaseWebActivity){
- BaseWebActivity baseWebActivity = (BaseWebActivity)activity;
- baseWebActivity.AppExit();
- }else{
- android.os.Process.killProcess(android.os.Process.myPid()); //获取PID
- System.exit(0);
- }
- });
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- /*---------------------------通过客户端请求接口------------------------*/
- /*
- *js 通过apk客户端访问网络接口
- *@param contentType 网络类型,详情参考网络请求的content-type
- *@param paramJson 请求参数集,格式为json字符串
- *@param headJson 请求头部集,格式为json字符串
- *@param method 请求方法,1,get;2,post
- *@param ignoreResult 是否忽略结果,true,忽略;false,不忽略.
- *@param tag 透传参数,将在结果回调中一并返回,主要区别多个并发请求
- * */
- @JavascriptInterface
- public int clientWebRequest(String paramsJson){
- try{
- Logger.d("clientWebRequest(),paramsJson" + paramsJson);
- JSONObject requestParams = new JSONObject(paramsJson);
- String url = JSONUtil.getString(requestParams,"url","");
- String methodStr = JSONUtil.getString(requestParams,"method","1");
- int method = 0;
- if(MathUtil.isDigitsOnly(methodStr)){
- method = Integer.parseInt(methodStr);
- if( method <0 || method > 1 ){
- Logger.w("clientWebRequest 请求参数method必须为0或者1,目前重置为0");
- method = 0;
- }
- }else{
- Logger.w("clientWebRequest 请求参数method必须为数字,目前重置为0");
- }
- String contentType = JSONUtil.getString(requestParams,"content_type","application/json");
- String headJson = JSONUtil.getString(requestParams,"head_json","{}");
- String paramJson = JSONUtil.getString(requestParams,"param_json","{}");
- String ignore = JSONUtil.getString(requestParams,"ignore_result","0");
- boolean ignoreResult = TextUtils.equals(ignore,"1");
- String tag = JSONUtil.getString(requestParams,"tag","");
- toolsUtil.clientWebRequest(url, method, contentType, headJson,paramJson, ignoreResult, tag,
- (int what,String response,String tag1)->{
- Logger.d(String.format("response:%s;tag:%s",response,tag1));
- JsUtil.evaluateJavascript(context,webView,
- String.format(JS_EVENT_RESPONSE,response,tag1));
- });
- return SUCCESS;
- }catch (Exception e){
- e.printStackTrace();
- return EXCEPTION_ERROR;
- }
- }
- /*-----------------------------本地盒子参数获取------------------------------------*/
- /**
- * 获取userName
- * @return
- */
- private String getUserNameFromSTB(){
- try{
- Uri uri = Uri.parse("content://stbconfig/authentication");
- Cursor mCursor = context.getContentResolver().query(uri,null,null,null,null);
- if(mCursor != null){
- String userName = "";
- while (mCursor.moveToNext()){
- //获取username
- String name = mCursor.getString(mCursor.getColumnIndex("name"));
- if("username".equals(name)){
- userName = mCursor.getString(mCursor.getColumnIndex("value"));
- break;
- }
- }
- return userName;
- }else{
- return "";
- }
- }catch (Exception e){
- e.printStackTrace();
- return "";
- }
- }
- /**
- * 返回用户名
- * @return
- */
- private String getUserName(){
- String userName = getUserNameFromSTB();
- if(TextUtils.isEmpty(userName)){
- return MacUtil.getMac(context);
- }
- return userName;
- }
- }
|