Guest User

Untitled

a guest
Jan 7th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.haxe.lime;
  2.  
  3.  
  4. import android.app.Activity;
  5. import android.content.res.AssetFileDescriptor;
  6. import android.content.res.AssetManager;
  7. import android.content.res.Configuration;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.SharedPreferences;
  11. import android.hardware.Sensor;
  12. import android.hardware.SensorEvent;
  13. import android.hardware.SensorEventListener;
  14. import android.hardware.SensorManager;
  15. import android.net.Uri;
  16. import android.os.Build;
  17. import android.os.Bundle;
  18. import android.os.Environment;
  19. import android.os.Handler;
  20. import android.os.Vibrator;
  21. import android.util.DisplayMetrics;
  22. import android.util.Log;
  23. import android.view.inputmethod.InputMethodManager;
  24. import android.view.View;
  25. import android.view.ViewGroup;
  26. import android.view.Window;
  27. import android.view.WindowManager;
  28. import dalvik.system.DexClassLoader;
  29. import java.io.File;
  30. import java.io.FileOutputStream;
  31. import java.io.InputStream;
  32. import java.io.IOException;
  33. import java.io.OutputStream;
  34. import java.lang.reflect.Constructor;
  35. import java.lang.Math;
  36. import java.lang.Runnable;
  37. import java.util.ArrayList;
  38. import java.util.HashMap;
  39. import java.util.Locale;
  40. import java.util.List;
  41. import org.haxe.extension.Extension;
  42. import org.haxe.HXCPP;
  43.  
  44. ////////////////////////////////////////////////////////////////////////
  45. import android.widget.FrameLayout;
  46. import android.widget.RelativeLayout;
  47. import android.widget.RelativeLayout.LayoutParams;
  48. import android.graphics.Color;
  49. import com.google.android.gms.ads.*;
  50. ////////////////////////////////////////////////////////////////////////
  51.  
  52. public class GameActivity extends Activity implements SensorEventListener {
  53.    
  54.    
  55.     private static final int DEVICE_ORIENTATION_UNKNOWN = 0;
  56.     private static final int DEVICE_ORIENTATION_PORTRAIT = 1;
  57.     private static final int DEVICE_ORIENTATION_PORTRAIT_UPSIDE_DOWN = 2;
  58.     private static final int DEVICE_ORIENTATION_LANDSCAPE_RIGHT = 3;
  59.     private static final int DEVICE_ORIENTATION_LANDSCAPE_LEFT = 4;
  60.     private static final int DEVICE_ORIENTATION_FACE_UP = 5;
  61.     private static final int DEVICE_ORIENTATION_FACE_DOWN = 6;
  62.     private static final int DEVICE_ROTATION_0 = 0;
  63.     private static final int DEVICE_ROTATION_90 = 1;
  64.     private static final int DEVICE_ROTATION_180 = 2;
  65.     private static final int DEVICE_ROTATION_270 = 3;
  66.     private static final String GLOBAL_PREF_FILE = "nmeAppPrefs";
  67.    
  68.     private static float[] accelData = new float[3];
  69.     private static GameActivity activity;
  70.     private static AssetManager mAssets;
  71.     private static int bufferedDisplayOrientation = -1;
  72.     private static int bufferedNormalOrientation = -1;
  73.     private static Context mContext;
  74.     private static List<Extension> extensions;
  75.     private static float[] inclinationMatrix = new float[16];
  76.     private static HashMap<String, Class> mLoadedClasses = new HashMap<String, Class>();
  77.     private static float[] magnetData = new float[3];
  78.     private static DisplayMetrics metrics;
  79.     private static float[] orientData = new float[3];
  80.     private static float[] rotationMatrix = new float[16];
  81.     private static SensorManager sensorManager;
  82.    
  83.     public Handler mHandler;
  84.    
  85.     ////////////////////////////////////////////////////////////////////////
  86.     static RelativeLayout adLayout;
  87.     static RelativeLayout.LayoutParams adMobLayoutParams;
  88.     static AdView adView;
  89.     static Boolean adVisible = false, adInitialized = false, adTestMode = false;
  90.     static InterstitialAd interstitial;
  91.     ////////////////////////////////////////////////////////////////////////
  92.    
  93.     private static MainView mMainView;
  94.     private MainView mView;
  95.     private Sound _sound;
  96.    
  97.    
  98.     protected void onCreate (Bundle state) {
  99.        
  100.         super.onCreate (state);
  101.        
  102.         activity = this;
  103.         mContext = this;
  104.         mHandler = new Handler ();
  105.         mAssets = getAssets ();
  106.        
  107.         Extension.assetManager = mAssets;
  108.         Extension.callbackHandler = mHandler;
  109.         Extension.mainActivity = this;
  110.         Extension.mainContext = this;
  111.        
  112.         _sound = new Sound (getApplication ());
  113.         //getResources().getAssets();
  114.        
  115.         requestWindowFeature (Window.FEATURE_NO_TITLE);
  116.        
  117.         ::if WIN_FULLSCREEN::
  118.             getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
  119.                 | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  120.         ::end::
  121.        
  122.        
  123.         metrics = new DisplayMetrics ();
  124.         getWindowManager ().getDefaultDisplay ().getMetrics (metrics);
  125.        
  126.         // Pre-load these, so the C++ knows where to find them
  127.     ::foreach ndlls::
  128.         System.loadLibrary ("::name::");
  129.         ::end::
  130.         HXCPP.run ("ApplicationMain");
  131.  
  132.  
  133.        
  134.        
  135.         //if (mMainView == null) {
  136.            
  137.             Log.d ("lime", "mMainView is NULL");
  138.            
  139.            
  140.             System.loadLibrary ("std");
  141.             System.loadLibrary ("regexp");
  142.             System.loadLibrary ("zlib");
  143.             System.loadLibrary ("openal");
  144.             System.loadLibrary ("lime");
  145.             HXCPP.run ("ApplicationMain");
  146.            
  147.             //mMainView = new MainView (getApplication (), this);
  148.            
  149.         /*} else {
  150.            
  151.             ViewGroup parent = (ViewGroup)mMainView.getParent ();
  152.            
  153.             if (parent != null) {
  154.                
  155.                 parent.removeView (mMainView);
  156.                
  157.             }
  158.            
  159.             mMainView.onResume ();
  160.            
  161.         }
  162.        
  163.         mView = mMainView;*/
  164.         mView = new MainView (getApplication (), this);
  165.         setContentView (mView);
  166.        
  167.                 ////////////////////////////////////////////////////////////////////////
  168.         FrameLayout rootLayout = new FrameLayout(this);
  169.         mView = new MainView(getApplication(), this);
  170.         adLayout = new RelativeLayout(this);
  171.        
  172.         RelativeLayout.LayoutParams adMobLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
  173.        
  174.         rootLayout.addView(mView);
  175.         rootLayout.addView(adLayout, adMobLayoutParams);
  176.        
  177.         setContentView(rootLayout);
  178.         ////////////////////////////////////////////////////////////////////////
  179.        
  180.         sensorManager = (SensorManager)activity.getSystemService (Context.SENSOR_SERVICE);
  181.        
  182.         if (sensorManager != null) {
  183.            
  184.             sensorManager.registerListener (this, sensorManager.getDefaultSensor (Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
  185.             sensorManager.registerListener (this, sensorManager.getDefaultSensor (Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_GAME);
  186.            
  187.         }
  188.         //modif
  189.         //Extension.packageName = getApplicationContext ().getPackageName ();
  190.        
  191.         if (extensions == null) {
  192.            
  193.             extensions = new ArrayList<Extension>();
  194.             ::if (ANDROID_EXTENSIONS != null)::::foreach ANDROID_EXTENSIONS::
  195.             extensions.add (new ::__current__:: ());::end::::end::
  196.            
  197.         }
  198.        
  199.         for (Extension extension : extensions) {
  200.            
  201.             extension.onCreate (state);
  202.            
  203.         }
  204.        
  205.     }
  206.    
  207.     ////////////////////////////////////////////////////////////////////////
  208.     static public void loadAd() {
  209.         AdRequest adRequest = new AdRequest.Builder().build();
  210.         adView.loadAd(adRequest);
  211.     }
  212.    
  213.     static public void initAd(final String id, final int x, final int y, final boolean testMode) {
  214.         activity.runOnUiThread(new Runnable() {
  215.             public void run() {
  216.                 String adID = id;
  217.                 adTestMode = testMode;
  218.                
  219.                 if (activity == null) {
  220.                     return;
  221.                 }
  222.  
  223.                 adView = new AdView(activity);
  224.                 adView.setAdUnitId(adID);
  225.                 adView.setAdSize(AdSize.SMART_BANNER);
  226.  
  227.                 loadAd();
  228.                 adMobLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  229.        
  230.                 if(x == 0) {
  231.                     adMobLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
  232.                 }
  233.                 else if(x == 1) {
  234.                     adMobLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
  235.                 }
  236.                 else if(x == 2) {
  237.                     adMobLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
  238.                 }
  239.                
  240.                 if(y == 0) {
  241.                     adMobLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
  242.                 }
  243.                 else if(y == 1) {
  244.                     adMobLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
  245.                 }
  246.                 else if(y == 2) {
  247.                     adMobLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
  248.                 }
  249.                
  250.                 adInitialized = true;
  251.             }
  252.         });
  253.     }
  254.    
  255.     static public void showAd() {
  256.         activity.runOnUiThread(new Runnable() {
  257.             public void run() {
  258.                 if (adInitialized && !adVisible) {
  259.                     adLayout.removeAllViews();
  260.                     adView.setBackgroundColor(Color.BLACK);
  261.                     adLayout.addView(adView, adMobLayoutParams);
  262.                     adView.setBackgroundColor(0);
  263.                     adVisible = true;
  264.                 }
  265.             }
  266.         });
  267.     }
  268.        
  269.     static public void hideAd() {
  270.         activity.runOnUiThread(new Runnable() {
  271.             public void run() {
  272.                 if (adInitialized && adVisible) {
  273.                     adLayout.removeAllViews();
  274.                     loadAd();
  275.                     adVisible = false;
  276.                 }
  277.             }
  278.         });
  279.     }
  280.    
  281.     static public void loadInterstitial() {
  282.         AdRequest adRequest = new AdRequest.Builder().build();
  283.         interstitial.loadAd(adRequest);
  284.     }
  285.    
  286.     static public void initInterstitial(final String id, final boolean testMode) {
  287.         activity.runOnUiThread(new Runnable() {
  288.             public void run() {
  289.                 if (activity == null) {
  290.                     return;
  291.                 }
  292.                
  293.                 interstitial = new InterstitialAd(activity);
  294.                 interstitial.setAdUnitId(id);
  295.  
  296.                 loadInterstitial();
  297.             }
  298.         });
  299.     }
  300.  
  301.     static public void showInterstitial() {
  302.         activity.runOnUiThread(new Runnable() {
  303.             public void run() {
  304.                 if (interstitial.isLoaded()) {
  305.                     interstitial.show();
  306.                 }
  307.             }
  308.         });
  309.     }
  310.     ///////////////////////////////////////////////////////////////////////////////////////////
  311.    
  312.     public static double CapabilitiesGetPixelAspectRatio () {
  313.        
  314.         return metrics.xdpi / metrics.ydpi;
  315.        
  316.     }
  317.    
  318.    
  319.     public static double CapabilitiesGetScreenDPI () {
  320.        
  321.         return metrics.xdpi;
  322.        
  323.     }
  324.    
  325.    
  326.     public static double CapabilitiesGetScreenResolutionX () {
  327.        
  328.         return metrics.widthPixels;
  329.        
  330.     }
  331.    
  332.    
  333.     public static double CapabilitiesGetScreenResolutionY () {
  334.        
  335.         return metrics.heightPixels;
  336.        
  337.     }
  338.    
  339.    
  340.     public static String CapabilitiesGetLanguage () {
  341.        
  342.         return Locale.getDefault ().getLanguage ();
  343.        
  344.     }
  345.    
  346.    
  347.     public static void clearUserPreference (String inId) {
  348.        
  349.         SharedPreferences prefs = activity.getSharedPreferences (GLOBAL_PREF_FILE, MODE_PRIVATE);
  350.         SharedPreferences.Editor prefEditor = prefs.edit ();
  351.         prefEditor.putString (inId, "");
  352.         prefEditor.commit ();
  353.        
  354.     }
  355.    
  356.    
  357.     public void doPause () {
  358.        
  359.         _sound.doPause ();
  360.        
  361.         mView.sendActivity (Lime.DEACTIVATE);
  362.         mView.onPause ();
  363.        
  364.         if (sensorManager != null) {
  365.            
  366.             sensorManager.unregisterListener (this);
  367.            
  368.         }
  369.        
  370.         ////////////////////////////////////////////////
  371.         if (adView != null) {
  372.             adView.pause();
  373.         }
  374.         ////////////////////////////////////////////////
  375.        
  376.     }
  377.    
  378.    
  379.     public void doResume () {
  380.            
  381.         mView.onResume ();
  382.        
  383.         _sound.doResume ();
  384.        
  385.         mView.sendActivity (Lime.ACTIVATE);
  386.        
  387.         if (sensorManager != null) {
  388.            
  389.             sensorManager.registerListener (this, sensorManager.getDefaultSensor (Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
  390.             sensorManager.registerListener (this, sensorManager.getDefaultSensor (Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_GAME);
  391.            
  392.         }
  393.    
  394.         ////////////////////////////////////////////////
  395.         if (adView != null) {
  396.             adView.resume();
  397.         }
  398.         ////////////////////////////////////////////////
  399.        
  400.     }
  401.    
  402.    
  403.     public static AssetManager getAssetManager () {
  404.        
  405.         return mAssets;
  406.        
  407.     }
  408.    
  409.    
  410.     public static Context getContext () {
  411.        
  412.         return mContext;
  413.        
  414.     }
  415.    
  416.    
  417.     public static GameActivity getInstance () {
  418.        
  419.         return activity;
  420.        
  421.     }
  422.    
  423.    
  424.     public static MainView getMainView () {
  425.        
  426.         return activity.mView;
  427.        
  428.     }
  429.    
  430.    
  431.     public static byte[] getResource (String inResource) {
  432.        
  433.         try {
  434.            
  435.             InputStream inputStream = mAssets.open (inResource, AssetManager.ACCESS_BUFFER);
  436.             long length = inputStream.available ();
  437.             byte[] result = new byte[(int)length];
  438.             inputStream.read (result);
  439.             inputStream.close ();
  440.             return result;
  441.            
  442.         } catch (IOException e) {
  443.            
  444.             Log.e ("GameActivity",  "getResource" + ":" + e.toString ());
  445.            
  446.         }
  447.        
  448.         return null;
  449.        
  450.     }
  451.    
  452.    
  453.     public static int getResourceID (String inFilename) {
  454.    
  455.         //::foreach assets::::if (type == "music")::if (inFilename.equals("::id::")) return ::APP_PACKAGE::.R.raw.::flatName::;
  456.         //::end::::end::
  457.         //::foreach assets::::if (type == "sound")::if (inFilename.equals("::id::")) return ::APP_PACKAGE::.R.raw.::flatName::;
  458.         //::end::::end::
  459.         return -1;
  460.        
  461.     }
  462.    
  463.    
  464.     static public String getSpecialDir (int inWhich) {
  465.        
  466.         Log.v ("GameActivity", "Get special Dir " + inWhich);
  467.         File path = null;
  468.        
  469.         switch (inWhich) {
  470.            
  471.             case 0: // App
  472.                 return mContext.getPackageCodePath ();
  473.            
  474.             case 1: // Storage
  475.                 path = mContext.getFilesDir ();
  476.                 break;
  477.            
  478.             case 2: // Desktop
  479.                 path = Environment.getDataDirectory ();
  480.                 break;
  481.            
  482.             case 3: // Docs
  483.                 path = Environment.getExternalStorageDirectory ();
  484.                 break;
  485.            
  486.             case 4: // User
  487.                 path = mContext.getExternalFilesDir (Environment.DIRECTORY_DOWNLOADS);
  488.                 break;
  489.            
  490.         }
  491.        
  492.         return path == null ? "" : path.getAbsolutePath ();
  493.        
  494.     }
  495.    
  496.    
  497.     public static String getUserPreference (String inId) {
  498.        
  499.         SharedPreferences prefs = activity.getSharedPreferences (GLOBAL_PREF_FILE, MODE_PRIVATE);
  500.         return prefs.getString (inId, "");
  501.        
  502.     }
  503.    
  504.    
  505.     public static void launchBrowser (String inURL) {
  506.        
  507.         Intent browserIntent = new Intent (Intent.ACTION_VIEW).setData (Uri.parse (inURL));
  508.        
  509.         try {
  510.            
  511.             activity.startActivity (browserIntent);
  512.            
  513.         } catch (Exception e) {
  514.            
  515.             Log.e ("GameActivity", e.toString ());
  516.             return;
  517.            
  518.         }
  519.        
  520.     }
  521.    
  522.    
  523.     private void loadNewSensorData (SensorEvent event) {
  524.        
  525.         final int type = event.sensor.getType ();
  526.        
  527.         if (type == Sensor.TYPE_ACCELEROMETER) {
  528.            
  529.             accelData = event.values.clone ();
  530.             Lime.onAccelerate (-accelData[0], -accelData[1], accelData[2]);
  531.            
  532.         }
  533.        
  534.         if (type == Sensor.TYPE_MAGNETIC_FIELD) {
  535.            
  536.             magnetData = event.values.clone ();
  537.             //Log.d("GameActivity","new mag: " + magnetData[0] + ", " + magnetData[1] + ", " + magnetData[2]);
  538.            
  539.         }
  540.        
  541.     }
  542.    
  543.    
  544.     @Override public void onAccuracyChanged (Sensor sensor, int accuracy) {
  545.        
  546.        
  547.        
  548.     }
  549.    
  550.    
  551.     @Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {
  552.        
  553.         for (Extension extension : extensions) {
  554.            
  555.             if (!extension.onActivityResult (requestCode, resultCode, data)) {
  556.                
  557.                 return;
  558.                
  559.             }
  560.            
  561.         }
  562.        
  563.         super.onActivityResult (requestCode, resultCode, data);
  564.        
  565.     }
  566.    
  567.    
  568.     @Override protected void onDestroy () {
  569.        
  570.         for (Extension extension : extensions) {
  571.            
  572.             extension.onDestroy ();
  573.            
  574.         }
  575.        
  576.         // TODO: Wait for result?
  577.         mView.sendActivity (Lime.DESTROY);
  578.         activity = null;
  579.         super.onDestroy ();
  580.        
  581.     }
  582.    
  583.    
  584.     @Override protected void onPause () {
  585.        
  586.         doPause ();
  587.         super.onPause ();
  588.        
  589.         for (Extension extension : extensions) {
  590.            
  591.             extension.onPause ();
  592.            
  593.         }
  594.        
  595.     }
  596.    
  597.    
  598.     @Override protected void onRestart () {
  599.        
  600.         super.onRestart ();
  601.        
  602.         for (Extension extension : extensions) {
  603.            
  604.             extension.onRestart ();
  605.            
  606.         }
  607.        
  608.     }
  609.    
  610.    
  611.     @Override protected void onResume () {
  612.        
  613.         super.onResume();
  614.         doResume();
  615.        
  616.         for (Extension extension : extensions) {
  617.            
  618.             extension.onResume ();
  619.            
  620.         }
  621.        
  622.     }
  623.    
  624.    
  625.     @Override public void onSensorChanged (SensorEvent event) {
  626.        
  627.         loadNewSensorData (event);
  628.        
  629.         if (accelData != null && magnetData != null) {
  630.            
  631.             boolean foundRotationMatrix = SensorManager.getRotationMatrix (rotationMatrix, inclinationMatrix, accelData, magnetData);
  632.            
  633.             if (foundRotationMatrix) {
  634.                
  635.                 SensorManager.getOrientation (rotationMatrix, orientData);
  636.                 Lime.onOrientationUpdate (orientData[0], orientData[1], orientData[2]);
  637.                
  638.             }
  639.            
  640.         }
  641.        
  642.         Lime.onDeviceOrientationUpdate (prepareDeviceOrientation ());
  643.         Lime.onNormalOrientationFound (bufferedNormalOrientation);
  644.        
  645.     }
  646.    
  647.    
  648.     @Override protected void onStart () {
  649.        
  650.         super.onStart();
  651.        
  652.         ::if WIN_FULLSCREEN::::if (ANDROID_TARGET_SDK_VERSION >= 16)::
  653.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  654.            
  655.             getWindow().getDecorView().setSystemUiVisibility (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LOW_PROFILE | View.SYSTEM_UI_FLAG_FULLSCREEN);
  656.            
  657.         }
  658.         ::end::::end::
  659.        
  660.         for (Extension extension : extensions) {
  661.             extension.onStart ();
  662.            
  663.         }
  664.        
  665.     }
  666.    
  667.    
  668.     @Override protected void onStop () {
  669.        
  670.         super.onStop ();
  671.        
  672.         for (Extension extension : extensions) {
  673.            
  674.             extension.onStop ();
  675.            
  676.         }
  677.        
  678.     }
  679.    
  680.    
  681.     public static void popView () {
  682.        
  683.         activity.setContentView (activity.mView);
  684.         activity.doResume ();
  685.        
  686.     }
  687.    
  688.    
  689.     public static void postUICallback (final long inHandle) {
  690.        
  691.         activity.mHandler.post (new Runnable () {
  692.            
  693.             @Override public void run () {
  694.                
  695.                 Lime.onCallback (inHandle);
  696.                
  697.             }
  698.            
  699.         });
  700.        
  701.     }
  702.    
  703.    
  704.     private int prepareDeviceOrientation () {
  705.        
  706.         int rawOrientation = getWindow ().getWindowManager ().getDefaultDisplay ().getOrientation ();
  707.        
  708.         if (rawOrientation != bufferedDisplayOrientation) {
  709.            
  710.             bufferedDisplayOrientation = rawOrientation;
  711.            
  712.         }
  713.        
  714.         int screenOrientation = getResources ().getConfiguration ().orientation;
  715.         int deviceOrientation = DEVICE_ORIENTATION_UNKNOWN;
  716.        
  717.         if (bufferedNormalOrientation < 0) {
  718.            
  719.             switch (screenOrientation) {
  720.                
  721.                 case Configuration.ORIENTATION_LANDSCAPE:
  722.                    
  723.                     switch (bufferedDisplayOrientation) {
  724.                        
  725.                         case DEVICE_ROTATION_0:
  726.                         case DEVICE_ROTATION_180:
  727.                             bufferedNormalOrientation = DEVICE_ORIENTATION_LANDSCAPE_LEFT;
  728.                             break;
  729.                        
  730.                         case DEVICE_ROTATION_90:
  731.                         case DEVICE_ROTATION_270:
  732.                             bufferedNormalOrientation = DEVICE_ORIENTATION_PORTRAIT;
  733.                             break;
  734.                        
  735.                         default:
  736.                             bufferedNormalOrientation = DEVICE_ORIENTATION_UNKNOWN;
  737.                        
  738.                     }
  739.                    
  740.                     break;
  741.                
  742.                 case Configuration.ORIENTATION_PORTRAIT:
  743.                    
  744.                     switch (bufferedDisplayOrientation) {
  745.                        
  746.                         case DEVICE_ROTATION_0:
  747.                         case DEVICE_ROTATION_180:
  748.                             bufferedNormalOrientation = DEVICE_ORIENTATION_PORTRAIT;
  749.                             break;
  750.                        
  751.                         case DEVICE_ROTATION_90:
  752.                         case DEVICE_ROTATION_270:
  753.                             bufferedNormalOrientation = DEVICE_ORIENTATION_LANDSCAPE_LEFT;
  754.                             break;
  755.                        
  756.                         default:
  757.                             bufferedNormalOrientation = DEVICE_ORIENTATION_UNKNOWN;
  758.                        
  759.                     }
  760.                    
  761.                     break;
  762.                
  763.                 default: // ORIENTATION_SQUARE OR ORIENTATION_UNDEFINED
  764.                     bufferedNormalOrientation = DEVICE_ORIENTATION_UNKNOWN;
  765.                
  766.             }
  767.            
  768.         }
  769.        
  770.         switch (screenOrientation) {
  771.            
  772.             case Configuration.ORIENTATION_LANDSCAPE:
  773.                
  774.                 switch (bufferedDisplayOrientation) {
  775.                    
  776.                     case DEVICE_ROTATION_0:
  777.                     case DEVICE_ROTATION_270:
  778.                         deviceOrientation = DEVICE_ORIENTATION_LANDSCAPE_LEFT;
  779.                         break;
  780.                    
  781.                     case DEVICE_ROTATION_90:
  782.                     case DEVICE_ROTATION_180:
  783.                         deviceOrientation = DEVICE_ORIENTATION_LANDSCAPE_RIGHT;
  784.                         break;
  785.                    
  786.                     default: // impossible!
  787.                         deviceOrientation = DEVICE_ORIENTATION_UNKNOWN;
  788.                    
  789.                 }
  790.                
  791.                 break;
  792.            
  793.             case Configuration.ORIENTATION_PORTRAIT:
  794.                
  795.                 switch (bufferedDisplayOrientation) {
  796.                    
  797.                     case DEVICE_ROTATION_0:
  798.                     case DEVICE_ROTATION_90:
  799.                         deviceOrientation = DEVICE_ORIENTATION_PORTRAIT;
  800.                         break;
  801.                    
  802.                     case DEVICE_ROTATION_180:
  803.                     case DEVICE_ROTATION_270:
  804.                         deviceOrientation = DEVICE_ORIENTATION_PORTRAIT_UPSIDE_DOWN;
  805.                         break;
  806.                    
  807.                     default: // impossible!
  808.                         deviceOrientation = DEVICE_ORIENTATION_UNKNOWN;
  809.                 }
  810.                
  811.                 break;
  812.            
  813.             default: // ORIENTATION_SQUARE OR ORIENTATION_UNDEFINED
  814.                 deviceOrientation = DEVICE_ORIENTATION_UNKNOWN;
  815.            
  816.         }
  817.        
  818.         return deviceOrientation;
  819.        
  820.     }
  821.    
  822.    
  823.     public static void pushView (View inView) {
  824.        
  825.         activity.doPause ();
  826.         activity.setContentView (inView);
  827.        
  828.     }
  829.    
  830.    
  831.     public void queueRunnable (Runnable runnable) {
  832.        
  833.         Log.e ("GameActivity", "queueing...");
  834.        
  835.     }
  836.    
  837.    
  838.     public static void registerExtension (Extension extension) {
  839.        
  840.         if (extensions.indexOf (extension) == -1) {
  841.            
  842.             extensions.add (extension);
  843.            
  844.         }
  845.        
  846.     }
  847.    
  848.    
  849.     public static void showKeyboard (boolean show) {
  850.        
  851.         if (activity == null) {
  852.            
  853.             return;
  854.            
  855.         }
  856.        
  857.         InputMethodManager mgr = (InputMethodManager)activity.getSystemService (Context.INPUT_METHOD_SERVICE);
  858.         mgr.hideSoftInputFromWindow (activity.mView.getWindowToken (), 0);
  859.        
  860.         if (show) {
  861.            
  862.             mgr.toggleSoftInput (InputMethodManager.SHOW_FORCED, 0);
  863.             // On the Nexus One, SHOW_FORCED makes it impossible
  864.             // to manually dismiss the keyboard.
  865.             // On the Droid SHOW_IMPLICIT doesn't bring up the keyboard.
  866.            
  867.         }
  868.        
  869.     }
  870.    
  871.    
  872.     public static void setUserPreference (String inId, String inPreference) {
  873.        
  874.         SharedPreferences prefs = activity.getSharedPreferences (GLOBAL_PREF_FILE, MODE_PRIVATE);
  875.         SharedPreferences.Editor prefEditor = prefs.edit ();
  876.         prefEditor.putString (inId, inPreference);
  877.         prefEditor.commit ();
  878.        
  879.     }
  880.    
  881.    
  882.     public static void vibrate (int period, int duration) {
  883.        
  884.         Vibrator v = (Vibrator)activity.getSystemService (Context.VIBRATOR_SERVICE);
  885.        
  886.         if (period == 0) {
  887.            
  888.             v.vibrate (duration);
  889.            
  890.         } else {
  891.                
  892.             int periodMS = (int)Math.ceil (period / 2);
  893.             int count = (int)Math.ceil ((duration / period) * 2);
  894.             long[] pattern = new long[count];
  895.            
  896.             for (int i = 0; i < count; i++) {
  897.                
  898.                 pattern[i] = periodMS;
  899.                
  900.             }
  901.            
  902.             v.vibrate (pattern, -1);
  903.            
  904.         }
  905.        
  906.     }
  907.    
  908.    
  909. }
Advertisement
Add Comment
Please, Sign In to add comment