lib/android: hacks to fix new_global_ref awaiting a fix for #845
[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 native_app_glue
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 extern class NativeAssetManager in "Java" `{ android.content.res.AssetManager `}
48 super JavaObject
49 redef type SELF: NativeAssetManager
50
51 fun close in "Java" `{ recv.close(); `}
52
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);
57 }
58 return arr;
59 `}
60
61 fun list(path: JavaString): Array[JavaString] import Array[JavaString], Array[JavaString].add in "Java" `{
62 int arr = new_Array_of_JavaString();
63 try {
64 for (String s : recv.list(path)) {
65 Array_of_JavaString_add(arr, s);
66 }
67 }catch (IOException e) {
68 Log.e("Error retrieving the list of assets at 'path' ", e.getMessage());
69 e.printStackTrace();
70 }
71 return arr;
72 `}
73
74 fun open(file_name: JavaString): NativeInputStream in "Java" `{
75 InputStream stream = null;
76 try {
77 stream = recv.open(file_name);
78 }catch (IOException e) {
79 Log.e("Error while opening " + file_name, e.getMessage());
80 e.printStackTrace();
81 }
82 return stream;
83 `}
84
85 fun open_fd(file_name: JavaString): NativeAssetFileDescriptor in "Java" `{
86 AssetFileDescriptor afd = null;
87 try {
88 afd = recv.openFd(file_name);
89 }catch(IOException e){
90 Log.e("Error while opening " + file_name, e.getMessage());
91 e.printStackTrace();
92 }
93 return afd;
94 `}
95
96 fun open_non_asset_fd(file_name: JavaString): NativeAssetFileDescriptor in "Java" `{
97 AssetFileDescriptor afd = null;
98 try {
99 afd = recv.openNonAssetFd(file_name);
100 }catch(IOException e){
101 Log.e("Error while opening " + file_name, e.getMessage());
102 e.printStackTrace();
103 }
104 return afd;
105 `}
106 end
107
108 # Assets manager using a `NativeAssetManager` to manage android assets
109 class AssetManager
110 # Native asset manager
111 var native_assets_manager: NativeAssetManager
112
113 init(app: App) do self.native_assets_manager = app.assets.new_global_ref
114
115 # Close this asset manager
116 fun close do native_assets_manager.close
117
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)
124 end
125 return nit_array
126 end
127
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)
135 end
136 sys.jni_env.pop_local_frame
137 return nit_array
138 end
139
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)
143 return return_value
144 end
145
146 # Open an asset using it's name and returning a NativeAssetFileDescriptor
147 # `file_name` is
148 fun open_fd(file_name: String): NativeAssetFileDescriptor do
149 var return_value = native_assets_manager.open_fd(file_name.to_java_string)
150 return return_value
151 end
152
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)
156 return return_value
157 end
158
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
164 return return_value
165 end
166
167 # Deallocate the global reference allocated by AssetManager
168 fun destroy do self.native_assets_manager.delete_global_ref
169 end
170
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 `}
174 super JavaObject
175 redef type SELF: NativeResources
176
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); `}
189
190 # HACK for bug #845
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);
195 `}
196 end
197
198 # Resource manager for android resources placed in the `res` folder of your app
199 class ResourcesManager
200 # Native resources
201 var android_resources: NativeResources
202
203 # The name of the app_package
204 var app_package: String
205
206 init(res: NativeResources, app_package: String)
207 do
208 self.android_resources = res.new_global_ref
209 self.app_package = app_package
210 end
211
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
217 return return_value
218 end
219
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
225 return return_value
226 end
227
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
234 return return_value
235 end
236
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
242 return return_value
243 end
244
245
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
251 return return_value
252 end
253
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
260 return return_value
261 end
262
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
268 return return_value
269 end
270
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
276 return return_value
277 end
278
279 # Deallocate global reference allocated by ResourcesManager
280 fun destroy do self.android_resources.delete_global_ref
281 end
282
283 # An android Bitmap, get an instance using the AssetManager or the ResourceManager
284 extern class NativeBitmap in "Java" `{ android.graphics.Bitmap `}
285 super JavaObject
286 redef type SELF: NativeBitmap
287
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); `}
291
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(); `}
297 end
298
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 `}
301 super JavaObject
302 redef type SELF: NativeAssetFileDescriptor
303
304 fun close in "Java" `{
305 try {
306 recv.close();
307 }catch(IOException e){
308 e.printStackTrace();
309 }
310 `}
311 fun create_input_stream: NativeFileInputStream in "Java" `{
312 try {
313 return recv.createInputStream();
314 }catch(IOException e){
315 Log.e("Error creating input_stream", e.getMessage());
316 e.printStackTrace();
317 return null;
318 }
319 `}
320 fun create_output_stream: NativeFileOutputStream in "Java" `{
321 try {
322 return recv.createOutputStream();
323 }catch(IOException e){
324 Log.e("Error creating output stream", e.getMessage());
325 e.printStackTrace();
326 return null;
327 }
328 `}
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(); `}
332
333 fun file_descriptor: NativeFileDescriptor in "Java" `{
334 FileDescriptor fd = recv.getFileDescriptor();
335 if (fd == null) {
336 Log.e("AssetFileDesciptorError", "Can't retrieve the FileDescriptor of this AssetFileDescriptor");
337 }
338 return fd;
339 `}
340
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()); `}
344 end
345
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 `}
349 end
350
351 redef class App
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)
354
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)
357
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(); `}
360
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(); `}
363
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(); `}
366 end