18
18
import androidx .annotation .NonNull ;
19
19
import com .google .android .datatransport .Encoding ;
20
20
import com .google .android .datatransport .Event ;
21
+ import com .google .android .datatransport .Transformer ;
21
22
import com .google .android .datatransport .Transport ;
22
23
import com .google .android .datatransport .cct .CCTDestination ;
23
24
import com .google .android .datatransport .runtime .TransportRuntime ;
24
25
import com .google .android .gms .tasks .Task ;
25
26
import com .google .android .gms .tasks .TaskCompletionSource ;
27
+ import com .google .android .gms .tasks .Tasks ;
28
+ import com .google .firebase .crashlytics .internal .Logger ;
26
29
import com .google .firebase .crashlytics .internal .common .CrashlyticsReportWithSessionId ;
27
30
import com .google .firebase .crashlytics .internal .model .CrashlyticsReport ;
28
31
import com .google .firebase .crashlytics .internal .model .serialization .CrashlyticsReportJsonTransform ;
@@ -41,8 +44,14 @@ public class DataTransportCrashlyticsReportSender {
41
44
private static final String CRASHLYTICS_API_KEY =
42
45
mergeStrings ("AzSBpY4F0rHiHFdinTvM" , "IayrSTFL9eJ69YeSUO2" );
43
46
private static final String CRASHLYTICS_TRANSPORT_NAME = "FIREBASE_CRASHLYTICS_REPORT" ;
47
+ private static final Transformer <CrashlyticsReport , byte []> DEFAULT_TRANSFORM =
48
+ (r ) -> TRANSFORM .reportToJson (r ).getBytes (Charset .forName ("UTF-8" ));
49
+
50
+ // Assumed limit of 1MB, with a little extra headroom
51
+ private static final int MAX_DATATRANSPORT_BYTES = 1024 * 832 ;
44
52
45
53
private final Transport <CrashlyticsReport > transport ;
54
+ private final Transformer <CrashlyticsReport , byte []> transportTransform ;
46
55
47
56
public static DataTransportCrashlyticsReportSender create (Context context ) {
48
57
TransportRuntime .initialize (context );
@@ -53,26 +62,42 @@ public static DataTransportCrashlyticsReportSender create(Context context) {
53
62
CRASHLYTICS_TRANSPORT_NAME ,
54
63
CrashlyticsReport .class ,
55
64
Encoding .of ("json" ),
56
- r -> TRANSFORM . reportToJson ( r ). getBytes ( Charset . forName ( "UTF-8" )) );
57
- return new DataTransportCrashlyticsReportSender (transport );
65
+ DEFAULT_TRANSFORM );
66
+ return new DataTransportCrashlyticsReportSender (transport , DEFAULT_TRANSFORM );
58
67
}
59
68
60
- DataTransportCrashlyticsReportSender (Transport <CrashlyticsReport > transport ) {
69
+ DataTransportCrashlyticsReportSender (
70
+ Transport <CrashlyticsReport > transport ,
71
+ Transformer <CrashlyticsReport , byte []> transportTransform ) {
61
72
this .transport = transport ;
73
+ this .transportTransform = transportTransform ;
62
74
}
63
75
64
76
@ NonNull
65
77
public Task <CrashlyticsReportWithSessionId > sendReport (
66
- @ NonNull CrashlyticsReportWithSessionId report ) {
78
+ @ NonNull CrashlyticsReportWithSessionId reportWithSessionId ) {
79
+ final CrashlyticsReport report = reportWithSessionId .getReport ();
80
+
81
+ // Workaround for b/152905875, impose a maximum size on reports.
82
+ final int reportSize = transportTransform .apply (report ).length ;
83
+ if (reportSize > MAX_DATATRANSPORT_BYTES ) {
84
+ Logger .getLogger ()
85
+ .d (
86
+ String .format (
87
+ "Report is too large to be sent via DataTransport. Maximum size is %d bytes. Report size is %d bytes. Removing report." ,
88
+ MAX_DATATRANSPORT_BYTES , reportSize ));
89
+ return Tasks .forResult (reportWithSessionId );
90
+ }
91
+
67
92
TaskCompletionSource <CrashlyticsReportWithSessionId > tcs = new TaskCompletionSource <>();
68
93
transport .schedule (
69
- Event .ofUrgent (report . getReport () ),
94
+ Event .ofUrgent (report ),
70
95
error -> {
71
96
if (error != null ) {
72
97
tcs .trySetException (error );
73
98
return ;
74
99
}
75
- tcs .trySetResult (report );
100
+ tcs .trySetResult (reportWithSessionId );
76
101
});
77
102
return tcs .getTask ();
78
103
}
0 commit comments