From 3f41182984d69d7fae703776edd1591f48dff93f Mon Sep 17 00:00:00 2001 From: mac <user@users-MacBook-Pro.local> Date: 星期三, 22 十一月 2023 17:13:05 +0800 Subject: [PATCH] 2023年11月22日17:12:40 --- app/src/main/java/com/hdl/photovoltaic/internet/HttpServer/MyNanoHttpServer.java | 2 app/src/main/java/com/hdl/photovoltaic/utils/ByteUtils.java | 213 +++++++++++++ HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/ota/bean/GatewayDriverBean.java | 11 app/src/main/java/com/hdl/photovoltaic/ui/StartActivity.java | 11 app/src/main/java/com/hdl/photovoltaic/utils/AesUtils.java | 174 ++++++++++ app/src/main/java/com/hdl/photovoltaic/utils/Md5Utils.java | 72 ---- app/src/main/java/com/hdl/photovoltaic/ui/me/SetActivity.java | 16 app/src/main/java/com/hdl/photovoltaic/other/HdlLogLogic.java | 4 app/src/main/java/com/hdl/photovoltaic/HDLApp.java | 12 app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java | 42 +- app/src/main/java/com/hdl/photovoltaic/other/HdlOtaLogic.java | 93 +++++ app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java | 133 ++++--- app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java | 66 +++ app/src/main/java/com/hdl/photovoltaic/ui/MyPowerStationActivity.java | 27 + app/src/main/java/com/hdl/photovoltaic/ui/test/TestLogActivity.java | 4 HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/utils/ByteUtils.java | 35 -- app/src/main/java/com/hdl/photovoltaic/other/HdlAccountLogic.java | 2 17 files changed, 695 insertions(+), 222 deletions(-) diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/utils/ByteUtils.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/utils/ByteUtils.java index e4f6a9d..2c2f4a1 100644 --- a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/utils/ByteUtils.java +++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/common/utils/ByteUtils.java @@ -203,41 +203,8 @@ } public static int byteArrayToInt(byte[] b) { - int i = (b[0] & 0xFF) * 256 * 256 * 256 + (b[1] & 0xFF) * 256 * 256 + (b[2] & 0xFF) * 256 + (b[3] & 0xFF); - return i; + return (b[0] & 0xFF) * 256 * 256 * 256 + (b[1] & 0xFF) * 256 * 256 + (b[2] & 0xFF) * 256 + (b[3] & 0xFF); } - /** - * 瑙e瘑 - * - * @param contentByte 寰呰В瀵嗗緟瀛楃涓瞙exStr - * @param contentByte 瀵嗛挜 - * @return - */ - public static byte[] decrypt(byte[] contentByte) { - try { - //KEY杞崲 - Key key = new SecretKeySpec("HDLRDCENTER1985.".getBytes(), "AES"); - //瑙e瘑 - Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); - IvParameterSpec ivps = new IvParameterSpec("HDLRDCENTER1985.".getBytes()); - cipher.init(Cipher.DECRYPT_MODE, key, ivps); - byte[] result = cipher.doFinal(contentByte); - return result; - } catch (NoSuchAlgorithmException e) { - LogUtils.e(e.getMessage()); - } catch (InvalidKeyException e) { - LogUtils.e(e.getMessage()); - } catch (NoSuchPaddingException e) { - LogUtils.e(e.getMessage()); - } catch (BadPaddingException e) { - LogUtils.e(e.getMessage()); - } catch (IllegalBlockSizeException e) { - LogUtils.e(e.getMessage()); - } catch (InvalidAlgorithmParameterException e) { - LogUtils.e(e.getMessage()); - } - return null; - } } diff --git a/HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/ota/bean/GatewayDriverBean.java b/HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/ota/bean/GatewayDriverBean.java index 0fd41fd..4d573f1 100644 --- a/HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/ota/bean/GatewayDriverBean.java +++ b/HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/ota/bean/GatewayDriverBean.java @@ -7,7 +7,7 @@ /** * Created by jlchen on 12/17/21. - * + * <p> * 缃戝叧璁惧椹卞姩 */ public class GatewayDriverBean implements Serializable { @@ -18,6 +18,15 @@ private String driverNameCn;//椹卞姩涓枃鍚嶇О private String imageId;//imageId private String version;//鐗堟湰鍙� + private String localUrl;//鍗囩骇椹卞姩鏂囦欢鏈湴璺緞 + + public String getLocalUrl() { + return localUrl == null ? "" : localUrl; + } + + public void setLocalUrl(String localUrl) { + this.localUrl = localUrl; + } @NonNull public String getDriverCode() { diff --git a/app/src/main/java/com/hdl/photovoltaic/HDLApp.java b/app/src/main/java/com/hdl/photovoltaic/HDLApp.java index 2abcc75..17c4dda 100644 --- a/app/src/main/java/com/hdl/photovoltaic/HDLApp.java +++ b/app/src/main/java/com/hdl/photovoltaic/HDLApp.java @@ -1,10 +1,13 @@ package com.hdl.photovoltaic; import android.app.Application; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.ServiceConnection; import android.content.res.Configuration; import android.content.res.Resources; +import android.os.IBinder; import android.util.DisplayMetrics; @@ -15,6 +18,10 @@ import com.hdl.linkpm.sdk.utils.HDLSDKSPUtils; import com.hdl.photovoltaic.config.AppConfigManage; import com.hdl.photovoltaic.config.UserConfigManage; +import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpServer; +import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpService; +import com.hdl.photovoltaic.other.HdlFileLogic; +import com.hdl.photovoltaic.other.HdlOtaLogic; import com.hdl.photovoltaic.ui.HomeLoginActivity; import com.hdl.photovoltaic.uni.HDLUniMPSDKManager; import com.hdl.photovoltaic.utils.AppManagerUtils; @@ -110,6 +117,8 @@ //4.鍒濆鍖朞KHttpLog 鏄惁寮�鍚� setOKHttpLogOpen(isLogEnabled); + + } /** @@ -192,6 +201,9 @@ } + + + // /** // * 鍒濆鍖杣ni app mqtt // */ diff --git a/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java b/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java index 034fa23..7ab633b 100644 --- a/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java +++ b/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java @@ -9,13 +9,11 @@ import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; import com.hdl.hdlhttp.HxHttp; -import com.hdl.hdlhttp.HxHttpBuilder; import com.hdl.hdlhttp.utils.GsonConvert; import com.hdl.linkpm.sdk.core.api.HDLCloudUserApi; import com.hdl.linkpm.sdk.core.exception.HDLException; +import com.hdl.linkpm.sdk.core.interceptor.HDLSmartHeader; import com.hdl.linkpm.sdk.core.response.HDLResponse; -import com.hdl.linkpm.sdk.user.bean.HDLLoginBean; -import com.hdl.linkpm.sdk.user.callback.ILoginCallBack; import com.hdl.linkpm.sdk.utils.HDLExceptionSubmitUtils; import com.hdl.photovoltaic.bean.HttpResponsePack; import com.hdl.photovoltaic.config.AppConfigManage; @@ -23,7 +21,7 @@ import com.hdl.photovoltaic.listener.BaseSuccessFailureCallBeak; import com.hdl.photovoltaic.listener.CloudCallBeak; import com.hdl.photovoltaic.other.HdlLogLogic; -import com.hdl.photovoltaic.utils.HDLMD5Utils; +import com.hdl.photovoltaic.utils.Md5Utils; import java.io.File; import java.io.IOException; @@ -34,15 +32,16 @@ import java.util.Objects; import io.reactivex.rxjava3.disposables.Disposable; +import io.reactivex.rxjava3.functions.Consumer; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; -import okhttp3.Headers; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; +import okhttp3.ResponseBody; public class HttpClient { @@ -75,30 +74,25 @@ String requestUrl = HDLCloudUserApi.getRequestUrl(api); Gson gson = new Gson(); System.out.println("http->鍙戦��->" + requestUrl + "\r\n" + body); - return HxHttp.builder() - .url(requestUrl) - .raw(body) - .build() - .post() - .subscribeWith(new HDLResponse<String>() { - @Override - public void onResponse(String str) { - if (callBack != null) { - callBack.onSuccess(str); + return HxHttp.builder().url(requestUrl).raw(body).build().post().subscribeWith(new HDLResponse<String>() { + @Override + public void onResponse(String str) { + if (callBack != null) { + callBack.onSuccess(str); // System.out.println("http->鍥炲->" + requestUrl + "\r\n" + gson.fromJson(str, JsonObject.class).toString()); - System.out.println("http->鍥炲->" + requestUrl + "\r\n" + str); - } - } + System.out.println("http->鍥炲->" + requestUrl + "\r\n" + str); + } + } - @Override - public void onFailure(HDLException e) { - HDLExceptionSubmitUtils.submit(requestUrl, body, e); - if (callBack != null) { - callBack.onFailure(e); - System.out.println("http->鍥炲->" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}"); - } - } - }); + @Override + public void onFailure(HDLException e) { + HDLExceptionSubmitUtils.submit(requestUrl, body, e); + if (callBack != null) { + callBack.onFailure(e); + System.out.println("http->鍥炲->" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}"); + } + } + }); } @@ -117,31 +111,60 @@ String requestUrl = HDLCloudUserApi.getRequestUrl(api); Gson gson = new Gson(); System.out.println("http->鍙戦��->" + requestUrl + "\r\n" + body); - return HxHttp.builder() - .url(requestUrl) - .file(body) - .build() - .post() - .subscribeWith(new HDLResponse<String>() { - @Override - public void onResponse(String str) { - if (callBack != null) { - callBack.onSuccess(str); + return HxHttp.builder().url(requestUrl).file(body).build().post().subscribeWith(new HDLResponse<String>() { + @Override + public void onResponse(String str) { + if (callBack != null) { + callBack.onSuccess(str); // System.out.println("http->鍥炲->" + requestUrl + "\r\n" + gson.fromJson(str, JsonObject.class).toString()); - HdlLogLogic.print("http->鍥炲->" + requestUrl + "\r\n" + str, true); + HdlLogLogic.print("http->鍥炲->" + requestUrl + "\r\n" + str, true); + } + } + + @Override + public void onFailure(HDLException e) { + HDLExceptionSubmitUtils.submit(requestUrl, body, e); + if (callBack != null) { + callBack.onFailure(e); + HdlLogLogic.print("http->鍥炲->" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", true); + } + } + }); + + } + + + /** + * 涓嬭浇鏂囦欢 + * + * @param callBack - + */ + public Disposable downLoadFile(String url, CloudCallBeak<ResponseBody> callBack) { + return HxHttp.builder() + .url(url) + .headers(HDLSmartHeader.IGNORE_SIGN_HEADER, 1) + .build() + .download() + .subscribe(new Consumer<ResponseBody>() { + @Override + public void accept(ResponseBody responseBody) throws Exception { + if (callBack != null) { + callBack.onSuccess(responseBody); } } - + }, new Consumer<Throwable>() { @Override - public void onFailure(HDLException e) { - HDLExceptionSubmitUtils.submit(requestUrl, body, e); + public void accept(Throwable throwable) throws Exception { + /** + * 缃戠粶璇锋眰澶辫触 + * 鍏蜂綋鍙互鑷繁瀹炵幇 + */ + HDLExceptionSubmitUtils.submit(url, null, throwable); if (callBack != null) { - callBack.onFailure(e); - HdlLogLogic.print("http->鍥炲->" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", true); + callBack.onFailure(new HDLException(403, throwable.getMessage())); } } }); - } @@ -157,13 +180,13 @@ } /** - * 璇锋眰鏈嶅姟鍣紙涓婁紶鏂囦欢锛� + * 涓婁紶鏂囦欢 * * @param api 璇锋眰鎺ュ彛 * @param body 璇锋眰鍙傛暟(json) * @param callBack 鍥炶皟 */ - public Disposable requestHttpFile(String api, File body, CloudCallBeak<String> callBack) { + public Disposable uploadingFile(String api, File body, CloudCallBeak<String> callBack) { return requestFilePost(api, body, callBack); } @@ -212,11 +235,8 @@ HdlLogLogic.print("http->鍙戦��->" + fullUrl + "\r\n" + json, false); OkHttpClient okHttpClient = new OkHttpClient(); RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), getJson(json)); - final Request request = new Request.Builder() - .url(fullUrl)//璇锋眰鐨剈rl - .addHeader("Authorization", UserConfigManage.getInstance().getHeaderPrefix() + UserConfigManage.getInstance().getToken()) - .post(requestBody) - .build(); + final Request request = new Request.Builder().url(fullUrl)//璇锋眰鐨剈rl + .addHeader("Authorization", UserConfigManage.getInstance().getHeaderPrefix() + UserConfigManage.getInstance().getToken()).post(requestBody).build(); if (isExecute) { Response response = okHttpClient.newCall(request).execute();//鍚屾 if (response.isSuccessful()) { @@ -285,9 +305,8 @@ * @param json 鎵�鏈夊瓧娈典娇鐢╱rlParameter鎷兼帴锛岄櫎浜哸ppSecret */ private String getSign(JsonObject json, String appSecret) { - String builder = jsonToUrlParameter(json) + - appSecret; - return HDLMD5Utils.encodeMD5(builder); + String builder = jsonToUrlParameter(json) + appSecret; + return Md5Utils.encodeMD5(builder); } @@ -318,13 +337,11 @@ * @return - */ private boolean IfValueNeedSign(String valueStr) { - if (TextUtils.isEmpty(valueStr)) - return false; + if (TextUtils.isEmpty(valueStr)) return false; final char[] strChar = valueStr.substring(0, 1).toCharArray(); final char firstChar = strChar[0]; //System.out.println("getJSONType firstChar = "+firstChar); - if (firstChar != '{' && firstChar != '[') - return true; + if (firstChar != '{' && firstChar != '[') return true; return false; } diff --git a/app/src/main/java/com/hdl/photovoltaic/internet/HttpServer/MyNanoHttpServer.java b/app/src/main/java/com/hdl/photovoltaic/internet/HttpServer/MyNanoHttpServer.java index 7d0b5f3..ae5e6f3 100644 --- a/app/src/main/java/com/hdl/photovoltaic/internet/HttpServer/MyNanoHttpServer.java +++ b/app/src/main/java/com/hdl/photovoltaic/internet/HttpServer/MyNanoHttpServer.java @@ -70,7 +70,9 @@ String fileName = ((HTTPSession) session).getUri(); String data = ""; if (fileName.equals(HdlFileLogic.getInstance().getDriveRootPath())) { + data = HdlFileLogic.getInstance().readFile(fileName); + } //鍝嶅簲瀹㈡埛绔� return newFixedLengthResponse(data); diff --git a/app/src/main/java/com/hdl/photovoltaic/other/HdlAccountLogic.java b/app/src/main/java/com/hdl/photovoltaic/other/HdlAccountLogic.java index 44fe12e..9afe853 100644 --- a/app/src/main/java/com/hdl/photovoltaic/other/HdlAccountLogic.java +++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlAccountLogic.java @@ -255,7 +255,7 @@ */ public void uploadUserHeadImage_C(File file, CloudCallBeak<String> cloudCallBeak) { String requestUrl = HttpApi.C_POST_GET_IMAGE_URL; - HttpClient.getInstance().requestHttpFile(requestUrl, file, new CloudCallBeak<String>() { + HttpClient.getInstance().uploadingFile(requestUrl, file, new CloudCallBeak<String>() { @Override public void onSuccess(String jsonStr) { if (cloudCallBeak != null) { diff --git a/app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java b/app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java index 10569f4..012a181 100644 --- a/app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java +++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java @@ -15,6 +15,8 @@ import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; /** @@ -96,7 +98,7 @@ /** * 鑾峰彇銆愭棩蹇楁枃浠躲�戝叏璺緞 */ - public String getLogFilePath() { + public String getLogFileNamePath() { return getCurrentHomeRootPath() + "/log.txt"; } @@ -115,7 +117,7 @@ * @return 鍏ㄨ矾寰� */ public String getDrivePathFileName(String driverCode, String version) { - String driverFileName = driverCode + "_" + version + ".zip"; + String driverFileName = driverCode + "_" + version + ".zip"; return HdlFileLogic.getInstance().getDriveRootPath() + "/" + driverFileName; } @@ -242,7 +244,7 @@ * @param fullPath 鍏ㄨ矾寰� * @param data 鏁版嵁 */ - public void writeFile(String fullPath, String data) { + public void writeFile(String fullPath, byte[] data) { try { File file = new File(fullPath); if (!file.exists()) { @@ -252,7 +254,7 @@ } } FileOutputStream d = new FileOutputStream(file); - d.write(data.getBytes()); + d.write(data); d.flush(); d.close(); System.out.println("鍐欏叆鏂囦欢鎴愬姛==" + fullPath); @@ -260,6 +262,16 @@ System.out.println("鍐欏叆鏂囦欢鏈夊紓甯�==" + e.getMessage()); } + } + + /** + * 鍐欏叆鏂囦欢 + * + * @param fullPath 鍏ㄨ矾寰� + * @param data 鏁版嵁 + */ + public void writeFile(String fullPath, String data) { + writeFile(fullPath, data.getBytes()); } /** @@ -302,25 +314,55 @@ * @param filePath 鍏ㄨ矾寰� * @return 鏁版嵁 */ - public String readFile(String filePath) { + public byte[] readFileByte(String filePath) { try { if (!isBoolean(filePath)) { - return ""; + return null; } - String fileContent = ""; File f = new File(filePath); if (!f.exists()) { - return fileContent; + return null; } FileInputStream fis = new FileInputStream(f); - byte[] bytes = new byte[fis.available()]; - fis.read(bytes); +// byte[] bytes = new byte[fis.available()]; + byte[] readByte = new byte[1024]; + List<Byte> byteList = new ArrayList<>(); + int len = 0; + while ((len = fis.read(readByte)) != -1) { + for (int i = 0; i < len; i++) { + byteList.add(readByte[i]); + } + } + Byte[] forbByte = byteList.toArray(new Byte[0]); + byte[] bytes = new byte[forbByte.length]; + for (int i = 0; i < forbByte.length; i++) { + bytes[i] = forbByte[i]; + } fis.close(); System.out.println("璇诲彇鏂囦欢鎴愬姛===" + filePath); - fileContent = new String(bytes); - return fileContent; + return bytes; } catch (Exception e1) { System.out.println("璇诲彇鏂囦欢鏈夊紓甯�===" + e1.getMessage()); + return null; + } + + } + + /** + * 璇诲彇鏂囦欢 + * + * @param filePath 鍏ㄨ矾寰� + * @return 鏁版嵁 + */ + public String readFile(String filePath) { + try { + + byte[] bytes = readFileByte(filePath); + if (bytes == null) { + return ""; + } + return new String(bytes); + } catch (Exception e1) { return ""; } diff --git a/app/src/main/java/com/hdl/photovoltaic/other/HdlLogLogic.java b/app/src/main/java/com/hdl/photovoltaic/other/HdlLogLogic.java index 896bfc9..75225f8 100644 --- a/app/src/main/java/com/hdl/photovoltaic/other/HdlLogLogic.java +++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlLogLogic.java @@ -76,7 +76,7 @@ public static void writeLog(String strLog) { synchronized (HdlLogLogic.class) { strLog = TimeUtils.getTimeFromTimestamp(System.currentTimeMillis()) + "s%" + " " + strLog;//鍔犳墦鍗版椂闂�(s%鍗犱綅绗︼紝璁剧疆鏄剧ず鏃堕棿涓嶅悓瀛椾綋棰滆壊) - HdlFileLogic.getInstance().appendFile(HdlFileLogic.getInstance().getLogFilePath(), strLog); + HdlFileLogic.getInstance().appendFile(HdlFileLogic.getInstance().getLogFileNamePath(), strLog); } } @@ -86,7 +86,7 @@ * @return 鏃ュ織鍐呭 */ public static String readLog() { - return HdlFileLogic.getInstance().readFile(HdlFileLogic.getInstance().getLogFilePath()); + return HdlFileLogic.getInstance().readFile(HdlFileLogic.getInstance().getLogFileNamePath()); } diff --git a/app/src/main/java/com/hdl/photovoltaic/other/HdlOtaLogic.java b/app/src/main/java/com/hdl/photovoltaic/other/HdlOtaLogic.java index 96a2d53..77ed570 100644 --- a/app/src/main/java/com/hdl/photovoltaic/other/HdlOtaLogic.java +++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlOtaLogic.java @@ -28,13 +28,25 @@ import com.hdl.photovoltaic.internet.api.TopicApi; import com.hdl.photovoltaic.listener.CloudCallBeak; import com.hdl.photovoltaic.listener.LinkCallBack; +import com.hdl.photovoltaic.utils.AesUtils; import com.hdl.photovoltaic.utils.AppManagerUtils; +import com.hdl.photovoltaic.utils.Md5Utils; import com.hdl.sdk.link.common.exception.HDLLinkException; +import com.hdl.sdk.link.common.utils.ByteUtils; import com.hdl.sdk.link.core.callback.HDLLinkCallBack; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; import java.lang.reflect.Type; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.Objects; +import java.util.logging.FileHandler; + +import okhttp3.ResponseBody; /** @@ -449,9 +461,24 @@ * * @param downloadUrl 浜戠鍥轰欢鍦板潃 */ - public void getDeviceDriverDownloadFile(String downloadUrl, CloudCallBeak<DownloadUrlBean> callBack) { - String requestUrl = HttpApi.POST_OTA_GET_LinkDeviceFirmwareDownloadUrl; - JsonObject json = new JsonObject(); + public void getDeviceDriverDownloadFile(String downloadUrl, CloudCallBeak<ResponseBody> callBack) { + HttpClient.getInstance().downLoadFile(downloadUrl, new CloudCallBeak<ResponseBody>() { + @Override + public void onSuccess(ResponseBody s) { + + if (callBack != null) { + callBack.onSuccess(s); + } + } + + @Override + public void onFailure(HDLException e) { + if (callBack != null) { + callBack.onFailure(e); + } + } + }); + } @@ -525,4 +552,64 @@ } } + /** + * 澶勭悊涓嬭浇鏂囦欢鏁版嵁 + * + * @param zipData 鏂囦欢鏁版嵁娴佸璞� + * @param md5 妫�楠屾枃浠禡D5鍊� + * @return 杩斿洖瑙e瘑鐨刡yte[] + */ + public byte[] disposeDownLoadFile(ResponseBody zipData, String md5) { + if (zipData == null) { + return null; + } + if (TextUtils.isEmpty(md5)) { + return null; + } + InputStream is = null; + List<Byte> byteList = new ArrayList<>(); + byte[] decrypt = null; + try { + is = zipData.byteStream(); + int total = (int) zipData.contentLength(); + byte[] buf = new byte[1024*4]; + long sum = 0; + int len = 0; + while ((len = is.read(buf)) != -1) { + sum += len; + //todo 涓嬭浇涓紝鍙互鑷鍔犲叆杩涘害鏉�(鍙互骞挎挱鍑哄幓) + int progress = (int) (sum * 1.0f / total * 100); + for (int i = 0; i < len; i++) { + byteList.add(buf[i]); + } + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (is != null) { + is.close(); + } + Byte[] sourceByte = byteList.toArray(new Byte[0]); + byte[] bytes = new byte[sourceByte.length]; + for (int i = 0; i < sourceByte.length; i++) { + bytes[i] = sourceByte[i]; + } + //涓嬭浇鏂囦欢闇�瑕佽В瀵嗕箣鍚庡啀杩涜md5鍘诲仛姣斿 + decrypt = AesUtils.decrypt(bytes); + if (decrypt != null) { + String fileMD5 = Md5Utils.encodeMD5(decrypt); + if (TextUtils.isEmpty(fileMD5) || !Objects.equals(fileMD5, md5)) { + //鏍¢獙澶辫触,缃┖ + decrypt = null; + } + } + } catch (IOException ignored) { + + } + } + return decrypt; + } + + } diff --git a/app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java b/app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java index a5be3d4..18a7ce7 100644 --- a/app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java +++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlUniLogic.java @@ -53,6 +53,7 @@ import java.util.Objects; import io.dcloud.feature.unimp.DCUniMPJSCallback; +import okhttp3.ResponseBody; /** * uni灏忕▼搴忕殑閫昏緫 @@ -530,12 +531,15 @@ File file = new File(driverFileList); if (file.list() != null) { for (int i = 0; i < Objects.requireNonNull(file.list()).length; i++) { - String path = Objects.requireNonNull(file.list())[i]; - String[] ary = path.split("/"); - String[] driverInfoAry = ary[ary.length - 1].split("_"); + String fileName = Objects.requireNonNull(file.list())[i]; + if (!fileName.contains(driveCode)) { + continue; + } + String[] driverInfoAry = fileName.split("_"); GatewayDriverBean gatewayDriverBean = new GatewayDriverBean(); gatewayDriverBean.setDriverCode(driverInfoAry[0]); gatewayDriverBean.setVersion(driverInfoAry[1].replace(".zip", "")); + gatewayDriverBean.setLocalUrl(HdlFileLogic.getInstance().getDrivePathFileName(gatewayDriverBean.getDriverCode(), gatewayDriverBean.getVersion())); list.add(gatewayDriverBean); } } @@ -557,32 +561,22 @@ String imageId = getKeyValue("imageId", getKeyValue("data", data));//椹卞姩闀滃儚id String version = getKeyValue("version", getKeyValue("data", data));//椹卞姩鐗堟湰 String driverVersionId = getKeyValue("driverVersionId", getKeyValue("data", data));//椹卞姩鐗堟湰id - HdlOtaLogic.getInstance().getDeviceDriverDownloadUrl(driverVersionId, new CloudCallBeak<DownloadUrlBean>() { + String localUrl = getKeyValue("localUrl", getKeyValue("data", data));//鍌ㄥ瓨鍦ㄦ湰鍦伴┍鍔ㄦ枃浠惰矾寰� + String url = getKeyValue("url", getKeyValue("data", data));//浜戠涓婃枃浠跺湴鍧� + String md5 = getKeyValue("md5", getKeyValue("data", data));//浜戠涓婃枃浠禡D5 + HdlOtaLogic.getInstance().getDeviceDriverDownloadFile(url, new CloudCallBeak<ResponseBody>() { @Override - public void onSuccess(DownloadUrlBean downloadUrlBean) { - if (downloadUrlBean == null) { - uniCallbackData(null, -2, "涓嬭浇閾炬帴鎵句笉鍒�", callback); - return; - } - HdlOtaLogic.getInstance().getDeviceDriverDownloadFile(downloadUrlBean.getDownloadUrl(), new CloudCallBeak<DownloadUrlBean>() { - @Override - public void onSuccess(DownloadUrlBean obj) { - Gson gson = new Gson(); - String json = gson.toJson(obj); - String drivePathFileName = HdlFileLogic.getInstance().getDrivePathFileName(driverCode, version); - HdlFileLogic.getInstance().writeFile(drivePathFileName, json); - - } - - @Override - public void onFailure(HDLException e) { - - } - }); + public void onSuccess(ResponseBody responseBody) { + byte[] bytes = HdlOtaLogic.getInstance().disposeDownLoadFile(responseBody, md5); + HdlFileLogic.getInstance().deleteFile(localUrl);//鍒犻櫎鏃ф枃浠� + String drivePathFileName = HdlFileLogic.getInstance().getDrivePathFileName(driverCode, version); + HdlFileLogic.getInstance().writeFile(drivePathFileName, bytes);//鍐欏叆鏂版枃浠� + HdlLogLogic.print("鍐欏叆鏂伴┍鍔ㄦ枃浠跺埌鍐呭瓨鎴愬姛.", false); } @Override public void onFailure(HDLException e) { + HdlLogLogic.print("涓嬭浇椹卞姩鏂囦欢鍒板唴瀛樺け璐�.", false); uniCallbackData(null, e.getCode(), e.getMsg(), callback); } }); diff --git a/app/src/main/java/com/hdl/photovoltaic/ui/MyPowerStationActivity.java b/app/src/main/java/com/hdl/photovoltaic/ui/MyPowerStationActivity.java index c895a5a..48627f5 100644 --- a/app/src/main/java/com/hdl/photovoltaic/ui/MyPowerStationActivity.java +++ b/app/src/main/java/com/hdl/photovoltaic/ui/MyPowerStationActivity.java @@ -4,34 +4,38 @@ import androidx.appcompat.content.res.AppCompatResources; import android.Manifest; -import android.content.Intent; +import android.content.ComponentName; +import android.content.ServiceConnection; import android.content.pm.PackageManager; import android.os.Bundle; +import android.os.IBinder; import android.os.Process; import android.text.TextUtils; +import android.util.Log; import android.view.View; import com.google.gson.Gson; import com.hdl.linkpm.sdk.core.exception.HDLException; import com.hdl.linkpm.sdk.home.type.HomeType; -import com.hdl.photovoltaic.HDLApp; +import com.hdl.linkpm.sdk.ota.bean.GatewayDriverBean; import com.hdl.photovoltaic.R; import com.hdl.photovoltaic.base.CustomBaseActivity; import com.hdl.photovoltaic.bean.MqttInfo; import com.hdl.photovoltaic.config.UserConfigManage; import com.hdl.photovoltaic.databinding.ActivityMyPowerStationBinding; -import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpServer; -import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpService; import com.hdl.photovoltaic.listener.CloudCallBeak; import com.hdl.photovoltaic.other.HdlDeviceLogic; import com.hdl.photovoltaic.other.HdlFileLogic; import com.hdl.photovoltaic.other.HdlLogLogic; import com.hdl.photovoltaic.other.HdlMqttLogic; +import com.hdl.photovoltaic.other.HdlOtaLogic; import com.hdl.photovoltaic.other.HdlThreadLogic; import com.hdl.photovoltaic.other.HdlUniLogic; import com.hdl.photovoltaic.ui.bean.DeviceRemoteInfo; import com.hdl.photovoltaic.uni.HDLUniMP; +import com.hdl.photovoltaic.utils.AesUtils; import com.hdl.photovoltaic.utils.AppManagerUtils; +import com.hdl.photovoltaic.utils.Md5Utils; import com.hdl.photovoltaic.utils.PermissionUtils; import com.hdl.sdk.link.HDLLinkLocalSdk; import com.hdl.sdk.link.common.event.EventListener; @@ -45,7 +49,15 @@ import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; import java.util.List; +import java.util.Objects; + + +import okhttp3.ResponseBody; /** @@ -78,12 +90,6 @@ //鍒濆鍖杕qtt瀹㈡埛绔� initMqttClient(); - String path = HdlFileLogic.getInstance().getDriveRootPath() + "/036F6C1B.zip"; - MyNanoHttpServer.getInstance(AppManagerUtils.getAppManager().getIPAddress(HDLApp.getInstance())); - //MyNanoHttpServer.getInstance("127.0.0.1"); - Intent intentServer = new Intent(); - intentServer.setClass(HDLApp.getInstance(), MyNanoHttpService.class); - startService(intentServer); } @@ -345,4 +351,5 @@ super.onBackPressed(); } + } \ No newline at end of file diff --git a/app/src/main/java/com/hdl/photovoltaic/ui/StartActivity.java b/app/src/main/java/com/hdl/photovoltaic/ui/StartActivity.java index f85c6af..07657c9 100644 --- a/app/src/main/java/com/hdl/photovoltaic/ui/StartActivity.java +++ b/app/src/main/java/com/hdl/photovoltaic/ui/StartActivity.java @@ -1,17 +1,25 @@ package com.hdl.photovoltaic.ui; +import android.content.ComponentName; +import android.content.Context; import android.content.Intent; +import android.content.ServiceConnection; import android.os.Bundle; +import android.os.IBinder; import com.hdl.linkpm.sdk.core.exception.HDLException; +import com.hdl.photovoltaic.HDLApp; import com.hdl.photovoltaic.R; import com.hdl.photovoltaic.base.CustomBaseActivity; import com.hdl.photovoltaic.config.UserConfigManage; +import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpServer; import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpService; import com.hdl.photovoltaic.listener.CloudCallBeak; +import com.hdl.photovoltaic.other.HdlOtaLogic; import com.hdl.photovoltaic.other.HdlResidenceLogic; import com.hdl.photovoltaic.ui.bean.HouseIdBean; +import com.hdl.photovoltaic.utils.AppManagerUtils; import java.util.List; @@ -24,6 +32,7 @@ @Override public void onBindView(Bundle savedInstanceState) { + //7澶╁唴鍏嶇櫥褰� if (UserConfigManage.getInstance().isAutoLogin()) { // showLoading(); @@ -57,4 +66,6 @@ } + + } \ No newline at end of file diff --git a/app/src/main/java/com/hdl/photovoltaic/ui/me/SetActivity.java b/app/src/main/java/com/hdl/photovoltaic/ui/me/SetActivity.java index 254ced8..4606f79 100644 --- a/app/src/main/java/com/hdl/photovoltaic/ui/me/SetActivity.java +++ b/app/src/main/java/com/hdl/photovoltaic/ui/me/SetActivity.java @@ -1,7 +1,12 @@ package com.hdl.photovoltaic.ui.me; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; import android.os.Bundle; +import android.os.IBinder; import android.view.View; import com.hdl.linkpm.sdk.HDLLinkPMSdk; @@ -11,9 +16,12 @@ import com.hdl.photovoltaic.base.CustomBaseActivity; import com.hdl.photovoltaic.config.UserConfigManage; import com.hdl.photovoltaic.databinding.ActivitySetBinding; +import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpServer; +import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpService; import com.hdl.photovoltaic.listener.CloudCallBeak; import com.hdl.photovoltaic.other.HdlAccountLogic; import com.hdl.photovoltaic.other.HdlThreadLogic; +import com.hdl.photovoltaic.utils.AppManagerUtils; import com.hdl.photovoltaic.widget.ConfirmationInputDialog; /** @@ -31,7 +39,6 @@ @Override public void onBindView(Bundle savedInstanceState) { - //鍒濆鍖� initView(); //鍒濆鍖栫晫闈㈢洃鍚櫒 @@ -76,9 +83,9 @@ private void initView() { viewBinding.toolbarTopFragmentHouseListRl.topTitleTv.setText(R.string.me_set); - viewBinding.toolbarTopFragmentHouseListRl.topTitleTv.setTextColor(getResources().getColor(R.color.text_030D1C,null)); + viewBinding.toolbarTopFragmentHouseListRl.topTitleTv.setTextColor(getResources().getColor(R.color.text_030D1C, null)); viewBinding.toolbarTopFragmentHouseListRl.topBackBtn.setVisibility(View.VISIBLE); - viewBinding.toolbarTopFragmentHouseListRl.topBarView.setBackgroundColor(getResources().getColor(R.color.text_FFFFFFFF,null)); + viewBinding.toolbarTopFragmentHouseListRl.topBarView.setBackgroundColor(getResources().getColor(R.color.text_FFFFFFFF, null)); viewBinding.setNicknameIl.sllLlRlNameTv.setText(R.string.set_nickname); viewBinding.setChangePasswordIl.sllLlRlNameTv.setText(R.string.home_login_change_password); viewBinding.setCloseAccountIl.sllLlRlNameTv.setText(R.string.set_close_account); @@ -125,7 +132,7 @@ public void onSuccess(Boolean obj) { UserConfigManage.getInstance().setUserName(str); UserConfigManage.getInstance().Save(); - HdlThreadLogic.tipFlashingBox(_mActivity, true, getString(R.string.home_login_change_name_succeed),0); + HdlThreadLogic.tipFlashingBox(_mActivity, true, getString(R.string.home_login_change_name_succeed), 0); } @Override @@ -146,6 +153,7 @@ } + /** * 閫�鍑虹櫥褰曡姹傜殑鏂规硶 */ diff --git a/app/src/main/java/com/hdl/photovoltaic/ui/test/TestLogActivity.java b/app/src/main/java/com/hdl/photovoltaic/ui/test/TestLogActivity.java index 8bb76ca..8656407 100644 --- a/app/src/main/java/com/hdl/photovoltaic/ui/test/TestLogActivity.java +++ b/app/src/main/java/com/hdl/photovoltaic/ui/test/TestLogActivity.java @@ -114,7 +114,7 @@ if (value > 1) { HdlFileLogic.getInstance().deleteDirectory(HdlFileLogic.getInstance().getCurrentUserRootPath()); } else { - HdlFileLogic.getInstance().deleteFile(HdlFileLogic.getInstance().getLogFilePath()); + HdlFileLogic.getInstance().deleteFile(HdlFileLogic.getInstance().getLogFileNamePath()); } viewBinding.replyContentTv.setText(""); } @@ -135,7 +135,7 @@ viewBinding.homeIdTv.setText(homeId); viewBinding.homeNameTv.setText(homeName); - HdlFileLogic.getInstance().delFileLien(HdlFileLogic.getInstance().getLogFilePath(), 2); + HdlFileLogic.getInstance().delFileLien(HdlFileLogic.getInstance().getLogFileNamePath(), 2); String content = HdlLogLogic.readLog(); int startIndex = 0; int endIndex; diff --git a/app/src/main/java/com/hdl/photovoltaic/utils/AesUtils.java b/app/src/main/java/com/hdl/photovoltaic/utils/AesUtils.java new file mode 100644 index 0000000..7da21a6 --- /dev/null +++ b/app/src/main/java/com/hdl/photovoltaic/utils/AesUtils.java @@ -0,0 +1,174 @@ +package com.hdl.photovoltaic.utils; + +import android.annotation.SuppressLint; +import android.text.TextUtils; + +import com.hdl.sdk.link.common.utils.LogUtils; + +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.Key; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.KeyGenerator; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; + +import io.dcloud.common.adapter.util.CryptoProvider; + +/** + * AES鍔犲瘑瑙e瘑 + */ +public class AesUtils { + + private static final String SHA1PRNG = "HDLRDCENTER1985."; // SHA1PRNG 寮洪殢鏈虹瀛愮畻娉� + private static final String AES = "AES"; //AES 鍔犲瘑 + private static final String CIPHERMODE = "AES/CBC/PKCS7Padding"; //AES绠楁硶/CBC妯″紡/PKCS5Padding濉厖妯″紡 + + /** + * 瑙e瘑 + * + * @param contentByte 寰呰В瀵嗗緟瀛楃涓瞙exStr + * @return 瑙e瘑鍚巄yte[]鏁扮粍 + */ + public static byte[] decrypt(byte[] contentByte) { + try { + //KEY杞崲 + Key key = new SecretKeySpec(SHA1PRNG.getBytes(), AES); + //瑙e瘑 + Cipher cipher = Cipher.getInstance(CIPHERMODE); + IvParameterSpec ivps = new IvParameterSpec(SHA1PRNG.getBytes()); + cipher.init(Cipher.DECRYPT_MODE, key, ivps); + return cipher.doFinal(contentByte); + } catch (NoSuchAlgorithmException + | InvalidKeyException + | NoSuchPaddingException + | BadPaddingException + | IllegalBlockSizeException + | InvalidAlgorithmParameterException e) { + LogUtils.e(e.getMessage()); + } + return null; + } + + + /** + * 鍔犲瘑 + */ + public static String encrypt(String key, String cleartext) { + if (TextUtils.isEmpty(cleartext)) { + return cleartext; + } + try { + byte[] result = encrypt(key, cleartext.getBytes()); + return parseByte2HexStr(result); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + /** + * 鍔犲瘑 + */ + public static byte[] encrypt(String key, byte[] clear) throws Exception { + byte[] raw = getRawKey(key.getBytes()); + SecretKeySpec keySpec = new SecretKeySpec(raw, AES); + Cipher cipher = Cipher.getInstance(CIPHERMODE); + cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(new + byte[cipher.getBlockSize()])); + return cipher.doFinal(clear); + } + + /** + * 瑙e瘑 + */ + public static String decrypt(String key, String encrypted) { + if (TextUtils.isEmpty(encrypted)) { + return encrypted; + } + try { + byte[] enc = parseHexStr2Byte(encrypted); + byte[] result = decrypt(key, enc); + return new String(result); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + /** + * 瑙e瘑 + */ + public static byte[] decrypt(String key, byte[] encrypted) throws Exception { + byte[] raw = getRawKey(key.getBytes()); + SecretKeySpec keySpec = new SecretKeySpec(raw, AES); + Cipher cipher = Cipher.getInstance(CIPHERMODE); + cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(new + byte[cipher.getBlockSize()])); + return cipher.doFinal(encrypted); + } + + + /** + * 澶勭悊瀵嗛挜 + */ + @SuppressLint("DeletedProvider") + public static byte[] getRawKey(byte[] seed) throws Exception { + KeyGenerator kgen = KeyGenerator.getInstance(AES); + //for android + SecureRandom sr = null; + // 鍦�4.2浠ヤ笂鐗堟湰涓紝SecureRandom鑾峰彇鏂瑰紡鍙戠敓浜嗘敼鍙� + if (android.os.Build.VERSION.SDK_INT >= 17) { + sr = SecureRandom.getInstance(SHA1PRNG, new CryptoProvider()); + } else { + sr = SecureRandom.getInstance(SHA1PRNG); + } + + sr.setSeed(seed); + kgen.init(128, sr); //128bits,192bits,256bits + //AES涓�128浣嶅瘑閽ワ紝鍔犲瘑杞涓�10杞紱192浣嶅瘑閽ワ紝鍔犲瘑杞涓�12杞紱256浣嶅瘑閽ワ紝鍔犲瘑杞涓�14杞�� + SecretKey secretKey = kgen.generateKey(); + return secretKey.getEncoded(); + } + + + /** + * 灏嗕簩杩涘埗杞崲鎴�16杩涘埗 + */ + public static String parseByte2HexStr(byte[] buf) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < buf.length; i++) { + String hex = Integer.toHexString(buf[i] & 0xFF); + if (hex.length() == 1) { + hex = '0' + hex; + } + sb.append(hex.toUpperCase()); + } + return sb.toString(); + } + + /** + * 灏�16杩涘埗杞崲涓轰簩杩涘埗 + */ + public static byte[] parseHexStr2Byte(String hexStr) { + if (hexStr.length() < 1) + return null; + byte[] result = new byte[hexStr.length() / 2]; + for (int i = 0; i < hexStr.length() / 2; i++) { + int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); + int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), + 16); + result[i] = (byte) (high * 16 + low); + } + return result; + } + + +} diff --git a/app/src/main/java/com/hdl/photovoltaic/utils/ByteUtils.java b/app/src/main/java/com/hdl/photovoltaic/utils/ByteUtils.java new file mode 100644 index 0000000..a3880ee --- /dev/null +++ b/app/src/main/java/com/hdl/photovoltaic/utils/ByteUtils.java @@ -0,0 +1,213 @@ +package com.hdl.photovoltaic.utils; + +import com.hdl.sdk.link.common.utils.LogUtils; + +import java.io.UnsupportedEncodingException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.Key; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; + +/** + * Created by Tong on 2021/9/23. + */ +public class ByteUtils { + + public static byte[] toByteArray(List<Byte> list) { + Byte[] temps = list.toArray(new Byte[0]); + byte[] result = new byte[temps.length]; + for (int i = 0; i < result.length; i++) { + result[i] = temps[i]; + } + return result; + + } + + + public static List<Byte> toByteList(byte[] bytes) { + final List<Byte> list = new ArrayList<>(); + for (byte aByte : bytes) { + list.add(aByte); + } + return list; + + } + + public static byte[] getRangeBytes(List<Byte> list, int start, int end) { + Byte[] temps = Arrays.copyOfRange(list.toArray(new Byte[0]), start, end); + byte[] result = new byte[temps.length]; + for (int i = 0; i < temps.length; i++) { + result[i] = temps[i]; + } + return result; + + } + + public static byte[] copyBytes(byte bytes[], int index, int length) { + byte[] result = new byte[length]; + for (int i = 0; i < result.length; i++) { + result[i] = bytes[index + i]; + } + return result; + } + + /** + * 鎷兼帴byte + */ + public static byte[] concatBytes(byte[] bt1, byte[] bt2) { + if (bt1 == null) { + return bt2; + } + if (bt2 == null) { + return bt1; + } + byte[] bt3 = new byte[bt1.length + bt2.length]; + System.arraycopy(bt1, 0, bt3, 0, bt1.length); + System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length); + return bt3; + } + + + public boolean endWith(Byte[] src, byte[] target) { + if (src.length < target.length) { + return false; + } + for (int i = 0; i < target.length; i++) { + if (target[target.length - i - 1] != src[src.length - i - 1]) { + return false; + } + } + return true; + } + + + public static int byteIndexOf(byte[] searched, byte[] find, int start) { + boolean matched; + int end = find.length - 1; + int skip = 0; + for (int index = start; index <= searched.length - find.length; ++index) { + matched = true; + if (find[0] != searched[index] || find[end] != searched[index + end]) continue; + else skip++; + if (end > 10) + if (find[skip] != searched[index + skip] || find[end - skip] != searched[index + end - skip]) + continue; + else skip++; + for (int subIndex = skip; subIndex < find.length - skip; ++subIndex) { + if (find[subIndex] != searched[index + subIndex]) { + matched = false; + break; + } + } + if (matched) { + return index; + } + } + return -1; + + } + + public static int getByteIndexOf(byte[] sources, byte[] src) { + return getByteIndexOf(sources, src, 0, sources.length); + } + + //鍒ゆ柇涓�涓猙yte鏁板�煎湪鍙﹀涓�涓猙yte鏁扮粍涓搴旂殑娓告爣鍊� + public static int getByteIndexOf(byte[] sources, byte[] src, int startIndex) { + return getByteIndexOf(sources, src, startIndex, sources.length); + } + + + //鍒ゆ柇涓�涓猙yte鏁板�煎湪鍙﹀涓�涓猙yte鏁扮粍涓搴旂殑娓告爣鍊硷紝鎸囧畾寮�濮嬬殑娓告爣鍜岀粨鏉熺殑娓告爣浣嶇疆 + public static int getByteIndexOf(byte[] sources, byte[] src, int startIndex, int endIndex) { + + if (sources == null || src == null || sources.length == 0 || src.length == 0) { + return -1; + } + + if (endIndex > sources.length) { + endIndex = sources.length; + } + + int i, j; + for (i = startIndex; i < endIndex; i++) { + if (sources[i] == src[0] && i + src.length < endIndex) { + for (j = 1; j < src.length; j++) { + if (sources[i + j] != src[j]) { + break; + } + } + + if (j == src.length) { + return i; + } + } + } + return -1; + } + + /** + * 瀛楃涓瞭o Bytes + * + * @param str 瀛楃涓� + * @return + */ + public static byte[] stringToBytes(String str) { + try { + // 浣跨敤鎸囧畾鐨勫瓧绗﹂泦灏嗘瀛楃涓茬紪鐮佷负byte搴忓垪骞跺瓨鍒颁竴涓猙yte鏁扮粍涓� + return str.getBytes("utf-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + return new byte[]{}; + } + + public static String byte2hex(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + String tmp = null; + for (byte b : bytes) { + //灏嗘瘡涓瓧鑺備笌0xFF杩涜涓庤繍绠楋紝鐒跺悗杞寲涓�10杩涘埗锛岀劧鍚庡�熷姪浜嶪nteger鍐嶈浆鍖栦负16杩涘埗 + tmp = Integer.toHexString(0xFF & b); + if (tmp.length() == 1) { + tmp = "0" + tmp; + } + sb.append(tmp + " "); + } + return sb.toString(); + } + + + public static int bytes2int(byte[] bytes) { + return bytes[3] & 0xFF | // + (bytes[2] & 0xFF) << 8 | // + (bytes[1] & 0xFF) << 16 | // + (bytes[0] & 0xFF) << 24; // + } + + + public static byte[] intToByteArray(int i) { + byte[] result = new byte[4]; + result[0] = (byte) ((i >> 24) & 0xFF); + result[1] = (byte) ((i >> 16) & 0xFF); + result[2] = (byte) ((i >> 8) & 0xFF); + result[3] = (byte) (i & 0xFF); + return result; + } + + public static int byteArrayToInt(byte[] b) { + int i = (b[0] & 0xFF) * 256 * 256 * 256 + (b[1] & 0xFF) * 256 * 256 + (b[2] & 0xFF) * 256 + (b[3] & 0xFF); + return i; + } + + + +} diff --git a/app/src/main/java/com/hdl/photovoltaic/utils/HDLMD5Utils.java b/app/src/main/java/com/hdl/photovoltaic/utils/Md5Utils.java similarity index 73% rename from app/src/main/java/com/hdl/photovoltaic/utils/HDLMD5Utils.java rename to app/src/main/java/com/hdl/photovoltaic/utils/Md5Utils.java index c51dfcf..34a1a39 100644 --- a/app/src/main/java/com/hdl/photovoltaic/utils/HDLMD5Utils.java +++ b/app/src/main/java/com/hdl/photovoltaic/utils/Md5Utils.java @@ -2,13 +2,12 @@ import java.io.File; import java.io.FileInputStream; -import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -public class HDLMD5Utils { +public class Md5Utils { /** * MD5杞瓧绗︿覆 */ @@ -107,73 +106,4 @@ } - - - public final static String md5(String plainText) { - -// 杩斿洖瀛楃涓� - - String md5Str = null; - - try { - -// 鎿嶄綔瀛楃涓� - - StringBuffer buf = new StringBuffer(); - - MessageDigest md = - - MessageDigest.getInstance("MD5"); - -// 娣诲姞瑕佽繘琛岃绠楁憳瑕佺殑淇℃伅,浣跨敤 plainText 鐨� byte - - - md.update(plainText.getBytes()); - -// 璁$畻鍑烘憳瑕�,瀹屾垚鍝堝笇璁$畻銆� - - byte b[] = md.digest(); - - int i; - - for (int offset = 0; offset < b.length; offset++) { - - i = b[offset]; - - if (i < 0) { - - i += 256; - - } - - if (i < 16) { - - buf.append("0"); - - } - -// 灏嗘暣鍨� 鍗佽繘鍒� i - - - buf.append(Integer.toHexString(i)); - - } - -// 32浣嶇殑鍔犲瘑 - - md5Str = buf.toString(); - -// 16浣嶇殑鍔犲瘑 - -// md5Str = buf.toString().md5Strstring(8,24); - - } catch (Exception e) { - - e.printStackTrace(); - - } - - return md5Str; - - } } -- Gitblit v1.8.0