2019-04-02 17:41:53 +08:00
|
|
|
package com.novelbook.android;
|
|
|
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
2019-04-03 16:21:00 +08:00
|
|
|
import android.util.Log;
|
2019-04-02 17:41:53 +08:00
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import com.novelbook.android.adapter.BookListAdapter;
|
2019-04-03 18:09:00 +08:00
|
|
|
import com.novelbook.android.db.Chapter;
|
2019-04-03 16:21:00 +08:00
|
|
|
import com.novelbook.android.db.Novel;
|
2019-04-02 17:41:53 +08:00
|
|
|
import com.novelbook.android.netsubscribe.MovieSubscribe;
|
2019-04-03 16:21:00 +08:00
|
|
|
import com.novelbook.android.netutils.HttpMethods;
|
2019-04-02 17:41:53 +08:00
|
|
|
import com.novelbook.android.netutils.OnSuccessAndFaultListener;
|
|
|
|
import com.novelbook.android.netutils.OnSuccessAndFaultSub;
|
2019-04-03 18:09:00 +08:00
|
|
|
import com.novelbook.android.utils.BookUtil;
|
2019-04-03 16:21:00 +08:00
|
|
|
import com.novelbook.android.utils.NovelParseUtil;
|
2019-04-02 17:41:53 +08:00
|
|
|
|
2019-04-03 16:21:00 +08:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2019-04-02 17:41:53 +08:00
|
|
|
import org.litepal.LitePal;
|
|
|
|
|
|
|
|
import java.io.File;
|
2019-04-03 16:21:00 +08:00
|
|
|
import java.io.IOException;
|
2019-04-03 18:09:00 +08:00
|
|
|
import java.util.ArrayList;
|
2019-04-02 17:41:53 +08:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.OnClick;
|
2019-04-03 16:21:00 +08:00
|
|
|
import okhttp3.Call;
|
|
|
|
import okhttp3.Callback;
|
|
|
|
import okhttp3.Request;
|
|
|
|
import okhttp3.Response;
|
|
|
|
import okhttp3.ResponseBody;
|
2019-04-02 17:41:53 +08:00
|
|
|
|
|
|
|
public class BookActivity extends Activity_base {
|
2019-04-03 16:21:00 +08:00
|
|
|
|
|
|
|
static String TAG = BookActivity.class.getSimpleName();
|
2019-04-02 17:41:53 +08:00
|
|
|
BookListAdapter mAdapter;
|
|
|
|
// private BookListAdapter mAdapter;
|
2019-04-03 16:21:00 +08:00
|
|
|
private List<Novel> mData;;
|
2019-04-02 17:41:53 +08:00
|
|
|
|
2019-04-03 18:09:00 +08:00
|
|
|
private List<Chapter> chapters = new ArrayList<>();
|
2019-04-02 17:41:53 +08:00
|
|
|
@BindView(R.id.toolbar)
|
|
|
|
Toolbar toolbar;
|
|
|
|
@BindView(R.id.rvBooklist)
|
|
|
|
RecyclerView rvBooklist;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getLayoutRes() {
|
|
|
|
return R.layout.activity_book;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void initViews() {
|
|
|
|
initialBookList();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void setupToolbar(){
|
|
|
|
super.setupToolbar();
|
|
|
|
toolbar.inflateMenu(R.menu.menu_book);//设置右上角的填充菜单
|
|
|
|
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onMenuItemClick(MenuItem item) {
|
|
|
|
int menuItemId = item.getItemId();
|
|
|
|
if (menuItemId == R.id.menuShelf) {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void setTitle() {
|
|
|
|
String title = getIntent().getStringExtra("BOOKNAME");
|
|
|
|
this.setTitle(title);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void initData() {
|
|
|
|
mData =getFakeData(5);
|
|
|
|
mAdapter = getBookListAdapter(mData);
|
|
|
|
}
|
|
|
|
|
|
|
|
@OnClick({R.id.btnRead,R.id.btnCacheBook})
|
|
|
|
void submitButton(View view){
|
|
|
|
|
|
|
|
switch (view.getId()) {
|
|
|
|
case R.id.btnRead:
|
2019-04-03 16:21:00 +08:00
|
|
|
testBook();
|
|
|
|
// openBook(new Novel() );
|
2019-04-02 17:41:53 +08:00
|
|
|
break;
|
|
|
|
case R.id.btnCacheBook:
|
|
|
|
cachBook();
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void cachBook() {
|
|
|
|
MovieSubscribe.getData(1,10,new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(String result) {
|
|
|
|
//成功
|
|
|
|
Toast.makeText(BookActivity.this,"请求成功:"+result,Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFault(String errorMsg) {
|
|
|
|
//失败
|
|
|
|
Toast.makeText(BookActivity.this,"请求失败:"+errorMsg,Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
},BookActivity.this));
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:21:00 +08:00
|
|
|
void openBook(Novel book ) {
|
2019-04-02 17:41:53 +08:00
|
|
|
|
2019-04-03 16:21:00 +08:00
|
|
|
final String path = book.getNovelPath();
|
2019-04-02 17:41:53 +08:00
|
|
|
if(null ==path) {
|
2019-04-03 16:21:00 +08:00
|
|
|
List<Novel> bookLists = LitePal.findAll(Novel.class);
|
2019-04-02 17:41:53 +08:00
|
|
|
if(bookLists.size()>0) {
|
|
|
|
openBook(bookLists.get(0) );
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
File file = new File(path);
|
|
|
|
if (!file.exists()){
|
|
|
|
|
|
|
|
//to get book content txt from web
|
|
|
|
new AlertDialog.Builder(this)
|
|
|
|
.setTitle(this.getString(R.string.app_name))
|
|
|
|
.setMessage(path + "文件不存在,是否删除该书本?")
|
|
|
|
.setPositiveButton("删除", new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2019-04-03 16:21:00 +08:00
|
|
|
LitePal.deleteAll(Novel.class, "bookpath = ?", path);
|
2019-04-02 17:41:53 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
}).setCancelable(true).show();
|
|
|
|
return;
|
|
|
|
}
|
2019-04-03 18:09:00 +08:00
|
|
|
ReadActivity.openBook(book ,chapters,this);
|
2019-04-02 17:41:53 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:21:00 +08:00
|
|
|
void testBook(){
|
|
|
|
|
|
|
|
String url = "https://www.qu.la/book/161/";//"https://www.qu.la/book/746/";
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
.url(url)
|
|
|
|
// .header("User-Agent", "OkHttp Example")
|
|
|
|
.build();
|
|
|
|
HttpMethods.getOkClient().newCall(request).enqueue(new Callback() {
|
|
|
|
@Override
|
|
|
|
public void onFailure(Call call, IOException e) {
|
|
|
|
Log.d(TAG, "onFailure: " + e.getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(Call call, Response response){
|
|
|
|
ResponseBody body = response.body();
|
|
|
|
if (body != null) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
String bodyStr = body.string();
|
|
|
|
Log.d(TAG, "onResponse: " +bodyStr);
|
|
|
|
onResponseProcess(bodyStr,url);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}finally {
|
|
|
|
body.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void onResponseProcess( String content ,String url){
|
|
|
|
// HttpResult result ;
|
|
|
|
// HttpResult result2 = null;
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject siteJson = new JSONObject();
|
|
|
|
siteJson.put("chapterUrlRegexOnMulu", "");
|
|
|
|
siteJson.put("chapterUrlPattern", "/book/[\\d]+/[\\d]+\\.html$");
|
|
|
|
|
|
|
|
/*
|
|
|
|
String[] chapters = NovelParseUtil.getChapters(url, content, siteJson);
|
|
|
|
|
|
|
|
|
|
|
|
if (chapters != null) {
|
|
|
|
for (int i = 0; i < chapters.length; i += 2) {
|
|
|
|
Log.d(TAG,String.format("%s-->%s", chapters[i], chapters[i+1]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2019-04-03 18:09:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-03 16:21:00 +08:00
|
|
|
siteJson.put("chapterUrlRegexOnMulu", "<dd> <a[^>]*href=\"(/book/[\\d]+/[\\d]+\\.html)\">([^<]+)</a></dd>");
|
|
|
|
String[] chapters2 = NovelParseUtil.getChapters(url, content, siteJson);
|
|
|
|
if (chapters2 != null) {
|
|
|
|
for (int i = 0; i < chapters2.length; i += 2) {
|
|
|
|
Log.d(TAG, String.format("%s-->%s", chapters2[i], chapters2[i + 1]));
|
2019-04-03 18:09:00 +08:00
|
|
|
Chapter chapter = new Chapter();
|
|
|
|
chapter.setChapterName(chapters2[i + 1]);
|
|
|
|
chapter.setChapterPath(chapters2[i ]);
|
|
|
|
chapters.add(chapter);
|
|
|
|
|
2019-04-03 16:21:00 +08:00
|
|
|
}
|
2019-04-03 18:09:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-03 16:21:00 +08:00
|
|
|
siteJson.put("chapterContentRegex", "<div id=\"content\">([\\s\\S]+?)</div>");
|
|
|
|
siteJson.put("chapterContentDumpRegex", "<script>chaptererror();</script>");
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < chapters2.length; i += 2) {
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
.url(chapters2[i])
|
|
|
|
// .header("User-Agent", "OkHttp Example")
|
|
|
|
.build();
|
|
|
|
Response response = HttpMethods.getOkClient().newCall(request).execute();
|
2019-04-03 18:09:00 +08:00
|
|
|
// Log.d(TAG,String.format("%s-->%s\n%s" , chapters2[i], chapters2[i+1] , NovelParseUtil.getChapterContent(response.body().string(), siteJson)));
|
2019-04-03 16:21:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (JSONException | IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
// result.close();
|
|
|
|
// if (result2 != null) result2.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 17:41:53 +08:00
|
|
|
//----------------绑定列表
|
|
|
|
|
|
|
|
|
|
|
|
void initialBookList() {
|
|
|
|
|
|
|
|
rvBooklist.setLayoutManager(new LinearLayoutManager(this));
|
|
|
|
rvBooklist.setAdapter(mAdapter);
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
// View v =findViewById(R.id.head_img);
|
|
|
|
// v.measure(0,0);
|
|
|
|
// BlurKit.getInstance().blur(v, 20);
|
|
|
|
// BlurKit.getInstance().fastBlur(v,20, 0.3f);
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
protected void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
// blurLayout.startBlur();
|
|
|
|
// blurLayout.lockView();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStop() {
|
|
|
|
super.onStop();
|
|
|
|
// blurLayout.pauseBlur();
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
class BookListAdapter extends RecyclerView.Adapter< BookListAdapter.MyViewHolder> {
|
|
|
|
private final int EMPTY_VIEW = 1;
|
|
|
|
private final int PROGRESS_VIEW = 2;
|
|
|
|
private final int IMAGE_VIEW = 3;
|
|
|
|
|
|
|
|
private Context context;
|
|
|
|
private List<String> mDatas = new ArrayList<String>();
|
|
|
|
private OnItemClickListener mOnItemClickListener;
|
|
|
|
private int listItemID;
|
|
|
|
public BookListAdapter(Context context,List<String> mDatas,int listItemID,OnItemClickListener clickLitener) {
|
|
|
|
this.context = context;
|
|
|
|
this.mDatas = mDatas;
|
|
|
|
this.mOnItemClickListener = clickLitener;
|
|
|
|
this.listItemID = listItemID;
|
|
|
|
}
|
|
|
|
public BookListAdapter(Context context, OnItemClickListener clickLitener) {
|
|
|
|
this.context = context;
|
|
|
|
this.mOnItemClickListener = clickLitener;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemViewType(int position) {
|
|
|
|
if(mDatas.size() == 0){
|
|
|
|
return EMPTY_VIEW;
|
|
|
|
} else if(mDatas.get(position) == null){
|
|
|
|
return PROGRESS_VIEW;
|
|
|
|
} else {
|
|
|
|
return super.getItemViewType(position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
|
|
|
|
{
|
|
|
|
BookListAdapter.MyViewHolder holder = new BookListAdapter.MyViewHolder(LayoutInflater.from(
|
|
|
|
context).inflate(listItemID, parent,
|
|
|
|
false));
|
|
|
|
return holder;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setParameters(List<String> mDatas,int listItemID ) {
|
|
|
|
this.mDatas = mDatas;
|
|
|
|
this.listItemID = listItemID;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener)
|
|
|
|
{
|
|
|
|
this.mOnItemClickListener = mOnItemClickLitener;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder( BookListAdapter.MyViewHolder holder, int position)
|
|
|
|
{
|
|
|
|
holder.tvTitle.setText(mDatas.get(position));
|
|
|
|
holder.tvAuthor.setText("金庸" +position);
|
|
|
|
holder.tvCate.setText("cate"+position);
|
|
|
|
holder.tvDesc.setText("this is desc " +position);
|
|
|
|
// 如果设置了回调,则设置点击事件
|
|
|
|
if (mOnItemClickListener != null)
|
|
|
|
{
|
|
|
|
holder.itemView.setOnClickListener(new View.OnClickListener()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void onClick(View v)
|
|
|
|
{
|
|
|
|
int pos = holder.getLayoutPosition();
|
|
|
|
mOnItemClickListener.onItemClick(holder.itemView, pos);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
holder.itemView.setOnLongClickListener(new View.OnLongClickListener()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public boolean onLongClick(View v)
|
|
|
|
{
|
|
|
|
int pos = holder.getLayoutPosition();
|
|
|
|
mOnItemClickListener.onItemLongClick(holder.itemView, pos);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount()
|
|
|
|
{
|
|
|
|
return mDatas.size();
|
|
|
|
}
|
|
|
|
public void addData(int position) {
|
|
|
|
mDatas.add(position, "Insert One");
|
|
|
|
notifyItemInserted(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeData(int position) {
|
|
|
|
mDatas.remove(position);
|
|
|
|
notifyItemRemoved(position);
|
|
|
|
}
|
|
|
|
class MyViewHolder extends RecyclerView.ViewHolder
|
|
|
|
{
|
|
|
|
@BindView(R.id.title)
|
|
|
|
TextView tvTitle;
|
|
|
|
@BindView(R.id.author)
|
|
|
|
TextView tvAuthor;
|
|
|
|
@BindView(R.id.category)
|
|
|
|
TextView tvCate;
|
|
|
|
@BindView(R.id.desc)
|
|
|
|
TextView tvDesc;
|
|
|
|
public MyViewHolder(View view)
|
|
|
|
{
|
|
|
|
super(view);
|
|
|
|
ButterKnife.bind(this, view);
|
|
|
|
//tvTitle = (TextView) view.findViewById(R.id.title);
|
|
|
|
// tvAuthor = (TextView) view.findViewById(R.id.author);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|