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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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);
            }
        }
    }
}