SessionInitiator.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2022 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. #if os(iOS) || os(tvOS) || os(visionOS)
  16. import UIKit
  17. #elseif os(macOS)
  18. import AppKit
  19. import Cocoa
  20. #elseif os(watchOS)
  21. import WatchKit
  22. #endif // os(iOS) || os(tvOS)
  23. #if SWIFT_PACKAGE
  24. internal import GoogleUtilities_Environment
  25. #else
  26. internal import GoogleUtilities
  27. #endif // SWIFT_PACKAGE
  28. /// The SessionInitiator is responsible for:
  29. /// 1) Running the initiate callback whenever a Session Start Event should
  30. /// begin sending. This can happen at a cold start of the app, and when it
  31. /// been in the background for a period of time (originally set at 30 mins)
  32. /// and comes to the foreground.
  33. ///
  34. class SessionInitiator {
  35. let currentTime: () -> Date
  36. let settings: SettingsProtocol
  37. private var backgroundTime = Date.distantFuture
  38. private var initiateSessionStart: () -> Void = {}
  39. init(settings: SettingsProtocol, currentTimeProvider: @escaping () -> Date = Date.init) {
  40. currentTime = currentTimeProvider
  41. self.settings = settings
  42. }
  43. func beginListening(initiateSessionStart: @escaping () -> Void) {
  44. self.initiateSessionStart = initiateSessionStart
  45. self.initiateSessionStart()
  46. let notificationCenter = NotificationCenter.default
  47. #if os(iOS) || os(tvOS) || os(visionOS)
  48. // Change background update event listener for iPadOS 26 multi-windowing support
  49. if #available(iOS 26, *), GULAppEnvironmentUtil.appleDevicePlatform().contains("ipados") {
  50. notificationCenter.addObserver(
  51. self,
  52. selector: #selector(appBackgrounded),
  53. name: UIApplication.willResignActiveNotification,
  54. object: nil
  55. )
  56. } else {
  57. notificationCenter.addObserver(
  58. self,
  59. selector: #selector(appBackgrounded),
  60. name: UIApplication.didEnterBackgroundNotification,
  61. object: nil
  62. )
  63. }
  64. notificationCenter.addObserver(
  65. self,
  66. selector: #selector(appForegrounded),
  67. name: UIApplication.didBecomeActiveNotification,
  68. object: nil
  69. )
  70. #elseif os(macOS)
  71. notificationCenter.addObserver(
  72. self,
  73. selector: #selector(appBackgrounded),
  74. name: NSApplication.didResignActiveNotification,
  75. object: nil
  76. )
  77. notificationCenter.addObserver(
  78. self,
  79. selector: #selector(appForegrounded),
  80. name: NSApplication.didBecomeActiveNotification,
  81. object: nil
  82. )
  83. #elseif os(watchOS)
  84. // Versions below WatchOS 7 do not support lifecycle events
  85. if #available(watchOSApplicationExtension 7.0, *) {
  86. notificationCenter.addObserver(
  87. self,
  88. selector: #selector(appBackgrounded),
  89. name: WKExtension.applicationDidEnterBackgroundNotification,
  90. object: nil
  91. )
  92. notificationCenter.addObserver(
  93. self,
  94. selector: #selector(appForegrounded),
  95. name: WKExtension.applicationDidBecomeActiveNotification,
  96. object: nil
  97. )
  98. }
  99. #endif // os(iOS) || os(tvOS)
  100. }
  101. @objc private func appBackgrounded() {
  102. backgroundTime = currentTime()
  103. }
  104. @objc private func appForegrounded() {
  105. let interval = currentTime().timeIntervalSince(backgroundTime)
  106. // If the interval is greater the the session timeout duration, generate a new session.
  107. if interval > settings.sessionTimeout {
  108. initiateSessionStart()
  109. }
  110. }
  111. }