From 56253ff351053aefce1ee0b5f57093ca8a9e0a62 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 26 Oct 2023 09:37:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=87=E7=BA=A7=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/xypower/mpapp/AppMaster.java | 204 ++++++++++++++++++ .../xypower/mpapp/BootBroadcastReceiver.java | 30 +++ .../com/xypower/mpapp/FileDownloader.java | 8 +- .../java/com/xypower/mpapp/MainActivity.java | 7 + .../com/xypower/mpapp/MicroPhotoService.java | 19 ++ .../main/java/com/xypower/mpapp/Upgrader.java | 4 + .../main/res/xml/network_security_config.xml | 4 + 7 files changed, 274 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/xypower/mpapp/AppMaster.java create mode 100644 app/src/main/java/com/xypower/mpapp/BootBroadcastReceiver.java create mode 100644 app/src/main/java/com/xypower/mpapp/Upgrader.java create mode 100644 app/src/main/res/xml/network_security_config.xml diff --git a/app/src/main/java/com/xypower/mpapp/AppMaster.java b/app/src/main/java/com/xypower/mpapp/AppMaster.java new file mode 100644 index 00000000..c475586f --- /dev/null +++ b/app/src/main/java/com/xypower/mpapp/AppMaster.java @@ -0,0 +1,204 @@ +package com.xypower.mpapp; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.List; + +import android.content.Context; +import android.os.PowerManager; +import android.text.TextUtils; +import android.util.Pair; + +import com.dev.devapi.api.SysApi; + +import org.json.JSONObject; + +public class AppMaster { + + private MicroPhotoService mService; + private String mCmdid; + private PowerManager.WakeLock mWakelock; + + private final static String MASTER_URL = "http://180.166.218.222:40101/?cmdid="; + + public AppMaster(MicroPhotoService service, String cmdid) { + + PowerManager powerManager = (PowerManager) service.getSystemService(Context.POWER_SERVICE); + mWakelock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "com.xinyingpower.microphoto:Upgrader"); + + mService = service; + mCmdid = cmdid; + } + + @Override + protected void finalize() { + mWakelock.release(); + mService = null; + } + + public void start() { + + new Thread(new Runnable() { + @Override + public void run() { + HttpURLConnection httpURLConnection = null; + InputStream inputStream = null; + + try { + String url = MASTER_URL + URLEncoder.encode(mCmdid, "UTF-8"); + URL mUrl = new URL(url); + httpURLConnection = (HttpURLConnection) mUrl.openConnection(); + httpURLConnection.setConnectTimeout(15000); + httpURLConnection.setReadTimeout(15000); + httpURLConnection.setRequestMethod("POST"); + // httpURLConnection.setRequestProperty("Connection", "Keep-Alive"); + httpURLConnection.setDoInput(true); + httpURLConnection.setDoOutput(true); + + List> postParams = new ArrayList<>(); + postParams.add(new Pair("id", mCmdid)); + // postParams(httpURLConnection.getOutputStream(), postParams); + buildParams(httpURLConnection.getOutputStream()); + httpURLConnection.connect(); + inputStream = httpURLConnection.getInputStream(); + + int responseCode = httpURLConnection.getResponseCode(); + + if (responseCode == HttpURLConnection.HTTP_OK) { + //在子线程中不能操作UI线程,通过handler在UI线程中进行操作 + // handler.sendEmptyMessage(0x00); + String response = convertStreamToString(inputStream); + process(response); + } + } catch (Exception e) { + e.printStackTrace(); + } + + } + }).start(); + } + + private void process(String content) { + if (TextUtils.isEmpty(content)) { + return; + } + + try { + JSONObject jsonObject = new JSONObject(content); + int isUpgrade = jsonObject.optInt("isUpgrade", 0); + String url = jsonObject.optString("url", null); + + if (isUpgrade == 1 && !TextUtils.isEmpty(url)) { + upgradeApp(url); + } + + } catch (Exception e) { + e.printStackTrace(); + } + + } + + private void upgradeApp(String url) { + + FileDownloader dl = new FileDownloader(); + File path = new File(MicroPhotoService.buildAppDir(mService.getApplicationContext()), "packages"); + if (!path.exists()) { + path.mkdirs(); + } + + File file = new File(path, "app.apk"); + if (file.exists()) { + file.delete(); + } + String apkPath = file.getAbsolutePath(); + if (dl.download(url, apkPath)) { + Context context = mService.getApplicationContext(); + SysApi.installApk(context, apkPath, context.getPackageName(), true); + } + } + + private void buildParams(OutputStream output) { + BufferedWriter bufferedWriter = null; + try { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("id", mCmdid); + + bufferedWriter = new BufferedWriter(new OutputStreamWriter(output, "UTF-8")); + bufferedWriter.write(jsonObject.toString()); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (bufferedWriter != null) { + bufferedWriter.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + public static void postParams(OutputStream output, List> paramsList) { + + BufferedWriter bufferedWriter = null; + try { + StringBuilder stringBuilder = new StringBuilder(); + for (Pair pair : paramsList) { + if (!TextUtils.isEmpty(stringBuilder)) { + stringBuilder.append("&"); + } + stringBuilder.append(URLEncoder.encode(pair.first, "UTF-8")); + stringBuilder.append("="); + stringBuilder.append(URLEncoder.encode(pair.second, "UTF-8")); + bufferedWriter = new BufferedWriter(new OutputStreamWriter(output, "UTF-8")); + bufferedWriter.write(stringBuilder.toString()); + } + + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (bufferedWriter != null) { + bufferedWriter.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + private String convertStreamToString(InputStream inputStream) { + BufferedReader bufferedReader = null; + StringBuffer stringBuffer = new StringBuffer(); + String line; + try { + bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + while ((line = bufferedReader.readLine()) != null) { + stringBuffer.append(line).append("\n"); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (bufferedReader != null) { + bufferedReader.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + return stringBuffer.toString(); + } + + +} diff --git a/app/src/main/java/com/xypower/mpapp/BootBroadcastReceiver.java b/app/src/main/java/com/xypower/mpapp/BootBroadcastReceiver.java new file mode 100644 index 00000000..33e6ddc4 --- /dev/null +++ b/app/src/main/java/com/xypower/mpapp/BootBroadcastReceiver.java @@ -0,0 +1,30 @@ +package com.xypower.mpapp; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +public class BootBroadcastReceiver extends BroadcastReceiver { + private static final String ACTION = "android.intent.action.BOOT_COMPLETED"; + + @Override + public void onReceive(Context context, Intent intent) { + // Log.e("接收广播", "onReceive: "); + // Log.e("接收广播", "onReceive: " + intent.getAction()); + //开机启动 + if (ACTION.equals(intent.getAction())) { + // Log.e("接收广播", "onReceive: 启动了。。。"); + + Intent mainIntent = new Intent(context, MainActivity.class); + mainIntent.putExtra("reboot", 1); + /** + * Intent.FLAG_ACTIVITY_NEW_TASK + * Intent.FLAG_ACTIVITY_CLEAR_TOP + */ + mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(mainIntent); + +// context.startService(mainIntent); + } + } +} diff --git a/app/src/main/java/com/xypower/mpapp/FileDownloader.java b/app/src/main/java/com/xypower/mpapp/FileDownloader.java index e43d93e1..b14f6f5a 100644 --- a/app/src/main/java/com/xypower/mpapp/FileDownloader.java +++ b/app/src/main/java/com/xypower/mpapp/FileDownloader.java @@ -15,11 +15,12 @@ import java.net.Socket; import java.net.URL; public class FileDownloader { - protected void download(String urlString, String filePath) { + protected boolean download(String urlString, String filePath) { File downloadFile = null; if (TextUtils.isEmpty(urlString)) - return; + return false; HttpURLConnection connection = null; + boolean res = false; try { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); URL url = new URL(urlString); @@ -47,6 +48,7 @@ public class FileDownloader { if (os instanceof FileOutputStream) { ((FileOutputStream) os).getFD().sync(); } + res = true; } finally { closeSilently(os); closeSilently(is); @@ -62,6 +64,8 @@ public class FileDownloader { if (connection != null) connection.disconnect(); } + + return res; } public static final void closeSilently(Object closeable) { diff --git a/app/src/main/java/com/xypower/mpapp/MainActivity.java b/app/src/main/java/com/xypower/mpapp/MainActivity.java index c267264f..0f038fd3 100644 --- a/app/src/main/java/com/xypower/mpapp/MainActivity.java +++ b/app/src/main/java/com/xypower/mpapp/MainActivity.java @@ -28,6 +28,7 @@ import android.view.View; import android.view.WindowManager; import android.widget.Toast; +import com.dev.devapi.api.SysApi; import com.dowse.camera.client.DSCameraManager; import com.xypower.mpapp.databinding.ActivityMainBinding; //import com.xinyingpower.microphoto.request.INettyMessageListener; @@ -94,6 +95,12 @@ public class MainActivity extends AppCompatActivity { Log.d(TAG, "Screen Size: " + width + " x " + height); + Intent intent = getIntent(); + int rebootFlag = intent.getIntExtra("reboot", 0); + if (rebootFlag == 1) { + // SysApi.enableAirPlane(MainActivity.this, true); + } + String cmdid = "0123456789ABCDEFG"; String server = "47.96.238.157"; Integer port = new Integer(6891); diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index 00b8dec5..b7fa41d3 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -75,6 +75,8 @@ public class MicroPhotoService extends Service { public static final int CONNECTED = 10; public static final int NOT_CONNECTED = 0; } + + private String mCmdid = ""; private NotificationManager mNotificationManager; private final Map mWakeLocks = new HashMap<>(); private int mHeartbeatDuration = 0; // 5m: 5 * 60 * 1000 @@ -149,6 +151,11 @@ public class MicroPhotoService extends Service { super.onDestroy(); } + + public String getCmdid() { + return mCmdid; + } + public static class AlarmReceiver extends BroadcastReceiver { private MicroPhotoService mService; public AlarmReceiver() { @@ -163,6 +170,14 @@ public class MicroPhotoService extends Service { Log.i(TAG, "HB Timer Fired ACTION=" + action); mService.sendHeartbeat(mService.mHandler); mService.registerHeartbeatTimer(); + + String cmdid = mService.getCmdid(); + if (!TextUtils.isEmpty(cmdid)) { + AppMaster appMaster = new AppMaster(mService, cmdid); + appMaster.start(); + } + + } else if (TextUtils.equals(ACTION_TAKE_PHOTO, action)) { long ts = intent.getLongExtra(EXTRA_PARAM_TIME, 0); int cnt = intent.getIntExtra(EXTRA_PARAM_SCHEDULES, 0); @@ -452,6 +467,7 @@ public class MicroPhotoService extends Service { // Start the locker receiver if (mHandler !=0) { + mCmdid = cmdid; Date date = new Date(); long nowTs = date.getTime() / 1000; date.setHours(0); @@ -462,6 +478,9 @@ public class MicroPhotoService extends Service { registerCaptureSchedule(startTime, baseTime); + // AppMaster appMaster = new AppMaster(this, cmdid); + // appMaster.start(); + } // registerPhotoTimer(); diff --git a/app/src/main/java/com/xypower/mpapp/Upgrader.java b/app/src/main/java/com/xypower/mpapp/Upgrader.java new file mode 100644 index 00000000..08c2e1e4 --- /dev/null +++ b/app/src/main/java/com/xypower/mpapp/Upgrader.java @@ -0,0 +1,4 @@ +package com.xypower.mpapp; + +public class Upgrader { +} diff --git a/app/src/main/res/xml/network_security_config.xml b/app/src/main/res/xml/network_security_config.xml new file mode 100644 index 00000000..dca93c07 --- /dev/null +++ b/app/src/main/res/xml/network_security_config.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file