readme: add information section
[nit.git] / lib / android / assets_and_resources.nit
index b759c30..8bbc7da 100644 (file)
@@ -120,14 +120,8 @@ end
 # Assets manager using a `NativeAssetManager` to manage android assets
 class AssetManager
 
-       # App instance used to initalize the NativeAssetManager
-       var app: App
-
        # Native asset manager
-       private var native_assets_manager: NativeAssetManager is noinit
-
-       # Instanciate this AssetManager with the context of the app
-       init do self.native_assets_manager = app.assets.new_global_ref
+       private var native_assets_manager: NativeAssetManager = app.native_activity.assets.new_global_ref is lazy
 
        # Close this asset manager
        fun close do native_assets_manager.close
@@ -179,10 +173,9 @@ class AssetManager
 
        # Return a bitmap from the assets
        private fun bitmap(name: String): NativeBitmap do
-               sys.jni_env.push_local_frame(1)
-               var return_value = new NativeBitmap.from_stream(native_assets_manager.open(name.to_java_string)).new_global_ref
-               sys.jni_env.pop_local_frame
-               return return_value
+               sys.jni_env.push_local_frame 2
+               var return_value = new NativeBitmap.from_stream(native_assets_manager.open(name.to_java_string))
+               return return_value.pop_from_local_frame
        end
 
        # Deallocate the global reference allocated by AssetManager
@@ -225,8 +218,7 @@ class ResourcesManager
 
        private init native(res: NativeResources, app_package: String)
        do
-               self.android_resources = res.new_global_ref
-               self.app_package = app_package
+               init(res.new_global_ref, app_package)
        end
 
        # Get a color from resources
@@ -311,9 +303,31 @@ private extern class NativeBitmap in "Java" `{ android.graphics.Bitmap `}
        # Create a NativeBitmap using a resource ID and the NativeResources
        # Called by the ResourceManager
        new from_resources(res: NativeResources, id: Int) in "Java" `{ return BitmapFactory.decodeResource(res, (int)id); `}
+
+       # Width in pixels
+       #
+       # Wraps Java: `int android.graphics.Bitmap.getWidth()`
        fun width: Int in "Java" `{ return self.getWidth(); `}
+
+       # Height in pixels
+       #
+       # Wraps Java: `int android.graphics.Bitmap.getHeight()`
        fun height: Int in "Java" `{ return self.getHeight(); `}
 
+       # Number of bytes per row
+       #
+       # Wraps Java: `int android.graphics.Bitmap.getRowBytes()`
+       fun row_bytes: Int in "Java" `{
+               return self.getRowBytes();
+       `}
+
+       # Does this bitmap has an alpha channel?
+       #
+       # Wraps Java: `boolean android.graphics.Bitmap.hasAlpha()`
+       fun has_alpha: Bool in "Java" `{
+               return self.hasAlpha();
+       `}
+
        # HACK for bug #845
        redef fun new_global_ref import sys, Sys.jni_env `{
                Sys sys = NativeBitmap_sys(self);
@@ -321,6 +335,9 @@ private extern class NativeBitmap in "Java" `{ android.graphics.Bitmap `}
                return (*env)->NewGlobalRef(env, self);
        `}
 
+       redef fun pop_from_local_frame_with_env(jni_env) `{
+               return (*jni_env)->PopLocalFrame(jni_env, self);
+       `}
 end
 
 # Android AssetFileDescriptor, can be retrieve by AssetManager and used to load a sound in a SoundPool
@@ -383,17 +400,30 @@ end
 
 redef class App
        # Resource Manager used to manage resources placed in the `res` folder of the app
-       var resource_manager: ResourcesManager is lazy  do return new ResourcesManager.native(self.resources, self.package_name.to_s)
+       var resource_manager: ResourcesManager is lazy do
+               var res = native_activity.resources
+               var pkg = native_activity.package_name
+               return new ResourcesManager.native(res, pkg.to_s)
+       end
 
        # Assets Manager used to manage resources placed in the `assets` folder of the app
-       var asset_manager: AssetManager is lazy do return new AssetManager(self)
+       var asset_manager: AssetManager is lazy do return new AssetManager
+end
+
+redef extern class NativeActivity
 
        # Get the native AssetsManager of the application, used to initialize the nit's AssetManager
-       private fun assets: NativeAssetManager import native_activity in "Java" `{ return App_native_activity(self).getAssets(); `}
+       private fun assets: NativeAssetManager in "Java" `{
+               return self.getAssets();
+       `}
 
        # Get the package name of the application
-       private fun package_name: JavaString import native_activity in "Java" `{ return App_native_activity(self).getPackageName(); `}
+       private fun package_name: JavaString in "Java" `{
+               return self.getPackageName();
+       `}
 
        # Get the native ResourceManager of the application, used to initialize the nit's ResourceManager
-       private fun resources: NativeResources import native_activity in "Java" `{ return App_native_activity(self).getResources(); `}
+       private fun resources: NativeResources in "Java" `{
+               return self.getResources();
+       `}
 end