RCNDevice.m 8.4 KB

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