예제 #1
0
 @Override
 public void run() {
   while (!isStop() && !isFinish()) {
     try {
       URL url = new URL(info.getUrl());
       HttpURLConnection con = (HttpURLConnection) url.openConnection();
       String prop = "bytes=" + info.getPos() + "-";
       System.out.println(prop);
       System.out.println(info.getFileSize());
       con.setRequestProperty("RANGE", prop);
       InputStream is = con.getInputStream();
       raf = new RandomAccessFile(info.getFileName(), "rw");
       raf.seek(info.getPos());
       byte[] buffer = new byte[BUFFER_SIZE];
       int b = -1;
       while ((b = is.read(buffer)) != -1 && !isStop()) {
         raf.write(buffer, 0, b);
         info.setPos(info.getPos() + b);
       }
       if (!isStop()) {
         isFinish = true;
       }
     } catch (MalformedURLException e) {
       e.printStackTrace();
     } catch (IOException e) {
       e.printStackTrace();
     } finally {
       try {
         raf.close();
       } catch (IOException e) {
       }
     }
   }
 }
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {

      convertView = mInflater.inflate(R.layout.item_download, null);
      holder = new ViewHolder();
      holder.resourceDownloadName = (TextView) convertView.findViewById(R.id.downloadfilename);
      holder.resouceDownloadState = (TextView) convertView.findViewById(R.id.download_state);
      holder.resourceImage = (ImageView) convertView.findViewById(R.id.movieImageview);
      holder.resourceDownProgress = (ProgressBar) convertView.findViewById(R.id.downloadprogress);
      holder.resourcePercentDown = (TextView) convertView.findViewById(R.id.precentDownload);
      holder.down_wait = (ImageView) convertView.findViewById(R.id.down_wait);
      holder.down_pause = (ImageView) convertView.findViewById(R.id.down_pause);
      holder.down_downing = (ImageView) convertView.findViewById(R.id.down_downing);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    aqtemp = new AQuery(convertView);
    if (position < data.size()) {
      DownloadInfo info = data.get(position);
      long completesize = info.getCompeleteSize();
      long filesize = info.getFileSize();
      long percent = 0;
      if (filesize == 0) {

      } else {
        percent = completesize * MAX / filesize;
      }
      String posterurl = info.getUrlposter();
      holder.resourceDownloadName.setText(info.getMy_name());
      if (info.getMy_index().contains("movie")) {
        if (info.getDownload_state().equalsIgnoreCase("wait")) {
          holder.resouceDownloadState.setText("等待下载");
          aqtemp.id(R.id.down_wait).visible();
          aqtemp.id(R.id.down_pause).gone();
          aqtemp.id(R.id.down_downing).gone();
        } else if (info.getDownload_state().equalsIgnoreCase("downloading")) {
          holder.resouceDownloadState.setText("正在下载");
          aqtemp.id(R.id.down_wait).gone();
          aqtemp.id(R.id.down_pause).gone();
          aqtemp.id(R.id.down_downing).visible();
        } else if (info.getDownload_state().equalsIgnoreCase("pause")) {
          holder.resouceDownloadState.setText("暂停下载");
          aqtemp.id(R.id.down_wait).gone();
          aqtemp.id(R.id.down_pause).visible();
          aqtemp.id(R.id.down_downing).gone();
        }
        holder.resourceDownProgress.setMax(MAX);
        if (filesize != 0) {
          holder.resourceDownProgress.setSecondaryProgress((int) (completesize * MAX / filesize));
        } else {
          holder.resourceDownProgress.setSecondaryProgress(0);
        }

        holder.resourcePercentDown.setText((percent) + "%");
        if (info.getCompeleteSize() == info.getFileSize()) {
          if (info.getFileSize() > 10) // 过滤掉文件长度为零的情况
          {
            holder.resouceDownloadState.setText("");
            holder.resourcePercentDown.setText("");
            aqtemp.id(R.id.downloadprogress).gone();

            aqtemp.id(R.id.down_wait).gone();
            aqtemp.id(R.id.down_pause).gone();
            aqtemp.id(R.id.down_downing).gone();
          }
        }
      } else {
        aqtemp.id(R.id.downloadprogress).gone();
        aqtemp.id(R.id.state_layer).gone();
      }
      aqtemp.id(R.id.movieImageview).image(posterurl, true, true);
    }
    return convertView;
  }