use of android.support.v4.view.NonRestoringViewPager in project quran_android by quran.
the class PagerActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
QuranApplication quranApp = (QuranApplication) getApplication();
quranApp.refreshLocale(this, false);
super.onCreate(savedInstanceState);
// field injection
getPagerActivityComponent().inject(this);
bookmarksCache = new SparseBooleanArray();
boolean shouldAdjustPageNumber = false;
isDualPages = QuranUtils.isDualPages(this, quranScreenInfo);
// remove the window background to avoid overdraw. note that, per Romain's blog, this is
// acceptable (as long as we don't set the background color to null in the theme, since
// that is used to generate preview windows).
getWindow().setBackgroundDrawable(null);
numberOfPages = quranInfo.getNumberOfPages();
numberOfPagesDual = quranInfo.getNumberOfPagesDual();
int page = -1;
isActionBarHidden = true;
if (savedInstanceState != null) {
Timber.d("non-null saved instance state!");
DownloadAudioRequest lastAudioRequest = savedInstanceState.getParcelable(LAST_AUDIO_DL_REQUEST);
if (lastAudioRequest != null) {
Timber.d("restoring request from saved instance!");
lastAudioDownloadRequest = lastAudioRequest;
}
page = savedInstanceState.getInt(LAST_READ_PAGE, -1);
if (page != -1) {
page = numberOfPages - page;
}
showingTranslation = savedInstanceState.getBoolean(LAST_READING_MODE_IS_TRANSLATION, false);
if (savedInstanceState.containsKey(LAST_ACTIONBAR_STATE)) {
isActionBarHidden = !savedInstanceState.getBoolean(LAST_ACTIONBAR_STATE);
}
boolean lastWasDualPages = savedInstanceState.getBoolean(LAST_WAS_DUAL_PAGES, isDualPages);
shouldAdjustPageNumber = (lastWasDualPages != isDualPages);
start = savedInstanceState.getParcelable(LAST_START_POINT);
end = savedInstanceState.getParcelable(LAST_ENDING_POINT);
this.lastAudioRequest = savedInstanceState.getParcelable(LAST_AUDIO_REQUEST);
} else {
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
page = numberOfPages - extras.getInt("page", Constants.PAGES_FIRST);
showingTranslation = extras.getBoolean(EXTRA_JUMP_TO_TRANSLATION, showingTranslation);
highlightedSura = extras.getInt(EXTRA_HIGHLIGHT_SURA, -1);
highlightedAyah = extras.getInt(EXTRA_HIGHLIGHT_AYAH, -1);
}
}
compositeDisposable = new CompositeDisposable();
// subscribe to changes in bookmarks
compositeDisposable.add(bookmarkModel.bookmarksObservable().observeOn(AndroidSchedulers.mainThread()).subscribe(ignore -> {
onBookmarksChanged();
}));
final Resources resources = getResources();
isImmersiveInPortrait = resources.getBoolean(R.bool.immersive_in_portrait);
isLandscape = resources.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
ayahToolBarTotalHeight = resources.getDimensionPixelSize(R.dimen.toolbar_total_height);
setContentView(R.layout.quran_page_activity_slider);
audioStatusBar = (AudioStatusBar) findViewById(R.id.audio_area);
audioStatusBar.setIsDualPageMode(quranScreenInfo.isDualPageMode());
audioStatusBar.setQariList(audioUtils.getQariList(this));
audioStatusBar.setAudioBarListener(this);
audioBarParams = (ViewGroup.MarginLayoutParams) audioStatusBar.getLayoutParams();
toolBarArea = findViewById(R.id.toolbar_area);
translationsSpinner = (QuranSpinner) findViewById(R.id.spinner);
// this is the colored view behind the status bar on kitkat and above
final View statusBarBackground = findViewById(R.id.status_bg);
statusBarBackground.getLayoutParams().height = getStatusBarHeight();
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (quranSettings.isArabicNames() || QuranUtils.isRtl()) {
// remove when we remove LTR from quran_page_activity's root
ViewCompat.setLayoutDirection(toolbar, ViewCompat.LAYOUT_DIRECTION_RTL);
}
setSupportActionBar(toolbar);
final ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setDisplayShowHomeEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);
}
initAyahActionPanel();
if (showingTranslation && translationItems != null) {
updateActionBarSpinner();
} else {
updateActionBarTitle(numberOfPages - page);
}
lastPopupTime = System.currentTimeMillis();
pagerAdapter = new QuranPageAdapter(getSupportFragmentManager(), isDualPages, showingTranslation, quranInfo);
ayahToolBar = (AyahToolBar) findViewById(R.id.ayah_toolbar);
final NonRestoringViewPager nonRestoringViewPager = findViewById(R.id.quran_pager);
nonRestoringViewPager.setIsDualPagesInLandscape(QuranUtils.isDualPagesInLandscape(this, quranScreenInfo));
viewPager = nonRestoringViewPager;
viewPager.setAdapter(pagerAdapter);
ayahToolBar.setOnItemSelectedListener(new AyahMenuItemSelectionHandler());
viewPager.addOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (ayahToolBar.isShowing() && ayahToolBarPos != null) {
final int startPage = quranInfo.getPageFromSuraAyah(start.sura, start.ayah);
int barPos = quranInfo.getPosFromPage(startPage, isDualPages);
if (position == barPos) {
// Swiping to next ViewPager page (i.e. prev quran page)
ayahToolBarPos.xScroll = 0 - positionOffsetPixels;
} else if (position == barPos - 1) {
// Swiping to prev ViewPager page (i.e. next quran page)
ayahToolBarPos.xScroll = viewPager.getWidth() - positionOffsetPixels;
} else {
// Totally off screen, should hide toolbar
ayahToolBar.setVisibility(View.GONE);
return;
}
ayahToolBar.updatePosition(ayahToolBarPos);
// If the toolbar is not showing, show it
if (ayahToolBar.getVisibility() != View.VISIBLE) {
ayahToolBar.setVisibility(View.VISIBLE);
}
}
}
@Override
public void onPageSelected(int position) {
Timber.d("onPageSelected(): %d", position);
final int page = quranInfo.getPageFromPos(position, isDualPages);
if (quranSettings.shouldDisplayMarkerPopup()) {
lastPopupTime = QuranDisplayHelper.displayMarkerPopup(PagerActivity.this, quranInfo, page, lastPopupTime);
if (isDualPages) {
lastPopupTime = QuranDisplayHelper.displayMarkerPopup(PagerActivity.this, quranInfo, page - 1, lastPopupTime);
}
}
if (!showingTranslation) {
updateActionBarTitle(page);
} else {
refreshActionBarSpinner();
}
if (bookmarksCache.indexOfKey(page) < 0) {
if (isDualPages) {
if (bookmarksCache.indexOfKey(page - 1) < 0) {
checkIfPageIsBookmarked(page - 1, page);
}
} else {
// we don't have the key
checkIfPageIsBookmarked(page);
}
}
// If we're more than 1 page away from ayah selection end ayah mode
if (isInAyahMode) {
final int startPage = quranInfo.getPageFromSuraAyah(start.sura, start.ayah);
int ayahPos = quranInfo.getPosFromPage(startPage, isDualPages);
if (Math.abs(ayahPos - position) > 1) {
endAyahMode();
}
}
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
setUiVisibilityListener();
audioStatusBar.setVisibility(View.VISIBLE);
}
toggleActionBarVisibility(true);
if (shouldAdjustPageNumber) {
// when going from two page per screen to one or vice versa, we adjust the page number,
// such that the first page is always selected.
int curPage = numberOfPages - page;
if (isDualPages) {
if (curPage % 2 != 0) {
curPage++;
}
curPage = numberOfPagesDual - (curPage / 2);
} else {
if (curPage % 2 == 0) {
curPage--;
}
curPage = numberOfPages - curPage;
}
page = curPage;
} else if (isDualPages) {
page = page / 2;
}
viewPager.setCurrentItem(page);
// just got created, need to reconnect to service
shouldReconnect = true;
// enforce orientation lock
if (quranSettings.isLockOrientation()) {
int current = getResources().getConfiguration().orientation;
if (quranSettings.isLandscapeOrientation()) {
if (current == Configuration.ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
return;
}
} else if (current == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
return;
}
}
LocalBroadcastManager.getInstance(this).registerReceiver(audioReceiver, new IntentFilter(AudioService.AudioUpdateIntent.INTENT_NAME));
downloadReceiver = new DefaultDownloadReceiver(this, QuranDownloadService.DOWNLOAD_TYPE_AUDIO);
String action = QuranDownloadNotifier.ProgressIntent.INTENT_NAME;
LocalBroadcastManager.getInstance(this).registerReceiver(downloadReceiver, new IntentFilter(action));
downloadReceiver.setListener(this);
}
Aggregations