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
27 import native_app_glue
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 extern class NativeAssetManager in "Java" `{ android.content.res.AssetManager `}
49 redef type SELF: NativeAssetManager
51 fun close in "Java" `{ recv.close(); `}
53 fun get_locales
: Array[JavaString] import Array[JavaString], Array[JavaString].add
in "Java" `{
54 int arr = new_Array_of_JavaString();
55 for (String s : recv.getLocales()) {
56 Array_of_JavaString_add(arr, s);
61 fun list
(path
: JavaString): Array[JavaString] import Array[JavaString], Array[JavaString].add
in "Java" `{
62 int arr = new_Array_of_JavaString();
64 for (String s : recv.list(path)) {
65 Array_of_JavaString_add(arr, s);
67 }catch (IOException e) {
68 Log.e("Error retrieving the list of assets at 'path' ", e.getMessage());
74 fun open
(file_name
: JavaString): NativeInputStream in "Java" `{
75 InputStream stream = null;
77 stream = recv.open(file_name);
78 }catch (IOException e) {
79 Log.e("Error while opening " + file_name, e.getMessage());
85 fun open_fd
(file_name
: JavaString): NativeAssetFileDescriptor in "Java" `{
86 AssetFileDescriptor afd = null;
88 afd = recv.openFd(file_name);
89 }catch(IOException e){
90 Log.e("Error while opening " + file_name, e.getMessage());
96 fun open_non_asset_fd
(file_name
: JavaString): NativeAssetFileDescriptor in "Java" `{
97 AssetFileDescriptor afd = null;
99 afd = recv.openNonAssetFd(file_name);
100 }catch(IOException e){
101 Log.e("Error while opening " + file_name, e.getMessage());
108 # Assets manager using a `NativeAssetManager` to manage android assets
110 # Native asset manager
111 var native_assets_manager
: NativeAssetManager
113 init(app
: App) do self.native_assets_manager
= app
.assets
.new_global_ref
115 # Close this asset manager
116 fun close
do native_assets_manager
.close
118 # Get the locales that this assets manager contains data for
119 fun locales
: Array[String] do
120 var java_array
= native_assets_manager
.get_locales
121 var nit_array
= new Array[String]
122 for s
in java_array
do
123 nit_array
.add
(s
.to_s
)
128 # Return a string array of all the assets at the given path
129 fun list
(path
: String): Array[String] do
130 sys
.jni_env
.push_local_frame
(1)
131 var java_array
= native_assets_manager
.list
(path
.to_java_string
)
132 var nit_array
= new Array[String]
133 for s
in java_array
do
134 nit_array
.add
(s
.to_s
)
136 sys
.jni_env
.pop_local_frame
140 # Open an asset using ACCESS_STREAMING mode, returning a NativeInputStream
141 fun open
(file_name
: String): NativeInputStream do
142 var return_value
= native_assets_manager
.open
(file_name
.to_java_string
)
146 # Open an asset using it's name and returning a NativeAssetFileDescriptor
148 fun open_fd
(file_name
: String): NativeAssetFileDescriptor do
149 var return_value
= native_assets_manager
.open_fd
(file_name
.to_java_string
)
153 # Open a file that is not an asset returning a NativeAssetFileDescriptor
154 fun open_non_asset_fd
(file_name
: String): NativeAssetFileDescriptor do
155 var return_value
= native_assets_manager
.open_non_asset_fd
(file_name
.to_java_string
)
159 # Return a bitmap from the assets
160 fun bitmap
(name
: String): NativeBitmap do
161 sys
.jni_env
.push_local_frame
(1)
162 var return_value
= new NativeBitmap.from_stream
(native_assets_manager
.open
(name
.to_java_string
))
163 sys
.jni_env
.pop_local_frame
167 # Deallocate the global reference allocated by AssetManager
168 fun destroy
do self.native_assets_manager
.delete_global_ref
171 # Resource manager for android resources placed in the `res` folder of your app
172 # This is a low-level class, use `ResourcesManager` instead
173 extern class NativeResources in "Java" `{ android.content.res.Resources `}
175 redef type SELF: NativeResources
177 fun get_assets:NativeAssetManager in "Java" `{ return recv.getAssets(); `}
178 fun get_color
(id
: Int): Int in "Java" `{ return recv.getColor((int)id); `}
179 fun get_boolean(id: Int): Bool in "Java" `{ return recv.getBoolean((int)id); `}
180 fun get_dimension
(id
: Int): Int in "Java" `{ return (int)recv.getDimension((int)id); `}
181 fun get_drawable(id: Int): NativeDrawable in "Java" `{ return recv.getDrawable((int)id); `}
182 fun get_identifier
(name
, def_type
, def_package
: JavaString): Int in "Java" `{ return recv.getIdentifier(name, def_type, def_package); `}
183 fun get_integer(id: Int): Int in "Java" `{ return recv.getInteger((int)id); `}
184 fun get_string
(id
: Int): JavaString in "Java" `{ return recv.getString((int)id); `}
185 fun get_resource_entry_name(resid: Int): JavaString in "Java" `{ return recv.getResourceEntryName((int)resid); `}
186 fun get_resource_name
(resid
: Int): JavaString in "Java" `{ return recv.getResourceName((int)resid); `}
187 fun get_resource_pakage_name(resid: Int): JavaString in "Java" `{ return recv.getResourcePackageName((int)resid); `}
188 fun get_resource_type_name
(resid
: Int): JavaString in "Java" `{ return recv.getResourceTypeName((int)resid); `}
191 redef fun new_global_ref import sys, Sys.jni_env `{
192 Sys sys
= NativeResources_sys(recv
);
193 JNIEnv *env
= Sys_jni_env(sys
);
194 return (*env
)->NewGlobalRef(env
, recv
);
198 # Resource manager for android resources placed in the `res
` folder of your app
199 class ResourcesManager
201 var android_resources: NativeResources
203 # The name of the app_package
204 var app_package: String
206 init(res: NativeResources, app_package: String)
208 self.android_resources = res.new_global_ref
209 self.app_package = app_package
212 # Get a color from resources
213 fun color(name: String): Int do
214 sys.jni_env.push_local_frame(3)
215 var return_value = android_resources.get_color(android_resources.get_identifier(name.to_java_string, "color".to_java_string, app_package.to_java_string))
216 sys.jni_env.pop_local_frame
220 # Get a `Bool` from resources
221 fun boolean(name: String): Bool do
222 sys.jni_env.push_local_frame(3)
223 var return_value = android_resources.get_boolean(android_resources.get_identifier(name.to_java_string, "bool".to_java_string, app_package.to_java_string))
224 sys.jni_env.pop_local_frame
228 # Get a dimension from resources
229 # A dimension is specified with a number followed by a unit of measure
230 fun dimension(name: String): Int do
231 sys.jni_env.push_local_frame(3)
232 var return_value = android_resources.get_dimension(android_resources.get_identifier(name.to_java_string, "dimen".to_java_string, app_package.to_java_string))
233 sys.jni_env.pop_local_frame
237 # Get an `Integer` from resources
238 fun integer(name: String): Int do
239 sys.jni_env.push_local_frame(3)
240 var return_value = android_resources.get_integer(android_resources.get_identifier(name.to_java_string, "integer".to_java_string, app_package.to_java_string))
241 sys.jni_env.pop_local_frame
246 # Get a `String` from resources
247 fun string(name: String): String do
248 sys.jni_env.push_local_frame(3)
249 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
250 sys.jni_env.pop_local_frame
254 # Get a resource ID from one resource in `res
/raw
`folder
255 # you may use this to retrieve the id of a sound for example
256 fun raw_id(name: String): Int do
257 sys.jni_env.push_local_frame(3)
258 var return_value = android_resources.get_identifier(name.to_java_string, "raw".to_java_string, app_package.to_java_string)
259 sys.jni_env.pop_local_frame
263 # Get a drawable from `res
/drawable
` folder
264 fun drawable(name: String): NativeDrawable do
265 sys.jni_env.push_local_frame(3)
266 var return_value = android_resources.get_drawable(android_resources.get_identifier(name.to_java_string, "drawable".to_java_string, app_package.to_java_string))
267 sys.jni_env.pop_local_frame
271 # Get and ID from a specific resource in `res
/res_type
` folder
272 fun other_id(name, res_type: String): Int do
273 sys.jni_env.push_local_frame(3)
274 var return_value = android_resources.get_identifier(name.to_java_string, res_type.to_java_string, app_package.to_java_string)
275 sys.jni_env.pop_local_frame
279 # Deallocate global reference allocated by ResourcesManager
280 fun destroy do self.android_resources.delete_global_ref
283 # An android Bitmap, get an instance using the AssetManager or the ResourceManager
284 extern class NativeBitmap in "Java" `{ android.graphics.Bitmap `}
286 redef type SELF: NativeBitmap
288 # Create a NativeBitmap from a NativeInputStream retrieved with `open` function of the AssetManager
289 # Called by the AssetManager
290 new from_stream
(input_stream
: NativeInputStream) in "Java" `{ return BitmapFactory.decodeStream(input_stream); `}
292 # Create a NativeBitmap using a resource ID and the NativeResources
293 # Called by the ResourceManager
294 new from_resources(res: NativeResources, id: Int) in "Java" `{ return BitmapFactory.decodeResource(res, (int)id); `}
295 fun width
: Int in "Java" `{ return recv.getWidth(); `}
296 fun height: Int in "Java" `{ return recv.getHeight(); `}
299 # Android AssetFileDescriptor, can be retrieve by AssetManager and used to load a sound in a SoundPool
300 extern class NativeAssetFileDescriptor in "Java" `{ android.content.res.AssetFileDescriptor `}
302 redef type SELF: NativeAssetFileDescriptor
304 fun close in "Java" `{
307 }catch
(IOException e
){
311 fun create_input_stream: NativeFileInputStream in "Java" `{
313 return recv
.createInputStream
();
314 }catch
(IOException e
){
315 Log.e
("Error creating input_stream", e
.getMessage
());
320 fun create_output_stream: NativeFileOutputStream in "Java" `{
322 return recv
.createOutputStream
();
323 }catch
(IOException e
){
324 Log.e
("Error creating output stream", e
.getMessage
());
329 fun describe_contents: Int in "Java" `{ return (int)recv.describeContents(); `}
330 fun declared_length
: Int in "Java" `{ return (int)recv.getDeclaredLength(); `}
331 # fun extras: Bundle in "Java" `{ return recv.getExtras(); `}
333 fun file_descriptor
: NativeFileDescriptor in "Java" `{
334 FileDescriptor fd = recv.getFileDescriptor();
336 Log.e("AssetFileDesciptorError", "Can't retrieve the FileDescriptor of this AssetFileDescriptor");
341 fun length
: Int in "Java" `{ return (int)recv.getLength(); `}
342 fun start_offset: Int in "Java" `{ return (int)recv.getStartOffset(); `}
343 redef fun to_s
: String import JavaString.to_s
in "Java" `{ return JavaString_to_s(recv.toString()); `}
346 # Native class representing something drawable, can be retrieved from the resources
347 # will be used by the GUI
348 extern class NativeDrawable in "Java" `{ android.graphics.drawable.Drawable `}
352 # Resource Manager used to manage resources placed in the `res` folder of the app
353 fun resource_manager
: ResourcesManager is cached
do return new ResourcesManager(self.resources
, self.package_name
.to_s
)
355 # Assets Manager used to manage resources placed in the `assets` folder of the app
356 fun asset_manager
: AssetManager is cached
do return new AssetManager(self)
358 # Get the native AssetsManager of the application, used to initialize the nit's AssetManager
359 private fun assets
: NativeAssetManager import native_activity
in "Java" `{ return App_native_activity(recv).getAssets(); `}
361 # Get the package name of the application
362 private fun package_name: JavaString import native_activity in "Java" `{ return App_native_activity(recv).getPackageName(); `}
364 # Get the native ResourceManager of the application, used to initialize the nit's ResourceManager
365 private fun resources
: NativeResources import native_activity
in "Java" `{ return App_native_activity(recv).getResources(); `}