kr.wdream.ui.IntroActivity.java Source code

Java tutorial

Introduction

Here is the source code for kr.wdream.ui.IntroActivity.java

Source

/*
 * This is the source code of Telegram for Android v. 3.x.x.
 * It is licensed under GNU GPL v. 2 or later.
 * You should have received a copy of the license in this archive (see LICENSE).
 *
 * Copyright Nikolai Kudashov, 2013-2016.
 */

package kr.wdream.ui;

import android.animation.ObjectAnimator;
import android.animation.StateListAnimator;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.DataSetObserver;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import kr.wdream.storyshop.AndroidUtilities;
import kr.wdream.storyshop.BuildVars;
import kr.wdream.storyshop.LocaleController;
import kr.wdream.tgnet.ConnectionsManager;
import kr.wdream.ui.ActionBar.Theme;

public class IntroActivity extends Activity {

    private static final String LOG_TAG = "IntroActivity";

    private ViewPager viewPager;
    private ImageView topImage1;
    private ImageView topImage2;
    private ViewGroup bottomPages;
    private int lastPage = 0;
    private boolean justCreated = false;
    private boolean startPressed = false;
    private int[] icons;
    private int[] titles;
    private int[] messages;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(kr.wdream.storyshop.R.style.Theme_TMessages);
        super.onCreate(savedInstanceState);
        Theme.loadRecources(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        Log.d(LOG_TAG, "onCreate");

        // ? 
        if (AndroidUtilities.isTablet()) {
            setContentView(kr.wdream.storyshop.R.layout.intro_layout_tablet);
            View imageView = findViewById(kr.wdream.storyshop.R.id.background_image_intro);
            BitmapDrawable drawable = (BitmapDrawable) getResources()
                    .getDrawable(kr.wdream.storyshop.R.drawable.catstile);
            drawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
            imageView.setBackgroundDrawable(drawable);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            setContentView(kr.wdream.storyshop.R.layout.intro_layout);
        }

        if (LocaleController.isRTL) {
            icons = new int[] { kr.wdream.storyshop.R.drawable.intro7, kr.wdream.storyshop.R.drawable.intro6,
                    kr.wdream.storyshop.R.drawable.intro5, kr.wdream.storyshop.R.drawable.intro4,
                    kr.wdream.storyshop.R.drawable.intro3, kr.wdream.storyshop.R.drawable.intro2,
                    kr.wdream.storyshop.R.drawable.intro1 };
            titles = new int[] { kr.wdream.storyshop.R.string.Page7Title, kr.wdream.storyshop.R.string.Page6Title,
                    kr.wdream.storyshop.R.string.Page5Title, kr.wdream.storyshop.R.string.Page4Title,
                    kr.wdream.storyshop.R.string.Page3Title, kr.wdream.storyshop.R.string.Page2Title,
                    kr.wdream.storyshop.R.string.Page1Title };
            messages = new int[] { kr.wdream.storyshop.R.string.Page7Message,
                    kr.wdream.storyshop.R.string.Page6Message, kr.wdream.storyshop.R.string.Page5Message,
                    kr.wdream.storyshop.R.string.Page4Message, kr.wdream.storyshop.R.string.Page3Message,
                    kr.wdream.storyshop.R.string.Page2Message, kr.wdream.storyshop.R.string.Page1Message };
        } else {
            icons = new int[] { kr.wdream.storyshop.R.drawable.intro1, kr.wdream.storyshop.R.drawable.intro2,
                    kr.wdream.storyshop.R.drawable.intro3, kr.wdream.storyshop.R.drawable.intro4,
                    kr.wdream.storyshop.R.drawable.intro5, kr.wdream.storyshop.R.drawable.intro6,
                    kr.wdream.storyshop.R.drawable.intro7 };
            titles = new int[] { kr.wdream.storyshop.R.string.Page1Title, kr.wdream.storyshop.R.string.Page2Title,
                    kr.wdream.storyshop.R.string.Page3Title, kr.wdream.storyshop.R.string.Page4Title,
                    kr.wdream.storyshop.R.string.Page5Title, kr.wdream.storyshop.R.string.Page6Title,
                    kr.wdream.storyshop.R.string.Page7Title };
            messages = new int[] { kr.wdream.storyshop.R.string.Page1Message,
                    kr.wdream.storyshop.R.string.Page2Message, kr.wdream.storyshop.R.string.Page3Message,
                    kr.wdream.storyshop.R.string.Page4Message, kr.wdream.storyshop.R.string.Page5Message,
                    kr.wdream.storyshop.R.string.Page6Message, kr.wdream.storyshop.R.string.Page7Message };
        }

        initView();

    }

    private void initView() {
        viewPager = (ViewPager) findViewById(kr.wdream.storyshop.R.id.intro_view_pager);
        TextView startMessagingButton = (TextView) findViewById(kr.wdream.storyshop.R.id.start_messaging_button);
        startMessagingButton.setText(LocaleController
                .getString("StartMessaging", kr.wdream.storyshop.R.string.StartMessaging).toUpperCase());
        if (Build.VERSION.SDK_INT >= 21) {
            StateListAnimator animator = new StateListAnimator();
            animator.addState(new int[] { android.R.attr.state_pressed }, ObjectAnimator
                    .ofFloat(startMessagingButton, "translationZ", AndroidUtilities.dp(2), AndroidUtilities.dp(4))
                    .setDuration(200));
            animator.addState(new int[] {}, ObjectAnimator
                    .ofFloat(startMessagingButton, "translationZ", AndroidUtilities.dp(4), AndroidUtilities.dp(2))
                    .setDuration(200));
            startMessagingButton.setStateListAnimator(animator);
        }
        topImage1 = (ImageView) findViewById(kr.wdream.storyshop.R.id.icon_image1);
        topImage2 = (ImageView) findViewById(kr.wdream.storyshop.R.id.icon_image2);
        bottomPages = (ViewGroup) findViewById(kr.wdream.storyshop.R.id.bottom_pages);
        topImage2.setVisibility(View.GONE);
        viewPager.setAdapter(new IntroAdapter());
        viewPager.setPageMargin(0);
        viewPager.setOffscreenPageLimit(1);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {
                if (i == ViewPager.SCROLL_STATE_IDLE || i == ViewPager.SCROLL_STATE_SETTLING) {
                    if (lastPage != viewPager.getCurrentItem()) {
                        lastPage = viewPager.getCurrentItem();

                        final ImageView fadeoutImage;
                        final ImageView fadeinImage;
                        if (topImage1.getVisibility() == View.VISIBLE) {
                            fadeoutImage = topImage1;
                            fadeinImage = topImage2;

                        } else {
                            fadeoutImage = topImage2;
                            fadeinImage = topImage1;
                        }

                        fadeinImage.bringToFront();
                        fadeinImage.setImageResource(icons[lastPage]);
                        fadeinImage.clearAnimation();
                        fadeoutImage.clearAnimation();

                        Animation outAnimation = AnimationUtils.loadAnimation(IntroActivity.this,
                                kr.wdream.storyshop.R.anim.icon_anim_fade_out);
                        outAnimation.setAnimationListener(new Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation animation) {
                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                                fadeoutImage.setVisibility(View.GONE);
                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {

                            }
                        });

                        Animation inAnimation = AnimationUtils.loadAnimation(IntroActivity.this,
                                kr.wdream.storyshop.R.anim.icon_anim_fade_in);
                        inAnimation.setAnimationListener(new Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation animation) {
                                fadeinImage.setVisibility(View.VISIBLE);
                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {

                            }
                        });

                        fadeoutImage.startAnimation(outAnimation);
                        fadeinImage.startAnimation(inAnimation);
                    }
                }
            }
        });

        startMessagingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (startPressed) {
                    return;
                }
                startPressed = true;
                Intent intent2 = new Intent(IntroActivity.this, LaunchActivity.class);
                intent2.putExtra("fromIntro", true);
                Log.d(LOG_TAG, "startActivity(LaunchActivity), fromIntro : " + true);
                startActivity(intent2);
                finish();
            }
        });
        if (BuildVars.DEBUG_VERSION) {
            startMessagingButton.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    ConnectionsManager.getInstance().switchBackend();
                    return true;
                }
            });
        }

        justCreated = true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (justCreated) {
            if (LocaleController.isRTL) {
                viewPager.setCurrentItem(6);
                lastPage = 6;
            } else {
                viewPager.setCurrentItem(0);
                lastPage = 0;
            }
            justCreated = false;
        }
        AndroidUtilities.checkForCrashes(this);
        AndroidUtilities.checkForUpdates(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        AndroidUtilities.unregisterUpdates();
    }

    private class IntroAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return 7;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = View.inflate(container.getContext(), kr.wdream.storyshop.R.layout.intro_view_layout, null);
            TextView headerTextView = (TextView) view.findViewById(kr.wdream.storyshop.R.id.header_text);
            TextView messageTextView = (TextView) view.findViewById(kr.wdream.storyshop.R.id.message_text);
            container.addView(view, 0);

            headerTextView.setText(getString(titles[position]));
            messageTextView.setText(AndroidUtilities.replaceTags(getString(messages[position])));

            return view;
        }

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

        @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            super.setPrimaryItem(container, position, object);
            int count = bottomPages.getChildCount();
            for (int a = 0; a < count; a++) {
                View child = bottomPages.getChildAt(a);
                if (a == position) {
                    child.setBackgroundColor(0xff2ca5e0);
                } else {
                    child.setBackgroundColor(0xffbbbbbb);
                }
            }
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view.equals(object);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {
            if (observer != null) {
                super.unregisterDataSetObserver(observer);
            }
        }
    }
}