460 lines
		
	
	
		
			16 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
		
		
			
		
	
	
			460 lines
		
	
	
		
			16 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
|  | package com.novelbook.android; | ||
|  | 
 | ||
|  | import android.app.AlertDialog; | ||
|  | import android.content.BroadcastReceiver; | ||
|  | import android.content.Context; | ||
|  | import android.content.DialogInterface; | ||
|  | import android.content.Intent; | ||
|  | import android.content.IntentFilter; | ||
|  | import android.os.Build; | ||
|  | import android.support.v7.app.AppCompatActivity; | ||
|  | import android.os.Bundle; | ||
|  | import android.support.v7.widget.GridLayoutManager; | ||
|  | import android.support.v7.widget.LinearLayoutManager; | ||
|  | import android.support.v7.widget.RecyclerView; | ||
|  | import android.util.Log; | ||
|  | import android.view.LayoutInflater; | ||
|  | import android.view.View; | ||
|  | import android.view.ViewGroup; | ||
|  | import android.widget.ImageView; | ||
|  | import android.widget.ProgressBar; | ||
|  | import android.widget.TextView; | ||
|  | 
 | ||
|  | import com.novelbook.android.Activity_base; | ||
|  | import com.novelbook.android.R; | ||
|  | import com.novelbook.android.adapter.BookListAdapter; | ||
|  | import com.novelbook.android.bean.Cataloge; | ||
|  | import com.novelbook.android.db.Chapter; | ||
|  | import com.novelbook.android.db.DownloadTask; | ||
|  | import com.novelbook.android.db.Novel; | ||
|  | import com.novelbook.android.service.ServiceDownload; | ||
|  | import com.novelbook.android.utils.FileUtils; | ||
|  | import com.novelbook.android.utils.Fileutil; | ||
|  | import com.novelbook.android.utils.ImageUtil; | ||
|  | 
 | ||
|  | import org.litepal.LitePal; | ||
|  | 
 | ||
|  | import java.util.ArrayList; | ||
|  | import java.util.Comparator; | ||
|  | import java.util.List; | ||
|  | 
 | ||
|  | import butterknife.BindView; | ||
|  | import butterknife.ButterKnife; | ||
|  | 
 | ||
|  | public class Activity_cache extends Activity_base { | ||
|  |     public static final String TAG=Activity_cache.class.getSimpleName(); | ||
|  |     @BindView(R.id.recycleView) | ||
|  |     RecyclerView mRecyclerView; | ||
|  |     @BindView(R.id.tvMsg) | ||
|  |     TextView tvMsg; | ||
|  |     private List<DownloadTask> mData; | ||
|  |     private CacheAdapter mAdapter; | ||
|  |    // private String cacheSize; | ||
|  |     @Override | ||
|  |     public int getLayoutRes() { | ||
|  |         return R.layout.activitycache; | ||
|  |     } | ||
|  | 
 | ||
|  |     @Override | ||
|  |     protected void initViews() { | ||
|  | 
 | ||
|  |     } | ||
|  | 
 | ||
|  |     @Override | ||
|  |     protected void setTitle() { | ||
|  | 
 | ||
|  |     } | ||
|  | 
 | ||
|  |     @Override | ||
|  |     protected void initData() { | ||
|  |         initiDownloadReceiver(); | ||
|  | 
 | ||
|  |         showProgressDialog(true,"正在加载..."); | ||
|  | 
 | ||
|  |         new Thread() { | ||
|  | 
 | ||
|  |             @Override | ||
|  |             public void run() { | ||
|  |               //  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
|  |                     mData = LitePal.where("id >0").order("id desc").find(DownloadTask.class);//findAll(DownloadTask.class); | ||
|  | 
 | ||
|  |                     //cacheSize = FileUtils.getCachedSize(); | ||
|  |                   /*  mData.sort(new Comparator<DownloadTask>(){ | ||
|  |                         public int compare(DownloadTask arg0, DownloadTask arg1) { | ||
|  |                             int i = arg0.getFinishedChpats()/arg0.getTotalChapts() >  arg1.getFinishedChpats()/arg1.getTotalChapts() ?1:-1; | ||
|  | 
 | ||
|  |                             return i; }});*/ | ||
|  | 
 | ||
|  |             //    } | ||
|  | 
 | ||
|  |                 handler.sendEmptyMessage(1); | ||
|  | 
 | ||
|  |             } | ||
|  |         }.start(); | ||
|  | 
 | ||
|  |     } | ||
|  | 
 | ||
|  |     @Override | ||
|  |     public void fillData() { | ||
|  | 
 | ||
|  |         mAdapter = new CacheAdapter(this,mData,R.layout.recycle_list_item_cache,new  OnItemClickLitener() { | ||
|  |             @Override | ||
|  |             public void onItemClick(View view, int position) { | ||
|  |                 Novel novel = LitePal.find(Novel.class,mData.get(position).getNovelId()); | ||
|  |                 showBookDetail(novel); | ||
|  |             } | ||
|  | 
 | ||
|  |             @Override | ||
|  |             public void onItemLongClick(View view, int position) | ||
|  |             { | ||
|  |                 final AlertDialog.Builder normalDialog = | ||
|  |                         new AlertDialog.Builder(Activity_cache.this); | ||
|  | 
 | ||
|  |                 normalDialog.setTitle("删除此下载任务并清除缓存吗"); | ||
|  |                 normalDialog.setMessage("确定删除吗?"); | ||
|  |                 normalDialog.setPositiveButton("取消", | ||
|  |                         new DialogInterface.OnClickListener() { | ||
|  |                             @Override | ||
|  |                             public void onClick(DialogInterface dialog, int which) { | ||
|  | 
 | ||
|  |                             } | ||
|  |                         }); | ||
|  |                 normalDialog.setNegativeButton("删除", | ||
|  |                         new DialogInterface.OnClickListener() { | ||
|  |                             @Override | ||
|  |                             public void onClick(DialogInterface dialog, int which) { | ||
|  |                                 DownloadTask cl = mData.get(position); | ||
|  |                                 cl.delete(); | ||
|  |                                 //   Chapter.deleteByTask(); | ||
|  |                                 //TODO:clear cache ? | ||
|  | 
 | ||
|  | 
 | ||
|  |                                 mAdapter.removeData(position); | ||
|  | 
 | ||
|  |                             } | ||
|  |                         }); | ||
|  | 
 | ||
|  |                 normalDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { | ||
|  |                     @Override | ||
|  |                     public void onDismiss(DialogInterface dialog) { | ||
|  | 
 | ||
|  |                     } | ||
|  |                 }); | ||
|  |                 // 显示 | ||
|  |                 normalDialog.show(); | ||
|  |                 // mAdapter.removeData(position); | ||
|  |             } | ||
|  | 
 | ||
|  |         }); | ||
|  |         if (mAdapter != null) { | ||
|  |             mAdapter.setNorecord(R.string.msgLoading); | ||
|  |         } | ||
|  |         mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); | ||
|  |         mRecyclerView.setAdapter(mAdapter ); | ||
|  |         if (mAdapter != null) { | ||
|  |             mAdapter.setNorecord(R.string.noRecord); | ||
|  |         } | ||
|  |       if( mData.size()>0){ | ||
|  |         tvMsg.setText(String.format("共有%s条下载记录",mData.size())); | ||
|  |       }else{ | ||
|  |           tvMsg.setText(R.string.noRecord); | ||
|  |       } | ||
|  |     } | ||
|  | 
 | ||
|  |     private IntentFilter filter; | ||
|  |     DownloadProcessReceiver receiver; | ||
|  |     void initiDownloadReceiver(){ | ||
|  |         filter = new IntentFilter("ServiceDownload.ChapterContent.finished"); | ||
|  |         filter.addCategory(Intent.CATEGORY_DEFAULT); | ||
|  |         receiver = new DownloadProcessReceiver(); | ||
|  |     } | ||
|  |     class DownloadProcessReceiver extends BroadcastReceiver { | ||
|  | 
 | ||
|  |         @Override | ||
|  |         public void onReceive(Context context, Intent intent){ | ||
|  |             int taskId =0; | ||
|  |             int progress=0; | ||
|  |             int status=0; | ||
|  | 
 | ||
|  |          //   tvMsg.setVisibility(View.GONE); | ||
|  |             if( intent.hasExtra("network")){ | ||
|  |                 String txt  = intent.getStringExtra("network"); | ||
|  |                 tvMsg.setText(txt); | ||
|  |                 tvMsg.setVisibility(View.VISIBLE); | ||
|  |                 return; | ||
|  | 
 | ||
|  |             } | ||
|  | 
 | ||
|  |             if( intent.hasExtra("progress")){ | ||
|  |                 progress = intent.getIntExtra("progress",0); | ||
|  |             } | ||
|  |             if( intent.hasExtra("taskId")){ | ||
|  |                 taskId = intent.getIntExtra("taskId",0); | ||
|  |             } | ||
|  |             if( intent.hasExtra("status")){ | ||
|  |                 status = intent.getIntExtra("status",0); | ||
|  |             } | ||
|  | 
 | ||
|  | 
 | ||
|  |             Log.d(TAG, String.format("onReceive: taskId %s progress %s ",taskId,progress)); | ||
|  |             for(DownloadTask dt : mData){ | ||
|  |                 if(taskId == dt.getId()){ | ||
|  |                    dt.setFinishedChpats(progress); | ||
|  |                    if(status==1){ | ||
|  |                        dt.setStatus(1); | ||
|  |                         dt.setDownSatus(DownloadTask.DownStatus.下载完成); | ||
|  |                     }else | ||
|  |                   if(dt.getDownSatus()!=DownloadTask.DownStatus.暂停下载) { | ||
|  |                         dt.setDownSatus(DownloadTask.DownStatus.正在下载); | ||
|  |                       dt.setStatus(0); | ||
|  |                     } | ||
|  | 
 | ||
|  |                   // dt = LitePal.find(DownloadTask.class,taskId); | ||
|  | 
 | ||
|  |                 }else{ | ||
|  |                    if(dt.getDownSatus()==DownloadTask.DownStatus.正在下载) { | ||
|  |                         dt.setDownSatus(DownloadTask.DownStatus.排队中); | ||
|  |                     } | ||
|  |                 } | ||
|  |             } | ||
|  | 
 | ||
|  |             mAdapter.setData(mData); | ||
|  | 
 | ||
|  | 
 | ||
|  |         } | ||
|  |     } | ||
|  |     private void startDownloadService(int taskId,boolean start) { | ||
|  |         Intent serviceIntent =  new Intent(this, ServiceDownload.class); | ||
|  |         serviceIntent.putExtra("taskId",taskId); | ||
|  |         serviceIntent.putExtra("start",start); | ||
|  |         startService(serviceIntent); | ||
|  |     } | ||
|  | 
 | ||
|  |     @Override | ||
|  |     protected void onResume() { | ||
|  |         super.onResume(); | ||
|  |         registerReceiver(receiver, filter); | ||
|  |     } | ||
|  | 
 | ||
|  |     @Override | ||
|  |     public void onPause() { | ||
|  |         super.onPause(); | ||
|  |         unregisterReceiver(receiver); | ||
|  |     } | ||
|  | 
 | ||
|  |     class CacheAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | ||
|  |         private final int EMPTY_VIEW = 1; | ||
|  |         private final int PROGRESS_VIEW = 2; | ||
|  |         private final int IMAGE_VIEW = 3; | ||
|  | 
 | ||
|  |         private Context context; | ||
|  |         private List<DownloadTask> mDatas = new ArrayList<DownloadTask>(); | ||
|  |         private OnItemClickLitener mOnItemClickLitener; | ||
|  |         private int listItemID; | ||
|  |         private int norecord =R.string.noRecord; | ||
|  | 
 | ||
|  |         public CacheAdapter(Context context, List<DownloadTask> mDatas, int listItemID, OnItemClickLitener clickLitener) { | ||
|  |             this.context = context; | ||
|  |             this.mDatas = mDatas; | ||
|  |             this.mOnItemClickLitener = clickLitener; | ||
|  |             this.listItemID = listItemID; | ||
|  |         } | ||
|  | 
 | ||
|  |         public CacheAdapter(Context context, OnItemClickLitener clickLitener) { | ||
|  |             this.context = context; | ||
|  |             this.mOnItemClickLitener = 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 RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | ||
|  |             if(viewType == EMPTY_VIEW){ | ||
|  |                 EmptyViewHolder  holder = new EmptyViewHolder (LayoutInflater.from(context).inflate(R.layout.recycle_list_empty_item, parent, false)); | ||
|  |                 return holder; | ||
|  |             } | ||
|  | 
 | ||
|  |             CacheAdapter.MyViewHolder holder = new CacheAdapter.MyViewHolder(LayoutInflater.from( | ||
|  |                     context).inflate(listItemID, parent, | ||
|  |                     false)); | ||
|  |             return holder; | ||
|  |         } | ||
|  | 
 | ||
|  | 
 | ||
|  |         public void setParameters(List<DownloadTask> mDatas, int listItemID) { | ||
|  |             this.mDatas = mDatas; | ||
|  |             this.listItemID = listItemID; | ||
|  |         } | ||
|  | 
 | ||
|  |         public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) { | ||
|  |             this.mOnItemClickLitener = mOnItemClickLitener; | ||
|  |         } | ||
|  | 
 | ||
|  |         @Override | ||
|  |         public void onBindViewHolder( RecyclerView.ViewHolder hd, int position) { | ||
|  | 
 | ||
|  |             if   (hd instanceof  EmptyViewHolder) { | ||
|  |                 EmptyViewHolder holder = ( EmptyViewHolder)hd; | ||
|  |                 holder.tvEmpty.setVisibility(View.VISIBLE); | ||
|  |                 holder.tvEmpty.setText(norecord); | ||
|  |                 return; | ||
|  |             } | ||
|  |            MyViewHolder holder = ( MyViewHolder)hd; | ||
|  |             holder.tvTitle.setText(mDatas.get(position).getNovelTitle()); | ||
|  |             holder.tvSource.setText(mDatas.get(position).getDomainName()); | ||
|  |             boolean finished = mData.get(position).getFinishedChpats() == mData.get(position).getTotalChapts(); | ||
|  |             holder.tvSize.setText( FileUtils.getCacheSizeK(mData.get(position).getNovelId())); | ||
|  |            if( mData.get(position).getTotalChapts() >0) { | ||
|  |                float progress = mData.get(position).getFinishedChpats() * 100 / mData.get(position).getTotalChapts(); | ||
|  |                holder.tvProgress.setText(String.format("%s/%s", mData.get(position).getFinishedChpats() > mData.get(position).getTotalChapts() ? mData.get(position).getTotalChapts() : mData.get(position).getFinishedChpats(), mData.get(position).getTotalChapts())); | ||
|  |                holder.barProgress.setProgress((int) progress); | ||
|  | 
 | ||
|  |                holder.tvStatus.setText(mData.get(position).getDownSatus() == DownloadTask.DownStatus.正在下载 ? "正在下载" | ||
|  |                        : mData.get(position).getDownSatus() == DownloadTask.DownStatus.暂停下载 ? "暂停下载" : "休眠中"); | ||
|  | 
 | ||
|  |                holder.tvStatus.setText(mData.get(position).getStatus() == 1 || mData.get(position).getFinishedChpats() >= mData.get(position).getTotalChapts() | ||
|  |                        ? "下载完成" : holder.tvStatus.getText()); | ||
|  | 
 | ||
|  | 
 | ||
|  |                holder.tvStatus.setText(mData.get(position).getDownSatus() == DownloadTask.DownStatus.排队中 ? "排队中" : holder.tvStatus.getText()); | ||
|  | 
 | ||
|  |                int img = R.mipmap.play; | ||
|  |                img = mData.get(position).getDownSatus() == DownloadTask.DownStatus.正在下载 ? R.mipmap.pause : R.mipmap.play; | ||
|  | 
 | ||
|  |                holder.imgStart.setImageResource(img); | ||
|  |                holder.imgStart.setEnabled(true); | ||
|  |                if (mData.get(position).getStatus() == 1 || mData.get(position).getFinishedChpats() >= mData.get(position).getTotalChapts()) { | ||
|  |                    holder.tvStatus.setText("下载完成"); | ||
|  |                    holder.imgStart.setEnabled(false); | ||
|  |                    holder.imgStart.setImageResource(R.mipmap.pause); | ||
|  | 
 | ||
|  |                } | ||
|  | 
 | ||
|  |            } | ||
|  |            // holder.imgStart.setVisibility(View.GONE); | ||
|  | 
 | ||
|  |            holder.imgStart.setOnClickListener(new View.OnClickListener() { | ||
|  |                @Override | ||
|  |                public void onClick(View v) { | ||
|  |                    int pos = holder.getLayoutPosition(); | ||
|  | 
 | ||
|  |                     if(mData.get(position).getDownSatus() != DownloadTask.DownStatus.正在下载) { | ||
|  |                         startDownloadService(mData.get(pos).getId(),true); | ||
|  |                         holder.imgStart.setImageResource( R.mipmap.pause); | ||
|  |                         mData.get(pos).setDownSatus(DownloadTask.DownStatus.排队中); | ||
|  | 
 | ||
|  |                     }else{ | ||
|  |                         startDownloadService(mData.get(pos).getId(),false); | ||
|  |                         holder.imgStart.setImageResource( R.mipmap.play); | ||
|  |                         mData.get(pos).setDownSatus(DownloadTask.DownStatus.暂停下载); | ||
|  |                     } | ||
|  |                    mData.get(pos).update( mData.get(pos).getId()); | ||
|  | 
 | ||
|  | 
 | ||
|  |                } | ||
|  |            }); | ||
|  | 
 | ||
|  |             // 如果设置了回调,则设置点击事件 | ||
|  |             if (mOnItemClickLitener != null) { | ||
|  |                 holder.itemView.setOnClickListener(new View.OnClickListener() { | ||
|  |                     @Override | ||
|  |                     public void onClick(View v) { | ||
|  |                         int pos = holder.getLayoutPosition(); | ||
|  |                         mOnItemClickLitener.onItemClick(holder.itemView, pos); | ||
|  |                     } | ||
|  |                 }); | ||
|  |             } | ||
|  | 
 | ||
|  |             holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { | ||
|  |                 @Override | ||
|  |                 public boolean onLongClick(View v) { | ||
|  |                     int pos = holder.getLayoutPosition(); | ||
|  |                     mOnItemClickLitener.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); | ||
|  |             notifyDataSetChanged(); | ||
|  |         } | ||
|  | 
 | ||
|  |         public void setData(List<DownloadTask> data) { | ||
|  |             mData = data; | ||
|  |             notifyDataSetChanged(); | ||
|  |         } | ||
|  | 
 | ||
|  |         public void setNorecord(int msgLoading) { | ||
|  |             norecord = msgLoading; | ||
|  |         } | ||
|  | 
 | ||
|  |         public int getNorecord() { | ||
|  |             return norecord; | ||
|  |         } | ||
|  | 
 | ||
|  |         class EmptyViewHolder extends RecyclerView.ViewHolder { | ||
|  |             @BindView(R.id.tvLoadText) | ||
|  |             TextView tvEmpty; | ||
|  | 
 | ||
|  |             public EmptyViewHolder(View itemView) { | ||
|  |                 super(itemView); | ||
|  |                 ButterKnife.bind(this, itemView); | ||
|  |             } | ||
|  |         } | ||
|  |         class MyViewHolder extends RecyclerView.ViewHolder { | ||
|  |             @BindView(R.id.tvTitle) | ||
|  |             TextView tvTitle; | ||
|  |             @BindView(R.id.tvProgress) | ||
|  |             TextView tvProgress; | ||
|  |             @BindView(R.id.tvStatus) | ||
|  |             TextView tvStatus; | ||
|  |             @BindView(R.id.barProgress) | ||
|  |             ProgressBar barProgress; | ||
|  |             @BindView(R.id.imgStart) | ||
|  |             ImageView imgStart; | ||
|  |             @BindView(R.id.tvSource) | ||
|  |             TextView tvSource; | ||
|  |             @BindView(R.id.tvSize) | ||
|  |             TextView tvSize; | ||
|  |             public MyViewHolder(View view) { | ||
|  |                 super(view); | ||
|  |                 ButterKnife.bind(this, view); | ||
|  |                 //tvTitle = (TextView) view.findViewById(R.id.title); | ||
|  |                 // tvAuthor = (TextView) view.findViewById(R.id.author); | ||
|  | 
 | ||
|  |             } | ||
|  |         } | ||
|  |     } | ||
|  |     interface OnItemClickLitener | ||
|  |     { | ||
|  |         void onItemClick(View view, int position); | ||
|  | 
 | ||
|  |         void onItemLongClick(View itemView, int pos); | ||
|  |     } | ||
|  | 
 | ||
|  | 
 | ||
|  | } |