11package com .blankj .utilcode .util ;
22
3+ import android .content .pm .PackageInfo ;
4+ import android .content .pm .PackageManager ;
5+ import android .os .Build ;
36import android .os .Environment ;
47import android .support .annotation .IntDef ;
58import android .support .annotation .IntRange ;
@@ -389,10 +392,7 @@ private static void print2File(final int type, final String tag, final String ms
389392 .append (msg )
390393 .append (LINE_SEP );
391394 final String content = sb .toString ();
392- if (sExecutor == null ) {
393- sExecutor = Executors .newSingleThreadExecutor ();
394- }
395- sExecutor .execute (new Runnable () {
395+ execute (new Runnable () {
396396 @ Override
397397 public void run () {
398398 BufferedWriter bw = null ;
@@ -421,13 +421,57 @@ private static boolean createOrExistsFile(final String filePath) {
421421 if (file .exists ()) return file .isFile ();
422422 if (!createOrExistsDir (file .getParentFile ())) return false ;
423423 try {
424- return file .createNewFile ();
424+ boolean r = file .createNewFile ();
425+ printDeviceInfo (filePath );
426+ return r ;
425427 } catch (IOException e ) {
426428 e .printStackTrace ();
427429 return false ;
428430 }
429431 }
430432
433+ private static void printDeviceInfo (final String filePath ) {
434+ String versionName = "" ;
435+ int versionCode = 0 ;
436+ try {
437+ PackageInfo pi = Utils .getApp ().getPackageManager ().getPackageInfo (Utils .getApp ().getPackageName (), 0 );
438+ if (pi != null ) {
439+ versionName = pi .versionName ;
440+ versionCode = pi .versionCode ;
441+ }
442+ } catch (PackageManager .NameNotFoundException e ) {
443+ e .printStackTrace ();
444+ }
445+ final String head = "\n ************* Log Head ****************" +
446+ "\n Device Manufacturer: " + Build .MANUFACTURER +// 设备厂商
447+ "\n Device Model : " + Build .MODEL +// 设备型号
448+ "\n Android Version : " + Build .VERSION .RELEASE +// 系统版本
449+ "\n Android SDK : " + Build .VERSION .SDK_INT +// SDK版本
450+ "\n App VersionName : " + versionName +
451+ "\n App VersionCode : " + versionCode +
452+ "\n ************* Log Head ****************\n \n " ;
453+ execute (new Runnable () {
454+ @ Override
455+ public void run () {
456+ BufferedWriter bw = null ;
457+ try {
458+ bw = new BufferedWriter (new FileWriter (filePath , true ));
459+ bw .write (head );
460+ } catch (IOException e ) {
461+ e .printStackTrace ();
462+ } finally {
463+ try {
464+ if (bw != null ) {
465+ bw .close ();
466+ }
467+ } catch (IOException e ) {
468+ e .printStackTrace ();
469+ }
470+ }
471+ }
472+ });
473+ }
474+
431475 private static boolean createOrExistsDir (final File file ) {
432476 return file != null && (file .exists () ? file .isDirectory () : file .mkdirs ());
433477 }
@@ -442,6 +486,13 @@ private static boolean isSpace(final String s) {
442486 return true ;
443487 }
444488
489+ private static void execute (Runnable runnable ) {
490+ if (sExecutor == null ) {
491+ sExecutor = Executors .newSingleThreadExecutor ();
492+ }
493+ sExecutor .execute (runnable );
494+ }
495+
445496 public static class Config {
446497 private Config () {
447498 if (sDefaultDir != null ) return ;
0 commit comments