ApplicationInfo.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. @_implementationOnly import GoogleUtilities
  18. /// Development environment for the application.
  19. enum DevEnvironment: String {
  20. case prod // Prod environment
  21. case staging // Staging environment
  22. case autopush // Autopush environment
  23. }
  24. protocol ApplicationInfoProtocol {
  25. /// Google App ID / GMP App ID
  26. var appID: String { get }
  27. /// Version of the Firebase SDK
  28. var sdkVersion: String { get }
  29. /// Crashlytics-specific device / OS filter values.
  30. var osName: String { get }
  31. /// Model of the device
  32. var deviceModel: String { get }
  33. /// Validated Mobile Country Code and Mobile Network Code
  34. var mccMNC: String { get }
  35. /// Network information for the application
  36. var networkInfo: NetworkInfoProtocol { get }
  37. /// Development environment on which the application is running.
  38. var environment: DevEnvironment { get }
  39. var appBuildVersion: String { get }
  40. var appDisplayVersion: String { get }
  41. }
  42. class ApplicationInfo: ApplicationInfoProtocol {
  43. let appID: String
  44. private let networkInformation: NetworkInfoProtocol
  45. private let envParams: [String: String]
  46. init(appID: String, networkInfo: NetworkInfoProtocol = NetworkInfo(),
  47. envParams: [String: String] = ProcessInfo.processInfo.environment) {
  48. self.appID = appID
  49. networkInformation = networkInfo
  50. self.envParams = envParams
  51. }
  52. var sdkVersion: String {
  53. return FirebaseVersion()
  54. }
  55. var osName: String {
  56. return GULAppEnvironmentUtil.appleDevicePlatform()
  57. }
  58. var deviceModel: String {
  59. return GULAppEnvironmentUtil.deviceSimulatorModel() ?? ""
  60. }
  61. var mccMNC: String {
  62. return FIRSESValidateMccMnc(networkInfo.mobileCountryCode, networkInfo.mobileNetworkCode) ?? ""
  63. }
  64. var networkInfo: NetworkInfoProtocol {
  65. return networkInformation
  66. }
  67. var environment: DevEnvironment {
  68. if let environment = envParams["FirebaseSessionsRunEnvironment"] {
  69. return DevEnvironment(rawValue: environment.trimmingCharacters(in: .whitespaces).lowercased())
  70. ?? DevEnvironment.prod
  71. }
  72. return DevEnvironment.prod
  73. }
  74. var appBuildVersion: String {
  75. return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
  76. }
  77. var appDisplayVersion: String {
  78. return Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? ""
  79. }
  80. }