Browse Source

1,剔除了一些无用的函数接口
2,添加了一些参数的正则验证
3,函数返回代码做了更新

lyn 5 years ago
parent
commit
50e2932114

+ 2 - 2
app/src/main/java/com/haochuan/hciptvbasic/Util/DownloadUtils.java

@@ -25,7 +25,7 @@ public class DownloadUtils {
         Logger.d("准备下载文件:%s");
         final String filePath = FileDownloadUtils.getDefaultSaveRootPath() + File.separator + "temp" + File.separator + fileName + "." + fileExtension;
         if (listener != null) {
-            listener.onDownloadStart(filePath);
+            listener.onDownloadStart();
         }
         FileDownloader.getImpl().create(url)
                 .setPath(filePath, false)
@@ -75,7 +75,7 @@ public class DownloadUtils {
     }
 
     public interface DownloadProgressListener {
-        void onDownloadStart(String fileName);
+        void onDownloadStart();
 
         void onDownloadProgress(int progress);
 

+ 26 - 0
app/src/main/java/com/haochuan/hciptvbasic/Util/JSONUtil.java

@@ -0,0 +1,26 @@
+package com.haochuan.hciptvbasic.Util;
+
+import org.json.JSONObject;
+
+public class JSONUtil {
+
+    //获取Json对象String类型值
+    public static String getString(JSONObject json,String key,String defaultValue){
+        try{
+            return json.has(key)?json.get(key).toString():defaultValue;
+        }catch (Exception e){
+            e.printStackTrace();
+            return defaultValue;
+        }
+    }
+
+    //获取Json对象int类型值
+    public static int getInt(JSONObject json,String key,int defaultValue){
+        try{
+            return json.has(key)?Integer.parseInt(json.get(key).toString()):defaultValue;
+        }catch (Exception e){
+            e.printStackTrace();
+            return defaultValue;
+        }
+    }
+}

+ 4 - 4
app/src/main/java/com/haochuan/hciptvbasic/Util/MessageCode.java

@@ -2,8 +2,8 @@ package com.haochuan.hciptvbasic.Util;
 
 public class MessageCode {
     public final static int EXCEPTION_ERROR = -1;       //异常抛出
-    public final static int SUCCESS = 0;                //成功
-    public final static int PARAM_ERROR = 1;            //参数错误
-    public final static int PLAYER_OBJ_NULL = 2;        //播放器对象为空
-    public final static int PLAYER_NO_INIT = 3;        //播放器对象未初始化
+    public final static int SUCCESS = 6;                //成功
+    public final static int PARAM_ERROR = 7;            //参数错误
+    public final static int PLAYER_OBJ_NULL = 8;        //播放器对象为空
+    public final static int PLAYER_NO_INIT = 9;        //播放器对象未初始化
 }

+ 11 - 0
app/src/main/java/com/haochuan/hciptvbasic/Util/RegexUtil.java

@@ -13,4 +13,15 @@ public class RegexUtil {
         Pattern pattern = Pattern.compile("(http?|https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]");
         return pattern.matcher(url).matches();
     }
+
+    /*
+     * 判断字符串是否为正确的包名或者类名
+     * */
+    public static boolean isPackageName(String name){
+        if (null == name || "".equals(name)) {
+            return false;
+        }
+        Pattern pattern = Pattern.compile("^([a-zA-Z_][a-zA-Z0-9_]*)+([.][a-zA-Z_][a-zA-Z0-9_]*)+$");
+        return pattern.matcher(name).matches();
+    }
 }

+ 0 - 2
app/src/main/java/com/haochuan/hciptvbasic/Util/ToolsUtil.java

@@ -9,8 +9,6 @@ import android.net.Uri;
 import android.os.Bundle;
 import android.text.TextUtils;
 import android.util.Base64;
-import android.util.Log;
-import android.webkit.JavascriptInterface;
 
 import com.haochuan.hciptvbasic.web.MyRequest;
 import com.yanzhenjie.nohttp.NoHttp;

+ 21 - 35
app/src/main/java/com/haochuan/hciptvbasic/webview/PlayerToJS.java

@@ -12,9 +12,11 @@ import android.webkit.JavascriptInterface;
 import android.webkit.WebView;
 import android.widget.FrameLayout;
 
+import com.haochuan.hciptvbasic.Util.JSONUtil;
 import com.haochuan.hciptvbasic.Util.JsUtil;
 import com.haochuan.hciptvbasic.Util.Logger;
 import com.haochuan.hciptvbasic.Util.MathUtil;
+import com.haochuan.hciptvbasic.Util.MessageCode;
 import com.haochuan.hciptvbasic.Util.RegexUtil;
 import com.haochuan.hciptvbasic.Util.ScreenSnap;
 import com.haochuan.hciptvbasic.video.BaseMediaPlayer;
@@ -127,7 +129,7 @@ public class PlayerToJS {
         try{
             JSONObject playParam = new JSONObject(playParamJson);
             String url = "";
-            String typeStr = playParam.has("type")?playParam.get("type").toString():"1";
+            String typeStr = JSONUtil.getString(playParam,"type","1");
             int type = 1;
             if(TextUtils.isEmpty(typeStr)){
                 type = 1;
@@ -139,24 +141,24 @@ public class PlayerToJS {
             }
             switch (type){
                 case 1:
-                    url = playParam.has("url")?playParam.get("url").toString():"";
+                    url = JSONUtil.getString(playParam,"url","");
                     break;
                 case 2:
                     //该版本没有code获取url的功能,暂缺,请根据实际情况添加
                     break;
                 default:
-                    url = playParam.has("url")?playParam.get("url").toString():"";
+                    url = JSONUtil.getString(playParam,"url","");
                     break;
             }
             if(TextUtils.isEmpty(url)){
                 Logger.e(PARAM_ERROR,"调用play函数,url为空,不能执行播放");
                 return PARAM_ERROR;
             }
-            String seekTime = playParam.has("seek_time")?playParam.get("seek_time").toString():"0";
-            String x = playParam.has("x")?playParam.get("x").toString():"0";
-            String y = playParam.has("y")?playParam.get("y").toString():"0";
-            String width = playParam.has("width")?playParam.get("width").toString():"1280";
-            String height = playParam.has("height")?playParam.get("height").toString():"720";
+            String seekTime = JSONUtil.getString(playParam,"seek_time","0");
+            String x = JSONUtil.getString(playParam,"x","0");
+            String y = JSONUtil.getString(playParam,"y","0");
+            String width = JSONUtil.getString(playParam,"width","1280");
+            String height = JSONUtil.getString(playParam,"height","720");
             return videoPlay(url,seekTime,x,y,width,height);
         }catch (Exception e){
             e.printStackTrace();
@@ -173,10 +175,10 @@ public class PlayerToJS {
     public int change(String changeParamJson){
         try{
             JSONObject changeParam = new JSONObject(changeParamJson);
-            String x = changeParam.has("x")?changeParam.get("x").toString():"0";
-            String y = changeParam.has("y")?changeParam.get("y").toString():"0";
-            String width = changeParam.has("width")?changeParam.get("width").toString():"1280";
-            String height = changeParam.has("height")?changeParam.get("height").toString():"720";
+            String x = JSONUtil.getString(changeParam,"x","0");
+            String y = JSONUtil.getString(changeParam,"y","0");
+            String width = JSONUtil.getString(changeParam,"width","1280");
+            String height = JSONUtil.getString(changeParam,"height","720");
             return videoChange(x,y,width,height);
         }catch (Exception e){
             e.printStackTrace();
@@ -227,12 +229,17 @@ public class PlayerToJS {
      * 快进到指定位置
      * */
     @JavascriptInterface
-    public int seek(int position){
+    public int seek(String paramJson){
         if(baseMediaPlayer == null){
             Logger.e("播放器为空,不能拖动");
             return PLAYER_OBJ_NULL;
         }
         try{
+            JSONObject jsonObject = new JSONObject(paramJson);
+            int position = JSONUtil.getInt(jsonObject,"time",-1);
+            if(position == -1){
+                return PARAM_ERROR;
+            }
             Activity activity = (Activity)context;
             activity.runOnUiThread(()->baseMediaPlayer.seek(position));
             return SUCCESS;
@@ -242,27 +249,6 @@ public class PlayerToJS {
         }
     }
 
-
-    /*
-     * 资源释放
-     * 已去除,由客户端这边执行
-     * */
-    @JavascriptInterface
-    public int release(){
-        if(baseMediaPlayer == null){
-            Logger.e("播放器为空,不能释放资源");
-            return PLAYER_OBJ_NULL;
-        }
-        try{
-            Activity activity = (Activity)context;
-            activity.runOnUiThread(()->baseMediaPlayer.release());
-            return SUCCESS;
-        }catch (Exception e){
-            e.printStackTrace();
-            return EXCEPTION_ERROR;
-        }
-    }
-
     /*
     * 退出播放
     * */
@@ -480,8 +466,8 @@ public class PlayerToJS {
     private void destroyVideo(){
         try{
             if(baseMediaPlayer != null && baseMediaPlayer.getParent() != null){
-                release();
                 Activity activity = (Activity)context;
+                activity.runOnUiThread(()->baseMediaPlayer.release());
                 ViewGroup viewGroup = (ViewGroup) activity.getWindow().getDecorView();
                 viewGroup.removeView(baseMediaPlayer);
             }else{

+ 8 - 193
app/src/main/java/com/haochuan/hciptvbasic/webview/UtilToJS.java

@@ -8,6 +8,7 @@ import android.webkit.WebView;
 
 import com.haochuan.hciptvbasic.BuildConfig;
 import com.haochuan.hciptvbasic.Util.DownloadUtils;
+import com.haochuan.hciptvbasic.Util.JSONUtil;
 import com.haochuan.hciptvbasic.Util.JsUtil;
 import com.haochuan.hciptvbasic.Util.Logger;
 import com.haochuan.hciptvbasic.Util.MacUtil;
@@ -40,9 +41,6 @@ public class UtilToJS {
     private String JS_EVENT_RESPONSE ="javascript:onWebRequestResponse('%s','%s')";
 
 
-    //下载事件
-    private String JS_EVENT_DOWNLOAD_EVENT = "javascript:onDownloadEvent(%s,'%s')";
-
 
     public UtilToJS(Context context, WebView webView){
         this.context = context;
@@ -51,7 +49,6 @@ public class UtilToJS {
     }
 
     /*------------------------------------功能性函数-----------------------------------------*/
-    /*---------------------------------------------------------------------------------------*/
 
     /*
      * 将log传递给前端
@@ -69,30 +66,6 @@ public class UtilToJS {
     }
 
     /*---------------------------------获取本地参数--------------------------*/
-    /**
-     * 当前app版本号
-     */
-    @JavascriptInterface
-    public int getVersionCode() {
-        return BuildConfig.VERSION_CODE;
-    }
-
-    /**
-     * 当前app版本名
-     */
-    @JavascriptInterface
-    public String getVersionName() {
-        return BuildConfig.VERSION_NAME;
-    }
-
-
-    /**
-     * 获取Mac地址
-     */
-    @JavascriptInterface
-    public String getMac() {
-        return MacUtil.getMac(context);
-    }
 
     /*
     * 获取intent启动参数
@@ -118,164 +91,6 @@ public class UtilToJS {
 
     /*-----------------------------操作APK-------------------------------------*/
 
-    /**
-     * 判定是否安装第三方应用
-     * packageName,包名
-     * 返回 0,安装;-1,未安装
-     * **/
-    @JavascriptInterface
-    public int checkAppInstalled(String paramsJson){
-        try{
-            JSONObject jsonObject = new JSONObject(paramsJson);
-            String packageName = jsonObject.has("package_name")?jsonObject.get("package_name").toString():"";
-            return toolsUtil.checkSubAppInstalled(context,packageName)?0:-1;
-        }catch (Exception e){
-            e.printStackTrace();
-            return -1;
-        }
-    }
-
-    /*
-    * 下载
-    * */
-    @JavascriptInterface
-    public int download(String paramsJson){
-        try{
-            JSONObject jsonObject = new JSONObject(paramsJson);
-            String downloadUrl = jsonObject.has("download_url")?jsonObject.get("download_url").toString():"";
-            if(TextUtils.isEmpty(downloadUrl)){
-                Logger.e(PARAM_ERROR,"download_url is empty,download stopped");
-                return PARAM_ERROR;
-            }
-            DownloadUtils.download(downloadUrl, context.getPackageName() + getVersionCode(), "apk", new DownloadUtils.DownloadProgressListener() {
-                @Override
-                public void onDownloadStart(String fileName) {
-                    exeDownloadEvent(1,"");
-                }
-
-                @Override
-                public void onDownloadProgress(int progress) {
-                    exeDownloadEvent(2,String.valueOf(progress));
-                }
-
-                @Override
-                public void onDownloadSuccessful(String filePath) {
-                    exeDownloadEvent(3,"");
-                }
-
-                @Override
-                public void onDownloadFail(String message){
-                    exeDownloadEvent(4,message);
-                }
-            });
-            return SUCCESS;
-        }catch (Exception e){
-            e.printStackTrace();
-            return EXCEPTION_ERROR;
-        }
-    }
-
-    /*
-     * 下载并且安装
-     * */
-    @JavascriptInterface
-    public int downLoadAndInstall(String paramsJson){
-        try{
-            JSONObject jsonObject = new JSONObject(paramsJson);
-            String downloadUrl = jsonObject.has("download_url")?jsonObject.get("download_url").toString():"";
-            if(TextUtils.isEmpty(downloadUrl)){
-                Logger.e(PARAM_ERROR,"download_url is empty,download stopped");
-                return PARAM_ERROR;
-            }
-            DownloadUtils.download(downloadUrl, context.getPackageName() + getVersionCode(), "apk", new DownloadUtils.DownloadProgressListener() {
-                @Override
-                public void onDownloadStart(String fileName) {
-                    exeDownloadEvent(1,"");
-                }
-
-                @Override
-                public void onDownloadProgress(int progress) {
-                    exeDownloadEvent(2,String.valueOf(progress));
-                }
-
-                @Override
-                public void onDownloadSuccessful(String filePath) {
-                    exeDownloadEvent(3,"");
-                    install(String.format("{\"file_path\": \"%s\"}",filePath));
-                }
-
-                @Override
-                public void onDownloadFail(String message){
-                    exeDownloadEvent(4,message);
-                }
-            });
-            return SUCCESS;
-        }catch (Exception e){
-            e.printStackTrace();
-            return EXCEPTION_ERROR;
-        }
-    }
-
-    private void exeDownloadEvent(int type,String message){
-        JsUtil.evaluateJavascript(context,webView,
-                String.format(JS_EVENT_DOWNLOAD_EVENT,type,message));
-    }
-
-    /*
-    * 获得下载文件MD5值
-    * */
-    @JavascriptInterface
-    public String getMD5(String paramsJson){
-        try{
-            JSONObject jsonObject = new JSONObject(paramsJson);
-            String filePath = jsonObject.has("file_path")?jsonObject.get("file_path").toString():"";
-            if(TextUtils.isEmpty(filePath)){
-                Logger.e(PARAM_ERROR,"file_path is empty,getMD5 function stopped");
-                return "";
-            }
-            return Md5Util.getFileMD5(new File(filePath));
-        }catch (Exception e){
-            e.printStackTrace();
-            return "";
-        }
-    }
-
-    /**
-     * 安装app
-     */
-    @JavascriptInterface
-    public int install(String paramsJson) {
-        try{
-            JSONObject jsonObject = new JSONObject(paramsJson);
-            String filePath = jsonObject.has("file_path")?jsonObject.get("file_path").toString():"";
-            if(TextUtils.isEmpty(filePath)){
-                Logger.e(PARAM_ERROR,"file_path is empty,install function stopped");
-                return PARAM_ERROR;
-            }
-            ((Activity) context).runOnUiThread(() -> toolsUtil.installApk(context,filePath));
-            return SUCCESS;
-        }catch (Exception e){
-            e.printStackTrace();
-            return EXCEPTION_ERROR;
-        }
-    }
-
-    /**
-     * 卸载app
-     */
-    @JavascriptInterface
-    public int uninstall(String paramsJson) {
-        try{
-            JSONObject jsonObject = new JSONObject(paramsJson);
-            String packageName = jsonObject.has("package_name")?jsonObject.get("package_name").toString():"";
-            ((Activity) context).runOnUiThread(() -> toolsUtil.uninstall(context,packageName));
-            return SUCCESS;
-        }catch (Exception e){
-            e.printStackTrace();
-            return EXCEPTION_ERROR;
-        }
-    }
-
     /*
      * 退出app
      * */
@@ -310,8 +125,8 @@ public class UtilToJS {
     public int clientWebRequest(String paramsJson){
         try{
             JSONObject requestParams = new JSONObject(paramsJson);
-            String url = requestParams.has("url")?requestParams.get("url").toString():"";
-            String methodStr = requestParams.has("method")?requestParams.get("method").toString():"1";
+            String url = JSONUtil.getString(requestParams,"url","");
+            String methodStr = JSONUtil.getString(requestParams,"method","1");
             int method = 0;
             if(MathUtil.isDigitsOnly(methodStr)){
                 method = Integer.parseInt(methodStr);
@@ -322,12 +137,12 @@ public class UtilToJS {
             }else{
                 Logger.w("clientWebRequest 请求参数method必须为数字,目前重置为0");
             }
-            String contentType = requestParams.has("content_type")?requestParams.get("content_type").toString():"application/json";
-            String headJson = requestParams.has("head_json")?requestParams.get("head_json").toString():"{}";
-            String paramJson = requestParams.has("param_json")?requestParams.get("param_json").toString():"{}";
-            String ignore = requestParams.has("ignore_result")?requestParams.get("ignore_result").toString():"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 = requestParams.has("tag")?requestParams.get("tag").toString():"";
+            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));