多线程问题

This commit is contained in:
mwang 2019-04-07 23:49:08 +08:00
parent bd17a920c0
commit b3eb69339a
12 changed files with 342 additions and 138 deletions

View File

@ -313,7 +313,7 @@ public class BookActivity extends Activity_base {
handler.sendEmptyMessage(1); handler.sendEmptyMessage(1);
} }
pageFactory.prepareBook(mNovel, BookActivity.this); pageFactory.prepareBook(mNovel );
// Toast.makeText(BookActivity.this,"Novel 请求成功:"+result,Toast.LENGTH_SHORT).show(); // Toast.makeText(BookActivity.this,"Novel 请求成功:"+result,Toast.LENGTH_SHORT).show();
} }
@ -325,7 +325,7 @@ public class BookActivity extends Activity_base {
mNovel = Novel.getNovelBySvrId(novelId); mNovel = Novel.getNovelBySvrId(novelId);
if(null != mNovel) { if(null != mNovel) {
handler.sendEmptyMessage(1); handler.sendEmptyMessage(1);
pageFactory.prepareBook(mNovel, BookActivity.this); pageFactory.prepareBook(mNovel );
} }

View File

@ -157,7 +157,8 @@ public abstract class BasicFragment extends Fragment {
nv= LitePal.find(Novel.class,id); nv= LitePal.find(Novel.class,id);
PageFactory.getInstance().prepareBook(nv, getActivity()); PageFactory.getInstance().prepareBook(nv ); //打开本地小说内容
ReadActivity.openBook(nv ,activity);
} }
@ -165,7 +166,8 @@ public abstract class BasicFragment extends Fragment {
public void onFault(String errorMsg) { public void onFault(String errorMsg) {
//失败 //失败
Toast.makeText(getActivity(),"Novel 请求失败:"+errorMsg,Toast.LENGTH_SHORT).show(); Toast.makeText(getActivity(),"Novel 请求失败:"+errorMsg,Toast.LENGTH_SHORT).show();
PageFactory.getInstance().prepareBook(novel, getActivity()); //打开本地小说内容 PageFactory.getInstance().prepareBook(novel ); //打开本地小说内容
ReadActivity.openBook(novel ,activity);
} }
},getActivity())); },getActivity()));
@ -183,7 +185,7 @@ public abstract class BasicFragment extends Fragment {
if(!book.isLocalBook()){ if(!book.isLocalBook()){
getBookInfo(book); getBookInfo(book);
ReadActivity.openBook(book ,activity);
return; return;
} }

View File

@ -1,5 +1,6 @@
package com.novelbook.android.Fragments; package com.novelbook.android.Fragments;
import android.app.ProgressDialog;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -30,7 +31,20 @@ public class CatalogFragment extends BasicFragment {
@BindView(R.id.lv_catalogue) @BindView(R.id.lv_catalogue)
ListView lv_catalogue; ListView lv_catalogue;
private ProgressDialog progressDialog;
private void showProgressDialog() {
if ( null == progressDialog) {
progressDialog =new ProgressDialog(getActivity());
}
progressDialog.show();
}
private void dismissProgressDialog() {
if ( null != progressDialog) {
progressDialog.dismiss();
}
}
@Override @Override
protected int getLayoutRes() { protected int getLayoutRes() {
return R.layout.fragment_catalog; return R.layout.fragment_catalog;
@ -50,7 +64,9 @@ public class CatalogFragment extends BasicFragment {
lv_catalogue.setOnItemClickListener(new AdapterView.OnItemClickListener() { lv_catalogue.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
showProgressDialog();
pageFactory.changeChapter(position+1); pageFactory.changeChapter(position+1);
dismissProgressDialog();
getActivity().finish(); getActivity().finish();
} }
}); });

View File

@ -2,6 +2,7 @@ package com.novelbook.android;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
@ -24,6 +25,7 @@ import android.view.View;
import android.view.WindowManager; import android.view.WindowManager;
import android.view.animation.Animation; import android.view.animation.Animation;
import android.view.animation.AnimationUtils; import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.SeekBar; import android.widget.SeekBar;
@ -40,6 +42,7 @@ import com.novelbook.android.db.Novel;
import com.novelbook.android.db.BookMarks; import com.novelbook.android.db.BookMarks;
import com.novelbook.android.dialog.PageModeDialog; import com.novelbook.android.dialog.PageModeDialog;
import com.novelbook.android.dialog.SettingDialog; import com.novelbook.android.dialog.SettingDialog;
import com.novelbook.android.utils.AdInterface;
import com.novelbook.android.utils.BrightnessUtil; import com.novelbook.android.utils.BrightnessUtil;
import com.novelbook.android.utils.PageFactory; import com.novelbook.android.utils.PageFactory;
import com.novelbook.android.view.PageWidget; import com.novelbook.android.view.PageWidget;
@ -57,7 +60,7 @@ import butterknife.BindView;
import butterknife.OnClick; import butterknife.OnClick;
public class ReadActivity extends Activity_base implements SpeechSynthesizerListener { public class ReadActivity extends Activity_base implements SpeechSynthesizerListener , AdInterface {
private static final String TAG = "ReadActivity"; private static final String TAG = "ReadActivity";
private final static String EXTRA_BOOK = "book"; private final static String EXTRA_BOOK = "book";
// private final static String EXTRA_CHAPTER = "chapter"; // private final static String EXTRA_CHAPTER = "chapter";
@ -172,6 +175,7 @@ public class ReadActivity extends Activity_base implements SpeechSynthesizerLis
config = Config.getInstance(); config = Config.getInstance();
pageFactory = PageFactory.getInstance(); pageFactory = PageFactory.getInstance();
pageFactory.setAd(this);
IntentFilter mfilter = new IntentFilter(); IntentFilter mfilter = new IntentFilter();
mfilter.addAction(Intent.ACTION_BATTERY_CHANGED); mfilter.addAction(Intent.ACTION_BATTERY_CHANGED);
@ -322,9 +326,13 @@ public class ReadActivity extends Activity_base implements SpeechSynthesizerLis
@Override @Override
public Boolean prePage() { public Boolean prePage() {
if (isShow || isSpeaking){ if (isShow) {
hideReadSetting();
return false; return false;
} }
if (isShow || isSpeaking){
// return false;
}
pageFactory.prePage(); pageFactory.prePage();
if (pageFactory.isfirstPage()) { if (pageFactory.isfirstPage()) {
@ -337,9 +345,13 @@ public class ReadActivity extends Activity_base implements SpeechSynthesizerLis
@Override @Override
public Boolean nextPage() { public Boolean nextPage() {
Log.e("setTouchListener", "nextPage"); Log.e("setTouchListener", "nextPage");
if (isShow || isSpeaking){ if (isShow) {
hideReadSetting();
return false; return false;
} }
if (isShow || isSpeaking){
// return false;
}
pageFactory.nextPage(); pageFactory.nextPage();
if (pageFactory.islastPage()) { if (pageFactory.islastPage()) {
@ -708,7 +720,9 @@ public class ReadActivity extends Activity_base implements SpeechSynthesizerLis
*/ */
} }
@OnClick({R.id.tv_progress, R.id.rl_progress, R.id.tv_pre, R.id.sb_progress, R.id.tv_next, R.id.tv_directory, R.id.tv_dayornight,R.id.tv_pagemode, R.id.tv_setting, R.id.bookpop_bottom, R.id.rl_bottom,R.id.tv_stop_read}) @OnClick({R.id.tv_progress, R.id.rl_progress, R.id.tv_pre, R.id.sb_progress, R.id.tv_next, R.id.tv_directory,
R.id.tv_dayornight,R.id.tv_pagemode, R.id.tv_setting, R.id.bookpop_bottom, R.id.rl_bottom,R.id.tv_stop_read
,R.id.llTopAd})
public void onClick(View view) { public void onClick(View view) {
switch (view.getId()) { switch (view.getId()) {
// case R.id.btn_return: // case R.id.btn_return:
@ -753,10 +767,36 @@ public class ReadActivity extends Activity_base implements SpeechSynthesizerLis
isSpeaking = false; isSpeaking = false;
hideReadSetting(); hideReadSetting();
} }
case R.id.llTopAd:
Toast.makeText(this,"ad is clicked ",Toast.LENGTH_LONG).show();
Log.d(TAG,"Ad is clicked");
showProgressDialog();// sleep 结束后才显示dismiss 不工作
/* try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
dismissProgressDialog();
break; break;
} }
} }
ProgressDialog progressDialog;
private void showProgressDialog() {
if ( null == progressDialog) {
progressDialog =ProgressDialog.show(this,"测试","你点了广告",false,true); //new ProgressDialog(this);
}else{
progressDialog.show();
}
}
private void dismissProgressDialog() {
if ( null != progressDialog) {
progressDialog.dismiss();
}
}
/* /*
* @param arg0 * @param arg0
*/ */
@ -838,4 +878,19 @@ public class ReadActivity extends Activity_base implements SpeechSynthesizerLis
Log.e(TAG,error.description); Log.e(TAG,error.description);
} }
final int contentAdHight=350;
@Override
public void showAd(int adHeight,int adY) {
llTopAd.setVisibility(View.GONE);
if(adHeight >contentAdHight){
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) llTopAd.getLayoutParams();
params.setMargins(20, adY, 20, 10);
llTopAd.setLayoutParams(params);
llTopAd.setVisibility(View.VISIBLE);
}
}
} }

View File

@ -244,11 +244,12 @@ public class HttpMethods {
public Response intercept(Chain chain) throws IOException { public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request()); Response originalResponse = chain.proceed(chain.request());
String cacheControl = originalResponse.header("Cache-Control"); String cacheControl = originalResponse.header("Cache-Control");
int maxAge =60*60; //一小时
if (cacheControl == null || cacheControl.contains("no-store") || cacheControl.contains("no-cache") || if (cacheControl == null || cacheControl.contains("no-store") || cacheControl.contains("no-cache") ||
cacheControl.contains("must-revalidate") || cacheControl.contains("max-age=0")) { cacheControl.contains("must-revalidate") || cacheControl.contains("max-age=0")) { //目标网站禁用cache则设置为1小时
return originalResponse.newBuilder() return originalResponse.newBuilder()
.removeHeader("Pragma") .removeHeader("Pragma")
.header("Cache-Control", "public, max-age=" + 5000) .header("Cache-Control", "public, max-age=" + maxAge)
.build(); .build();
} else { } else {
return originalResponse; return originalResponse;
@ -261,9 +262,10 @@ public class HttpMethods {
public Response intercept(Chain chain) throws IOException { public Response intercept(Chain chain) throws IOException {
Request request = chain.request(); Request request = chain.request();
if (!NetUtil.isNetworkConnected()) { if (!NetUtil.isNetworkConnected()) {
int maxStale = 60 * 60 * 24 * 28;
request = request.newBuilder() request = request.newBuilder()
.removeHeader("Pragma") .removeHeader("Pragma")
.header("Cache-Control", "public, only-if-cached") .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.build(); .build();
} }
return chain.proceed(request); return chain.proceed(request);

View File

@ -50,8 +50,10 @@ public class OnSuccessAndFaultSub extends DisposableObserver<ResponseBody>
*/ */
public OnSuccessAndFaultSub(OnSuccessAndFaultListener mOnSuccessAndFaultListener, Context context) { public OnSuccessAndFaultSub(OnSuccessAndFaultListener mOnSuccessAndFaultListener, Context context) {
this.mOnSuccessAndFaultListener = mOnSuccessAndFaultListener; this.mOnSuccessAndFaultListener = mOnSuccessAndFaultListener;
this.context = context; if(context!=null) {
progressDialog = new ProgressDialog(context); this.context = context;
progressDialog = new ProgressDialog(context);
}
} }

View File

@ -0,0 +1,10 @@
package com.novelbook.android.utils;
public interface AdInterface {
/**
* 显示activity 上的广告
* @param height
* @param adY
*/
public void showAd(int height,int adY);
}

View File

@ -133,7 +133,7 @@ public class BookUtil {
} }
mNovel.setDomain(mSite.getDomain()); mNovel.setDomain(mSite.getDomain());
mNovel.setMuluUrl(mSite.getMuluUrl()); mNovel.setMuluUrl(mSite.getMuluUrl());
mNovel.save(); mNovel.update(mNovel.getId());
} }
public void getTargetSites(){ public void getTargetSites(){
@ -151,15 +151,15 @@ public class BookUtil {
e.printStackTrace(); e.printStackTrace();
} }
Toast.makeText(mContext,"getMuluInfo 请求成功 " ,Toast.LENGTH_SHORT).show(); // Toast.makeText(mContext,"getMuluInfo 请求成功 " ,Toast.LENGTH_SHORT).show();
} }
@Override @Override
public void onFault(String errorMsg) { public void onFault(String errorMsg) {
//失败 //失败
Toast.makeText(mContext,"getMuluInfo 请求失败"+errorMsg,Toast.LENGTH_SHORT).show(); // Toast.makeText(mContext,"getMuluInfo 请求失败"+errorMsg,Toast.LENGTH_SHORT).show();
} }
},mContext)); },null));
} }
private void getSiteRule() { private void getSiteRule() {
mSiteRule = null; mSiteRule = null;
@ -235,6 +235,7 @@ public class BookUtil {
progressDialog =new ProgressDialog(mContext); progressDialog =new ProgressDialog(mContext);
} }
progressDialog.show(); progressDialog.show();
// progressDialog.show(mContext,"网络不给力","正努力加载",false,true);
} }
@ -260,7 +261,7 @@ public class BookUtil {
boolean isLocalImport = TextUtils.isEmpty( novel.getNovelId()); boolean isLocalImport = TextUtils.isEmpty( novel.getNovelId());
boolean isOnShelf = isLocalImport || novel.isOnShelf(); boolean isOnShelf = isLocalImport || novel.isOnShelf();
boolean isLoadChaptsFromRemote = !isLocalImport ;// && !novel.isFinished() ; //是否从目标网站下载目录 boolean isLoadChaptsFromRemote = !isLocalImport ;// && !novel.isFinished() ; //是否从目标网站下载目录
showProgressDialog(); // showProgressDialog();
if(isLocalImport) { if(isLocalImport) {
mChapters = LitePal.where("novelId=?", mNovel.getId() + "").find(Chapter.class); mChapters = LitePal.where("novelId=?", mNovel.getId() + "").find(Chapter.class);
@ -287,12 +288,13 @@ public class BookUtil {
Thread.sleep(50); Thread.sleep(50);
Log.d(TAG,String.format("waiting for mulu downloading ,mMuluStatus %s" ,mMuluStatus)); Log.d(TAG,String.format("waiting for mulu downloading ,mMuluStatus %s" ,mMuluStatus));
if(mMuluStatus == MuluStatus.failed){ if(mMuluStatus == MuluStatus.failed){
dismissProgressDialog();
throw new RuntimeException("读取资源失败,请检查网络"); throw new RuntimeException("读取资源失败,请检查网络");
} }
} }
} }
dismissProgressDialog(); // dismissProgressDialog();
} }
// String getMuluUrl() { // String getMuluUrl() {
@ -500,7 +502,12 @@ public class BookUtil {
} }
public char chaptCurrent(){ public char chaptCurrent(){
char[] charArray = chaptChars(chapterNo); char[] charArray = chaptChars(chapterNo);
return charArray[(int)position-1]; return charArray[(int)position-1];
} }
public char current(){ public char current(){
@ -750,7 +757,7 @@ public class BookUtil {
return cachedPath + mNovel.getName() + index ; return cachedPath + mNovel.getName() + index ;
} }
protected String fileChapterName(int chaptId ) { protected String fileChapterName(int chaptId ) {
if(TextUtils.isEmpty(mNovel.getDomain())){ if(!TextUtils.isEmpty(mNovel.getDomain())){
return getChapterPath() +mNovel.getDomain()+"/"+ chaptId ; return getChapterPath() +mNovel.getDomain()+"/"+ chaptId ;
} }
@ -799,21 +806,27 @@ public class BookUtil {
} }
boolean isDownloadChapt =false; boolean isDownloadChapt =false;
synchronized boolean getDownloadStatus(){
void setDownloadFlag(boolean flag){ return isDownloadChapt;
}
synchronized void setDownloadFlag(boolean flag){
isDownloadChapt = flag; isDownloadChapt = flag;
Log.d("loadChaptContent",String.format("set download flat",isDownloadChapt) );
} }
Handler handler = new Handler() { final Handler handler = new Handler() {
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
int wt = msg.what; int wt = msg.what;
dismissProgressDialog();
if (msg.what == 123) { if (msg.what == 123) {
isDownloadChapt =true; isDownloadChapt =true;
Log.d("loadChaptContent",String.format("handler msg, download %s",isDownloadChapt) );
}else if(msg.what==1){ }else if(msg.what==1){
isDownloadChapt =true;
Toast.makeText(mContext,"网络错误",Toast.LENGTH_LONG).show(); Toast.makeText(mContext,"网络错误",Toast.LENGTH_LONG).show();
} }
@ -842,22 +855,34 @@ public class BookUtil {
Log.d(TAG,String.format("loadChaptContent----start %s" ,new Date().toString() )); Log.d(TAG,String.format("loadChaptContent----start %s" ,new Date().toString() ));
loadChaptContent(index); //showProgressDialog();//why not show
// showProgressDialog();//why not show Log.d( "loadChaptContent",String.format("begin to load content for chapter %s",index));
Log.d( "loadChaptContent",String.format("isDownloadChapt: %s",isDownloadChapt));
if(getDownloadStatus() ) {
loadChaptContent(index);
}
Log.d( "loadChaptContent",String.format("showing dialog " ));
// Log.d(TAG,String.format("showing progress diaglog......"));
int maxSleep =6000;
int slepttime =0; int slepttime =0;
while(!isDownloadChapt){ // while(!file.exists() && !getDownloadStatus()){//&& slepttime <maxSleep){
while( !getDownloadStatus() && slepttime <maxSleep){
Thread.sleep(50); Thread.sleep(50);
slepttime+=50; slepttime+=50;
Log.d(TAG,String.format("loadChaptContent slept %s for downloading ",slepttime ) ); Log.d("loadChaptContent",String.format("loadChaptContent slept %s for downloading,isDownload %s ",slepttime,getDownloadStatus() ) );
} }
// dismissProgressDialog(); Log.d("loadChaptContent",String.format("loadChaptContent slept %s for downloading ",slepttime ) );
// Log.d(TAG,String.format("loadChaptContent slept %s for downloading %s ",slepttime, mChapters.get(index -1).getChapterName() )); Log.d( "loadChaptContent",String.format("dismissing dialog " ));
dismissProgressDialog();
Log.d(TAG,String.format("loadChaptContent slept %s for downloading chaptercontent ",slepttime ));
} }
Log.d( "loadChaptContent",String.format(" %s, file.exists()? %s", file.getPath(),file.exists()));
if(!file.exists()) { if(!file.exists()) {
String error = "网络错误";
String error = "万里长城";
return error.toCharArray(); return error.toCharArray();
} }
if(mChapters.size() > index ) { if(mChapters.size() > index ) {
@ -893,6 +918,8 @@ public class BookUtil {
e.printStackTrace(); e.printStackTrace();
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
}finally {
dismissProgressDialog();
} }
Cache cache = new Cache(); Cache cache = new Cache();
cache.setSize(block.length); cache.setSize(block.length);
@ -920,6 +947,7 @@ private void loadChaptContent(int index) throws JSONException, InterruptedExcept
setDownloadFlag(false); setDownloadFlag(false);
Log.d( "loadChaptContent",String.format("loadChaptContent isDownloadChapt: %s",isDownloadChapt));
JSONObject siteJson = new JSONObject(); JSONObject siteJson = new JSONObject();
siteJson.put("chapterContentRegex", mSiteRule.getChapterContentRegex()); siteJson.put("chapterContentRegex", mSiteRule.getChapterContentRegex());
siteJson.put("chapterContentDumpRegex", mSiteRule.getChapterContentDumpRegex()); siteJson.put("chapterContentDumpRegex", mSiteRule.getChapterContentDumpRegex());
@ -928,9 +956,11 @@ private void loadChaptContent(int index) throws JSONException, InterruptedExcept
@Override @Override
public void onFailure(Call call, IOException e) { public void onFailure(Call call, IOException e) {
handler.sendEmptyMessage(123); handler.sendEmptyMessage(123);
handler.sendEmptyMessage(1);
setDownloadFlag(true); setDownloadFlag(true);
Log.d(TAG, "loadChaptContent---- onFailure: " + e.getMessage()); Log.d( "loadChaptContent",String.format("loadChaptContent fail, isDownloadChapt: %s",isDownloadChapt));
throw new RuntimeException("Error during writing " + fileChapterName( index)); e.printStackTrace();
// throw new RuntimeException("Error during writing " + fileChapterName( index));
} }
@Override @Override
@ -938,8 +968,9 @@ private void loadChaptContent(int index) throws JSONException, InterruptedExcept
ResponseBody body = response.body(); ResponseBody body = response.body();
if (body != null ) { if (body != null ) {
if(response.code()!=200){ if(response.code()!=200){
Log.d(TAG, "loadChaptContent----network failure returnCode " + response.code());
setDownloadFlag(true); setDownloadFlag(true);
Log.d( "loadChaptContent",String.format("loadChaptContent error %s ,isDownloadChapt: %s", response.code(),isDownloadChapt));
handler.sendEmptyMessage(1); handler.sendEmptyMessage(1);
return; return;
} }
@ -955,6 +986,9 @@ private void loadChaptContent(int index) throws JSONException, InterruptedExcept
final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(fileChapterName(index)), charachterType);//"UTF-16LE"); // UTF-16LE utf-8 文件小 final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(fileChapterName(index)), charachterType);//"UTF-16LE"); // UTF-16LE utf-8 文件小
writer.write(buf); writer.write(buf);
writer.close(); writer.close();
Log.d( "loadChaptContent",String.format("loadChaptContent file created: %s", file.getPath()));
handler.sendEmptyMessage(123);
setDownloadFlag(true);
} catch (IOException | JSONException e) { } catch (IOException | JSONException e) {
e.printStackTrace(); e.printStackTrace();
throw new RuntimeException("Error during writing " + fileChapterName( index)); throw new RuntimeException("Error during writing " + fileChapterName( index));
@ -963,12 +997,12 @@ private void loadChaptContent(int index) throws JSONException, InterruptedExcept
body.close(); body.close();
handler.sendEmptyMessage(123); handler.sendEmptyMessage(123);
setDownloadFlag(true); setDownloadFlag(true);
} }
chapter.setNovelId(mNovel.getId()); chapter.setNovelId(mNovel.getId());
chapter.setChapterPath(fileChapterName(index)); chapter.setChapterPath(fileChapterName(index));
chapter.save(); chapter.save();
setDownloadFlag(true);
Log.d(TAG,String.format("loadChaptContent---- finished download %s, cost time %s ,content path %s ", chapter.getChapterName(), new Date().getTime() -startTime ,chapter.getChapterPath() )); Log.d(TAG,String.format("loadChaptContent---- finished download %s, cost time %s ,content path %s ", chapter.getChapterName(), new Date().getTime() -startTime ,chapter.getChapterPath() ));
} }

View File

@ -163,11 +163,16 @@ public class PageFactory {
private List<TRPage> nextChaptPages; private List<TRPage> nextChaptPages;
private List<TRPage> preChaptPages; private List<TRPage> preChaptPages;
private AdInterface mAd;
public void setAd(AdInterface ad){
mAd =ad;
}
private List<TRPage> loadCurrentChapt(int chaptId){ private List<TRPage> loadCurrentChapt(int chaptId){
List<TRPage> chaptPages = new ArrayList<TRPage>(); List<TRPage> chaptPages = new ArrayList<TRPage>();
char[] chars = mBookUtil.chaptChars(chaptId); char[] chars = mBookUtil.chaptChars(chaptId);
mBookUtil.setBookLen(chars.length); mBookUtil.setBookLen(chars.length);
mBookUtil.setChapterNo(chaptId); mBookUtil.setChapterNo(chaptId);
// TRPage page = new TRPage(); // TRPage page = new TRPage();
@ -183,11 +188,14 @@ public class PageFactory {
// break; // break;
// } // }
page.setChapterNo(chaptId);
page.setLastPage(page.getEnd()==chars.length);
page.setFirstPage(pageNo==1);
page.setPageNo(pageNo); page.setPageNo(pageNo);
chaptPages.add(page); chaptPages.add(page);
length= page.getEnd(); length= page.getEnd();
} }
return chaptPages; return chaptPages;
} }
public TRPage getNextChapterPage(long position){ public TRPage getNextChapterPage(long position){
@ -359,11 +367,18 @@ public class PageFactory {
@Override @Override
public void run() { public void run() {
super.run(); super.run();
if(mBook.getLastReadChapt() !=currentChapter || mBook.getLastReadPos()!=currentPage.getBegin()) {
mBook.setLastReadChapt(currentChapter);
mBook.setLastReadPos(currentPage.getBegin());
mBook.update(mBook.getId());
}
/*
values.put("lastReadPos",currentPage.getBegin()); values.put("lastReadPos",currentPage.getBegin());
values.put("lastReadChapt",currentChapter); values.put("lastReadChapt",currentChapter);
Log.d(TAG,String.format("begin to update book %s chapter%s bigin %s ",mBook.getName(),currentChapter, currentPage.getBegin() ) ); 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()); 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) ); Log.d(TAG,String.format("update book %s chapter%s bigin %s, result %s",mBook.getName(),currentChapter, currentPage.getBegin(),rows) );
*/
} }
}.start(); }.start();
} }
@ -379,6 +394,7 @@ public class PageFactory {
} }
float space =m_fontSize + lineSpace; float space =m_fontSize + lineSpace;
paragraphSpace = prate * lineSpace; paragraphSpace = prate * lineSpace;
if (m_lines.size() > 0) { if (m_lines.size() > 0) {
float y = marginHeight; float y = marginHeight;
for (String strLine : m_lines) { for (String strLine : m_lines) {
@ -399,6 +415,12 @@ public class PageFactory {
c.drawText(strLine, measureMarginWidth, y, mPaint); c.drawText(strLine, measureMarginWidth, y, mPaint);
// word.append(strLine); // word.append(strLine);
} }
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));
} }
//画进度及时间 //画进度及时间
@ -460,8 +482,19 @@ public class PageFactory {
mBookPageWidget.postInvalidate(); mBookPageWidget.postInvalidate();
} }
//向前翻页 private void showAd(int adHeight,int adY) {
if(mAd!=null){
mAd.showAd(adHeight,adY);
}
}
//向前翻页
public void prePage(){ public void prePage(){
if(null == currentPage){
return;
}
m_isfirstPage = false; m_isfirstPage = false;
if (currentPage.getBegin() <= 1) { if (currentPage.getBegin() <= 1) {
Log.d(TAG,"当前是本章第一页"); Log.d(TAG,"当前是本章第一页");
@ -481,6 +514,9 @@ public class PageFactory {
//向后翻页 //向后翻页
public void nextPage(){ public void nextPage(){
if(null == currentPage){
return;
}
m_islastPage = false; m_islastPage = false;
if (currentPage.getEnd() >= mBookUtil.getBookLen()) { if (currentPage.getEnd() >= mBookUtil.getBookLen()) {
Log.d(TAG,"已经是本章最后一页了"); Log.d(TAG,"已经是本章最后一页了");
@ -505,15 +541,24 @@ public class PageFactory {
//取消翻页 //取消翻页
public void cancelPage(){ public void cancelPage(){
if(cancelPage.isLastPage() || cancelPage.isFirstPage()){
mBookUtil.setChapterNo(cancelPage.getChapterNo());
currentChaptPages = loadCurrentChapt(cancelPage.getChapterNo());
currentPage = getPageForBegin(cancelPage.getBegin()) ;// currentChaptPages.get(0);
// currentPage = getPageForBegin(begin);
if (mBookPageWidget != null) {
currentPage(true);
}
}
currentPage = cancelPage; currentPage = cancelPage;
} }
public void prepareBook(Novel book, Context context){ public void prepareBook(Novel book){
if(mBook!=null &&mBook.getNovelId() !=book.getNovelId()){ //取消未上本书完成的web请求待验证效果 if(mBook!=null &&mBook.getNovelId() !=book.getNovelId()){ //取消未上本书完成的web请求待验证效果
NetUtil.cancelRequest(mBook.getNovelId()); NetUtil.cancelRequest(mBook.getNovelId());
} }
mBookUtil = new BookUtil(); mBookUtil = new BookUtil();
this.mBookUtil.setContext(context); //this.mBookUtil.setContext(context);
this.mBookUtil.setNovel(book); this.mBookUtil.setNovel(book);
this.mBookUtil.getTargetSites(); this.mBookUtil.getTargetSites();
} }
@ -525,9 +570,9 @@ public class PageFactory {
public void openBook(Novel book ,Context context) throws IOException { public void openBook(Novel book ,Context context) throws IOException {
if(book.isLocalBook() ){ //离线书籍重新初始化加载mBookUtil if(book.isLocalBook() ){ //离线书籍重新初始化加载mBookUtil
mBookUtil = new BookUtil(); mBookUtil = new BookUtil();
mBookUtil.setContext(context);
}
}
mBookUtil.setContext(context);
//清空数据 //清空数据
currentChapter = 0; currentChapter = 0;
// m_mbBufLen = 0; // m_mbBufLen = 0;
@ -645,6 +690,7 @@ public class PageFactory {
trPage.setBegin(mBookUtil.getPosition() ); trPage.setBegin(mBookUtil.getPosition() );
return trPage; return trPage;
*/ */
int prePageNo =currentPage.getPageNo()-1; int prePageNo =currentPage.getPageNo()-1;
Log.d(TAG,String.format("currentPageno %s,total pagno %s",currentPage.getPageNo(),currentChaptPages.size())); Log.d(TAG,String.format("currentPageno %s,total pagno %s",currentPage.getPageNo(),currentChaptPages.size()));
if(prePageNo <=0){ if(prePageNo <=0){

View File

@ -7,8 +7,41 @@ import java.util.List;
public class TRPage { public class TRPage {
private long begin; private long begin;
private long end; private long end;
private int pageNo;
private int chapterNo;
private boolean isLastPage;
private boolean isFirstPage;
private List<String> lines = new ArrayList<>(); private List<String> lines = new ArrayList<>();
public int getChapterNo() {
return chapterNo;
}
public void setChapterNo(int chapterNo) {
this.chapterNo = chapterNo;
}
public boolean isLastPage() {
return isLastPage;
}
public void setLastPage(boolean lastPage) {
isLastPage = lastPage;
}
public boolean isFirstPage() {
return isFirstPage;
}
public void setFirstPage(boolean firstPage) {
isFirstPage = firstPage;
}
public int getPageNo() { public int getPageNo() {
return pageNo; return pageNo;
} }
@ -17,7 +50,7 @@ public class TRPage {
this.pageNo = pageNo; this.pageNo = pageNo;
} }
private int pageNo;
public long getBegin() { public long getBegin() {
return begin; return begin;

View File

@ -219,7 +219,7 @@ public class PageWidget extends View {
if (!isMove){ if (!isMove){
cancelPage = false; cancelPage = false;
//是否点击了中间 //是否点击了中间
if (downX > mScreenWidth / 5 && downX < mScreenWidth * 4 / 5 && downY > mScreenHeight / 3 && downY < mScreenHeight * 2 / 3){ if (downX > mScreenWidth / 3 && downX < mScreenWidth * 2 / 3 && downY > mScreenHeight / 3 && downY < mScreenHeight * 2 / 3){
if (mTouchListener != null){ if (mTouchListener != null){
mTouchListener.center(); mTouchListener.center();
} }

View File

@ -4,120 +4,122 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:background="#000000"
android:background="#000000"> android:orientation="vertical">
<FrameLayout <FrameLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<com.novelbook.android.view.PageWidget <com.novelbook.android.view.PageWidget
android:id="@+id/bookpage" android:id="@+id/bookpage"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="@+id/llTopAd"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top"
android:layout_marginTop="20dp"
android:visibility="gone"
>
<ImageView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent" />
android:scaleType="centerCrop" <LinearLayout
android:src="@mipmap/side_bg" android:id="@+id/llTopAd"
android:layout_width="match_parent"
android:layout_height="150dp"
/> android:layout_marginTop="520dp"
</LinearLayout> android:visibility="visible">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@mipmap/side_bg"
/>
</LinearLayout>
</FrameLayout>
</FrameLayout>
<android.support.design.widget.AppBarLayout <android.support.design.widget.AppBarLayout
android:id="@+id/appbar" android:id="@+id/appbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:fitsSystemWindows="true" android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay" android:theme="@style/AppTheme.AppBarOverlay"
android:layout_alignParentTop="true"
android:visibility="gone"> android:visibility="gone">
<android.support.v7.widget.Toolbar <android.support.v7.widget.Toolbar
android:id="@+id/toolbar" android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" android:background="?attr/colorPrimary"
android:fitsSystemWindows="true" android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay" /> app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout> </android.support.design.widget.AppBarLayout>
<!--<LinearLayout--> <!--<LinearLayout-->
<!--android:id="@+id/ll_status"--> <!--android:id="@+id/ll_status"-->
<!--android:layout_width="match_parent"--> <!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"--> <!--android:layout_height="wrap_content"-->
<!--android:background="?attr/colorPrimary"--> <!--android:background="?attr/colorPrimary"-->
<!--android:fitsSystemWindows="true"--> <!--android:fitsSystemWindows="true"-->
<!--android:orientation="vertical"--> <!--android:orientation="vertical"-->
<!--android:visibility="invisible">--> <!--android:visibility="invisible">-->
<!--</LinearLayout>--> <!--</LinearLayout>-->
<!--<LinearLayout--> <!--<LinearLayout-->
<!--android:id="@+id/ll_top"--> <!--android:id="@+id/ll_top"-->
<!--android:layout_width="match_parent"--> <!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"--> <!--android:layout_height="wrap_content"-->
<!--android:background="?attr/colorPrimary"--> <!--android:background="?attr/colorPrimary"-->
<!--android:orientation="horizontal"--> <!--android:orientation="horizontal"-->
<!--android:layout_below="@id/ll_status"--> <!--android:layout_below="@id/ll_status"-->
<!--android:visibility="gone">--> <!--android:visibility="gone">-->
<!--<ImageButton--> <!--<ImageButton-->
<!--android:id="@+id/btn_return"--> <!--android:id="@+id/btn_return"-->
<!--android:layout_width="wrap_content"--> <!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"--> <!--android:layout_height="wrap_content"-->
<!--android:background="@mipmap/return_button"--> <!--android:background="@mipmap/return_button"-->
<!--android:layout_marginLeft="10dp"--> <!--android:layout_marginLeft="10dp"-->
<!--android:layout_gravity="center_vertical" />--> <!--android:layout_gravity="center_vertical" />-->
<!--<LinearLayout--> <!--<LinearLayout-->
<!--android:layout_width="match_parent"--> <!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"--> <!--android:layout_height="wrap_content"-->
<!--android:gravity="right"--> <!--android:gravity="right"-->
<!--android:orientation="horizontal">--> <!--android:orientation="horizontal">-->
<!--<ImageButton--> <!--<ImageButton-->
<!--android:id="@+id/btn_light"--> <!--android:id="@+id/btn_light"-->
<!--android:layout_width="wrap_content"--> <!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"--> <!--android:layout_height="wrap_content"-->
<!--android:layout_gravity="center_vertical"--> <!--android:layout_gravity="center_vertical"-->
<!--android:background="@color/black" />--> <!--android:background="@color/black" />-->
<!--<ImageButton--> <!--<ImageButton-->
<!--android:id="@+id/btn_listener_book"--> <!--android:id="@+id/btn_listener_book"-->
<!--android:layout_width="wrap_content"--> <!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"--> <!--android:layout_height="wrap_content"-->
<!--android:layout_gravity="center_vertical"--> <!--android:layout_gravity="center_vertical"-->
<!--android:layout_marginRight="10dp"--> <!--android:layout_marginRight="10dp"-->
<!--android:background="@mipmap/img_tts" />--> <!--android:background="@mipmap/img_tts" />-->
<!--</LinearLayout>--> <!--</LinearLayout>-->
<!--</LinearLayout>--> <!--</LinearLayout>-->
<RelativeLayout <RelativeLayout
android:id="@+id/rl_read_bottom" android:id="@+id/rl_read_bottom"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="200dp" android:layout_height="200dp"
android:background="@color/read_dialog_bg"
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:layout_gravity="bottom" android:layout_gravity="bottom"
android:background="@color/read_dialog_bg"
android:visibility="gone"> android:visibility="gone">
<TextView <TextView
android:id="@+id/tv_stop_read" android:id="@+id/tv_stop_read"
style="@style/setting_dialog_button" style="@style/setting_dialog_button"
android:layout_centerInParent="true" android:layout_centerInParent="true"
android:textSize="20dp" android:text="停止语音播放"
android:text="停止语音播放" /> android:textSize="20dp" />
</RelativeLayout> </RelativeLayout>
<RelativeLayout <RelativeLayout
@ -127,18 +129,20 @@
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:layout_gravity="bottom" android:layout_gravity="bottom"
android:visibility="gone"> android:visibility="gone">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">
<RelativeLayout <RelativeLayout
android:id="@+id/rl_progress" android:id="@+id/rl_progress"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal" android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="20dp" android:layout_marginBottom="20dp"
android:padding="20dp"
android:background="?attr/colorPrimary" android:background="?attr/colorPrimary"
android:padding="20dp"
android:visibility="gone"> android:visibility="gone">
<TextView <TextView
@ -146,67 +150,67 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerInParent="true" android:layout_centerInParent="true"
android:text="00.00%"
android:textColor="@color/white" android:textColor="@color/white"
android:textSize="16sp" android:textSize="16sp" />
android:text="00.00%"/>
</RelativeLayout> </RelativeLayout>
<LinearLayout <LinearLayout
android:id="@+id/bookpop_bottom"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/bookpop_bottom"
android:baselineAligned="false"
android:background="@color/read_dialog_bg" android:background="@color/read_dialog_bg"
android:baselineAligned="false"
android:orientation="vertical"> android:orientation="vertical">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="70dp" android:layout_height="70dp"
android:orientation="horizontal"> android:orientation="horizontal">
<TextView <TextView
android:id="@+id/tv_pre" android:id="@+id/tv_pre"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1" android:layout_weight="1"
android:gravity="center" android:gravity="center"
android:text="@string/read_setting_pre" android:text="@string/read_setting_pre"
android:textAlignment="center" android:textAlignment="center"
android:textSize="16sp"
android:textColor="@color/white" android:textColor="@color/white"
android:layout_gravity="center_vertical" android:textSize="16sp" />
/>
<SeekBar <SeekBar
android:id="@+id/sb_progress" android:id="@+id/sb_progress"
android:layout_width="150dp" android:layout_width="150dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" android:layout_weight="1"
android:gravity="center" android:gravity="center"
android:max="10000" android:max="10000"></SeekBar>
android:layout_gravity="center_vertical">
</SeekBar>
<TextView <TextView
android:id="@+id/tv_next" android:id="@+id/tv_next"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:gravity="center" android:layout_gravity="center_vertical"
android:layout_weight="1" android:layout_weight="1"
android:gravity="center"
android:text="@string/read_setting_next" android:text="@string/read_setting_next"
android:textAlignment="center" android:textAlignment="center"
android:textSize="16sp"
android:textColor="@color/white" android:textColor="@color/white"
android:layout_gravity="center_vertical" android:textSize="16sp" />
/>
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingBottom="5dp" android:orientation="horizontal"
android:orientation="horizontal"> android:paddingBottom="5dp">
<TextView <TextView
style="@style/text_style"
android:id="@+id/tv_directory" android:id="@+id/tv_directory"
style="@style/text_style"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
@ -215,8 +219,8 @@
android:text="@string/read_setting_directory" /> android:text="@string/read_setting_directory" />
<TextView <TextView
style="@style/text_style"
android:id="@+id/tv_dayornight" android:id="@+id/tv_dayornight"
style="@style/text_style"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
@ -225,8 +229,8 @@
android:text="@string/read_setting_night" /> android:text="@string/read_setting_night" />
<TextView <TextView
style="@style/text_style"
android:id="@+id/tv_pagemode" android:id="@+id/tv_pagemode"
style="@style/text_style"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
@ -235,8 +239,8 @@
android:text="@string/read_setting_pagemode" /> android:text="@string/read_setting_pagemode" />
<TextView <TextView
style="@style/text_style"
android:id="@+id/tv_setting" android:id="@+id/tv_setting"
style="@style/text_style"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"