ReaderActivity.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package eu.kanade.mangafeed.ui.reader;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.os.Build;
  5. import android.os.Bundle;
  6. import android.support.v7.widget.Toolbar;
  7. import android.view.MotionEvent;
  8. import android.view.View;
  9. import android.view.WindowManager;
  10. import android.widget.FrameLayout;
  11. import android.widget.TextView;
  12. import java.util.List;
  13. import javax.inject.Inject;
  14. import butterknife.Bind;
  15. import butterknife.ButterKnife;
  16. import eu.kanade.mangafeed.App;
  17. import eu.kanade.mangafeed.R;
  18. import eu.kanade.mangafeed.data.database.models.Chapter;
  19. import eu.kanade.mangafeed.data.database.models.Manga;
  20. import eu.kanade.mangafeed.data.preference.PreferencesHelper;
  21. import eu.kanade.mangafeed.data.source.model.Page;
  22. import eu.kanade.mangafeed.ui.base.activity.BaseRxActivity;
  23. import eu.kanade.mangafeed.ui.reader.viewer.base.BaseReader;
  24. import eu.kanade.mangafeed.ui.reader.viewer.horizontal.LeftToRightReader;
  25. import eu.kanade.mangafeed.ui.reader.viewer.horizontal.RightToLeftReader;
  26. import eu.kanade.mangafeed.ui.reader.viewer.vertical.VerticalReader;
  27. import eu.kanade.mangafeed.ui.reader.viewer.webtoon.WebtoonReader;
  28. import eu.kanade.mangafeed.util.ToastUtil;
  29. import nucleus.factory.RequiresPresenter;
  30. @RequiresPresenter(ReaderPresenter.class)
  31. public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
  32. @Bind(R.id.page_number) TextView pageNumber;
  33. @Bind(R.id.reader) FrameLayout container;
  34. @Bind(R.id.toolbar) Toolbar toolbar;
  35. @Inject PreferencesHelper prefs;
  36. private BaseReader viewer;
  37. private ReaderMenu readerMenu;
  38. private int uiFlags;
  39. private static final int LEFT_TO_RIGHT = 1;
  40. private static final int RIGHT_TO_LEFT = 2;
  41. private static final int VERTICAL = 3;
  42. private static final int WEBTOON = 4;
  43. public static Intent newInstance(Context context) {
  44. return new Intent(context, ReaderActivity.class);
  45. }
  46. @Override
  47. public void onCreate(Bundle savedState) {
  48. super.onCreate(savedState);
  49. App.get(this).getComponent().inject(this);
  50. setContentView(R.layout.activity_reader);
  51. ButterKnife.bind(this);
  52. setupToolbar(toolbar);
  53. readerMenu = new ReaderMenu(this, prefs);
  54. createUiHideFlags();
  55. enableHardwareAcceleration();
  56. viewer = getViewer();
  57. }
  58. @Override
  59. protected void onResume() {
  60. super.onResume();
  61. hideSystemUI();
  62. }
  63. @Override
  64. protected void onPause() {
  65. getPresenter().setCurrentPage(viewer.getCurrentPosition());
  66. viewer.destroySubscriptions();
  67. super.onPause();
  68. }
  69. private void createUiHideFlags() {
  70. uiFlags |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
  71. if (prefs.isHideStatusBarSet())
  72. uiFlags |= View.SYSTEM_UI_FLAG_FULLSCREEN;
  73. if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
  74. uiFlags |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
  75. }
  76. public void onChapterReady(List<Page> pages, Manga manga, Chapter chapter) {
  77. viewer.onPageListReady(pages);
  78. viewer.updatePageNumber();
  79. readerMenu.onChapterReady(pages.size(), manga, chapter);
  80. }
  81. public void onChapterError() {
  82. finish();
  83. ToastUtil.showShort(this, R.string.page_list_error);
  84. }
  85. public void onPageChanged(int currentPageIndex, int totalPages) {
  86. String page = (currentPageIndex + 1) + "/" + totalPages;
  87. pageNumber.setText(page);
  88. readerMenu.onPageChanged(currentPageIndex);
  89. }
  90. public void setSelectedPage(int pageIndex) {
  91. viewer.setSelectedPage(pageIndex);
  92. }
  93. public void hideSystemUI() {
  94. getWindow().getDecorView().setSystemUiVisibility(uiFlags);
  95. }
  96. public void enableHardwareAcceleration() {
  97. getWindow().setFlags(
  98. WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
  99. WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
  100. }
  101. public boolean onImageSingleTap(MotionEvent motionEvent) {
  102. return viewer.onImageTouch(motionEvent);
  103. }
  104. public void onCenterSingleTap() {
  105. readerMenu.toggle();
  106. }
  107. private BaseReader getViewer() {
  108. switch (prefs.getDefaultViewer()) {
  109. case LEFT_TO_RIGHT: default:
  110. return new LeftToRightReader(this, container);
  111. case RIGHT_TO_LEFT:
  112. return new RightToLeftReader(this, container);
  113. case VERTICAL:
  114. return new VerticalReader(this, container);
  115. case WEBTOON:
  116. return new WebtoonReader(this, container);
  117. }
  118. }
  119. }