|  |  |  | @ -3,6 +3,7 @@ package xyz.fycz.myreader.greendao.service; | 
			
		
	
		
			
				
					|  |  |  |  | import android.database.Cursor; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | import xyz.fycz.myreader.common.APPCONST; | 
			
		
	
		
			
				
					|  |  |  |  | import xyz.fycz.myreader.greendao.entity.BookMark; | 
			
		
	
		
			
				
					|  |  |  |  | import xyz.fycz.myreader.greendao.entity.Chapter; | 
			
		
	
		
			
				
					|  |  |  |  | import xyz.fycz.myreader.greendao.gen.ChapterDao; | 
			
		
	
		
			
				
					|  |  |  |  | import xyz.fycz.myreader.util.IOUtils; | 
			
		
	
	
		
			
				
					|  |  |  | @ -19,15 +20,16 @@ public class ChapterService extends BaseService { | 
			
		
	
		
			
				
					|  |  |  |  |     private static volatile ChapterService sInstance; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     public static ChapterService getInstance() { | 
			
		
	
		
			
				
					|  |  |  |  |         if (sInstance == null){ | 
			
		
	
		
			
				
					|  |  |  |  |             synchronized (ChapterService.class){ | 
			
		
	
		
			
				
					|  |  |  |  |                 if (sInstance == null){ | 
			
		
	
		
			
				
					|  |  |  |  |         if (sInstance == null) { | 
			
		
	
		
			
				
					|  |  |  |  |             synchronized (ChapterService.class) { | 
			
		
	
		
			
				
					|  |  |  |  |                 if (sInstance == null) { | 
			
		
	
		
			
				
					|  |  |  |  |                     sInstance = new ChapterService(); | 
			
		
	
		
			
				
					|  |  |  |  |                 } | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |         return sInstance; | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     private List<Chapter> findChapters(String sql, String[] selectionArgs) { | 
			
		
	
		
			
				
					|  |  |  |  |         ArrayList<Chapter> chapters = new ArrayList<>(); | 
			
		
	
		
			
				
					|  |  |  |  |         try { | 
			
		
	
	
		
			
				
					|  |  |  | @ -188,11 +190,11 @@ public class ChapterService extends BaseService { | 
			
		
	
		
			
				
					|  |  |  |  |             return; | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         File file = getBookFile(chapter.getBookId(), chapter.getTitle()); | 
			
		
	
		
			
				
					|  |  |  |  |         File file = getChapterFileExisted(chapter); | 
			
		
	
		
			
				
					|  |  |  |  |         BufferedWriter bw = null; | 
			
		
	
		
			
				
					|  |  |  |  |         try { | 
			
		
	
		
			
				
					|  |  |  |  |             bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))); | 
			
		
	
		
			
				
					|  |  |  |  |             bw.write(content.replace(chapter.getTitle(), "")); | 
			
		
	
		
			
				
					|  |  |  |  |             bw.write(content); | 
			
		
	
		
			
				
					|  |  |  |  |             bw.flush(); | 
			
		
	
		
			
				
					|  |  |  |  |         } catch (IOException e) { | 
			
		
	
		
			
				
					|  |  |  |  |             e.printStackTrace(); | 
			
		
	
	
		
			
				
					|  |  |  | @ -207,25 +209,25 @@ public class ChapterService extends BaseService { | 
			
		
	
		
			
				
					|  |  |  |  |      * @param chapter | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
		
			
				
					|  |  |  |  |     public void deleteChapterCacheFile(Chapter chapter) { | 
			
		
	
		
			
				
					|  |  |  |  |         File file = getBookFile(chapter.getBookId(), chapter.getTitle()); | 
			
		
	
		
			
				
					|  |  |  |  |         file.delete(); | 
			
		
	
		
			
				
					|  |  |  |  |         File file = getChapterFile(chapter); | 
			
		
	
		
			
				
					|  |  |  |  |         if (file.exists()) file.delete(); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |  |      * 获取缓存章节内容 | 
			
		
	
		
			
				
					|  |  |  |  |      * | 
			
		
	
		
			
				
					|  |  |  |  |      * @param chapter | 
			
		
	
		
			
				
					|  |  |  |  |      * @return | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
		
			
				
					|  |  |  |  |     public String getChapterCatheContent(Chapter chapter){ | 
			
		
	
		
			
				
					|  |  |  |  |         File file = new File(APPCONST.BOOK_CACHE_PATH + chapter.getBookId() | 
			
		
	
		
			
				
					|  |  |  |  |                 + File.separator + chapter.getTitle() + FileUtils.SUFFIX_FY); | 
			
		
	
		
			
				
					|  |  |  |  |     public String getChapterCatheContent(Chapter chapter) { | 
			
		
	
		
			
				
					|  |  |  |  |         File file = getChapterFile(chapter); | 
			
		
	
		
			
				
					|  |  |  |  |         if (!file.exists()) return null; | 
			
		
	
		
			
				
					|  |  |  |  |         BufferedReader br = null; | 
			
		
	
		
			
				
					|  |  |  |  |         try { | 
			
		
	
		
			
				
					|  |  |  |  |             br = new BufferedReader(new FileReader(file)); | 
			
		
	
		
			
				
					|  |  |  |  |             StringBuilder s = new StringBuilder(); | 
			
		
	
		
			
				
					|  |  |  |  |             String line = null; | 
			
		
	
		
			
				
					|  |  |  |  |             while ((line = br.readLine()) != null){ | 
			
		
	
		
			
				
					|  |  |  |  |             while ((line = br.readLine()) != null) { | 
			
		
	
		
			
				
					|  |  |  |  |                 s.append(line); | 
			
		
	
		
			
				
					|  |  |  |  |                 s.append("\n"); | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
	
		
			
				
					|  |  |  | @ -233,7 +235,7 @@ public class ChapterService extends BaseService { | 
			
		
	
		
			
				
					|  |  |  |  |         } catch (IOException e) { | 
			
		
	
		
			
				
					|  |  |  |  |             e.printStackTrace(); | 
			
		
	
		
			
				
					|  |  |  |  |             return null; | 
			
		
	
		
			
				
					|  |  |  |  |         }finally { | 
			
		
	
		
			
				
					|  |  |  |  |         } finally { | 
			
		
	
		
			
				
					|  |  |  |  |             IOUtils.close(br); | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
	
		
			
				
					|  |  |  | @ -278,13 +280,20 @@ public class ChapterService extends BaseService { | 
			
		
	
		
			
				
					|  |  |  |  |      * 根据文件名判断是否被缓存过 (因为可能数据库显示被缓存过,但是文件中却没有的情况,所以需要根据文件判断是否被缓存 | 
			
		
	
		
			
				
					|  |  |  |  |      * 过) | 
			
		
	
		
			
				
					|  |  |  |  |      * | 
			
		
	
		
			
				
					|  |  |  |  |      * @param folderName : bookId | 
			
		
	
		
			
				
					|  |  |  |  |      * @param fileName:  chapterName | 
			
		
	
		
			
				
					|  |  |  |  |      * @param chapter : chapter | 
			
		
	
		
			
				
					|  |  |  |  |      * @return | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
		
			
				
					|  |  |  |  |     public static boolean isChapterCached(String folderName, String fileName) { | 
			
		
	
		
			
				
					|  |  |  |  |         File file = new File(APPCONST.BOOK_CACHE_PATH + folderName | 
			
		
	
		
			
				
					|  |  |  |  |                 + File.separator + fileName + FileUtils.SUFFIX_FY); | 
			
		
	
		
			
				
					|  |  |  |  |     public static boolean isChapterCached(Chapter chapter) { | 
			
		
	
		
			
				
					|  |  |  |  |         File file = getChapterFile(chapter); | 
			
		
	
		
			
				
					|  |  |  |  |         return file.exists(); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     public static boolean isChapterCached(BookMark bookMark) { | 
			
		
	
		
			
				
					|  |  |  |  |         Chapter chapter = new Chapter(); | 
			
		
	
		
			
				
					|  |  |  |  |         chapter.setBookId(bookMark.getBookId()); | 
			
		
	
		
			
				
					|  |  |  |  |         chapter.setNumber(bookMark.getBookMarkChapterNum()); | 
			
		
	
		
			
				
					|  |  |  |  |         chapter.setTitle(bookMark.getTitle()); | 
			
		
	
		
			
				
					|  |  |  |  |         File file = getChapterFile(chapter); | 
			
		
	
		
			
				
					|  |  |  |  |         return file.exists(); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
	
		
			
				
					|  |  |  | @ -326,4 +335,24 @@ public class ChapterService extends BaseService { | 
			
		
	
		
			
				
					|  |  |  |  |                 + File.separator + fileName + FileUtils.SUFFIX_FY); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     public static File getChapterFile(Chapter chapter) { | 
			
		
	
		
			
				
					|  |  |  |  |         File file = new File(APPCONST.BOOK_CACHE_PATH + chapter.getBookId() | 
			
		
	
		
			
				
					|  |  |  |  |                 + File.separator + chapter.getNumber() + "、" + chapter.getTitle() + FileUtils.SUFFIX_FY); | 
			
		
	
		
			
				
					|  |  |  |  |         if (!file.exists()) { | 
			
		
	
		
			
				
					|  |  |  |  |             file = new File(APPCONST.BOOK_CACHE_PATH + chapter.getBookId() | 
			
		
	
		
			
				
					|  |  |  |  |                     + File.separator + chapter.getTitle() + FileUtils.SUFFIX_FY); | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |         return file; | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     public static File getChapterFileExisted(Chapter chapter) { | 
			
		
	
		
			
				
					|  |  |  |  |         File file = new File(APPCONST.BOOK_CACHE_PATH + chapter.getBookId() | 
			
		
	
		
			
				
					|  |  |  |  |                 + File.separator + chapter.getTitle() + FileUtils.SUFFIX_FY); | 
			
		
	
		
			
				
					|  |  |  |  |         if (!file.exists()) { | 
			
		
	
		
			
				
					|  |  |  |  |             file = FileUtils.getFile(APPCONST.BOOK_CACHE_PATH + chapter.getBookId() | 
			
		
	
		
			
				
					|  |  |  |  |                     + File.separator + chapter.getNumber() + "、" + chapter.getTitle() + FileUtils.SUFFIX_FY); | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |         return file; | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | } | 
			
		
	
	
		
			
				
					|  |  |  | 
 |