Advertisement
VandroiyX

Cocos2dxHelper.java APK Expansion

Sep 11th, 2013
2,649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.75 KB | None | 0 0
  1. /****************************************************************************
  2. Copyright (c) 2010-2013 cocos2d-x.org
  3.  
  4. http://www.cocos2d-x.org
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23.  ****************************************************************************/
  24. package org.cocos2dx.lib;
  25.  
  26. import java.io.UnsupportedEncodingException;
  27. import java.util.Locale;
  28.  
  29. import android.app.Activity;
  30. import android.content.Context;
  31. import android.content.SharedPreferences;
  32. import android.content.pm.ApplicationInfo;
  33. import android.content.res.AssetManager;
  34. import android.os.Build;
  35. import android.os.Environment;
  36. import android.util.DisplayMetrics;
  37. import android.util.Log;
  38. import android.view.Display;
  39. import android.view.WindowManager;
  40.  
  41. public class Cocos2dxHelper {
  42.     // ===========================================================
  43.     // Constants
  44.     // ===========================================================
  45.     private static final String PREFS_NAME = "Cocos2dxPrefsFile";
  46.  
  47.     // ===========================================================
  48.     // Fields
  49.     // ===========================================================
  50.  
  51.     private static Cocos2dxMusic sCocos2dMusic;
  52.     private static Cocos2dxSound sCocos2dSound;
  53.     private static AssetManager sAssetManager;
  54.     private static Cocos2dxAccelerometer sCocos2dxAccelerometer;
  55.     private static boolean sAccelerometerEnabled;
  56.     private static String sPackageName;
  57.     private static String sFileDirectory;
  58.     private static Context sContext = null;
  59.     private static Cocos2dxHelperListener sCocos2dxHelperListener;
  60.  
  61.     // ===========================================================
  62.     // Constructors
  63.     // ===========================================================
  64.  
  65.     public static void init(final Context pContext, final Cocos2dxHelperListener pCocos2dxHelperListener) {
  66.         final ApplicationInfo applicationInfo = pContext.getApplicationInfo();
  67.        
  68.         Cocos2dxHelper.sContext = pContext;
  69.         Cocos2dxHelper.sCocos2dxHelperListener = pCocos2dxHelperListener;
  70.  
  71.         Cocos2dxHelper.sPackageName = applicationInfo.packageName;
  72.         Cocos2dxHelper.sFileDirectory = pContext.getFilesDir().getAbsolutePath();
  73.         //Cocos2dxHelper.nativeSetApkPath(applicationInfo.sourceDir);
  74.        
  75.     //THIS LINE IS ESSENTIAL IN ORDER TO READ FROM THE OBB FILE
  76.         Cocos2dxHelper.nativeSetApkPath(Environment.getExternalStorageDirectory() + "/Android/obb/com.example.package/main.2.com.example.package.obb");
  77.        
  78.  
  79.         Cocos2dxHelper.sCocos2dxAccelerometer = new Cocos2dxAccelerometer(pContext);
  80.         Cocos2dxHelper.sCocos2dMusic = new Cocos2dxMusic(pContext);
  81.         Cocos2dxHelper.sCocos2dSound = new Cocos2dxSound(pContext);
  82.         Cocos2dxHelper.sAssetManager = pContext.getAssets();
  83.         Cocos2dxBitmap.setContext(pContext);
  84.         Cocos2dxETCLoader.setContext(pContext);
  85.     }
  86.  
  87.     // ===========================================================
  88.     // Getter & Setter
  89.     // ===========================================================
  90.  
  91.     // ===========================================================
  92.     // Methods for/from SuperClass/Interfaces
  93.     // ===========================================================
  94.  
  95.     // ===========================================================
  96.     // Methods
  97.     // ===========================================================
  98.  
  99.     private static native void nativeSetApkPath(final String pApkPath);
  100.  
  101.     private static native void nativeSetEditTextDialogResult(final byte[] pBytes);
  102.  
  103.     public static String getCocos2dxPackageName() {
  104.         return Cocos2dxHelper.sPackageName;
  105.     }
  106.  
  107.     public static String getCocos2dxWritablePath() {
  108.         return Cocos2dxHelper.sFileDirectory;
  109.     }
  110.  
  111.     public static String getCurrentLanguage() {
  112.         return Locale.getDefault().getLanguage();
  113.     }
  114.    
  115.     public static String getDeviceModel(){
  116.         return Build.MODEL;
  117.     }
  118.  
  119.     public static AssetManager getAssetManager() {
  120.         return Cocos2dxHelper.sAssetManager;
  121.     }
  122.  
  123.     public static void enableAccelerometer() {
  124.         Cocos2dxHelper.sAccelerometerEnabled = true;
  125.         Cocos2dxHelper.sCocos2dxAccelerometer.enable();
  126.     }
  127.  
  128.  
  129.     public static void setAccelerometerInterval(float interval) {
  130.         Cocos2dxHelper.sCocos2dxAccelerometer.setInterval(interval);
  131.     }
  132.  
  133.     public static void disableAccelerometer() {
  134.         Cocos2dxHelper.sAccelerometerEnabled = false;
  135.         Cocos2dxHelper.sCocos2dxAccelerometer.disable();
  136.     }
  137.  
  138.     public static void preloadBackgroundMusic(final String pPath) {
  139.         Cocos2dxHelper.sCocos2dMusic.preloadBackgroundMusic(pPath);
  140.     }
  141.  
  142.     public static void playBackgroundMusic(final String pPath, final boolean isLoop) {
  143.         Cocos2dxHelper.sCocos2dMusic.playBackgroundMusic(pPath, isLoop);
  144.     }
  145.  
  146.     public static void resumeBackgroundMusic() {
  147.         Cocos2dxHelper.sCocos2dMusic.resumeBackgroundMusic();
  148.     }
  149.  
  150.     public static void pauseBackgroundMusic() {
  151.         Cocos2dxHelper.sCocos2dMusic.pauseBackgroundMusic();
  152.     }
  153.  
  154.     public static void stopBackgroundMusic() {
  155.         Cocos2dxHelper.sCocos2dMusic.stopBackgroundMusic();
  156.     }
  157.  
  158.     public static void rewindBackgroundMusic() {
  159.         Cocos2dxHelper.sCocos2dMusic.rewindBackgroundMusic();
  160.     }
  161.  
  162.     public static boolean isBackgroundMusicPlaying() {
  163.         return Cocos2dxHelper.sCocos2dMusic.isBackgroundMusicPlaying();
  164.     }
  165.  
  166.     public static float getBackgroundMusicVolume() {
  167.         return Cocos2dxHelper.sCocos2dMusic.getBackgroundVolume();
  168.     }
  169.  
  170.     public static void setBackgroundMusicVolume(final float volume) {
  171.         Cocos2dxHelper.sCocos2dMusic.setBackgroundVolume(volume);
  172.     }
  173.  
  174.     public static void preloadEffect(final String path) {
  175.         Cocos2dxHelper.sCocos2dSound.preloadEffect(path);
  176.     }
  177.  
  178.     public static int playEffect(final String path, final boolean isLoop) {
  179.         return Cocos2dxHelper.sCocos2dSound.playEffect(path, isLoop);
  180.     }
  181.  
  182.     public static void resumeEffect(final int soundId) {
  183.         Cocos2dxHelper.sCocos2dSound.resumeEffect(soundId);
  184.     }
  185.  
  186.     public static void pauseEffect(final int soundId) {
  187.         Cocos2dxHelper.sCocos2dSound.pauseEffect(soundId);
  188.     }
  189.  
  190.     public static void stopEffect(final int soundId) {
  191.         Cocos2dxHelper.sCocos2dSound.stopEffect(soundId);
  192.     }
  193.  
  194.     public static float getEffectsVolume() {
  195.         return Cocos2dxHelper.sCocos2dSound.getEffectsVolume();
  196.     }
  197.  
  198.     public static void setEffectsVolume(final float volume) {
  199.         Cocos2dxHelper.sCocos2dSound.setEffectsVolume(volume);
  200.     }
  201.  
  202.     public static void unloadEffect(final String path) {
  203.         Cocos2dxHelper.sCocos2dSound.unloadEffect(path);
  204.     }
  205.  
  206.     public static void pauseAllEffects() {
  207.         Cocos2dxHelper.sCocos2dSound.pauseAllEffects();
  208.     }
  209.  
  210.     public static void resumeAllEffects() {
  211.         Cocos2dxHelper.sCocos2dSound.resumeAllEffects();
  212.     }
  213.  
  214.     public static void stopAllEffects() {
  215.         Cocos2dxHelper.sCocos2dSound.stopAllEffects();
  216.     }
  217.  
  218.     public static void end() {
  219.         Cocos2dxHelper.sCocos2dMusic.end();
  220.         Cocos2dxHelper.sCocos2dSound.end();
  221.     }
  222.  
  223.     public static void onResume() {
  224.         if (Cocos2dxHelper.sAccelerometerEnabled) {
  225.             Cocos2dxHelper.sCocos2dxAccelerometer.enable();
  226.         }
  227.     }
  228.  
  229.     public static void onPause() {
  230.         if (Cocos2dxHelper.sAccelerometerEnabled) {
  231.             Cocos2dxHelper.sCocos2dxAccelerometer.disable();
  232.         }
  233.     }
  234.  
  235.     public static void terminateProcess() {
  236.         android.os.Process.killProcess(android.os.Process.myPid());
  237.     }
  238.  
  239.     private static void showDialog(final String pTitle, final String pMessage) {
  240.         Cocos2dxHelper.sCocos2dxHelperListener.showDialog(pTitle, pMessage);
  241.     }
  242.  
  243.     private static void showEditTextDialog(final String pTitle, final String pMessage, final int pInputMode, final int pInputFlag, final int pReturnType, final int pMaxLength) {
  244.         Cocos2dxHelper.sCocos2dxHelperListener.showEditTextDialog(pTitle, pMessage, pInputMode, pInputFlag, pReturnType, pMaxLength);
  245.     }
  246.  
  247.     public static void setEditTextDialogResult(final String pResult) {
  248.         try {
  249.             final byte[] bytesUTF8 = pResult.getBytes("UTF8");
  250.  
  251.             Cocos2dxHelper.sCocos2dxHelperListener.runOnGLThread(new Runnable() {
  252.                 @Override
  253.                 public void run() {
  254.                     Cocos2dxHelper.nativeSetEditTextDialogResult(bytesUTF8);
  255.                 }
  256.             });
  257.         } catch (UnsupportedEncodingException pUnsupportedEncodingException) {
  258.             /* Nothing. */
  259.         }
  260.     }
  261.  
  262.     public static int getDPI()
  263.     {
  264.         if (sContext != null)
  265.         {
  266.             DisplayMetrics metrics = new DisplayMetrics();
  267.             WindowManager wm = ((Activity)sContext).getWindowManager();
  268.             if (wm != null)
  269.             {
  270.                 Display d = wm.getDefaultDisplay();
  271.                 if (d != null)
  272.                 {
  273.                     d.getMetrics(metrics);
  274.                     return (int)(metrics.density*160.0f);
  275.                 }
  276.             }
  277.         }
  278.         return -1;
  279.     }
  280.    
  281.     // ===========================================================
  282.     // Functions for CCUserDefault
  283.     // ===========================================================
  284.    
  285.     public static boolean getBoolForKey(String key, boolean defaultValue) {
  286.         SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
  287.         return settings.getBoolean(key, defaultValue);
  288.     }
  289.    
  290.     public static int getIntegerForKey(String key, int defaultValue) {
  291.         SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
  292.         return settings.getInt(key, defaultValue);
  293.     }
  294.    
  295.     public static float getFloatForKey(String key, float defaultValue) {
  296.         SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
  297.         return settings.getFloat(key, defaultValue);
  298.     }
  299.    
  300.     public static double getDoubleForKey(String key, double defaultValue) {
  301.         // SharedPreferences doesn't support saving double value
  302.         SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
  303.         return settings.getFloat(key, (float)defaultValue);
  304.     }
  305.    
  306.     public static String getStringForKey(String key, String defaultValue) {
  307.         SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
  308.         return settings.getString(key, defaultValue);
  309.     }
  310.    
  311.     public static void setBoolForKey(String key, boolean value) {
  312.         SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
  313.         SharedPreferences.Editor editor = settings.edit();
  314.         editor.putBoolean(key, value);
  315.         editor.commit();
  316.     }
  317.    
  318.     public static void setIntegerForKey(String key, int value) {
  319.         SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
  320.         SharedPreferences.Editor editor = settings.edit();
  321.         editor.putInt(key, value);
  322.         editor.commit();
  323.     }
  324.    
  325.     public static void setFloatForKey(String key, float value) {
  326.         SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
  327.         SharedPreferences.Editor editor = settings.edit();
  328.         editor.putFloat(key, value);
  329.         editor.commit();
  330.     }
  331.    
  332.     public static void setDoubleForKey(String key, double value) {
  333.         // SharedPreferences doesn't support recording double value
  334.         SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
  335.         SharedPreferences.Editor editor = settings.edit();
  336.         editor.putFloat(key, (float)value);
  337.         editor.commit();
  338.     }
  339.    
  340.     public static void setStringForKey(String key, String value) {
  341.         SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
  342.         SharedPreferences.Editor editor = settings.edit();
  343.         editor.putString(key, value);
  344.         editor.commit();
  345.     }
  346.    
  347.     // ===========================================================
  348.     // Inner and Anonymous Classes
  349.     // ===========================================================
  350.  
  351.     public static interface Cocos2dxHelperListener {
  352.         public void showDialog(final String pTitle, final String pMessage);
  353.         public void showEditTextDialog(final String pTitle, final String pMessage, final int pInputMode, final int pInputFlag, final int pReturnType, final int pMaxLength);
  354.  
  355.         public void runOnGLThread(final Runnable pRunnable);
  356.     }
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement