com.ubiLive.GameCloud.activity.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.ubiLive.GameCloud.activity.MainActivity.java

Source

package com.ubiLive.GameCloud.activity;

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

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.StateListDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ubiLive.GameCloud.CloudGamePlayer;
import com.ubiLive.GameCloud.Constants;
import com.ubiLive.GameCloud.DebugLog;
import com.ubiLive.GameCloud.PreferenceManager;
import com.ubiLive.GameCloud.Utils;
import com.ubiLive.GameCloud.Browser.WebBrowser;
import com.ubiLive.GameCloud.parse.GCParse;
import com.ubiLive.GameCloud.payment.BillingActivity;
import com.ubiLive.GameCloud.ui.EulaUI;
import com.ubiLive.GameCloud.ui.GCApplication;
import com.ubiLive.GameCloud.ui.GCVideoView;
import com.ubiLive.GameCloud.ui.GameActivityRes;

public class MainActivity extends FragmentActivity {

    private final static String TAG = MainActivity.class.getSimpleName();
    public FrameLayout mFrameLayout;
    private EulaUI mEula;
    private GCVideoView mVideoView;
    public GCViewPage mViewPager;
    private GCParse mGCParse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setFullscreen(this);
        Utils.setNoTitle(this);
        if (GameActivity.m_player == null) {
            Utils.ShowOKPromptLoadLibError(this, "Error", "Sorry,Device Model is not supported.",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            MainActivity.this.finish();
                        }
                    });
            return;
        }
        mGCParse = GCApplication.getApplication().getGCParse();
        if (mGCParse != null && mGCParse.track(this)) {
            return;
        }
        DebugLog.d(TAG, "onCreate");
        mFrameLayout = new FrameLayout(this);
        mFrameLayout.setLayoutParams(
                new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        setContentView(mFrameLayout);
        mViewPager = initIntroPage(mFrameLayout);
        mEula = initEulaPage(mFrameLayout);
        if (mEula == null && mViewPager == null) {
            checkContentlicenseAction();
        }
    }

    private void switchToWebOrGameActivity() {
        DebugLog.d(TAG, "MainActivity.switchToWebOrGameActivity() enter");
        DebugLog.e(TAG, "switchToWebOrGameActivity() WebBrowser.sBCurrentActivityStatus="
                + WebBrowser.sBCurrentActivityStatus);
        if (WebBrowser.sBCurrentActivityStatus) {
            switchToWebActivity();
        } else {
            switchToGameActivity();
        }
    }

    private void switchToGameActivity() {
        Intent intent = new Intent();
        intent.setClass(this, GameActivity.class);
        this.startActivity(intent);
        this.finish();
    }

    private void switchToWebActivity() {
        Intent intent = new Intent();
        intent.setClass(this, LoadWebJsActivity.class);
        this.startActivity(intent);
        this.finish();
    }

    private void checkContentlicenseAction() {
        DebugLog.e(TAG, "=======checkContentlicenseAction=======");

        if (mGCParse != null) {
            mGCParse.subscribe(GCApplication.getApplication());
        }

        //UtilsRes.sBInitialJNI = true;
        if (Constants.CONTROL_BILLING_CHECK_CONTENT_LICENSE) {
            billingCheckContentLicense();
        } else {
            switchToWebOrGameActivity();
        }
    }

    private void billingCheckContentLicense() {
        final ProgressDialog progressDialog = Utils.ShowProgressDialog(this, null,
                GameActivityRes.STRING_CHECKINGCONTENTLICENSE, null, false);
        progressDialog.show();
        new Thread(new Runnable() {
            public void run() {
                if (-1 == BillingActivity.checkContentLicense(Constants.BILLING_KEYWORD, MainActivity.this)) {//Constants.BILLING_KEYWORD   
                    DebugLog.d(TAG, "====failed check content license =====");
                    MainActivity.this.runOnUiThread(new Runnable() {
                        public void run() {
                            if (progressDialog != null && progressDialog.isShowing()) {
                                progressDialog.dismiss();
                            }
                            Utils.ShowOKPrompt(MainActivity.this, GameActivityRes.STRING_ERROR,
                                    GameActivityRes.STRING_CHECKCONTENTLICENSEFAIL,
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                            finish();
                                        }
                                    }, new DialogInterface.OnKeyListener() {
                                        @Override
                                        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                                            // TODO Auto-generated method stub
                                            DebugLog.d(TAG, "KeyEvent.KEYCODE_BACK keyCode = " + keyCode);
                                            DebugLog.d(TAG, "KeyEvent.KEYCODE_BACK = " + KeyEvent.KEYCODE_BACK);
                                            if (keyCode == KeyEvent.KEYCODE_BACK) {
                                                return true;
                                            }
                                            return false;
                                        }
                                    });

                        }
                    });

                } else {//verify license successfully
                    DebugLog.d(TAG, "====successed check content license =====");
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            if (progressDialog != null && progressDialog.isShowing()) {
                                progressDialog.dismiss();
                            }
                            //skip
                            switchToWebOrGameActivity();
                        }

                    });
                }

            }
        }).start();

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        mEula = null;
        mVideoView = null;
        mViewPager = null;
        mFrameLayout = null;
        mEulaUIOnClickListener = null;
        mGCParse = null;
        super.onDestroy();
        DebugLog.d(TAG, "onDestroy");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        DebugLog.d(TAG, "onKeyDown");
        return super.onKeyDown(keyCode, event);

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        if (mVideoView != null) {
            mVideoView.pauseVideo();
            ;
        }
        DebugLog.d(TAG, "MainActivity onPause");
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        DebugLog.d(TAG, "onRestart");
    }

    @Override
    protected void onResume() {
        DebugLog.d(TAG, "MainActivity onResume");
        super.onResume();
        if (mVideoView != null) {
            mVideoView.resumeVideo();
        }

    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        DebugLog.d(TAG, "MainActivity onStart");
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        DebugLog.d(TAG, "MainActivity onStop");
    }

    private GCViewPage initIntroPage(ViewGroup root) {
        SharedPreferences sharedpreferences = PreferenceManager.openPrference(this, Constants.PREFERENCE_DB);
        boolean enableIntro = !PreferenceManager.getBooleanValue(sharedpreferences, Constants.INTROSHOW);
        GCViewPage viewpager = null;
        if (enableIntro) {
            MarkView mark = null;
            int[] introPictures;
            int[] introStartBtn;
            if (Utils.getLanguage() == Constants.UPUI_MSG_LANGUAGE_TRAN_CHINESE) {
                if ((float) Utils.getScreenWidth(this) / (float) Utils.getScreenHeight(this) > 1.5f) {
                    introPictures = GCViewPage.INTRO_16_9_ZH_TW;
                    introStartBtn = GCViewPage.INTRO_BTN_16_9_ZH_TW;
                    DebugLog.d(TAG, "INTRO_16_9");
                } else {
                    introPictures = GCViewPage.INTRO_ZH_TW;
                    introStartBtn = GCViewPage.INTRO_BTN_ZH_TW;
                    DebugLog.d(TAG, "INTRO");
                }
            } else {
                introPictures = GCViewPage.INTRO;
                introStartBtn = GCViewPage.INTRO_BTN;
                mark = new MarkView(this, introPictures);
            }
            DebugLog.d(TAG, "DetailsFragment introPictures = " + introPictures.length);
            viewpager = new GCViewPage(this, root, introPictures, introStartBtn);
            viewpager.setId(11111);
            viewpager.attachToWindow();
            if (mark != null) {
                viewpager.setOnPageChangeListener(mark);
                root.addView(mark);
            }
        }
        return viewpager;
    }

    private EulaUI initEulaPage(ViewGroup root) {
        SharedPreferences sharedpreferences = PreferenceManager.openPrference(this, Constants.PREFERENCE_DB);
        boolean enableEula = !PreferenceManager.getBooleanValue(sharedpreferences, Constants.EULASTATUS);
        EulaUI eulaUI = null;
        if (enableEula) {
            mVideoView = initVideoPage(root);
            eulaUI = new EulaUI(this, Utils.getScreenWidth(this), Utils.getScreenHeight(this));
            eulaUI.getAcceptButton().setOnClickListener(mEulaUIOnClickListener);
            root.addView(eulaUI);
            if (mGCParse != null) {
                mGCParse.trackEvent(GCParse.TRACKEVENT_EULASHOW, null);
            }
        }
        return eulaUI;
    }

    private GCVideoView initVideoPage(ViewGroup root) {
        GCVideoView videoView = new GCVideoView(MainActivity.this);
        videoView.setVideoSize(Utils.getScreenWidth(MainActivity.this), Utils.getScreenHeight(MainActivity.this));
        root.addView(videoView);
        return videoView;
    }

    private View.OnClickListener mEulaUIOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences sharedpreferences = PreferenceManager.openPrference(MainActivity.this,
                    Constants.PREFERENCE_DB);
            PreferenceManager.updateBooleanValue(sharedpreferences, Constants.EULASTATUS, true);
            mFrameLayout.removeView(mEula);
            if (mGCParse != null) {
                mGCParse.trackEvent(GCParse.TRACKEVENT_EULABTNPRESSED, null);
            }
            if (mVideoView != null) {
                mVideoView.setTouchable(false);
                mVideoView.playVideo(mVideoView.getResourceUri(GameActivityRes.RAW_INTRO_VIDEO_480P));
                mVideoView.setOnCompletionListener(new OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mVideoView.suspend();
                        mFrameLayout.removeView(mVideoView);
                        mVideoView = null;
                        System.gc();
                        if (mViewPager == null) {
                            checkContentlicenseAction();
                        }
                    }
                });
            } else {
                checkContentlicenseAction();
            }
        }
    };

    private static class GCViewPage extends ViewPager {

        private static final int[] INTRO = { GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_01,
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_02,
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_03,
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_04,
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_05 };

        private static final int[] INTRO_ZH_TW = { GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_01_ZH_TW,
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_02_ZH_TW,
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_03_ZH_TW,
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_04_ZH_TW,
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_05_ZH_TW,
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_06_ZH_TW };

        private static final int[] INTRO_16_9_ZH_TW = {
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_01_16_9_ZH_TW,
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_02_16_9_ZH_TW,
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_03_16_9_ZH_TW,
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_04_16_9_ZH_TW,
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_05_16_9_ZH_TW,
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_06_16_9_ZH_TW };

        private static final int[] INTRO_BTN = {
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_START_PRESS,
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_START };

        private static final int[] INTRO_BTN_ZH_TW = {
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_START_PRESS_ZH_TW,
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_START_ZH_TW };

        private static final int[] INTRO_BTN_16_9_ZH_TW = {
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_START_PRESS_16_9_ZH_TW,
                GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_TABLET_START_16_9_ZH_TW };

        private ViewGroup mRoot;
        private List<DetailsFragment> mDetailsFragmentList = new ArrayList<DetailsFragment>();
        private int[] mPicArray;
        private int[] mBtnArray;

        public GCViewPage(FragmentActivity context, ViewGroup root, int[] picArray, int[] btnArray) {
            super(context);
            mRoot = root;
            mPicArray = picArray;
            mBtnArray = btnArray;
            PagerAdapter pageAdapter = new PagerAdapter(context.getSupportFragmentManager());
            setAdapter(pageAdapter);
        }

        public void attachToWindow() {
            mRoot.addView(this);
        }

        public void detachedFromWindow() {
            mRoot.removeAllViews();
            for (int i = 0; i < (mPicArray.length); i++) {
                mDetailsFragmentList.get(i).releaseBitmap();
                DebugLog.d(TAG, "DetailsFragment detachedFromWindow = " + i);
            }
            System.gc();
        }

        private class PagerAdapter extends FragmentPagerAdapter {
            public PagerAdapter(FragmentManager fm) {
                super(fm);
            }

            @Override
            public int getCount() {
                return mPicArray.length;
            }

            @Override
            public Fragment getItem(int position) {
                DebugLog.d(TAG, "Fragment getItem ");
                DetailsFragment detail = new DetailsFragment(position);
                mDetailsFragmentList.add(detail);
                return detail;
            }
        }

        public class DetailsFragment extends Fragment {

            private final static int USGNOW_DESIGNER_STANDED_WIDTH = 1024;
            private final static int USGNOW_DESIGNER_STANDED_HEIGHT = 572;

            private final static int USGNOW_DESIGNER_STANDED_BUTTON_WIDTH = 412;
            private final static int USGNOW_DESIGNER_STANDED_BUTTON_HEIGHT = 110;

            private Bitmap mDetailsBitmap;
            private ImageView mDetailsView;

            public DetailsFragment(int index) {
                Bundle args = new Bundle();
                args.putInt("index", index);
                setArguments(args);
            }

            @Override
            public void onStart() {
                DebugLog.d(TAG, "DetailsFragment onStart() = " + getArguments().getInt("index", 0));
                DisplayMetrics metrics = new DisplayMetrics();
                getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
                Bitmap bitmap = Utils.BitmapUtils.decodeSampledBitmapFromResource(getActivity().getResources(),
                        mPicArray[getArguments().getInt("index", 0)], metrics.widthPixels, metrics.heightPixels);
                mDetailsBitmap = Utils.BitmapUtils.scaleBitmap(bitmap, metrics.widthPixels, metrics.heightPixels);
                bitmap.recycle();
                mDetailsView.setImageBitmap(mDetailsBitmap);
                super.onStart();
            }

            @Override
            public void onStop() {
                super.onStop();
                DebugLog.d(TAG, "DetailsFragment onStop() = " + getArguments().getInt("index", 0));
                releaseBitmap();
            }

            @Override
            public void onDestroy() {
                super.onDestroy();
            }

            public void releaseBitmap() {
                mDetailsView.setImageBitmap(null);
                if (mDetailsBitmap != null && !mDetailsBitmap.isRecycled()) {
                    mDetailsBitmap.recycle();
                }
            }

            public int getShownIndex() {
                return getArguments().getInt("index", 0);
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

                if (container == null) {
                    return null;
                }

                FrameLayout frameLayout = new FrameLayout(getActivity());
                frameLayout.setLayoutParams(
                        new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                FrameLayout.LayoutParams imagelp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                        LayoutParams.MATCH_PARENT);
                mDetailsView = new ImageView(getActivity());
                mDetailsView.setLayoutParams(imagelp);
                frameLayout.addView(mDetailsView);

                if (getShownIndex() == (mPicArray.length - 1)) {
                    Button start = new Button(getActivity());
                    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                            (int) (USGNOW_DESIGNER_STANDED_BUTTON_WIDTH
                                    * ((float) Utils.getScreenWidth(getActivity())
                                            / USGNOW_DESIGNER_STANDED_WIDTH)),
                            (int) (USGNOW_DESIGNER_STANDED_BUTTON_HEIGHT
                                    * (((float) Utils.getScreenHeight(getActivity())
                                            / USGNOW_DESIGNER_STANDED_HEIGHT))));
                    lp.gravity = Gravity.CENTER_HORIZONTAL;
                    lp.topMargin = Utils.getScreenHeight(getActivity()) / 2 + 50;
                    DebugLog.d(TAG, "lp.topMargin = " + lp.topMargin);
                    StateListDrawable drawable = new StateListDrawable();
                    drawable.addState(new int[] { android.R.attr.state_pressed },
                            getActivity().getResources().getDrawable(mBtnArray[0]));
                    drawable.addState(new int[] {}, getActivity().getResources().getDrawable(mBtnArray[1]));
                    start.setBackgroundDrawable(drawable);
                    start.setLayoutParams(lp);
                    start.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            final SharedPreferences sharedpreferences = PreferenceManager
                                    .openPrference(getActivity(), Constants.PREFERENCE_DB);
                            PreferenceManager.updateBooleanValue(sharedpreferences, Constants.INTROSHOW, true);
                            PreferenceManager.updateBooleanValue(sharedpreferences, Constants.FIRSTLOGIN, true);
                            GCParse parse = GCApplication.getApplication().getGCParse();
                            if (parse != null) {
                                parse.trackEvent(GCParse.TRACKEVENT_STARTBTNPRESSED, null);
                            }

                            ((MainActivity) getActivity()).checkContentlicenseAction();

                        }
                    });
                    frameLayout.addView(start);
                }

                return frameLayout;

            }
        }
    }

    private static class MarkView extends LinearLayout implements OnPageChangeListener {

        public MarkView(Context context, int[] marks) {
            super(context);
            setOrientation(LinearLayout.HORIZONTAL);
            FrameLayout.LayoutParams mlt = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            mlt.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
            mlt.bottomMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20,
                    getResources().getDisplayMetrics());
            setLayoutParams(mlt);
            int value = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
                    getResources().getDisplayMetrics());
            for (int i = 0; i < marks.length; i++) {
                ImageView imageView = new ImageView(context);
                LinearLayout.LayoutParams ilt = new LinearLayout.LayoutParams(value, value);
                ilt.rightMargin = value;
                if (i == (marks.length - 1))
                    ilt.rightMargin = 0;
                if (i == 0)
                    imageView.setImageResource(GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_ICON_GREEN);
                imageView.setLayoutParams(ilt);
                imageView.setBackgroundResource(GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_ICON_WHILE);
                addView(imageView);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            int count = getChildCount();
            for (int i = 0; i < count; i++) {
                ((ImageView) getChildAt(i)).setImageResource(0);
            }
            ((ImageView) getChildAt(position))
                    .setImageResource(GameActivityRes.DRAWABLE_GC_CLIENT_ANDROID_INTRO_ICON_GREEN);
        }
    }
}