You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExtensionDelegate.swift 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright @ 2018-present 8x8, Inc.
  3. * Copyright @ 2017-2018 Atlassian Pty Ltd
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import WatchConnectivity
  18. import WatchKit
  19. class ExtensionDelegate: NSObject, WCSessionDelegate, WKExtensionDelegate {
  20. var currentContext : JitsiMeetContext = JitsiMeetContext()
  21. static var currentJitsiMeetContext: JitsiMeetContext {
  22. get {
  23. return (WKExtension.shared().delegate as! ExtensionDelegate).currentContext
  24. }
  25. }
  26. func applicationDidFinishLaunching() {
  27. // Start Watch Connectivity
  28. if WCSession.isSupported() {
  29. let session = WCSession.default
  30. session.delegate = self
  31. session.activate()
  32. }
  33. }
  34. func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
  35. // Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one.
  36. for task in backgroundTasks {
  37. // Use a switch statement to check the task type
  38. switch task {
  39. case let backgroundTask as WKApplicationRefreshBackgroundTask:
  40. // Be sure to complete the background task once you’re done.
  41. backgroundTask.setTaskCompletedWithSnapshot(false)
  42. case let snapshotTask as WKSnapshotRefreshBackgroundTask:
  43. // Snapshot tasks have a unique completion call, make sure to set your expiration date
  44. snapshotTask.setTaskCompleted(restoredDefaultState: true, estimatedSnapshotExpiration: Date.distantFuture, userInfo: nil)
  45. case let connectivityTask as WKWatchConnectivityRefreshBackgroundTask:
  46. // Be sure to complete the connectivity task once you’re done.
  47. connectivityTask.setTaskCompletedWithSnapshot(false)
  48. case let urlSessionTask as WKURLSessionRefreshBackgroundTask:
  49. // Be sure to complete the URL session task once you’re done.
  50. urlSessionTask.setTaskCompletedWithSnapshot(false)
  51. default:
  52. // make sure to complete unhandled task types
  53. task.setTaskCompletedWithSnapshot(false)
  54. }
  55. }
  56. }
  57. func session(_ session: WCSession, activationDidCompleteWith
  58. activationState: WCSessionActivationState, error: Error?) {
  59. if let error = error {
  60. print("WATCH Session activation failed with error: \(error.localizedDescription)")
  61. return
  62. }
  63. print("WATCH Session activated with state: \(activationState.rawValue)")
  64. }
  65. func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
  66. DispatchQueue.main.async {
  67. let newContext = JitsiMeetContext(context: applicationContext)
  68. print("WATCH got new context: \(newContext.description)");
  69. // Update context on the root controller which displays the recent list
  70. let controller = WKExtension.shared().rootInterfaceController as! InterfaceController
  71. controller.updateUI(newContext)
  72. // If the current controller is not the in-call controller and we have a
  73. // conference URL, show the in-call controller
  74. if let currentController = WKExtension.shared().visibleInterfaceController as? InterfaceController {
  75. // Go to the in-call controller only if the conference URL has changed, because the user may have
  76. // clicked the back button
  77. if newContext.conferenceURL != nil
  78. && self.currentContext.conferenceURL != newContext.conferenceURL {
  79. currentController.pushController(withName: "InCallController", context: newContext)
  80. }
  81. } else if let inCallController = WKExtension.shared().visibleInterfaceController as? InCallController {
  82. if newContext.conferenceURL == nil {
  83. inCallController.popToRootController()
  84. } else {
  85. inCallController.updateUI(newContext)
  86. }
  87. }
  88. self.currentContext = newContext;
  89. }
  90. }
  91. }