-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathAppSettingsOptions.swift
1357 lines (1114 loc) · 55.4 KB
/
AppSettingsOptions.swift
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0
import Foundation
import Shared
import Account
import LocalAuthentication
import Glean
// This file contains all of the settings available in the main settings screen of the app.
private var ShowDebugSettings: Bool = false
private var DebugSettingsClickCount: Int = 0
private var disclosureIndicator: UIImageView {
let disclosureIndicator = UIImageView()
disclosureIndicator.image = UIImage(named: "menu-Disclosure")?.withRenderingMode(.alwaysTemplate).imageFlippedForRightToLeftLayoutDirection()
disclosureIndicator.tintColor = UIColor.theme.tableView.accessoryViewTint
disclosureIndicator.sizeToFit()
return disclosureIndicator
}
// MARK: - ConnectSetting
// Sync setting for connecting a Firefox Account. Shown when we don't have an account.
class ConnectSetting: WithoutAccountSetting {
override var accessoryView: UIImageView? { return disclosureIndicator }
override var title: NSAttributedString? {
return NSAttributedString(string: .Settings.Sync.ButtonTitle, attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override var accessibilityIdentifier: String? { return "SignInToSync" }
override func onClick(_ navigationController: UINavigationController?) {
let viewController = FirefoxAccountSignInViewController(profile: profile, parentType: .settings, deepLinkParams: nil)
TelemetryWrapper.recordEvent(category: .firefoxAccount, method: .view, object: .settings)
navigationController?.pushViewController(viewController, animated: true)
}
override func onConfigureCell(_ cell: UITableViewCell) {
super.onConfigureCell(cell)
cell.imageView?.image = UIImage.templateImageNamed("FxA-Default")
cell.imageView?.tintColor = UIColor.theme.tableView.disabledRowText
cell.imageView?.layer.cornerRadius = (cell.imageView?.frame.size.width)! / 2
cell.imageView?.layer.masksToBounds = true
}
}
// MARK: - SyncNowSetting
class SyncNowSetting: WithAccountSetting {
let imageView = UIImageView(frame: CGRect(width: 30, height: 30))
let syncIconWrapper = UIImage.createWithColor(CGSize(width: 30, height: 30), color: UIColor.clear)
let syncBlueIcon = UIImage(named: "FxA-Sync-Blue")
let syncIcon: UIImage? = {
let image = UIImage(named: "FxA-Sync")
return LegacyThemeManager.instance.currentName == .dark ? image?.tinted(withColor: .white) : image
}()
// Animation used to rotate the Sync icon 360 degrees while syncing is in progress.
let continuousRotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
override init(settings: SettingsTableViewController) {
super.init(settings: settings)
NotificationCenter.default.addObserver(self, selector: #selector(stopRotateSyncIcon), name: .ProfileDidFinishSyncing, object: nil)
}
fileprivate lazy var timestampFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return formatter
}()
fileprivate var syncNowTitle: NSAttributedString {
if !DeviceInfo.hasConnectivity() {
return NSAttributedString(
string: .FxANoInternetConnection,
attributes: [
NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.errorText,
NSAttributedString.Key.font: DynamicFontHelper.defaultHelper.DefaultMediumFont
]
)
}
let syncText = UIColor.theme.tableView.syncText
let headerLightText = UIColor.theme.tableView.headerTextLight
return NSAttributedString(
string: .FxASyncNow,
attributes: [
NSAttributedString.Key.foregroundColor: self.enabled ? syncText : headerLightText,
NSAttributedString.Key.font: DynamicFontHelper.defaultHelper.DefaultStandardFont
]
)
}
fileprivate let syncingTitle = NSAttributedString(
string: .SyncingMessageWithEllipsis,
attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.syncText,
NSAttributedString.Key.font: UIFont.systemFont(
ofSize: DynamicFontHelper.defaultHelper.DefaultStandardFontSize,
weight: UIFont.Weight.regular)])
func startRotateSyncIcon() {
DispatchQueue.main.async {
self.imageView.layer.add(self.continuousRotateAnimation, forKey: "rotateKey")
}
}
@objc func stopRotateSyncIcon() {
DispatchQueue.main.async {
self.imageView.layer.removeAllAnimations()
}
}
override var accessoryType: UITableViewCell.AccessoryType { return .none }
override var image: UIImage? {
guard let syncStatus = profile.syncManager.syncDisplayState else {
return syncIcon
}
switch syncStatus {
case .inProgress:
return syncBlueIcon
default:
return syncIcon
}
}
override var title: NSAttributedString? {
guard let syncStatus = profile.syncManager.syncDisplayState else {
return syncNowTitle
}
switch syncStatus {
case .bad(let message):
guard let message = message else { return syncNowTitle }
return NSAttributedString(
string: message,
attributes: [
NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.errorText,
NSAttributedString.Key.font: DynamicFontHelper.defaultHelper.DefaultStandardFont])
case .warning(let message):
return NSAttributedString(
string: message,
attributes: [
NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.warningText,
NSAttributedString.Key.font: DynamicFontHelper.defaultHelper.DefaultStandardFont])
case .inProgress:
return syncingTitle
default:
return syncNowTitle
}
}
override var status: NSAttributedString? {
guard let timestamp = profile.syncManager.lastSyncFinishTime else { return nil }
let formattedLabel = timestampFormatter.string(from: Date.fromTimestamp(timestamp))
let attributedString = NSMutableAttributedString(string: formattedLabel)
let attributes = [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.headerTextLight, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)]
let range = NSRange(location: 0, length: attributedString.length)
attributedString.setAttributes(attributes, range: range)
return attributedString
}
override var hidden: Bool { return !enabled }
override var enabled: Bool {
get {
if !DeviceInfo.hasConnectivity() {
return false
}
return profile.hasSyncableAccount()
}
set {
}
}
fileprivate lazy var troubleshootButton: UIButton = {
let troubleshootButton = UIButton(type: .roundedRect)
troubleshootButton.setTitle(.FirefoxSyncTroubleshootTitle, for: .normal)
troubleshootButton.addTarget(self, action: #selector(self.troubleshoot), for: .touchUpInside)
troubleshootButton.tintColor = UIColor.theme.tableView.rowActionAccessory
troubleshootButton.titleLabel?.font = DynamicFontHelper.defaultHelper.DefaultSmallFont
troubleshootButton.sizeToFit()
return troubleshootButton
}()
fileprivate lazy var warningIcon: UIImageView = {
let imageView = UIImageView(image: UIImage(named: "AmberCaution"))
imageView.sizeToFit()
return imageView
}()
fileprivate lazy var errorIcon: UIImageView = {
let imageView = UIImageView(image: UIImage(named: "RedCaution"))
imageView.sizeToFit()
return imageView
}()
fileprivate let syncSUMOURL = SupportUtils.URLForTopic("sync-status-ios")
@objc fileprivate func troubleshoot() {
let viewController = SettingsContentViewController()
viewController.url = syncSUMOURL
settings.navigationController?.pushViewController(viewController, animated: true)
}
override func onConfigureCell(_ cell: UITableViewCell) {
cell.textLabel?.attributedText = title
cell.textLabel?.numberOfLines = 0
cell.textLabel?.lineBreakMode = .byWordWrapping
if let syncStatus = profile.syncManager.syncDisplayState {
switch syncStatus {
case .bad(let message):
if let _ = message {
// add the red warning symbol
// add a link to the MANA page
cell.detailTextLabel?.attributedText = nil
cell.accessoryView = troubleshootButton
addIcon(errorIcon, toCell: cell)
} else {
cell.detailTextLabel?.attributedText = status
cell.accessoryView = nil
}
case .warning:
// add the amber warning symbol
// add a link to the MANA page
cell.detailTextLabel?.attributedText = nil
cell.accessoryView = troubleshootButton
addIcon(warningIcon, toCell: cell)
case .good:
cell.detailTextLabel?.attributedText = status
fallthrough
default:
cell.accessoryView = nil
}
} else {
cell.accessoryView = nil
}
cell.accessoryType = accessoryType
cell.isUserInteractionEnabled = !profile.syncManager.isSyncing && DeviceInfo.hasConnectivity()
// Animation that loops continuously until stopped
continuousRotateAnimation.fromValue = 0.0
continuousRotateAnimation.toValue = CGFloat(Double.pi)
continuousRotateAnimation.isRemovedOnCompletion = true
continuousRotateAnimation.duration = 0.5
continuousRotateAnimation.repeatCount = .infinity
// To ensure sync icon is aligned properly with user's avatar, an image is created with proper
// dimensions and color, then the scaled sync icon is added as a subview.
imageView.contentMode = .center
imageView.image = image
imageView.transform = CGAffineTransform(scaleX: -1, y: 1)
cell.imageView?.subviews.forEach({ $0.removeFromSuperview() })
cell.imageView?.image = syncIconWrapper
cell.imageView?.addSubview(imageView)
if let syncStatus = profile.syncManager.syncDisplayState {
switch syncStatus {
case .inProgress:
self.startRotateSyncIcon()
default:
self.stopRotateSyncIcon()
}
}
}
fileprivate func addIcon(_ image: UIImageView, toCell cell: UITableViewCell) {
cell.contentView.addSubview(image)
cell.textLabel?.snp.updateConstraints { make in
make.leading.equalTo(image.snp.trailing).offset(5)
make.trailing.lessThanOrEqualTo(cell.contentView)
make.centerY.equalTo(cell.contentView)
}
image.snp.makeConstraints { make in
make.leading.equalTo(cell.contentView).offset(17)
make.top.equalTo(cell.textLabel!).offset(2)
}
}
override func onClick(_ navigationController: UINavigationController?) {
if !DeviceInfo.hasConnectivity() {
return
}
NotificationCenter.default.post(name: .UserInitiatedSyncManually, object: nil)
profile.syncManager.syncEverything(why: .syncNow)
}
}
// MARK: - AccountStatusSetting
// Sync setting that shows the current Firefox Account status.
class AccountStatusSetting: WithAccountSetting {
override init(settings: SettingsTableViewController) {
super.init(settings: settings)
NotificationCenter.default.addObserver(self, selector: #selector(updateAccount), name: .FirefoxAccountProfileChanged, object: nil)
}
@objc func updateAccount(notification: Notification) {
DispatchQueue.main.async {
self.settings.tableView.reloadData()
}
}
override var accessoryView: UIImageView? {
return disclosureIndicator
}
override var title: NSAttributedString? {
if let displayName = RustFirefoxAccounts.shared.userProfile?.displayName {
return NSAttributedString(
string: displayName,
attributes: [
NSAttributedString.Key.font: DynamicFontHelper.defaultHelper.DefaultStandardFontBold,
NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.syncText])
}
if let email = RustFirefoxAccounts.shared.userProfile?.email {
return NSAttributedString(
string: email,
attributes: [
NSAttributedString.Key.font: DynamicFontHelper.defaultHelper.DefaultStandardFontBold,
NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.syncText])
}
return nil
}
override var status: NSAttributedString? {
if RustFirefoxAccounts.shared.isActionNeeded {
let string: String = .FxAAccountVerifyPassword
let orange = UIColor.theme.tableView.warningText
let range = NSRange(location: 0, length: string.count)
let attrs = [NSAttributedString.Key.foregroundColor: orange]
let res = NSMutableAttributedString(string: string)
res.setAttributes(attrs, range: range)
return res
}
return nil
}
override func onClick(_ navigationController: UINavigationController?) {
guard !profile.rustFxA.accountNeedsReauth() else {
let vc = FirefoxAccountSignInViewController(profile: profile, parentType: .settings, deepLinkParams: nil)
TelemetryWrapper.recordEvent(category: .firefoxAccount, method: .view, object: .settings)
navigationController?.pushViewController(vc, animated: true)
return
}
let viewController = SyncContentSettingsViewController()
viewController.profile = profile
navigationController?.pushViewController(viewController, animated: true)
}
override func onConfigureCell(_ cell: UITableViewCell) {
super.onConfigureCell(cell)
if let imageView = cell.imageView {
imageView.subviews.forEach({ $0.removeFromSuperview() })
imageView.frame = CGRect(width: 30, height: 30)
imageView.layer.cornerRadius = (imageView.frame.height) / 2
imageView.layer.masksToBounds = true
imageView.image = UIImage(named: ImageIdentifiers.placeholderAvatar)?.createScaled(CGSize(width: 30, height: 30))
RustFirefoxAccounts.shared.avatar?.image.uponQueue(.main) { image in
imageView.image = image.createScaled(CGSize(width: 30, height: 30))
}
}
}
}
// MARK: - Hidden Settings
/// Used for only for debugging purposes. These settings are hidden behind a
/// 5-tap gesture on the Firefox version cell in the Settings Menu
class HiddenSetting: Setting {
unowned let settings: SettingsTableViewController
init(settings: SettingsTableViewController) {
self.settings = settings
super.init(title: nil)
}
override var hidden: Bool {
return !ShowDebugSettings
}
func updateCell(_ navigationController: UINavigationController?) {
let controller = navigationController?.topViewController
let tableView = (controller as? AppSettingsTableViewController)?.tableView
tableView?.reloadData()
}
}
class DeleteExportedDataSetting: HiddenSetting {
override var title: NSAttributedString? {
// Not localized for now.
return NSAttributedString(string: "Debug: delete exported databases", attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override func onClick(_ navigationController: UINavigationController?) {
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let fileManager = FileManager.default
do {
let files = try fileManager.contentsOfDirectory(atPath: documentsPath)
for file in files {
if file.hasPrefix("browser.") || file.hasPrefix("logins.") {
try fileManager.removeItemInDirectory(documentsPath, named: file)
}
}
} catch {
print("Couldn't delete exported data: \(error).")
}
}
}
class ExportBrowserDataSetting: HiddenSetting {
override var title: NSAttributedString? {
// Not localized for now.
return NSAttributedString(string: "Debug: copy databases to app container", attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override func onClick(_ navigationController: UINavigationController?) {
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
do {
let log = Logger.syncLogger
try self.settings.profile.files.copyMatching(fromRelativeDirectory: "", toAbsoluteDirectory: documentsPath) { file in
log.debug("Matcher: \(file)")
return file.hasPrefix("browser.") || file.hasPrefix("logins.") || file.hasPrefix("metadata.")
}
} catch {
print("Couldn't export browser data: \(error).")
}
}
}
class ExportLogDataSetting: HiddenSetting {
override var title: NSAttributedString? {
// Not localized for now.
return NSAttributedString(string: "Debug: copy log files to app container", attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override func onClick(_ navigationController: UINavigationController?) {
Logger.copyPreviousLogsToDocuments()
}
}
/*
FeatureSwitchSetting is a boolean switch for features that are enabled via a FeatureSwitch.
These are usually features behind a partial release and not features released to the entire population.
*/
class FeatureSwitchSetting: BoolSetting {
let featureSwitch: FeatureSwitch
let prefs: Prefs
init(prefs: Prefs, featureSwitch: FeatureSwitch, with title: NSAttributedString) {
self.featureSwitch = featureSwitch
self.prefs = prefs
super.init(prefs: prefs, defaultValue: featureSwitch.isMember(prefs), attributedTitleText: title)
}
override var hidden: Bool {
return !ShowDebugSettings
}
override func displayBool(_ control: UISwitch) {
control.isOn = featureSwitch.isMember(prefs)
}
override func writeBool(_ control: UISwitch) {
self.featureSwitch.setMembership(control.isOn, for: self.prefs)
}
}
class ForceCrashSetting: HiddenSetting {
override var title: NSAttributedString? {
return NSAttributedString(string: "💥 Debug: Force Crash", attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override func onClick(_ navigationController: UINavigationController?) {
SentryIntegration.shared.crash()
}
}
class ChangeToChinaSetting: HiddenSetting {
override var title: NSAttributedString? {
return NSAttributedString(string: "Debug: toggle China version (needs restart)", attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override func onClick(_ navigationController: UINavigationController?) {
if UserDefaults.standard.bool(forKey: debugPrefIsChinaEdition) {
UserDefaults.standard.removeObject(forKey: debugPrefIsChinaEdition)
} else {
UserDefaults.standard.set(true, forKey: debugPrefIsChinaEdition)
}
}
}
class SlowTheDatabase: HiddenSetting {
override var title: NSAttributedString? {
return NSAttributedString(string: "Debug: simulate slow database operations", attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override func onClick(_ navigationController: UINavigationController?) {
debugSimulateSlowDBOperations = !debugSimulateSlowDBOperations
}
}
class ForgetSyncAuthStateDebugSetting: HiddenSetting {
override var title: NSAttributedString? {
return NSAttributedString(
string: "Debug: forget Sync auth state",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override func onClick(_ navigationController: UINavigationController?) {
settings.profile.rustFxA.syncAuthState.invalidate()
settings.tableView.reloadData()
}
}
class SentryIDSetting: HiddenSetting {
let deviceAppHash = UserDefaults(suiteName: AppInfo.sharedContainerIdentifier)?.string(forKey: "SentryDeviceAppHash") ?? "0000000000000000000000000000000000000000"
override var title: NSAttributedString? {
return NSAttributedString(
string: "Sentry ID: \(deviceAppHash)",
attributes: [
NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10)])
}
override func onClick(_ navigationController: UINavigationController?) {
copyAppDeviceIDAndPresentAlert(by: navigationController)
}
func copyAppDeviceIDAndPresentAlert(by navigationController: UINavigationController?) {
let alertTitle: String = .SettingsCopyAppVersionAlertTitle
let alert = AlertController(title: alertTitle, message: nil, preferredStyle: .alert)
getSelectedCell(by: navigationController)?.setSelected(false, animated: true)
UIPasteboard.general.string = deviceAppHash
navigationController?.topViewController?.present(alert, animated: true) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
alert.dismiss(animated: true)
}
}
}
func getSelectedCell(by navigationController: UINavigationController?) -> UITableViewCell? {
let controller = navigationController?.topViewController
let tableView = (controller as? AppSettingsTableViewController)?.tableView
guard let indexPath = tableView?.indexPathForSelectedRow else { return nil }
return tableView?.cellForRow(at: indexPath)
}
}
class ShowEtpCoverSheet: HiddenSetting {
let profile: Profile
override var title: NSAttributedString? {
return NSAttributedString(string: "Debug: ETP Cover Sheet On", attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override init(settings: SettingsTableViewController) {
self.profile = settings.profile
super.init(settings: settings)
}
override func onClick(_ navigationController: UINavigationController?) {
BrowserViewController.foregroundBVC().hasTriedToPresentETPAlready = false
// ETP is shown when user opens app for 3rd time on clean install.
// Hence setting session to 2 (0,1,2) for 3rd install as it starts from 0 being 1st session
self.profile.prefs.setInt(2, forKey: PrefsKeys.KeyInstallSession)
self.profile.prefs.setString(ETPCoverSheetShowType.CleanInstall.rawValue, forKey: PrefsKeys.KeyETPCoverSheetShowType)
}
}
class ExperimentsSettings: HiddenSetting {
override var title: NSAttributedString? { return NSAttributedString(string: "Experiments")}
override func onClick(_ navigationController: UINavigationController?) {
navigationController?.pushViewController(ExperimentsViewController(), animated: true)
}
}
class TogglePullToRefresh: HiddenSetting, FeatureFlaggable {
override var title: NSAttributedString? {
let toNewStatus = featureFlags.isFeatureEnabled(.pullToRefresh, checking: .userOnly) ? "OFF" : "ON"
return NSAttributedString(string: "Toggle Pull to Refresh \(toNewStatus)",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override func onClick(_ navigationController: UINavigationController?) {
let newStatus = !featureFlags.isFeatureEnabled(.pullToRefresh, checking: .userOnly)
featureFlags.set(feature: .pullToRefresh, to: newStatus)
updateCell(navigationController)
}
}
class ToggleInactiveTabs: HiddenSetting, FeatureFlaggable {
override var title: NSAttributedString? {
let toNewStatus = featureFlags.isFeatureEnabled(.inactiveTabs, checking: .userOnly) ? "OFF" : "ON"
return NSAttributedString(string: "Toggle inactive tabs \(toNewStatus)",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override func onClick(_ navigationController: UINavigationController?) {
let newStatus = !featureFlags.isFeatureEnabled(.inactiveTabs, checking: .userOnly)
featureFlags.set(feature: .inactiveTabs, to: newStatus)
InactiveTabModel.hasRunInactiveTabFeatureBefore = false
updateCell(navigationController)
}
}
class ToggleHistoryGroups: HiddenSetting, FeatureFlaggable {
override var title: NSAttributedString? {
let toNewStatus = featureFlags.isFeatureEnabled(.historyGroups, checking: .userOnly) ? "OFF" : "ON"
return NSAttributedString(
string: "Toggle history groups \(toNewStatus)",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override func onClick(_ navigationController: UINavigationController?) {
let newStatus = !featureFlags.isFeatureEnabled(.historyGroups, checking: .userOnly)
featureFlags.set(feature: .historyGroups, to: newStatus)
updateCell(navigationController)
}
}
class ResetContextualHints: HiddenSetting {
let profile: Profile
override var accessibilityIdentifier: String? { return "ResetContextualHints.Setting" }
override var title: NSAttributedString? {
return NSAttributedString(
string: "Reset all contextual hints",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override init(settings: SettingsTableViewController) {
self.profile = settings.profile
super.init(settings: settings)
}
override func onClick(_ navigationController: UINavigationController?) {
PrefsKeys.ContextualHints.allCases.forEach {
self.profile.prefs.removeObjectForKey($0.rawValue)
}
}
}
class OpenFiftyTabsDebugOption: HiddenSetting {
override var accessibilityIdentifier: String? { return "OpenFiftyTabsOption.Setting" }
override var title: NSAttributedString? {
return NSAttributedString(string: "⚠️ Open 50 `mozilla.org` tabs ⚠️", attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override func onClick(_ navigationController: UINavigationController?) {
guard let url = URL(string: "https://www.mozilla.org") else { return }
BrowserViewController.foregroundBVC().debugOpen(numberOfNewTabs: 50, at: url)
}
}
// Show the current version of Firefox
class VersionSetting: Setting {
unowned let settings: SettingsTableViewController
override var accessibilityIdentifier: String? { return "FxVersion" }
init(settings: SettingsTableViewController) {
self.settings = settings
super.init(title: nil)
}
override var title: NSAttributedString? {
return NSAttributedString(string: "\(AppName.shortName) \(AppInfo.appVersion) (\(AppInfo.buildNumber))", attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override func onConfigureCell(_ cell: UITableViewCell) {
super.onConfigureCell(cell)
}
override func onClick(_ navigationController: UINavigationController?) {
DebugSettingsClickCount += 1
if DebugSettingsClickCount >= 5 {
DebugSettingsClickCount = 0
ShowDebugSettings = !ShowDebugSettings
settings.tableView.reloadData()
}
}
override func onLongPress(_ navigationController: UINavigationController?) {
copyAppVersionAndPresentAlert(by: navigationController)
}
func copyAppVersionAndPresentAlert(by navigationController: UINavigationController?) {
let alertTitle: String = .SettingsCopyAppVersionAlertTitle
let alert = AlertController(title: alertTitle, message: nil, preferredStyle: .alert)
getSelectedCell(by: navigationController)?.setSelected(false, animated: true)
UIPasteboard.general.string = self.title?.string
navigationController?.topViewController?.present(alert, animated: true) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
alert.dismiss(animated: true)
}
}
}
func getSelectedCell(by navigationController: UINavigationController?) -> UITableViewCell? {
let controller = navigationController?.topViewController
let tableView = (controller as? AppSettingsTableViewController)?.tableView
guard let indexPath = tableView?.indexPathForSelectedRow else { return nil }
return tableView?.cellForRow(at: indexPath)
}
}
// Opens the license page in a new tab
class LicenseAndAcknowledgementsSetting: Setting {
override var title: NSAttributedString? {
return NSAttributedString(string: .AppSettingsLicenses, attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override var url: URL? {
return URL(string: "\(InternalURL.baseUrl)/\(AboutLicenseHandler.path)")
}
override func onClick(_ navigationController: UINavigationController?) {
setUpAndPushSettingsContentViewController(navigationController, self.url)
}
}
// Opens the App Store review page of this app
class AppStoreReviewSetting: Setting {
override var title: NSAttributedString? {
return NSAttributedString(string: .Settings.About.RateOnAppStore, attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override func onClick(_ navigationController: UINavigationController?) {
RatingPromptManager.goToAppStoreReview()
}
}
// Opens about:rights page in the content view controller
class YourRightsSetting: Setting {
override var title: NSAttributedString? {
return NSAttributedString(string: .AppSettingsYourRights, attributes:
[NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override var url: URL? {
return URL(string: "https://www.mozilla.org/about/legal/terms/firefox/")
}
override func onClick(_ navigationController: UINavigationController?) {
setUpAndPushSettingsContentViewController(navigationController, self.url)
}
}
// Opens the on-boarding screen again
class ShowIntroductionSetting: Setting {
let profile: Profile
override var accessibilityIdentifier: String? { return "ShowTour" }
init(settings: SettingsTableViewController) {
self.profile = settings.profile
super.init(title: NSAttributedString(string: .AppSettingsShowTour, attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText]))
}
override func onClick(_ navigationController: UINavigationController?) {
navigationController?.dismiss(animated: true, completion: {
BrowserViewController.foregroundBVC().presentIntroViewController(true)
})
}
}
class SendFeedbackSetting: Setting {
override var title: NSAttributedString? {
return NSAttributedString(string: .AppSettingsSendFeedback, attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText])
}
override var url: URL? {
return URL(string: "https://connect.mozilla.org/")
}
override func onClick(_ navigationController: UINavigationController?) {
setUpAndPushSettingsContentViewController(navigationController, self.url)
}
}
class SendAnonymousUsageDataSetting: BoolSetting {
init(prefs: Prefs, delegate: SettingsDelegate?) {
let statusText = NSMutableAttributedString()
statusText.append(NSAttributedString(string: .SendUsageSettingMessage, attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.headerTextLight]))
statusText.append(NSAttributedString(string: " "))
statusText.append(NSAttributedString(string: .SendUsageSettingLink, attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.general.highlightBlue]))
super.init(
prefs: prefs, prefKey: AppConstants.PrefSendUsageData, defaultValue: true,
attributedTitleText: NSAttributedString(string: .SendUsageSettingTitle),
attributedStatusText: statusText,
settingDidChange: {
AdjustHelper.setEnabled($0)
Glean.shared.setUploadEnabled($0)
Experiments.setTelemetrySetting($0)
}
)
// We make sure to set this on initialization, in case the setting is turned off
// in which case, we would to make sure that users are opted out of experiments
Experiments.setTelemetrySetting(prefs.boolForKey(AppConstants.PrefSendUsageData) ?? true)
}
override var accessibilityIdentifier: String? { return "SendAnonymousUsageData" }
override var url: URL? {
return SupportUtils.URLForTopic("adjust")
}
override func onClick(_ navigationController: UINavigationController?) {
setUpAndPushSettingsContentViewController(navigationController, self.url)
}
}
class StudiesToggleSetting: BoolSetting {
init(prefs: Prefs, delegate: SettingsDelegate?) {
let statusText = NSMutableAttributedString()
statusText.append(NSAttributedString(string: .SettingsStudiesToggleMessage, attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.headerTextLight]))
statusText.append(NSAttributedString(string: " "))
statusText.append(NSAttributedString(string: .SettingsStudiesToggleLink, attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.general.highlightBlue]))
super.init(
prefs: prefs, prefKey: AppConstants.PrefStudiesToggle, defaultValue: true,
attributedTitleText: NSAttributedString(string: .SettingsStudiesToggleTitle),
attributedStatusText: statusText,
settingDidChange: {
Experiments.setStudiesSetting($0)
}
)
// We make sure to set this on initialization, in case the setting is turned off
// in which case, we would to make sure that users are opted out of experiments
Experiments.setStudiesSetting(prefs.boolForKey(AppConstants.PrefStudiesToggle) ?? true)
}
override var accessibilityIdentifier: String? { return "StudiesToggle" }
override var url: URL? {
return SupportUtils.URLForTopic("ios-studies")
}
override func onClick(_ navigationController: UINavigationController?) {
setUpAndPushSettingsContentViewController(navigationController, self.url)
}
}
// Opens the SUMO page in a new tab
class OpenSupportPageSetting: Setting {
init(delegate: SettingsDelegate?) {
super.init(title: NSAttributedString(string: .AppSettingsHelp, attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText]),
delegate: delegate)
}
override func onClick(_ navigationController: UINavigationController?) {
navigationController?.dismiss(animated: true) {
if let url = URL(string: "https://support.mozilla.org/products/ios") {
self.delegate?.settingsOpenURLInNewTab(url)
}
}
}
}
// Opens the search settings pane
class SearchSetting: Setting {
let profile: Profile
override var accessoryView: UIImageView? { return disclosureIndicator }
override var style: UITableViewCell.CellStyle { return .value1 }
override var status: NSAttributedString { return NSAttributedString(string: profile.searchEngines.defaultEngine.shortName) }
override var accessibilityIdentifier: String? { return "Search" }
init(settings: SettingsTableViewController) {
self.profile = settings.profile
super.init(title: NSAttributedString(string: .AppSettingsSearch, attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText]))
}
override func onClick(_ navigationController: UINavigationController?) {
let viewController = SearchSettingsTableViewController()
viewController.model = profile.searchEngines
viewController.profile = profile
navigationController?.pushViewController(viewController, animated: true)
}
}
class LoginsSetting: Setting {
let profile: Profile
var tabManager: TabManager!
weak var navigationController: UINavigationController?
weak var settings: AppSettingsTableViewController?
override var accessoryView: UIImageView? { return disclosureIndicator }
override var accessibilityIdentifier: String? { return "Logins" }
init(settings: SettingsTableViewController, delegate: SettingsDelegate?) {
self.profile = settings.profile
self.tabManager = settings.tabManager
self.navigationController = settings.navigationController
self.settings = settings as? AppSettingsTableViewController
super.init(title: NSAttributedString(string: .Settings.Passwords.LoginsAndPasswordsTitle, attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText]),
delegate: delegate)
}
func deselectRow () {
if let selectedRow = self.settings?.tableView.indexPathForSelectedRow {
self.settings?.tableView.deselectRow(at: selectedRow, animated: true)
}
}
override func onClick(_: UINavigationController?) {
deselectRow()
guard let navController = navigationController else { return }
let navigationHandler: (_ url: URL?) -> Void = { url in
guard let url = url else { return }
UIWindow.keyWindow?.rootViewController?.dismiss(animated: true, completion: nil)
self.delegate?.settingsOpenURLInNewTab(url)
}
if AppAuthenticator.canAuthenticateDeviceOwner() {
if LoginOnboarding.shouldShow() {
let loginOnboardingViewController = LoginOnboardingViewController(profile: profile, tabManager: tabManager)
loginOnboardingViewController.doneHandler = {
loginOnboardingViewController.dismiss(animated: true)
}
loginOnboardingViewController.proceedHandler = {
LoginListViewController.create(
authenticateInNavigationController: navController,
profile: self.profile,
settingsDelegate: BrowserViewController.foregroundBVC(),
webpageNavigationHandler: navigationHandler).uponQueue(.main) { loginsVC in
guard let loginsVC = loginsVC else { return }
navController.pushViewController(loginsVC, animated: true)
// Remove the onboarding from the navigation stack so that we go straight back to settings
navController.viewControllers.removeAll { viewController in
viewController == loginOnboardingViewController
}
}
}
navigationController?.pushViewController(loginOnboardingViewController, animated: true)
LoginOnboarding.setShown()
} else {
LoginListViewController.create(
authenticateInNavigationController: navController,
profile: profile,
settingsDelegate: BrowserViewController.foregroundBVC(),
webpageNavigationHandler: navigationHandler).uponQueue(.main) { loginsVC in
guard let loginsVC = loginsVC else { return }
navController.pushViewController(loginsVC, animated: true)
}
}
} else {
let viewController = DevicePasscodeRequiredViewController()
viewController.profile = profile
viewController.tabManager = tabManager
navigationController?.pushViewController(viewController, animated: true)
}
}
}
class ContentBlockerSetting: Setting {
let profile: Profile
var tabManager: TabManager!
override var accessoryView: UIImageView? { return disclosureIndicator }
override var accessibilityIdentifier: String? { return "TrackingProtection" }
init(settings: SettingsTableViewController) {
self.profile = settings.profile
self.tabManager = settings.tabManager
super.init(title: NSAttributedString(string: .SettingsTrackingProtectionSectionName, attributes: [NSAttributedString.Key.foregroundColor: UIColor.theme.tableView.rowText]))
}
override func onClick(_ navigationController: UINavigationController?) {
let viewController = ContentBlockerSettingViewController(prefs: profile.prefs)
viewController.profile = profile