RCNDevice.m 8.3 KB

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