static void download(final DownloadJob job) throws IOException {
    final HttpURLConnection connection = (HttpURLConnection) job.url.openConnection();
    connection.setRequestMethod("GET");
    if (job.headers != null) {
      for (final Entry<String, String> e : job.headers.entrySet()) {
        connection.addRequestProperty(e.getKey(), e.getValue());
      }
    }
    connection.connect();

    job.responseCode = connection.getResponseCode();
    job.responseHeaders.clear();
    job.responseText = null;

    for (int i = 1; ; i++) {
      final String key = connection.getHeaderFieldKey(i);
      if (key == null) {
        break;
      }
      final String value = connection.getHeaderField(i);
      job.responseHeaders.put(key.toLowerCase(), value);
    }

    final StringBuilder sb = new StringBuilder();
    final BufferedReader r =
        new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
    try {
      for (int ch = r.read(); ch != -1; ch = r.read()) {
        sb.append((char) ch);
      }
      job.responseText = sb.toString();
    } finally {
      r.close();
    }
  }
 @Override
 public boolean trackAvailable(Track track) {
   for (DownloadJob dJob : mCompletedJobs) {
     if (track.getId() == dJob.getPlaylistEntry().getTrack().getId()) {
       return true;
     }
   }
   return false;
 }
 public void removeDownload(DownloadJob job) {
   if (job.getProgress() < 100) {
     job.cancel();
     mQueuedJobs.remove(job);
   } else {
     mCompletedJobs.remove(job);
   }
   mDb.remove(job);
   mDownloadManager.notifyObservers();
 }
 private void loadOldDownloads() {
   ArrayList<DownloadJob> oldDownloads = mDb.getAllDownloadJobs();
   for (DownloadJob dJob : oldDownloads) {
     if (dJob.getProgress() == 100) {
       mCompletedJobs.add(dJob);
     } else {
       mDownloadManager.download(dJob.getPlaylistEntry());
     }
   }
   mDownloadManager.notifyObservers();
 }
    @Override
    protected void onPostExecute(Boolean success) {
      AttachmentView attachmentView = downloadJob.getAttachmentView();
      downloadJob.setSuccess(success);

      if (success) {
        attachmentView.setImage(bitmap, bitmapOrientation);

      } else {
        if (!downloadJob.hasRetry()) {
          attachmentView.signalImageLoadingError();
        }
      }

      handler.sendEmptyMessage(0);
    }
Ejemplo n.º 6
0
  private DownloadJob buildJob(Cursor cursor) {
    DownloadEntry entry = new DownloadEntry();
    entry.aid = cursor.getString(cursor.getColumnIndex(COLUMN_AID));
    entry.title = cursor.getString(cursor.getColumnIndex(COLUMN_TITLE));
    entry.destination = cursor.getString(cursor.getColumnIndex(COLUMN_DEST));

    entry.part = new VideoPart();
    entry.part.vid = cursor.getString(cursor.getColumnIndex(COLUMN_VID));
    entry.part.subtitle = cursor.getString(cursor.getColumnIndex(COLUMN_SUBTITLE));
    entry.part.vtype = cursor.getString(cursor.getColumnIndex(COLUMN_VTYPE));
    entry.part.segments = new ArrayList<VideoSegment>();
    DownloadJob job = new DownloadJob(entry);
    job.setUserAgent(cursor.getString(cursor.getColumnIndex(COLUMN_UA)));
    int status = cursor.getInt(cursor.getColumnIndex(COLUMN_STATUS));
    job.setStatus(status);
    entry.part.isDownloaded = status == STATUS_SUCCESS;
    return job;
  }
Ejemplo n.º 7
0
 @Override
 public int remove(DownloadJob job) {
   if (mDb == null) return 0;
   DownloadEntry entry = job.getEntry();
   String whereClause = COLUMN_AID + "=? and " + COLUMN_VID + "=?";
   String[] args = new String[] {entry.aid, entry.part.vid};
   int delete = mDb.delete(DOWNLOAD_TABLE, whereClause, args);
   return delete;
 }
    private void loadImageThumbnail() {
      try {
        String filename = downloadJob.getFeedbackAttachment().getCacheId();
        AttachmentView attachmentView = downloadJob.getAttachmentView();

        bitmapOrientation = ImageUtils.determineOrientation(new File(dropFolder, filename));
        int width =
            bitmapOrientation == ImageUtils.ORIENTATION_LANDSCAPE
                ? attachmentView.getWidthLandscape()
                : attachmentView.getWidthPortrait();
        int height =
            bitmapOrientation == ImageUtils.ORIENTATION_LANDSCAPE
                ? attachmentView.getMaxHeightLandscape()
                : attachmentView.getMaxHeightPortrait();

        bitmap = ImageUtils.decodeSampledBitmap(new File(dropFolder, filename), width, height);

      } catch (IOException e) {
        e.printStackTrace();
        bitmap = null;
      }
    }
    @Override
    protected Boolean doInBackground(Void... args) {
      FeedbackAttachment attachment = downloadJob.getFeedbackAttachment();

      if (attachment.isAvailableInCache()) {
        HockeyLog.error("Cached...");
        loadImageThumbnail();
        return true;

      } else {
        HockeyLog.error("Downloading...");
        boolean success = downloadAttachment(attachment.getUrl(), attachment.getCacheId());
        if (success) {
          loadImageThumbnail();
        }
        return success;
      }
    }
  public boolean queueDownload(DownloadJob downloadJob) {
    for (DownloadJob dJob : mCompletedJobs) {
      if (dJob.getPlaylistEntry().getTrack().getId()
          == downloadJob.getPlaylistEntry().getTrack().getId()) return false;
    }

    for (DownloadJob dJob : mQueuedJobs) {
      if (dJob.getPlaylistEntry().getTrack().getId()
          == downloadJob.getPlaylistEntry().getTrack().getId()) return false;
    }

    if (mDb.addToLibrary(downloadJob.getPlaylistEntry())) {
      mQueuedJobs.add(downloadJob);
      mDownloadManager.notifyObservers();
      return true;
    } else {
      return false;
    }
  }
Ejemplo n.º 11
0
  @Override
  // TODO 这个方法似乎没有用了
  public int updateDownload(DownloadJob job) {
    if (mDb == null || job == null) {
      return 0;
    }

    DownloadEntry entry = job.getEntry();
    if (entry == null || entry.part == null || entry.part.segments == null)
      throw new IllegalEntryException("check your entry data!");

    ContentValues values = new ContentValues();
    values.put(COLUMN_STATUS, STATUS_PENDING);
    values.put(COLUMN_CURRENT, 0);
    values.put(COLUMN_AID, entry.aid);
    values.put(COLUMN_TITLE, entry.title);
    values.put(COLUMN_DEST, entry.destination);
    values.put(COLUMN_VID, entry.part.vid);
    values.put(COLUMN_VTYPE, entry.part.vtype);
    values.put(COLUMN_SUBTITLE, entry.part.subtitle);
    String whereClause = COLUMN_AID + "=? and " + COLUMN_VID + "=? and " + COLUMN_NUM + "=?";
    int rowCount = 0;
    for (int i = 0; i < entry.part.segments.size(); i++) {
      VideoSegment s = entry.part.segments.get(i);
      values.put(COLUMN_TOTAL, s.size);
      values.put(COLUMN_NUM, s.num);
      values.put(COLUMN_URL, s.stream);
      rowCount =
          mDb.update(
              DOWNLOAD_TABLE,
              values,
              whereClause,
              new String[] {entry.aid, entry.part.vid, String.valueOf(s.num)});
    }
    return rowCount;
  }
  @Override
  public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    // 网络发生变化时
    if (NET_ACTION.equals(action)) {
      ConnectivityManager manager =
          (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
      // 当切换到只有移动网络,只有当非只在wifi下才自动开启下载
      if (wifi.isConnected()) {
        for (DownloadJob job : BrowserApp.getInstance().getDownloadManager().getQueuedDownloads()) {
          if (!job.isUserPause()
              && (job.getStatus() == DownloadJob.PAUSE || job.getStatus() == DownloadJob.WAITING))
            job.start();
        }
      }
    }

    if (SPEED_ACTION.equals(action)) {
      boolean cutSpeed = intent.getBooleanExtra("player", false);
      if (cutSpeed) {
        int dn = BrowserApp.getInstance().getDownloadManager().downloading_num;
        for (DownloadJob job : BrowserApp.getInstance().getDownloadManager().getQueuedDownloads()) {
          if (job.getStatus() == DownloadJob.DOWNLOADING) {
            job.dn = dn;
            job.pauseOnSpeed();
          }
          BrowserApp.getInstance().getDownloadManager().notifyObservers();
        }
      } else {
        for (DownloadJob job : BrowserApp.getInstance().getDownloadManager().getQueuedDownloads()) {
          if (job.getStatus() == DownloadJob.PAUSEONSPEED) job.start();
        }
      }
    }

    if (DOWNLOADONLYWIFI.equals(action)) {
      boolean isDownloadOnlyWifi = intent.getBooleanExtra("isDownloadOnlyWifi", true);
      // 当非移动网络时
      if (!(NetworkUtil.reportNetType(BrowserApp.getInstance()) == 2)) return;
      if (isDownloadOnlyWifi) {
        for (DownloadJob job : BrowserApp.getInstance().getDownloadManager().getQueuedDownloads()) {
          if (job.getStatus() == DownloadJob.DOWNLOADING) job.pauseOnSetWifiChange();
        }
      } else {
        for (DownloadJob job : BrowserApp.getInstance().getDownloadManager().getQueuedDownloads()) {
          if (job.getStatus() == DownloadJob.PAUSE) {
            job.getEntity().setUserPauseWhen3G(false);
            job.start();
          }
        }
      }
    }

    if (OFFLINECACHE.equals(action)) {
      boolean isDownload = intent.getBooleanExtra("download", true);
      if (isDownload) {
        for (DownloadJob job : BrowserApp.getInstance().getDownloadManager().getQueuedDownloads()) {
          if (job.getStatus() == DownloadJob.PAUSEONOFFLINECACHE) {
            job.start();
          }
        }
      } else {
        for (DownloadJob job : BrowserApp.getInstance().getDownloadManager().getQueuedDownloads()) {
          if (job.getStatus() == DownloadJob.DOWNLOADING) {
            job.pauseOnOfflineCache();
          }
        }
      }
    }
  }
Ejemplo n.º 13
0
  @Override
  public List<DownloadJob> getAllDownloads() {
    List<DownloadJob> all = new ArrayList<DownloadJob>();
    if (mDb != null) {
      Cursor query = mDb.query(DOWNLOAD_TABLE, null, null, null, null, null, null);
      DownloadJob job = null;
      try {
        for (query.moveToFirst(); !query.isAfterLast(); query.moveToNext()) {
          String aid = query.getString(query.getColumnIndex(COLUMN_AID));
          String vid = query.getString(query.getColumnIndex(COLUMN_VID));
          // 是否同一个part
          if (job == null
              || !job.getEntry().aid.equals(aid)
              || !job.getEntry().part.vid.equals(vid)) {
            job = buildJob(query);
            all.add(job);
          }
          VideoSegment s = new VideoSegment();
          s.num = query.getInt(query.getColumnIndex(COLUMN_NUM));
          s.size = query.getLong(query.getColumnIndex(COLUMN_TOTAL));
          s.stream = query.getString(query.getColumnIndex(COLUMN_URL));
          s.etag = query.getString(query.getColumnIndex(COLUMN_ETAG));
          s.fileName = query.getString(query.getColumnIndex(COLUMN_DATA));
          s.duration = query.getLong(query.getColumnIndex(COLUMN_DURATION));
          job.getEntry().part.segments.add(s);
          if (AcApp.isExternalStorageAvailable()) {
            if (!TextUtils.isEmpty(job.getEntry().destination) && !TextUtils.isEmpty(s.fileName)) {
              // 设置segment的播放路径为本地文件uri
              // 实现边下边播的功能

              File file = new File(job.getEntry().destination, s.fileName);
              if (file.exists()) {
                s.url = Uri.fromFile(file).toString();
              } else s.url = s.stream;
              int downloadedSize = (int) file.length();
              job.addDownloadedSize(downloadedSize);
              if (query.getInt(query.getColumnIndex(COLUMN_CURRENT)) != downloadedSize) {
                // 数据库中的数据有误!
                // 写入正确的数据
                // XXX: 也许根本就不需要current_bytes这个字段...

                ContentValues values = new ContentValues();
                values.put(COLUMN_CURRENT, downloadedSize);
                updateDownload(job.getEntry().part.vid, s.num, values);
              }
            }
          }

          // 真实的下载进度由file.length来确定比较好。
          // @see downloadTask.setupDestinationFile()
          // int downloadedSize = query.getInt(query.getColumnIndex(COLUMN_CURRENT));
          // job.addDownloadedSize(downloadedSize);

          int totalSize = query.getInt(query.getColumnIndex(COLUMN_TOTAL));
          if (totalSize != -1) job.addTotalSize(totalSize);
        }
      } catch (Exception e) {
      } finally {
        query.close();
      }
    }
    return all;
  }
 public void downloadCompleted(DownloadJob job) {
   mQueuedJobs.remove(job);
   mCompletedJobs.add(job);
   mDb.setStatus(job.getPlaylistEntry(), true);
   mDownloadManager.notifyObservers();
 }