每一个章节一个缓存文件

This commit is contained in:
mwang 2019-03-06 22:58:59 +08:00
parent 0249ac4119
commit f1f1c6ecc8
7 changed files with 393 additions and 73 deletions

View File

@ -140,7 +140,7 @@ public abstract class BasicFragment extends Fragment {
Book book1 = (Book)LitePal.find(Book.class,book.getId());
book.setBegin(book1.getBegin());
book.setBiginChapt(book1.getBiginChapt());
Toast.makeText(activity, book.getBookname() + "加载", Toast.LENGTH_SHORT).show();
final String path = book.getBookpath();

View File

@ -165,16 +165,17 @@ public class ShelfAdapter extends BaseAdapter implements DragGridListener {
* @param bookLists
*/
public void updateBookPosition (int position,int databaseId,List<Book> bookLists) {
Book bookList = new Book();
Book book = new Book();
String bookpath = bookLists.get(position).getBookpath();
String bookname = bookLists.get(position).getBookname();
bookList.setBookpath(bookpath);
bookList.setBookname(bookname);
bookList.setBegin(bookLists.get(position).getBegin());
bookList.setCharset(bookLists.get(position).getCharset());
book.setBookpath(bookpath);
book.setBookname(bookname);
book.setBegin(bookLists.get(position).getBegin());
book.setBiginChapt(bookLists.get(position).getBiginChapt());
book.setCharset(bookLists.get(position).getCharset());
//开线程保存改动的数据到数据库
//使用litepal数据库框架update时每次只能update一个id中的一条信息如果相同则不更新
upDateBookToSqlite3(databaseId , bookList);
upDateBookToSqlite3(databaseId , book);
}
/**

View File

@ -8,15 +8,25 @@ import java.io.Serializable;
public class Book extends LitePalSupport implements Serializable{
private int id;
private int bookId;
private String bookname;
private String bookpath;
private long begin;
private long biginChapt=1;
private String charset;
private String cate;
private String author;
private String lastChapter;
private String desc;
private long lastUpdate;
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getCate() {
return cate;
}
@ -40,9 +50,6 @@ public class Book extends LitePalSupport implements Serializable{
public void setLastUpdate(long lastUpdate) {
this.lastUpdate = lastUpdate;
}
private long lastUpdate;
public String getLastChapter() {
return lastChapter;
}
@ -98,4 +105,14 @@ public class Book extends LitePalSupport implements Serializable{
public void setDesc(String desc) {
this.desc = desc;
}
public long getBiginChapt() {
return biginChapt;
}
public void setBiginChapt(long biginChapt) {
this.biginChapt = biginChapt;
}
}

View File

@ -6,15 +6,41 @@ import org.litepal.crud.LitePalSupport;
public class BookChapter extends LitePalSupport {
private int id;
private int bookId;
private String bookpath;
private String chapterName;
private long bookChapterStartPos;
private String chapterUrl;
private int length;
private String chapterPath;
public String getChapterPath() {
return chapterPath;
}
public void setChapterPath(String chapterPath) {
this.chapterPath = chapterPath;
}
public String getChapterUrl() {
return chapterUrl;
}
public void setChapterUrl(String chapterUrl) {
this.chapterUrl = chapterUrl;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getId() {
return id;
}
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public void setId(int id) {
this.id = id;
}

View File

@ -22,14 +22,18 @@ import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BookUtil {
public static final String TAG ="BookUtil";
private static final String cachedPath = Environment.getExternalStorageDirectory() + "/zhuike/";
private static final String cachedPath = Environment.getExternalStorageDirectory() + "/zhuike/cache/";
private static final String chapterPath = Environment.getExternalStorageDirectory() + "/zhuike/chapter/";
private static final String charachterType = "utf-8";//"UTF-16LE";
//存储的字符数
public static final int cachedSize = 30000;
public static final int cachedSize = 300000;
// protected final ArrayList<WeakReference<char[]>> myArray = new ArrayList<>();
public static final String lineBreakChar ="\n";
@ -40,9 +44,25 @@ public class BookUtil {
private String m_strCharsetName;
private String bookName;
private String bookPath;
public void setBookLen(long bookLen) {
this.bookLen = bookLen;
}
private long bookLen;
private long position;
private Book bookList;
private Book book;
public void setChapterNo(int chapterNo) {
this.chapterNo = chapterNo;
}
public int getChapterNo() {
return chapterNo;
}
private int chapterNo;//当前章节
public String getLineBreakChar(){
return "\n";
@ -53,24 +73,38 @@ public class BookUtil {
if (!file.exists()){
file.mkdir();
}
File file2 = new File(chapterPath);
if (!file2.exists()){
file2.mkdir();
}
}
public synchronized void openBook(Book bookList) throws IOException {
this.bookList = bookList;
public synchronized void openBook(Book book) throws IOException {
this.book = book;
//如果当前缓存不是要打开的书本就缓存书本同时删除缓存
//TODO 构建新的缓存策略几个选项1每本书一个缓存 2控制缓存总大小超过限制删除旧缓存 3网络小说的缓存
if (bookPath == null || !bookPath.equals(bookList.getBookpath())) {
cleanCacheFile();
this.bookPath = bookList.getBookpath();
bookName = FileUtils.getFileName(bookPath);
cacheBook();
directoryList = LitePal.where("bookId=?",book.getId()+"").find(BookChapter.class);
for(BookChapter c :directoryList){
Log.d(TAG, String.format("bookchapter :%s,fileName :%s, chapter Size %s",c.getChapterName(),c.getChapterPath(),c.getLength()));
}
if(directoryList.isEmpty()) {
if (bookPath == null || !bookPath.equals(book.getBookpath())) {
cleanCacheFile();
this.bookPath = book.getBookpath();
bookName = FileUtils.getFileName(bookPath);
cacheBook();
}
}
}
private void cleanCacheFile(){
File file = new File(cachedPath);
File file = new File(cachedPath );
if (!file.exists()){
file.mkdir();
}else{
@ -79,6 +113,19 @@ public class BookUtil {
files[i].delete();
}
}
file = new File(getChapterPath());
if (!file.exists()){
file.mkdir();
}else{
File[] files = file.listFiles();
for (int i = 0; i < files.length;i++){
files[i].delete();
}
}
}
public int next(boolean back){
@ -87,7 +134,7 @@ public class BookUtil {
position = bookLen;
return -1;
}
char result = current();
char result = chaptCurrent(); //current();
if (back) {
position -= 1;
}
@ -135,7 +182,11 @@ public class BookUtil {
return line.toCharArray();
}
public char chaptCurrent(){
char[] charArray = chaptChars(chapterNo);
return charArray[(int)position-1];
}
public char current(){
// int pos = (int) (position % cachedSize);
// int cachePos = (int) (position / cachedSize);
@ -179,16 +230,16 @@ public class BookUtil {
//缓存书本
private void cacheBook() throws IOException {
if (TextUtils.isEmpty(bookList.getCharset())) {
if (TextUtils.isEmpty(book.getCharset())) {
m_strCharsetName = FileUtils.getCharset(bookPath);
if (m_strCharsetName == null) {
m_strCharsetName = "utf-8";
}
ContentValues values = new ContentValues();
values.put("charset",m_strCharsetName);
LitePal.update(Book.class,values,bookList.getId());
LitePal.update(Book.class,values,book.getId());
}else{
m_strCharsetName = bookList.getCharset();
m_strCharsetName = book.getCharset();
}
File file = new File(bookPath);
@ -225,8 +276,9 @@ public class BookUtil {
myArray.add(cache);
// myArray.add(new WeakReference<char[]>(buf));
// myArray.set(index,);
// Log.e(TAG,String.format("缓存的内容写入文件\n %s",fileName(index)));
// Log.e(TAG,"---------------------------------------------------------------------------------------------------------");
Log.e(TAG,String.format("缓存的内容写入文件\n %s",fileName(index)));
Log.e(TAG,"---------------------------------------------------------------------------------------------------------");
try {
File cacheBook = new File(fileName(index));
if (!cacheBook.exists()){
@ -239,35 +291,91 @@ public class BookUtil {
throw new RuntimeException("Error during writing " + fileName(index));
}
index ++;
}
new Thread(){
}
getChapter();
/*new Thread(){
@Override
public void run() {
getChapter();
getChapter();
}
}.start();
*/
}
//获取章节
public synchronized void getChapter(){
try {
long size = 0;
String title ="";
long start =0;
int chaptFileId=0;
int chaptId =0;
BookChapter bookChapter = null;
OutputStreamWriter writer = null;
for (int i = 0; i < myArray.size(); i++) {
char[] buf = block(i);
String bufStr = new String(buf);
String[] paragraphs = bufStr.split(lineBreakChar); // String[] paragraphs = bufStr.split("\r\n");
for (String str : paragraphs) {
// if (str.length() <= 30 && (str.matches(".*第.{1,8}章.*") || str.matches(".*第.{1,8}节.*"))) {
if(isChapterTitle(str)) {
BookChapter bookCatalogue = new BookChapter();
bookCatalogue.setBookChapterStartPos(size);
bookCatalogue.setChapterName(str.replaceAll("###",""));
bookCatalogue.setBookpath(bookPath);
directoryList.add(bookCatalogue);
if(title.length()==0) {
title = str;
start =0;
}else {
start = size;
title = str;
}
if(bookChapter!=null) {
bookChapter.setLength((int)(size - start));
bookChapter.setChapterPath(fileChapterName(chaptId) );
bookChapter.update(bookChapter.getId());
directoryList.add(bookChapter);
}
bookChapter = new BookChapter();
bookChapter.setBookId(book.getId());
bookChapter.setBookChapterStartPos(start);
bookChapter.setChapterName(str.replaceAll("###",""));
bookChapter.setBookpath(bookPath);
bookChapter.save();
int id= bookChapter.getId();
Log.d(TAG,str + " chaptId is " + id);
File chapter = new File(fileChapterName(++chaptId));
if (!chapter.exists()){
chapter.createNewFile();
}
if(writer!=null) {
writer.close();
}
writer = new OutputStreamWriter(new FileOutputStream(fileChapterName(chaptId)), charachterType);
}
if(writer==null) {
bookChapter = new BookChapter();
bookChapter.setBookId(book.getId());
bookChapter.setBookChapterStartPos(start);
bookChapter.setChapterName(str.replaceAll("###",""));
bookChapter.setBookpath(bookPath);
bookChapter.save();
writer = new OutputStreamWriter(new FileOutputStream(fileChapterName(++chaptId)), charachterType); //
}
str+=lineBreakChar;
writer.write(str);
// Log.e(TAG,String.format("当前行\n %s",str));
if (str.contains("\u3000\u3000")) {
size += str.length() + 2;
@ -276,13 +384,40 @@ public class BookUtil {
}else {
size += str.length();
}
/*
BookChapter bookChapter = new BookChapter();
bookChapter.setBookId(book.getId());
bookChapter.setBookChapterStartPos(start);
bookChapter.setChapterName(title.replaceAll("###",""));
bookChapter.setBookpath(bookPath);
bookChapter.setLength((int)(size - start));
bookChapter.save();
int id= bookChapter.getId();
Log.d(TAG,str + " chaptId is " + id);
directoryList.add(bookChapter);
*/
}
}
if(writer!=null) {
writer.close();
}
if(bookChapter!=null) {
bookChapter.setLength((int)(size - start));
bookChapter.setChapterPath(fileChapterName(chaptId) );
bookChapter.update(bookChapter.getId());
directoryList.add(bookChapter);
}
}catch (Exception e){
e.printStackTrace();
}
}
void createChapContent(){
}
public List<BookChapter> getDirectoryList(){
return directoryList;
}
@ -294,6 +429,14 @@ public class BookUtil {
protected String fileName(int index) {
return cachedPath + bookName + index ;
}
protected String fileChapterName(int chaptId ) {
return getChapterPath() + chaptId ;
}
String getChapterPath(){
return chapterPath +book.getId()+"/";
}
//获取书本缓存
public char[] block(int index) {
@ -327,6 +470,45 @@ public class BookUtil {
}
return block;
}
Map<Integer,Cache> chaptCache = new HashMap<Integer,Cache>();
//获取chapter 缓存
public char[] chaptChars(int index) {
char[] block=null;
if(chaptCache.containsKey(Integer.valueOf(index))) {
block = chaptCache .get(index).getData().get();
}
if (block == null) {
try {
File file = new File(fileChapterName(index));
int size = (int)file.length();
if (size < 0) {
throw new RuntimeException("Error during reading " + fileChapterName(index));
}
block = new char[size / 2];
InputStreamReader reader =
new InputStreamReader(
new FileInputStream(file),
charachterType
);
long l = reader.read(block);
if (reader.read(block) != block.length) {
// throw new RuntimeException("Error during reading " + fileChapterName(index));
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Error during reading " + fileChapterName(index));
}
Cache cache = new Cache();
cache.setSize(block.length);
cache.setData(new WeakReference<char[]>(block));
chaptCache.put(index, cache);
// myArray.set(index, new WeakReference<char[]>(block));
}
return block;
}
public boolean isChapterTitle(String line){
return (line.length() <= 30 && (line.matches(".*第.{1,8}章.*") || line.matches(".*第.{1,8}节.*"))) ;
}

View File

@ -136,7 +136,7 @@ public class PageFactory {
private String bookName = "";
private Book bookList;
//书本章节
private int currentCharter = 0;
private int currentChapter = 0;
//当前电量
private int level = 0;
private BookUtil mBookUtil;
@ -147,6 +147,43 @@ public class PageFactory {
private BookTask bookTask;
ContentValues values = new ContentValues();
private List<TRPage> currentChaptPages;
private List<TRPage> nextChaptPages;
private List<TRPage> preChaptPages;
private List<TRPage> loadCurrentChapt(int chaptId){
List<TRPage> chaptPages = new ArrayList<TRPage>();
char[] chars = mBookUtil.chaptChars(chaptId);
mBookUtil.setBookLen(chars.length);
mBookUtil.setChapterNo(chaptId);
// TRPage page = new TRPage();
long length =0;
int pageNo =0;
while(length <chars.length ) {
pageNo++;
TRPage page = getNextChapterPage(length);
page.setPageNo(pageNo);
chaptPages.add(page);
length= page.getEnd();
}
return chaptPages;
}
public TRPage getNextChapterPage(long position){
mBookUtil.setPostition(position);
TRPage trPage = new TRPage();
trPage.setBegin(position +1);
Log.e(TAG,"page postion next begin:" + (position + 1) + "");
trPage.setLines(getNextLines());
Log.e(TAG,"page postion next end:" +mBookUtil.getPosition() + "");
trPage.setEnd(mBookUtil.getPosition());
return trPage;
}
private static Status mStatus = Status.OPENING;
public enum Status {
@ -286,9 +323,9 @@ public class PageFactory {
mBookPageWidget.postInvalidate();
}
public void onDraw(Bitmap bitmap,List<String> m_lines,Boolean updateCharter) {
if (getDirectoryList().size() > 0 && updateCharter) {
currentCharter = getCurrentCharter();
public void onDraw(Bitmap bitmap,List<String> m_lines,Boolean updateChapter) {
if (getDirectoryList().size() > 0 && updateChapter) {
currentChapter = getCurrentCharter();
}
//更新数据库进度
if (currentPage != null && bookList != null){
@ -297,6 +334,7 @@ public class PageFactory {
public void run() {
super.run();
values.put("begin",currentPage.getBegin());
values.put("biginChapt",currentChapter);
int rows = LitePal.update(Book.class,values,bookList.getId());
Log.e(TAG,String.format("update book %s bigin %s, result %s",bookList.getBookname(),currentPage.getBegin(),rows) );
}
@ -366,7 +404,7 @@ public class PageFactory {
c.drawText(CommonUtil.subString(bookName,12), marginWidth ,statusMarginBottom + mBatterryFontSize, mBatterryPaint);
//画章
if (getDirectoryList().size() > 0) {
String charterName = CommonUtil.subString(getDirectoryList().get(currentCharter).getChapterName(),12);
String charterName = CommonUtil.subString(getDirectoryList().get(currentChapter-1).getChapterName(),12);
int nChaterWidth = (int) mBatterryPaint.measureText(charterName) + 1;
c.drawText(charterName, mWidth - marginWidth - nChaterWidth, statusMarginBottom + mBatterryFontSize, mBatterryPaint);
}
@ -377,14 +415,12 @@ public class PageFactory {
//向前翻页
public void prePage(){
if (currentPage.getBegin() <= 0) {
Log.e(TAG,"当前是第一页");
if (!m_isfirstPage){
Log.e(TAG,"当前是本章第一页");
m_isfirstPage =currentChapter ==1;
if ( m_isfirstPage){
Toast.makeText(mContext, "当前是第一页", Toast.LENGTH_SHORT).show();
return;
}
m_isfirstPage = true;
return;
} else {
m_isfirstPage = false;
}
cancelPage = currentPage;
@ -392,30 +428,27 @@ public class PageFactory {
currentPage = getPrePage();
onDraw(mBookPageWidget.getNextPage(),currentPage.getLines(),true);
}
//向后翻页
public void nextPage(){
if (currentPage.getEnd() >= mBookUtil.getBookLen()) {
Log.e(TAG,"已经是最后一页了");
if (!m_islastPage){
Log.e(TAG,"已经是本章最后一页了");
m_islastPage =currentChapter == mBookUtil.getDirectoryList().size();
if ( m_islastPage){
Toast.makeText(mContext, "已经是最后一页了", Toast.LENGTH_SHORT).show();
return;
} else {
}
m_islastPage = true;
return;
} else {
m_islastPage = false;
}
cancelPage = currentPage;
onDraw(mBookPageWidget.getCurPage(),currentPage.getLines(),true);
prePage = currentPage;
currentPage = getNextPage();
currentPage = getNextPage();
// currentPage = currentChaptPages.get(currentPage.getPageNo()-1);
onDraw(mBookPageWidget.getNextPage(),currentPage.getLines(),true);
Log.e("nextPage","nextPagenext");
}
@ -429,13 +462,13 @@ public class PageFactory {
* 打开书本
* @throws IOException
*/
public void openBook(Book bookList) throws IOException {
public void openBook(Book book ) throws IOException {
//清空数据
currentCharter = 0;
currentChapter = 0;
// m_mbBufLen = 0;
initBg(config.getDayOrNight());
this.bookList = bookList;
this.bookList = book ;
bookPath = bookList.getBookpath();
bookName = FileUtils.getFileName(bookPath);
@ -446,10 +479,12 @@ public class PageFactory {
bookTask.cancel(true);
}
bookTask = new BookTask();
bookTask.execute(bookList.getBegin());
bookTask.execute(book.getBiginChapt(),book .getBegin());
}
private class BookTask extends AsyncTask<Long,Void,Boolean>{
private long chapter=0;
private long begin = 0;
@Override
protected void onPostExecute(Boolean result) {
@ -461,7 +496,10 @@ public class PageFactory {
if (result) {
PageFactory.mStatus = PageFactory.Status.FINISH;
// m_mbBufLen = mBookUtil.getBookLen();
currentPage = getPageForBegin(begin);
mBookUtil.setChapterNo((int)chapter);
currentChaptPages = loadCurrentChapt((int)chapter);
currentPage = currentChaptPages.get(0);
// currentPage = getPageForBegin(begin);
if (mBookPageWidget != null) {
currentPage(true);
}
@ -486,7 +524,9 @@ public class PageFactory {
@Override
protected Boolean doInBackground(Long... params) {
begin = params[0];
chapter = params[0];
begin = params[1];
currentChapter = (int) chapter;
try {
mBookUtil.openBook(bookList);
} catch (Exception e) {
@ -499,6 +539,7 @@ public class PageFactory {
}
public TRPage getNextPage(){
/*
mBookUtil.setPostition(currentPage.getEnd());
TRPage trPage = new TRPage();
@ -508,9 +549,20 @@ public class PageFactory {
Log.e(TAG,"page postion next end:" +mBookUtil.getPosition() + "");
trPage.setEnd(mBookUtil.getPosition());
return trPage;
*/
int nextPageNo =currentPage.getPageNo();
if(nextPageNo == currentChaptPages.size()){
preChaptPages =currentChaptPages;
currentChapter++;
mBookUtil.setChapterNo(currentChapter);
currentChaptPages = loadCurrentChapt(currentChapter ) ;
nextPageNo =0;
}
return currentChaptPages.get(nextPageNo);
}
public TRPage getPrePage(){
/*
mBookUtil.setPostition(currentPage.getBegin());
TRPage trPage = new TRPage();
@ -520,9 +572,24 @@ public class PageFactory {
Log.e(TAG,"page postion pre begin:" +mBookUtil.getPosition() + "");
trPage.setBegin(mBookUtil.getPosition() );
return trPage;
*/
int prePageNo =currentPage.getPageNo()-1;
if(prePageNo ==0){
nextChaptPages =currentChaptPages;
currentChapter--;
if(currentChapter ==0) {
return new TRPage();
}
mBookUtil.setChapterNo(currentChapter);
currentChaptPages = loadCurrentChapt(currentChapter ) ;
prePageNo = currentChaptPages.size();
}
return currentChaptPages.get(prePageNo-1);
}
public TRPage getPageForBegin(long begin){
/*
TRPage trPage = new TRPage();
trPage.setBegin(begin);
@ -530,6 +597,15 @@ public class PageFactory {
trPage.setLines(getNextLines());
trPage.setEnd(mBookUtil.getPosition());
return trPage;
*/
for(TRPage page : currentChaptPages)
{
if(page.getEnd() >begin){
return page;
}
}
return new TRPage();
}
boolean showChapTitleOnTopWhenNextPage =false;
@ -679,7 +755,7 @@ public class PageFactory {
//上一章
public void preChapter(){
if (mBookUtil.getDirectoryList().size() > 0){
int num = currentCharter;
int num = currentChapter;
if (num ==0){
num =getCurrentCharter();
}
@ -688,14 +764,14 @@ public class PageFactory {
long begin = mBookUtil.getDirectoryList().get(num).getBookChapterStartPos();
currentPage = getPageForBegin(begin);
currentPage(true);
currentCharter = num;
currentChapter = num;
}
}
}
//下一章
public void nextChapter(){
int num = currentCharter;
int num = currentChapter;
if (num == 0){
num =getCurrentCharter();
}
@ -704,13 +780,13 @@ public class PageFactory {
long begin = getDirectoryList().get(num).getBookChapterStartPos();
currentPage = getPageForBegin(begin);
currentPage(true);
currentCharter = num;
currentChapter = num;
}
}
//获取现在的章
public int getCurrentCharter(){
int num = 0;
/*int num = 0;
for (int i = 0;getDirectoryList().size() > i;i++){
BookChapter bookCatalogue = getDirectoryList().get(i);
if (currentPage.getEnd() >= bookCatalogue.getBookChapterStartPos()){
@ -720,6 +796,9 @@ public class PageFactory {
}
}
return num;
*/
currentChapter = mBookUtil.getChapterNo();
return currentChapter ;
}
//绘制当前页面
@ -846,7 +925,7 @@ public class PageFactory {
}
public void clear(){
currentCharter = 0;
currentChapter = 0;
bookPath = "";
bookName = "";
bookList = null;
@ -855,6 +934,10 @@ public class PageFactory {
cancelPage = null;
prePage = null;
currentPage = null;
currentChaptPages =null;
preChaptPages=null;
nextChaptPages=null;
}
public static Status getStatus(){

View File

@ -1,12 +1,23 @@
package com.deiniu.zhuike.utils;
import java.util.ArrayList;
import java.util.List;
public class TRPage {
private long begin;
private long end;
private List<String> lines;
private List<String> lines = new ArrayList<>();
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
private int pageNo;
public long getBegin() {
return begin;