1 # this file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Romain Chanoir <romain.chanoir@viacesi.fr>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Android Assets and Resources Management
19 # Use the ResourceManager to retrieve resources from the `res` folder of your app
20 # Use the AssetManager to retrieve resources files from the `assets` folder of your app
21 # both are available from `App`
22 # If you write your own resources in your NIT project part of the application,
23 # you are obliged to set a string resource with the name "app_name" or it will be
24 # impossible for you to compile the apk correctly
25 module assets_and_resources
32 import android.content.res.AssetManager;
33 import android.content.res.AssetFileDescriptor;
34 import android.content.res.Resources;
35 import android.content.res.XmlResourceParser;
36 import java.io.IOException;
37 import android.graphics.Bitmap;
38 import android.graphics.BitmapFactory;
39 import android.graphics.drawable.Drawable;
40 import java.io.InputStream;
41 import android.util.Log;
42 import java.io.FileDescriptor;
45 # AssetManager from Java, used by `AssetManager` to access resources in `assets` app's directory
46 # This is a low-level class, use `AssetManager` instead
47 private extern class NativeAssetManager in "Java" `{ android.content.res.AssetManager `}
50 # Close this asset manager
51 fun close in "Java" `{ self.close(); `}
53 # Get the locales that this assets manager contains data for
54 fun get_locales
: Array[JavaString] import Array[JavaString], Array[JavaString].add
in "Java" `{
55 int arr = new_Array_of_JavaString();
56 for (String s : self.getLocales()) {
57 Array_of_JavaString_add(arr, s);
62 # String Array of all the assets at the given path
63 fun list
(path
: JavaString): Array[JavaString] import Array[JavaString], Array[JavaString].add
in "Java" `{
64 int arr = new_Array_of_JavaString();
66 for (String s : self.list(path)) {
67 Array_of_JavaString_add(arr, s);
69 }catch (IOException e) {
70 Log.e("Error retrieving the list of assets at 'path' ", e.getMessage());
76 # Open an asset using ACCESS_STREAMING mode
77 fun open
(file_name
: JavaString): NativeInputStream in "Java" `{
78 InputStream stream = null;
80 stream = self.open(file_name);
81 }catch (IOException e) {
82 Log.e("Error while opening " + file_name, e.getMessage());
88 # Open an asset and return it's file descriptor
89 fun open_fd
(file_name
: JavaString): NativeAssetFileDescriptor in "Java" `{
90 AssetFileDescriptor afd = null;
92 afd = self.openFd(file_name);
93 }catch(IOException e){
94 Log.e("Error while opening " + file_name, e.getMessage());
100 # Open a ,non-asset and return it's file descriptor
101 fun open_non_asset_fd
(file_name
: JavaString): NativeAssetFileDescriptor in "Java" `{
102 AssetFileDescriptor afd = null;
104 afd = self.openNonAssetFd(file_name);
105 }catch(IOException e){
106 Log.e("Error while opening " + file_name, e.getMessage());
113 redef fun new_global_ref
import sys
, Sys.jni_env
`{
114 Sys sys = NativeAssetManager_sys(self);
115 JNIEnv *env = Sys_jni_env(sys);
116 return (*env)->NewGlobalRef(env, self);
120 # Assets manager using a `NativeAssetManager` to manage android assets
123 # App instance used to initalize the NativeAssetManager
126 # Native asset manager
127 private var native_assets_manager
: NativeAssetManager is noinit
129 # Instanciate this AssetManager with the context of the app
130 init do self.native_assets_manager
= app
.assets
.new_global_ref
132 # Close this asset manager
133 fun close
do native_assets_manager
.close
135 # Get the locales that this assets manager contains data for
136 fun locales
: Array[String] do
137 var java_array
= native_assets_manager
.get_locales
138 var nit_array
= new Array[String]
139 for s
in java_array
do
140 nit_array
.add
(s
.to_s
)
145 # Return a string array of all the assets at the given path
146 fun list
(path
: String): Array[String] do
147 sys
.jni_env
.push_local_frame
(1)
148 var java_array
= native_assets_manager
.list
(path
.to_java_string
)
149 var nit_array
= new Array[String]
150 for s
in java_array
do
151 nit_array
.add
(s
.to_s
)
153 sys
.jni_env
.pop_local_frame
157 # Open an asset using ACCESS_STREAMING mode, returning a NativeInputStream
158 private fun open
(file_name
: String): NativeInputStream do
159 sys
.jni_env
.push_local_frame
(1)
160 var return_value
= native_assets_manager
.open
(file_name
.to_java_string
)
161 sys
.jni_env
.pop_local_frame
165 # Open an asset using it's name and returning a NativeAssetFileDescriptor
167 private fun open_fd
(file_name
: String): NativeAssetFileDescriptor do
168 sys
.jni_env
.push_local_frame
(1)
169 var return_value
= native_assets_manager
.open_fd
(file_name
.to_java_string
).new_global_ref
170 sys
.jni_env
.pop_local_frame
174 # Open a file that is not an asset returning a NativeAssetFileDescriptor
175 private fun open_non_asset_fd
(file_name
: String): NativeAssetFileDescriptor do
176 var return_value
= native_assets_manager
.open_non_asset_fd
(file_name
.to_java_string
)
180 # Return a bitmap from the assets
181 private fun bitmap
(name
: String): NativeBitmap do
182 sys
.jni_env
.push_local_frame
(1)
183 var return_value
= new NativeBitmap.from_stream
(native_assets_manager
.open
(name
.to_java_string
)).new_global_ref
184 sys
.jni_env
.pop_local_frame
188 # Deallocate the global reference allocated by AssetManager
189 fun destroy
do self.native_assets_manager
.delete_global_ref
192 # Resource manager for android resources placed in the `res` folder of your app
193 # This is a low-level class, use `ResourcesManager` instead
194 private extern class NativeResources in "Java" `{ android.content.res.Resources `}
197 fun get_assets:NativeAssetManager in "Java" `{ return self.getAssets(); `}
198 fun get_color
(id
: Int): Int in "Java" `{ return self.getColor((int)id); `}
199 fun get_boolean(id: Int): Bool in "Java" `{ return self.getBoolean((int)id); `}
200 fun get_dimension
(id
: Int): Int in "Java" `{ return (int)self.getDimension((int)id); `}
201 fun get_drawable(id: Int): NativeDrawable in "Java" `{ return self.getDrawable((int)id); `}
202 fun get_identifier
(name
, def_type
, def_package
: JavaString): Int in "Java" `{ return self.getIdentifier(name, def_type, def_package); `}
203 fun get_integer(id: Int): Int in "Java" `{ return self.getInteger((int)id); `}
204 fun get_string
(id
: Int): JavaString in "Java" `{ return self.getString((int)id); `}
205 fun get_resource_entry_name(resid: Int): JavaString in "Java" `{ return self.getResourceEntryName((int)resid); `}
206 fun get_resource_name
(resid
: Int): JavaString in "Java" `{ return self.getResourceName((int)resid); `}
207 fun get_resource_pakage_name(resid: Int): JavaString in "Java" `{ return self.getResourcePackageName((int)resid); `}
208 fun get_resource_type_name
(resid
: Int): JavaString in "Java" `{ return self.getResourceTypeName((int)resid); `}
211 redef fun new_global_ref import sys, Sys.jni_env `{
212 Sys sys
= NativeResources_sys(self);
213 JNIEnv *env
= Sys_jni_env(sys
);
214 return (*env
)->NewGlobalRef(env
, self);
218 # Resource manager for android resources placed in the `res
` folder of your app
219 class ResourcesManager
221 private var android_resources: NativeResources
223 # The name of the app_package
224 private var app_package: String
226 private init native(res: NativeResources, app_package: String)
228 self.android_resources = res.new_global_ref
229 self.app_package = app_package
232 # Get a color from resources
233 fun color(name: String): Int do
234 sys.jni_env.push_local_frame(3)
235 var return_value = android_resources.get_color(android_resources.get_identifier(name.to_java_string, "color".to_java_string, app_package.to_java_string))
236 sys.jni_env.pop_local_frame
240 # Get a `Bool` from resources
241 fun boolean(name: String): Bool do
242 sys.jni_env.push_local_frame(3)
243 var return_value = android_resources.get_boolean(android_resources.get_identifier(name.to_java_string, "bool".to_java_string, app_package.to_java_string))
244 sys.jni_env.pop_local_frame
248 # Get a dimension from resources
249 # A dimension is specified with a number followed by a unit of measure
250 fun dimension(name: String): Int do
251 sys.jni_env.push_local_frame(3)
252 var return_value = android_resources.get_dimension(android_resources.get_identifier(name.to_java_string, "dimen".to_java_string, app_package.to_java_string))
253 sys.jni_env.pop_local_frame
257 # Get an `Integer` from resources
258 fun integer(name: String): Int do
259 sys.jni_env.push_local_frame(3)
260 var return_value = android_resources.get_integer(android_resources.get_identifier(name.to_java_string, "integer".to_java_string, app_package.to_java_string))
261 sys.jni_env.pop_local_frame
266 # Get a `String` from resources
267 fun string(name: String): String do
268 sys.jni_env.push_local_frame(3)
269 var return_value = android_resources.get_string(android_resources.get_identifier(name.to_java_string, "string".to_java_string, app_package.to_java_string)).to_s
270 sys.jni_env.pop_local_frame
274 # Get a resource ID from one resource in `res
/raw
`folder
275 # you may use this to retrieve the id of a sound for example
276 fun raw_id(name: String): Int do
277 sys.jni_env.push_local_frame(3)
278 var return_value = android_resources.get_identifier(name.to_java_string, "raw".to_java_string, app_package.to_java_string)
279 sys.jni_env.pop_local_frame
283 # Get a drawable from `res
/drawable
` folder
284 private fun drawable(name: String): NativeDrawable do
285 sys.jni_env.push_local_frame(3)
286 var return_value = android_resources.get_drawable(android_resources.get_identifier(name.to_java_string, "drawable".to_java_string, app_package.to_java_string))
287 sys.jni_env.pop_local_frame
291 # Get and ID from a specific resource in `res
/res_type
` folder
292 fun other_id(name, res_type: String): Int do
293 sys.jni_env.push_local_frame(3)
294 var return_value = android_resources.get_identifier(name.to_java_string, res_type.to_java_string, app_package.to_java_string)
295 sys.jni_env.pop_local_frame
299 # Deallocate global reference allocated by ResourcesManager
300 fun destroy do self.android_resources.delete_global_ref
303 # An android Bitmap, get an instance using the AssetManager or the ResourceManager
304 private extern class NativeBitmap in "Java" `{ android.graphics.Bitmap `}
307 # Create a NativeBitmap from a NativeInputStream retrieved with `open` function of the AssetManager
308 # Called by the AssetManager
309 new from_stream
(input_stream
: NativeInputStream) in "Java" `{ return BitmapFactory.decodeStream(input_stream); `}
311 # Create a NativeBitmap using a resource ID and the NativeResources
312 # Called by the ResourceManager
313 new from_resources(res: NativeResources, id: Int) in "Java" `{ return BitmapFactory.decodeResource(res, (int)id); `}
314 fun width
: Int in "Java" `{ return self.getWidth(); `}
315 fun height: Int in "Java" `{ return self.getHeight(); `}
318 redef fun new_global_ref
import sys
, Sys.jni_env
`{
319 Sys sys = NativeBitmap_sys(self);
320 JNIEnv *env = Sys_jni_env(sys);
321 return (*env)->NewGlobalRef(env, self);
326 # Android AssetFileDescriptor, can be retrieve by AssetManager and used to load a sound in a SoundPool
327 extern class NativeAssetFileDescriptor in "Java" `{ android.content.res.AssetFileDescriptor `}
330 fun close in "Java" `{
333 }catch
(IOException e
){
337 fun create_input_stream: NativeFileInputStream in "Java" `{
339 return self.createInputStream
();
340 }catch
(IOException e
){
341 Log.e
("Error creating input_stream", e
.getMessage
());
346 fun create_output_stream: NativeFileOutputStream in "Java" `{
348 return self.createOutputStream
();
349 }catch
(IOException e
){
350 Log.e
("Error creating output stream", e
.getMessage
());
355 fun describe_contents: Int in "Java" `{ return (int)self.describeContents(); `}
356 fun declared_length
: Int in "Java" `{ return (int)self.getDeclaredLength(); `}
357 # fun extras: Bundle in "Java" `{ return self.getExtras(); `}
359 fun file_descriptor
: NativeFileDescriptor in "Java" `{
360 FileDescriptor fd = self.getFileDescriptor();
362 Log.e("AssetFileDesciptorError", "Can't retrieve the FileDescriptor of this AssetFileDescriptor");
367 fun length
: Int in "Java" `{ return (int)self.getLength(); `}
368 fun start_offset: Int in "Java" `{ return (int)self.getStartOffset(); `}
369 redef fun to_s
import JavaString.to_s
in "Java" `{ return JavaString_to_s(self.toString()); `}
372 redef fun new_global_ref import sys, Sys.jni_env `{
373 Sys sys
= NativeAssetFileDescriptor_sys(self);
374 JNIEnv *env
= Sys_jni_env(sys
);
375 return (*env
)->NewGlobalRef(env
, self);
379 # Native class representing something drawable, can be retrieved from the resources
380 # will be used by the GUI
381 private extern class NativeDrawable in "Java" `{ android.graphics.drawable.Drawable `}
385 # Resource Manager used to manage resources placed in the `res` folder of the app
386 var resource_manager
: ResourcesManager is lazy
do return new ResourcesManager.native
(self.resources
, self.package_name
.to_s
)
388 # Assets Manager used to manage resources placed in the `assets` folder of the app
389 var asset_manager
: AssetManager is lazy
do return new AssetManager(self)
391 # Get the native AssetsManager of the application, used to initialize the nit's AssetManager
392 private fun assets
: NativeAssetManager import native_activity
in "Java" `{ return App_native_activity(self).getAssets(); `}
394 # Get the package name of the application
395 private fun package_name: JavaString import native_activity in "Java" `{ return App_native_activity(self).getPackageName(); `}
397 # Get the native ResourceManager of the application, used to initialize the nit's ResourceManager
398 private fun resources
: NativeResources import native_activity
in "Java" `{ return App_native_activity(self).getResources(); `}