Ejemplo n.º 1
0
 private FavoritesManager getFavoritesManager() {
   if (getActivity() != null && getActivity() instanceof HomeActivity) {
     return ((HomeActivity) getActivity()).getFavoritesManager();
   } else {
     return FavoritesManager.getInstance(getApplicationContext());
   }
 }
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
   int id = item.getItemId();
   if (id == android.R.id.home) {
     this.onBackPressed();
     return true;
   }
   if (id == R.id.action_favorite) {
     FavoritesManager fm = FavoritesManager.getInstance(getApplicationContext());
     if (isFavorite) {
       fm.remove(fm.find(book));
     } else {
       fm.add(book);
     }
     fm.save();
     isFavorite = !isFavorite;
     Snackbar.make(
             $(R.id.main_content),
             isFavorite ? R.string.favorite_add_finished : R.string.favorite_remove_finished,
             Snackbar.LENGTH_LONG)
         .show();
     invalidateOptionsMenu();
     return true;
   }
   if (id == R.id.action_download) {
     new Thread() {
       @Override
       public void run() {
         onActionDownloadClick();
       }
     }.start();
     return true;
   }
   return super.onOptionsItemSelected(item);
 }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_book_details);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    collapsingToolbar = $(R.id.collapsing_toolbar);

    Intent intent = getIntent();
    if (Intent.ACTION_VIEW.equals(intent.getAction())) {
      book = new Book();
      String url = intent.getData().toString();
      if (url.endsWith("/")) url = url.substring(0, url.length() - 1);
      String bid = url.substring(url.lastIndexOf("/") + 1, url.length());
      Log.i("TAG", "url get id:" + bid);
      book.bookId = bid;
      fromPosition = -1;
      isFromExternal = true;
    } else {
      book = Book.toBookFromJson(intent.getStringExtra(EXTRA_BOOK_DATA));
      fromPosition = intent.getIntExtra(EXTRA_POSITION, 0);

      collapsingToolbar.setTitle(book.title);
    }

    mFileCacheManager = FileCacheManager.getInstance(getApplicationContext());

    isFavorite =
        originFavorite =
            FavoritesManager.getInstance(getApplicationContext()).contains(book.bookId);

    mImageView = $(R.id.app_bar_background);
    ViewCompat.setTransitionName(mImageView, TRANSITION_NAME_IMAGE);

    mFAB = $(R.id.fab);
    mTitleText = $(R.id.tv_title);
    mTagsLayout = $(R.id.book_tags_layout);
    mContentView = $(R.id.book_content);
    mProgressWheel = $(R.id.wheel_progress);
    mPreviewList = $(R.id.preview_list);

    mPreviewList.setHasFixedSize(true);
    mPreviewList.setLayoutManager(
        new GridLayoutManager(getApplicationContext(), 2, LinearLayoutManager.HORIZONTAL, false));

    FileCacheManager cm = FileCacheManager.getInstance(getApplicationContext());
    if (book.galleryId != null) {
      if (cm.cacheExistsUrl(Constants.CACHE_COVER, book.bigCoverImageUrl)) {
        if (cm.cacheExistsUrl(
            Constants.CACHE_PAGE_IMG, NHentaiUrl.getOriginPictureUrl(book.galleryId, "1"))) {
          Picasso.with(getApplicationContext())
              .load(
                  cm.getBitmapUrlFile(
                      Constants.CACHE_PAGE_IMG,
                      NHentaiUrl.getOriginPictureUrl(book.galleryId, "1")))
              .fit()
              .centerCrop()
              .into(mImageView);
        } else {
          Picasso.with(getApplicationContext())
              .load(cm.getBitmapUrlFile(Constants.CACHE_COVER, book.bigCoverImageUrl))
              .fit()
              .centerCrop()
              .into(mImageView);
        }
      } else {
        if (cm.cacheExistsUrl(Constants.CACHE_THUMB, book.previewImageUrl)) {
          Picasso.with(getApplicationContext())
              .load(cm.getBitmapUrlFile(Constants.CACHE_THUMB, book.previewImageUrl))
              .fit()
              .centerCrop()
              .into(mImageView);
        } else {
          int color = ColorGenerator.MATERIAL.getColor(book.title);
          TextDrawable drawable =
              TextDrawable.builder().buildRect(book.title.substring(0, 1), color);
          mImageView.setImageDrawable(drawable);
        }
        new CoverTask().execute(book);
      }
    }

    if (book.pageCount != 0) {
      mContentView.setVisibility(View.GONE);
      mProgressWheel.setVisibility(View.VISIBLE);
      mProgressWheel.spin();
      new Thread() {
        @Override
        public void run() {
          try {
            Thread.sleep(250);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          runOnUiThread(
              new Runnable() {
                @Override
                public void run() {
                  updateUIContent();
                }
              });
        }
      }.start();
    } else {
      startBookGet();
    }

    if (!isFromExternal) checkIsDownloaded();
  }