code: explicitly call init in some named constructors
[nit.git] / lib / android / assets_and_resources.nit
1 # this file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Romain Chanoir <romain.chanoir@viacesi.fr>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Android Assets and Resources Management
18 #
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
26
27 import dalvik
28 import java
29 import java::io
30
31 in "Java" `{
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;
43 `}
44
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 `}
48 super JavaObject
49
50 # Close this asset manager
51 fun close in "Java" `{ self.close(); `}
52
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);
58 }
59 return arr;
60 `}
61
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();
65 try {
66 for (String s : self.list(path)) {
67 Array_of_JavaString_add(arr, s);
68 }
69 }catch (IOException e) {
70 Log.e("Error retrieving the list of assets at 'path' ", e.getMessage());
71 e.printStackTrace();
72 }
73 return arr;
74 `}
75
76 # Open an asset using ACCESS_STREAMING mode
77 fun open(file_name: JavaString): NativeInputStream in "Java" `{
78 InputStream stream = null;
79 try {
80 stream = self.open(file_name);
81 }catch (IOException e) {
82 Log.e("Error while opening " + file_name, e.getMessage());
83 return null;
84 }
85 return stream;
86 `}
87
88 # Open an asset and return it's file descriptor
89 fun open_fd(file_name: JavaString): NativeAssetFileDescriptor in "Java" `{
90 AssetFileDescriptor afd = null;
91 try {
92 afd = self.openFd(file_name);
93 }catch(IOException e){
94 Log.e("Error while opening " + file_name, e.getMessage());
95 return null;
96 }
97 return afd;
98 `}
99
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;
103 try {
104 afd = self.openNonAssetFd(file_name);
105 }catch(IOException e){
106 Log.e("Error while opening " + file_name, e.getMessage());
107 return null;
108 }
109 return afd;
110 `}
111
112 # HACK for bug #845
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);
117 `}
118 end
119
120 # Assets manager using a `NativeAssetManager` to manage android assets
121 class AssetManager
122
123 # Native asset manager
124 private var native_assets_manager: NativeAssetManager = app.native_activity.assets.new_global_ref is lazy
125
126 # Close this asset manager
127 fun close do native_assets_manager.close
128
129 # Get the locales that this assets manager contains data for
130 fun locales: Array[String] do
131 var java_array = native_assets_manager.get_locales
132 var nit_array = new Array[String]
133 for s in java_array do
134 nit_array.add(s.to_s)
135 end
136 return nit_array
137 end
138
139 # Return a string array of all the assets at the given path
140 fun list(path: String): Array[String] do
141 sys.jni_env.push_local_frame(1)
142 var java_array = native_assets_manager.list(path.to_java_string)
143 var nit_array = new Array[String]
144 for s in java_array do
145 nit_array.add(s.to_s)
146 end
147 sys.jni_env.pop_local_frame
148 return nit_array
149 end
150
151 # Open an asset using ACCESS_STREAMING mode, returning a NativeInputStream
152 private fun open(file_name: String): NativeInputStream do
153 sys.jni_env.push_local_frame(1)
154 var return_value = native_assets_manager.open(file_name.to_java_string)
155 sys.jni_env.pop_local_frame
156 return return_value
157 end
158
159 # Open an asset using it's name and returning a NativeAssetFileDescriptor
160 # `file_name` is
161 private fun open_fd(file_name: String): NativeAssetFileDescriptor do
162 sys.jni_env.push_local_frame(1)
163 var return_value = native_assets_manager.open_fd(file_name.to_java_string).new_global_ref
164 sys.jni_env.pop_local_frame
165 return return_value
166 end
167
168 # Open a file that is not an asset returning a NativeAssetFileDescriptor
169 private fun open_non_asset_fd(file_name: String): NativeAssetFileDescriptor do
170 var return_value = native_assets_manager.open_non_asset_fd(file_name.to_java_string)
171 return return_value
172 end
173
174 # Return a bitmap from the assets
175 private fun bitmap(name: String): NativeBitmap do
176 sys.jni_env.push_local_frame 2
177 var return_value = new NativeBitmap.from_stream(native_assets_manager.open(name.to_java_string))
178 return return_value.pop_from_local_frame
179 end
180
181 # Deallocate the global reference allocated by AssetManager
182 fun destroy do self.native_assets_manager.delete_global_ref
183 end
184
185 # Resource manager for android resources placed in the `res` folder of your app
186 # This is a low-level class, use `ResourcesManager` instead
187 private extern class NativeResources in "Java" `{ android.content.res.Resources `}
188 super JavaObject
189
190 fun get_assets:NativeAssetManager in "Java" `{ return self.getAssets(); `}
191 fun get_color(id: Int): Int in "Java" `{ return self.getColor((int)id); `}
192 fun get_boolean(id: Int): Bool in "Java" `{ return self.getBoolean((int)id); `}
193 fun get_dimension(id: Int): Int in "Java" `{ return (int)self.getDimension((int)id); `}
194 fun get_drawable(id: Int): NativeDrawable in "Java" `{ return self.getDrawable((int)id); `}
195 fun get_identifier(name, def_type, def_package: JavaString): Int in "Java" `{ return self.getIdentifier(name, def_type, def_package); `}
196 fun get_integer(id: Int): Int in "Java" `{ return self.getInteger((int)id); `}
197 fun get_string(id: Int): JavaString in "Java" `{ return self.getString((int)id); `}
198 fun get_resource_entry_name(resid: Int): JavaString in "Java" `{ return self.getResourceEntryName((int)resid); `}
199 fun get_resource_name(resid: Int): JavaString in "Java" `{ return self.getResourceName((int)resid); `}
200 fun get_resource_pakage_name(resid: Int): JavaString in "Java" `{ return self.getResourcePackageName((int)resid); `}
201 fun get_resource_type_name(resid: Int): JavaString in "Java" `{ return self.getResourceTypeName((int)resid); `}
202
203 # HACK for bug #845
204 redef fun new_global_ref import sys, Sys.jni_env `{
205 Sys sys = NativeResources_sys(self);
206 JNIEnv *env = Sys_jni_env(sys);
207 return (*env)->NewGlobalRef(env, self);
208 `}
209 end
210
211 # Resource manager for android resources placed in the `res` folder of your app
212 class ResourcesManager
213 # Native resources
214 private var android_resources: NativeResources
215
216 # The name of the app_package
217 private var app_package: String
218
219 private init native(res: NativeResources, app_package: String)
220 do
221 init(res.new_global_ref, app_package)
222 end
223
224 # Get a color from resources
225 fun color(name: String): Int do
226 sys.jni_env.push_local_frame(3)
227 var return_value = android_resources.get_color(android_resources.get_identifier(name.to_java_string, "color".to_java_string, app_package.to_java_string))
228 sys.jni_env.pop_local_frame
229 return return_value
230 end
231
232 # Get a `Bool` from resources
233 fun boolean(name: String): Bool do
234 sys.jni_env.push_local_frame(3)
235 var return_value = android_resources.get_boolean(android_resources.get_identifier(name.to_java_string, "bool".to_java_string, app_package.to_java_string))
236 sys.jni_env.pop_local_frame
237 return return_value
238 end
239
240 # Get a dimension from resources
241 # A dimension is specified with a number followed by a unit of measure
242 fun dimension(name: String): Int do
243 sys.jni_env.push_local_frame(3)
244 var return_value = android_resources.get_dimension(android_resources.get_identifier(name.to_java_string, "dimen".to_java_string, app_package.to_java_string))
245 sys.jni_env.pop_local_frame
246 return return_value
247 end
248
249 # Get an `Integer` from resources
250 fun integer(name: String): Int do
251 sys.jni_env.push_local_frame(3)
252 var return_value = android_resources.get_integer(android_resources.get_identifier(name.to_java_string, "integer".to_java_string, app_package.to_java_string))
253 sys.jni_env.pop_local_frame
254 return return_value
255 end
256
257
258 # Get a `String` from resources
259 fun string(name: String): String do
260 sys.jni_env.push_local_frame(3)
261 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
262 sys.jni_env.pop_local_frame
263 return return_value
264 end
265
266 # Get a resource ID from one resource in `res/raw`folder
267 # you may use this to retrieve the id of a sound for example
268 fun raw_id(name: String): Int do
269 sys.jni_env.push_local_frame(3)
270 var return_value = android_resources.get_identifier(name.to_java_string, "raw".to_java_string, app_package.to_java_string)
271 sys.jni_env.pop_local_frame
272 return return_value
273 end
274
275 # Get a drawable from `res/drawable` folder
276 private fun drawable(name: String): NativeDrawable do
277 sys.jni_env.push_local_frame(3)
278 var return_value = android_resources.get_drawable(android_resources.get_identifier(name.to_java_string, "drawable".to_java_string, app_package.to_java_string))
279 sys.jni_env.pop_local_frame
280 return return_value
281 end
282
283 # Get and ID from a specific resource in `res/res_type` folder
284 fun other_id(name, res_type: String): Int do
285 sys.jni_env.push_local_frame(3)
286 var return_value = android_resources.get_identifier(name.to_java_string, res_type.to_java_string, app_package.to_java_string)
287 sys.jni_env.pop_local_frame
288 return return_value
289 end
290
291 # Deallocate global reference allocated by ResourcesManager
292 fun destroy do self.android_resources.delete_global_ref
293 end
294
295 # An android Bitmap, get an instance using the AssetManager or the ResourceManager
296 private extern class NativeBitmap in "Java" `{ android.graphics.Bitmap `}
297 super JavaObject
298
299 # Create a NativeBitmap from a NativeInputStream retrieved with `open` function of the AssetManager
300 # Called by the AssetManager
301 new from_stream(input_stream: NativeInputStream) in "Java" `{ return BitmapFactory.decodeStream(input_stream); `}
302
303 # Create a NativeBitmap using a resource ID and the NativeResources
304 # Called by the ResourceManager
305 new from_resources(res: NativeResources, id: Int) in "Java" `{ return BitmapFactory.decodeResource(res, (int)id); `}
306
307 # Width in pixels
308 #
309 # Wraps Java: `int android.graphics.Bitmap.getWidth()`
310 fun width: Int in "Java" `{ return self.getWidth(); `}
311
312 # Height in pixels
313 #
314 # Wraps Java: `int android.graphics.Bitmap.getHeight()`
315 fun height: Int in "Java" `{ return self.getHeight(); `}
316
317 # Number of bytes per row
318 #
319 # Wraps Java: `int android.graphics.Bitmap.getRowBytes()`
320 fun row_bytes: Int in "Java" `{
321 return self.getRowBytes();
322 `}
323
324 # Does this bitmap has an alpha channel?
325 #
326 # Wraps Java: `boolean android.graphics.Bitmap.hasAlpha()`
327 fun has_alpha: Bool in "Java" `{
328 return self.hasAlpha();
329 `}
330
331 # HACK for bug #845
332 redef fun new_global_ref import sys, Sys.jni_env `{
333 Sys sys = NativeBitmap_sys(self);
334 JNIEnv *env = Sys_jni_env(sys);
335 return (*env)->NewGlobalRef(env, self);
336 `}
337
338 redef fun pop_from_local_frame_with_env(jni_env) `{
339 return (*jni_env)->PopLocalFrame(jni_env, self);
340 `}
341 end
342
343 # Android AssetFileDescriptor, can be retrieve by AssetManager and used to load a sound in a SoundPool
344 extern class NativeAssetFileDescriptor in "Java" `{ android.content.res.AssetFileDescriptor `}
345 super JavaObject
346
347 fun close in "Java" `{
348 try {
349 self.close();
350 }catch(IOException e){
351 e.printStackTrace();
352 }
353 `}
354 fun create_input_stream: NativeFileInputStream in "Java" `{
355 try {
356 return self.createInputStream();
357 }catch(IOException e){
358 Log.e("Error creating input_stream", e.getMessage());
359 e.printStackTrace();
360 return null;
361 }
362 `}
363 fun create_output_stream: NativeFileOutputStream in "Java" `{
364 try {
365 return self.createOutputStream();
366 }catch(IOException e){
367 Log.e("Error creating output stream", e.getMessage());
368 e.printStackTrace();
369 return null;
370 }
371 `}
372 fun describe_contents: Int in "Java" `{ return (int)self.describeContents(); `}
373 fun declared_length: Int in "Java" `{ return (int)self.getDeclaredLength(); `}
374 # fun extras: Bundle in "Java" `{ return self.getExtras(); `}
375
376 fun file_descriptor: NativeFileDescriptor in "Java" `{
377 FileDescriptor fd = self.getFileDescriptor();
378 if (fd == null) {
379 Log.e("AssetFileDesciptorError", "Can't retrieve the FileDescriptor of this AssetFileDescriptor");
380 }
381 return fd;
382 `}
383
384 fun length: Int in "Java" `{ return (int)self.getLength(); `}
385 fun start_offset: Int in "Java" `{ return (int)self.getStartOffset(); `}
386 redef fun to_s import JavaString.to_s in "Java" `{ return JavaString_to_s(self.toString()); `}
387
388 # HACK for bug #845
389 redef fun new_global_ref import sys, Sys.jni_env `{
390 Sys sys = NativeAssetFileDescriptor_sys(self);
391 JNIEnv *env = Sys_jni_env(sys);
392 return (*env)->NewGlobalRef(env, self);
393 `}
394 end
395
396 # Native class representing something drawable, can be retrieved from the resources
397 # will be used by the GUI
398 private extern class NativeDrawable in "Java" `{ android.graphics.drawable.Drawable `}
399 end
400
401 redef class App
402 # Resource Manager used to manage resources placed in the `res` folder of the app
403 var resource_manager: ResourcesManager is lazy do
404 var res = native_activity.resources
405 var pkg = native_activity.package_name
406 return new ResourcesManager.native(res, pkg.to_s)
407 end
408
409 # Assets Manager used to manage resources placed in the `assets` folder of the app
410 var asset_manager: AssetManager is lazy do return new AssetManager
411 end
412
413 redef extern class NativeActivity
414
415 # Get the native AssetsManager of the application, used to initialize the nit's AssetManager
416 private fun assets: NativeAssetManager in "Java" `{
417 return self.getAssets();
418 `}
419
420 # Get the package name of the application
421 private fun package_name: JavaString in "Java" `{
422 return self.getPackageName();
423 `}
424
425 # Get the native ResourceManager of the application, used to initialize the nit's ResourceManager
426 private fun resources: NativeResources in "Java" `{
427 return self.getResources();
428 `}
429 end