Example #1
0
  @Override
  public synchronized void onProgress(int progress) {
    // 已下载大小
    totalProgress += progress;
    info.progress = totalProgress;
    info.currentBytes = totalProgress;

    // 计算下载速度
    long totalTime = (System.currentTimeMillis() - mPreviousTime) / 1000;
    if (totalTime == 0) {
      totalTime += 1;
    }
    long networkSpeed = totalProgress / totalTime;
    info.networkSpeed = networkSpeed;

    long currentTime = System.currentTimeMillis();
    if (currentTime - lastTime > 1000 || totalProgress == info.totalBytes) {
      // LogUtils.e(info.appName + "  下载进度:" + totalProgress);
      try {
        mDLInfoDao.update(info);
      } catch (SQLException e) {
        e.printStackTrace();
      }
      if (info.hasListener) info.listener.onProgress(info);
      EventBus.getDefault().post(info, "onProgress");
      lastTime = currentTime;
    }
  }
Example #2
0
 @Override
 public synchronized void onStop(DLThreadInfo threadInfo) {
   // DLDBManager.getInstance(context).updateThreadInfo(threadInfo);
   try {
     mDLThreadInfoDao.update(threadInfo);
   } catch (SQLException e) {
     e.printStackTrace();
   }
   count++;
   if (count >= info.threads.size()) {
     LogUtils.e("All the threads was stopped.");
     info.currentBytes = totalProgress;
     info.state = DLState.PAUSE;
     DLManager.getInstance(context).addStopTask(info).removeDLTask(info.baseUrl);
     // DLDBManager.getInstance(context).updateTaskInfo(info);
     try {
       mDLInfoDao.update(info);
     } catch (SQLException e) {
       e.printStackTrace();
     }
     count = 0;
     if (info.hasListener) info.listener.onStop(info);
     EventBus.getDefault().post(info, "onStop");
   }
 }
Example #3
0
  @Override
  public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
    while (info.redirect < MAX_REDIRECTS) {
      HttpURLConnection conn = null;
      try {
        conn = (HttpURLConnection) new URL(info.realUrl).openConnection();
        conn.setInstanceFollowRedirects(false);
        conn.setConnectTimeout(DEFAULT_TIMEOUT);
        conn.setReadTimeout(DEFAULT_TIMEOUT);

        addRequestHeaders(conn);

        final int code = conn.getResponseCode();
        switch (code) {
          case HTTP_OK:
          case HTTP_PARTIAL:
            dlInit(conn, code);
            return;
          case HTTP_MOVED_PERM:
          case HTTP_MOVED_TEMP:
          case HTTP_SEE_OTHER:
          case HTTP_NOT_MODIFIED:
          case HTTP_TEMP_REDIRECT:
            final String location = conn.getHeaderField("location");
            if (TextUtils.isEmpty(location))
              throw new DLException("Can not obtain real url from location in header.");
            info.realUrl = location;
            info.redirect++;
            continue;
          default:
            if (info.hasListener) info.listener.onError(code, conn.getResponseMessage(), info);
            EventBus.getDefault().post(info, "onError");
            DLManager.getInstance(context).removeDLTask(info.baseUrl);
            info.state = DLState.FAIL;
            try {
              mDLInfoDao.update(info);
            } catch (SQLException e1) {
              e1.printStackTrace();
            }
            return;
        }
      } catch (Exception e) {
        if (info.hasListener) info.listener.onError(ERROR_OPEN_CONNECT, e.toString(), info);
        EventBus.getDefault().post(info, "onError");
        DLManager.getInstance(context).removeDLTask(info.baseUrl);
        info.state = DLState.FAIL;
        try {
          mDLInfoDao.update(info);
        } catch (SQLException e1) {
          e1.printStackTrace();
        }
        return;
      } finally {
        if (null != conn) conn.disconnect();
      }
    }
    throw new RuntimeException("Too many redirects");
  }
Example #4
0
 private void dlDispatch() {
   int threadSize;
   int threadLength = LENGTH_PER_THREAD;
   if (info.totalBytes <= LENGTH_PER_THREAD) {
     threadSize = 2;
     threadLength = info.totalBytes / threadSize;
   } else {
     threadSize = info.totalBytes / LENGTH_PER_THREAD;
   }
   int remainder = info.totalBytes % threadLength;
   for (int i = 0; i < threadSize; i++) {
     int start = i * threadLength;
     int end = start + threadLength - 1;
     if (i == threadSize - 1) {
       end = start + threadLength + remainder;
     }
     DLThreadInfo threadInfo =
         new DLThreadInfo(UUID.randomUUID().toString(), info.baseUrl, start, end);
     info.addDLThread(threadInfo);
     // DLDBManager.getInstance(context).insertThreadInfo(threadInfo);
     try {
       mDLThreadInfoDao.save(threadInfo);
     } catch (SQLException e) {
       e.printStackTrace();
     }
     mPreviousTime = System.currentTimeMillis();
     DLManager.getInstance(context).addDLThread(new DLThread(threadInfo, info, this));
   }
 }
Example #5
0
 @Override
 public synchronized void onFinish(DLThreadInfo threadInfo) {
   LogUtils.e("已经完成:" + info.appName + "  进度:" + info.progress + "  " + threadInfo);
   if (null == threadInfo) {
     if (info.hasListener) {
       try {
         info.state = DLState.COMPLETE;
         mDLInfoDao.update(info);
       } catch (SQLException e) {
         e.printStackTrace();
       }
       info.listener.onProgress(info);
       info.listener.onFinish(info);
     }
     EventBus.getDefault().post(info, "onProgress");
     EventBus.getDefault().post(info, "onFinish");
     return;
   }
   info.removeDLThread(threadInfo);
   // DLDBManager.getInstance(context).deleteThreadInfo(threadInfo.id);
   try {
     mDLThreadInfoDao.delete(DBCons.TB_THREAD_ID, threadInfo.id);
   } catch (SQLException e) {
     e.printStackTrace();
   }
   LogUtils.e("Thread size " + info.threads.size());
   if (info.threads.isEmpty()) {
     LogUtils.e("Task was finished.");
     DLManager.getInstance(context).removeDLTask(info.baseUrl);
     // DLDBManager.getInstance(context).deleteTaskInfo(info.baseUrl);
     try {
       info.state = DLState.COMPLETE;
       mDLInfoDao.update(info);
     } catch (SQLException e) {
       e.printStackTrace();
     }
     LogUtils.e("info.hasListener=" + info.hasListener);
     if (info.hasListener) {
       info.listener.onProgress(info);
       info.listener.onFinish(info);
     }
     EventBus.getDefault().post(info, "onProgress");
     EventBus.getDefault().post(info, "onFinish");
     DLManager.getInstance(context).addDLTask();
   }
 }
Example #6
0
 private void dlInit(HttpURLConnection conn, int code) throws Exception {
   readResponseHeaders(conn);
   // DLDBManager.getInstance(context).updateTaskInfo(info);
   try {
     LogUtils.e("保存数据库前安装包的大小:" + info.totalBytes);
     mDLInfoDao.update(info);
   } catch (SQLException e) {
     e.printStackTrace();
   }
   if (!DLUtil.createFile(info.dirPath, info.fileName))
     throw new DLException("Can not create file");
   info.file = new File(info.dirPath, info.fileName);
   // if (info.file.exists() && info.file.length() == info.totalBytes) {
   //    Log.d(TAG, "The file which we want to download was already here.");
   //    return;
   // }
   if (info.hasListener) info.listener.onStart(info);
   EventBus.getDefault().post(info, "onStart");
   switch (code) {
     case HTTP_OK:
       mPreviousTime = System.currentTimeMillis();
       dlData(conn);
       break;
     case HTTP_PARTIAL:
       if (info.totalBytes <= 0) {
         mPreviousTime = System.currentTimeMillis();
         dlData(conn);
         break;
       }
       if (info.isResume) {
         mPreviousTime = System.currentTimeMillis();
         for (DLThreadInfo threadInfo : info.threads) {
           DLManager.getInstance(context).addDLThread(new DLThread(threadInfo, info, this));
         }
         break;
       }
       dlDispatch();
       break;
   }
 }
Example #7
0
 private void readResponseHeaders(HttpURLConnection conn) {
   info.disposition = conn.getHeaderField("Content-Disposition");
   info.location = conn.getHeaderField("Content-Location");
   info.mimeType = DLUtil.normalizeMimeType(conn.getContentType());
   final String transferEncoding = conn.getHeaderField("Transfer-Encoding");
   if (TextUtils.isEmpty(transferEncoding)) {
     try {
       info.totalBytes = Integer.parseInt(conn.getHeaderField("Content-Length"));
       LogUtils.e("安装包的大小:" + info.totalBytes);
     } catch (NumberFormatException e) {
       info.totalBytes = -1;
     }
   } else {
     info.totalBytes = -1;
   }
   if (info.totalBytes == -1
       && (TextUtils.isEmpty(transferEncoding) || !transferEncoding.equalsIgnoreCase("chunked")))
     throw new RuntimeException("Can not obtain size of download file.");
   if (TextUtils.isEmpty(info.fileName))
     info.fileName = DLUtil.obtainFileName(info.realUrl, info.disposition, info.location);
 }