Java tutorial
/* Swisscom Safe Connect Copyright (C) 2014 Swisscom This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.swisscom.safeconnect.activity; import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.graphics.BitmapFactory; 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.support.v4.view.ViewPager.PageTransformer; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.widget.Button; import android.widget.FrameLayout; import android.widget.HorizontalScrollView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.swisscom.safeconnect.R; import com.swisscom.safeconnect.utils.Config; import com.swisscom.safeconnect.view.HideShowImgSet; /** * Created by cianci on 10/15/14. */ public class TutorialActivity extends PipeActivity { public static final String KEY_INVOKED_FROM_MENU = "invoked_from_menu"; private static float BG_STRETCH_FACTOR = 2f; private HorizontalScrollView textureScrollView; private HorizontalScrollView secondPlanScrollView; private HorizontalScrollView firstPlanScrollView; private HideShowImgSet imgSet; private LinearLayout secondPlanImages; private FrameLayout firstPlanImages; private ImageView textureImageView; private ImageView visitor1, visitor2, victim, logo; private ImageView messages; private ViewPager viewPager; private ViewGroup paginator; private Button butSkip; private int currentPage = 0; private int pageIncrement = 1; private boolean landscape = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final boolean invokedFromMenu = getIntent().getBooleanExtra(KEY_INVOKED_FROM_MENU, false); if (!invokedFromMenu && !Config.getInstance().isShowTutorial()) { proceed(); return; } landscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE; setContentView(R.layout.tutorial); textureScrollView = (HorizontalScrollView) findViewById(R.id.textureScrollView); secondPlanScrollView = (HorizontalScrollView) findViewById(R.id.secondPlanScrollView); firstPlanScrollView = (HorizontalScrollView) findViewById(R.id.firstPlanScrollView); textureImageView = (ImageView) findViewById(R.id.textureImageView); secondPlanImages = (LinearLayout) findViewById(R.id.imgs_second_plan); firstPlanImages = (FrameLayout) findViewById(R.id.imgs_first_plan); imgSet = (HideShowImgSet) findViewById(R.id.img_set); visitor1 = (ImageView) findViewById(R.id.visitor1); visitor2 = (ImageView) findViewById(R.id.visitor2); messages = (ImageView) findViewById(R.id.msgs); victim = (ImageView) findViewById(R.id.victim); logo = (ImageView) findViewById(R.id.img_logo); viewPager = (ViewPager) findViewById(R.id.viewPager); paginator = (ViewGroup) findViewById(R.id.paginator); butSkip = (Button) findViewById(R.id.but_tutorial); butSkip.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // start the main activity if not invoked from menu if (!invokedFromMenu) { Config.getInstance().setShowTutorial(false); proceed(); } else { finish(); } } }); setUpPaginator(); int width = getResources().getDisplayMetrics().widthPixels; int height = getResources().getDisplayMetrics().heightPixels; Size size = setUpTextureImageView(width, height); setUpSecondPlanScrollView(width); setUpFirstPlanScrollView(width, height); setUpViewPagers(size, width); setUpContent(width); } private void proceed() { Intent intent; if (!Config.getInstance().areTermsAccepted()) { intent = new Intent(this, TermsPreviewActivity.class); } else { intent = new Intent(this, RegistrationActivity.class); } startActivity(intent); finish(); } @Override public boolean dispatchTouchEvent(android.view.MotionEvent event) { try { return super.dispatchTouchEvent(event); } catch (IllegalArgumentException e) { return true; } }; protected void setUpPaginator() { activatePageIndicator(currentPage); } // called by paginator public void nextPage(View view) { if (currentPage == 0) pageIncrement = 1; else if (currentPage == viewPager.getChildCount() - 1) pageIncrement = -1; activatePageIndicator(currentPage + pageIncrement); viewPager.setCurrentItem(currentPage); } private void activatePageIndicator(int page) { ImageView nextIndicator = (ImageView) paginator.getChildAt(currentPage); nextIndicator.setImageResource(R.drawable.ic_pagination_normal); currentPage = page % paginator.getChildCount(); ImageView currentIndicator = (ImageView) paginator.getChildAt(currentPage); currentIndicator.setImageResource(R.drawable.ic_pagination_active); if (currentPage == viewPager.getChildCount() - 1) { butSkip.setText(R.string.btn_continue); } else { butSkip.setText(R.string.btn_skip); } } protected Size getSize(int resourceId) { BitmapFactory.Options dimensions = new BitmapFactory.Options(); dimensions.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), resourceId, dimensions); return new Size(dimensions.outWidth, dimensions.outHeight); } private Size setUpTextureImageView(final int width, int height) { final Size size = getSize(R.drawable.tutorial_background); textureImageView.getLayoutParams().width = (int) (width * BG_STRETCH_FACTOR); size.height = textureImageView.getLayoutParams().height; size.width = textureImageView.getLayoutParams().width; textureScrollView.getViewTreeObserver() .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @SuppressWarnings("deprecation") @Override public void onGlobalLayout() { textureScrollView.getViewTreeObserver().removeGlobalOnLayoutListener(this); if (currentPage > 0 && textureScrollView.getScrollX() == 0) { textureScrollView.scrollTo(width / (viewPager.getChildCount() - 1) * currentPage, 0); } } }); return size; } private void setUpSecondPlanScrollView(final int width) { secondPlanImages.getLayoutParams().width = width * 2; LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width / (landscape ? 8 : 4), ViewGroup.LayoutParams.WRAP_CONTENT); params.setMargins(landscape ? width / 8 : 0, 0, width / 2, 0); visitor1.setLayoutParams(params); params = new LinearLayout.LayoutParams(width / (landscape ? 8 : 4), ViewGroup.LayoutParams.WRAP_CONTENT); visitor2.setLayoutParams(params); } private void setUpFirstPlanScrollView(int width, int height) { firstPlanImages.getLayoutParams().width = (int) (width * BG_STRETCH_FACTOR); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width / (landscape ? 6 : 3), height / 5); params.setMargins(width / 2 - width / (landscape ? 12 : 6), height / 5, 0, 0); victim.setLayoutParams(params); params = new FrameLayout.LayoutParams(4 * width / (landscape ? 10 : 5), height / 6); params.setMargins(width / 2 - width / (landscape ? 32 : 16), height / 13, 0, 0); messages.setLayoutParams(params); imgSet.addImage(R.drawable.tutorial4, width / (landscape ? 4 : 2), height / 4, width / 2 - width / 6); int imgWidth, imgHeight; if (getResources().getBoolean(R.bool.isTablet)) { imgHeight = 2 * (landscape ? height : width) / 5; imgWidth = 4 * (landscape ? height : width) / 5; } else { imgHeight = 2 * height / 5; imgWidth = imgHeight; } imgSet.addImage(R.drawable.android_sc, imgWidth, imgHeight, width / 2 - imgWidth / 2); } protected void setUpViewPagers(final Size size, final int width) { TextLayerPagerAdapter adapter = new TextLayerPagerAdapter(); viewPager.setOffscreenPageLimit(viewPager.getChildCount() - 1); viewPager.setAdapter(adapter); viewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int page) { activatePageIndicator(page); } @Override public void onPageScrolled(int position, float offset, int offsetPixels) { } @Override public void onPageScrollStateChanged(int state) { } }); viewPager.setPageTransformer(true, new PageTransformer() { @Override public void transformPage(View view, float pos) { if (view == viewPager.getChildAt(0)) { textureScrollView.scrollTo(Math.round(-width / (viewPager.getChildCount() - 1) * pos), 0); secondPlanScrollView.scrollTo(Math.round(-width / (viewPager.getChildCount() - 1) * pos * 1.8f), 0); firstPlanScrollView.scrollTo(Math.round(-width * 1.2f / (viewPager.getChildCount() - 1) * pos), 0); visitor1.setAlpha(1 + pos); visitor2.setAlpha(1 + pos); if (-pos <= 1) { messages.setAlpha(-pos); imgSet.moveImg(0, -pos); } else if (-pos >= 1 && -pos <= 2) { messages.setAlpha(2 + pos); imgSet.moveImg(0, 2 + pos); logo.setAlpha(-pos - 1); } else if (-pos > 2 && -pos <= 3) { logo.setAlpha(3 + pos); imgSet.moveImg(1, -pos - 2); } } } }); viewPager.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return false; } }); } protected void setUpContent(final int width) { final String[] pageDescriptions = getResources().getStringArray(R.array.tutorial_page_descriptions); for (int i = 0; i < viewPager.getAdapter().getCount(); i++) { ViewGroup viewGroup = (ViewGroup) viewPager.getAdapter().instantiateItem(null, i); viewGroup.getLayoutParams().width = width; if (i < pageDescriptions.length) { TextView descriptionTextView = (TextView) viewGroup.findViewById(R.id.descriptionTextView); descriptionTextView.setText(pageDescriptions[i]); } } } private void setLayer1Image(ImageView img, int pos) { switch (pos) { case 0: img.setImageResource(R.drawable.tutorial2); break; case 1: img.setImageResource(R.drawable.tutorial4); break; case 2: img.setImageResource(R.drawable.tutorial5); break; default: break; } } protected static class Size { public int width; public int height; Size(int width, int height) { this.width = width; this.height = height; } } public static float getSp(Context context, float pixel) { float scale = context.getResources().getDisplayMetrics().density; return Math.round(pixel / scale); } protected int getPageCount() { return getResources().getStringArray(R.array.tutorial_page_descriptions).length; } private class TextLayerPagerAdapter extends PagerAdapter { public Object instantiateItem(View collection, int position) { ViewGroup viewGroup = (ViewGroup) TutorialActivity.this.viewPager.getChildAt(position); return viewGroup; } @Override public int getCount() { return TutorialActivity.this.viewPager.getChildCount(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { // do nothing } } }