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 `}
50 fun close in "Java" `{ recv.close(); `}
52 fun get_locales
: Array[JavaString] import Array[JavaString], Array[JavaString].add
in "Java" `{
53 int arr = new_Array_of_JavaString();
54 for (String s : recv.getLocales()) {
55 Array_of_JavaString_add(arr, s);
60 fun list
(path
: JavaString): Array[JavaString] import Array[JavaString], Array[JavaString].add
in "Java" `{
61 int arr = new_Array_of_JavaString();
63 for (String s : recv.list(path)) {
64 Array_of_JavaString_add(arr, s);
66 }catch (IOException e) {
67 Log.e("Error retrieving the list of assets at 'path' ", e.getMessage());
73 fun open
(file_name
: JavaString): NativeInputStream in "Java" `{
74 InputStream stream = null;
76 stream = recv.open(file_name);
77 }catch (IOException e) {
78 Log.e("Error while opening " + file_name, e.getMessage());
84 fun open_fd
(file_name
: JavaString): NativeAssetFileDescriptor in "Java" `{
85 AssetFileDescriptor afd = null;
87 afd = recv.openFd(file_name);
88 }catch(IOException e){
89 Log.e("Error while opening " + file_name, e.getMessage());
95 fun open_non_asset_fd
(file_name
: JavaString): NativeAssetFileDescriptor in "Java" `{
96 AssetFileDescriptor afd = null;
98 afd = recv.openNonAssetFd(file_name);
99 }catch(IOException e){
100 Log.e("Error while opening " + file_name, e.getMessage());
107 # Assets manager using a `NativeAssetManager` to manage android assets
109 # Native asset manager
110 var native_assets_manager
: NativeAssetManager
112 init(app
: App) do self.native_assets_manager
= app
.assets
.new_global_ref
114 # Close this asset manager
115 fun close
do native_assets_manager
.close
117 # Get the locales that this assets manager contains data for
118 fun locales
: Array[String] do
119 var java_array
= native_assets_manager
.get_locales
120 var nit_array
= new Array[String]
121 for s
in java_array
do
122 nit_array
.add
(s
.to_s
)
127 # Return a string array of all the assets at the given path
128 fun list
(path
: String): Array[String] do
129 sys
.jni_env
.push_local_frame
(1)
130 var java_array
= native_assets_manager
.list
(path
.to_java_string
)
131 var nit_array
= new Array[String]
132 for s
in java_array
do
133 nit_array
.add
(s
.to_s
)
135 sys
.jni_env
.pop_local_frame
139 # Open an asset using ACCESS_STREAMING mode, returning a NativeInputStream
140 fun open
(file_name
: String): NativeInputStream do
141 var return_value
= native_assets_manager
.open
(file_name
.to_java_string
)
145 # Open an asset using it's name and returning a NativeAssetFileDescriptor
147 fun open_fd
(file_name
: String): NativeAssetFileDescriptor do
148 var return_value
= native_assets_manager
.open_fd
(file_name
.to_java_string
)
152 # Open a file that is not an asset returning a NativeAssetFileDescriptor
153 fun open_non_asset_fd
(file_name
: String): NativeAssetFileDescriptor do
154 var return_value
= native_assets_manager
.open_non_asset_fd
(file_name
.to_java_string
)
158 # Return a bitmap from the assets
159 fun bitmap
(name
: String): NativeBitmap do
160 sys
.jni_env
.push_local_frame
(1)
161 var return_value
= new NativeBitmap.from_stream
(native_assets_manager
.open
(name
.to_java_string
))
162 sys
.jni_env
.pop_local_frame
166 # Deallocate the global reference allocated by AssetManager
167 fun destroy
do self.native_assets_manager
.delete_global_ref
170 # Resource manager for android resources placed in the `res` folder of your app
171 # This is a low-level class, use `ResourcesManager` instead
172 extern class NativeResources in "Java" `{ android.content.res.Resources `}
175 fun get_assets:NativeAssetManager in "Java" `{ return recv.getAssets(); `}
176 fun get_color
(id
: Int): Int in "Java" `{ return recv.getColor((int)id); `}
177 fun get_boolean(id: Int): Bool in "Java" `{ return recv.getBoolean((int)id); `}
178 fun get_dimension
(id
: Int): Int in "Java" `{ return (int)recv.getDimension((int)id); `}
179 fun get_drawable(id: Int): NativeDrawable in "Java" `{ return recv.getDrawable((int)id); `}
180 fun get_identifier
(name
, def_type
, def_package
: JavaString): Int in "Java" `{ return recv.getIdentifier(name, def_type, def_package); `}
181 fun get_integer(id: Int): Int in "Java" `{ return recv.getInteger((int)id); `}
182 fun get_string
(id
: Int): JavaString in "Java" `{ return recv.getString((int)id); `}
183 fun get_resource_entry_name(resid: Int): JavaString in "Java" `{ return recv.getResourceEntryName((int)resid); `}
184 fun get_resource_name
(resid
: Int): JavaString in "Java" `{ return recv.getResourceName((int)resid); `}
185 fun get_resource_pakage_name(resid: Int): JavaString in "Java" `{ return recv.getResourcePackageName((int)resid); `}
186 fun get_resource_type_name
(resid
: Int): JavaString in "Java" `{ return recv.getResourceTypeName((int)resid); `}
189 redef fun new_global_ref import sys, Sys.jni_env `{
190 Sys sys
= NativeResources_sys(recv
);
191 JNIEnv *env
= Sys_jni_env(sys
);
192 return (*env
)->NewGlobalRef(env
, recv
);
196 # Resource manager for android resources placed in the `res
` folder of your app
197 class ResourcesManager
199 var android_resources: NativeResources
201 # The name of the app_package
202 var app_package: String
204 init(res: NativeResources, app_package: String)
206 self.android_resources = res.new_global_ref
207 self.app_package = app_package
210 # Get a color from resources
211 fun color(name: String): Int do
212 sys.jni_env.push_local_frame(3)
213 var return_value = android_resources.get_color(android_resources.get_identifier(name.to_java_string, "color".to_java_string, app_package.to_java_string))
214 sys.jni_env.pop_local_frame
218 # Get a `Bool` from resources
219 fun boolean(name: String): Bool do
220 sys.jni_env.push_local_frame(3)
221 var return_value = android_resources.get_boolean(android_resources.get_identifier(name.to_java_string, "bool".to_java_string, app_package.to_java_string))
222 sys.jni_env.pop_local_frame
226 # Get a dimension from resources
227 # A dimension is specified with a number followed by a unit of measure
228 fun dimension(name: String): Int do
229 sys.jni_env.push_local_frame(3)
230 var return_value = android_resources.get_dimension(android_resources.get_identifier(name.to_java_string, "dimen".to_java_string, app_package.to_java_string))
231 sys.jni_env.pop_local_frame
235 # Get an `Integer` from resources
236 fun integer(name: String): Int do
237 sys.jni_env.push_local_frame(3)
238 var return_value = android_resources.get_integer(android_resources.get_identifier(name.to_java_string, "integer".to_java_string, app_package.to_java_string))
239 sys.jni_env.pop_local_frame
244 # Get a `String` from resources
245 fun string(name: String): String do
246 sys.jni_env.push_local_frame(3)
247 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
248 sys.jni_env.pop_local_frame
252 # Get a resource ID from one resource in `res
/raw
`folder
253 # you may use this to retrieve the id of a sound for example
254 fun raw_id(name: String): Int do
255 sys.jni_env.push_local_frame(3)
256 var return_value = android_resources.get_identifier(name.to_java_string, "raw".to_java_string, app_package.to_java_string)
257 sys.jni_env.pop_local_frame
261 # Get a drawable from `res
/drawable
` folder
262 fun drawable(name: String): NativeDrawable do
263 sys.jni_env.push_local_frame(3)
264 var return_value = android_resources.get_drawable(android_resources.get_identifier(name.to_java_string, "drawable".to_java_string, app_package.to_java_string))
265 sys.jni_env.pop_local_frame
269 # Get and ID from a specific resource in `res
/res_type
` folder
270 fun other_id(name, res_type: String): Int do
271 sys.jni_env.push_local_frame(3)
272 var return_value = android_resources.get_identifier(name.to_java_string, res_type.to_java_string, app_package.to_java_string)
273 sys.jni_env.pop_local_frame
277 # Deallocate global reference allocated by ResourcesManager
278 fun destroy do self.android_resources.delete_global_ref
281 # An android Bitmap, get an instance using the AssetManager or the ResourceManager
282 extern class NativeBitmap in "Java" `{ android.graphics.Bitmap `}
285 # Create a NativeBitmap from a NativeInputStream retrieved with `open` function of the AssetManager
286 # Called by the AssetManager
287 new from_stream
(input_stream
: NativeInputStream) in "Java" `{ return BitmapFactory.decodeStream(input_stream); `}
289 # Create a NativeBitmap using a resource ID and the NativeResources
290 # Called by the ResourceManager
291 new from_resources(res: NativeResources, id: Int) in "Java" `{ return BitmapFactory.decodeResource(res, (int)id); `}
292 fun width
: Int in "Java" `{ return recv.getWidth(); `}
293 fun height: Int in "Java" `{ return recv.getHeight(); `}
296 # Android AssetFileDescriptor, can be retrieve by AssetManager and used to load a sound in a SoundPool
297 extern class NativeAssetFileDescriptor in "Java" `{ android.content.res.AssetFileDescriptor `}
300 fun close in "Java" `{
303 }catch
(IOException e
){
307 fun create_input_stream: NativeFileInputStream in "Java" `{
309 return recv
.createInputStream
();
310 }catch
(IOException e
){
311 Log.e
("Error creating input_stream", e
.getMessage
());
316 fun create_output_stream: NativeFileOutputStream in "Java" `{
318 return recv
.createOutputStream
();
319 }catch
(IOException e
){
320 Log.e
("Error creating output stream", e
.getMessage
());
325 fun describe_contents: Int in "Java" `{ return (int)recv.describeContents(); `}
326 fun declared_length
: Int in "Java" `{ return (int)recv.getDeclaredLength(); `}
327 # fun extras: Bundle in "Java" `{ return recv.getExtras(); `}
329 fun file_descriptor
: NativeFileDescriptor in "Java" `{
330 FileDescriptor fd = recv.getFileDescriptor();
332 Log.e("AssetFileDesciptorError", "Can't retrieve the FileDescriptor of this AssetFileDescriptor");
337 fun length
: Int in "Java" `{ return (int)recv.getLength(); `}
338 fun start_offset: Int in "Java" `{ return (int)recv.getStartOffset(); `}
339 redef fun to_s
: String import JavaString.to_s
in "Java" `{ return JavaString_to_s(recv.toString()); `}
342 # Native class representing something drawable, can be retrieved from the resources
343 # will be used by the GUI
344 extern class NativeDrawable in "Java" `{ android.graphics.drawable.Drawable `}
348 # Resource Manager used to manage resources placed in the `res` folder of the app
349 fun resource_manager
: ResourcesManager is cached
do return new ResourcesManager(self.resources
, self.package_name
.to_s
)
351 # Assets Manager used to manage resources placed in the `assets` folder of the app
352 fun asset_manager
: AssetManager is cached
do return new AssetManager(self)
354 # Get the native AssetsManager of the application, used to initialize the nit's AssetManager
355 private fun assets
: NativeAssetManager import native_activity
in "Java" `{ return App_native_activity(recv).getAssets(); `}
357 # Get the package name of the application
358 private fun package_name: JavaString import native_activity in "Java" `{ return App_native_activity(recv).getPackageName(); `}
360 # Get the native ResourceManager of the application, used to initialize the nit's ResourceManager
361 private fun resources
: NativeResources import native_activity
in "Java" `{ return App_native_activity(recv).getResources(); `}