ApplicationInfo.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // Copyright 2022 Google LLC
  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. import Foundation
  16. @_implementationOnly import FirebaseCore
  17. #if SWIFT_PACKAGE
  18. import FirebaseSessionsObjC
  19. #endif // SWIFT_PACKAGE
  20. #if SWIFT_PACKAGE
  21. @_implementationOnly import GoogleUtilities_Environment
  22. #else
  23. @_implementationOnly import GoogleUtilities
  24. #endif // SWIFT_PACKAGE
  25. /// Development environment for the application.
  26. enum DevEnvironment: String {
  27. case prod // Prod environment
  28. case staging // Staging environment
  29. case autopush // Autopush environment
  30. }
  31. protocol ApplicationInfoProtocol {
  32. /// Google App ID / GMP App ID
  33. var appID: String { get }
  34. /// Version of the Firebase SDK
  35. var sdkVersion: String { get }
  36. /// Crashlytics-specific device / OS filter values.
  37. var osName: String { get }
  38. /// Model of the device
  39. var deviceModel: String { get }
  40. /// Network information for the application
  41. var networkInfo: NetworkInfoProtocol { get }
  42. /// Development environment on which the application is running.
  43. var environment: DevEnvironment { get }
  44. var appBuildVersion: String { get }
  45. var appDisplayVersion: String { get }
  46. var osBuildVersion: String { get }
  47. var osDisplayVersion: String { get }
  48. }
  49. class ApplicationInfo: ApplicationInfoProtocol {
  50. let appID: String
  51. private let networkInformation: NetworkInfoProtocol
  52. private let envParams: [String: String]
  53. private let infoDict: [String: Any]?
  54. init(appID: String, networkInfo: NetworkInfoProtocol = NetworkInfo(),
  55. envParams: [String: String] = ProcessInfo.processInfo.environment,
  56. infoDict: [String: Any]? = Bundle.main.infoDictionary) {
  57. self.appID = appID
  58. networkInformation = networkInfo
  59. self.envParams = envParams
  60. self.infoDict = infoDict
  61. }
  62. var sdkVersion: String {
  63. return FirebaseVersion()
  64. }
  65. var osName: String {
  66. return GULAppEnvironmentUtil.appleDevicePlatform()
  67. }
  68. var deviceModel: String {
  69. return GULAppEnvironmentUtil.deviceSimulatorModel() ?? ""
  70. }
  71. var networkInfo: NetworkInfoProtocol {
  72. return networkInformation
  73. }
  74. var environment: DevEnvironment {
  75. if let environment = envParams["FirebaseSessionsRunEnvironment"] {
  76. return DevEnvironment(rawValue: environment.trimmingCharacters(in: .whitespaces).lowercased())
  77. ?? DevEnvironment.prod
  78. }
  79. return DevEnvironment.prod
  80. }
  81. var appBuildVersion: String {
  82. return infoDict?["CFBundleVersion"] as? String ?? ""
  83. }
  84. var appDisplayVersion: String {
  85. return infoDict?["CFBundleShortVersionString"] as? String ?? ""
  86. }
  87. var osBuildVersion: String {
  88. return FIRSESGetSysctlEntry("kern.osversion") ?? ""
  89. }
  90. var osDisplayVersion: String {
  91. return GULAppEnvironmentUtil.systemVersion()
  92. }
  93. }