7
7
import android .os .Environment ;
8
8
import android .support .annotation .NonNull ;
9
9
import android .support .annotation .RequiresPermission ;
10
+ import android .util .Log ;
10
11
11
12
import java .io .BufferedWriter ;
12
13
import java .io .File ;
18
19
import java .text .Format ;
19
20
import java .text .SimpleDateFormat ;
20
21
import java .util .Date ;
21
- import java .util .concurrent .ExecutorService ;
22
+ import java .util .concurrent .Callable ;
23
+ import java .util .concurrent .ExecutionException ;
22
24
import java .util .concurrent .Executors ;
25
+ import java .util .concurrent .Future ;
23
26
24
27
import static android .Manifest .permission .WRITE_EXTERNAL_STORAGE ;
25
28
@@ -38,8 +41,6 @@ public final class CrashUtils {
38
41
private static String versionName ;
39
42
private static int versionCode ;
40
43
41
- private static ExecutorService sExecutor ;
42
-
43
44
private static final String FILE_SEP = System .getProperty ("file.separator" );
44
45
@ SuppressLint ("SimpleDateFormat" )
45
46
private static final Format FORMAT = new SimpleDateFormat ("MM-dd HH-mm-ss" );
@@ -105,30 +106,11 @@ public void uncaughtException(final Thread t, final Throwable e) {
105
106
Date now = new Date (System .currentTimeMillis ());
106
107
String fileName = FORMAT .format (now ) + ".txt" ;
107
108
final String fullPath = (dir == null ? defaultDir : dir ) + fileName ;
108
- if (!createOrExistsFile (fullPath )) return ;
109
- if (sExecutor == null ) {
110
- sExecutor = Executors .newSingleThreadExecutor ();
109
+ if (createOrExistsFile (fullPath )) {
110
+ input2File (crashInfo , fullPath );
111
+ } else {
112
+ Log .e ("CrashUtils" , "create " + fullPath + " failed!" );
111
113
}
112
- sExecutor .execute (new Runnable () {
113
- @ Override
114
- public void run () {
115
- BufferedWriter bw = null ;
116
- try {
117
- bw = new BufferedWriter (new FileWriter (fullPath , false ));
118
- bw .write (crashInfo );
119
- } catch (IOException e ) {
120
- e .printStackTrace ();
121
- } finally {
122
- if (bw != null ) {
123
- try {
124
- bw .close ();
125
- } catch (IOException e1 ) {
126
- e1 .printStackTrace ();
127
- }
128
- }
129
- }
130
- }
131
- });
132
114
133
115
if (sOnCrashListener != null ) {
134
116
sOnCrashListener .onCrash (crashInfo , e );
@@ -229,6 +211,39 @@ public static void init(final String crashDirPath, final OnCrashListener onCrash
229
211
Thread .setDefaultUncaughtExceptionHandler (UNCAUGHT_EXCEPTION_HANDLER );
230
212
}
231
213
214
+ private static void input2File (final String input , final String filePath ) {
215
+ Future <Boolean > submit = Executors .newSingleThreadExecutor ().submit (new Callable <Boolean >() {
216
+ @ Override
217
+ public Boolean call () throws Exception {
218
+ BufferedWriter bw = null ;
219
+ try {
220
+ bw = new BufferedWriter (new FileWriter (filePath , true ));
221
+ bw .write (input );
222
+ return true ;
223
+ } catch (IOException e ) {
224
+ e .printStackTrace ();
225
+ return false ;
226
+ } finally {
227
+ try {
228
+ if (bw != null ) {
229
+ bw .close ();
230
+ }
231
+ } catch (IOException e ) {
232
+ e .printStackTrace ();
233
+ }
234
+ }
235
+ }
236
+ });
237
+ try {
238
+ if (submit .get ()) return ;
239
+ } catch (InterruptedException e ) {
240
+ e .printStackTrace ();
241
+ } catch (ExecutionException e ) {
242
+ e .printStackTrace ();
243
+ }
244
+ Log .e ("CrashUtils" , "write crash info to " + filePath + " failed!" );
245
+ }
246
+
232
247
private static boolean createOrExistsFile (final String filePath ) {
233
248
File file = new File (filePath );
234
249
if (file .exists ()) return file .isFile ();
0 commit comments