/**
   * Download the data for a specific intent note, that only one concurrent download() is possible
   * with a static synchronized method!
   */
  private static synchronized void download(Intent intent, DownloadService service) {
    // Set the app version if not set
    PackageInfo pi = Util.getPackageInfo(service);
    if (pi != null) {
      G.appVersion = pi.versionName; // Version
      G.appPackage = pi.packageName; // Package name
      G.appVersionCode = pi.versionCode; // Version code e.g.: 45
    }

    String action = intent.getStringExtra(Const.ACTION_EXTRA);

    // No action: leave service
    if (action == null) {
      return;
    }

    boolean successful = true;
    boolean force = intent.getBooleanExtra(Const.FORCE_DOWNLOAD, false);
    boolean launch = intent.getBooleanExtra(Const.APP_LAUNCHES, false);

    // Check if device has a internet connection

    boolean backgroundServicePermitted = Utils.isBackgroundServicePermitted(service);

    if (NetUtils.isConnected(service) && (launch || backgroundServicePermitted)) {
      Utils.logv("Handle action <" + action + ">");
      switch (action) {
        case Const.NEWS:
          successful = service.downloadNews(force);
          break;
        case Const.FACULTIES:
          successful = service.downloadFacultiesAndSurveyData();
          break;
        case Const.CAFETERIAS:
          successful = service.downloadCafeterias(force);
          break;
        case Const.KINO:
          successful = service.downLoadKino(force);
          break;
        case Const.DOWNLOAD_ALL_FROM_EXTERNAL:
        default:
          successful = service.downloadAll(force);

          boolean isSetup = Utils.getInternalSettingBool(service, Const.EVERYTHING_SETUP, false);
          if (isSetup) {
            break;
          }
          CacheManager cm = new CacheManager(service);
          cm.syncCalendar();
          if (successful) {
            Utils.setInternalSetting(service, Const.EVERYTHING_SETUP, true);
          }
          break;
      }
    }

    // Update the last run time saved in shared prefs
    if (action.equals(Const.DOWNLOAD_ALL_FROM_EXTERNAL)) {
      try {
        service.importLocationsDefaults();
      } catch (IOException e) {
        Utils.log(e);
        successful = false;
      }
      if (successful) {
        SharedPreferences prefs = service.getSharedPreferences(Const.INTERNAL_PREFS, 0);
        prefs.edit().putLong(LAST_UPDATE, System.currentTimeMillis()).apply();
      }
      CardManager.update(service);
      successful = true;
    }

    // After done the job, create an broadcast intent and send it. The receivers will be informed
    // that the download service has finished.
    Utils.logv("Downloadservice was " + (successful ? "" : "not ") + "successful");
    if (successful) {
      service.broadcastDownloadCompleted();
    } else {
      service.broadcastError(service.getResources().getString(R.string.exception_unknown));
    }

    // Do all other import stuff that is not relevant for creating the viewing the start page
    if (action.equals(Const.DOWNLOAD_ALL_FROM_EXTERNAL)) {
      service.startService(new Intent(service, FillCacheService.class));
    }
  }