ApplicationInfo.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /// Validated Mobile Country Code and Mobile Network Code
  41. var mccMNC: String { get }
  42. /// Network information for the application
  43. var networkInfo: NetworkInfoProtocol { get }
  44. /// Development environment on which the application is running.
  45. var environment: DevEnvironment { get }
  46. var appBuildVersion: String { get }
  47. var appDisplayVersion: String { get }
  48. var osBuildVersion: String { get }
  49. var osDisplayVersion: String { get }
  50. }
  51. class ApplicationInfo: ApplicationInfoProtocol {
  52. let appID: String
  53. private let networkInformation: NetworkInfoProtocol
  54. private let envParams: [String: String]
  55. private let infoDict: [String: Any]?
  56. init(appID: String, networkInfo: NetworkInfoProtocol = NetworkInfo(),
  57. envParams: [String: String] = ProcessInfo.processInfo.environment,
  58. infoDict: [String: Any]? = Bundle.main.infoDictionary) {
  59. self.appID = appID
  60. networkInformation = networkInfo
  61. self.envParams = envParams
  62. self.infoDict = infoDict
  63. }
  64. var sdkVersion: String {
  65. return FirebaseVersion()
  66. }
  67. var osName: String {
  68. return GULAppEnvironmentUtil.appleDevicePlatform()
  69. }
  70. var deviceModel: String {
  71. return GULAppEnvironmentUtil.deviceSimulatorModel() ?? ""
  72. }
  73. var mccMNC: String {
  74. return FIRSESValidateMccMnc(networkInfo.mobileCountryCode, networkInfo.mobileNetworkCode) ?? ""
  75. }
  76. var networkInfo: NetworkInfoProtocol {
  77. return networkInformation
  78. }
  79. var environment: DevEnvironment {
  80. if let environment = envParams["FirebaseSessionsRunEnvironment"] {
  81. return DevEnvironment(rawValue: environment.trimmingCharacters(in: .whitespaces).lowercased())
  82. ?? DevEnvironment.prod
  83. }
  84. return DevEnvironment.prod
  85. }
  86. var appBuildVersion: String {
  87. return infoDict?["CFBundleVersion"] as? String ?? ""
  88. }
  89. var appDisplayVersion: String {
  90. return infoDict?["CFBundleShortVersionString"] as? String ?? ""
  91. }
  92. var osBuildVersion: String {
  93. return FIRSESGetSysctlEntry("kern.osversion") ?? ""
  94. }
  95. var osDisplayVersion: String {
  96. return GULAppEnvironmentUtil.systemVersion()
  97. }
  98. }