492 lines
		
	
	
		
			16 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
		
		
			
		
	
	
			492 lines
		
	
	
		
			16 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
|  | package com.novelbook.android.Fragments; | ||
|  | 
 | ||
|  | 
 | ||
|  | import android.app.AlertDialog; | ||
|  | import android.content.Context; | ||
|  | import android.content.DialogInterface; | ||
|  | import android.content.Intent; | ||
|  | import android.content.res.Resources; | ||
|  | import android.graphics.pdf.PdfDocument; | ||
|  | import android.net.Uri; | ||
|  | import android.os.Bundle; | ||
|  | import android.os.Handler; | ||
|  | import android.os.Message; | ||
|  | import android.support.annotation.Nullable; | ||
|  | import android.support.design.widget.TabLayout; | ||
|  | import android.support.v4.app.Fragment; | ||
|  | import android.support.v4.app.FragmentActivity; | ||
|  | import android.support.v4.app.FragmentManager; | ||
|  | import android.support.v4.app.FragmentTransaction; | ||
|  | import android.support.v4.widget.SwipeRefreshLayout; | ||
|  | import android.support.v7.widget.RecyclerView; | ||
|  | import android.text.TextUtils; | ||
|  | import android.util.Log; | ||
|  | import android.util.TypedValue; | ||
|  | import android.view.LayoutInflater; | ||
|  | import android.view.View; | ||
|  | import android.view.ViewGroup; | ||
|  | import android.widget.FrameLayout; | ||
|  | import android.widget.ImageView; | ||
|  | import android.widget.LinearLayout; | ||
|  | import android.widget.TextView; | ||
|  | import android.widget.Toast; | ||
|  | 
 | ||
|  | 
 | ||
|  | import com.google.gson.Gson; | ||
|  | 
 | ||
|  | import com.novelbook.android.Activity_base; | ||
|  | 
 | ||
|  | import com.novelbook.android.Activity_paihangbang; | ||
|  | import com.novelbook.android.Activity_shudan; | ||
|  | import com.novelbook.android.BookActivity; | ||
|  | import com.novelbook.android.BuildConfig; | ||
|  | import com.novelbook.android.Main2Activity; | ||
|  | import com.novelbook.android.R; | ||
|  | import com.novelbook.android.ReadActivity; | ||
|  | import com.novelbook.android.adapter.BookListAdapter; | ||
|  | import com.novelbook.android.bean.NovelBlock; | ||
|  | import com.novelbook.android.db.Novel; | ||
|  | import com.novelbook.android.netsubscribe.BookSubscribe; | ||
|  | import com.novelbook.android.netutils.NetUtil; | ||
|  | import com.novelbook.android.netutils.OnSuccessAndFaultListener; | ||
|  | import com.novelbook.android.netutils.OnSuccessAndFaultSub; | ||
|  | import com.novelbook.android.utils.Constants; | ||
|  | import com.novelbook.android.utils.ImageUtil; | ||
|  | import com.novelbook.android.utils.MyImageLoader; | ||
|  | import com.novelbook.android.utils.OnItemClickListener; | ||
|  | import com.novelbook.android.utils.PageFactory; | ||
|  | import com.qq.e.ads.nativ.ADSize; | ||
|  | import com.qq.e.ads.nativ.NativeADUnifiedListener; | ||
|  | import com.qq.e.ads.nativ.NativeExpressAD; | ||
|  | import com.qq.e.ads.nativ.NativeExpressADView; | ||
|  | import com.qq.e.ads.nativ.NativeUnifiedAD; | ||
|  | import com.qq.e.ads.nativ.NativeUnifiedADData; | ||
|  | import com.qq.e.comm.constants.AdPatternType; | ||
|  | import com.qq.e.comm.util.AdError; | ||
|  | import com.qq.e.comm.util.GDTLogger; | ||
|  | import com.umeng.analytics.MobclickAgent; | ||
|  | import com.youth.banner.loader.ImageLoader; | ||
|  | 
 | ||
|  | import org.litepal.LitePal; | ||
|  | import org.w3c.dom.Text; | ||
|  | 
 | ||
|  | import java.io.File; | ||
|  | import java.lang.reflect.Field; | ||
|  | import java.util.ArrayList; | ||
|  | import java.util.Iterator; | ||
|  | import java.util.List; | ||
|  | 
 | ||
|  | import butterknife.BindView; | ||
|  | import butterknife.ButterKnife; | ||
|  | 
 | ||
|  | import static com.novelbook.android.bean.AdSetting.getAppID; | ||
|  | 
 | ||
|  | 
 | ||
|  | /** | ||
|  |  * A simple {@link Fragment} subclass. | ||
|  |  */ | ||
|  | public abstract class BasicFragment extends Fragment  /*implements  NativeADUnifiedListener,NativeExpressAD.NativeExpressADListener*/ { | ||
|  | 
 | ||
|  |    private static String TAG = BasicFragment.class.getSimpleName(); | ||
|  |     protected View rootView; | ||
|  |     protected int pageNo=1; | ||
|  |     private int pageCount; | ||
|  | 
 | ||
|  |     public int getPageCount() { | ||
|  |         pageCount =  pageCount >Constants.MAX_PAGE_CNT ? Constants.MAX_PAGE_CNT :pageCount; | ||
|  |         return pageCount; | ||
|  |     } | ||
|  | 
 | ||
|  |     public void setPageCount(int pageCount) { | ||
|  |         this.pageCount = pageCount; | ||
|  |     } | ||
|  | 
 | ||
|  |     protected abstract int getLayoutRes(); | ||
|  |     protected abstract void initData(); | ||
|  |     protected abstract void fillData(); | ||
|  |     protected   void initListener(){} | ||
|  |     protected abstract void initViews(); | ||
|  |     protected Gson gson = new Gson(); | ||
|  |   //  protected MyImageLoader imgloader = new MyImageLoader(); | ||
|  |    // Main2Activity activity; | ||
|  |     Activity_base activity; | ||
|  |     @Nullable | ||
|  |     @BindView(R.id.swipeLayout) | ||
|  |     SwipeRefreshLayout mSwipeRefresh; | ||
|  |     @Nullable | ||
|  |     @BindView(R.id.banner_container) | ||
|  |     FrameLayout mBannerContainer; | ||
|  |     int MSG_INIT_TAB =99; | ||
|  | 
 | ||
|  |     int error_try=0; | ||
|  |     int error_try_max=5; | ||
|  | 
 | ||
|  |     public BasicFragment() { | ||
|  |         // Required empty public constructor | ||
|  |     } | ||
|  |     @Override | ||
|  |     public void onCreate(Bundle savedInstanceState) { | ||
|  |         super.onCreate(savedInstanceState); | ||
|  |         setFTag(); | ||
|  |        // Log.d(TAG,"fragment onCreate "); | ||
|  |        // activity =(Main2Activity)getActivity(); | ||
|  |         activity = (Activity_base)getActivity(); | ||
|  |         processArguments(); | ||
|  | 
 | ||
|  | 
 | ||
|  |         //			 QLog.getLogger().e(dTAG,TAG+" "+ "onCreate"); | ||
|  | //		mediumFontSize = activity.getMiddleSizeFont(); | ||
|  |     } | ||
|  | 
 | ||
|  |     protected abstract void processArguments(); | ||
|  | 
 | ||
|  |     @Override | ||
|  |     public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||
|  |                              Bundle savedInstanceState) { | ||
|  |         Log.d(TAG,"onCreateView"); | ||
|  |         View view = inflater.inflate(getLayoutRes(), container, false); | ||
|  |         rootView = view; | ||
|  |         // 初始化View注入 | ||
|  |         ButterKnife.bind(this,view); | ||
|  | 
 | ||
|  |         initListener(); | ||
|  |         initData(); | ||
|  |         initViews(); | ||
|  |         initSwipeRefreshLayout(); | ||
|  |         if (mBannerContainer!=null && (!Constants.AD_SETTING.isShowAdsense() || !Constants.AD_SETTING.getFixBanner().isShow() )) { | ||
|  |             if( mBannerContainer.getVisibility() !=View.GONE) { | ||
|  |                 mBannerContainer.setVisibility(View.GONE); | ||
|  |             } | ||
|  |         } | ||
|  | 
 | ||
|  |         return view; | ||
|  |         // Inflate the layout for this fragment | ||
|  | 
 | ||
|  |     } | ||
|  | 
 | ||
|  |     public String getCurrentTag() { | ||
|  |         setFTag(); | ||
|  |         return TAG; | ||
|  |     } | ||
|  | 
 | ||
|  | 
 | ||
|  |     public void setFTag(){}; | ||
|  | 
 | ||
|  |     Handler handler = new Handler() { | ||
|  |         @Override | ||
|  |         public void handleMessage(Message msg) { | ||
|  |             Log.d(TAG, "prepare book handleMessage: msg" +msg.what); | ||
|  | 
 | ||
|  |             hideProgress(); | ||
|  |             int wt = msg.what; | ||
|  | 
 | ||
|  |             if (msg.what == 1) { | ||
|  |                 try { | ||
|  |                     fillData(); | ||
|  |                 }catch (Exception e){ | ||
|  |                     Log.e(TAG, "handleMessage: filldata error ", e); | ||
|  |                 } | ||
|  | 
 | ||
|  |             } else if (msg.what == MSG_INIT_TAB)  // | ||
|  |             { | ||
|  |                 initTabs(); | ||
|  |               //  Toast.makeText(getActivity(), "数据请求失败 ", Toast.LENGTH_LONG).show(); | ||
|  |             } | ||
|  | 
 | ||
|  |             if(getActivity()!=null && !NetUtil.isNetworkConnected()){ | ||
|  |                 Toast.makeText(getActivity(),"好像没有网络啦",Toast.LENGTH_LONG).show(); | ||
|  |             } | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  |         } | ||
|  |     }; | ||
|  | 
 | ||
|  | void initTabs(){ | ||
|  | 
 | ||
|  | } | ||
|  | 
 | ||
|  |     //--------------------------------------------------------------book list adapter------------------------------------------------ | ||
|  |   /*  interface mOnItemClickListener | ||
|  |     { | ||
|  |         void onItemClick(View view, int position); | ||
|  |         void onItemLongClick(View view , int position); | ||
|  |         void onLinearOutClick(View view,int bookId); | ||
|  |     } | ||
|  | */ | ||
|  | 
 | ||
|  |     protected  List<Novel> initData(List<Novel> mDatas, char x) | ||
|  |     { | ||
|  |         mDatas = new ArrayList<Novel>(); | ||
|  |         for (int i = 'A'; i < x; i++) | ||
|  |         { | ||
|  |             Novel bk = new Novel(); | ||
|  |             bk.setNovelId(i); | ||
|  |             bk.setAuthor("金庸"); | ||
|  |             bk.setName("射雕英雄传" +(char)i); | ||
|  |             bk.setNovelType("武侠"); | ||
|  |             bk.setDesc("南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事"); | ||
|  |             mDatas.add(bk); | ||
|  |         } | ||
|  |         return  mDatas; | ||
|  |     } | ||
|  | 
 | ||
|  |     void showBookDetail(Novel book) { | ||
|  | 
 | ||
|  |         Intent intent = new Intent(activity, BookActivity.class); | ||
|  |         intent.putExtra(BookActivity.EXTRA_BOOK,book); | ||
|  |         startActivity(intent); | ||
|  |     } | ||
|  | 
 | ||
|  |     void getBookInfo(Novel novel){ | ||
|  |         int novelId = novel.getNovelId(); | ||
|  |         long id = novel.getId(); | ||
|  |         Log.d(TAG, String.format("prepare book %s ,start to loading book info from host ",novel.getName())); | ||
|  |       //  showProgressDialog(false,"正在加载..."); | ||
|  |         PageFactory pageFactory =PageFactory.getInstance(activity ) ; | ||
|  |         pageFactory.initBookUtil(); | ||
|  |         ReadActivity.openBook(novel ,activity); | ||
|  | 
 | ||
|  | 
 | ||
|  |         BookSubscribe.getNovel(novel.getNovelId(),new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() { | ||
|  |             @Override | ||
|  |             public void onSuccess(String result) { | ||
|  |                 //成功 | ||
|  | 
 | ||
|  |                 Novel nv ; | ||
|  |                 nv = gson.fromJson(result,Novel.class); | ||
|  |                 if(id>0) { | ||
|  |                     nv.update(id); | ||
|  |                  //   nv.checkAndUpdate(); | ||
|  |                     nv= LitePal.find(Novel.class,id); | ||
|  |                 }else { | ||
|  |                     nv.save(); | ||
|  |                 } | ||
|  |                 Log.d(TAG, String.format("prepare book %s ,start to loading book info from host -----------done",novel.getName())); | ||
|  |                 Log.d(TAG, String.format("prepare book %s ,lastchapt %s, last pos %s ,开始prepare.",nv.getName(),nv.getLastReadChapt(),nv.getLastReadPos()) ); | ||
|  |             //    final Novel novel = nv; | ||
|  |         /*       new Thread(){ | ||
|  |                     @Override | ||
|  |                     public void run() { | ||
|  |                         super.run(); | ||
|  | 
 | ||
|  |                     } | ||
|  |                 }.start();*/ | ||
|  |               //  ReadActivity.openBook(nv ,activity); | ||
|  |              //    PageFactory.getInstance(activity.getApplicationContext()).prepareBook(nv ); //打开本地小说内容 | ||
|  |                 //PageFactory.getInstance(activity ).prepareBook(nv ); //打开本地小说内容 | ||
|  |                 pageFactory.prepareBook(nv ); | ||
|  |                 Log.d(TAG, String.format("prepare book %s 开始open book.",nv.getName()) ); | ||
|  | 
 | ||
|  |                 handler.sendEmptyMessage(99); | ||
|  |             } | ||
|  | 
 | ||
|  |             @Override | ||
|  |             public void onFault(String errorMsg) { | ||
|  |                 //失败 | ||
|  |                 Toast.makeText(getActivity(),novel.getName() +" 信息请求失败" ,Toast.LENGTH_SHORT).show(); | ||
|  |              //  PageFactory.getInstance(activity ).prepareBook(novel ); //打开本地小说内容 | ||
|  |                 pageFactory.prepareBook(novel ); | ||
|  |              //    ReadActivity.openBook(novel ,activity); | ||
|  |                 handler.sendEmptyMessage(99); | ||
|  |             } | ||
|  |         },getActivity())); | ||
|  | 
 | ||
|  |     } | ||
|  |     void showProgressDialog(boolean flag,String msg){ | ||
|  |         if(!Constants.showDialogOnUi){ | ||
|  |             return; | ||
|  |         } | ||
|  |         if(pageNo >1 && !Constants.showDialogOnUiPage){ | ||
|  |             return; | ||
|  |         } | ||
|  |         if(getActivity()==null){ | ||
|  |             return; | ||
|  |         } | ||
|  |         if(NetUtil.isNetworkConnected()) | ||
|  |         if(mSwipeRefresh ==null ||  !mSwipeRefresh.isRefreshing()) { | ||
|  |             ((Activity_base) getActivity()).showProgressDialog(flag,msg); | ||
|  |         } | ||
|  | 
 | ||
|  | 
 | ||
|  |     } | ||
|  |     void hideProgress(){ | ||
|  |         if(getActivity()==null){ | ||
|  |             return; | ||
|  |         } | ||
|  |         if(mSwipeRefresh!=null &&  mSwipeRefresh.isRefreshing()) { | ||
|  |             mSwipeRefresh.setRefreshing(false); | ||
|  |         } | ||
|  |         ((Activity_base) getActivity()).hideProgress(); | ||
|  | 
 | ||
|  |     } | ||
|  |     void initSwipeRefreshLayout(){ | ||
|  |         if(mSwipeRefresh==null){return;} | ||
|  |         mSwipeRefresh.setColorSchemeResources(R.color.colorPrimary,R.color.colorPrimaryDark); | ||
|  |         mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { | ||
|  |             @Override | ||
|  |             public void onRefresh() { | ||
|  | 
 | ||
|  |                 // 开始刷新,设置当前为刷新状态 | ||
|  |                 //swipeRefreshLayout.setRefreshing(true); | ||
|  |                 pageNo=1; | ||
|  |                 error_try=0; | ||
|  |                 initData(); | ||
|  |                 // TODO 获取数据 | ||
|  | 
 | ||
|  |             } | ||
|  |         }); | ||
|  |     } | ||
|  | 
 | ||
|  |     void openBook(Novel book, BookListAdapter mAdapter) { | ||
|  | 
 | ||
|  |         //从新从数据库抓取该书的最新阅读进度 | ||
|  | 
 | ||
|  |         Novel book1 = (Novel)LitePal.find(Novel.class,book.getId()); | ||
|  |         if(book1!=null) { //读取本地最新进度 | ||
|  |             book.setLastReadPos(book1.getLastReadPos()); | ||
|  |             book.setLastReadChapt(book1.getLastReadChapt()); | ||
|  |         } | ||
|  |        // Toast.makeText(activity, book.getName() + "加载",  Toast.LENGTH_SHORT).show(); | ||
|  | 
 | ||
|  |        if(!book.isLocalBook()){ | ||
|  | 
 | ||
|  |             getBookInfo(book); | ||
|  | 
 | ||
|  |            /* | ||
|  |            PageFactory pageFactory = PageFactory.getInstance(); | ||
|  |             showProgressDialog(false,"读取目录信息"); | ||
|  | 
 | ||
|  |             int slept = 0; | ||
|  |             while(slept <100 || pageFactory.getChapters() == null || pageFactory.getChapters().size()==0){ | ||
|  |                 try { | ||
|  |                     Thread.sleep(50); | ||
|  |                     slept++; | ||
|  |                 } catch (InterruptedException e) { | ||
|  |                     e.printStackTrace(); | ||
|  |                 } | ||
|  |             } | ||
|  |            hideProgress(); | ||
|  |             */ | ||
|  |            //ReadActivity.openBook(book ,activity); | ||
|  |             return; | ||
|  |         } | ||
|  | 
 | ||
|  | 
 | ||
|  |         final String path = book.getNovelPath(); | ||
|  | 
 | ||
|  |         File file = new File(path); | ||
|  |         if (!file.exists()){ | ||
|  |             new AlertDialog.Builder(activity) | ||
|  |                     .setTitle(activity.getString(R.string.app_name)) | ||
|  |                     .setMessage(path + "文件不存在,是否删除该书本?") | ||
|  |                     .setPositiveButton("删除", new DialogInterface.OnClickListener() { | ||
|  |                         @Override | ||
|  |                         public void onClick(DialogInterface dialog, int which) { | ||
|  |                             LitePal.deleteAll(Novel.class, "id = ?", book.getId()+""); | ||
|  |                             if(mAdapter!=null) {mAdapter.setBookList( LitePal.where("isOnShelf=?","1").find(Novel.class));} | ||
|  |                         } | ||
|  |                     }).setCancelable(true).show(); | ||
|  |             return; | ||
|  |         } | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  |         ReadActivity.openBook(book ,activity); | ||
|  | 
 | ||
|  |     } | ||
|  |     void showShudanDetail(int shuandanId){ | ||
|  |         Intent intent = new Intent(activity, Activity_shudan.class); | ||
|  |         intent.putExtra("NAME",shuandanId); | ||
|  |         startActivity(intent); | ||
|  | 
 | ||
|  |     } | ||
|  | 
 | ||
|  | //-------------------------------------------------------------------------------------book list adapter end-------------------------------------------------- | ||
|  | 
 | ||
|  |   void showPaihangbang(Object obj) {  //show paihangbang activity | ||
|  | 
 | ||
|  |      NovelBlock nb = (NovelBlock) obj; | ||
|  |     Intent intent = new Intent(activity, Activity_paihangbang.class); | ||
|  |     String title =  TextUtils.isEmpty(nb.getTitle())  ?  nb.getName() :  nb.getTitle() ; | ||
|  | 
 | ||
|  |       intent.putExtra(Activity_paihangbang.EXTR_FN,nb.getFn() ); | ||
|  |     intent.putExtra(Activity_paihangbang.EXTR_TITLE,title ); | ||
|  |     startActivity(intent); | ||
|  | 
 | ||
|  | } | ||
|  |     void showPaihangbang(NovelBlock nb) {  //show paihangbang activity | ||
|  | 
 | ||
|  |         Intent intent = new Intent(activity, Activity_paihangbang.class); | ||
|  |         String title =  TextUtils.isEmpty(nb.getTitle())  ?  nb.getName() :  nb.getTitle() ; | ||
|  | 
 | ||
|  |         intent.putExtra(Activity_paihangbang.EXTR_CID,nb.getCid() ); | ||
|  |         intent.putExtra(Activity_paihangbang.EXTR_TITLE,title ); | ||
|  |         intent.putExtra(Activity_paihangbang.EXTR_BANGDAN,title ); | ||
|  |         startActivity(intent); | ||
|  | 
 | ||
|  |     } | ||
|  | 
 | ||
|  | 
 | ||
|  |     protected void loadImageView(String url, ImageView imageView){ | ||
|  |         ImageUtil.loadImage( getActivity(), url , imageView); | ||
|  |     } | ||
|  | 
 | ||
|  | 
 | ||
|  |     public void reload() { | ||
|  |         loadData(); | ||
|  |     } | ||
|  |     public void loadData() { | ||
|  |         initData(); | ||
|  |     }  //on tab changed | ||
|  | 
 | ||
|  | 
 | ||
|  |     void retryErorr(int msg){ | ||
|  |         if(error_try<error_try_max){ | ||
|  |             error_try++; | ||
|  |             initData(); | ||
|  |         }else { | ||
|  |             handler.sendEmptyMessage(msg); | ||
|  |         } | ||
|  |     } | ||
|  |     void initRetryCnt(){ | ||
|  |         error_try=0; | ||
|  |     } | ||
|  | 
 | ||
|  |     //---------banner ad------ | ||
|  |     public void showBanner( FrameLayout bannerContainer  , int adHeight ) { | ||
|  |         if(bannerContainer==null){ | ||
|  |             return; | ||
|  |         } | ||
|  | 
 | ||
|  |        if (  !Constants.AD_SETTING.isShowAdsense() || !Constants.AD_SETTING.getFixBanner().isShow() || !NetUtil.isNetworkConnected()) { | ||
|  |             mBannerContainer.setVisibility(View.GONE); | ||
|  |             return; | ||
|  |         } | ||
|  | 
 | ||
|  |        activity .loadBanner(bannerContainer, (int)(adHeight*5.9), adHeight); | ||
|  |        if( mBannerContainer.getVisibility() !=View.VISIBLE) | ||
|  |         mBannerContainer.setVisibility(View.VISIBLE); | ||
|  |     } | ||
|  |     void loadListAd( BookListAdapter adapter, int adCnt, boolean addFooter) { | ||
|  |         activity.loadListAd(adapter,adCnt,addFooter); | ||
|  |     } | ||
|  | 
 | ||
|  |     @Override | ||
|  |     public void onResume() { | ||
|  |         super.onResume(); | ||
|  |         error_try=0; | ||
|  |         showBanner(mBannerContainer,(int) getResources().getDimension(R.dimen.topBannerHeight) ); | ||
|  |         // MobclickAgent.onPageStart(TAG); | ||
|  |     } | ||
|  |     @Override | ||
|  |     public void onPause(){ | ||
|  |         super.onPause(); | ||
|  |         hideProgress(); | ||
|  |         //    MobclickAgent.onPageEnd(TAG); | ||
|  |     } | ||
|  | 
 | ||
|  | } |