@@ -388,13 +388,22 @@ synchronized void handleUncaughtException(
388388 new Callable <Task <Void >>() {
389389 @ Override
390390 public Task <Void > call () throws Exception {
391+ final long timestampSeconds = getTimestampSeconds (time );
392+
393+ final String currentSessionId = getCurrentSessionId ();
394+ if (currentSessionId == null ) {
395+ Logger .getLogger ()
396+ .e ("Tried to write a fatal exception while no session was open." );
397+ return Tasks .forResult (null );
398+ }
399+
391400 // We've fatally crashed, so write the marker file that indicates a crash occurred.
392401 crashMarker .create ();
393402
394- long timestampSeconds = getTimestampSeconds ( time );
395- reportingCoordinator . persistFatalEvent ( ex , thread , timestampSeconds );
396- writeFatal (thread , ex , timestampSeconds );
397- writeAppExceptionMarker (time .getTime ());
403+ reportingCoordinator . persistFatalEvent (
404+ ex , thread , makeFirebaseSessionIdentifier ( currentSessionId ) , timestampSeconds );
405+ doWriteFatal (thread , ex , currentSessionId , timestampSeconds );
406+ doWriteAppExceptionMarker (time .getTime ());
398407
399408 Settings settings = settingsDataProvider .getSettings ();
400409 int maxCustomExceptionEvents = settings .getSessionData ().maxCustomExceptionEvents ;
@@ -652,8 +661,15 @@ void writeNonFatalException(@NonNull final Thread thread, @NonNull final Throwab
652661 public void run () {
653662 if (!isHandlingException ()) {
654663 long timestampSeconds = getTimestampSeconds (time );
655- reportingCoordinator .persistNonFatalEvent (ex , thread , timestampSeconds );
656- doWriteNonFatal (thread , ex , timestampSeconds );
664+ final String currentSessionId = getCurrentSessionId ();
665+ if (currentSessionId == null ) {
666+ Logger .getLogger ()
667+ .d ("Tried to write a non-fatal exception while no session was open." );
668+ return ;
669+ }
670+ reportingCoordinator .persistNonFatalEvent (
671+ ex , thread , makeFirebaseSessionIdentifier (currentSessionId ), timestampSeconds );
672+ doWriteNonFatal (thread , ex , currentSessionId , timestampSeconds );
657673 }
658674 }
659675 });
@@ -690,8 +706,12 @@ private void cacheUserData(final UserMetadata userMetaData) {
690706 new Callable <Void >() {
691707 @ Override
692708 public Void call () throws Exception {
693- reportingCoordinator .persistUserId ();
694709 final String currentSessionId = getCurrentSessionId ();
710+ if (currentSessionId == null ) {
711+ Logger .getLogger ().d ("Tried to cache user data while no session was open." );
712+ return null ;
713+ }
714+ reportingCoordinator .persistUserId (makeFirebaseSessionIdentifier (currentSessionId ));
695715 new MetaDataStore (getFilesDir ()).writeUserData (currentSessionId , userMetaData );
696716 return null ;
697717 }
@@ -739,21 +759,14 @@ public Void call() throws Exception {
739759 *
740760 * <p>May return <code>null</code> if no session begin file is present.
741761 */
762+ @ Nullable
742763 private String getCurrentSessionId () {
743764 final File [] sessionBeginFiles = listSortedSessionBeginFiles ();
744765 return (sessionBeginFiles .length > 0 )
745766 ? getSessionIdFromSessionFile (sessionBeginFiles [0 ])
746767 : null ;
747768 }
748769
749- /** @return */
750- private String getPreviousSessionId () {
751- final File [] sessionBeginFiles = listSortedSessionBeginFiles ();
752- return (sessionBeginFiles .length > 1 )
753- ? getSessionIdFromSessionFile (sessionBeginFiles [1 ])
754- : null ;
755- }
756-
757770 /**
758771 * Returns the session ID that forms the beginning part of the name of the provided file
759772 *
@@ -790,7 +803,7 @@ boolean finalizeSessions(int maxCustomExceptionEvents) {
790803
791804 Logger .getLogger ().d ("Finalizing previously open sessions." );
792805 try {
793- doCloseSessions (maxCustomExceptionEvents , false );
806+ doCloseSessions (maxCustomExceptionEvents , true );
794807 } catch (Exception e ) {
795808 Logger .getLogger ().e ("Unable to finalize previously open sessions." , e );
796809 return false ;
@@ -823,16 +836,16 @@ private void doOpenSession() throws Exception {
823836 }
824837
825838 void doCloseSessions (int maxCustomExceptionEvents ) throws Exception {
826- doCloseSessions (maxCustomExceptionEvents , true );
839+ doCloseSessions (maxCustomExceptionEvents , false );
827840 }
828841
829842 /**
830843 * Not synchronized/locked. Must be executed from the single thread executor service used by this
831844 * class.
832845 */
833- private void doCloseSessions (int maxCustomExceptionEvents , boolean includeCurrent )
846+ private void doCloseSessions (int maxCustomExceptionEvents , boolean skipCurrentSession )
834847 throws Exception {
835- final int offset = includeCurrent ? 0 : 1 ;
848+ final int offset = skipCurrentSession ? 1 : 0 ;
836849
837850 trimOpenSessions (MAX_OPEN_SESSIONS + offset );
838851
@@ -850,9 +863,7 @@ private void doCloseSessions(int maxCustomExceptionEvents, boolean includeCurren
850863 // maximum chance that the user code that sets this information has been run.
851864 writeSessionUser (mostRecentSessionIdToClose );
852865
853- if (includeCurrent ) {
854- reportingCoordinator .onEndSession ();
855- } else if (nativeComponent .hasCrashDataForSession (mostRecentSessionIdToClose )) {
866+ if (nativeComponent .hasCrashDataForSession (mostRecentSessionIdToClose )) {
856867 // We only finalize the current session if it's a Java crash, so only finalize native crash
857868 // data when we aren't including current.
858869 finalizePreviousNativeSession (mostRecentSessionIdToClose );
@@ -863,7 +874,13 @@ private void doCloseSessions(int maxCustomExceptionEvents, boolean includeCurren
863874
864875 closeOpenSessions (sessionBeginFiles , offset , maxCustomExceptionEvents );
865876
866- reportingCoordinator .finalizeSessions (getCurrentTimestampSeconds ());
877+ String currentSessionId = null ;
878+ if (skipCurrentSession ) {
879+ currentSessionId =
880+ makeFirebaseSessionIdentifier (getSessionIdFromSessionFile (sessionBeginFiles [0 ]));
881+ }
882+
883+ reportingCoordinator .finalizeSessions (getCurrentTimestampSeconds (), currentSessionId );
867884 }
868885
869886 /**
@@ -942,11 +959,11 @@ private File[] listFilesMatching(FilenameFilter filter) {
942959 return listFilesMatching (getFilesDir (), filter );
943960 }
944961
945- private File [] listFilesMatching (File directory , FilenameFilter filter ) {
962+ private static File [] listFilesMatching (File directory , FilenameFilter filter ) {
946963 return ensureFileArrayNotNull (directory .listFiles (filter ));
947964 }
948965
949- private File [] ensureFileArrayNotNull (File [] files ) {
966+ private static File [] ensureFileArrayNotNull (File [] files ) {
950967 return (files == null ) ? new File [] {} : files ;
951968 }
952969
@@ -1117,7 +1134,7 @@ private void finalizePreviousNativeSession(String previousSessionId) {
11171134 return ;
11181135 }
11191136
1120- writeAppExceptionMarker (eventTime );
1137+ doWriteAppExceptionMarker (eventTime );
11211138 List <NativeSessionFile > nativeSessionFiles =
11221139 getNativeSessionFiles (
11231140 nativeSessionFileProvider ,
@@ -1151,17 +1168,14 @@ private static String makeFirebaseSessionIdentifier(@NonNull String sessionIdent
11511168 * Not synchronized/locked. Must be executed from the single thread executor service used by this
11521169 * class.
11531170 */
1154- private void writeFatal (Thread thread , Throwable ex , long eventTime ) {
1171+ private void doWriteFatal (
1172+ @ NonNull Thread thread ,
1173+ @ NonNull Throwable ex ,
1174+ @ NonNull String currentSessionId ,
1175+ long eventTime ) {
11551176 ClsFileOutputStream fos = null ;
11561177 CodedOutputStream cos = null ;
11571178 try {
1158- final String currentSessionId = getCurrentSessionId ();
1159-
1160- if (currentSessionId == null ) {
1161- Logger .getLogger ().e ("Tried to write a fatal exception while no session was open." );
1162- return ;
1163- }
1164-
11651179 fos = new ClsFileOutputStream (getFilesDir (), currentSessionId + SESSION_FATAL_TAG );
11661180 cos = CodedOutputStream .newInstance (fos );
11671181 writeSessionEvent (cos , thread , ex , eventTime , EVENT_TYPE_CRASH , true );
@@ -1173,7 +1187,7 @@ private void writeFatal(Thread thread, Throwable ex, long eventTime) {
11731187 }
11741188 }
11751189
1176- private void writeAppExceptionMarker (long eventTime ) {
1190+ private void doWriteAppExceptionMarker (long eventTime ) {
11771191 try {
11781192 new File (getFilesDir (), APP_EXCEPTION_MARKER_PREFIX + eventTime ).createNewFile ();
11791193 } catch (IOException e ) {
@@ -1185,14 +1199,11 @@ private void writeAppExceptionMarker(long eventTime) {
11851199 * Not synchronized/locked. Must be executed from the single thread executor service used by this
11861200 * class.
11871201 */
1188- private void doWriteNonFatal (@ NonNull Thread thread , @ NonNull Throwable ex , long eventTime ) {
1189- final String currentSessionId = getCurrentSessionId ();
1190-
1191- if (currentSessionId == null ) {
1192- Logger .getLogger ().d ("Tried to write a non-fatal exception while no session was open." );
1193- return ;
1194- }
1195-
1202+ private void doWriteNonFatal (
1203+ @ NonNull Thread thread ,
1204+ @ NonNull Throwable ex ,
1205+ @ NonNull String currentSessionId ,
1206+ long eventTime ) {
11961207 ClsFileOutputStream fos = null ;
11971208 CodedOutputStream cos = null ;
11981209 try {
0 commit comments