RCNDevice.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright 2019 Google
  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. */
  16. #import "FirebaseRemoteConfig/Sources/RCNDevice.h"
  17. #import <sys/utsname.h>
  18. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  19. #import "FirebaseRemoteConfig/Sources/Private/RCNConfigSettings.h"
  20. #import "FirebaseRemoteConfig/Sources/RCNConfigConstants.h"
  21. #define STR(x) STR_EXPAND(x)
  22. #define STR_EXPAND(x) #x
  23. static NSString *const RCNDeviceContextKeyVersion = @"app_version";
  24. static NSString *const RCNDeviceContextKeyOSVersion = @"os_version";
  25. static NSString *const RCNDeviceContextKeyDeviceLocale = @"device_locale";
  26. static NSString *const RCNDeviceContextKeyLocaleLanguage = @"locale_language";
  27. static NSString *const RCNDeviceContextKeyGMPProjectIdentifier = @"GMP_project_Identifier";
  28. NSString *FIRRemoteConfigAppVersion() {
  29. return [[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"];
  30. }
  31. NSString *FIRRemoteConfigPodVersion() {
  32. return [NSString stringWithUTF8String:STR(FIRRemoteConfig_VERSION)];
  33. }
  34. RCNDeviceModel FIRRemoteConfigDeviceSubtype() {
  35. NSString *model = [GULAppEnvironmentUtil deviceModel];
  36. if ([model hasPrefix:@"iPhone"]) {
  37. return RCNDeviceModelPhone;
  38. }
  39. if ([model isEqualToString:@"iPad"]) {
  40. return RCNDeviceModelTablet;
  41. }
  42. return RCNDeviceModelOther;
  43. }
  44. NSString *FIRRemoteConfigDeviceCountry() {
  45. return [[[NSLocale currentLocale] objectForKey:NSLocaleCountryCode] lowercaseString];
  46. }
  47. NSDictionary<NSString *, NSArray *> *FIRRemoteConfigFirebaseLocaleMap() {
  48. return @{
  49. // Albanian
  50. @"sq" : @[ @"sq_AL" ],
  51. // Belarusian
  52. @"be" : @[ @"be_BY" ],
  53. // Bulgarian
  54. @"bg" : @[ @"bg_BG" ],
  55. // Catalan
  56. @"ca" : @[ @"ca", @"ca_ES" ],
  57. // Croatian
  58. @"hr" : @[ @"hr", @"hr_HR" ],
  59. // Czech
  60. @"cs" : @[ @"cs", @"cs_CZ" ],
  61. // Danish
  62. @"da" : @[ @"da", @"da_DK" ],
  63. // Estonian
  64. @"et" : @[ @"et_EE" ],
  65. // Finnish
  66. @"fi" : @[ @"fi", @"fi_FI" ],
  67. // Hebrew
  68. @"he" : @[ @"he", @"iw_IL" ],
  69. // Hindi
  70. @"hi" : @[ @"hi_IN" ],
  71. // Hungarian
  72. @"hu" : @[ @"hu", @"hu_HU" ],
  73. // Icelandic
  74. @"is" : @[ @"is_IS" ],
  75. // Indonesian
  76. @"id" : @[ @"id", @"in_ID", @"id_ID" ],
  77. // Irish
  78. @"ga" : @[ @"ga_IE" ],
  79. // Korean
  80. @"ko" : @[ @"ko", @"ko_KR", @"ko-KR" ],
  81. // Latvian
  82. @"lv" : @[ @"lv_LV" ],
  83. // Lithuanian
  84. @"lt" : @[ @"lt_LT" ],
  85. // Macedonian
  86. @"mk" : @[ @"mk_MK" ],
  87. // Malay
  88. @"ms" : @[ @"ms_MY" ],
  89. // Maltese
  90. @"ms" : @[ @"mt_MT" ],
  91. // Polish
  92. @"pl" : @[ @"pl", @"pl_PL", @"pl-PL" ],
  93. // Romanian
  94. @"ro" : @[ @"ro", @"ro_RO" ],
  95. // Russian
  96. @"ru" : @[ @"ru_RU", @"ru", @"ru_BY", @"ru_KZ", @"ru-RU" ],
  97. // Slovak
  98. @"sk" : @[ @"sk", @"sk_SK" ],
  99. // Slovenian
  100. @"sl" : @[ @"sl_SI" ],
  101. // Swedish
  102. @"sv" : @[ @"sv", @"sv_SE", @"sv-SE" ],
  103. // Turkish
  104. @"tr" : @[ @"tr", @"tr-TR", @"tr_TR" ],
  105. // Ukrainian
  106. @"uk" : @[ @"uk", @"uk_UA" ],
  107. // Vietnamese
  108. @"vi" : @[ @"vi", @"vi_VN" ],
  109. // The following are groups of locales or locales that sub-divide a
  110. // language).
  111. // Arabic
  112. @"ar" : @[
  113. @"ar", @"ar_DZ", @"ar_BH", @"ar_EG", @"ar_IQ", @"ar_JO", @"ar_KW",
  114. @"ar_LB", @"ar_LY", @"ar_MA", @"ar_OM", @"ar_QA", @"ar_SA", @"ar_SD",
  115. @"ar_SY", @"ar_TN", @"ar_AE", @"ar_YE", @"ar_GB", @"ar-IQ", @"ar_US"
  116. ],
  117. // Simplified Chinese
  118. @"zh_Hans" : @[ @"zh_CN", @"zh_SG", @"zh-Hans" ],
  119. // Traditional Chinese
  120. // Remove zh_HK until console added to the list. Otherwise client sends
  121. // zh_HK and server/console falls back to zh.
  122. // @"zh_Hant" : @[ @"zh_HK", @"zh_TW", @"zh-Hant", @"zh-HK", @"zh-TW" ],
  123. @"zh_Hant" : @[ @"zh_TW", @"zh-Hant", @"zh-TW" ],
  124. // Dutch
  125. @"nl" : @[ @"nl", @"nl_BE", @"nl_NL", @"nl-NL" ],
  126. // English
  127. @"en" : @[
  128. @"en", @"en_AU", @"en_CA", @"en_IN", @"en_IE", @"en_MT", @"en_NZ", @"en_PH",
  129. @"en_SG", @"en_ZA", @"en_GB", @"en_US", @"en_AE", @"en-AE", @"en_AS", @"en-AU",
  130. @"en_BD", @"en-CA", @"en_EG", @"en_ES", @"en_GB", @"en-GB", @"en_HK", @"en_ID",
  131. @"en-IN", @"en_NG", @"en-PH", @"en_PK", @"en-SG", @"en-US"
  132. ],
  133. // French
  134. @"fr" :
  135. @[ @"fr", @"fr_BE", @"fr_CA", @"fr_FR", @"fr_LU", @"fr_CH", @"fr-CA", @"fr-FR", @"fr_MA" ],
  136. // German
  137. @"de" : @[ @"de", @"de_AT", @"de_DE", @"de_LU", @"de_CH", @"de-DE" ],
  138. // Greek
  139. @"el" : @[ @"el", @"el_CY", @"el_GR" ],
  140. // Italian
  141. @"it" : @[ @"it", @"it_IT", @"it_CH", @"it-IT" ],
  142. // Japanese
  143. @"ja" : @[ @"ja", @"ja_JP", @"ja_JP_JP", @"ja-JP" ],
  144. // Norwegian
  145. @"no" : @[ @"nb", @"no_NO", @"no_NO_NY", @"nb_NO" ],
  146. // Brazilian Portuguese
  147. @"pt_BR" : @[ @"pt_BR", @"pt-BR" ],
  148. // European Portuguese
  149. @"pt_PT" : @[ @"pt", @"pt_PT", @"pt-PT" ],
  150. // Serbian
  151. @"sr" : @[ @"sr_BA", @"sr_ME", @"sr_RS", @"sr_Latn_BA", @"sr_Latn_ME", @"sr_Latn_RS" ],
  152. // European Spanish
  153. @"es_ES" : @[ @"es", @"es_ES", @"es-ES" ],
  154. // Mexican Spanish
  155. @"es_MX" : @[ @"es-MX", @"es_MX", @"es_US", @"es-US" ],
  156. // Latin American Spanish
  157. @"es_419" : @[
  158. @"es_AR", @"es_BO", @"es_CL", @"es_CO", @"es_CR", @"es_DO", @"es_EC",
  159. @"es_SV", @"es_GT", @"es_HN", @"es_NI", @"es_PA", @"es_PY", @"es_PE",
  160. @"es_PR", @"es_UY", @"es_VE", @"es-AR", @"es-CL", @"es-CO"
  161. ],
  162. // Thai
  163. @"th" : @[ @"th", @"th_TH", @"th_TH_TH" ],
  164. };
  165. }
  166. NSArray<NSString *> *FIRRemoteConfigAppManagerLocales() {
  167. NSMutableArray *locales = [NSMutableArray array];
  168. NSDictionary<NSString *, NSArray *> *localesMap = FIRRemoteConfigFirebaseLocaleMap();
  169. for (NSString *key in localesMap) {
  170. [locales addObjectsFromArray:localesMap[key]];
  171. }
  172. return locales;
  173. }
  174. NSString *FIRRemoteConfigDeviceLocale() {
  175. NSArray<NSString *> *locales = FIRRemoteConfigAppManagerLocales();
  176. NSArray<NSString *> *preferredLocalizations =
  177. [NSBundle preferredLocalizationsFromArray:locales
  178. forPreferences:[NSLocale preferredLanguages]];
  179. NSString *legalDocsLanguage = [preferredLocalizations firstObject];
  180. // Use en as the default language
  181. return legalDocsLanguage ? legalDocsLanguage : @"en";
  182. }
  183. NSString *FIRRemoteConfigTimezone() {
  184. NSTimeZone *timezone = [NSTimeZone systemTimeZone];
  185. return timezone.name;
  186. }
  187. int FIRRemoteConfigSDKVersion() {
  188. return kRCNMajorVersion * 10000 + kRCNMinorVersion * 100 + kRCNPatchVersion;
  189. }
  190. NSMutableDictionary *FIRRemoteConfigDeviceContextWithProjectIdentifier(
  191. NSString *GMPProjectIdentifier) {
  192. NSMutableDictionary *deviceContext = [[NSMutableDictionary alloc] init];
  193. deviceContext[RCNDeviceContextKeyVersion] = FIRRemoteConfigAppVersion();
  194. deviceContext[RCNDeviceContextKeyOSVersion] = [GULAppEnvironmentUtil systemVersion];
  195. deviceContext[RCNDeviceContextKeyDeviceLocale] = FIRRemoteConfigDeviceLocale();
  196. // NSDictionary setObjectForKey will fail if there's no GMP project ID, must check ahead.
  197. if (GMPProjectIdentifier) {
  198. deviceContext[RCNDeviceContextKeyGMPProjectIdentifier] = GMPProjectIdentifier;
  199. }
  200. return deviceContext;
  201. }
  202. BOOL FIRRemoteConfigHasDeviceContextChanged(NSDictionary *deviceContext,
  203. NSString *GMPProjectIdentifier) {
  204. if (![deviceContext[RCNDeviceContextKeyVersion] isEqual:FIRRemoteConfigAppVersion()]) {
  205. return YES;
  206. }
  207. if (!
  208. [deviceContext[RCNDeviceContextKeyOSVersion] isEqual:[GULAppEnvironmentUtil systemVersion]]) {
  209. return YES;
  210. }
  211. if (![deviceContext[RCNDeviceContextKeyDeviceLocale] isEqual:FIRRemoteConfigDeviceLocale()]) {
  212. return YES;
  213. }
  214. // GMP project id is optional.
  215. if (deviceContext[RCNDeviceContextKeyGMPProjectIdentifier] &&
  216. ![deviceContext[RCNDeviceContextKeyGMPProjectIdentifier] isEqual:GMPProjectIdentifier]) {
  217. return YES;
  218. }
  219. return NO;
  220. }