From ab0ca69ff3b331f1aee286594b0ead46c9786c5a Mon Sep 17 00:00:00 2001
From: hjw <hjw@ma.nodka.com>
Date: Thu, 28 Nov 2024 11:35:29 +0000
Subject: [PATCH] 开放接口(HDMI分辨率设置、应用保活、系统截图、静默安装、静默卸载、蓝牙开关、WIFI开关、以太网开关、系统重启/关机)

---
 app/src/main/java/com/jwipc/nodka_reboot_under/BroadcastReceiver_Sys.java |  247 +++++++++++++++++++++++++++++++++++++++++
 app/libs/classes-header.jar                                               |    0 
 app/src/main/AndroidManifest.xml                                          |   24 +++
 app/src/main/java/com/jwipc/nodka_reboot_under/utils/Utils.java           |   61 ++++++++++
 app/build.gradle                                                          |    1 
 5 files changed, 329 insertions(+), 4 deletions(-)

diff --git a/app/build.gradle b/app/build.gradle
index 74de442..7e429df 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -40,4 +40,5 @@
     implementation 'com.android.support:support-v4:25.+'
     implementation files('libs/spirit.jar')
     implementation files('libs/NodkaAPI.jar')
+    compileOnly files('libs/classes-header.jar')
 }
diff --git a/app/libs/classes-header.jar b/app/libs/classes-header.jar
new file mode 100644
index 0000000..24ca9d6
--- /dev/null
+++ b/app/libs/classes-header.jar
Binary files differ
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index bf49da0..8dc0334 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -7,8 +7,11 @@
     <uses-sdk
         android:minSdkVersion="25"
         android:targetSdkVersion="25" />
-    
-    
+
+
+    <uses-permission android:name="android.permission.BLUETOOTH" />
+    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
+    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
     
 
@@ -50,8 +53,21 @@
                 <action android:name="android.intent.action.TIME_SET" />
             </intent-filter>
         </receiver>
-        
-        
+
+        <receiver android:name="BroadcastReceiver_Sys">
+            <intent-filter>
+                <action android:name="com.nodka.action.HDMI_RESOLUTION" />
+                <action android:name="com.nodka.action.APP_ALIVE" />
+                <action android:name="com.nodka.action.SCREEN_CAP" />
+                <action android:name="com.nodka.action.PKG_INSTALL" />
+                <action android:name="com.nodka.action.PKG_UNINSTALL" />
+                <action android:name="com.nodka.action.BLUETOOTH" />
+                <action android:name="com.nodka.action.WIFI" />
+                <action android:name="com.nodka.action.ETHERNET" />
+                <action android:name="com.nodka.action.POWER" />
+            </intent-filter>
+        </receiver>
+
         <service android:name="The_Service"></service>
         
         
diff --git a/app/src/main/java/com/jwipc/nodka_reboot_under/BroadcastReceiver_Sys.java b/app/src/main/java/com/jwipc/nodka_reboot_under/BroadcastReceiver_Sys.java
new file mode 100644
index 0000000..45e5037
--- /dev/null
+++ b/app/src/main/java/com/jwipc/nodka_reboot_under/BroadcastReceiver_Sys.java
@@ -0,0 +1,247 @@
+package com.jwipc.nodka_reboot_under;
+
+import android.app.ProgressDialog;
+import android.bluetooth.BluetoothAdapter;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.content.SharedPreferences.Editor;
+import android.net.EthernetManager;
+import android.net.wifi.WifiManager;
+import android.os.Handler;
+import android.os.Looper;
+import android.text.TextUtils;
+import android.util.Log;
+import android.view.WindowManager;
+
+import com.jwipc.nodka_reboot_under.utils.Utils;
+
+public class BroadcastReceiver_Sys extends BroadcastReceiver {
+    public static final String TAG = "BroadcastReceiver_Sys";
+    private Utils mUtils = null;
+    private SharedPreferences sharedPreferences;
+    private Editor editor;
+
+    private static int MAIN_RESOLUTION_COUNT = 1;
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        mUtils = new Utils(context);
+        sharedPreferences = context.getSharedPreferences(mUtils.shapre, context.MODE_PRIVATE);
+        editor = sharedPreferences.edit();
+        String action = intent.getAction();
+        if (action.equals("com.nodka.action.HDMI_RESOLUTION")) {
+            /**
+             * 设置主屏幕分辨率
+             * action com.nodka.action.HDMI_RESOLUTION
+             * stringExtra params HDMI屏幕参数
+             *
+             * 设置1080p60
+             * adb shell am broadcast -a com.nodka.action.HDMI_RESOLUTION --es params 1920x1080@60-1920-2008-2052-2200-1080-1084-1089-1125-5
+             * 设置720P60
+             * adb shell am broadcast -a com.nodka.action.HDMI_RESOLUTION --es params 1280x720@60.00-1390-1430-1650-725-730-750-5
+             * 设置480P60
+             * adb shell am broadcast -a com.nodka.action.HDMI_RESOLUTION --es params 720x480@59.94-736-798-858-489-495-525-a
+             *
+             * 设置4k60
+             * setprop persist.vendor.resolution.HDMI-A-0 3840x2160@60-3840-4016-4104-4400-2160-2168-2178-2250-5
+             * setprop vendor.display.timeline 1
+             *
+             * 设置1080p60
+             * setprop persist.vendor.resolution.HDMI-A-0 1920x1080@60-1920-2008-2052-2200-1080-1084-1089-1125-5
+             * setprop vendor.display.timeline 2
+             *
+             * 设置720P60
+             * setprop persist.vendor.resolution.HDMI-A-0 1280x720@60.00-1390-1430-1650-725-730-750-5
+             * setprop vendor.display.timeline 3
+             *
+             * 设置480P60
+             * setprop persist.vendor.resolution.HDMI-A-0 720x480@59.94-736-798-858-489-495-525-a
+             * setprop vendor.display.timeline 4
+             */
+            String params = intent.getStringExtra("params");
+            if (!TextUtils.isEmpty(params)) {
+                String params1 = "setprop persist.vendor.resolution.HDMI-A-0 " + params;
+                String params2 = "setprop vendor.display.timeline " + MAIN_RESOLUTION_COUNT++;
+                String result1 = mUtils.getRootCmdSilent(params1);
+                String result2 = mUtils.getRootCmdSilent(params2);
+                Log.i(TAG, "onReceive: HDMI_RESOLUTION, params1 = " + params1 +
+                        ", params2 = " + params2 + ", result1 = " + result1 + ", result2 = " + result2);
+                ProgressDialog progressDialog = new ProgressDialog(context);
+                progressDialog.setCanceledOnTouchOutside(false);
+                progressDialog.setCancelable(false);
+                progressDialog.setMessage("设置HDMI分辨率");
+                progressDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
+                progressDialog.show();
+                new Handler(Looper.getMainLooper()).postDelayed(() -> progressDialog.dismiss(), 1000);
+            }
+        } else if (action.equals("com.nodka.action.APP_ALIVE")) {
+            /**
+             * 设置保活应用(系统重启生效)
+             * action com.nodka.action.APP_ALIVE
+             * stringExtra componentName 应用包名/类名
+             *
+             * adb shell am broadcast -a com.nodka.action.APP_ALIVE --es componentName com.nodka.screen/.MainActivity
+             *
+             * setprop persist.keep.alive.name
+             */
+            String componentName = intent.getStringExtra("componentName");
+            String params;
+            if (!TextUtils.isEmpty(componentName)) {
+                params = "setprop persist.keep.alive.name " + componentName;
+            } else {
+                params = "setprop persist.keep.alive.name \"\"";
+            }
+            String result = mUtils.getRootCmdSilent(params);
+            Log.i(TAG, "onReceive: APP_ALIVE, params = " + params + ", result = " + result);
+        } else if (action.equals("com.nodka.action.SCREEN_CAP")) {
+            /**
+             * 屏幕截图
+             * action com.nodka.action.SCREEN_CAP
+             * stringExtra path 截图保存路径以及图片名称
+             *
+             * adb shell am broadcast -a com.nodka.action.SCREEN_CAP --es path sdcard/screenshot.png
+             *
+             * screencap -p /sdcard/screenshot.png
+             */
+            String path = intent.getStringExtra("path");
+            if (!TextUtils.isEmpty(path)) {
+                String params = "screencap -p " + path;
+                new Handler(Looper.getMainLooper()).post(() -> {
+                    String result = mUtils.getRootCmdSilent(params);
+                    Log.i(TAG, "onReceive: SCREEN_CAP, params = " + params + ", result = " + result);
+                });
+
+            }
+        } else if (action.equals("com.nodka.action.PKG_INSTALL")) {
+            /**
+             * 静默安装
+             * action com.nodka.action.PKG_INSTALL
+             * stringExtra path APK路径
+             *
+             * adb shell am broadcast -a com.nodka.action.PKG_INSTALL --es path sdcard/DeviceID.apk
+             *
+             * pm install -r
+             */
+            String path = intent.getStringExtra("path");
+            if (!TextUtils.isEmpty(path)) {
+                String params = "pm install -r " + path;
+                String result = mUtils.getRootCmdSilent(params);
+                Log.i(TAG, "onReceive: PKG_INSTALL, params = " + params + ", result = " + result);
+            }
+        } else if (action.equals("com.nodka.action.PKG_UNINSTALL")) {
+            /**
+             * 静默卸载
+             * action com.nodka.action.PKG_UNINSTALL
+             * stringExtra pkgName 应用包名
+             *
+             * adb shell am broadcast -a com.nodka.action.PKG_UNINSTALL --es pkgName com.evozi.deviceid
+             *
+             * pm uninstall
+             */
+            String pkgName = intent.getStringExtra("pkgName");
+            if (!TextUtils.isEmpty(pkgName)) {
+                String params = "pm uninstall " + pkgName;
+                String result = mUtils.getRootCmdSilent(params);
+                Log.i(TAG, "onReceive: PKG_UNINSTALL, params = " + params + ", result = " + result);
+            }
+        } else if (action.equals("com.nodka.action.BLUETOOTH")) {
+            /**
+             * 打开关闭蓝牙
+             * action com.nodka.action.BLUETOOTH
+             * intExtra power 0-关闭 1-开启
+             *
+             * adb shell am broadcast -a com.nodka.action.BLUETOOTH --ei power 1
+             */
+            int power = intent.getIntExtra("power", -1);
+            if (power != -1) {
+                BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
+                if (power == 1) {
+                    if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) {
+                        bluetoothAdapter.enable(); // 开启蓝牙
+                    }
+                } else if (power == 0) {
+                    if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
+                        bluetoothAdapter.disable(); // 关闭蓝牙
+                    }
+                }
+                Log.i(TAG, "onReceive: BLUETOOTH, power = " + power);
+            }
+        } else if (action.equals("com.nodka.action.WIFI")) {
+            /**
+             * 打开关闭WIFI
+             * action com.nodka.action.WIFI
+             * intExtra power 0-关闭 1-开启
+             *
+             * adb shell am broadcast -a com.nodka.action.WIFI --ei power 1
+             */
+            int power = intent.getIntExtra("power", -1);
+            if (power != -1) {
+                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
+                if (power == 1) {
+                    if (wifiManager != null && !wifiManager.isWifiEnabled()) {
+                        wifiManager.setWifiEnabled(true); // 开启 Wi-Fi
+                    }
+                } else if (power == 0) {
+                    if (wifiManager != null && wifiManager.isWifiEnabled()) {
+                        wifiManager.setWifiEnabled(false); // 关闭 Wi-Fi
+                    }
+                }
+                Log.i(TAG, "onReceive: WIFI, power = " + power);
+            }
+        } else if (action.equals("com.nodka.action.ETHERNET")) {
+            /**
+             * 打开关闭ETHERNET
+             * action com.nodka.action.ETHERNET
+             * stringExtra ethX eth0 eth1 此参数不指定则控制所有
+             * intExtra power 0-关闭 1-开启
+             *
+             * adb shell am broadcast -a com.nodka.action.ETHERNET --ei power 0
+             * adb shell am broadcast -a com.nodka.action.ETHERNET --es ethX eth0 --ei power 0
+             * adb shell am broadcast -a com.nodka.action.ETHERNET --es ethX eth1 --ei power 0
+             */
+            String ethX = intent.getStringExtra("ethX");
+            int power = intent.getIntExtra("power", -1);
+            if (power != -1) {
+                EthernetManager mEthManager = (EthernetManager) context.getSystemService("ethernet");
+                if (power == 1) {
+                    if (!TextUtils.isEmpty(ethX)) {
+                        mEthManager.setEthernetEnabled(ethX, true);
+                    } else {
+                        mEthManager.setEthernetEnabled("eth0", true);
+                        mEthManager.setEthernetEnabled("eth1", true);
+                    }
+                } else if (power == 0) {
+                    if (!TextUtils.isEmpty(ethX)) {
+                        mEthManager.setEthernetEnabled(ethX, false);
+                    } else {
+                        mEthManager.setEthernetEnabled("eth0", false);
+                        mEthManager.setEthernetEnabled("eth1", false);
+                    }
+                }
+                Log.i(TAG, "onReceive: ETHERNET, ethX = " + ethX + ", power = " + power);
+            }
+        } else if (action.equals("com.nodka.action.POWER")) {
+            /**
+             * 系统重启/关机
+             * action com.nodka.action.POWER
+             * intExtra powerType 1-重启 2-关机
+             *
+             * adb shell am broadcast -a com.nodka.action.POWER --ei powerType 1
+             */
+            int powerType = intent.getIntExtra("powerType", -1);
+            if (powerType != -1) {
+                String params = "";
+                if (powerType == 1) {
+                    params = "reboot";
+                    mUtils.getRootCmdSilent(params);
+                } else if (powerType == 2) {
+                    params = "reboot -p";
+                    mUtils.getRootCmdSilent(params);
+                }
+                Log.i(TAG, "onReceive: POWER, params = " + params);
+            }
+        }
+    }
+}
diff --git a/app/src/main/java/com/jwipc/nodka_reboot_under/utils/Utils.java b/app/src/main/java/com/jwipc/nodka_reboot_under/utils/Utils.java
index 210c117..4f07730 100644
--- a/app/src/main/java/com/jwipc/nodka_reboot_under/utils/Utils.java
+++ b/app/src/main/java/com/jwipc/nodka_reboot_under/utils/Utils.java
@@ -1,6 +1,7 @@
 package com.jwipc.nodka_reboot_under.utils;
 
 import java.io.BufferedReader;
+import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -688,6 +689,66 @@
 		
 		return re;
 	}
+
+	public String getRootCmdSilent(String cmd) {
+		DataOutputStream dos = null;
+		DataInputStream dis = null;
+		Process process = null;
+		try {
+			process = Runtime.getRuntime().exec("su");
+			dos = new DataOutputStream(process.getOutputStream());
+			dis = new DataInputStream(process.getInputStream());
+			dos.write(cmd.getBytes());
+			dos.flush();
+			dos.close();
+			process.waitFor();
+			return getStrFromDataInPutStream(dis);
+		} catch (Exception e) {
+			e.printStackTrace();
+			return null;
+		} finally {
+			if (dos != null) {
+				try {
+					dos.close();
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+			}
+			if (dis != null) {
+				try {
+					dis.close();
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+			}
+			if (process != null) {
+				process.destroy();
+			}
+		}
+	}
+
+	private String getStrFromDataInPutStream(DataInputStream dos) {
+		if (null == dos) {
+			return "";
+		}
+		int BUFFER_SIZE = 512;
+		byte[] buffer = new byte[BUFFER_SIZE];
+		StringBuilder result = new StringBuilder();
+		try {
+			while (true) {
+				int read = dos.read(buffer);
+				if (read > 0) {
+					result.append(new String(buffer, 0, read));
+				}
+				if (read < BUFFER_SIZE) {
+					break;
+				}
+			}
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		return result.toString();
+	}
 	
 	public boolean isSpiritVersion() {
 		return Build.VERSION.SDK_INT <= 25;

--
Gitblit v1.6.2