public void startDownloadFromJson(GetApkInfoJson json, long id, Download download) {
    ArrayList<DownloadModel> filesToDownload = new ArrayList<DownloadModel>();

    if (json.getObb() != null) {
      DownloadModel mainObbDownload =
          new DownloadModel(
              json.getObb().getMain().getPath(),
              OBB_DESTINATION
                  + download.getPackageName()
                  + "/"
                  + json.getObb().getMain().getFilename(),
              json.getObb().getMain().getMd5sum(),
              json.getObb().getMain().getFilesize().longValue());
      filesToDownload.add(mainObbDownload);
      if (json.getObb().getPatch() != null) {
        DownloadModel patchObbDownload =
            new DownloadModel(
                json.getObb().getPatch().getPath(),
                OBB_DESTINATION
                    + download.getPackageName()
                    + "/"
                    + json.getObb().getPatch().getFilename(),
                json.getObb().getPatch().getMd5sum(),
                json.getObb().getPatch().getFilesize().longValue());
        filesToDownload.add(patchObbDownload);
      }
    }

    String path = Aptoide.getConfiguration().getPathCacheApks();

    if (json.getApk().getMd5sum() != null) {
      download.setId(json.getApk().getMd5sum().hashCode());
    }

    DownloadModel downloadModel =
        new DownloadModel(
            json.getApk().getPath(),
            path + json.getApk().getMd5sum() + ".apk",
            json.getApk().getMd5sum(),
            json.getApk().getSize().longValue());
    downloadModel.setAutoExecute(true);
    downloadModel.setFallbackUrl(json.getApk().getAltPath());
    filesToDownload.add(downloadModel);

    FinishedApk apk =
        new FinishedApk(
            download.getName(),
            download.getPackageName(),
            download.getVersion(),
            id,
            download.getIcon(),
            path + json.getApk().getMd5sum() + ".apk",
            new ArrayList<String>(json.getApk().getPermissions()));
    apk.setId(json.getApk().getId().longValue());

    download(download.getId(), download, apk, filesToDownload);
  }
  public void startDownloadFromV6(Download download, UpdatesResponse.UpdateApk apk) {
    ArrayList<DownloadModel> filesToDownload = new ArrayList<>();

    String path = Aptoide.getConfiguration().getPathCacheApks();

    if (apk.md5sum != null) {
      download.setId(apk.md5sum.hashCode());
    }

    DownloadModel downloadModel =
        new DownloadModel(
            apk.apk.path, path + apk.md5sum + ".apk", apk.md5sum, apk.apk.filesize.longValue());
    downloadModel.setAutoExecute(true);
    downloadModel.setFallbackUrl(apk.apk.path_alt);
    filesToDownload.add(downloadModel);

    FinishedApk fapk =
        new FinishedApk(
            download.getName(),
            download.getPackageName(),
            download.getVersion(),
            apk.md5sum.hashCode(),
            download.getIcon(),
            path + apk.md5sum + ".apk",
            new ArrayList<String>());
    fapk.setId(apk.md5sum.hashCode());

    download(download.getId(), download, fapk, filesToDownload);
  }
  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();
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // Aptoide.getThemePicker().setAptoideTheme(this);
    super.onCreate(savedInstanceState);
    this.context = this;
    apk = getIntent().getParcelableExtra("apk");

    ArrayList<String> permissionsList = getIntent().getStringArrayListExtra("permissions");

    try {

      List<String> list =
          Arrays.asList(
              getPackageManager()
                  .getPackageInfo(apk.getApkid(), PackageManager.GET_PERMISSIONS)
                  .requestedPermissions);
      ArrayList<String> installedPermissionsList = new ArrayList<>(list);

      //            Log.d("TAG", "Installed permissions: " + installedPermissionsList.size() + " " +
      // installedPermissionsList);
      //            Log.d("TAG", "apk permissions: " + permissionsList.size() + " " +
      // permissionsList);

      permissionsList.removeAll(installedPermissionsList);

      //            Log.d("TAG", "apk permissions 2: " + permissionsList.size() + " " +
      // permissionsList);

      ArrayList<ApkPermission> descriptionList = permissions(context, permissionsList);
      if (!permissionsList.isEmpty() && !descriptionList.isEmpty()) {
        permissionsDialog(apk, descriptionList);
      } else {
        DownloadUtils.installWithRoot(apk);
        finish();
      }

    } catch (PackageManager.NameNotFoundException e) {

      try {
        permissionsDialog(apk, permissions(context, permissionsList));
      } catch (NullPointerException ignore) {
        DownloadUtils.installWithRoot(apk);
        finish();
      }

    } catch (NullPointerException e) {
      DownloadUtils.installWithRoot(apk);
      finish();
    }
  }
  public void startDownloadFromUrl(
      String remotePath, String md5, long id, Download download, String repoName) {
    ArrayList<DownloadModel> filesToDownload = new ArrayList<DownloadModel>();

    String path = Aptoide.getConfiguration().getPathCacheApks();

    DownloadModel downloadModel = new DownloadModel(remotePath, path + md5 + ".apk", md5, 0);
    downloadModel.setAutoExecute(true);
    filesToDownload.add(downloadModel);

    FinishedApk apk =
        new FinishedApk(
            download.getName(),
            download.getPackageName(),
            download.getVersion(),
            id,
            download.getIcon(),
            path + md5 + ".apk",
            new ArrayList<String>());
    apk.setRepoName(repoName);

    download(id, download, apk, filesToDownload);
  }
  private void permissionsDialog(final FinishedApk viewApk, ArrayList<ApkPermission> permissions) {

    View v = LayoutInflater.from(context).inflate(R.layout.app_permission, null);
    LinearLayout layout = (LinearLayout) v.findViewById(R.id.container);

    if (permissions != null) {

      if (!permissions.isEmpty()) {
        for (int i = 0; i < permissions.size(); i++) {
          ApkPermission permission = permissions.get(i);

          View permissionView = LayoutInflater.from(context).inflate(R.layout.row_permission, null);
          if (i == 0 || !permissions.get(i - 1).getName().equals(permission.getName())) {
            ((TextView) permissionView.findViewById(R.id.permission_name))
                .setText(permission.getName());
          } else {
            permissionView.findViewById(R.id.permission_name).setVisibility(View.GONE);
          }
          ((TextView) permissionView.findViewById(R.id.permission_description))
              .setText(permission.getDescription());
          layout.addView(permissionView);
        }
      } else {
        TextView tv = new TextView(context);
        tv.setPadding(10, 10, 10, 10);
        tv.setText(context.getString(R.string.no_permissions_required));
        layout.addView(tv);
      }
    }

    final AlertDialog dialog = new AlertDialog.Builder(context).setView(v).create();
    dialog.setTitle(context.getString(R.string.restore) + " " + viewApk.getName() + "?");
    // dialog.setIcon(BitmapDrawable.createFromPath(ImageLoader.getInstance().getDiscCache().get(path).getAbsolutePath()));

    dialog.setButton(
        DialogInterface.BUTTON_POSITIVE,
        context.getString(android.R.string.yes),
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            executor.submit(
                new Runnable() {
                  @Override
                  public void run() {
                    DownloadUtils.installWithRoot(viewApk);
                  }
                });
            finish();
          }
        });

    dialog.setButton(
        DialogInterface.BUTTON_NEGATIVE,
        context.getString(android.R.string.no),
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
          }
        });
    dialog.setOnDismissListener(
        new DialogInterface.OnDismissListener() {
          @Override
          public void onDismiss(DialogInterface dialog) {
            finish();
          }
        });
    dialog.setCancelable(false);
    // dialog.show();

    String path = viewApk.getIconPath();
    GlideUtils.showDialogWithGlideLoadingIconFromCache(dialog, path, this);
  }