Created
September 18, 2017 11:10
-
-
Save demixdn/de79e343cea7b80c258a3037372e1c97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.v4.view.GravityCompat; | |
import android.support.v4.widget.DrawerLayout; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.SparseArray; | |
import android.view.View; | |
import android.view.Window; | |
import android.view.WindowManager; | |
import android.widget.AdapterView; | |
import android.widget.LinearLayout; | |
import android.widget.ListView; | |
import android.widget.RelativeLayout; | |
import android.widget.Toast; | |
import com.github.barteksc.pdfviewer.PDFView; | |
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener; | |
import com.google.android.gms.analytics.HitBuilders; | |
import com.google.android.gms.analytics.Tracker; | |
import com.google.gson.Gson; | |
import com.google.gson.reflect.TypeToken; | |
import com.korpusprava.journals.App; | |
import com.korpusprava.journals.R; | |
import com.korpusprava.journals.data.JournalManager; | |
import com.korpusprava.journals.data.local.BookmarkStorage; | |
import com.korpusprava.journals.data.models.BookmarkData; | |
import com.korpusprava.journals.data.models.Chapter; | |
import com.korpusprava.journals.data.models.Journal; | |
import com.korpusprava.journals.ui.bookmarks.BookMarkActivity; | |
import java.io.File; | |
import java.lang.reflect.Type; | |
import java.util.ArrayDeque; | |
import java.util.Deque; | |
import java.util.List; | |
import butterknife.BindView; | |
import butterknife.ButterKnife; | |
/** | |
* Created by Aleksandr on 08.11.2015 in Journals | |
*/ | |
public class PdfViewerActivity extends AppCompatActivity implements View.OnClickListener, ToolbarPanel.ToolbarInteractionListener, OnPageChangeListener { | |
public static final String PATH = "path"; | |
private static final String EXTRA_JOURNAL = "extra_journal"; | |
private static final Type journalType = new TypeToken<Journal>() { | |
}.getType(); | |
@BindView(R.id.rootView) | |
RelativeLayout rootView; | |
@BindView(R.id.rlTopView) | |
RelativeLayout topToolbar; | |
@BindView(R.id.llBottomView) | |
LinearLayout bottomToolbar; | |
@BindView(R.id.lvDrawerList) | |
ListView lvChapters; | |
@BindView(R.id.journalChapters) | |
DrawerLayout drawerChapters; | |
@BindView(R.id.pdfView) | |
PDFView pdfView; | |
@BindView(R.id.chapter_view) | |
RelativeLayout chapterView; | |
private BookmarkStorage bookmarkStorage; | |
private SparseArray<BookmarkData> mapBookmark; | |
private Journal journal; | |
private Deque<Integer> queue; | |
private boolean isStackAction; | |
private Tracker mTracker; | |
private ToolbarPanelContract toolbarPanel; | |
public static void start(@NonNull Context context, @NonNull Journal journal) { | |
Intent starter = new Intent(context, PdfViewerActivity.class); | |
starter.putExtra(EXTRA_JOURNAL, new Gson().toJson(journal, journalType)); | |
context.startActivity(starter); | |
} | |
public void setBookmarkStorage(BookmarkStorage bookmarkStorage) { | |
this.bookmarkStorage = bookmarkStorage; | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); | |
super.onCreate(savedInstanceState); | |
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); | |
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); | |
setContentView(R.layout.activity_pdf_viewer); | |
if (getSupportActionBar() != null) { | |
getSupportActionBar().hide(); | |
} | |
ButterKnife.bind(this); | |
App.getInstance().getComponent().inject(this); | |
initData(); | |
initUI(); | |
initPdf(); | |
sendTrack(); | |
} | |
private void sendTrack() { | |
mTracker.setScreenName("Image~Журнал"); | |
mTracker.send(new HitBuilders.ScreenViewBuilder().build()); | |
mTracker.send(new HitBuilders.EventBuilder() | |
.setCategory("Open") | |
.setAction("Journal") | |
.setLabel(journal == null ? "default" : journal.getTitle()) | |
.build()); | |
} | |
private void initData() { | |
mapBookmark = new SparseArray<>(); | |
queue = new ArrayDeque<>(); | |
journal = new Gson().fromJson(getIntent().getStringExtra(EXTRA_JOURNAL), journalType); | |
mTracker = App.getInstance().getDefaultTracker(); | |
String localPath = journal.getJournalPdf().getLocalPath(JournalManager.getInstance().isEnLang); | |
List<BookmarkData> bookmarks = bookmarkStorage.getAllFormPath(localPath); | |
if (bookmarks != null && !bookmarks.isEmpty()) { | |
for (BookmarkData bd : bookmarks) { | |
mapBookmark.put(bd.getPage(), bd); | |
} | |
} | |
} | |
private void initUI() { | |
toolbarPanel = new ToolbarPanel(topToolbar, bottomToolbar, this); | |
ChapterAdapter adapter = new ChapterAdapter(this, JournalManager.getInstance().getCurrentJournal().getChapters()); | |
lvChapters.setAdapter(adapter); | |
lvChapters.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
drawerChapters.closeDrawer(chapterView); | |
Chapter chapter = JournalManager.getInstance().getCurrentJournal().getChapters().get(position); | |
jumpToWithoutAnim(chapter.getPage()); | |
} | |
}); | |
} | |
private void initPdf() { | |
String localPath = journal.getJournalPdf().getLocalPath(JournalManager.getInstance().isEnLang); | |
pdfView.fromFile(new File(localPath)) | |
.enableSwipe(true) | |
.swipeVertical(false) | |
.defaultPage(1) | |
.onPageChange(this) | |
.load(); | |
pdfView.setClickable(true); | |
pdfView.setOnClickListener(this); | |
} | |
@Override | |
protected void onDestroy() { | |
pdfView.recycle(); | |
super.onDestroy(); | |
} | |
@Override | |
public void onClick(View v) { | |
toolbarPanel.toggle(); | |
} | |
@Override | |
public void onBooksClick() { | |
finish(); | |
} | |
@Override | |
public void onChaptersClick() { | |
drawerChapters.openDrawer(GravityCompat.START, true); | |
} | |
@Override | |
public void onBackPageClick() { | |
isStackAction = true; | |
if (!queue.isEmpty()) { | |
jumpToWithoutAnim(queue.pop()); | |
} | |
} | |
@Override | |
public void onThumbnailsClick() { | |
JournalManager.getInstance().setCurrentBookmarks(mapBookmark); | |
int currentPage = pdfView.getCurrentPage(); | |
String localPath = journal.getJournalPdf().getLocalPath(JournalManager.getInstance().isEnLang); | |
boolean b = App.getInstance().createThumbnailCreator(localPath); | |
if (b) { | |
Intent thumbIntent = new Intent(PdfViewerActivity.this, BookMarkActivity.class); | |
thumbIntent.putExtra("position", currentPage); | |
startActivityForResult(thumbIntent, 1010); | |
} | |
} | |
@Override | |
public void onAddBookmarkClick() { | |
bookmarkToggle(); | |
} | |
private void bookmarkToggle() { | |
int currentPage = pdfView.getCurrentPage() + 1; | |
String localPath = journal.getJournalPdf().getLocalPath(JournalManager.getInstance().isEnLang); | |
if (mapBookmark.get(currentPage) == null) { | |
String currentTime = String.valueOf(System.currentTimeMillis()); | |
BookmarkData bookmark = new BookmarkData(); | |
bookmark.setFilePath(localPath); | |
bookmark.setBookName(journal.getTitle()); | |
bookmark.setPage(currentPage); | |
bookmark.setMarkName("-"); | |
bookmark.setAddTime(currentTime); | |
mapBookmark.put(currentPage, bookmark); | |
bookmarkStorage.addBookmark(bookmark); | |
toolbarPanel.showPage(currentPage, pdfView.getPageCount(), bookmark); | |
Toast.makeText(this, "Page " + currentPage + " bookmark added", Toast.LENGTH_LONG).show(); | |
} else { | |
bookmarkStorage.deleteBookmarkFromPath(localPath, currentPage); | |
mapBookmark.remove(currentPage); | |
toolbarPanel.showPage(currentPage, pdfView.getPageCount(), null); | |
} | |
} | |
@Override | |
public void moveTo(int progress) { | |
jumpToWithoutAnim(progress); | |
} | |
@Override | |
public void onPageChanged(int page, int pageCount) { | |
if (queue.isEmpty() || (!isStackAction && queue.peek() != page)) { | |
queue.push(page); | |
} | |
isStackAction = false; | |
toolbarPanel.showPage(page, pageCount, mapBookmark.get(page)); | |
} | |
private void jumpToWithoutAnim(int position) { | |
pdfView.setShowPageWithAnimation(false); | |
pdfView.jumpTo(position); | |
pdfView.setShowPageWithAnimation(true); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
switch (requestCode) { | |
case 1010: | |
if (resultCode == RESULT_OK) { | |
int page = data.getIntExtra("clickpos", 1); | |
jumpToWithoutAnim(page); | |
} | |
break; | |
} | |
super.onActivityResult(requestCode, resultCode, data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment