Callback.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // Callback.swift
  3. // Runner
  4. //
  5. // Created by Yayan Dwi on 15/04/20.
  6. // Copyright © 2020 The Chromium Authors. All rights reserved.
  7. //
  8. import Foundation
  9. import nuSDKService
  10. import Network
  11. import NotificationBannerSwift
  12. class Callback : CallBack {
  13. var sID: String = "Callback"
  14. func connectionStateChanged(sUserID: String!, sDeviceID: String!, bConState: Bool!, nConType: Int!, nConSubType: Int!, nCLMConStat: UInt8!) {
  15. //print(sUserID, "/", sDeviceID, "/", bConState, "/", nConType, "/", nConSubType, "/", nCLMConStat)
  16. if let dispatch = Nexilis.dispatch, bConState {
  17. dispatch.leave()
  18. }
  19. if let delegate = Nexilis.shared.connectionDelegate {
  20. delegate.connectionStateChanged(userId: sUserID, deviceId: sDeviceID, state: bConState)
  21. }
  22. InquiryThread.default.set(wait: nCLMConStat == 0)
  23. OutgoingThread.default.set(wait: nCLMConStat == 0)
  24. }
  25. func gpsStateChanged(nState: Int!) {
  26. }
  27. func sleepStateChanged(bState: Bool!) {
  28. }
  29. func callStateChanged(nState: Int!, sMessage: String!) -> Int {
  30. //print(nState,"/",sMessage)
  31. if nState == Nexilis.AUDIO_CALL_INCOMING || nState == Nexilis.VIDEO_CALL_INCOMING {
  32. if let delegate = Nexilis.shared.callDelegate {
  33. if !Nexilis.showLibraryNotification || Nexilis.callAPNActivated {
  34. delegate.onStatusCall(state: nState, message: sMessage)
  35. } else {
  36. delegate.onIncomingCall(state: nState, message: sMessage)
  37. }
  38. }
  39. } else {
  40. if let delegate = Nexilis.shared.callDelegate {
  41. delegate.onStatusCall(state: nState, message: sMessage)
  42. }
  43. }
  44. return 1
  45. }
  46. func bcastStateChanged(nState: Int!, sMessage: String!) -> Int {
  47. //print("LS CALLBACK ",nState,"/",sMessage)
  48. if let delegate = Nexilis.shared.streamingDelagate {
  49. delegate.onStartLS(state: nState, message: sMessage)
  50. }
  51. if let delegate = Nexilis.shared.streamingDelagate {
  52. delegate.onJoinLS(state: nState, message: sMessage)
  53. }
  54. if let delegate = Nexilis.shared.seminarDelegate {
  55. delegate.onStartSeminar(state: nState, message: sMessage)
  56. }
  57. if let delegate = Nexilis.shared.seminarDelegate {
  58. delegate.onJoinSeminar(state: nState, message: sMessage)
  59. }
  60. return 1
  61. }
  62. func sshareStateChanged(nState: Int!, sMessage: String!) -> Int {
  63. //print("Screen sharing state: ",nState,"/",sMessage)
  64. switch nState {
  65. case 0:
  66. if (sMessage.starts(with: "Initiating")){
  67. if let delegate = Nexilis.shared.screenSharingDelegate {
  68. delegate.onStartScreenSharing(state: nState, message: sMessage)
  69. }
  70. }
  71. case 12:
  72. if let delegate = Nexilis.shared.screenSharingDelegate {
  73. delegate.onStartScreenSharing(state: nState, message: sMessage)
  74. }
  75. case 22:
  76. if let delegate = Nexilis.shared.screenSharingDelegate {
  77. delegate.onJoinScreenSharing(state: nState, message: sMessage)
  78. }
  79. case 88:
  80. if let delegate = Nexilis.shared.screenSharingDelegate {
  81. delegate.onStartScreenSharing(state: nState, message: sMessage)
  82. }
  83. if let delegate = Nexilis.shared.screenSharingDelegate {
  84. delegate.onJoinScreenSharing(state: nState, message: sMessage)
  85. }
  86. default:
  87. break
  88. }
  89. return 1
  90. }
  91. func incomingData(sPacketID: String!, oData: AnyObject!) throws {
  92. Nexilis.incomingData(packetId: sPacketID!, data: oData!)
  93. }
  94. func lateResponse(sPacketID: String!, sResponse: String!) throws {
  95. }
  96. func asycnACKReceived(sPacketID: String!) throws {
  97. //print("asycnACKReceived: \(sPacketID)")
  98. DispatchQueue.global().async {
  99. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  100. do {
  101. OutgoingThread.default.delOutgoing(fmdb: fmdb, packageId: sPacketID)
  102. } catch {
  103. rollback.pointee = true
  104. print("Access database error: \(error.localizedDescription)")
  105. }
  106. })
  107. }
  108. }
  109. func locationUpdated(lTime: Int64!, sLocationInfo: String!) {
  110. }
  111. func resetDB() {
  112. }
  113. }
  114. class NetworkMonitor {
  115. static let shared = NetworkMonitor()
  116. private let monitor = NWPathMonitor()
  117. private var isMonitoring = false
  118. var isConnected: Bool = false
  119. var fromDisconnect = false
  120. var timerReloadData: Timer?
  121. private init() {}
  122. func startMonitoring() {
  123. guard !isMonitoring else { return }
  124. monitor.pathUpdateHandler = { path in
  125. self.canAccessGoogle(completion: { [self] connected in
  126. self.isConnected = connected
  127. InquiryThread.default.set(wait: !connected)
  128. OutgoingThread.default.set(wait: !connected)
  129. if !connected {
  130. fromDisconnect = true
  131. DispatchQueue.main.async { [self] in
  132. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  133. imageView.tintColor = .white
  134. let banner = FloatingNotificationBanner(title: "Check your connection".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
  135. banner.show()
  136. if Utils.getSecureFolderEncrypt().isEmpty && Database.shared.database != nil {
  137. timerReloadData?.invalidate()
  138. timerReloadData = Timer.scheduledTimer(withTimeInterval: 3, repeats: false) {_ in
  139. Database.shared.database = nil
  140. FileEncryption.shared.aesKey = nil
  141. FileEncryption.shared.aesIV = nil
  142. Database.recreateInstance()
  143. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "disconnected_nexilis"), object: nil, userInfo: nil)
  144. if let navigationC = UIApplication.shared.visibleViewController as? UINavigationController {
  145. if navigationC.viewControllers[navigationC.viewControllers.count - 1] is EditorPersonal || navigationC.viewControllers[navigationC.viewControllers.count - 1] is EditorGroup {
  146. navigationC.popViewController(animated: true)
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }
  153. if connected && fromDisconnect {
  154. fromDisconnect = false
  155. DispatchQueue.main.async {
  156. self.timerReloadData?.invalidate()
  157. self.timerReloadData = nil
  158. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  159. imageView.tintColor = .white
  160. let banner = FloatingNotificationBanner(title: "You're Connected".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .success, colors: nil, iconPosition: .center)
  161. banner.show()
  162. }
  163. if Database.shared.database == nil {
  164. Nexilis.getFeatureAccess()
  165. }
  166. }
  167. })
  168. }
  169. let queue = DispatchQueue.global(qos: .background)
  170. monitor.start(queue: queue)
  171. isMonitoring = true
  172. }
  173. func stopMonitoring() {
  174. guard isMonitoring else { return }
  175. monitor.cancel()
  176. isMonitoring = false
  177. }
  178. func canAccessGoogle(completion: @escaping (Bool) -> Void) {
  179. guard let url = URL(string: "https://www.google.com") else {
  180. completion(false)
  181. return
  182. }
  183. var request = URLRequest(url: url)
  184. request.timeoutInterval = 5
  185. let task = URLSession.shared.dataTask(with: request) { _, response, error in
  186. if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 {
  187. completion(true)
  188. } else {
  189. completion(false)
  190. }
  191. }
  192. task.resume()
  193. }
  194. }