/**
  * Closes the activity and saves it as completed. A subsequent call to WelcomeScreenHelper.show()
  * would not show this again, unless the key is changed.
  */
 protected void completeWelcomeScreen() {
   SharedPreferencesHelper.storeWelcomeCompleted(this, getKey());
   setWelcomeScreenResult(RESULT_OK);
   super.finish();
   if (mConfiguration.getExitAnimation() != WelcomeScreenConfiguration.NO_ANIMATION_SET)
     overridePendingTransition(R.anim.none, mConfiguration.getExitAnimation());
 }
  @Override
  public void onBackPressed() {

    boolean scrolledBack = false;
    if (mConfiguration.getBackButtonNavigatesPages()) {
      scrolledBack = scrollToPreviousPage();
    }

    if (!scrolledBack) {

      if (mConfiguration.getCanSkip() && mConfiguration.getBackButtonSkips()) {
        completeWelcomeScreen();
      } else {
        cancelWelcomeScreen();
      }
    }
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

    if (mConfiguration.getShowActionBarBackButton() && item.getItemId() == android.R.id.home) {
      cancelWelcomeScreen();
      return true;
    }

    return super.onOptionsItemSelected(item);
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    mConfiguration = configuration();
    // overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    super.setTheme(mConfiguration.getThemeResId());
    /* Passing null for savedInstanceState fixes issue with fragments in list not matching
      the displayed ones after the screen was rotated. (Parallax animations would stop working)
      TODO: Look into this more
    */
    super.onCreate(null);
    setContentView(R.layout.activity_welcome);

    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

    mAdapter = new WelcomeFragmentPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.view_pager);
    mViewPager.setAdapter(mAdapter);

    if (mConfiguration.getShowActionBarBackButton()) {
      ActionBar actionBar = getSupportActionBar();
      if (actionBar != null) actionBar.setDisplayHomeAsUpEnabled(true);
    }

    SkipButton skip = new SkipButton(findViewById(R.id.button_skip), mConfiguration.getCanSkip());
    skip.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            completeWelcomeScreen();
          }
        });

    PreviousButton prev = new PreviousButton(findViewById(R.id.button_prev));
    prev.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View view) {
            scrollToPreviousPage();
          }
        });

    NextButton next = new NextButton(findViewById(R.id.button_next));
    next.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            scrollToNextPage();
          }
        });

    DoneButton done = new DoneButton(findViewById(R.id.button_done));
    done.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            completeWelcomeScreen();
          }
        });

    WelcomeScreenViewPagerIndicator indicator =
        (WelcomeScreenViewPagerIndicator) findViewById(R.id.pager_indicator);
    WelcomeScreenBackgroundView background =
        (WelcomeScreenBackgroundView) findViewById(R.id.background_view);

    WelcomeScreenHider hider = new WelcomeScreenHider(findViewById(R.id.root));
    hider.setOnViewHiddenListener(
        new WelcomeScreenHider.OnViewHiddenListener() {
          @Override
          public void onViewHidden() {
            completeWelcomeScreen();
          }
        });

    mItems =
        new WelcomeScreenItemList(
            background, indicator, skip, prev, next, done, hider, mConfiguration.getPages());
    mViewPager.addOnPageChangeListener(mItems);
    mViewPager.setCurrentItem(mConfiguration.firstPageIndex());
    mItems.setup(mConfiguration);
    mItems.onPageSelected(mViewPager.getCurrentItem());
  }
 private boolean canScrollToPreviousPage() {
   return mConfiguration.isRtl()
       ? getPreviousPageIndex() <= mConfiguration.firstPageIndex()
       : getPreviousPageIndex() >= mConfiguration.firstPageIndex();
 }
 private boolean canScrollToNextPage() {
   return mConfiguration.isRtl()
       ? getNextPageIndex() >= mConfiguration.lastViewablePageIndex()
       : getNextPageIndex() <= mConfiguration.lastViewablePageIndex();
 }
 private int getPreviousPageIndex() {
   return mViewPager.getCurrentItem() + (mConfiguration.isRtl() ? 1 : -1);
 }
 private int getNextPageIndex() {
   return mViewPager.getCurrentItem() + (mConfiguration.isRtl() ? -1 : 1);
 }