14
14
15
15
package com .google .firebase .segmentation .remote ;
16
16
17
+ import static com .google .firebase .segmentation .FirebaseSegmentation .TAG ;
18
+
19
+ import android .content .Context ;
20
+ import android .content .pm .PackageManager ;
21
+ import android .util .Log ;
17
22
import androidx .annotation .NonNull ;
23
+ import com .google .android .gms .common .util .AndroidUtilsLight ;
24
+ import com .google .android .gms .common .util .Hex ;
18
25
import java .io .IOException ;
19
26
import java .net .URL ;
20
27
import java .util .zip .GZIPOutputStream ;
@@ -37,6 +44,14 @@ public class SegmentationServiceClient {
37
44
private static final String JSON_CONTENT_TYPE = "application/json" ;
38
45
private static final String CONTENT_ENCODING_HEADER_KEY = "Content-Encoding" ;
39
46
private static final String GZIP_CONTENT_ENCODING = "gzip" ;
47
+ private static final String X_ANDROID_PACKAGE_HEADER_KEY = "X-Android-Package" ;
48
+ private static final String X_ANDROID_CERT_HEADER_KEY = "X-Android-Cert" ;
49
+
50
+ private final Context context ;
51
+
52
+ public SegmentationServiceClient (@ NonNull Context context ) {
53
+ this .context = context ;
54
+ }
40
55
41
56
public enum Code {
42
57
OK ,
@@ -78,6 +93,9 @@ public Code updateCustomInstallationId(
78
93
"Authorization" , "FIREBASE_INSTALLATIONS_AUTH " + firebaseInstanceIdToken );
79
94
httpsURLConnection .addRequestProperty (CONTENT_TYPE_HEADER_KEY , JSON_CONTENT_TYPE );
80
95
httpsURLConnection .addRequestProperty (CONTENT_ENCODING_HEADER_KEY , GZIP_CONTENT_ENCODING );
96
+ httpsURLConnection .addRequestProperty (X_ANDROID_PACKAGE_HEADER_KEY , context .getPackageName ());
97
+ httpsURLConnection .addRequestProperty (
98
+ X_ANDROID_CERT_HEADER_KEY , getFingerprintHashForPackage ());
81
99
GZIPOutputStream gzipOutputStream =
82
100
new GZIPOutputStream (httpsURLConnection .getOutputStream ());
83
101
try {
@@ -143,6 +161,9 @@ public Code clearCustomInstallationId(
143
161
"Authorization" , "FIREBASE_INSTALLATIONS_AUTH " + firebaseInstanceIdToken );
144
162
httpsURLConnection .addRequestProperty (CONTENT_TYPE_HEADER_KEY , JSON_CONTENT_TYPE );
145
163
httpsURLConnection .addRequestProperty (CONTENT_ENCODING_HEADER_KEY , GZIP_CONTENT_ENCODING );
164
+ httpsURLConnection .addRequestProperty (X_ANDROID_PACKAGE_HEADER_KEY , context .getPackageName ());
165
+ httpsURLConnection .addRequestProperty (
166
+ X_ANDROID_CERT_HEADER_KEY , getFingerprintHashForPackage ());
146
167
GZIPOutputStream gzipOutputStream =
147
168
new GZIPOutputStream (httpsURLConnection .getOutputStream ());
148
169
try {
@@ -175,4 +196,24 @@ private static JSONObject buildClearCustomSegmentationDataRequestBody(String res
175
196
throws JSONException {
176
197
return new JSONObject ().put ("name" , resourceName );
177
198
}
199
+
200
+ /** Gets the Android package's SHA-1 fingerprint. */
201
+ private String getFingerprintHashForPackage () {
202
+ byte [] hash ;
203
+
204
+ try {
205
+ hash = AndroidUtilsLight .getPackageCertificateHashBytes (context , context .getPackageName ());
206
+
207
+ if (hash == null ) {
208
+ Log .e (TAG , "Could not get fingerprint hash for package: " + context .getPackageName ());
209
+ return null ;
210
+ } else {
211
+ String cert = Hex .bytesToStringUppercase (hash , /* zeroTerminated= */ false );
212
+ return Hex .bytesToStringUppercase (hash , /* zeroTerminated= */ false );
213
+ }
214
+ } catch (PackageManager .NameNotFoundException e ) {
215
+ Log .e (TAG , "No such package: " + context .getPackageName (), e );
216
+ return null ;
217
+ }
218
+ }
178
219
}
0 commit comments