RCNDevice.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. // TODO(rizafran): To migrate to use ISOLanguageCodes in the future
  53. NSDictionary<NSString *, NSArray *> *FIRRemoteConfigFirebaseLocaleMap(void) {
  54. return @{
  55. // Albanian
  56. @"sq" : @[ @"sq_AL" ],
  57. // Belarusian
  58. @"be" : @[ @"be_BY" ],
  59. // Bulgarian
  60. @"bg" : @[ @"bg_BG" ],
  61. // Catalan
  62. @"ca" : @[ @"ca", @"ca_ES" ],
  63. // Croatian
  64. @"hr" : @[ @"hr", @"hr_HR" ],
  65. // Czech
  66. @"cs" : @[ @"cs", @"cs_CZ" ],
  67. // Danish
  68. @"da" : @[ @"da", @"da_DK" ],
  69. // Estonian
  70. @"et" : @[ @"et_EE" ],
  71. // Finnish
  72. @"fi" : @[ @"fi", @"fi_FI" ],
  73. // Hebrew
  74. @"he" : @[ @"he", @"iw_IL" ],
  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. // India
  145. @"hi_IN" :
  146. @[ @"hi_IN", @"ta_IN", @"te_IN", @"mr_IN", @"bn_IN", @"gu_IN", @"kn_IN", @"pa_Guru_IN" ],
  147. // Italian
  148. @"it" : @[ @"it", @"it_IT", @"it_CH", @"it-IT" ],
  149. // Japanese
  150. @"ja" : @[ @"ja", @"ja_JP", @"ja_JP_JP", @"ja-JP" ],
  151. // Norwegian
  152. @"no" : @[ @"nb", @"no_NO", @"no_NO_NY", @"nb_NO" ],
  153. // Brazilian Portuguese
  154. @"pt_BR" : @[ @"pt_BR", @"pt-BR" ],
  155. // European Portuguese
  156. @"pt_PT" : @[ @"pt", @"pt_PT", @"pt-PT" ],
  157. // Serbian
  158. @"sr" : @[ @"sr_BA", @"sr_ME", @"sr_RS", @"sr_Latn_BA", @"sr_Latn_ME", @"sr_Latn_RS" ],
  159. // European Spanish
  160. @"es_ES" : @[ @"es", @"es_ES", @"es-ES" ],
  161. // Mexican Spanish
  162. @"es_MX" : @[ @"es-MX", @"es_MX", @"es_US", @"es-US" ],
  163. // Latin American Spanish
  164. @"es_419" : @[
  165. @"es_AR", @"es_BO", @"es_CL", @"es_CO", @"es_CR", @"es_DO", @"es_EC",
  166. @"es_SV", @"es_GT", @"es_HN", @"es_NI", @"es_PA", @"es_PY", @"es_PE",
  167. @"es_PR", @"es_UY", @"es_VE", @"es-AR", @"es-CL", @"es-CO"
  168. ],
  169. // Thai
  170. @"th" : @[ @"th", @"th_TH", @"th_TH_TH" ],
  171. };
  172. }
  173. NSArray<NSString *> *FIRRemoteConfigAppManagerLocales(void) {
  174. NSMutableArray *locales = [NSMutableArray array];
  175. NSDictionary<NSString *, NSArray *> *localesMap = FIRRemoteConfigFirebaseLocaleMap();
  176. for (NSString *key in localesMap) {
  177. [locales addObjectsFromArray:localesMap[key]];
  178. }
  179. return locales;
  180. }
  181. NSString *FIRRemoteConfigDeviceLocale(void) {
  182. NSArray<NSString *> *locales = FIRRemoteConfigAppManagerLocales();
  183. NSArray<NSString *> *preferredLocalizations =
  184. [NSBundle preferredLocalizationsFromArray:locales
  185. forPreferences:[NSLocale preferredLanguages]];
  186. NSString *legalDocsLanguage = [preferredLocalizations firstObject];
  187. // Use en as the default language
  188. return legalDocsLanguage ? legalDocsLanguage : @"en";
  189. }
  190. NSString *FIRRemoteConfigTimezone(void) {
  191. NSTimeZone *timezone = [NSTimeZone systemTimeZone];
  192. return timezone.name;
  193. }
  194. NSMutableDictionary *FIRRemoteConfigDeviceContextWithProjectIdentifier(
  195. NSString *GMPProjectIdentifier) {
  196. NSMutableDictionary *deviceContext = [[NSMutableDictionary alloc] init];
  197. deviceContext[RCNDeviceContextKeyVersion] = FIRRemoteConfigAppVersion();
  198. deviceContext[RCNDeviceContextKeyBuild] = FIRRemoteConfigAppBuildVersion();
  199. deviceContext[RCNDeviceContextKeyOSVersion] = [GULAppEnvironmentUtil systemVersion];
  200. deviceContext[RCNDeviceContextKeyDeviceLocale] = FIRRemoteConfigDeviceLocale();
  201. // NSDictionary setObjectForKey will fail if there's no GMP project ID, must check ahead.
  202. if (GMPProjectIdentifier) {
  203. deviceContext[RCNDeviceContextKeyGMPProjectIdentifier] = GMPProjectIdentifier;
  204. }
  205. return deviceContext;
  206. }
  207. BOOL FIRRemoteConfigHasDeviceContextChanged(NSDictionary *deviceContext,
  208. NSString *GMPProjectIdentifier) {
  209. if (![deviceContext[RCNDeviceContextKeyVersion] isEqual:FIRRemoteConfigAppVersion()]) {
  210. return YES;
  211. }
  212. if (![deviceContext[RCNDeviceContextKeyBuild] isEqual:FIRRemoteConfigAppBuildVersion()]) {
  213. return YES;
  214. }
  215. if (![deviceContext[RCNDeviceContextKeyOSVersion]
  216. isEqual:[GULAppEnvironmentUtil systemVersion]]) {
  217. return YES;
  218. }
  219. if (![deviceContext[RCNDeviceContextKeyDeviceLocale] isEqual:FIRRemoteConfigDeviceLocale()]) {
  220. return YES;
  221. }
  222. // GMP project id is optional.
  223. if (deviceContext[RCNDeviceContextKeyGMPProjectIdentifier] &&
  224. ![deviceContext[RCNDeviceContextKeyGMPProjectIdentifier] isEqual:GMPProjectIdentifier]) {
  225. return YES;
  226. }
  227. return NO;
  228. }