From 85dd793fea8e458c5912d44070129580f5951591 Mon Sep 17 00:00:00 2001 From: BlueMatthew Date: Tue, 16 Jan 2024 15:28:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=BF=90=E7=BB=B4=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xypower/common/FileDownloader.java | 6 ++ .../java/com/xypower/common/HttpRequest.java | 101 ++++++++++++++++++ .../java/com/xypower/mpmaster/AppMaster.java | 31 ++++-- 3 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 common/src/main/java/com/xypower/common/HttpRequest.java diff --git a/common/src/main/java/com/xypower/common/FileDownloader.java b/common/src/main/java/com/xypower/common/FileDownloader.java index 04ff5e4d..d781fe14 100644 --- a/common/src/main/java/com/xypower/common/FileDownloader.java +++ b/common/src/main/java/com/xypower/common/FileDownloader.java @@ -13,6 +13,7 @@ import java.net.HttpURLConnection; import java.net.ServerSocket; import java.net.Socket; import java.net.URL; +import java.util.zip.GZIPInputStream; public class FileDownloader { public boolean download(String urlString, String filePath) { @@ -20,14 +21,17 @@ public class FileDownloader { if (TextUtils.isEmpty(urlString)) return false; HttpURLConnection connection = null; + GZIPInputStream gZIPInputStream = null; boolean res = false; try { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); URL url = new URL(urlString); connection = (HttpURLConnection) url.openConnection(); + // connection.setRequestProperty("Accept-Encoding", "gzip"); connection.setConnectTimeout(5000); connection.setReadTimeout(60000); connection.setDoInput(true); + String encoding = connection.getContentEncoding(); InputStream is = connection.getInputStream(); final File temp = new File(filePath); if (temp.exists()) @@ -68,6 +72,8 @@ public class FileDownloader { return res; } + + public static final void closeSilently(Object closeable) { try { if (closeable != null) { diff --git a/common/src/main/java/com/xypower/common/HttpRequest.java b/common/src/main/java/com/xypower/common/HttpRequest.java new file mode 100644 index 00000000..e1e975ab --- /dev/null +++ b/common/src/main/java/com/xypower/common/HttpRequest.java @@ -0,0 +1,101 @@ +package com.xypower.common; + +import android.text.TextUtils; +import android.util.Log; + +import java.io.BufferedReader; +import java.io.Closeable; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.URL; + +public class HttpRequest { + public static String get(String urlString) { + File downloadFile = null; + if (TextUtils.isEmpty(urlString)) + return ""; + HttpURLConnection connection = null; + StringBuilder response = new StringBuilder(); + InputStream inputStream = null; + InputStreamReader isr = null; + BufferedReader br = null; + try { + Thread.currentThread().setPriority(Thread.MIN_PRIORITY); + URL url = new URL(urlString); + connection = (HttpURLConnection) url.openConnection(); + connection.setConnectTimeout(5000); + connection.setReadTimeout(60000); + connection.setDoInput(true); + + int responseCode = connection.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + inputStream = connection.getInputStream(); + isr = new InputStreamReader(inputStream); + br = new BufferedReader(isr); + + String line; + while ((line = br.readLine()) != null) { + response.append(line); + } + } + } catch (Exception e) { + } finally { + try { + if (connection != null) + connection.disconnect(); + } catch (Exception ex) { + ex.printStackTrace(); + } + try { + if (br != null) { + br.close(); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + try { + if (isr != null) { + isr.close(); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + try { + if (inputStream != null) { + inputStream.close(); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + return response.toString(); + } + + + + public static final void closeSilently(Object closeable) { + try { + if (closeable != null) { + if (closeable instanceof Closeable) { + ((Closeable) closeable).close(); + } else if (closeable instanceof Socket) { + ((Socket) closeable).close(); + } else if (closeable instanceof ServerSocket) { + ((ServerSocket) closeable).close(); + } else { + throw new IllegalArgumentException("Unknown object to close"); + } + } + } catch (IOException e) { + // ignore + } + } +} diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java index d013b101..042f97eb 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java @@ -10,6 +10,7 @@ import android.util.Base64; import com.dev.devapi.api.SysApi; import com.xypower.common.FileDownloader; import com.xypower.common.FileUploader; +import com.xypower.common.HttpRequest; import com.xypower.common.InetAddressUtils; import com.xypower.common.JSONUtils; import com.xypower.common.MicroPhotoContext; @@ -156,6 +157,7 @@ public class AppMaster { JSONObject jsonObject = new JSONObject(content); int isUpgrade = jsonObject.optInt("isUpgrade", 0); String url = jsonObject.optString("url", null); + long cid = jsonObject.optLong("cid", 0); int mntnMode = jsonObject.optInt("yw", 0); int quickHbMode = jsonObject.optInt("kxt", 0); @@ -163,10 +165,10 @@ public class AppMaster { mService.setMntnMode(mntnMode != 0, quickHbMode != 0); if (isUpgrade == 1 && !TextUtils.isEmpty(url)) { - upgradeApp(url); + upgradeApp(cid, "upgrade", url); } - processCmd(jsonObject); + processCmd(cid, jsonObject); JSONArray cmdObjects = jsonObject.optJSONArray("cmds"); @@ -175,7 +177,7 @@ public class AppMaster { for (int idx = 0; idx < cnt; idx++) { JSONObject cmdObject = cmdObjects.getJSONObject(idx); if (cmdObject != null) { - processCmd(cmdObject); + processCmd(cid, cmdObject); } } } @@ -186,9 +188,25 @@ public class AppMaster { } - private void processCmd(JSONObject jsonObject) { + private void sendResult(long cid, int result, String action) { + String url = mMasterUrl; + try { + if (!url.endsWith("/")) { + url += "/"; + } + url += "status/?"; + url += MicroPhotoContext.MASTER_URL_CMDID + "=" + URLEncoder.encode(mCmdid, "UTF-8") + + "&cid=" + Long.toString(cid) + + "&res=" + Integer.toString(result) + "&act=" + URLEncoder.encode(action, "UTF-8"); + HttpRequest.get(url); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + private void processCmd(long cid, JSONObject jsonObject) { String cmd = jsonObject.optString("cmd", ""); - long cid = jsonObject.optLong("cid", 0); + if (TextUtils.equals(cmd, CMD_REBOOT_DEV)) { SysApi.reboot(mService.getApplicationContext()); @@ -355,7 +373,7 @@ public class AppMaster { return true; } - private void upgradeApp(String url) { + private void upgradeApp(long cid, String action, String url) { FileDownloader dl = new FileDownloader(); File path = new File(MicroPhotoContext.buildAppDir(mService.getApplicationContext()), "packages"); @@ -369,6 +387,7 @@ public class AppMaster { } String apkPath = file.getAbsolutePath(); if (dl.download(url, apkPath)) { + sendResult(cid, 1, action); Context context = mService.getApplicationContext(); SysApi.installApk(context, apkPath, context.getPackageName(), true); }