public void downloadApkFile(
     String url,
     String fileName,
     String pkg,
     int versionCode,
     String savePath,
     boolean autoResume,
     final RequestCallBack<File> callback)
     throws DbException {
   if (checkDownloaded(pkg, versionCode, savePath)) {
     PackageUtils.installApp(mContext, savePath);
     return;
   }
   String tmpApkPath = savePath + getMD5ApkTmpName(pkg, versionCode);
   String apkPath = savePath + getMD5ApkName(pkg, versionCode);
   final DownloadInfo downloadInfo = new DownloadInfo();
   downloadInfo.setDownloadUrl(url);
   downloadInfo.setAutoResume(autoResume);
   downloadInfo.setFileName(fileName);
   downloadInfo.setFileTmpPath(tmpApkPath);
   downloadInfo.setFileSavePath(apkPath);
   downloadInfo.setPkg(pkg);
   downloadInfo.setVersionCode(versionCode);
   downloadInfo.setAutoRenameTmp(true);
   HttpUtils http = new HttpUtils();
   http.configRequestThreadPoolSize(maxDownloadThread);
   HttpHandler<File> handler =
       http.download(url, tmpApkPath, autoResume, new ManagerCallBack(downloadInfo, callback));
   downloadInfo.setHandler(handler);
   downloadInfo.setState(handler.getState());
   downloadInfoList.add(downloadInfo);
   db.saveBindingId(downloadInfo);
 }
 @Override
 public void onSuccess(ResponseInfo<File> responseInfo) {
   if (downloadInfo.getFileLength() != downloadInfo.getProgress()) { // 不一致 返回
     return;
   }
   if (downloadInfo.getAutoRenameTmp()) { // 成功后改名
     File tarFile = new File(downloadInfo.getFileTmpPath());
     if (tarFile.exists()) {
       File newFile = new File(downloadInfo.getFileSavePath());
       tarFile.renameTo(newFile);
     }
   }
   LogUtils.d(downloadInfo.getPkg() + " onSuccess");
   HttpHandler<File> handler = downloadInfo.getHandler();
   if (handler != null) {
     downloadInfo.setState(handler.getState());
   }
   try {
     db.saveOrUpdate(downloadInfo);
   } catch (DbException e) {
     LogUtils.e(e.getMessage(), e);
   }
   if (baseCallBack != null) {
     baseCallBack.onSuccess(responseInfo);
   }
 }
 public void removeDownload(DownloadInfo downloadInfo) throws DbException {
   HttpHandler<File> handler = downloadInfo.getHandler();
   if (handler != null && !handler.isCancelled()) {
     handler.cancel();
   }
   downloadInfoList.remove(downloadInfo);
   db.delete(downloadInfo);
 }
 public void backupDownloadInfoList() throws DbException {
   for (DownloadInfo downloadInfo : downloadInfoList) {
     HttpHandler<File> handler = downloadInfo.getHandler();
     if (handler != null) {
       downloadInfo.setState(handler.getState());
     }
   }
   db.saveOrUpdateAll(downloadInfoList);
 }
 public void stopDownload(DownloadInfo downloadInfo) throws DbException {
   HttpHandler<File> handler = downloadInfo.getHandler();
   if (handler != null && !handler.isCancelled()) {
     handler.cancel();
   } else {
     downloadInfo.setState(HttpHandler.State.CANCELLED);
   }
   db.saveOrUpdate(downloadInfo);
 }
 public void resumeDownload(DownloadInfo downloadInfo, final RequestCallBack<File> callback)
     throws DbException {
   HttpUtils http = new HttpUtils();
   http.configRequestThreadPoolSize(maxDownloadThread);
   HttpHandler<File> handler =
       http.download(
           downloadInfo.getDownloadUrl(),
           downloadInfo.getFileSavePath(),
           downloadInfo.isAutoResume(),
           new ManagerCallBack(downloadInfo, callback));
   downloadInfo.setHandler(handler);
   downloadInfo.setState(handler.getState());
   db.saveOrUpdate(downloadInfo);
 }
 @Override
 public void onCancelled() {
   HttpHandler<File> handler = downloadInfo.getHandler();
   if (handler != null) {
     downloadInfo.setState(handler.getState());
   }
   try {
     db.saveOrUpdate(downloadInfo);
   } catch (DbException e) {
     LogUtils.e(e.getMessage(), e);
   }
   if (baseCallBack != null) {
     baseCallBack.onCancelled();
   }
 }
 @Override
 public void onFailure(HttpException error, String msg) {
   LogUtils.d(downloadInfo.getPkg() + " onFailure");
   HttpHandler<File> handler = downloadInfo.getHandler();
   if (handler != null) {
     downloadInfo.setState(handler.getState());
   }
   try {
     db.saveOrUpdate(downloadInfo);
   } catch (DbException e) {
     LogUtils.e(e.getMessage(), e);
   }
   if (baseCallBack != null) {
     baseCallBack.onFailure(error, msg);
   }
 }
 @Override
 public void onLoading(long total, long current, boolean isUploading) {
   HttpHandler<File> handler = downloadInfo.getHandler();
   if (handler != null) {
     downloadInfo.setState(handler.getState());
   }
   downloadInfo.setFileLength(total);
   downloadInfo.setProgress(current);
   try {
     db.saveOrUpdate(downloadInfo);
   } catch (DbException e) {
     LogUtils.e(e.getMessage(), e);
   }
   if (baseCallBack != null) {
     baseCallBack.onLoading(total, current, isUploading);
   }
 }
예제 #10
0
 public void addNewDownload(
     String url,
     String fileName,
     String target,
     boolean autoResume,
     boolean autoRename,
     final RequestCallBack<File> callback)
     throws DbException {
   final DownloadInfo downloadInfo = new DownloadInfo();
   downloadInfo.setDownloadUrl(url);
   downloadInfo.setAutoResume(autoResume);
   downloadInfo.setFileName(fileName);
   downloadInfo.setFileSavePath(target);
   downloadInfo.setAutoRenameTmp(autoRename);
   HttpUtils http = new HttpUtils();
   http.configRequestThreadPoolSize(maxDownloadThread);
   HttpHandler<File> handler =
       http.download(
           url, target, autoResume, autoRename, new ManagerCallBack(downloadInfo, callback));
   downloadInfo.setHandler(handler);
   downloadInfo.setState(handler.getState());
   downloadInfoList.add(downloadInfo);
   db.saveBindingId(downloadInfo);
 }