-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRootViewController.swift
More file actions
439 lines (363 loc) · 19.7 KB
/
RootViewController.swift
File metadata and controls
439 lines (363 loc) · 19.7 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import UIKit
class RootViewController: UIViewController {
// 定义一个闭包(接口)当桌面快捷方式被点击的时候调用
var onClickQuickAction: ((String) -> Void)?
private let settingsUtils = SettingsUtils.instance
private let checkPermissionLabel = UILabel()
private let respringButton = UIButton(type: .system)
override func viewDidLoad() {
super.viewDidLoad()
self.title = NSLocalizedString("CFBundleDisplayName", comment: "")
// 设置ViewController的背景颜色
self.view.backgroundColor = UIColor.systemBackground
// icon
let iconImageView = UIImageView()
if #available(iOS 15.0, *) {
iconImageView.image = UIImage(systemName: "power.circle.fill")
iconImageView.tintColor = UIColor(hex: "#32A5E7")
} else {
let appIcon = UIImage(named: "AppIcon")
iconImageView.image = appIcon
}
// 无障碍标签
iconImageView.isAccessibilityElement = true // 声明这是一个可访问性元素
iconImageView.accessibilityLabel = NSLocalizedString("CFBundleDisplayName", comment: "")
// 禁用自动调整大小
iconImageView.translatesAutoresizingMaskIntoConstraints = false
// 设置图片适应方式
iconImageView.contentMode = .scaleAspectFit
// 检查权限
checkPermissionLabel.translatesAutoresizingMaskIntoConstraints = false
checkPermissionLabel.textAlignment = .center // 设置文本居中
let enable = settingsUtils.checkInstallPermission()
if(enable) {
checkPermissionLabel.text = NSLocalizedString("Install_With_TrollStore_text", comment: "")
checkPermissionLabel.textColor = UIColor.green
checkPermissionLabel.isHidden = !settingsUtils.getShowRootText()
} else {
checkPermissionLabel.text = NSLocalizedString("Need_Install_With_TrollStore_text", comment: "")
checkPermissionLabel.textColor = UIColor.red
}
// 重启按钮
let rebootButton = UIButton(type: .system)
if #available(iOS 15.0, *) {
rebootButton.configuration = .filled()
rebootButton.setTitle(NSLocalizedString("Reboot_Device_text", comment: ""), for: .normal)
} else {
// iOS 14 及以下版本 没这效果只能硬凑了
rebootButton.backgroundColor = UIColor.systemBlue
rebootButton.layer.cornerRadius = 8
rebootButton.clipsToBounds = true
rebootButton.setTitle(NSLocalizedString("Reboot_Device_text", comment: ""), for: .normal)
rebootButton.setTitleColor(.white, for: .normal)
if !enable {
// 禁用状态按钮的样子 照顾一下iOS14 用户 但是又没权限的情况,貌似基本不存在
rebootButton.backgroundColor = UIColor.lightGray
rebootButton.setTitleColor(.darkGray, for: .normal)
}
}
rebootButton.translatesAutoresizingMaskIntoConstraints = false
// 添加点击事件
rebootButton.addTarget(self, action: #selector(onClickRebootButton), for: .touchUpInside)
rebootButton.isEnabled = enable // 无权限的时候不允许点击
// 重启用户空间
let rebootUserSpaceButton = UIButton(type: .system)
if #available(iOS 15.0, *) {
rebootUserSpaceButton.configuration = .filled()
rebootUserSpaceButton.setTitle(NSLocalizedString("Reboot_UserSpace_Device_text", comment: ""), for: .normal)
} else {
// iOS 14 及以下版本 没这效果只能硬凑了
rebootUserSpaceButton.backgroundColor = UIColor.systemBlue
rebootUserSpaceButton.layer.cornerRadius = 8
rebootUserSpaceButton.clipsToBounds = true
rebootUserSpaceButton.setTitle(NSLocalizedString("Reboot_UserSpace_Device_text", comment: ""), for: .normal)
rebootUserSpaceButton.setTitleColor(.white, for: .normal)
if !enable {
// 禁用状态按钮的样子 照顾一下iOS14 用户 但是又没权限的情况,貌似基本不存在
rebootUserSpaceButton.backgroundColor = UIColor.lightGray
rebootUserSpaceButton.setTitleColor(.darkGray, for: .normal)
}
}
rebootUserSpaceButton.translatesAutoresizingMaskIntoConstraints = false
// 添加点击事件
rebootUserSpaceButton.addTarget(self, action: #selector(onClickRebootUserSpaceButton), for: .touchUpInside)
rebootUserSpaceButton.isEnabled = enable // 无权限的时候不允许点击
// Respring
respringButton.setTitle(NSLocalizedString("Respring_text", comment: ""), for: .normal)
respringButton.translatesAutoresizingMaskIntoConstraints = false
respringButton.addTarget(self, action: #selector(onClickRespringButton), for: .touchUpInside)
respringButton.isEnabled = enable // 无权限的时候不允许点击
respringButton.isHidden = !settingsUtils.getEnableRespringFunction()
// 添加设置项
let settingsButton = UIButton(type: .system)
settingsButton.setTitle(NSLocalizedString("Settings_text", comment: ""), for: .normal)
settingsButton.translatesAutoresizingMaskIntoConstraints = false
settingsButton.addTarget(self, action: #selector(onClickSettingsButton), for: .touchUpInside)
// 设置图标
let settingsButtonIcon = UIImage(systemName: "gearshape")
settingsButton.setImage(settingsButtonIcon, for: .normal)
// 调整图标和文本之间的间距
settingsButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: -5, bottom: 0, right: 0)
// 向View中添加控件
self.view.addSubview(iconImageView)
self.view.addSubview(checkPermissionLabel)
self.view.addSubview(rebootButton)
self.view.addSubview(rebootUserSpaceButton)
self.view.addSubview(respringButton)
self.view.addSubview(settingsButton)
// AutoLayout
NSLayoutConstraint.activate([
iconImageView.widthAnchor.constraint(equalToConstant: 100), // 设置宽度
iconImageView.heightAnchor.constraint(equalToConstant: 100), // 设置高度
iconImageView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor), // 水平居中
iconImageView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100),
checkPermissionLabel.centerXAnchor.constraint(equalTo: self.view.centerXAnchor), // 水平居中
checkPermissionLabel.topAnchor.constraint(equalTo: iconImageView.bottomAnchor, constant: 30), // 在 iconImageView 底部,间隔 20 点
checkPermissionLabel.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 20), // 左侧边距
checkPermissionLabel.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -20), // 右侧边距
rebootButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor), // 水平居中
rebootButton.heightAnchor.constraint(equalToConstant: 50),
rebootButton.topAnchor.constraint(equalTo: checkPermissionLabel.bottomAnchor, constant: 30),
rebootButton.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 50), // 左侧边距
rebootButton.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -50), // 右侧边距
rebootUserSpaceButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor), // 水平居中
rebootUserSpaceButton.heightAnchor.constraint(equalToConstant: 50),
rebootUserSpaceButton.topAnchor.constraint(equalTo: rebootButton.bottomAnchor, constant: 30),
rebootUserSpaceButton.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 50), // 左侧边距
rebootUserSpaceButton.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -50), // 右侧边距
respringButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor), // 水平居中
respringButton.heightAnchor.constraint(equalToConstant: 50),
respringButton.topAnchor.constraint(equalTo: rebootUserSpaceButton.bottomAnchor, constant: 20),
respringButton.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 50), // 左侧边距
respringButton.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -50), // 右侧边距
settingsButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor), // 水平居中
settingsButton.heightAnchor.constraint(equalToConstant: 50),
settingsButton.topAnchor.constraint(equalTo: respringButton.bottomAnchor, constant: 30),
settingsButton.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 50), // 左侧边距
settingsButton.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -50) // 右侧边距
])
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// 桌面快捷方式的回调
onClickQuickAction = { [weak self] actionType in
guard let quickAction = SettingsUtils.QuickActionType(rawValue: actionType) else {
NSLog("Invalid quick action type: \(actionType)")
return
}
switch quickAction {
case .Reboot:
self?.onClickRebootButton()
case .Respring:
self?.onClickRespringButton()
case .CancelTimer:
self?.onClickCloseTimerQuickAction()
}
}
// 打开app时的自动操作
if settingsUtils.getEnableAction() && settingsUtils.getOpenApplicationAction() {
let action = settingsUtils.getAction()
switch action {
case .Reboot:
self.onClickRebootButton()
case .Respring:
self.onClickRespringButton()
}
}
}
@objc func onClickRebootButton() {
if settingsUtils.getShowAlertBeforeAction() {
// 创建 UIAlertController
let alertController = UIAlertController(title: nil, message: String.localizedStringWithFormat(NSLocalizedString("Action_Alert", comment: ""), NSLocalizedString("Reboot_Device_text", comment: "")), preferredStyle: .alert)
// 创建 "确定" 按钮(红色)
let confirmAction = UIAlertAction(title: NSLocalizedString("Confirm_text", comment: ""), style: .destructive) { _ in
self.rebootDevice()
}
// 创建 "取消" 按钮
let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel_text", comment: ""), style: .cancel) { _ in
//
}
// 添加按钮到 UIAlertController
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
// 显示弹窗
self.present(alertController, animated: true, completion: nil)
} else {
self.rebootDevice()
}
}
@objc func onClickRebootUserSpaceButton() {
if settingsUtils.getShowAlertBeforeAction() {
// 创建 UIAlertController
let alertController = UIAlertController(title: nil, message: String.localizedStringWithFormat(NSLocalizedString("Action_Alert", comment: ""), NSLocalizedString("Reboot_UserSpace_Device_text", comment: "")), preferredStyle: .alert)
// 创建 "确定" 按钮(红色)
let confirmAction = UIAlertAction(title: NSLocalizedString("Confirm_text", comment: ""), style: .destructive) { _ in
self.rebootUserSpaceDevice()
}
// 创建 "取消" 按钮
let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel_text", comment: ""), style: .cancel) { _ in
//
}
// 添加按钮到 UIAlertController
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
// 显示弹窗
self.present(alertController, animated: true, completion: nil)
} else {
self.rebootUserSpaceDevice()
}
}
private func rebootDevice() {
let time = settingsUtils.getTime()
if settingsUtils.getEnableAction() && time > 0 {
showCountdownAlert(actionName: NSLocalizedString("Reboot_Device_text", comment: ""), countdownSeconds: time) {
//计时器结束的操作
let deviceController = DeviceController()
deviceController.rebootDevice()
}
} else {
let deviceController = DeviceController()
deviceController.rebootDevice()
}
}
private func rebootUserSpaceDevice() {
let time = settingsUtils.getTime()
if settingsUtils.getEnableAction() && time > 0 {
showCountdownAlert(actionName: NSLocalizedString("Reboot_UserSpace_Device_text", comment: ""), countdownSeconds: time) {
//计时器结束的操作
let deviceController = DeviceController()
deviceController.rebootUserspace()
}
} else {
let deviceController = DeviceController()
deviceController.rebootUserspace()
}
}
@objc func onClickRespringButton() {
if settingsUtils.getShowAlertBeforeAction() {
// 创建 UIAlertController
let alertController = UIAlertController(title: nil, message: String.localizedStringWithFormat(NSLocalizedString("Action_Alert", comment: ""), NSLocalizedString("Respring_text", comment: "")), preferredStyle: .alert)
// 创建 "确定" 按钮(红色)
let confirmAction = UIAlertAction(title: NSLocalizedString("Confirm_text", comment: ""), style: .destructive) { _ in
self.respring()
}
// 创建 "取消" 按钮
let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel_text", comment: ""), style: .cancel) { _ in
//
}
// 添加按钮到 UIAlertController
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
// 显示弹窗
self.present(alertController, animated: true, completion: nil)
} else {
self.respring()
}
}
private func respring() {
let time = settingsUtils.getTime()
if settingsUtils.getEnableAction() && time > 0 {
showCountdownAlert(actionName: NSLocalizedString("Respring_text", comment: ""), countdownSeconds: time) {
//计时器结束的操作
let deviceController = DeviceController()
deviceController.respring()
}
} else {
let deviceController = DeviceController()
deviceController.respring()
}
}
@objc func onClickSettingsButton() {
let settingsViewController = SettingsViewController()
// 注册监听器
settingsViewController.onSettingsChanged = { [weak self] in
self?.updateUI()
}
// 嵌入到导航控制器
let navController = UINavigationController(rootViewController: settingsViewController)
// 设置导航栏完成按钮
let doneButton = UIBarButtonItem(title: NSLocalizedString("Done_text", comment: ""), style: .done, target: self, action: #selector(onClickDoneButton))
settingsViewController.navigationItem.rightBarButtonItem = doneButton
// 设置模态视图的显示样式
navController.modalPresentationStyle = .pageSheet
navController.modalTransitionStyle = .coverVertical
// 显示带导航栏的 SettingsViewController
present(navController, animated: true, completion: nil)
}
@objc func onClickDoneButton() {
// 关闭模态视图
dismiss(animated: true, completion: nil)
}
private func showCountdownAlert(actionName: String, countdownSeconds: Int, action: @escaping () -> Void) {
guard countdownSeconds > 0 else {
print("Invalid countdownSeconds: \(countdownSeconds). It should be greater than 0.")
return
}
// 初始化倒计时变量
var remainingSeconds = countdownSeconds
var timer: Timer?
// 创建 UIAlertController
let alertController = UIAlertController(
title: String.localizedStringWithFormat(NSLocalizedString("Countdown_Title_text", comment: ""), actionName),
message: String.localizedStringWithFormat(NSLocalizedString("Countdown_Message_text", comment: ""), remainingSeconds),
preferredStyle: .alert
)
// 添加取消按钮
let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel_text", comment: ""), style: .destructive) { _ in
timer?.invalidate() // 停止计时器
}
alertController.addAction(cancelAction)
// 显示弹窗
DispatchQueue.main.async {
self.present(alertController, animated: true, completion: nil)
}
// 创建 Timer,倒计时
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
guard remainingSeconds > 0 else { // 确保倒计时不为负数
timer?.invalidate()
return
}
remainingSeconds -= 1
DispatchQueue.main.async {
alertController.message = String.localizedStringWithFormat(NSLocalizedString("Countdown_Message_text", comment: ""), remainingSeconds)
}
// 倒计时结束,执行操作
if remainingSeconds <= 0 {
timer?.invalidate() // 停止计时器
DispatchQueue.main.async {
alertController.dismiss(animated: true, completion: nil) // 关闭弹窗
action() // 执行操作
}
}
}
}
private func updateUI() { // 因为设置更改 更新界面
checkPermissionLabel.isHidden = !settingsUtils.getShowRootText()
respringButton.isHidden = !settingsUtils.getEnableRespringFunction()
}
private func onClickCloseTimerQuickAction() {
settingsUtils.setEnableAction(value: false)
// 显示一个弹窗 提示用户倒计时器已被禁用
let alertController = UIAlertController(title: nil, message: NSLocalizedString("Timer_Closed_text", comment: ""), preferredStyle: .alert)
let dismissAction = UIAlertAction(title: NSLocalizedString("Dismiss_text", comment: ""), style: .cancel) { _ in
//
}
// 添加按钮到 UIAlertController
alertController.addAction(dismissAction)
// 显示弹窗
self.present(alertController, animated: true, completion: nil)
settingsUtils.configQuickActions(application: UIApplication.shared)
}
}
extension UIColor {
convenience init(hex: String) {
var hexSanitized = hex.trimmingCharacters(in: .whitespacesAndNewlines)
hexSanitized = hexSanitized.replacingOccurrences(of: "#", with: "")
var rgb: UInt64 = 0
Scanner(string: hexSanitized).scanHexInt64(&rgb)
let red = CGFloat((rgb & 0xFF0000) >> 16) / 255.0
let green = CGFloat((rgb & 0x00FF00) >> 8) / 255.0
let blue = CGFloat(rgb & 0x0000FF) / 255.0
self.init(red: red, green: green, blue: blue, alpha: 1.0)
}
}