pda/zhuike/.svn/pristine/ff/ff6756cd3f3fb717cb3fcc3f164...

1566 lines
55 KiB
Plaintext
Raw Permalink Normal View History

2024-02-06 22:23:29 +08:00
package com.novelbook.android.utils;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;
import com.novelbook.android.R;
import com.novelbook.android.bean.NovelSites;
import com.novelbook.android.bean.Site;
import com.novelbook.android.db.Chapter;
import com.novelbook.android.db.Novel;
import com.novelbook.android.netutils.NetUtil;
import com.novelbook.android.view.PageWidget;
import org.litepal.LitePal;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//import static com.baidu.tts.loopj.AsyncHttpClient.log;
public class PageFactory implements ChangeSource{
private static final String TAG = "PageFactory";
private static PageFactory pageFactory;
private Context mContext;
private Config config;
//<2F><>ǰ<EFBFBD><C7B0><EFBFBD>
// private File book_file = null;
// Ĭ<>ϱ<EFBFBD><CFB1><EFBFBD><EFBFBD><EFBFBD>ɫ
private int m_backColor = 0xffff9e85;
//ҳ<><D2B3><EFBFBD><EFBFBD>
private int mWidth;
//ҳ<><D2B3><EFBFBD><EFBFBD>
private int mHeight;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
private float m_fontSize ;
//ʱ<><CAB1><EFBFBD><EFBFBD>ʽ
private SimpleDateFormat sdf;
//ʱ<><CAB1>
private String date;
//<2F><><EFBFBD>ȸ<EFBFBD>ʽ
private DecimalFormat df ;
//<2F><><EFBFBD>ر߽<D8B1><DFBD><EFBFBD><EFBFBD><EFBFBD>
private float mBorderWidth;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ե<EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>
private float marginHeight ;
//<2F><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
private float screenHeihtDiff ;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ե<EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>
private float measureMarginWidth ;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ե<EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>
private float marginWidth ;
//״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>߶<EFBFBD>
private float statusMarginBottom;
//<2F>м<EFBFBD><D0BC><EFBFBD>
private float lineSpace;
//<2F>μ<EFBFBD><CEBC><EFBFBD>
private float paragraphSpace;
//<2F>μ<EFBFBD><CEBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>
private final float prate = 1.3f;
//<2F>ָ߶<D6B8>
private float fontHeight;
//<2F><><EFBFBD><EFBFBD>
private Typeface typeface;
//<2F><><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD>
private Paint mPaint;
//<2F><><EFBFBD>ػ<EFBFBD><D8BB><EFBFBD>
private Paint waitPaint;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
private int m_textColor = Color.rgb(50, 65, 78);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĿ<DDB5>
private float mVisibleHeight;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĿ<DDB5>
private float mVisibleWidth;
// ÿҳ<C3BF><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private int mLineCount;
//<2F><><EFBFBD>ػ<EFBFBD><D8BB><EFBFBD>
private Paint mBatterryPaint ;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
private float mBatterryFontSize;
//<2F><><EFBFBD><EFBFBD>ͼƬ
private Bitmap m_book_bg = null;
//<2F><>ǰ<EFBFBD><C7B0>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// private StringBuilder word = new StringBuilder();
//<2F><>ǰ<EFBFBD>ܹ<EFBFBD><DCB9><EFBFBD><EFBFBD><EFBFBD>
// private Vector<String> m_lines = new Vector<>();
// // <20><>ǰҳ<C7B0><D2B3>ʼλ<CABC><CEBB>
// private long m_mbBufBegin = 0;
// // <20><>ǰҳ<C7B0>յ<EFBFBD>λ<EFBFBD><CEBB>
// private long m_mbBufEnd = 0;
// // ֮ǰҳ<C7B0><D2B3>ʼλ<CABC><CEBB>
// private long m_preBegin = 0;
// // ֮ǰҳ<C7B0>յ<EFBFBD>λ<EFBFBD><CEBB>
// private long m_preEnd = 0;
// ͼ<><CDBC><EFBFBD>ܳ<EFBFBD><DCB3><EFBFBD>
// private long m_mbBufLen = 0;
private Intent batteryInfoIntent;
//<2F><><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD>ٷֱ<D9B7>
private float mBatteryPercentage;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߿<EFBFBD>
private RectF rect1 = new RectF();
//<2F><><EFBFBD><EFBFBD><EFBFBD>ڱ߿<DAB1>
private RectF rect2 = new RectF();
//<2F>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
// private String m_strCharsetName = "GBK";
//<2F><>ǰ<EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA>һҳ
private boolean m_isfirstPage;
//<2F><>ǰ<EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>һҳ
private boolean m_islastPage;
//<2F>鱾widget
private PageWidget mBookPageWidget;
// //<2F><EFBFBD><E9B1BE><EFBFBD>ж<EFBFBD>
// List<String> allParagraph;
// //<2F><EFBFBD><E9B1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// List<String> allLines = new ArrayList<>();
//<2F><><EFBFBD>ڵĽ<DAB5><C4BD><EFBFBD>
private float currentProgress;
//Ŀ¼
// private List<Chapter> directoryList = new ArrayList<>();
//<2F>鱾·<E9B1BE><C2B7>
private String bookPath = "";
//<2F><EFBFBD><E9B1BE><EFBFBD><EFBFBD>
private String bookName = "";
private Novel mBook;
//<2F><><EFBFBD><EFBFBD>Ŀ¼<C4BF>б<EFBFBD>
private List<Chapter> mChapters;
//<2F><>ǰ<EFBFBD>½<EFBFBD>
// private Chapter mCurrentChapter;
//<2F><EFBFBD>½<EFBFBD>
private int currentChapter = 0;
//<2F><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
private int level = 0;
private BookUtil mBookUtil;
private PageEvent mPageEvent;
private TRPage currentPage;
private TRPage prePage;
private TRPage cancelPage;
private long openBookPosition=0;//<2F><><EFBFBD><EFBFBD><EFBFBD>鱾ʱ<E9B1BE><CAB1><EFBFBD>½<EFBFBD>λ<EFBFBD><CEBB>
ContentValues values = new ContentValues();
private List<TRPage> currentChaptPages;
private List<TRPage> tmpChaptPages;
private List<TRPage> nextChaptPages;
private List<TRPage> preChaptPages;
private AdInterface mAd;
private BookTask bookTask;
private boolean isShowPrePage; //<2F><>ǰ<EFBFBD><C7B0>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ʱ<EFBFBD><CAB1><EFBFBD>Ӹ<EFBFBD><D3B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һҳ<D2BB><D2B3>ʼ<EFBFBD><CABC>ʾ
public void setAd(AdInterface ad){
mAd =ad;
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
int wt = msg.what;
handlerMsg(msg);
dismissProgressDialog();
}
};
void handlerMsg(Message msg) {
if (msg.what == 1) {
Log.d(TAG, String.format("prepare book handler get notic to download chapter %s , mBook is null? %s",currentChapter, mBook==null ) );
if (mBook != null) {
changeChapter(currentChapter);
}else{
mStatus = Status.FAIL;
drawStatus(mBookPageWidget.getCurPage());
drawStatus(mBookPageWidget.getNextPage());
}
}else if(msg.what==2){
mStatus = Status.FINISH;
Log.d(TAG, String.format("prepare book ready to open currentChapter %s ",currentChapter ) );
drawStatus(mBookPageWidget.getCurPage());
drawStatus(mBookPageWidget.getNextPage());
changeChapter(currentChapter,tmpChaptPages);
pageFactory.setBusy(false);
}
}
Map<Integer,Integer> fileRetryCnt = new HashMap<Integer,Integer>();
private List<TRPage> loadCurrentChapt(int chaptId){
Log.d(TAG, String.format("prepare book, loadCurrentChapt chaptId %s, getChapters().size() %s ,currentChapter %s ---------------------------------->",chaptId,getChapters().size() ,currentChapter) );
long startTime= new Date().getTime();
if(tmpChaptPages==null){
tmpChaptPages = new ArrayList<TRPage>();
}
tmpChaptPages.clear();
tmpChaptPages.add(new TRPage());
if (fileRetryCnt.containsKey(chaptId)) {
fileRetryCnt.put(chaptId, fileRetryCnt.get(chaptId) + 1);
} else {
fileRetryCnt.clear();//ֻ<><D6BB><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD>
fileRetryCnt.put(chaptId, 1);
}
Log.d(TAG, String.format("prepare book loadCurrentChapt %s, rertying count %s ",chaptId, fileRetryCnt.get(chaptId)));
if(fileRetryCnt.get(chaptId) > Constants.retryCnt){
mStatus = Status.FAIL;
drawStatus(mBookPageWidget.getCurPage());
drawStatus(mBookPageWidget.getNextPage());
return tmpChaptPages;
}
mStatus = Status.OPENING;
drawStatus(mBookPageWidget.getCurPage());
drawStatus(mBookPageWidget.getNextPage());
final File file = new File(getChapterFileName(chaptId));
if (!file.exists()) { //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
chaptId = chaptId > getChapters().size() ? getChapters().size() : chaptId;
chaptId = chaptId > 0 ? chaptId : 1;
}
final int chid = chaptId;
if(!mBook.isLocalBook()) {
/* mStatus = Status.OPENING;
drawStatus(mBookPageWidget.getCurPage());
drawStatus(mBookPageWidget.getNextPage());*/
// final File file = new File(getChapterFileName(chaptId));
Log.d(TAG, String.format("prepare book to open chapter %s ,file is exist ? %s",chaptId,file.exists() ) );
if (!file.exists()) {
mStatus = Status.OPENING;
if( !NetUtil.isNetworkConnected()){ //TODO: 500<30><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
mStatus = Status.NETWORKFAILE;
drawStatus(mBookPageWidget.getCurPage());
drawStatus(mBookPageWidget.getNextPage());
return tmpChaptPages;
}
if(getChapters().size()==0 && mBookUtil.muluRetryCount>=Constants.retryCnt) {
Log.d(TAG, String.format("prepare book download mulu ʧ<>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD> %s ,thread.name %s",mBookUtil.muluRetryCount,Thread.currentThread().getName() ) );
mBookUtil.muluRetryCount=0;
mStatus = Status.FAIL;
drawStatus(mBookPageWidget.getCurPage());
drawStatus(mBookPageWidget.getNextPage());
return tmpChaptPages;
}
// showProgressDialog();
new Thread() {
@Override
public void run() {
Log.d(TAG, String.format("prepare book to download chapter %s ,thread.name %s",chid,Thread.currentThread().getName() ) );
mBookUtil.chaptChars(chid);
super.run();
}}.start();
new Thread() {
@Override
public void run() {
int slepttime =0;
while( !file.exists() && slepttime <60 && mBookUtil.muluRetryCount<Constants.retryCnt){
try {
sleep(50);
slepttime++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.d(TAG, String.format("prepare book to download chapter %s ,slepted %s ,thread.name %s",chid,slepttime*50 ,Thread.currentThread().getName() ) );
mStatus = Status.FINISH;
if( mBookUtil.muluRetryCount>=Constants.retryCnt){
mStatus = Status.FAIL;
}
Log.d(TAG, String.format("prepare book to download chapter %s ,handler notice changeChapter %s",chid, currentChapter ) );
//notice file done
handler.sendEmptyMessage(1);
super.run();
}}.start();
drawStatus(mBookPageWidget.getCurPage());
drawStatus(mBookPageWidget.getNextPage());
return tmpChaptPages;
}
}
pageFactory.setBusy(true);
// chaptId = mChapters!=null && mChapters.size() <= chaptId ? 1 : chaptId;
new Thread() {
@Override
public void run() {
super.run();
Log.d(TAG, String.format("changing Source prepare book to open chapter %s, currentChapter %s ",chid ,currentChapter ) );
long time1 =new Date().getTime() - startTime;
Log.d(TAG, "loadCurrentChapt: time1: " +time1);
// startTime = new Date().getTime();
char[] chars = mBookUtil.chaptChars(chid);
long time2 =new Date().getTime() - startTime;
Log.d(TAG, "loadCurrentChapt: time2: " +time2);
// String s = new String(chars);
// Log.d(TAG, String.format("prepare book open chapter %s,chars %s ",chaptId ,s ) );
mBookUtil.setBookLen(chars.length);
mBookUtil.setChapterNo(chid);
// TRPage page = new TRPage();
tmpChaptPages.clear();
long length =0;
int pageNo =0;
while(length <chars.length ) {
pageNo++;
TRPage page = getNextChapterPage(length);
// Log.d(TAG,"prepare book page.getBegin :" + page.getBegin()+ ",chapter length "+ mBookUtil.getBookLen());
// if(page.getBegin() == mBookUtil.getBookLen() ){ //<2F><><EFBFBD><EFBFBD>һҳ<D2BB>հ׵<D5B0><D7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// break;
// }
page.setChapterNo(chid);
page.setLastPage(page.getEnd()==chars.length);
page.setFirstPage(pageNo==1);
page.setPageNo(pageNo);
// chaptPages.add(page);
tmpChaptPages.add(page);
length= page.getEnd();
}
long time3 =new Date().getTime() - startTime;
Log.d(TAG, "loadCurrentChapt: time3: " +time3);
handler.sendEmptyMessage(2);
}}.start();
/*
mStatus = Status.FINISH;
Log.d(TAG, String.format("changing Source prepare book to draw chapter %s, currentChapter %s ",chaptId ,currentChapter ) );
drawStatus(mBookPageWidget.getCurPage());
drawStatus(mBookPageWidget.getNextPage());*/
return tmpChaptPages;
}
public TRPage getNextChapterPage(long position){
mBookUtil.setPostition(position);
TRPage trPage = new TRPage();
trPage.setBegin(position +1);
// Log.d(TAG,"page postion next begin:" + (position + 1) + "");
trPage.setLines(getNextLines());
// Log.d(TAG,"page postion next end:" +mBookUtil.getPosition() + "");
trPage.setEnd(mBookUtil.getPosition());
return trPage;
}
private static Status mStatus = Status.OPENING;
public NovelSites getNovelSites() {
return mBookUtil.getmNovelSites();
}
private ProgressDialog progressDialog;
private void showProgressDialog() {
if ( null == progressDialog) {
progressDialog =new ProgressDialog(mContext);
progressDialog.setMessage("<22><><EFBFBD><EFBFBD>Ŭ<EFBFBD><C5AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>...");
}
try {
progressDialog.show();
}catch (Exception e)
{
Log.e(TAG, "prepare book: ", e);
}
// progressDialog.show(mContext,"<22><><EFBFBD><EFBFBD><E7B2BB><EFBFBD><EFBFBD>","<22><>Ŭ<EFBFBD><C5AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",false,true);
}
private void dismissProgressDialog() {
if ( null != progressDialog) {
progressDialog.dismiss();
}
}
public void changeSource(String domain,int chapId,String chapTitle) {
fileRetryCnt.clear();
mBookUtil.changeSource(domain, chapId, chapTitle);
}
public String getChapterName() {
return getChapters().get(currentChapter-1).getChapterName();
}
public String getSite() {
return mBookUtil.getSite();
}
public String getChapterFileName(int chapid) {
return mBookUtil.fileChapterName(chapid);
}
private static boolean isBusy =false;
public void setBusy(boolean isBusy) {
this.isBusy = isBusy;
}
public static boolean busy(){
return isBusy;
}
public Boolean canCancelPage() {
return !cancelPage.isFirstPage() && !cancelPage.isLastPage();
}
public enum Status {
OPENING,
FINISH,
FAIL,
NETWORKFAILE,
SERVERERROR
}
public static synchronized PageFactory getInstance(Context context){
if(pageFactory==null){
createPageFactory(context);
}
return pageFactory;
}
public static synchronized PageFactory createPageFactory(Context context){
if (pageFactory == null){
pageFactory = new PageFactory(context);
}
return pageFactory;
}
private PageFactory(Context context) {
// mBookUtil = new BookUtil();
mContext = context.getApplicationContext();
config = Config.getInstance();
//<2F><>ȡ<EFBFBD><C8A1>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metric = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(metric);
mWidth = metric.widthPixels;
// mHeight = metric.heightPixels;
// mHeight = CommonUtil.getDpi(context);
mHeight = CommonUtil.getScreenHeight(context) - CommonUtil.statusBarDiff(context);
sdf = new SimpleDateFormat("HH:mm");//HH:mmΪ24Сʱ<D0A1><CAB1>,hh:mmΪ12Сʱ<D0A1><CAB1>
date = sdf.format(new java.util.Date());
df = new DecimalFormat("#0.0");
marginWidth = mContext.getResources().getDimension(R.dimen.readingMarginWidth);
marginHeight = mContext.getResources().getDimension(R.dimen.readingMarginHeight);
// Log.d(TAG, "getNavigationBarSize: orignal marginHeight +" +marginHeight);
// marginHeight = CommonUtil. getStatusBarHeight(mContext);
statusMarginBottom = mContext.getResources().getDimension(R.dimen.reading_status_margin_bottom);
screenHeihtDiff =CommonUtil.statusBarDiff(mContext);
Log.d(TAG, "getNavigationBarSize: screenHeihtDiff " +screenHeihtDiff);
lineSpace = context.getResources().getDimension(R.dimen.reading_line_spacing);
paragraphSpace = context.getResources().getDimension(R.dimen.reading_paragraph_spacing);
mVisibleWidth = mWidth - marginWidth * 2;
mVisibleHeight = mHeight - marginHeight * 2;
mHeight +=screenHeihtDiff;
typeface = config.getTypeface();
m_fontSize = config.getFontSize();
lineSpace =config.getLineSpace();
paragraphSpace = prate * lineSpace ;
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);// <20><><EFBFBD><EFBFBD>
mPaint.setTextAlign(Paint.Align.LEFT);// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
mPaint.setTextSize(m_fontSize);// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
mPaint.setColor(m_textColor);// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
mPaint.setTypeface(typeface);
mPaint.setSubpixelText(true);// <20><><EFBFBD>ø<EFBFBD><C3B8><EFBFBD>Ϊtrue<75><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>LCD<43><44>Ļ<EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD>ʾЧ<CABE><D0A7>
waitPaint = new Paint(Paint.ANTI_ALIAS_FLAG);// <20><><EFBFBD><EFBFBD>
waitPaint.setTextAlign(Paint.Align.LEFT);// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
waitPaint.setTextSize(mContext.getResources().getDimension(R.dimen.reading_max_text_size));// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
waitPaint.setColor(m_textColor);// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
waitPaint.setTypeface(typeface);
waitPaint.setSubpixelText(true);// <20><><EFBFBD>ø<EFBFBD><C3B8><EFBFBD>Ϊtrue<75><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>LCD<43><44>Ļ<EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD>ʾЧ<CABE><D0A7>
calculateLineCount();
mBorderWidth = mContext.getResources().getDimension(R.dimen.reading_board_battery_border_width);
mBatterryPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBatterryFontSize = CommonUtil.sp2px(context, 12);
mBatterryPaint.setTextSize(mBatterryFontSize);
mBatterryPaint.setTypeface(typeface);
mBatterryPaint.setTextAlign(Paint.Align.LEFT);
mBatterryPaint.setColor(m_textColor);
batteryInfoIntent = context.getApplicationContext().registerReceiver(null,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED)) ;//ע<><D7A2><EFBFBD>㲥,<2C><>ʱ<EFBFBD><CAB1>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>Ϣ
initBg(config.getDayOrNight());
measureMarginWidth();
}
private void measureMarginWidth(){
float wordWidth =mPaint.measureText("\u3000");
float width = mVisibleWidth % wordWidth;
measureMarginWidth = marginWidth + width / 2;
// Rect rect = new Rect();
// mPaint.getTextBounds("<22><>", 0, 1, rect);
// float wordHeight = rect.height();
// float wordW = rect.width();
// Paint.FontMetrics fm = mPaint.getFontMetrics();
// float wrodH = (float) (Math.ceil(fm.top + fm.bottom + fm.leading));
// String a = "";
}
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private void initBg(Boolean isNight){
if (isNight) {
//<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD>
// setBgBitmap(BitmapUtil.decodeSampledBitmapFromResource(
// mContext.getResources(), R.drawable.main_bg, mWidth, mHeight));
Bitmap bitmap = Bitmap.createBitmap(mWidth,mHeight, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.BLACK);
setBgBitmap(bitmap);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
setM_textColor(Color.rgb(128, 128, 128));
setBookPageBg(Color.BLACK);
} else {
//<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD>
setBookBg(config.getBookBgType());
}
}
private void calculateLineCount(){
mLineCount = (int) (mVisibleHeight / (m_fontSize + lineSpace));// <20><><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
private void calculateLineCount(float paragrapheight){
mLineCount = (int) ((mVisibleHeight - paragrapheight ) / (m_fontSize + lineSpace));// <20><><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// Log.d(TAG,"line count is " + mLineCount +" paragrapheight is " +paragrapheight);
}
private void drawStatus(Bitmap bitmap){
mAd.hideSystemUI();
String status = "";
switch (mStatus){
case OPENING:
status = "<22><><EFBFBD><EFBFBD>ƴ<EFBFBD><C6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>...";
break;
case FAIL:
status = "<22><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ժ<EFBFBD><D4BA><EFBFBD><EFBFBD><EFBFBD>";
break;
case NETWORKFAILE:
status = "<22><EFBFBD><EBBFAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
break;
case SERVERERROR:
status = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
break;
case FINISH:
status = "<22><><EFBFBD>سɹ<D8B3>";
break;
}
Canvas c = new Canvas(bitmap);
c.drawBitmap(getBgBitmap(), 0, 0, null);
waitPaint.setColor(getTextColor());
waitPaint.setTextAlign(Paint.Align.CENTER);
Rect targetRect = new Rect(0, 0, mWidth, mHeight);
// c.drawRect(targetRect, waitPaint);
Paint.FontMetricsInt fontMetrics = waitPaint.getFontMetricsInt();
// ת<><D7AA><EFBFBD><EFBFBD>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>http://blog.csdn.net/hursing
int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>ˮƽ<CBAE><C6BD><EFBFBD>У<EFBFBD>drawText<78><74>Ӧ<EFBFBD><D3A6>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>targetRect.centerX()
waitPaint.setTextAlign(Paint.Align.CENTER);
c.drawText(status, targetRect.centerX(), baseline, waitPaint);
// c.drawText("<22><><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>鱾...", mHeight / 2, 0, waitPaint);
mBookPageWidget.postInvalidate();
mAd.hideSystemUI();
}
public void onDraw(Bitmap bitmap,List<String> m_lines,Boolean updateChapter) {
mAd.hideSystemUI();
if(m_lines.size()==0){
return;
}
// Log.d(TAG, String.format(" prepare book onDraw chapter %s, getChapters().size() %s ",currentChapter ,getChapters().size() ) );
if (getChapters().size() > 0 && updateChapter) {
// Log.d(TAG, String.format(" prepare book onDraw chapter to getCurrentChapter(),currentChapter %s ",currentChapter ) );
currentChapter = getCurrentChapter();
// Log.d(TAG, String.format(" prepare book onDraw chapter after getCurrentChapter(),currentChapter %s ",currentChapter ) );
}
// Log.d(TAG, String.format(" prepare book onDraw chapter _____________ %s ",currentChapter ) );
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>
if ( mStatus ==Status.FINISH && currentPage != null && mBook != null){
new Thread() {
@Override
public void run() {
super.run();
if(mBook.getLastReadChapt() !=currentChapter || mBook.getLastReadPos()!=currentPage.getBegin()) {
if(currentChapter >1) {
mBook.setLastReadChapt(currentChapter);
}else{
mBook.setToDefault("lastReadChapt");
}
mBook.setLastReadPos(currentPage.getBegin());
mBook.update(mBook.getId()); //If you set a default value to a field, the corresponding
// * column won't be updated.
// mBook.save();
/*Novel nv = LitePal.find(Novel.class,mBook.getId());
Log.d(TAG,String.format("prepare book saved lastchapt %s,lastpos %s, db lastchapt %s last pos %s",
currentChapter,currentPage.getBegin(),nv.getLastReadChapt(),nv.getLastReadPos()));
*/
}
/*
values.put("lastReadPos",currentPage.getBegin());
values.put("lastReadChapt",currentChapter);
Log.d(TAG,String.format("begin to update book %s chapter%s bigin %s ",mBook.getName(),currentChapter, currentPage.getBegin() ) );
int rows = LitePal.update(Novel.class,values,mBook.getId());
Log.d(TAG,String.format("update book %s chapter%s bigin %s, result %s",mBook.getName(),currentChapter, currentPage.getBegin(),rows) );
*/
}
}.start();
}
Canvas c = new Canvas(bitmap);
c.drawBitmap(getBgBitmap(), 0, 0, null);
// word.setLength(0);
mPaint.setTextSize(getFontSize());
mPaint.setColor(getTextColor());
mBatterryPaint.setColor(getTextColor());
if (m_lines.size() == 0) {
return;
}
float space =m_fontSize + lineSpace;
paragraphSpace = prate * lineSpace;
if (m_lines.size() > 0) {
float y = marginHeight;
String lastLine ="";
for (String strLine : m_lines) {
// if(strLine.endsWith("\n")) {
// if(strLine.charAt(strLine.length()-1) == ('\n' )) {
// Log.d(TAG, strLine);
// Log.d(TAG,"<22><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" +strLine.charAt(strLine.length()-1) + "");
lastLine =strLine;
if(( strLine.charAt(strLine.length()-1) + "" ).equals("\n")){
strLine =strLine.replace("\n","");
y += space;
space =m_fontSize + paragraphSpace;
// Log.d( TAG ,String.format("prepare book <20><>ʼ<EFBFBD><EFBFBD><C2B6><EFBFBD> %s, y plus is %s" ,strLine, m_fontSize + paragraphSpace));
}else{
y += space;
space =m_fontSize + lineSpace;
// Log.d(TAG,String.format("prepare book %s,y plus is %s" ,strLine, m_fontSize + lineSpace));
}
c.drawText(strLine, measureMarginWidth, y, mPaint);
// word.append(strLine);
}
// Log.d(TAG,String.format("getNavigationBarSize mHeight is %s ,last line height %s, power %s, %s",
// mHeight,y,mHeight - CommonUtil.convertDpToPixel(mContext,10) + mBorderWidth - statusMarginBottom, lastLine));
float adHeight = mHeight -y - space -marginHeight-statusMarginBottom;
float adY =y +space;
showAd((int)adHeight,(int)adY);
// Log.d(TAG,String.format("ad + statusMarginBottom %s ",200+ statusMarginBottom));
// Log.d(TAG,String.format("adHeight %s, adY %s",adHeight,adY));
}
//<2F><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>ʱ<EFBFBD><CAB1>
int dateWith = (int) (mBatterryPaint.measureText(date)+mBorderWidth);//ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// float fPercent = (float) (currentPage.getBegin() * 1.0 / mBookUtil.getBookLen());//<2F><><EFBFBD><EFBFBD>
float fPercent = (float) (currentPage.getPageNo() * 1.0 /currentChaptPages.size());//<2F><><EFBFBD><EFBFBD>
currentProgress = fPercent;
if (mPageEvent != null){
mPageEvent.changeProgress(fPercent);
}
// String strPercent = df.format(fPercent * 100) + "%";//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
String strPercent = String.format("%s/%s",currentPage.getPageNo(),currentChaptPages.size()) + "ҳ";//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int nPercentWidth = (int) mBatterryPaint.measureText("10/25ҳ") + 50; //Paint.measureTextֱ<74>ӷ<EFBFBD><D3B7>؅<EFBFBD><D885><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õČ<C3B5><C48C><EFBFBD>
float botoomY =mHeight - statusMarginBottom;// +screenHeihtDiff;
c.drawText(strPercent, mWidth - nPercentWidth, botoomY, mBatterryPaint);//x yΪ<79><CEAA><EFBFBD><EFBFBD>ֵ
c.drawText(date, marginWidth ,botoomY, mBatterryPaint);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
level = batteryInfoIntent.getIntExtra( "level" , 0 );
int scale = batteryInfoIntent.getIntExtra("scale", 100);
mBatteryPercentage = (float) level / scale;
float rect1Left = marginWidth + dateWith + statusMarginBottom;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>leftλ<74><CEBB>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
float width = CommonUtil.convertDpToPixel(mContext,20) - mBorderWidth;
float height = CommonUtil.convertDpToPixel(mContext,10);
rect1.set(rect1Left, botoomY - height ,rect1Left + width, botoomY);
rect2.set(rect1Left + mBorderWidth, botoomY - height + mBorderWidth , rect1Left + width - mBorderWidth, botoomY - mBorderWidth );
// c.save(Canvas.CLIP_SAVE_FLAG);
c.save();
c.clipRect(rect2, Region.Op.DIFFERENCE);
c.drawRect(rect1, mBatterryPaint);
c.restore();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
rect2.left += mBorderWidth;
rect2.right -= mBorderWidth;
rect2.right = rect2.left + rect2.width() * mBatteryPercentage;
rect2.top += mBorderWidth;
rect2.bottom -= mBorderWidth;
c.drawRect(rect2, mBatterryPaint);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ
int poleHeight = (int) CommonUtil.convertDpToPixel(mContext,10) / 2;
rect2.left = rect1.right;
rect2.top = rect2.top + poleHeight / 4;
rect2.right = rect1.right + mBorderWidth;
rect2.bottom = rect2.bottom - poleHeight/4;
c.drawRect(rect2, mBatterryPaint);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
c.drawText(CommonUtil.subString(bookName,12), marginWidth ,statusMarginBottom + mBatterryFontSize, mBatterryPaint);
//<2F><><EFBFBD><EFBFBD>
/* String chapterName ="";
if(mCurrentChapter!=null){
chapterName = mCurrentChapter.getChapterName();
}else*/
if (getChapters().size() > 0)
{
String chapterName = CommonUtil.subString(getChapterName(),16);
int nChaterWidth = (int) mBatterryPaint.measureText(chapterName) + 1;
c.drawText(chapterName, mWidth - marginWidth - nChaterWidth, statusMarginBottom + mBatterryFontSize, mBatterryPaint);
}
mBookPageWidget.postInvalidate();
}
private void showAd(int adHeight,int adY) {
boolean showAd = getCurrentPage().getPageNo() > 2
&&NetWorkUtil.isNetworkConnected(mContext);
if (mAd != null) {
mAd.showAd(showAd,adHeight, adY);
}
}
//<2F><>ǰ<EFBFBD><C7B0>ҳ
public void prePage(){
if(null == currentPage){
return;
}
if(mBookUtil.isBusy()){
return;
}
m_isfirstPage = false;
if (currentPage.getBegin() <= 1) {
Log.d(TAG,"<22><>ǰ<EFBFBD>DZ<EFBFBD><C7B1>µ<EFBFBD>һҳ");
m_isfirstPage =currentChapter ==1;
if ( m_isfirstPage){
Toast.makeText(mContext, "<22><>ǰ<EFBFBD>ǵ<EFBFBD>һҳ", Toast.LENGTH_SHORT).show();
return;
}
}
Log.d(TAG, "prepare book prePage: to open prepage: ");
cancelPage = currentPage;
onDraw(mBookPageWidget.getCurPage(),currentPage.getLines(),true);
currentPage = getPrePage();
onDraw(mBookPageWidget.getNextPage(),currentPage.getLines(),true);
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҳ
public void nextPage(){
m_islastPage = false;
if(null == currentPage){
return;
}
if(mBookUtil.isBusy()){
return;
}
if (currentPage.getEnd() >= mBookUtil.getBookLen()) {
Log.d(TAG,"<22>Ѿ<EFBFBD><D1BE>DZ<EFBFBD><C7B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һҳ<D2BB><D2B3>");
m_islastPage =currentChapter == mBookUtil.getChapters().size();
if ( m_islastPage){
Toast.makeText(mContext, "<22>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һҳ<D2BB><D2B3>", Toast.LENGTH_SHORT).show();
return;
} else {
}
}
// Log.d(TAG, "prepare book nextPage: to open next page: ");
cancelPage = currentPage;
onDraw(mBookPageWidget.getCurPage(),currentPage.getLines(),true);
prePage = currentPage;
currentPage = getNextPage();
// currentPage = currentChaptPages.get(currentPage.getPageNo()-1);
onDraw(mBookPageWidget.getNextPage(),currentPage.getLines(),true);
Log.d("nextPage","nextPagenext");
}
//ȡ<><C8A1><EFBFBD><EFBFBD>ҳ
public void cancelPage(){
if(cancelPage !=null && (cancelPage.isLastPage() || cancelPage.isFirstPage())){ //TODO:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
mBookUtil.setChapterNo(cancelPage.getChapterNo());
// openBookPosition = cancelPage.getBegin();
Log.d(TAG, "cancelPage: cancelPage is LastPage? " +cancelPage.isLastPage() );
Log.d(TAG, String.format("cancelPage:cancelPage.getBegin() %s, cancelPage.getPageno() %s, preChaptPages is null? %s, nextChaptPages is null %s",
cancelPage.getBegin() ,cancelPage.getPageNo(),preChaptPages ==null,nextChaptPages==null ));
if(cancelPage.isLastPage()){
currentChaptPages = nextChaptPages;
}else{
currentChaptPages = preChaptPages;
}
// currentChaptPages = loadCurrentChapt(cancelPage.getChapterNo());
currentPage = getPageForBegin(cancelPage.getBegin()) ;// currentChaptPages.get(0);
// currentPage = getPageForBegin(begin);
if (mBookPageWidget != null) {
// currentPage(true);
}
}
currentPage = cancelPage;
}
public void prepareBook(Novel book){
Log.d(TAG, "prepare book: start prepare book " + book.getName());
if(mBook!=null &&mBook.getNovelId() !=book.getNovelId()){ //ȡ<><C8A1>δ<EFBFBD>ϱ<EFBFBD><CFB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD>web<65><62><EFBFBD>󣬴<EFBFBD><F3A3ACB4><EFBFBD>֤Ч<D6A4><D0A7>
try {
NetUtil.cancelRequest(mBook.getNovelId() );
}catch (Exception e)
{
Log.e(TAG, "prepare book: error on canceling request "+e.getMessage());
e.printStackTrace();
}
}
this.mBook = book ;
mBookUtil = new BookUtil();
//this.mBookUtil.setContext(context);
this.mBookUtil.setNovel(book);
this.mBookUtil.getTargetSites();
}
/**
* <20><><EFBFBD><EFBFBD><EFBFBD>
* @throws IOException
*/
public void openBook(Novel book ,Context context) throws IOException {
if(book.isLocalBook() || mBookUtil==null){ //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9BCAE><EFBFBD>³<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>mBookUtil
mBookUtil = new BookUtil();
}
mBookUtil.setContext(context);
mContext =context;
mBookUtil.pagefactory=this;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
currentChapter = 0;
// m_mbBufLen = 0;
initBg(config.getDayOrNight());
if(mBook!=null &&mBook.getNovelId() !=book.getNovelId()){ //ȡ<><C8A1>δ<EFBFBD>ϱ<EFBFBD><CFB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD>web<65><62><EFBFBD><EFBFBD>
NetUtil.cancelRequest(mBook.getNovelId() );
}
this.mBook = book ;
bookPath = mBook.getNovelPath();
bookName =mBook.getName();// FileUtils.getFileName(bookPath);
openBookPosition = mBook.getLastReadPos();
// this.mCurrentChapter = chapter;
mStatus = Status.OPENING;
drawStatus(mBookPageWidget.getCurPage());
drawStatus(mBookPageWidget.getNextPage());
if (bookTask != null && bookTask.getStatus() != AsyncTask.Status.FINISHED){
bookTask.cancel(true);
}
bookTask = new BookTask();
Log.d(TAG, String.format("prepare book 1 to open chapter %s ",book.getLastReadChapt() ) );
bookTask.execute((long)book.getLastReadChapt(),book .getLastReadPos());
}
private class BookTask extends AsyncTask<Long,Void,Boolean>{
private long chapter=0;
// private long begin = 0;
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
Log.d("onPostExecute",isCancelled() + "");
if (isCancelled()){
return;
}
if (result) {
Log.d(TAG, String.format("prepare book ready, to open chapter %s ",chapter ) );
// m_mbBufLen = mBookUtil.getBookLen();
mBookUtil.setChapterNo((int)chapter);
Log.d(TAG, String.format("prepare book ready, to open chapter, loadCurrentChapt %s ",chapter ) );
loadCurrentChapt((int)chapter);
/* currentChaptPages = loadCurrentChapt((int)chapter);
currentPage = getPageForBegin(begin) ;// currentChaptPages.get(0);
if (mBookPageWidget != null) {
currentPage(true);
}*/
// Log.d(TAG, String.format("prepare book set PageFactory.mStatus %s .",PageFactory.Status.FINISH ) );
// PageFactory.mStatus = PageFactory.Status.FINISH;
}else{
PageFactory.mStatus = PageFactory.Status.FAIL;
drawStatus(mBookPageWidget.getCurPage());
drawStatus(mBookPageWidget.getNextPage());
Toast.makeText(mContext,"<22><><EFBFBD><EFBFBD><EFBFBD>鱾ʧ<E9B1BE>ܣ<EFBFBD>",Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected Boolean doInBackground(Long... params) {
chapter = params[0];
//begin = params[1];
currentChapter = (int) chapter;
try {
mBookUtil.openBook(mBook,chapter);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}
public TRPage getNextPage(){
int nextPageNo =currentPage.getPageNo();
Log.e(TAG,String.format("prepare book getNextPage() currentChapter %s, currentChaptPages .size() is %s,currentpage pageno is %s"
,currentChapter, currentChaptPages.size() ,currentChapter, nextPageNo));
if(nextPageNo >= currentChaptPages.size()){
preChaptPages =currentChaptPages;
currentChapter++;
mBookUtil.setChapterNo(currentChapter);
isShowPrePage = false;
currentChaptPages = loadCurrentChapt(currentChapter ) ;
nextPageNo =0;
}
return currentChaptPages.get(nextPageNo);
}
public TRPage getPrePage(){
int prePageNo =currentPage.getPageNo()-1;
Log.d(TAG,String.format("currentPageno %s,total pagno %s",currentPage.getPageNo(),currentChaptPages.size()));
if(prePageNo <=0){
nextChaptPages =currentChaptPages;
currentChapter--;
if(currentChapter ==0) {
return new TRPage("û<><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
mBookUtil.setChapterNo(currentChapter);
isShowPrePage = true;
currentChaptPages = loadCurrentChapt(currentChapter ) ;
prePageNo = currentChaptPages.size();
}
return currentChaptPages.get(prePageNo-1);
}
public TRPage getPageForBegin(long begin){
/*
TRPage trPage = new TRPage();
trPage.setBegin(begin);
mBookUtil.setPostition(begin - 1);
trPage.setLines(getNextLines());
trPage.setEnd(mBookUtil.getPosition());
return trPage;
*/
Log.d(TAG, String.format("prepare book getPageForBegin: chaptid %s, begin %s ",currentChapter,begin));
for(TRPage page : currentChaptPages)
{
if(page.getEnd() >begin){
return page;
}
}
//return new TRPage();// currentChaptPages.get(currentChaptPages.size()-1);
return currentChaptPages.get(currentChaptPages.size()-1);
}
boolean showChapTitleOnTopWhenNextPage =false;
public List<String> getNextLines(){
List<String> lines = new ArrayList<>();
float width = 0;
float height = 0;
String line = "";
if(mBookUtil.getPosition()==0) {
lines.add("\n");lines.add("\n");
}
calculateLineCount();
while (mBookUtil.next(true) != -1){
char word = (char) mBookUtil.next(false);
//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
if ((word + "" ).equals("\n") ){// if ((word + "" ).equals("\r") && (((char) mBookUtil.next(true)) + "").equals("\n")){
// mBookUtil.next(false);
if ( !line.isEmpty()){
if (showChapTitleOnTopWhenNextPage && lines.size() >0 && mBookUtil.isChapterTitle(line)) {
Log.d(TAG,String.format("title is %s\n,size is %s ,position is %s" ,line,line.length(),mBookUtil.getPosition() ));
break;
}
lines.add(line+word);
// lines.add("\n");
line = "";
width = 0;
height += paragraphSpace - lineSpace;
calculateLineCount(height);
if (lines.size()>= mLineCount){
// Log.d(TAG,String.format("lines count limit a %s,lines size %s",mLineCount,lines.size()));
line ="";
break;
}
}
}else {
float widthChar = mPaint.measureText(word + "");
width += widthChar;
if (width > mVisibleWidth) {
width = widthChar;
lines.add(line);
/* if (lines.size() == mLineCount){
Log.d(TAG,String.format("lines count limit b %s,lines size %s",mLineCount,lines.size()));
line ="";
break;
}
*/
line = word + "";
} else {
line += word;
}
}
if (lines.size() == mLineCount){
// Log.d(TAG,String.format("lines count limit c %s,lines size %s",mLineCount,lines.size()));
if (!line.isEmpty()){
// mBookUtil.setPostition(mBookUtil.getPosition() - line.length()-2);// mBookUtil.setPostition(mBookUtil.getPosition() - 1);
mBookUtil.setPostition(mBookUtil.getPosition() - 1);
}
break;
}
}
/*
if (lines.size() == mLineCount) {
Log.d(TAG, String.format("lines count limit d %s,lines size %s", mLineCount, lines.size()));
Log.d(TAG, String.format("lines count limit d %s,line is \n %s", mLineCount, line));
if (!line.isEmpty()) {
mBookUtil.setPostition(mBookUtil.getPosition() - line.length() - 2);// mBookUtil.setPostition(mBookUtil.getPosition() - 1);
}
}*/
if (!line.isEmpty() && lines.size() < mLineCount){
if (!(showChapTitleOnTopWhenNextPage && mBookUtil.isChapterTitle(line) ) ) {
lines.add(line);
}else
{
// mBookUtil.setPostition(mBookUtil.getPosition() - line.length()-2);
}
// lines.add(line);
}
/*for (String str : lines){
Log.d(TAG,str + " ");
}*/
return lines;
}
public List<String> getPreLines(){
List<String> lines = new ArrayList<>();
float width = 0;
String line = "";
char[] par = mBookUtil.preLine();
while (par != null){
List<String> preLines = new ArrayList<>();
for (int i = 0 ; i < par.length ; i++){
char word = par[i];
float widthChar = mPaint.measureText(word + "");
width += widthChar;
if (width > mVisibleWidth) {
width = widthChar;
preLines.add(line);
line = word + "";
} else {
line += word;
}
// Log.d(TAG,"preLine is \n" + line);
}
/* if ( mBookUtil.isChapterTitle(line)) {
mBookUtil.setPostition(mBookUtil.getPosition() - line.length()-2);
break;
}*/
// lines.add(line);
// Log.d(TAG,"preLine is \n" + line);
if (!line.isEmpty()){
preLines.add(line);
}
lines.addAll(0,preLines);
if (lines.size() >= mLineCount){
break;
}
width = 0;
line = "";
par = mBookUtil.preLine();
}
List<String> reLines = new ArrayList<>();
int num = 0;
for (int i = lines.size() -1;i >= 0;i --){
if (reLines.size() < mLineCount) {
reLines.add(0,lines.get(i));
}else{
num = num + lines.get(i).length();
}
Log.d(TAG,lines.get(i) + " ");
}
if (num > 0){
if ( mBookUtil.getPosition() > 0) {
mBookUtil.setPostition(mBookUtil.getPosition() + num + 1);//mBookUtil.setPostition(mBookUtil.getPosition() + num + 2); // /r/n -> /n,ֻ<><D6BB>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD>ַ<EFBFBD>
}else{
mBookUtil.setPostition(mBookUtil.getPosition() + num );
}
}
return reLines;
}
//<2F><>һ<EFBFBD><D2BB>
public void preChapter(){
fileRetryCnt.clear();
if(currentChapter==1){
Toast.makeText(mContext,"<22><>ͷ<EFBFBD><CDB7>",Toast.LENGTH_SHORT).show();
}
// if (mBookUtil.getChapters().size() > 0 ){
int num = currentChapter;
if (num ==1){
num = getCurrentChapter();
}
num --;
if (num >= 1){
nextChaptPages =currentChaptPages;
currentChapter = num;
mBookUtil.setChapterNo(currentChapter);
currentChaptPages = loadCurrentChapt(currentChapter ) ;
// currentPage = getPageForBegin(0);
// currentPage(true);
}
}
//}
//<2F><>һ<EFBFBD><D2BB>
public void nextChapter(){
fileRetryCnt.clear();
int num = currentChapter;
if (num == 0){
num = getCurrentChapter();
}
if(num==getChapters().size()){
Toast.makeText(mContext,"û<><C3BB><EFBFBD><EFBFBD>",Toast.LENGTH_SHORT).show();
}
num ++;
if (num <= getChapters().size() || mBookUtil.chaptCached(num)){
preChaptPages =currentChaptPages;
currentChapter = num;
mBookUtil.setChapterNo(currentChapter);
currentChaptPages = loadCurrentChapt(currentChapter ) ;
// currentPage = getPageForBegin(0);
// currentPage(true);
}
}
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>
public int getCurrentChapter(){
/*int num = 0;
for (int i = 0;getDirectoryList().size() > i;i++){
Chapter bookCatalogue = getDirectoryList().get(i);
if (currentPage.getEnd() >= bookCatalogue.getBookChapterStartPos()){
num = i;
}else{
break;
}
}
return num;
*/
currentChapter = mBookUtil.getChapterNo();
// Log.d(TAG, String.format(" prepare book onDraw chapter after mBookUtil.getChapterNo(),currentChapter %s ",currentChapter ) );
return currentChapter ;
}
//<2F><><EFBFBD>Ƶ<EFBFBD>ǰҳ<C7B0><D2B3>
public void currentPage(Boolean updateChapter){
// Log.d(TAG, "prepare book currentPage: to open current Page : ");
onDraw(mBookPageWidget.getCurPage(),currentPage.getLines(),updateChapter);
onDraw(mBookPageWidget.getNextPage(),currentPage.getLines(),updateChapter);
}
//<2F><><EFBFBD>µ<EFBFBD><C2B5><EFBFBD>
public void updateBattery(int mLevel){
mAd.hideSystemUI();
if (currentPage != null && mBookPageWidget != null && !mBookPageWidget.isRunning()) {
if (level != mLevel) {
level = mLevel;
currentPage(false);
}
}
}
public void updateTime(){
mAd.hideSystemUI();
if (currentPage != null && mBookPageWidget != null && !mBookPageWidget.isRunning()) {
String mDate = sdf.format(new java.util.Date());
if (date != mDate) {
date = mDate;
currentPage(false);
}
}
}
//<2F>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>
public void changeProgress(float progress){
long begin = (long) (mBookUtil.getBookLen() * progress);
currentPage = getPageForBegin(begin);
currentPage(true);
}
private void changeChapter(int chapNum, List<TRPage> chaptPages){
if (mBook == null) {
return;
}
//preChaptPages =currentChaptPages;
currentChapter = chapNum;
mBookUtil.setChapterNo(currentChapter);
currentChaptPages = chaptPages ;
if(isShowPrePage) {
currentPage = currentChaptPages.get(currentChaptPages.size()-1);
}else {
currentPage = getPageForBegin(openBookPosition);
}
openBookPosition =0;//<2F><><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><E9B1BE>λ<EFBFBD><CEBB><EFBFBD>ϴζ<CFB4><CEB6><EFBFBD>λ<EFBFBD>ã<EFBFBD><C3A3>Ժ<EFBFBD><D4BA>½<EFBFBD><C2BD>л<EFBFBD><D0BB>Ͷ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD>½<EFBFBD><C2BD><EFBFBD>ҳ
currentPage(true);
}
//<2F>ı<EFBFBD><C4B1>½<EFBFBD>
public void changeChapter(int chapNum){
if (mBook == null) {
return;
}
// preChaptPages =currentChaptPages;
currentChapter = chapNum;
mBookUtil.setChapterNo(currentChapter);
currentChaptPages = loadCurrentChapt(currentChapter ) ;
// currentPage = getPageForBegin(0);
// currentPage(true);
}
public void openBookmark(int chapNum,long position){
//preChaptPages =currentChaptPages;
currentChapter = chapNum;
mBookUtil.setChapterNo(currentChapter);
currentChaptPages = loadCurrentChapt(currentChapter ) ;
openBookPosition = position;
// currentPage = getPageForBegin(position);
// currentPage(true);
}
//<2F>ı<EFBFBD><C4B1>м<EFBFBD><D0BC><EFBFBD>
public void changeLineHight(int lineSpace) {
this.lineSpace = lineSpace;
mPaint.setTextSize(m_fontSize);
calculateLineCount();
measureMarginWidth();
currentChaptPages = loadCurrentChapt(currentChapter );
openBookPosition = currentPage.getBegin();
// currentPage = getPageForBegin(currentPage.getBegin());
// currentPage(true);
}
//<2F>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
public void changeFontSize(int fontSize){
this.m_fontSize = fontSize;
mPaint.setTextSize(m_fontSize);
calculateLineCount();
measureMarginWidth();
currentChaptPages = loadCurrentChapt(currentChapter );
openBookPosition = currentPage.getBegin();
// currentPage = getPageForBegin(currentPage.getBegin());
// currentPage(true);
}
//<2F>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>
public void changeTypeface(Typeface typeface){
this.typeface = typeface;
mPaint.setTypeface(typeface);
mBatterryPaint.setTypeface(typeface);
calculateLineCount();
measureMarginWidth();
//openBookPosition = currentPage.getBegin();
currentPage = getPageForBegin(currentPage.getBegin());
currentPage(true);
}
//<2F>ı<C4B1><E4B1B3>
public void changeBookBg(int type){
setBookBg(type);
currentPage(false);
}
//<2F><><EFBFBD><EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>
public void setBookBg(int type){
Bitmap bitmap = Bitmap.createBitmap(mWidth,mHeight, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
int color = 0;
switch (type){
case Config.BOOK_BG_DEFAULT:
canvas = null;
bitmap.recycle();
if (getBgBitmap() != null) {
getBgBitmap().recycle();
}
bitmap = BitmapUtil.decodeSampledBitmapFromResource(
mContext.getResources(), R.drawable.paper, mWidth, mHeight);
color = mContext.getResources().getColor(R.color.read_font_default);
setBookPageBg(mContext.getResources().getColor(R.color.read_bg_default));
break;
case Config.BOOK_BG_1:
canvas.drawColor(mContext.getResources().getColor(R.color.read_bg_1));
color = mContext.getResources().getColor(R.color.read_font_1);
setBookPageBg(mContext.getResources().getColor(R.color.read_bg_1));
break;
case Config.BOOK_BG_2:
canvas.drawColor(mContext.getResources().getColor(R.color.read_bg_2));
color = mContext.getResources().getColor(R.color.read_font_2);
setBookPageBg(mContext.getResources().getColor(R.color.read_bg_2));
break;
case Config.BOOK_BG_3:
canvas.drawColor(mContext.getResources().getColor(R.color.read_bg_3));
color = mContext.getResources().getColor(R.color.read_font_3);
if (mBookPageWidget != null) {
mBookPageWidget.setBgColor(mContext.getResources().getColor(R.color.read_bg_3));
}
break;
case Config.BOOK_BG_4:
canvas.drawColor(mContext.getResources().getColor(R.color.read_bg_4));
color = mContext.getResources().getColor(R.color.read_font_4);
setBookPageBg(mContext.getResources().getColor(R.color.read_bg_4));
break;
}
setBgBitmap(bitmap);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
setM_textColor(color);
}
public void setBookPageBg(int color){
if (mBookPageWidget != null) {
mBookPageWidget.setBgColor(color);
}
}
//<2F><><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD><EFBFBD>ҹ<EFBFBD><D2B9>ģʽ
public void setDayOrNight(Boolean isNgiht){
initBg(isNgiht);
currentPage(false);
}
public void clear(){
Log.d(TAG, String .format("prepare Book: clearing book info %s" , getNovle().getName()));
fileRetryCnt.clear();
//mBookUtil=null;
currentChapter = 0;
bookPath = "";
bookName = "";
mBook = null;
mBookPageWidget = null;
mPageEvent = null;
cancelPage = null;
prePage = null;
currentPage = null;
currentChaptPages =null;
// preChaptPages=null;
// nextChaptPages=null;
}
public static Status getStatus(){
return mStatus;
}
public long getBookLen(){
return mBookUtil.getBookLen();
}
public TRPage getCurrentPage(){
return currentPage;
}
//<2F><>ȡ<EFBFBD><EFBFBD><E9B1BE><EFBFBD><EFBFBD>
public List<Chapter> getChapters(){
return mBookUtil.getChapters();
}
public boolean isReadingCatalogs(){
return mBookUtil.isReadingCatalogs();
}
public String getBookPath(){
return bookPath;
}
public String getBookName(){
return getNovle().getName();
}
public Novel getNovle(){
return mBookUtil.getNovel();
}
public boolean isWorking(){
return mBookUtil !=null;
}
//<2F>Ƿ<EFBFBD><C7B7>ǵ<EFBFBD>һҳ
public boolean isfirstPage() {
return m_isfirstPage;
}
//ѯ<><D1AF><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3>Ķ<EFBFBD>
public boolean canExitSilent(){
return m_islastPage
|| mStatus==Status.FAIL
||mStatus==Status.NETWORKFAILE
|| mStatus==Status.SERVERERROR;
}
//<2F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һҳ
public boolean islastPage() {
return m_islastPage;
}
//<2F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>Է<EFBFBD>ҳ
public boolean isReady() {
return mStatus==Status.FINISH;
}
//<2F><><EFBFBD><EFBFBD>ҳ<EFBFBD><EFBFBD><E6B1B3>
public void setBgBitmap(Bitmap BG) {
m_book_bg = BG;
}
//<2F><><EFBFBD><EFBFBD>ҳ<EFBFBD><EFBFBD><E6B1B3>
public Bitmap getBgBitmap() {
return m_book_bg;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
public void setM_textColor(int m_textColor) {
this.m_textColor = m_textColor;
}
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
public int getTextColor() {
return this.m_textColor;
}
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD>ִ<EFBFBD>С
public float getFontSize() {
return this.m_fontSize;
}
public void setPageWidget(PageWidget mBookPageWidget){
this.mBookPageWidget = mBookPageWidget;
}
public PageWidget getPageWidget( ){
return mBookPageWidget;
}
public void setPageEvent(PageEvent pageEvent){
this.mPageEvent = pageEvent;
}
public interface PageEvent{
void changeProgress(float progress);
}
}