11package com .mapbox .core .utils ;
22
3+ import android .support .annotation .NonNull ;
34import android .support .annotation .Nullable ;
45import com .mapbox .core .constants .Constants ;
56
1213 */
1314public final class ApiCallHelper {
1415
16+ private static final String ONLY_PRINTABLE_CHARS = "[^\\ p{ASCII}]" ;
17+
1518 private ApiCallHelper () {
1619 // Private constructor preventing instances of class
1720 }
@@ -32,10 +35,31 @@ public static String getHeaderUserAgent(@Nullable String clientAppName) {
3235 if (TextUtils .isEmpty (osName ) || TextUtils .isEmpty (osVersion ) || TextUtils .isEmpty (osArch )) {
3336 return Constants .HEADER_USER_AGENT ;
3437 } else {
38+ return getHeaderUserAgent (clientAppName , osName , osVersion , osArch );
39+ }
40+ }
41+
42+ /**
43+ * Computes a full user agent header of the form:
44+ * {@code MapboxJava/1.2.0 Mac OS X/10.11.5 (x86_64)}.
45+ *
46+ * @param clientAppName Application Name
47+ * @param osName OS name
48+ * @param osVersion OS version
49+ * @param osArch OS Achitecture
50+ * @return {@link String} representing the header user agent
51+ * @since 1.0.0
52+ */
53+ public static String getHeaderUserAgent (@ Nullable String clientAppName ,
54+ @ NonNull String osName , @ NonNull String osVersion , @ NonNull String osArch ) {
55+
56+ osName = osName .replaceAll (ONLY_PRINTABLE_CHARS , "" );
57+ osVersion = osVersion .replaceAll (ONLY_PRINTABLE_CHARS , "" );
58+ osArch = osArch .replaceAll (ONLY_PRINTABLE_CHARS , "" );
3559 String baseUa = String .format (
3660 Locale .US , "%s %s/%s (%s)" , Constants .HEADER_USER_AGENT , osName , osVersion , osArch );
61+
3762 return TextUtils .isEmpty (clientAppName ) ? baseUa : String .format (Locale .US , "%s %s" ,
3863 clientAppName , baseUa );
39- }
4064 }
4165}
0 commit comments