VMDisplayViewController.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Copyright © 2021 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. import SwiftUI
  17. private var memoryAlertOnce = false
  18. @objc public extension VMDisplayViewController {
  19. var runInBackground: Bool {
  20. boolForSetting("RunInBackground")
  21. }
  22. var disableIdleTimer: Bool {
  23. boolForSetting("DisableIdleTimer")
  24. }
  25. }
  26. // MARK: - View Loading
  27. public extension VMDisplayViewController {
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. }
  31. override func viewWillAppear(_ animated: Bool) {
  32. super.viewWillAppear(animated)
  33. navigationController?.setNavigationBarHidden(true, animated: animated)
  34. }
  35. override func viewWillDisappear(_ animated: Bool) {
  36. super.viewWillDisappear(animated)
  37. if let parent = parent {
  38. parent.setChildForHomeIndicatorAutoHidden(nil)
  39. parent.setChildViewControllerForPointerLock(nil)
  40. UIPress.pressResponderOverride = nil
  41. }
  42. }
  43. override func viewDidAppear(_ animated: Bool) {
  44. super.viewDidAppear(animated)
  45. if let parent = parent {
  46. parent.setChildForHomeIndicatorAutoHidden(self)
  47. parent.setChildViewControllerForPointerLock(self)
  48. UIPress.pressResponderOverride = self
  49. }
  50. if runInBackground {
  51. logger.info("Start location tracking to enable running in background")
  52. UTMLocationManager.sharedInstance().startUpdatingLocation()
  53. }
  54. delegate.displayDidAppear()
  55. }
  56. }
  57. @objc extension VMDisplayViewController {
  58. func enterSuspended(isBusy busy: Bool) {
  59. if !busy {
  60. UIApplication.shared.isIdleTimerDisabled = false
  61. }
  62. }
  63. func enterLive() {
  64. UIApplication.shared.isIdleTimerDisabled = disableIdleTimer
  65. }
  66. private func suspend() {
  67. // dummy function for selector
  68. }
  69. func terminateApplication() {
  70. DispatchQueue.main.async { [self] in
  71. // animate to home screen
  72. let app = UIApplication.shared
  73. app.performSelector(onMainThread: #selector(suspend), with: nil, waitUntilDone: true)
  74. // wait 2 seconds while app is going background
  75. Thread.sleep(forTimeInterval: 2)
  76. // exit app when app is in background
  77. exit(0);
  78. }
  79. }
  80. }
  81. // MARK: Toolbar hiding
  82. public extension VMDisplayViewController {
  83. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  84. for touch in touches {
  85. if touch.type == .direct {
  86. delegate.displayDidAssertUserInteraction()
  87. break
  88. }
  89. }
  90. super.touchesBegan(touches, with: event)
  91. }
  92. }
  93. // MARK: Helper functions
  94. @objc public extension VMDisplayViewController {
  95. /*
  96. - (void)onDelay:(float)delay action:(void (^)(void))block {
  97. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC*0.1), dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), block);
  98. }
  99. - (BOOL)boolForSetting:(NSString *)key {
  100. return [[NSUserDefaults standardUserDefaults] boolForKey:key];
  101. }
  102. - (NSInteger)integerForSetting:(NSString *)key {
  103. return [[NSUserDefaults standardUserDefaults] integerForKey:key];
  104. }
  105. */
  106. func onDelay(_ delay: Float, action: @escaping () -> Void) {
  107. DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + .milliseconds(100), execute: action)
  108. }
  109. func boolForSetting(_ key: String) -> Bool {
  110. return UserDefaults.standard.bool(forKey: key)
  111. }
  112. func integerForSetting(_ key: String) -> Int {
  113. return UserDefaults.standard.integer(forKey: key)
  114. }
  115. }