package cn.shangyu.gdxzExpert.activity; import android.app.Dialog; import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.BaseAdapter; import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import com.gyf.immersionbar.ImmersionBar; import java.io.File; import java.util.ArrayList; import cn.shangyu.gdxzExpert.R; import cn.shangyu.gdxzExpert.base.BaseActivity; import cn.shangyu.gdxzExpert.ecdemo.storage.CoursewareBookDownloadSqlManager; import cn.shangyu.gdxzExpert.ecdemo.ui.chatting.model.DownloadModel; import cn.shangyu.gdxzExpert.pulltorefreshview.PullToRefreshBase; import cn.shangyu.gdxzExpert.pulltorefreshview.PullToRefreshBase.Mode; import cn.shangyu.gdxzExpert.pulltorefreshview.PullToRefreshBase.OnRefreshListener; import cn.shangyu.gdxzExpert.pulltorefreshview.PullToRefreshListView; import cn.shangyu.gdxzExpert.utils.CommonUtil; import cn.shangyu.gdxzExpert.utils.Constant; import cn.shangyu.gdxzExpert.utils.SharePrefUtil; import cn.shangyu.gdxzExpert.utils.TikaUtil; import cn.shangyu.gdxzExpert.utils.ToastUtil; import cn.shangyu.gdxzExpert.utils.UIUtils; import cn.shangyu.gdxzExpert.widget.ECListDialog; import cn.shangyu.gdxzExpert.widget.LoadingPage.ResultState; public class MyDownLoadCourseActivity extends BaseActivity implements OnItemClickListener, OnItemLongClickListener { private PullToRefreshListView ptrLv; private TextView load_empty; private ArrayList list = new ArrayList();; public final static int REQUESTCODE = 222; private static MyDownLoadCourseActivity instance; private CourseDownLoadAdapter mAdapter; CoursewareBookDownloadSqlManager sq; SQLiteDatabase db; public static MyDownLoadCourseActivity getInstance() { return instance; } @Override public void setTitle() { top_back_layout.setVisibility(View.VISIBLE); top_title.setText("课件文档"); } @Override public View onCreateSuccessedView() { ImmersionBar.with(this).keyboardEnable(true).statusBarDarkFont(true).titleBar(mToolbar).init(); View view = View.inflate(ct, R.layout.activity_my_download, null); instance = this; ptrLv = (PullToRefreshListView) view.findViewById(R.id.lv_item_down);// 下拉刷新的listview控件 // 处理正在加载,空,错误的布局 load_empty = (TextView) view.findViewById(R.id.load_empty); ll_loading = (FrameLayout) view.findViewById(R.id.ll_loading); ll_load_progress = (LinearLayout) view .findViewById(R.id.ll_load_progress); tv_load_fail = (TextView) view.findViewById(R.id.tv_load_fail); // 设置只有下拉刷新 ptrLv.setMode(Mode.DISABLED); // 得到实际的ListView 设置点击 ptrLv.getRefreshableView().setOnItemClickListener(this); ptrLv.getRefreshableView().setOnItemLongClickListener(this); ptrLv.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh(PullToRefreshBase refreshView) { // getDownLoadList(url); } }); db = openOrCreateDatabase("courseware.db", MODE_PRIVATE, null); sq = new CoursewareBookDownloadSqlManager(db); load_empty.setText("您暂未下载课件"); initData(); return view; } @Override public ResultState onLoad() { return ResultState.STATE_SUCCESSED; } /** * 实例化数据 */ private void initData() { showProgressDialog("正在加载"); list.clear(); ArrayList downList; ArrayList downListold; downList = CoursewareBookDownloadSqlManager.getDownloadInfoByState(2); downListold= CoursewareBookDownloadSqlManager.getDownloadInfoByState(4); if (downListold != null &&!downListold.isEmpty()) { if (downList == null || downList.isEmpty()) { downList=new ArrayList<>(); } downList.addAll(downListold); } if (downList == null || downList.isEmpty()) { ptrLv.setEmptyView(load_empty); } else { for (int i = 0; i < downList.size(); i++) { list.add(0, downList.get(i)); } if (mAdapter == null) { mAdapter = new CourseDownLoadAdapter(); ptrLv.getRefreshableView().setAdapter(mAdapter); } else { mAdapter.notifyDataSetChanged(); } onLoaded(); } closeProgressDialog(); } public void notifyAdapter() { if (mAdapter != null) { mAdapter.notifyDataSetChanged(); } } /** * 数据加载完成时,取消加载动画 */ private void onLoaded() { dismissLoadingProgress(); ptrLv.onRefreshComplete(); } @Override protected void processClick(View v) { } // android获取一个用于打开Word文件的intent public void openWordFile(String Path, String uuid,int position) { File file = new File(Path); SharedPreferences sp = getSharedPreferences(SharePrefUtil.SP_DOWN, Context.MODE_PRIVATE); String fileMD5_ = sp.getString(uuid+"fileMD5",""); String fileMD5 = TikaUtil.getFileMD5(file); if (TextUtils.isEmpty(fileMD5_)){ Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(file); intent.setDataAndType(uri, "application/msword"); try { ct.startActivity(intent); } catch (ActivityNotFoundException e) { ToastUtil.showToast("打开失败, 请安装相应应用!"); } }else if (file.exists()&&fileMD5_.contains(fileMD5)) { Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(file); intent.setDataAndType(uri, "application/msword"); try { ct.startActivity(intent); } catch (ActivityNotFoundException e) { ToastUtil.showToast("打开失败, 请安装相应应用!"); } } else { list.remove(position); CoursewareBookDownloadSqlManager.delDownladInfo(uuid); mAdapter.notifyDataSetChanged(); if (list.isEmpty()) { ptrLv.setEmptyView(load_empty); } CoursewareBookDownloadSqlManager.delDownladInfo(uuid); ToastUtil.showToast("打开失败,已经删除,请重新下载!"); } } public void openPPTFile(String Path, String uuid,int position) { File file = new File(Path); SharedPreferences sp = getSharedPreferences(SharePrefUtil.SP_DOWN, Context.MODE_PRIVATE); String fileMD5_ = sp.getString(uuid+"fileMD5",""); String fileMD5 = TikaUtil.getFileMD5(file); if (TextUtils.isEmpty(fileMD5_)){ Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(file); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); try { ct.startActivity(intent); } catch (ActivityNotFoundException e) { ToastUtil.showToast("打开失败, 请安装相应应用!"); } }else if (file.exists()&&fileMD5_.contains(fileMD5)) { Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(file); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); try { ct.startActivity(intent); } catch (ActivityNotFoundException e) { ToastUtil.showToast("打开失败, 请安装相应应用!"); } } else { list.remove(position); CoursewareBookDownloadSqlManager.delDownladInfo(uuid); mAdapter.notifyDataSetChanged(); if (list.isEmpty()) { ptrLv.setEmptyView(load_empty); } CoursewareBookDownloadSqlManager.delDownladInfo(uuid); ToastUtil.showToast("打开失败,已经删除,请重新下载!"); } } public void openPDFFile(final String path, String uuid,int position) { File file = new File(path); SharedPreferences sp = getSharedPreferences(SharePrefUtil.SP_DOWN, Context.MODE_PRIVATE); String fileMD5_ = sp.getString(uuid+"fileMD5",""); String fileMD5 = TikaUtil.getFileMD5(file); if (TextUtils.isEmpty(fileMD5_)){ Intent intent1 = new Intent(MyDownLoadCourseActivity.this,PdfViewActivity.class); intent1.putExtra("path",path); intent1.putExtra("title","文献详情"); startActivity(intent1); }else if (file.exists()&&fileMD5_.contains(fileMD5)) { Intent intent1 = new Intent(MyDownLoadCourseActivity.this,PdfViewActivity.class); intent1.putExtra("path",path); intent1.putExtra("title","文献详情"); startActivity(intent1); } else { list.remove(position); CoursewareBookDownloadSqlManager.delDownladInfo(uuid); mAdapter.notifyDataSetChanged(); if (list.isEmpty()) { ptrLv.setEmptyView(load_empty); } CoursewareBookDownloadSqlManager.delDownladInfo(uuid); ToastUtil.showToast("打开失败,已经删除,请重新下载!"); } } @Override public void onItemClick(AdapterView parent, View view, int position, long id) { int p = 0; if (ptrLv.getRefreshableView().getHeaderViewsCount() > 0) { p = position - ptrLv.getRefreshableView().getHeaderViewsCount(); } String filePath = list.get(p).getFilePath(); String listUuid = list.get(p).getUuid(); if (!TextUtils.isEmpty(filePath)) { if(filePath.contains(Constant.OldfilePath)||list.get(p).getState()==4) { Intent intent = new Intent(MyDownLoadCourseActivity.getInstance(), CourseUrlDetailActivity.class); intent.putExtra("url", list.get(p).getPath()); intent.putExtra("title", list.get(p).getTitle()); intent.putExtra("uuid", list.get(p).getUuid()); // intent.putExtra("again_download",true); // intent.putExtra("type", list.get(p).getType()); startActivity(intent); return; } if (list.get(position - 1).getFileFormat() != null && list.get(position - 1).getFileFormat().contains("pdf")) { openPDFFile(filePath, listUuid,p); } else if (list.get(position - 1).getFileFormat() != null && list.get(position - 1).getFileFormat().contains("ppt")) { openPPTFile(filePath, listUuid,p); } else if (list.get(position - 1).getFileFormat() != null && list.get(position - 1).getFileFormat().contains("doc")) { openWordFile(filePath, listUuid,p); } } else { // 从数据库清除此下载记录 // list移除相应位置 list.remove(p); CoursewareBookDownloadSqlManager.delDownladInfo(listUuid); mAdapter.notifyDataSetChanged(); if (list.isEmpty()) { ptrLv.setEmptyView(load_empty); } Intent intent = getIntent(); setResult(RESULT_OK, intent); ToastUtil.showToast("文件不存在,请重新下载!"); } } /** * 打开文件 openFile * * @param * @param * * @param position */ @Override public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { // TODO Auto-generated method stub int p = 0; if (ptrLv.getRefreshableView().getHeaderViewsCount() > 0) { p = position - ptrLv.getRefreshableView().getHeaderViewsCount(); } String filePath = list.get(p).getFilePath(); String listUuid = list.get(p).getUuid(); if (!TextUtils.isEmpty(filePath)) { delVisit(p, filePath, listUuid); } else { // 从数据库清除此下载记录 // list移除相应位置 list.remove(p); CoursewareBookDownloadSqlManager.delDownladInfo(listUuid); mAdapter.notifyDataSetChanged(); if (list.isEmpty()) { ptrLv.setEmptyView(load_empty); } ToastUtil.showToast("文件不存在!"); } return true; } public void delVisit(final int positions, final String path, final String uuid) { final ECListDialog dialog = new ECListDialog(instance,new String[] { "删除文章" }); dialog.setOnDialogItemClickListener(new ECListDialog.OnDialogItemClickListener() { @Override public void onDialogItemClick(Dialog d, int position) { // canclePatient(CanclePatientUrl, patientUuid, uuid, p); File file = new File(path); if (file.exists()) { file.delete(); } // list移除相应位置 list.remove(positions); CoursewareBookDownloadSqlManager.delDownladInfo(uuid); mAdapter.notifyDataSetChanged(); if (list.isEmpty()) { ptrLv.setEmptyView(load_empty); } dialog.dismiss(); } }); dialog.setTitle("删除此文章"); dialog.show(); } public class CourseDownLoadAdapter extends BaseAdapter { @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder vh = null; if (convertView == null) { if(CommonUtil.BigModule()) { convertView = UIUtils.inflate(R.layout.activity_download_course_big); } else { convertView = UIUtils.inflate(R.layout.activity_download_course); } vh = new ViewHolder(); vh.mTv = (TextView) convertView .findViewById(R.id.tv_download_course); convertView.setTag(vh); } else { vh = (ViewHolder) convertView.getTag(); } vh.mTv.setText(list.get(position).getTitle()); return convertView; } public class ViewHolder { TextView mTv; } } }