.. | .. |
---|
5 | 5 | import android.content.BroadcastReceiver;
|
---|
6 | 6 | import android.content.Context;
|
---|
7 | 7 | import android.content.Intent;
|
---|
8 | | -import android.os.Process;
|
---|
| 8 | +import java.lang.Process;
|
---|
9 | 9 | import android.os.SystemProperties;
|
---|
10 | 10 | import android.util.Log;
|
---|
11 | 11 | import android.content.ComponentName;
|
---|
| 12 | +import android.os.Environment;
|
---|
| 13 | +import android.os.storage.StorageManager;
|
---|
| 14 | +import java.util.List;
|
---|
| 15 | +import android.os.storage.StorageVolume;
|
---|
| 16 | +
|
---|
| 17 | +import java.io.File;
|
---|
| 18 | +import android.net.Uri;
|
---|
| 19 | +
|
---|
| 20 | +import java.io.DataOutputStream;
|
---|
| 21 | +import java.io.IOException;
|
---|
12 | 22 |
|
---|
13 | 23 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
|
---|
14 | 24 |
|
---|
15 | 25 | public class BootonAppReceiver extends BroadcastReceiver {
|
---|
| 26 | + private static final String TAG = "kickpi";
|
---|
| 27 | +
|
---|
| 28 | + public static void controlLED() {
|
---|
| 29 | + new Thread(() -> {
|
---|
| 30 | + Process process = null;
|
---|
| 31 | + DataOutputStream outputStream = null;
|
---|
| 32 | +
|
---|
| 33 | + try {
|
---|
| 34 | + // 获取 Root 权限
|
---|
| 35 | + process = Runtime.getRuntime().exec("su");
|
---|
| 36 | + outputStream = new DataOutputStream(process.getOutputStream());
|
---|
| 37 | +
|
---|
| 38 | + String baseDir = "/sys/class/leds/work-led/";
|
---|
| 39 | +
|
---|
| 40 | + // 设置触发模式为 timer
|
---|
| 41 | + outputStream.writeBytes("echo timer > " + baseDir + "trigger\n");
|
---|
| 42 | +
|
---|
| 43 | + // 设置亮灯时间(单位:ms)
|
---|
| 44 | + outputStream.writeBytes("echo 2000 > " + baseDir + "delay_on\n");
|
---|
| 45 | +
|
---|
| 46 | + // 设置灭灯时间(单位:ms)
|
---|
| 47 | + outputStream.writeBytes("echo 2000 > " + baseDir + "delay_off\n");
|
---|
| 48 | +
|
---|
| 49 | + outputStream.flush();
|
---|
| 50 | + // 等待命令执行完成
|
---|
| 51 | + Thread.sleep(100);
|
---|
| 52 | +
|
---|
| 53 | + int exitCode = process.waitFor();
|
---|
| 54 | + if (exitCode == 0) {
|
---|
| 55 | + Log.d(TAG, "LED control successful!");
|
---|
| 56 | + } else {
|
---|
| 57 | + Log.e(TAG, "LED control failed with exit code: " + exitCode);
|
---|
| 58 | + }
|
---|
| 59 | +
|
---|
| 60 | + } catch (IOException | InterruptedException e) {
|
---|
| 61 | + Log.e(TAG, "Error in LED control: " + e.getMessage());
|
---|
| 62 | + } finally {
|
---|
| 63 | + try {
|
---|
| 64 | + if (outputStream != null) outputStream.close();
|
---|
| 65 | + if (process != null) process.destroy();
|
---|
| 66 | + } catch (IOException e) {
|
---|
| 67 | + Log.e(TAG, "Error cleaning up: " + e.getMessage());
|
---|
| 68 | + }
|
---|
| 69 | + }
|
---|
| 70 | + }).start();
|
---|
| 71 | + }
|
---|
| 72 | +
|
---|
| 73 | + static void AgingTestbootApp(Context context) {
|
---|
| 74 | + try {
|
---|
| 75 | + Intent myintent = new Intent(Intent.ACTION_MAIN);
|
---|
| 76 | + String bootAppPack = "null";
|
---|
| 77 | + String bootAppClass = "null";
|
---|
| 78 | + bootAppPack = "com.oranth.factory";
|
---|
| 79 | + bootAppClass = "com.oranth.factory.MainActivity";
|
---|
| 80 | + Log.i(TAG,"will boot up APP - " + bootAppPack + "/" + bootAppClass);
|
---|
| 81 | +
|
---|
| 82 | + ComponentName cn = new ComponentName(bootAppPack, bootAppClass);
|
---|
| 83 | + myintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
---|
| 84 | + myintent.setComponent(cn);
|
---|
| 85 | + context.startActivity(myintent);
|
---|
| 86 | + } catch (Exception e) {
|
---|
| 87 | + e.printStackTrace();
|
---|
| 88 | + }
|
---|
| 89 | + }
|
---|
16 | 90 |
|
---|
17 | 91 | static void LauncherbootApp(Context context) {
|
---|
18 | 92 | try {
|
---|
.. | .. |
---|
39 | 113 | }
|
---|
40 | 114 | }
|
---|
41 | 115 |
|
---|
| 116 | + // 静态标志位,记录是否已找到文件
|
---|
| 117 | + private static boolean kickPiFound = false;
|
---|
| 118 | +
|
---|
42 | 119 | @Override
|
---|
43 | 120 | public void onReceive(Context context, Intent intent) {
|
---|
| 121 | +
|
---|
| 122 | + if (kickPiFound) {
|
---|
| 123 | + return;
|
---|
| 124 | + }
|
---|
| 125 | +
|
---|
| 126 | + Log.i("kickpi","intent.getAction() = " + intent.getAction());
|
---|
| 127 | + Log.i("kickpi","Intent.ACTION_MEDIA_MOUNTED = " + Intent.ACTION_MEDIA_MOUNTED);
|
---|
| 128 | +
|
---|
| 129 | + if (Intent.ACTION_MEDIA_MOUNTED.equals(intent.getAction())) {
|
---|
| 130 | + Uri uri = intent.getData();
|
---|
| 131 | + String path = uri.getPath();
|
---|
| 132 | + Log.d(TAG, "path: " + path);
|
---|
| 133 | + // 构造目标文件路径
|
---|
| 134 | + File kickPiFile = new File(path, "kickpi");
|
---|
| 135 | +
|
---|
| 136 | + // 检查文件是否存在
|
---|
| 137 | + if (kickPiFile.exists()) {
|
---|
| 138 | + // 设置标志位,后续广播将被忽略
|
---|
| 139 | + kickPiFound = true;
|
---|
| 140 | + AgingTestbootApp(context);
|
---|
| 141 | + controlLED();
|
---|
| 142 | + }
|
---|
| 143 | + }
|
---|
| 144 | +
|
---|
| 145 | + Log.i("kickpi","intent.getAction() = " + intent.getAction());
|
---|
44 | 146 | if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
|
---|
45 | | - LauncherbootApp(context);
|
---|
| 147 | + if (!kickPiFound) {
|
---|
| 148 | + Log.i("kickpi","LauncherbootApp");
|
---|
| 149 | + LauncherbootApp(context);}
|
---|
46 | 150 | }
|
---|
47 | 151 | }
|
---|
48 | 152 | }
|
---|