org.loon.framework.android.game.LGameAndroid2DActivity.java Source code

Java tutorial

Introduction

Here is the source code for org.loon.framework.android.game.LGameAndroid2DActivity.java

Source

package org.loon.framework.android.game;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.LinkedList;

import org.apache.http.HttpException;
import org.loon.framework.android.game.core.EmulatorListener;
import org.loon.framework.android.game.core.LSystem;
import org.loon.framework.android.game.core.graphics.IScreen;
import org.loon.framework.android.game.core.graphics.LImage;
import org.loon.framework.android.game.core.graphics.geom.Dimension;

import com.admob.android.ads.AdManager;
import com.admob.android.ads.AdView;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.res.Configuration;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

/**
 * 
 * Copyright 2008 - 2010
 * 
 * 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.
 * 
 * @project loonframework
 * @author chenpeng
 * @email ceponline@yahoo.com.cn
 * @version 0.1.0
 */
public abstract class LGameAndroid2DActivity extends Activity implements SensorEventListener {

    private static final String dealutKeywords = "game play video mobile app buy love down world";

    private SensorManager sensorManager;

    private Sensor sensorAccelerometer;

    private Sensor sensorMagnetic;

    private boolean visible, keyboardOpen, isLandscape, isBackLocked;

    private int androidSelect, orientation;

    // PS:Android
    private AlertDialog alert;

    // ??FPS?
    // 
    // ?,??for,???
    private long keyTimeMillis;

    private LGameAndroid2DView view;

    private AdView adview;

    private IAndroid2DHandler gameHandler;

    private FrameLayout frameLayout;

    private boolean setupSensors;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        LSystem.gc();
        Log.i("Android2DActivity", "LGame 2D Engine Start");
        this.androidSelect = -2;
        this.frameLayout = new FrameLayout(LGameAndroid2DActivity.this);
        this.onMain();
    }

    /**
     * ??
     */
    public void setupGravity() {
        this.setupSensors = true;
    }

    private void initSensors() {
        try {
            if (sensorManager == null) {
                sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
            }

            if (sensorAccelerometer == null) {
                sensorAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

            }

            if (sensorMagnetic == null) {
                sensorMagnetic = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

            }

            boolean accelSupported = sensorManager.registerListener(this, sensorAccelerometer,
                    SensorManager.SENSOR_DELAY_GAME);

            // ??Accelerometer??
            if (!accelSupported) {
                sensorManager.unregisterListener(this, sensorAccelerometer);
            }

            this.sensorManager.registerListener(this, sensorMagnetic, SensorManager.SENSOR_DELAY_GAME);
        } catch (Exception ex) {

        }
    }

    private void stopSensors() {
        try {
            if (sensorManager != null) {
                this.sensorManager.unregisterListener(this);
                this.sensorManager = null;
            }
        } catch (Exception ex) {

        }
    }

    public abstract void onMain();

    public void initialization() {
        initialization(false, false, null);
    }

    public void initialization(boolean landscape) {
        initialization(landscape, false, null);
    }

    public void initialization(boolean landscape, boolean openAD, String publisherId) {
        initialization(landscape, openAD, Location.BOTTOM, publisherId);
    }

    public void initialization(boolean landscape, String publisherId) {
        initialization(landscape, true, Location.BOTTOM, publisherId);
    }

    public void initialization(Location lad, String publisherId) {
        initialization(false, true, lad, publisherId);
    }

    public void initialization(Bundle icicle, boolean landscape, Location lad, String publisherId) {
        initialization(landscape, true, lad, publisherId);
    }

    public void initialization(boolean landscape, boolean openAD, Location lad, String publisherId) {
        initialization(landscape, openAD, lad, publisherId, dealutKeywords);
    }

    public void initialization(Location lad, String publisherId, int requestInterval) {
        initialization(false, lad, publisherId, requestInterval);
    }

    public void initialization(boolean landscape, Location lad, String publisherId, int requestInterval) {
        initialization(landscape, true, lad, publisherId, dealutKeywords, requestInterval);
    }

    public void initialization(boolean landscape, boolean openAD, Location lad, String publisherId,
            String keywords) {
        initialization(landscape, openAD, lad, publisherId, keywords, 60);
    }

    public void initialization(boolean landscape, Location lad, String publisherId, String keywords) {
        initialization(landscape, true, lad, publisherId, keywords, 60);
    }

    public void initializationNotKeywords(boolean landscape, Location lad, String publisherId) {
        initialization(landscape, true, lad, publisherId, null, 60);
    }

    public void initialization(IScreen screen, int maxFps) {
        this.initialization();
        this.setScreen(screen);
        this.setFPS(maxFps);
        this.setShowFPS(false);
    }

    public void initialization(IScreen screen) {
        this.initialization(screen, LSystem.DEFAULT_MAX_FPS);
    }

    public void initialization(final boolean landscape, final boolean openAD, final Location lad,
            final String publisherId, final String keywords, final int requestInterval) {
        if (openAD) {
            // setVolumeControlStream(AudioManager.STREAM_MUSIC);
            AdManager.setPublisherId(publisherId);
            AdManager.setTestDevices(new String[] { "" });
            AdManager.setAllowUseOfLocation(true);
            view = new LGameAndroid2DView(LGameAndroid2DActivity.this, landscape);
            IHandler handler = view.getGameHandler();
            RelativeLayout mainLayout = new RelativeLayout(LGameAndroid2DActivity.this);
            RelativeLayout.LayoutParams mainParams = new RelativeLayout.LayoutParams(handler.getWidth(),
                    handler.getHeight());
            if (landscape) {
                mainParams.addRule(RelativeLayout.CENTER_HORIZONTAL, 1);
            } else {
                mainParams.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
            }
            mainLayout.addView(view, mainParams);

            adview = new AdView(LGameAndroid2DActivity.this);
            if (keywords != null) {
                adview.setKeywords(keywords);
            }
            adview.setRequestInterval(requestInterval);
            adview.setGravity(Gravity.NO_GRAVITY);
            RelativeLayout rl = new RelativeLayout(LGameAndroid2DActivity.this);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);
            if (lad == Location.LEFT) {
                lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 1);
            } else if (lad == Location.RIGHT) {
                lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);
            } else if (lad == Location.TOP) {
                lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1);
            } else {
                lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
            }
            // adview.setVisibility(View.GONE);

            visible = true;
            frameLayout.addView(mainLayout);
            rl.addView(adview, lp);
            frameLayout.addView(rl);
        } else {
            view = new LGameAndroid2DView(LGameAndroid2DActivity.this, landscape);
            IHandler handler = view.getGameHandler();
            RelativeLayout mainLayout = new RelativeLayout(LGameAndroid2DActivity.this);
            RelativeLayout.LayoutParams mainParams = new RelativeLayout.LayoutParams(handler.getWidth(),
                    handler.getHeight());
            if (landscape) {
                mainParams.addRule(RelativeLayout.CENTER_HORIZONTAL, 1);
            } else {
                mainParams.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
            }
            mainLayout.addView(view, mainParams);
            frameLayout.addView(mainLayout);
        }
        if (setupSensors) {
            // ??
            this.initSensors();
        }
        if (view != null) {
            gameHandler = view.getGameHandler();
        }
    }

    /**
     * ?Screen
     * 
     */
    public void runFirstScreen() {
        if (gameHandler != null) {
            gameHandler.runFirstScreen();
        }
    }

    /**
     * ??Screen
     */
    public void runLastScreen() {
        if (gameHandler != null) {
            gameHandler.runLastScreen();
        }
    }

    /**
     * ??Screen
     * 
     * @param index
     */
    public void runIndexScreen(int index) {
        if (gameHandler != null) {
            gameHandler.runIndexScreen(index);
        }
    }

    /**
     * ??ScreenScreen
     */
    public void runPreviousScreen() {
        if (gameHandler != null) {
            gameHandler.runPreviousScreen();
        }
    }

    /**
     * ??ScreenScreen
     */
    public void runNextScreen() {
        if (gameHandler != null) {
            gameHandler.runNextScreen();
        }
    }

    /**
     * ?Screen???
     * 
     * @param screen
     */
    public void addScreen(IScreen screen) {
        if (gameHandler != null) {
            gameHandler.addScreen(screen);
        }
    }

    /**
     * ??Screen
     * 
     * @param screen
     */
    public void setScreen(IScreen screen) {
        if (gameHandler != null) {
            this.gameHandler.setScreen(screen);
        }
    }

    /**
     * ?Screen
     * 
     * @return
     */
    public LinkedList<IScreen> getScreens() {
        if (gameHandler != null) {
            return gameHandler.getScreens();
        }
        return null;
    }

    /**
     * Screen
     */
    public int getScreenCount() {
        if (gameHandler != null) {
            return gameHandler.getScreenCount();
        }
        return 0;
    }

    /**
     * ?ID????
     * 
     * @param ad
     * @return
     */
    public int setAD(String ad) {
        int result = 0;
        try {
            Class<LGameAndroid2DActivity> clazz = LGameAndroid2DActivity.class;
            Field[] field = clazz.getDeclaredFields();
            if (field != null) {
                result = field.length;
            }
        } catch (Exception e) {
        }
        return result + ad.length();
    }

    /**
     * ?
     */
    public void maxScreen(int w, int h) {
        LSystem.MAX_SCREEN_WIDTH = w;
        LSystem.MAX_SCREEN_HEIGHT = h;
    }

    /**
     * ?
     */
    public void showScreen() {
        setContentView(frameLayout);
        try {
            getWindow().setBackgroundDrawable(null);
        } catch (Exception e) {

        }
    }

    /**
     * ??
     * 
     * @return
     */
    public Dimension getScreenDimension() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        return new Dimension((int) dm.xdpi, (int) dm.ydpi, (int) dm.widthPixels, (int) dm.heightPixels);

    }

    /**
     * 
     * 
     * @return
     */
    public FrameLayout getFrameLayout() {
        return frameLayout;
    }

    /**
     * ??
     * 
     * @return
     */
    public PackageInfo getPackageInfo() {
        try {
            String packName = getPackageName();
            return getPackageManager().getPackageInfo(packName, 0);
        } catch (Exception ex) {

        }
        return null;
    }

    /**
     * ???
     * 
     * @return
     */
    public String getVersionName() {
        PackageInfo info = getPackageInfo();
        if (info != null) {
            return info.versionName;
        }
        return null;
    }

    /**
     * ???
     * 
     * @return
     */
    public int getVersionCode() {
        PackageInfo info = getPackageInfo();
        if (info != null) {
            return info.versionCode;
        }
        return -1;
    }

    /**
     * ?
     * 
     * @return
     */
    public boolean isVisibleAD() {
        return visible;
    }

    /**
     * ??
     */
    public void hideAD() {
        try {
            if (adview != null) {
                Runnable runnable = new Runnable() {
                    public void run() {
                        adview.setVisibility(View.GONE);
                        visible = false;
                    }
                };
                runOnUiThread(runnable);
            }
        } catch (Exception e) {
        }
    }

    /**
     * 
     */
    public void showAD() {
        try {
            if (adview != null) {
                Runnable runnable = new Runnable() {
                    public void run() {
                        adview.setVisibility(View.VISIBLE);
                        adview.requestFreshAd();
                        visible = true;
                    }
                };
                runOnUiThread(runnable);
            }
        } catch (Exception e) {
        }
    }

    /**
     * ???
     * 
     * @param url
     */
    public void openBrowser(final String url) {
        runOnUiThread(new Runnable() {
            public void run() {
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);
            }
        });
    }

    /**
     * 
     * 
     * @return
     */
    public int getAndroidSelect() {
        return androidSelect;
    }

    /**
     * ??Progress
     * 
     * @param title
     * @param message
     * @return
     */
    public AlertDialog showAndroidProgress(final String title, final String message) {
        if (alert == null || !alert.isShowing()) {
            Runnable showProgress = new Runnable() {
                public void run() {
                    alert = ProgressDialog.show(LGameAndroid2DActivity.this, title, message);
                }
            };
            runOnUiThread(showProgress);
        }
        return alert;
    }

    /**
     * ??Alter
     * 
     * @param message
     */
    public void showAndroidAlert(final String title, final String message) {
        Runnable showAlert = new Runnable() {
            public void run() {
                final AlertDialog alert = new AlertDialog.Builder(LGameAndroid2DActivity.this).create();
                alert.setTitle(title);
                alert.setMessage(message);
                alert.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        alert.dismiss();
                    }
                });
                alert.show();
            }
        };
        runOnUiThread(showAlert);
    }

    /**
     * ??Select
     * 
     * @param title
     * @param text
     * @return
     */
    public int showAndroidSelect(final String title, final String text[]) {
        Runnable showSelect = new Runnable() {
            public void run() {
                final AlertDialog.Builder builder = new AlertDialog.Builder(LGameAndroid2DActivity.this);
                builder.setTitle(title);
                builder.setItems(text, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                        androidSelect = item;
                    }
                });
                builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    public void onCancel(DialogInterface dialog) {
                        androidSelect = -1;
                    }
                });
                AlertDialog alert = builder.create();
                alert.show();
            }
        };
        runOnUiThread(showSelect);
        return androidSelect;
    }

    /**
     * ??Exception
     * 
     * @param ex
     */
    public void showAndroidException(final Exception ex) {
        runOnUiThread(new Runnable() {
            public void run() {
                androidException(ex);
            }
        });
    }

    /**
     * ??Android
     * 
     * @param exception
     */
    private void androidException(Exception exception) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        try {
            throw exception;
        } catch (IOException e) {
            if (e.getMessage().startsWith("Network unreachable")) {
                builder.setTitle("No network");
                builder.setMessage(
                        "LGame-Android Remote needs local network access. Please make sure that your wireless network is activated. You can click on the Settings button below to directly access your network settings.");
                builder.setNeutralButton("Settings", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
                    }
                });
            } else {
                builder.setTitle("Unknown I/O Exception");
                builder.setMessage(e.getMessage().toString());
            }
        } catch (HttpException e) {
            if (e.getMessage().startsWith("401")) {
                builder.setTitle("HTTP 401: Unauthorized");
                builder.setMessage(
                        "The supplied username and/or password is incorrect. Please check your settings.");
                builder.setNeutralButton("Settings", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        startActivity(new Intent());
                    }
                });
            }
        } catch (RuntimeException e) {
            builder.setTitle("RuntimeException");
            builder.setMessage(e.getStackTrace().toString());
        } catch (Exception e) {
            builder.setTitle("Exception");
            builder.setMessage(e.getMessage());
        } finally {
            exception.printStackTrace();
            builder.setCancelable(true);
            builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            final AlertDialog alert = builder.create();
            try {
                alert.show();
            } catch (Throwable e) {
            } finally {
                LSystem.destroy();
            }
        }
    }

    public void onConfigurationChanged(Configuration config) {
        super.onConfigurationChanged(config);
        orientation = config.orientation;
        keyboardOpen = config.keyboardHidden == Configuration.KEYBOARDHIDDEN_NO;
        isLandscape = config.orientation == Configuration.ORIENTATION_LANDSCAPE;
    }

    public void setShowFPS(boolean flag) {
        if (view != null) {
            this.view.setShowFPS(flag);
        }
    }

    public void setFPS(long frames) {
        if (view != null) {
            this.view.setFPS(frames);
        }
    }

    public void setEmulatorListener(EmulatorListener emulator) {
        if (view != null) {
            view.setEmulatorListener(emulator);
        }
    }

    public boolean isShowLogo() {
        if (view != null) {
            return view.isShowLogo();
        }
        return false;
    }

    public void setShowLogo(boolean showLogo) {
        if (view != null) {
            view.setShowLogo(showLogo);
        }
    }

    public void setLogo(LImage img) {
        if (view != null) {
            this.view.setLogo(img);
        }
    }

    public void setDebug(boolean debug) {
        if (view != null) {
            this.view.setDebug(true);
        }
    }

    public boolean isDebug() {
        if (view != null) {
            return this.view.isDebug();
        }
        return false;
    }

    public LGameAndroid2DView view() {
        return view;
    }

    public AdView adView() {
        return adview;
    }

    /**
     * ?
     * 
     * @return
     */
    public boolean isKeyboardOpen() {
        return keyboardOpen;
    }

    /**
     * ??
     * 
     * @return
     */
    public boolean isLandscape() {
        return isLandscape;
    }

    /**
     * ??
     * 
     * @return
     */
    public int getOrientation() {
        return orientation;
    }

    /**
     * ?
     */
    public void close() {
        finish();
    }

    public boolean onTouchEvent(MotionEvent e) {
        if (gameHandler == null) {
            return false;
        }
        gameHandler.onTouchEvent(e);
        return false;
    }

    public boolean onTrackballEvent(MotionEvent e) {
        if (gameHandler != null) {
            synchronized (gameHandler) {
                return gameHandler.onTrackballEvent(e);
            }
        }
        return super.onTrackballEvent(e);
    }

    public boolean isBackLocked() {
        return isBackLocked;
    }

    /**
     * ?BACK??
     * 
     * @param isBackLocked
     */
    public void setBackLocked(boolean isBackLocked) {
        this.isBackLocked = isBackLocked;
    }

    public boolean onKeyDown(int keyCode, KeyEvent e) {
        long curTime = System.currentTimeMillis();
        // ?1/5
        if ((curTime - keyTimeMillis) > LSystem.SECOND / 5) {
            keyTimeMillis = curTime;
            if (gameHandler != null) {
                synchronized (gameHandler) {
                    if (!isBackLocked) {
                        if (keyCode == KeyEvent.KEYCODE_BACK) {
                            LSystem.exit();
                            return true;
                        }
                    }
                    if (keyCode == KeyEvent.KEYCODE_MENU) {
                        return super.onKeyDown(keyCode, e);
                    }
                    if (gameHandler.onKeyDown(keyCode, e)) {
                        return true;
                    }
                    // ??Android???
                    try {
                        Thread.sleep(16);
                    } catch (Exception ex) {
                    }
                    return super.onKeyDown(keyCode, e);
                }
            }
        }
        return true;
    }

    public boolean onKeyUp(int keyCode, KeyEvent e) {
        if (gameHandler != null) {
            synchronized (gameHandler) {
                if (keyCode == KeyEvent.KEYCODE_MENU) {
                    return super.onKeyUp(keyCode, e);
                }
                if (gameHandler.onKeyUp(keyCode, e)) {
                    return true;
                }
                return super.onKeyUp(keyCode, e);
            }
        }
        return true;
    }

    protected void onPause() {
        if (gameHandler != null) {
            gameHandler.onPause();
        }
        if (view != null) {
            view.setPaused(true);
        }
        super.onPause();
        if (setupSensors) {
            // ??
            stopSensors();
        }
    }

    protected void onResume() {
        if (gameHandler != null) {
            gameHandler.onResume();
        }
        if (view != null) {
            view.setPaused(false);
        }
        super.onResume();
        if (setupSensors) {
            // ???
            initSensors();
        }
    }

    protected void onDestroy() {
        try {
            Log.i("Android2DActivity", "LGame 2D Engine Shutdown");
            if (view != null) {
                view.setRunning(false);
                if (adview != null) {
                    adview = null;
                }
                Thread.sleep(16);
            }
            super.onDestroy();
            // ??
            android.os.Process.killProcess(android.os.Process.myPid());
        } catch (Exception e) {

        }
    }

    protected void onStop() {
        try {
            if (view != null) {
                this.view.setPaused(true);
            }
            super.onStop();
        } catch (Exception e) {

        }
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        boolean result = super.onCreateOptionsMenu(menu);
        if (gameHandler != null) {
            if (gameHandler.onCreateOptionsMenu(menu)) {
                return true;
            }
        }
        return result;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        boolean result = super.onOptionsItemSelected(item);
        if (gameHandler != null) {
            if (gameHandler.onOptionsItemSelected(item)) {
                return true;
            }
        }
        return result;
    }

    public void onOptionsMenuClosed(Menu menu) {
        super.onOptionsMenuClosed(menu);
        if (gameHandler != null) {
            gameHandler.onOptionsMenuClosed(menu);
        }
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        if (gameHandler != null) {
            gameHandler.onAccuracyChanged(sensor, accuracy);
        }

    }

    public void onSensorChanged(SensorEvent event) {
        if (gameHandler != null) {
            gameHandler.onSensorChanged(event);
        }

    }

}