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