Exemplo n.º 1
0
  @Override
  protected AsyncTaskResult<NovelCollectionModel> doInBackground(PageModel... params) {
    Context ctx = owner.getContext();
    PageModel page = params[0];
    try {
      publishProgress(
          new CallbackEventData(
              ctx.getResources().getString(R.string.add_novel_task_check, page.getPage())));
      page = NovelsDao.getInstance().getUpdateInfo(page, this);
      if (page.isMissing()) {
        return new AsyncTaskResult<NovelCollectionModel>(
            new Exception(
                ctx.getResources().getString(R.string.add_novel_task_missing, page.getPage())));
      }

      NovelCollectionModel novelCol =
          NovelsDao.getInstance().getNovelDetailsFromInternet(page, this);
      Log.d("AddNovelTask", "Downloaded: " + novelCol.getPage());
      return new AsyncTaskResult<NovelCollectionModel>(novelCol);
    } catch (Exception e) {
      Log.e("AddNovelTask", e.getClass().toString() + ": " + e.getMessage(), e);
      publishProgress(
          new CallbackEventData(
              ctx.getResources()
                  .getString(R.string.add_novel_task_error, page.getPage(), e.getMessage())));
      return new AsyncTaskResult<NovelCollectionModel>(e);
    }
  }
 public void onGetResult(AsyncTaskResult<?> result, Class<?> t) {
   Exception e = result.getError();
   if (e == null) {
     // from LoadNovelsTask
     if (t == PageModel[].class) {
       PageModel[] list = (PageModel[]) result.getResult();
       if (list != null) {
         adapter.clear();
         adapter.addAll(list);
         toggleProgressBar(false);
       }
     }
     // from DownloadNovelDetailsTask
     else if (t == NovelCollectionModel[].class) {
       setMessageDialog(new CallbackEventData("Download complete."));
       NovelCollectionModel[] list = (NovelCollectionModel[]) result.getResult();
       for (NovelCollectionModel novelCol : list) {
         try {
           PageModel page = novelCol.getPageModel();
           boolean found = false;
           for (PageModel temp : adapter.data) {
             if (temp.getPage().equalsIgnoreCase(page.getPage())) {
               found = true;
               break;
             }
           }
           if (!found) {
             adapter.data.add(page);
           }
         } catch (Exception e1) {
           Log.e(TAG, e1.getClass().toString() + ": " + e1.getMessage(), e1);
         }
       }
       adapter.notifyDataSetChanged();
       toggleProgressBar(false);
     }
   } else {
     Log.e(TAG, e.getClass().toString() + ": " + e.getMessage(), e);
     Toast.makeText(this, e.getClass().toString() + ": " + e.getMessage(), Toast.LENGTH_LONG)
         .show();
   }
 }
  public void onGetResult(AsyncTaskResult<?> result, Class<?> t) {
    if (!isAdded()) return;

    Exception e = result.getError();
    if (e == null) {
      // from LoadNovelsTask
      if (t == PageModel[].class) {
        PageModel[] list = (PageModel[]) result.getResult();
        Log.d(TAG, "LoadNovelsTask result ok");
        if (list != null && list.length > 0) {
          adapter.clear();
          adapter.addAll(list);
          toggleProgressBar(false);

          // Show message if watch list is empty
          if (list.length == 0 && onlyWatched) {
            Log.d(TAG, "WatchList result set message empty");
            loadingText.setVisibility(TextView.VISIBLE);
            loadingText.setText("Watch List is empty.");
          }
        } else {
          toggleProgressBar(false);
          loadingText.setVisibility(TextView.VISIBLE);
          loadingText.setText("List is empty.");
          Log.w(TAG, "Empty ArrayList!");
        }
      }
      // from DownloadNovelDetailsTask
      else if (t == NovelCollectionModel[].class) {
        setMessageDialog(new CallbackEventData("Download complete."));
        NovelCollectionModel[] list = (NovelCollectionModel[]) result.getResult();
        for (NovelCollectionModel novelCol : list) {
          try {
            PageModel page = novelCol.getPageModel();
            boolean found = false;
            for (PageModel temp : adapter.data) {
              if (temp.getPage().equalsIgnoreCase(page.getPage())) {
                found = true;
                break;
              }
            }
            if (!found) {
              adapter.data.add(page);
            }
          } catch (Exception e1) {
            Log.e(TAG, e1.getClass().toString() + ": " + e1.getMessage(), e1);
          }
        }
        adapter.notifyDataSetChanged();
        toggleProgressBar(false);
      } else {
        Log.e(TAG, "Unknown ResultType: " + t.getName());
      }
    } else {
      Log.e(TAG, e.getClass().toString() + ": " + e.getMessage(), e);
      Toast.makeText(
              getSherlockActivity(),
              e.getClass().toString() + ": " + e.getMessage(),
              Toast.LENGTH_LONG)
          .show();
    }
  }