@SuppressLint("NewApi")
 private void executeDownloadTask(ArrayList<PageModel> novels) {
   downloadTask = new DownloadNovelDetailsTask(this);
   if (novels == null || novels.size() == 0) return;
   String key = DisplayLightNovelDetailsActivity.TAG + ":" + novels.get(0).getPage();
   if (novels.size() > 1) {
     key = DisplayLightNovelDetailsActivity.TAG + ":All_Novels";
   }
   boolean isAdded = LNReaderApplication.getInstance().addTask(key, downloadTask);
   if (isAdded) {
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
       downloadTask.executeOnExecutor(
           AsyncTask.THREAD_POOL_EXECUTOR, novels.toArray(new PageModel[novels.size()]));
     else downloadTask.execute(novels.toArray(new PageModel[novels.size()]));
   } else {
     Log.i(TAG, "Continue download task: " + key);
     DownloadNovelDetailsTask tempTask =
         (DownloadNovelDetailsTask) LNReaderApplication.getInstance().getTask(key);
     if (tempTask != null) {
       downloadTask = tempTask;
       downloadTask.owner = this;
     }
     toggleProgressBar(true);
   }
 }
  public boolean downloadListSetup(String id, String toastText, int type, boolean hasError) {
    boolean exists = false;
    if (!this.isAdded() || this.isDetached()) return exists;

    String name = touchedForDownload;
    if (type == 0) {
      if (LNReaderApplication.getInstance().checkIfDownloadExists(name)) {
        exists = true;
        Toast.makeText(getSherlockActivity(), "Download already on queue.", Toast.LENGTH_SHORT)
            .show();
      } else {
        Toast.makeText(getSherlockActivity(), "Downloading " + name + ".", Toast.LENGTH_SHORT)
            .show();
        LNReaderApplication.getInstance().addDownload(id, name);
      }
    } else if (type == 1) {
      Toast.makeText(getSherlockActivity(), toastText, Toast.LENGTH_SHORT).show();
    } else if (type == 2) {
      String message =
          String.format(
              "%s's download finished!",
              LNReaderApplication.getInstance().getDownloadDescription(id));
      if (hasError)
        message =
            String.format(
                "%s's download finished with error(s)!",
                LNReaderApplication.getInstance().getDownloadDescription(id));
      Toast.makeText(getSherlockActivity(), message, Toast.LENGTH_SHORT).show();
      LNReaderApplication.getInstance().removeDownload(id);
    }
    return exists;
  }
  public boolean downloadListSetup(String id, String toastText, int type, boolean hasError) {
    boolean exists = false;
    String name = touchedForDownload;
    if (type == 0) {
      if (LNReaderApplication.getInstance().checkIfDownloadExists(name)) {
        exists = true;
        Toast.makeText(
                this, getResources().getString(R.string.download_on_queue), Toast.LENGTH_SHORT)
            .show();
      } else {
        Toast.makeText(
                this,
                getResources().getString(R.string.downloading) + name + ".",
                Toast.LENGTH_SHORT)
            .show();
        LNReaderApplication.getInstance().addDownload(id, name);
      }
    } else if (type == 1) {
      Toast.makeText(this, toastText, Toast.LENGTH_SHORT).show();
    } else if (type == 2) {
      String message =
          String.format(
              "%s's download finished!",
              LNReaderApplication.getInstance().getDownloadDescription(id));
      if (hasError)
        message =
            String.format(
                "%s's download finished with error(s)!",
                LNReaderApplication.getInstance().getDownloadDescription(id));

      Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
      LNReaderApplication.getInstance().removeDownload(id);
    }
    return exists;
  }
 @SuppressLint("NewApi")
 private void executeAddTask(PageModel novel) {
   addTask = new AddNovelTask(this);
   String key = DisplayLightNovelDetailsActivity.TAG + ":Add:" + novel.getPage();
   boolean isAdded = LNReaderApplication.getInstance().addTask(key, addTask);
   if (isAdded) {
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
       addTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new PageModel[] {novel});
     else addTask.execute(new PageModel[] {novel});
   } else {
     Log.i(TAG, "Continue Add task: " + key);
     AddNovelTask tempTask = (AddNovelTask) LNReaderApplication.getInstance().getTask(key);
     if (tempTask != null) {
       addTask = tempTask;
       addTask.owner = this;
     }
     toggleProgressBar(true);
   }
 }
 @SuppressLint("NewApi")
 private void executeTask(boolean isRefresh, boolean onlyWatched, boolean alphOrder, String api) {
   task = new LoadNovelsTask(this, isRefresh, onlyWatched, alphOrder, api);
   String key = TAG + ":Main+Page";
   boolean isAdded = LNReaderApplication.getInstance().addTask(key, task);
   if (isAdded) {
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
       task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
     else task.execute();
   } else {
     Log.i(TAG, "Continue execute task: " + key);
     LoadNovelsTask tempTask = (LoadNovelsTask) LNReaderApplication.getInstance().getTask(key);
     if (tempTask != null) {
       task = tempTask;
       task.owner = this;
     }
     toggleProgressBar(true);
   }
 }
 public void updateProgress(String id, int current, int total, String messString) {
   double cur = current;
   double tot = total;
   double result = (cur / tot) * 100;
   LNReaderApplication.getInstance().updateDownload(id, (int) result, messString);
   if (loadingBar != null && loadingBar.getVisibility() == View.VISIBLE) {
     loadingBar.setIndeterminate(false);
     loadingBar.setMax(total);
     loadingBar.setProgress(current);
     loadingBar.setProgress(0);
     loadingBar.setProgress(current);
     loadingBar.setMax(total);
   }
 }
 public Context getContext() {
   Context ctx = this.getSherlockActivity();
   if (ctx == null) return LNReaderApplication.getInstance().getApplicationContext();
   return ctx;
 }