package com.ramdroid.wizardbuilder;

/**
 *    Copyright 2012-2013 by Ronald Ammann (ramdroid)

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.viewpagerindicator.LinePageIndicator;
import com.viewpagerindicator.PageIndicator;

/**
 * In this activity a ViewIndicator and a {@link WizardAdapter} is created.
 * The adapter holds a set of {@link WizardFragment} to allow the user to
 * swipe between the pages.
 *
 * The activity depends on the Android Support Library and ActionBarSherlock.
 *
 * Don't launch this activity directly. It will be called from {@link WizardBuilder}.
 */
public class WizardActivity extends SherlockFragmentActivity {

    private WizardPageSet pages;
    private int whatsNewId;
    private String title;
    private boolean indicatorBelow;
    private int backgroundImageId;
    private ViewPager mPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle data = this.getIntent().getExtras();
        if (data != null) {
            pages = data.getParcelable("pages");
            whatsNewId = data.getInt("whatsNewId");
            title = data.getString("title");
            indicatorBelow = data.getBoolean("indicatorBelow");
            backgroundImageId = data.getInt("backgroundImageId");
        }

        setContentView(indicatorBelow ? R.layout.helpwizard_indicator_below : R.layout.helpwizard_indicator_above);

        if (title != null && title.length() > 0) {
            setTitle(title);
        }

        // use action bar to jump back to calling activity (used on phones only)
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        // set the pager with an adapter
        WizardAdapter adapter = new WizardAdapter(getSupportFragmentManager());
        adapter.setValues(pages, whatsNewId);
        mPager = (ViewPager)findViewById(R.id.viewpager);
        mPager.setAdapter(adapter);

        // bind the title indicator to the adapter
        PageIndicator indicator = (PageIndicator)findViewById(R.id.indicator);
        indicator.setViewPager(mPager);

        // instead of mPager.setOnPageChangeListener(mOnPageChangeListener);
        indicator.setOnPageChangeListener(mOnPageChangeListener);

        ViewGroup viewGroup = (ViewGroup) mPager.getParent();
        if (viewGroup != null) {
            try {
                viewGroup.setBackgroundResource(backgroundImageId);
            }
            catch (Resources.NotFoundException e) {
            }
        }
    }

    public boolean onOptionsItemSelected (MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home: {
            finish();
            return true;
        }
        default: {
            return super.onOptionsItemSelected(item);
        }
        }
    }

    private ViewPager.OnPageChangeListener mOnPageChangeListener = new OnPageChangeListenerWrapper() {
        boolean lastPageOverscrolled;
        boolean isLastPage;

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (isLastPage && position == mPager.getCurrentItem() && !lastPageOverscrolled) {
                isLastPage = false;
                lastPageOverscrolled = true;
                finish();
            } else {
                isLastPage = false;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if (mPager.getCurrentItem() == mPager.getAdapter().getCount() - 1) {
                isLastPage = true;
            }
        }
    };
}

@@ -729,16 +729,75 @@
 wPager;%0A
+import android.view.MotionEvent;%0Aimport android.view.View;%0A
 import a
@@ -3431,32 +3431,1021 @@
      %7D%0A        %7D
+%0A%0A        mPager.setOnTouchListener(new View.OnTouchListener() %7B%0A            float oldX = 0, newX = 0, sens = 5;%0A%0A            @Override%0A            public boolean onTouch(View v, MotionEvent event) %7B%0A                switch (event.getAction()) %7B%0A                    case MotionEvent.ACTION_DOWN:%0A                        oldX = event.getX();%0A                        break;%0A%0A                    case MotionEvent.ACTION_UP:%0A                        newX = event.getX();%0A                        if (Math.abs(oldX - newX) %3C sens) %7B%0A                            int nextItem = mPager.getCurrentItem() + 1;%0A                            if (nextItem %3E= mPager.getAdapter().getCount()) finish();%0A                            else mPager.setCurrentItem(nextItem);%0A                            return true;%0A                        %7D%0A                        oldX = 0;%0A                        newX = 0;%0A                        break;%0A                %7D%0A%0A                return false;%0A            %7D%0A        %7D);
 %0A    %7D%0A%0A    publ

Allow click to next page