com.githang.androidkit.app.IntroActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.githang.androidkit.app.IntroActivity.java

Source

/*
 * @(#)IntroActivity.java             Project:androidkit
 * Date:2013-11-10
 *
 * Copyright (c) 2013 CFuture09, Institute of Software, 
 * Guangdong Ocean University, Zhanjiang, GuangDong, China.
 * All rights reserved.
 *
 * 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.
 */
package com.githang.androidkit.app;

import android.app.Activity;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.githang.androidkit.utils.Log4AK;
import com.githang.androidkit.utils.app.ActivityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Geek_Soledad <a target="_blank" href=
 *         "http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=XTAuOSVzPDM5LzI0OR0sLHM_MjA"
 *         style="text-decoration:none;"><img src=
 *         "http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_01.png"
 *         /></a>
 */
public abstract class IntroActivity extends Activity implements OnClickListener, OnPageChangeListener {

    private static final Log4AK log = Log4AK.getLog(IntroActivity.class);
    /**
     * SharedPreferences???
     */
    private static final String AK_PREF = "akconfig.pref";
    private static final String INTRO_KEY = "IntroVersion";

    private IntroResource mIntroResource;
    private ViewPager mViewPager;
    private List<ImageView> mIndicator;

    private ScrolledHelper mScroller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isFullScreen()) {
            ActivityUtil.setFullScreen(this);
        }
        init();
    }

    /**
     * ?
     */
    private void init() {
        mScroller = new ScrolledHelper();
        if (showOnlyAtUpdate() && !isAfterUpdate()) {
            finish();
            return;
        }
        createContentView();
        showIntro();
        updateIntroVersion();
    }

    /**
     * ?????
     * 
     * @return
     */
    private boolean isAfterUpdate() {
        try {
            int currentVersion = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionCode;
            int oldVersion = this.getSharedPreferences(AK_PREF, MODE_PRIVATE).getInt(INTRO_KEY, -1);
            return currentVersion > oldVersion;
        } catch (NameNotFoundException e) {
            log.w(e.getMessage(), e);
        }
        return false;
    }

    /**
     * ??????Activity
     */
    private void updateIntroVersion() {
        try {
            int versionCode = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionCode;
            this.getSharedPreferences(AK_PREF, MODE_PRIVATE).edit().putInt(INTRO_KEY, versionCode).commit();
        } catch (NameNotFoundException e) {
            log.w(e.getMessage(), e);
        }
    }

    /**
     * ?
     */
    private void showIntro() {
        IntroPagerAdapter adapter = new IntroPagerAdapter(mIntroResource.views);
        mViewPager.setAdapter(adapter);
        mViewPager.setOnPageChangeListener(this);
        mIndicator.get(0).setImageResource(mIntroResource.indicatorSelectedId);
    }

    /**
     * 
     */
    private void createContentView() {
        // ?
        mIntroResource = new IntroResource();
        setIntroViews(mIntroResource);

        // 
        RelativeLayout rootView = new RelativeLayout(this);
        rootView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        // 
        mViewPager = new ViewPager(this);
        RelativeLayout.LayoutParams pagerParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        rootView.addView(mViewPager, pagerParams);

        // 
        LinearLayout indicatorLayout = new LinearLayout(this);
        RelativeLayout.LayoutParams indicatorParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        indicatorParams.bottomMargin = mIntroResource.indicatorMarginBottom;
        indicatorParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        indicatorParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        indicatorLayout.setLayoutParams(indicatorParams);
        indicatorLayout.setOrientation(LinearLayout.HORIZONTAL);

        // 
        int indicatorCount = mIntroResource.views.size();
        int padding = mIntroResource.indicatorImagePadding;
        mIndicator = new ArrayList<ImageView>();
        for (int i = 0; i < indicatorCount; i++) {
            ImageView imageView = new ImageView(this);
            LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            imageView.setPadding(padding, padding, padding, padding);
            imageView.setClickable(true);
            imageView.setOnClickListener(this);
            imageView.setImageResource(mIntroResource.indicatorNoSelectedId);
            imageView.setTag(i);
            mIndicator.add(imageView);
            indicatorLayout.addView(imageView, imageParams);
        }

        rootView.addView(indicatorLayout);

        setContentView(rootView);
    }

    /**
     * ??
     * 
     * @return
     */
    protected boolean isFullScreen() {
        return false;
    }

    /**
     * ??
     * 
     * @return
     */
    protected boolean showOnlyAtUpdate() {
        return true;
    }

    /**
     * ??
     */
    protected void atEndButScrolled() {
    }

    /**
     * 
     * 
     * @param position
     */
    private void updateIndicator(int position) {
        int size = mIndicator.size();
        if (position >= 0 && position < size) {
            for (int i = 0; i < size; i++) {
                mIndicator.get(i).setImageResource(mIntroResource.indicatorNoSelectedId);
            }
            mIndicator.get(position).setImageResource(mIntroResource.indicatorSelectedId);
        }
    }

    /**
     * ??
     * 
     * @param position
     */
    private void setCurrentView(int position) {
        if (position >= 0 && position < mIntroResource.views.size()) {
            mViewPager.setCurrentItem(position);
        }
    }

    /**
     * ??view?
     * 
     * @param resource
     */
    protected abstract void setIntroViews(IntroResource resource);

    @Override
    public void onClick(View v) {
        if (v instanceof ImageView) {
            int position = (Integer) v.getTag();
            setCurrentView(position);
            updateIndicator(position);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        log.d("onPageScrollStateChanged:" + state);
        if (state == ViewPager.SCROLL_STATE_DRAGGING) {
        } else if (state == ViewPager.SCROLL_STATE_IDLE) {
            if (mScroller.isMoveDisable()) {
                atEndButScrolled();
            }
        } else if (state == ViewPager.SCROLL_STATE_SETTLING) {

        }
    }

    @Override
    public void onPageScrolled(int pos, float offset, int pixels) {
        if ((pos + 1) == mIndicator.size() && offset * 100000 < 1 && pixels == 0) {
            mScroller.setMoveDisable(true);
        } else {
            mScroller.setMoveDisable(false);
        }
        log.d(pos + "   " + offset + " " + pixels);
    }

    @Override
    public void onPageSelected(int pos) {
        updateIndicator(pos);
    }

    /**
     * ??????
     * 
     * @author Geek_Soledad <a target="_blank" href=
     *         "http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=XTAuOSVzPDM5LzI0OR0sLHM_MjA"
     *         style="text-decoration:none;"><img src=
     *         "http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_01.png"
     *         /></a>
     */
    public static class IntroResource {
        /**
         * view pager?
         */
        public List<? extends View> views;
        /**
         * id
         */
        public int indicatorSelectedId;
        /**
         * id
         */
        public int indicatorNoSelectedId;

        /**
         * ?24.
         */
        public int indicatorMarginBottom = 24;

        /**
         * ??
         */
        public int indicatorImagePadding = 15;

        /**
         * ??
         */
        public IntroResource() {
            super();
        }

        /**
         * ??
         * 
         * @param views
         *            view pager?
         * @param indicatorSelectedId
         *            id
         * @param indicatorNoSelectedId
         *            id
         */
        public IntroResource(List<? extends View> views, int indicatorSelectedId, int indicatorNoSelectedId) {
            super();
            this.views = views;
            this.indicatorSelectedId = indicatorSelectedId;
            this.indicatorNoSelectedId = indicatorNoSelectedId;
        }

    }

    /**
     * ??
     * 
     * @author Geek_Soledad <a target="_blank" href=
     *         "http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=XTAuOSVzPDM5LzI0OR0sLHM_MjA"
     *         style="text-decoration:none;"><img src=
     *         "http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_01.png"
     *         /></a>
     */
    public static class IntroPagerAdapter extends PagerAdapter {

        private List<? extends View> mListViews;

        public IntroPagerAdapter(List<? extends View> listviews) {
            mListViews = listviews;
        }

        @Override
        public int getCount() {
            return mListViews != null ? mListViews.size() : 0;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mListViews.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = mListViews.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }
    }

    protected static class ScrolledHelper {
        /**
         * ????
         */
        private boolean isMoveDisable;
        private int count;

        protected ScrolledHelper() {
        }

        public boolean isMoveDisable() {
            return isMoveDisable && count > 1;
        }

        public void setMoveDisable(boolean isMoveDisable) {
            this.isMoveDisable = isMoveDisable;
            if (isMoveDisable) {
                count++;
            } else {
                count = 0;
            }
        }
    }
}