82ae3d1775899fa486363aae3783482d72c3ccb4
[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
50 fun close in "Java" `{ recv.close(); `}
51
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);
56 }
57 return arr;
58 `}
59
60 fun list(path: JavaString): Array[JavaString] import Array[JavaString], Array[JavaString].add in "Java" `{
61 int arr = new_Array_of_JavaString();
62 try {
63 for (String s : recv.list(path)) {
64 Array_of_JavaString_add(arr, s);
65 }
66 }catch (IOException e) {
67 Log.e("Error retrieving the list of assets at 'path' ", e.getMessage());
68 e.printStackTrace();
69 }
70 return arr;
71 `}
72
73 fun open(file_name: JavaString): NativeInputStream in "Java" `{
74 InputStream stream = null;
75 try {
76 stream = recv.open(file_name);
77 }catch (IOException e) {
78 Log.e("Error while opening " + file_name, e.getMessage());
79 e.printStackTrace();
80 }
81 return stream;
82 `}
83
84 fun open_fd(file_name: JavaString): NativeAssetFileDescriptor in "Java" `{
85 AssetFileDescriptor afd = null;
86 try {
87 afd = recv.openFd(file_name);
88 }catch(IOException e){
89 Log.e("Error while opening " + file_name, e.getMessage());
90 e.printStackTrace();
91 }
92 return afd;
93 `}
94
95 fun open_non_asset_fd(file_name: JavaString): NativeAssetFileDescriptor in "Java" `{
96 AssetFileDescriptor afd = null;
97 try {
98 afd = recv.openNonAssetFd(file_name);
99 }catch(IOException e){
100 Log.e("Error while opening " + file_name, e.getMessage());
101 e.printStackTrace();
102 }
103 return afd;
104 `}
105 end
106
107 # Assets manager using a `NativeAssetManager` to manage android assets
108 class AssetManager
109 # Native asset manager
110 var native_assets_manager: NativeAssetManager
111
112 init(app: App) do self.native_assets_manager = app.assets.new_global_ref
113
114 # Close this asset manager
115 fun close do native_assets_manager.close
116
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)
123 end
124 return nit_array
125 end
126
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)
134 end
135 sys.jni_env.pop_local_frame
136 return nit_array
137 end
138
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)
142 return return_value
143 end
144
145 # Open an asset using it's name and returning a NativeAssetFileDescriptor
146 # `file_name` is
147 fun open_fd(file_name: String): NativeAssetFileDescriptor do
148 var return_value = native_assets_manager.open_fd(file_name.to_java_string)
149 return return_value
150 end
151
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)
155 return return_value
156 end
157
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
163 return return_value
164 end
165
166 # Deallocate the global reference allocated by AssetManager
167 fun destroy do self.native_assets_manager.delete_global_ref
168 end
169
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 `}
173 super JavaObject
174
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); `}
187
188 # HACK for bug #845
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);
193 `}
194 end
195
196 # Resource manager for android resources placed in the `res` folder of your app
197 class ResourcesManager
198 # Native resources
199 var android_resources: NativeResources
200
201 # The name of the app_package
202 var app_package: String
203
204 init(res: NativeResources, app_package: String)
205 do
206 self.android_resources = res.new_global_ref
207 self.app_package = app_package
208 end
209
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
215 return return_value
216 end
217
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
223 return return_value
224 end
225
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
232 return return_value
233 end
234
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
240 return return_value
241 end
242
243
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
249 return return_value
250 end
251
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
258 return return_value
259 end
260
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
266 return return_value
267 end
268
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
274 return return_value
275 end
276
277 # Deallocate global reference allocated by ResourcesManager
278 fun destroy do self.android_resources.delete_global_ref
279 end
280
281 # An android Bitmap, get an instance using the AssetManager or the ResourceManager
282 extern class NativeBitmap in "Java" `{ android.graphics.Bitmap `}
283 super JavaObject
284
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); `}
288
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(); `}
294 end
295
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 `}
298 super JavaObject
299
300 fun close in "Java" `{
301 try {
302 recv.close();
303 }catch(IOException e){
304 e.printStackTrace();
305 }
306 `}
307 fun create_input_stream: NativeFileInputStream in "Java" `{
308 try {
309 return recv.createInputStream();
310 }catch(IOException e){
311 Log.e("Error creating input_stream", e.getMessage());
312 e.printStackTrace();
313 return null;
314 }
315 `}
316 fun create_output_stream: NativeFileOutputStream in "Java" `{
317 try {
318 return recv.createOutputStream();
319 }catch(IOException e){
320 Log.e("Error creating output stream", e.getMessage());
321 e.printStackTrace();
322 return null;
323 }
324 `}
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(); `}
328
329 fun file_descriptor: NativeFileDescriptor in "Java" `{
330 FileDescriptor fd = recv.getFileDescriptor();
331 if (fd == null) {
332 Log.e("AssetFileDesciptorError", "Can't retrieve the FileDescriptor of this AssetFileDescriptor");
333 }
334 return fd;
335 `}
336
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()); `}
340 end
341
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 `}
345 end
346
347 redef class App
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)
350
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)
353
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(); `}
356
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(); `}
359
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(); `}
362 end