public void resumeDownload(int downloadId) {
    startService(new Intent(getApplicationContext(), DownloadService.class));

    if (mBuilder == null) mBuilder = createDefaultNotification();
    startForeground(-3, mBuilder.build());

    // Log.d("donwload-trace", "setmBuilder: resumeDownload");
    DownloadInfoRunnable info = getDownload(downloadId);
    NotificationCompat.Builder builder = setNotification(downloadId);
    info.setmBuilder(builder);

    info.download();

    startIfStopped();
  }
  private void download(
      long id, Download download, FinishedApk apk, ArrayList<DownloadModel> filesToDownload) {

    DownloadInfoRunnable info = getDownload(id);

    if (download.getCpiUrl() != null) {
      apk.setCpiUrl(download.getCpiUrl());
    }

    if (download.getReferrer() != null) {
      apk.setReferrer(download.getReferrer());
    } else {
      Log.d("AptoideDownloadService", "Creating download with no referrer");
    }

    info.setDownloadExecutor(new DownloadExecutor(apk));
    info.setDownload(download);
    info.setFilesToDownload(filesToDownload);

    boolean update;
    try {
      Aptoide.getContext().getPackageManager().getPackageInfo(download.getPackageName(), 0);
      update = true;
    } catch (PackageManager.NameNotFoundException e) {
      update = false;
    }

    info.setUpdate(update);
    downloads.put(info.getId(), info);
    NotificationCompat.Builder builder = setNotification(info.getId());
    info.setmBuilder(builder);
    info.download();

    if (mBuilder == null) mBuilder = createDefaultNotification();

    startForeground(-3, mBuilder.build());
    startService(new Intent(getApplicationContext(), DownloadService.class));

    startIfStopped();
    Toast.makeText(
            getApplicationContext(),
            getApplicationContext().getString(R.string.starting_download),
            Toast.LENGTH_LONG)
        .show();
  }
  public void startExistingDownload(long id) {
    startService(new Intent(getApplicationContext(), DownloadService.class));

    final NotificationCompat.Builder builder = setNotification(id);

    if (mBuilder == null) mBuilder = createDefaultNotification();
    startForeground(-3, mBuilder.build());

    startIfStopped();

    // Log.d("Aptoide-DownloadManager", "Starting existing download " + id);
    for (final DownloadInfoRunnable info : manager.getmCompletedList()) {
      if (info.getId() == id) {
        final PackageManager packageManager = getPackageManager();
        new Thread(
                new Runnable() {
                  @Override
                  public void run() {
                    for (DownloadModel model : info.getmFilesToDownload()) {
                      try {
                        PackageInfo packageInfo =
                            packageManager.getPackageInfo(
                                info.getDownload().getPackageName(),
                                PackageManager.SIGNATURE_MATCH);
                        if (packageInfo.versionName.equals(info.getDownload().getVersion())) {
                          new Handler(Looper.getMainLooper())
                              .post(
                                  new Runnable() {
                                    @Override
                                    public void run() {
                                      Intent LaunchIntent =
                                          packageManager.getLaunchIntentForPackage(
                                              info.getDownload().getPackageName());
                                      if (LaunchIntent != null) startActivity(LaunchIntent);
                                    }
                                  });
                        } else {
                          throw new PackageManager.NameNotFoundException();
                        }

                      } catch (PackageManager.NameNotFoundException e) {

                        try {
                          String calculatedMd5 =
                              AptoideUtils.Algorithms.md5Calc(new File(model.getDestination()));
                          if (!calculatedMd5.equals(info.getDownload().getMd5())) {
                            // Log.d("download-trace", "Failed Md5 for " +
                            // info.getDownload().getName() + " : " + info.getDestination() + "
                            // calculated " + calculatedMd5 + " vs " + info.getDownload().getMd5());
                            info.setmBuilder(builder);

                            info.download();
                            break;
                          } else {
                            info.autoExecute();
                            // Log.d("download-trace", "Checked Md5 for " +
                            // info.getDownload().getName() + ", application download it's already
                            // completed!");
                            break;
                          }
                        } catch (Exception e1) {
                          e1.printStackTrace();
                        }
                      }
                    }
                  }
                })
            .start();
        return;
      }
    }

    for (DownloadInfoRunnable info : manager.getmErrorList()) {
      if (info.getId() == id) {
        info.setmBuilder(builder);
        info.download();
        return;
      }
    }
  }