From 4567ab96b85fa68cad2cf3e5a374f79e217ed1ea Mon Sep 17 00:00:00 2001 From: folecr Date: Wed, 31 Jul 2013 18:18:01 -0700 Subject: [PATCH] Helper and LocalStorage now use NativeActivity directly --- .../src/org/cocos2dx/lib/Cocos2dxHelper.java | 61 ++++++------------- .../cocos2dx/lib/Cocos2dxLocalStorage.java | 4 +- 2 files changed, 20 insertions(+), 45 deletions(-) diff --git a/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java b/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java index e84f70a904..4738aa91ef 100755 --- a/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java +++ b/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java @@ -51,11 +51,10 @@ public class Cocos2dxHelper { private static Cocos2dxMusic sCocos2dMusic; private static Cocos2dxSound sCocos2dSound; private static AssetManager sAssetManager; - private static Cocos2dxAccelerometer sCocos2dxAccelerometer; private static boolean sAccelerometerEnabled; private static String sPackageName; private static String sFileDirectory; - private static Context sContext = null; + private static Activity sActivity = null; private static Cocos2dxHelperListener sCocos2dxHelperListener; /** @@ -73,7 +72,7 @@ public class Cocos2dxHelper { public static void init(final Activity activity) { final ApplicationInfo applicationInfo = activity.getApplicationInfo(); - Cocos2dxHelper.sContext = activity; + Cocos2dxHelper.sActivity = activity; try { // Get the lib_name from AndroidManifest.xml metadata @@ -95,7 +94,6 @@ public class Cocos2dxHelper { Cocos2dxHelper.sFileDirectory = activity.getFilesDir().getAbsolutePath(); //Cocos2dxHelper.nativeSetApkPath(applicationInfo.sourceDir); - Cocos2dxHelper.sCocos2dxAccelerometer = new Cocos2dxAccelerometer(activity); Cocos2dxHelper.sCocos2dMusic = new Cocos2dxMusic(activity); Cocos2dxHelper.sCocos2dSound = new Cocos2dxSound(activity); Cocos2dxHelper.sAssetManager = activity.getAssets(); @@ -105,6 +103,10 @@ public class Cocos2dxHelper { Cocos2dxETCLoader.setContext(activity); } + public static Activity getActivity() { + return sActivity; + } + // =========================================================== // Getter & Setter // =========================================================== @@ -139,21 +141,6 @@ public class Cocos2dxHelper { return Cocos2dxHelper.sAssetManager; } - public static void enableAccelerometer() { - Cocos2dxHelper.sAccelerometerEnabled = true; - Cocos2dxHelper.sCocos2dxAccelerometer.enable(); - } - - - public static void setAccelerometerInterval(float interval) { - Cocos2dxHelper.sCocos2dxAccelerometer.setInterval(interval); - } - - public static void disableAccelerometer() { - Cocos2dxHelper.sAccelerometerEnabled = false; - Cocos2dxHelper.sCocos2dxAccelerometer.disable(); - } - public static void preloadBackgroundMusic(final String pPath) { Cocos2dxHelper.sCocos2dMusic.preloadBackgroundMusic(pPath); } @@ -239,18 +226,6 @@ public class Cocos2dxHelper { Cocos2dxHelper.sCocos2dSound.end(); } - public static void onResume() { - if (Cocos2dxHelper.sAccelerometerEnabled) { - Cocos2dxHelper.sCocos2dxAccelerometer.enable(); - } - } - - public static void onPause() { - if (Cocos2dxHelper.sAccelerometerEnabled) { - Cocos2dxHelper.sCocos2dxAccelerometer.disable(); - } - } - public static void terminateProcess() { android.os.Process.killProcess(android.os.Process.myPid()); } @@ -280,10 +255,10 @@ public class Cocos2dxHelper { public static int getDPI() { - if (sContext != null) + if (sActivity != null) { DisplayMetrics metrics = new DisplayMetrics(); - WindowManager wm = ((Activity)sContext).getWindowManager(); + WindowManager wm = sActivity.getWindowManager(); if (wm != null) { Display d = wm.getDefaultDisplay(); @@ -302,47 +277,47 @@ public class Cocos2dxHelper { // =========================================================== public static boolean getBoolForKey(String key, boolean defaultValue) { - SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); return settings.getBoolean(key, defaultValue); } public static int getIntegerForKey(String key, int defaultValue) { - SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); return settings.getInt(key, defaultValue); } public static float getFloatForKey(String key, float defaultValue) { - SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); return settings.getFloat(key, defaultValue); } public static double getDoubleForKey(String key, double defaultValue) { // SharedPreferences doesn't support saving double value - SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); return settings.getFloat(key, (float)defaultValue); } public static String getStringForKey(String key, String defaultValue) { - SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); return settings.getString(key, defaultValue); } public static void setBoolForKey(String key, boolean value) { - SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean(key, value); editor.commit(); } public static void setIntegerForKey(String key, int value) { - SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putInt(key, value); editor.commit(); } public static void setFloatForKey(String key, float value) { - SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putFloat(key, value); editor.commit(); @@ -350,14 +325,14 @@ public class Cocos2dxHelper { public static void setDoubleForKey(String key, double value) { // SharedPreferences doesn't support recording double value - SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putFloat(key, (float)value); editor.commit(); } public static void setStringForKey(String key, String value) { - SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putString(key, value); editor.commit(); diff --git a/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxLocalStorage.java b/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxLocalStorage.java index 23556ec917..99c9126400 100644 --- a/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxLocalStorage.java +++ b/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxLocalStorage.java @@ -46,10 +46,10 @@ public class Cocos2dxLocalStorage { * @return */ public static boolean init(String dbName, String tableName) { - if (Cocos2dxActivity.getContext() != null) { + if (Cocos2dxHelper.getActivity() != null) { DATABASE_NAME = dbName; TABLE_NAME = tableName; - mDatabaseOpenHelper = new DBOpenHelper(Cocos2dxActivity.getContext()); + mDatabaseOpenHelper = new DBOpenHelper(Cocos2dxHelper.getActivity()); mDatabase = mDatabaseOpenHelper.getWritableDatabase(); return true; }