每一个章节一个缓存文件
This commit is contained in:
parent
0249ac4119
commit
f1f1c6ecc8
|
@ -140,7 +140,7 @@ public abstract class BasicFragment extends Fragment {
|
||||||
|
|
||||||
Book book1 = (Book)LitePal.find(Book.class,book.getId());
|
Book book1 = (Book)LitePal.find(Book.class,book.getId());
|
||||||
book.setBegin(book1.getBegin());
|
book.setBegin(book1.getBegin());
|
||||||
|
book.setBiginChapt(book1.getBiginChapt());
|
||||||
Toast.makeText(activity, book.getBookname() + "加载", Toast.LENGTH_SHORT).show();
|
Toast.makeText(activity, book.getBookname() + "加载", Toast.LENGTH_SHORT).show();
|
||||||
|
|
||||||
final String path = book.getBookpath();
|
final String path = book.getBookpath();
|
||||||
|
|
|
@ -165,16 +165,17 @@ public class ShelfAdapter extends BaseAdapter implements DragGridListener {
|
||||||
* @param bookLists
|
* @param bookLists
|
||||||
*/
|
*/
|
||||||
public void updateBookPosition (int position,int databaseId,List<Book> 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 bookpath = bookLists.get(position).getBookpath();
|
||||||
String bookname = bookLists.get(position).getBookname();
|
String bookname = bookLists.get(position).getBookname();
|
||||||
bookList.setBookpath(bookpath);
|
book.setBookpath(bookpath);
|
||||||
bookList.setBookname(bookname);
|
book.setBookname(bookname);
|
||||||
bookList.setBegin(bookLists.get(position).getBegin());
|
book.setBegin(bookLists.get(position).getBegin());
|
||||||
bookList.setCharset(bookLists.get(position).getCharset());
|
book.setBiginChapt(bookLists.get(position).getBiginChapt());
|
||||||
|
book.setCharset(bookLists.get(position).getCharset());
|
||||||
//开线程保存改动的数据到数据库
|
//开线程保存改动的数据到数据库
|
||||||
//使用litepal数据库框架update时每次只能update一个id中的一条信息,如果相同则不更新。
|
//使用litepal数据库框架update时每次只能update一个id中的一条信息,如果相同则不更新。
|
||||||
upDateBookToSqlite3(databaseId , bookList);
|
upDateBookToSqlite3(databaseId , book);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -8,15 +8,25 @@ import java.io.Serializable;
|
||||||
|
|
||||||
public class Book extends LitePalSupport implements Serializable{
|
public class Book extends LitePalSupport implements Serializable{
|
||||||
private int id;
|
private int id;
|
||||||
|
private int bookId;
|
||||||
private String bookname;
|
private String bookname;
|
||||||
private String bookpath;
|
private String bookpath;
|
||||||
private long begin;
|
private long begin;
|
||||||
|
private long biginChapt=1;
|
||||||
private String charset;
|
private String charset;
|
||||||
private String cate;
|
private String cate;
|
||||||
private String author;
|
private String author;
|
||||||
private String lastChapter;
|
private String lastChapter;
|
||||||
private String desc;
|
private String desc;
|
||||||
|
private long lastUpdate;
|
||||||
|
|
||||||
|
public int getBookId() {
|
||||||
|
return bookId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBookId(int bookId) {
|
||||||
|
this.bookId = bookId;
|
||||||
|
}
|
||||||
public String getCate() {
|
public String getCate() {
|
||||||
return cate;
|
return cate;
|
||||||
}
|
}
|
||||||
|
@ -40,9 +50,6 @@ public class Book extends LitePalSupport implements Serializable{
|
||||||
public void setLastUpdate(long lastUpdate) {
|
public void setLastUpdate(long lastUpdate) {
|
||||||
this.lastUpdate = lastUpdate;
|
this.lastUpdate = lastUpdate;
|
||||||
}
|
}
|
||||||
|
|
||||||
private long lastUpdate;
|
|
||||||
|
|
||||||
public String getLastChapter() {
|
public String getLastChapter() {
|
||||||
return lastChapter;
|
return lastChapter;
|
||||||
}
|
}
|
||||||
|
@ -98,4 +105,14 @@ public class Book extends LitePalSupport implements Serializable{
|
||||||
public void setDesc(String desc) {
|
public void setDesc(String desc) {
|
||||||
this.desc = desc;
|
this.desc = desc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long getBiginChapt() {
|
||||||
|
return biginChapt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBiginChapt(long biginChapt) {
|
||||||
|
this.biginChapt = biginChapt;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,15 +6,41 @@ import org.litepal.crud.LitePalSupport;
|
||||||
|
|
||||||
public class BookChapter extends LitePalSupport {
|
public class BookChapter extends LitePalSupport {
|
||||||
private int id;
|
private int id;
|
||||||
|
private int bookId;
|
||||||
private String bookpath;
|
private String bookpath;
|
||||||
private String chapterName;
|
private String chapterName;
|
||||||
private long bookChapterStartPos;
|
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() {
|
public int getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
public int getBookId() {
|
||||||
|
return bookId;
|
||||||
|
}
|
||||||
|
public void setBookId(int bookId) {
|
||||||
|
this.bookId = bookId;
|
||||||
|
}
|
||||||
public void setId(int id) {
|
public void setId(int id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,14 +22,18 @@ import java.io.InputStreamReader;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
public class BookUtil {
|
public class BookUtil {
|
||||||
public static final String TAG ="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<>();
|
// protected final ArrayList<WeakReference<char[]>> myArray = new ArrayList<>();
|
||||||
|
|
||||||
public static final String lineBreakChar ="\n";
|
public static final String lineBreakChar ="\n";
|
||||||
|
@ -40,9 +44,25 @@ public class BookUtil {
|
||||||
private String m_strCharsetName;
|
private String m_strCharsetName;
|
||||||
private String bookName;
|
private String bookName;
|
||||||
private String bookPath;
|
private String bookPath;
|
||||||
|
|
||||||
|
public void setBookLen(long bookLen) {
|
||||||
|
this.bookLen = bookLen;
|
||||||
|
}
|
||||||
|
|
||||||
private long bookLen;
|
private long bookLen;
|
||||||
private long position;
|
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(){
|
public String getLineBreakChar(){
|
||||||
return "\n";
|
return "\n";
|
||||||
|
@ -53,24 +73,38 @@ public class BookUtil {
|
||||||
if (!file.exists()){
|
if (!file.exists()){
|
||||||
file.mkdir();
|
file.mkdir();
|
||||||
}
|
}
|
||||||
|
File file2 = new File(chapterPath);
|
||||||
|
if (!file2.exists()){
|
||||||
|
file2.mkdir();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void openBook(Book bookList) throws IOException {
|
public synchronized void openBook(Book book) throws IOException {
|
||||||
this.bookList = bookList;
|
this.book = book;
|
||||||
//如果当前缓存不是要打开的书本就缓存书本同时删除缓存
|
//如果当前缓存不是要打开的书本就缓存书本同时删除缓存
|
||||||
|
|
||||||
//TODO 构建新的缓存策略,几个选项,1:每本书一个缓存 2:控制缓存总大小,超过限制删除旧缓存 3:网络小说的缓存
|
//TODO 构建新的缓存策略,几个选项,1:每本书一个缓存 2:控制缓存总大小,超过限制删除旧缓存 3:网络小说的缓存
|
||||||
|
|
||||||
if (bookPath == null || !bookPath.equals(bookList.getBookpath())) {
|
directoryList = LitePal.where("bookId=?",book.getId()+"").find(BookChapter.class);
|
||||||
cleanCacheFile();
|
|
||||||
this.bookPath = bookList.getBookpath();
|
for(BookChapter c :directoryList){
|
||||||
bookName = FileUtils.getFileName(bookPath);
|
Log.d(TAG, String.format("bookchapter :%s,fileName :%s, chapter Size %s",c.getChapterName(),c.getChapterPath(),c.getLength()));
|
||||||
cacheBook();
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if(directoryList.isEmpty()) {
|
||||||
|
|
||||||
|
if (bookPath == null || !bookPath.equals(book.getBookpath())) {
|
||||||
|
cleanCacheFile();
|
||||||
|
this.bookPath = book.getBookpath();
|
||||||
|
bookName = FileUtils.getFileName(bookPath);
|
||||||
|
cacheBook();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void cleanCacheFile(){
|
private void cleanCacheFile(){
|
||||||
File file = new File(cachedPath);
|
File file = new File(cachedPath );
|
||||||
if (!file.exists()){
|
if (!file.exists()){
|
||||||
file.mkdir();
|
file.mkdir();
|
||||||
}else{
|
}else{
|
||||||
|
@ -79,6 +113,19 @@ public class BookUtil {
|
||||||
files[i].delete();
|
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){
|
public int next(boolean back){
|
||||||
|
@ -87,7 +134,7 @@ public class BookUtil {
|
||||||
position = bookLen;
|
position = bookLen;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
char result = current();
|
char result = chaptCurrent(); //current();
|
||||||
if (back) {
|
if (back) {
|
||||||
position -= 1;
|
position -= 1;
|
||||||
}
|
}
|
||||||
|
@ -135,7 +182,11 @@ public class BookUtil {
|
||||||
|
|
||||||
return line.toCharArray();
|
return line.toCharArray();
|
||||||
}
|
}
|
||||||
|
public char chaptCurrent(){
|
||||||
|
|
||||||
|
char[] charArray = chaptChars(chapterNo);
|
||||||
|
return charArray[(int)position-1];
|
||||||
|
}
|
||||||
public char current(){
|
public char current(){
|
||||||
// int pos = (int) (position % cachedSize);
|
// int pos = (int) (position % cachedSize);
|
||||||
// int cachePos = (int) (position / cachedSize);
|
// int cachePos = (int) (position / cachedSize);
|
||||||
|
@ -179,16 +230,16 @@ public class BookUtil {
|
||||||
|
|
||||||
//缓存书本
|
//缓存书本
|
||||||
private void cacheBook() throws IOException {
|
private void cacheBook() throws IOException {
|
||||||
if (TextUtils.isEmpty(bookList.getCharset())) {
|
if (TextUtils.isEmpty(book.getCharset())) {
|
||||||
m_strCharsetName = FileUtils.getCharset(bookPath);
|
m_strCharsetName = FileUtils.getCharset(bookPath);
|
||||||
if (m_strCharsetName == null) {
|
if (m_strCharsetName == null) {
|
||||||
m_strCharsetName = "utf-8";
|
m_strCharsetName = "utf-8";
|
||||||
}
|
}
|
||||||
ContentValues values = new ContentValues();
|
ContentValues values = new ContentValues();
|
||||||
values.put("charset",m_strCharsetName);
|
values.put("charset",m_strCharsetName);
|
||||||
LitePal.update(Book.class,values,bookList.getId());
|
LitePal.update(Book.class,values,book.getId());
|
||||||
}else{
|
}else{
|
||||||
m_strCharsetName = bookList.getCharset();
|
m_strCharsetName = book.getCharset();
|
||||||
}
|
}
|
||||||
|
|
||||||
File file = new File(bookPath);
|
File file = new File(bookPath);
|
||||||
|
@ -225,8 +276,9 @@ public class BookUtil {
|
||||||
myArray.add(cache);
|
myArray.add(cache);
|
||||||
// myArray.add(new WeakReference<char[]>(buf));
|
// myArray.add(new WeakReference<char[]>(buf));
|
||||||
// myArray.set(index,);
|
// myArray.set(index,);
|
||||||
// Log.e(TAG,String.format("缓存的内容写入文件\n %s",fileName(index)));
|
Log.e(TAG,String.format("缓存的内容写入文件\n %s",fileName(index)));
|
||||||
// Log.e(TAG,"---------------------------------------------------------------------------------------------------------");
|
Log.e(TAG,"---------------------------------------------------------------------------------------------------------");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
File cacheBook = new File(fileName(index));
|
File cacheBook = new File(fileName(index));
|
||||||
if (!cacheBook.exists()){
|
if (!cacheBook.exists()){
|
||||||
|
@ -239,35 +291,91 @@ public class BookUtil {
|
||||||
throw new RuntimeException("Error during writing " + fileName(index));
|
throw new RuntimeException("Error during writing " + fileName(index));
|
||||||
}
|
}
|
||||||
index ++;
|
index ++;
|
||||||
}
|
|
||||||
|
|
||||||
new Thread(){
|
}
|
||||||
|
getChapter();
|
||||||
|
/*new Thread(){
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
getChapter();
|
getChapter();
|
||||||
}
|
}
|
||||||
}.start();
|
}.start();
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
//获取章节
|
//获取章节
|
||||||
public synchronized void getChapter(){
|
public synchronized void getChapter(){
|
||||||
try {
|
try {
|
||||||
long size = 0;
|
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++) {
|
for (int i = 0; i < myArray.size(); i++) {
|
||||||
char[] buf = block(i);
|
char[] buf = block(i);
|
||||||
String bufStr = new String(buf);
|
String bufStr = new String(buf);
|
||||||
String[] paragraphs = bufStr.split(lineBreakChar); // String[] paragraphs = bufStr.split("\r\n");
|
String[] paragraphs = bufStr.split(lineBreakChar); // String[] paragraphs = bufStr.split("\r\n");
|
||||||
|
|
||||||
for (String str : paragraphs) {
|
for (String str : paragraphs) {
|
||||||
|
|
||||||
// if (str.length() <= 30 && (str.matches(".*第.{1,8}章.*") || str.matches(".*第.{1,8}节.*"))) {
|
// if (str.length() <= 30 && (str.matches(".*第.{1,8}章.*") || str.matches(".*第.{1,8}节.*"))) {
|
||||||
if(isChapterTitle(str)) {
|
if(isChapterTitle(str)) {
|
||||||
BookChapter bookCatalogue = new BookChapter();
|
|
||||||
bookCatalogue.setBookChapterStartPos(size);
|
if(title.length()==0) {
|
||||||
bookCatalogue.setChapterName(str.replaceAll("###",""));
|
|
||||||
bookCatalogue.setBookpath(bookPath);
|
title = str;
|
||||||
directoryList.add(bookCatalogue);
|
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));
|
// Log.e(TAG,String.format("当前行\n %s",str));
|
||||||
if (str.contains("\u3000\u3000")) {
|
if (str.contains("\u3000\u3000")) {
|
||||||
size += str.length() + 2;
|
size += str.length() + 2;
|
||||||
|
@ -276,13 +384,40 @@ public class BookUtil {
|
||||||
}else {
|
}else {
|
||||||
size += str.length();
|
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){
|
}catch (Exception e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void createChapContent(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public List<BookChapter> getDirectoryList(){
|
public List<BookChapter> getDirectoryList(){
|
||||||
return directoryList;
|
return directoryList;
|
||||||
}
|
}
|
||||||
|
@ -294,6 +429,14 @@ public class BookUtil {
|
||||||
protected String fileName(int index) {
|
protected String fileName(int index) {
|
||||||
return cachedPath + bookName + index ;
|
return cachedPath + bookName + index ;
|
||||||
}
|
}
|
||||||
|
protected String fileChapterName(int chaptId ) {
|
||||||
|
|
||||||
|
return getChapterPath() + chaptId ;
|
||||||
|
}
|
||||||
|
|
||||||
|
String getChapterPath(){
|
||||||
|
return chapterPath +book.getId()+"/";
|
||||||
|
}
|
||||||
|
|
||||||
//获取书本缓存
|
//获取书本缓存
|
||||||
public char[] block(int index) {
|
public char[] block(int index) {
|
||||||
|
@ -327,6 +470,45 @@ public class BookUtil {
|
||||||
}
|
}
|
||||||
return block;
|
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){
|
public boolean isChapterTitle(String line){
|
||||||
return (line.length() <= 30 && (line.matches(".*第.{1,8}章.*") || line.matches(".*第.{1,8}节.*"))) ;
|
return (line.length() <= 30 && (line.matches(".*第.{1,8}章.*") || line.matches(".*第.{1,8}节.*"))) ;
|
||||||
}
|
}
|
||||||
|
|
|
@ -136,7 +136,7 @@ public class PageFactory {
|
||||||
private String bookName = "";
|
private String bookName = "";
|
||||||
private Book bookList;
|
private Book bookList;
|
||||||
//书本章节
|
//书本章节
|
||||||
private int currentCharter = 0;
|
private int currentChapter = 0;
|
||||||
//当前电量
|
//当前电量
|
||||||
private int level = 0;
|
private int level = 0;
|
||||||
private BookUtil mBookUtil;
|
private BookUtil mBookUtil;
|
||||||
|
@ -147,6 +147,43 @@ public class PageFactory {
|
||||||
private BookTask bookTask;
|
private BookTask bookTask;
|
||||||
ContentValues values = new ContentValues();
|
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;
|
private static Status mStatus = Status.OPENING;
|
||||||
|
|
||||||
public enum Status {
|
public enum Status {
|
||||||
|
@ -286,9 +323,9 @@ public class PageFactory {
|
||||||
mBookPageWidget.postInvalidate();
|
mBookPageWidget.postInvalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onDraw(Bitmap bitmap,List<String> m_lines,Boolean updateCharter) {
|
public void onDraw(Bitmap bitmap,List<String> m_lines,Boolean updateChapter) {
|
||||||
if (getDirectoryList().size() > 0 && updateCharter) {
|
if (getDirectoryList().size() > 0 && updateChapter) {
|
||||||
currentCharter = getCurrentCharter();
|
currentChapter = getCurrentCharter();
|
||||||
}
|
}
|
||||||
//更新数据库进度
|
//更新数据库进度
|
||||||
if (currentPage != null && bookList != null){
|
if (currentPage != null && bookList != null){
|
||||||
|
@ -297,6 +334,7 @@ public class PageFactory {
|
||||||
public void run() {
|
public void run() {
|
||||||
super.run();
|
super.run();
|
||||||
values.put("begin",currentPage.getBegin());
|
values.put("begin",currentPage.getBegin());
|
||||||
|
values.put("biginChapt",currentChapter);
|
||||||
int rows = LitePal.update(Book.class,values,bookList.getId());
|
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) );
|
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);
|
c.drawText(CommonUtil.subString(bookName,12), marginWidth ,statusMarginBottom + mBatterryFontSize, mBatterryPaint);
|
||||||
//画章
|
//画章
|
||||||
if (getDirectoryList().size() > 0) {
|
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;
|
int nChaterWidth = (int) mBatterryPaint.measureText(charterName) + 1;
|
||||||
c.drawText(charterName, mWidth - marginWidth - nChaterWidth, statusMarginBottom + mBatterryFontSize, mBatterryPaint);
|
c.drawText(charterName, mWidth - marginWidth - nChaterWidth, statusMarginBottom + mBatterryFontSize, mBatterryPaint);
|
||||||
}
|
}
|
||||||
|
@ -377,14 +415,12 @@ public class PageFactory {
|
||||||
//向前翻页
|
//向前翻页
|
||||||
public void prePage(){
|
public void prePage(){
|
||||||
if (currentPage.getBegin() <= 0) {
|
if (currentPage.getBegin() <= 0) {
|
||||||
Log.e(TAG,"当前是第一页");
|
Log.e(TAG,"当前是本章第一页");
|
||||||
if (!m_isfirstPage){
|
m_isfirstPage =currentChapter ==1;
|
||||||
|
if ( m_isfirstPage){
|
||||||
Toast.makeText(mContext, "当前是第一页", Toast.LENGTH_SHORT).show();
|
Toast.makeText(mContext, "当前是第一页", Toast.LENGTH_SHORT).show();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
m_isfirstPage = true;
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
m_isfirstPage = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cancelPage = currentPage;
|
cancelPage = currentPage;
|
||||||
|
@ -392,30 +428,27 @@ public class PageFactory {
|
||||||
currentPage = getPrePage();
|
currentPage = getPrePage();
|
||||||
onDraw(mBookPageWidget.getNextPage(),currentPage.getLines(),true);
|
onDraw(mBookPageWidget.getNextPage(),currentPage.getLines(),true);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//向后翻页
|
//向后翻页
|
||||||
public void nextPage(){
|
public void nextPage(){
|
||||||
if (currentPage.getEnd() >= mBookUtil.getBookLen()) {
|
if (currentPage.getEnd() >= mBookUtil.getBookLen()) {
|
||||||
Log.e(TAG,"已经是最后一页了");
|
Log.e(TAG,"已经是本章最后一页了");
|
||||||
if (!m_islastPage){
|
|
||||||
|
m_islastPage =currentChapter == mBookUtil.getDirectoryList().size();
|
||||||
|
if ( m_islastPage){
|
||||||
Toast.makeText(mContext, "已经是最后一页了", Toast.LENGTH_SHORT).show();
|
Toast.makeText(mContext, "已经是最后一页了", Toast.LENGTH_SHORT).show();
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
|
||||||
}
|
}
|
||||||
m_islastPage = true;
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
m_islastPage = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cancelPage = currentPage;
|
cancelPage = currentPage;
|
||||||
onDraw(mBookPageWidget.getCurPage(),currentPage.getLines(),true);
|
onDraw(mBookPageWidget.getCurPage(),currentPage.getLines(),true);
|
||||||
prePage = currentPage;
|
prePage = currentPage;
|
||||||
currentPage = getNextPage();
|
currentPage = getNextPage();
|
||||||
|
// currentPage = currentChaptPages.get(currentPage.getPageNo()-1);
|
||||||
onDraw(mBookPageWidget.getNextPage(),currentPage.getLines(),true);
|
onDraw(mBookPageWidget.getNextPage(),currentPage.getLines(),true);
|
||||||
Log.e("nextPage","nextPagenext");
|
Log.e("nextPage","nextPagenext");
|
||||||
}
|
}
|
||||||
|
@ -429,13 +462,13 @@ public class PageFactory {
|
||||||
* 打开书本
|
* 打开书本
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public void openBook(Book bookList) throws IOException {
|
public void openBook(Book book ) throws IOException {
|
||||||
//清空数据
|
//清空数据
|
||||||
currentCharter = 0;
|
currentChapter = 0;
|
||||||
// m_mbBufLen = 0;
|
// m_mbBufLen = 0;
|
||||||
initBg(config.getDayOrNight());
|
initBg(config.getDayOrNight());
|
||||||
|
|
||||||
this.bookList = bookList;
|
this.bookList = book ;
|
||||||
bookPath = bookList.getBookpath();
|
bookPath = bookList.getBookpath();
|
||||||
bookName = FileUtils.getFileName(bookPath);
|
bookName = FileUtils.getFileName(bookPath);
|
||||||
|
|
||||||
|
@ -446,10 +479,12 @@ public class PageFactory {
|
||||||
bookTask.cancel(true);
|
bookTask.cancel(true);
|
||||||
}
|
}
|
||||||
bookTask = new BookTask();
|
bookTask = new BookTask();
|
||||||
bookTask.execute(bookList.getBegin());
|
|
||||||
|
bookTask.execute(book.getBiginChapt(),book .getBegin());
|
||||||
}
|
}
|
||||||
|
|
||||||
private class BookTask extends AsyncTask<Long,Void,Boolean>{
|
private class BookTask extends AsyncTask<Long,Void,Boolean>{
|
||||||
|
private long chapter=0;
|
||||||
private long begin = 0;
|
private long begin = 0;
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(Boolean result) {
|
protected void onPostExecute(Boolean result) {
|
||||||
|
@ -461,7 +496,10 @@ public class PageFactory {
|
||||||
if (result) {
|
if (result) {
|
||||||
PageFactory.mStatus = PageFactory.Status.FINISH;
|
PageFactory.mStatus = PageFactory.Status.FINISH;
|
||||||
// m_mbBufLen = mBookUtil.getBookLen();
|
// 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) {
|
if (mBookPageWidget != null) {
|
||||||
currentPage(true);
|
currentPage(true);
|
||||||
}
|
}
|
||||||
|
@ -486,7 +524,9 @@ public class PageFactory {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Boolean doInBackground(Long... params) {
|
protected Boolean doInBackground(Long... params) {
|
||||||
begin = params[0];
|
chapter = params[0];
|
||||||
|
begin = params[1];
|
||||||
|
currentChapter = (int) chapter;
|
||||||
try {
|
try {
|
||||||
mBookUtil.openBook(bookList);
|
mBookUtil.openBook(bookList);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -499,6 +539,7 @@ public class PageFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
public TRPage getNextPage(){
|
public TRPage getNextPage(){
|
||||||
|
/*
|
||||||
mBookUtil.setPostition(currentPage.getEnd());
|
mBookUtil.setPostition(currentPage.getEnd());
|
||||||
|
|
||||||
TRPage trPage = new TRPage();
|
TRPage trPage = new TRPage();
|
||||||
|
@ -508,9 +549,20 @@ public class PageFactory {
|
||||||
Log.e(TAG,"page postion next end:" +mBookUtil.getPosition() + "");
|
Log.e(TAG,"page postion next end:" +mBookUtil.getPosition() + "");
|
||||||
trPage.setEnd(mBookUtil.getPosition());
|
trPage.setEnd(mBookUtil.getPosition());
|
||||||
return trPage;
|
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(){
|
public TRPage getPrePage(){
|
||||||
|
/*
|
||||||
mBookUtil.setPostition(currentPage.getBegin());
|
mBookUtil.setPostition(currentPage.getBegin());
|
||||||
|
|
||||||
TRPage trPage = new TRPage();
|
TRPage trPage = new TRPage();
|
||||||
|
@ -520,9 +572,24 @@ public class PageFactory {
|
||||||
Log.e(TAG,"page postion pre begin:" +mBookUtil.getPosition() + "");
|
Log.e(TAG,"page postion pre begin:" +mBookUtil.getPosition() + "");
|
||||||
trPage.setBegin(mBookUtil.getPosition() );
|
trPage.setBegin(mBookUtil.getPosition() );
|
||||||
return trPage;
|
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){
|
public TRPage getPageForBegin(long begin){
|
||||||
|
/*
|
||||||
TRPage trPage = new TRPage();
|
TRPage trPage = new TRPage();
|
||||||
trPage.setBegin(begin);
|
trPage.setBegin(begin);
|
||||||
|
|
||||||
|
@ -530,6 +597,15 @@ public class PageFactory {
|
||||||
trPage.setLines(getNextLines());
|
trPage.setLines(getNextLines());
|
||||||
trPage.setEnd(mBookUtil.getPosition());
|
trPage.setEnd(mBookUtil.getPosition());
|
||||||
return trPage;
|
return trPage;
|
||||||
|
*/
|
||||||
|
|
||||||
|
for(TRPage page : currentChaptPages)
|
||||||
|
{
|
||||||
|
if(page.getEnd() >begin){
|
||||||
|
return page;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new TRPage();
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean showChapTitleOnTopWhenNextPage =false;
|
boolean showChapTitleOnTopWhenNextPage =false;
|
||||||
|
@ -679,7 +755,7 @@ public class PageFactory {
|
||||||
//上一章
|
//上一章
|
||||||
public void preChapter(){
|
public void preChapter(){
|
||||||
if (mBookUtil.getDirectoryList().size() > 0){
|
if (mBookUtil.getDirectoryList().size() > 0){
|
||||||
int num = currentCharter;
|
int num = currentChapter;
|
||||||
if (num ==0){
|
if (num ==0){
|
||||||
num =getCurrentCharter();
|
num =getCurrentCharter();
|
||||||
}
|
}
|
||||||
|
@ -688,14 +764,14 @@ public class PageFactory {
|
||||||
long begin = mBookUtil.getDirectoryList().get(num).getBookChapterStartPos();
|
long begin = mBookUtil.getDirectoryList().get(num).getBookChapterStartPos();
|
||||||
currentPage = getPageForBegin(begin);
|
currentPage = getPageForBegin(begin);
|
||||||
currentPage(true);
|
currentPage(true);
|
||||||
currentCharter = num;
|
currentChapter = num;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//下一章
|
//下一章
|
||||||
public void nextChapter(){
|
public void nextChapter(){
|
||||||
int num = currentCharter;
|
int num = currentChapter;
|
||||||
if (num == 0){
|
if (num == 0){
|
||||||
num =getCurrentCharter();
|
num =getCurrentCharter();
|
||||||
}
|
}
|
||||||
|
@ -704,13 +780,13 @@ public class PageFactory {
|
||||||
long begin = getDirectoryList().get(num).getBookChapterStartPos();
|
long begin = getDirectoryList().get(num).getBookChapterStartPos();
|
||||||
currentPage = getPageForBegin(begin);
|
currentPage = getPageForBegin(begin);
|
||||||
currentPage(true);
|
currentPage(true);
|
||||||
currentCharter = num;
|
currentChapter = num;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//获取现在的章
|
//获取现在的章
|
||||||
public int getCurrentCharter(){
|
public int getCurrentCharter(){
|
||||||
int num = 0;
|
/*int num = 0;
|
||||||
for (int i = 0;getDirectoryList().size() > i;i++){
|
for (int i = 0;getDirectoryList().size() > i;i++){
|
||||||
BookChapter bookCatalogue = getDirectoryList().get(i);
|
BookChapter bookCatalogue = getDirectoryList().get(i);
|
||||||
if (currentPage.getEnd() >= bookCatalogue.getBookChapterStartPos()){
|
if (currentPage.getEnd() >= bookCatalogue.getBookChapterStartPos()){
|
||||||
|
@ -720,6 +796,9 @@ public class PageFactory {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return num;
|
return num;
|
||||||
|
*/
|
||||||
|
currentChapter = mBookUtil.getChapterNo();
|
||||||
|
return currentChapter ;
|
||||||
}
|
}
|
||||||
|
|
||||||
//绘制当前页面
|
//绘制当前页面
|
||||||
|
@ -846,7 +925,7 @@ public class PageFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clear(){
|
public void clear(){
|
||||||
currentCharter = 0;
|
currentChapter = 0;
|
||||||
bookPath = "";
|
bookPath = "";
|
||||||
bookName = "";
|
bookName = "";
|
||||||
bookList = null;
|
bookList = null;
|
||||||
|
@ -855,6 +934,10 @@ public class PageFactory {
|
||||||
cancelPage = null;
|
cancelPage = null;
|
||||||
prePage = null;
|
prePage = null;
|
||||||
currentPage = null;
|
currentPage = null;
|
||||||
|
|
||||||
|
currentChaptPages =null;
|
||||||
|
preChaptPages=null;
|
||||||
|
nextChaptPages=null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Status getStatus(){
|
public static Status getStatus(){
|
||||||
|
|
|
@ -1,12 +1,23 @@
|
||||||
package com.deiniu.zhuike.utils;
|
package com.deiniu.zhuike.utils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
public class TRPage {
|
public class TRPage {
|
||||||
private long begin;
|
private long begin;
|
||||||
private long end;
|
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() {
|
public long getBegin() {
|
||||||
return begin;
|
return begin;
|
||||||
|
|
Loading…
Reference in New Issue