Merge branch 'master' into polymorphic_extern_classes
[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 end
190
191 # Resource manager for android resources placed in the `res` folder of your app
192 class ResourcesManager
193 # Native resources
194 var android_resources: NativeResources
195
196 # The name of the app_package
197 var app_package: String
198
199 init(res: NativeResources, app_package: String)
200 do
201 self.android_resources = res.new_global_ref
202 self.app_package = app_package
203 end
204
205 # Get a color from resources
206 fun color(name: String): Int do
207 sys.jni_env.push_local_frame(3)
208 var return_value = android_resources.get_color(android_resources.get_identifier(name.to_java_string, "color".to_java_string, app_package.to_java_string))
209 sys.jni_env.pop_local_frame
210 return return_value
211 end
212
213 # Get a `Bool` from resources
214 fun boolean(name: String): Bool do
215 sys.jni_env.push_local_frame(3)
216 var return_value = android_resources.get_boolean(android_resources.get_identifier(name.to_java_string, "bool".to_java_string, app_package.to_java_string))
217 sys.jni_env.pop_local_frame
218 return return_value
219 end
220
221 # Get a dimension from resources
222 # A dimension is specified with a number followed by a unit of measure
223 fun dimension(name: String): Int do
224 sys.jni_env.push_local_frame(3)
225 var return_value = android_resources.get_dimension(android_resources.get_identifier(name.to_java_string, "dimen".to_java_string, app_package.to_java_string))
226 sys.jni_env.pop_local_frame
227 return return_value
228 end
229
230 # Get an `Integer` from resources
231 fun integer(name: String): Int do
232 sys.jni_env.push_local_frame(3)
233 var return_value = android_resources.get_integer(android_resources.get_identifier(name.to_java_string, "integer".to_java_string, app_package.to_java_string))
234 sys.jni_env.pop_local_frame
235 return return_value
236 end
237
238
239 # Get a `String` from resources
240 fun string(name: String): String do
241 sys.jni_env.push_local_frame(3)
242 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
243 sys.jni_env.pop_local_frame
244 return return_value
245 end
246
247 # Get a resource ID from one resource in `res/raw`folder
248 # you may use this to retrieve the id of a sound for example
249 fun raw_id(name: String): Int do
250 sys.jni_env.push_local_frame(3)
251 var return_value = android_resources.get_identifier(name.to_java_string, "raw".to_java_string, app_package.to_java_string)
252 sys.jni_env.pop_local_frame
253 return return_value
254 end
255
256 # Get a drawable from `res/drawable` folder
257 fun drawable(name: String): NativeDrawable do
258 sys.jni_env.push_local_frame(3)
259 var return_value = android_resources.get_drawable(android_resources.get_identifier(name.to_java_string, "drawable".to_java_string, app_package.to_java_string))
260 sys.jni_env.pop_local_frame
261 return return_value
262 end
263
264 # Get and ID from a specific resource in `res/res_type` folder
265 fun other_id(name, res_type: String): Int do
266 sys.jni_env.push_local_frame(3)
267 var return_value = android_resources.get_identifier(name.to_java_string, res_type.to_java_string, app_package.to_java_string)
268 sys.jni_env.pop_local_frame
269 return return_value
270 end
271
272 # Deallocate global reference allocated by ResourcesManager
273 fun destroy do self.android_resources.delete_global_ref
274 end
275
276 # An android Bitmap, get an instance using the AssetManager or the ResourceManager
277 extern class NativeBitmap in "Java" `{ android.graphics.Bitmap `}
278 super JavaObject
279 redef type SELF: NativeBitmap
280
281 # Create a NativeBitmap from a NativeInputStream retrieved with `open` function of the AssetManager
282 # Called by the AssetManager
283 new from_stream(input_stream: NativeInputStream) in "Java" `{ return BitmapFactory.decodeStream(input_stream); `}
284
285 # Create a NativeBitmap using a resource ID and the NativeResources
286 # Called by the ResourceManager
287 new from_resources(res: NativeResources, id: Int) in "Java" `{ return BitmapFactory.decodeResource(res, (int)id); `}
288 fun width: Int in "Java" `{ return recv.getWidth(); `}
289 fun height: Int in "Java" `{ return recv.getHeight(); `}
290 end
291
292 # Android AssetFileDescriptor, can be retrieve by AssetManager and used to load a sound in a SoundPool
293 extern class NativeAssetFileDescriptor in "Java" `{ android.content.res.AssetFileDescriptor `}
294 super JavaObject
295 redef type SELF: NativeAssetFileDescriptor
296
297 fun close in "Java" `{
298 try {
299 recv.close();
300 }catch(IOException e){
301 e.printStackTrace();
302 }
303 `}
304 fun create_input_stream: NativeFileInputStream in "Java" `{
305 try {
306 return recv.createInputStream();
307 }catch(IOException e){
308 Log.e("Error creating input_stream", e.getMessage());
309 e.printStackTrace();
310 return null;
311 }
312 `}
313 fun create_output_stream: NativeFileOutputStream in "Java" `{
314 try {
315 return recv.createOutputStream();
316 }catch(IOException e){
317 Log.e("Error creating output stream", e.getMessage());
318 e.printStackTrace();
319 return null;
320 }
321 `}
322 fun describe_contents: Int in "Java" `{ return (int)recv.describeContents(); `}
323 fun declared_length: Int in "Java" `{ return (int)recv.getDeclaredLength(); `}
324 # fun extras: Bundle in "Java" `{ return recv.getExtras(); `}
325
326 fun file_descriptor: NativeFileDescriptor in "Java" `{
327 FileDescriptor fd = recv.getFileDescriptor();
328 if (fd == null) {
329 Log.e("AssetFileDesciptorError", "Can't retrieve the FileDescriptor of this AssetFileDescriptor");
330 }
331 return fd;
332 `}
333
334 fun length: Int in "Java" `{ return (int)recv.getLength(); `}
335 fun start_offset: Int in "Java" `{ return (int)recv.getStartOffset(); `}
336 redef fun to_s: String import JavaString.to_s in "Java" `{ return JavaString_to_s(recv.toString()); `}
337 end
338
339 # Native class representing something drawable, can be retrieved from the resources
340 # will be used by the GUI
341 extern class NativeDrawable in "Java" `{ android.graphics.drawable.Drawable `}
342 end
343
344 redef class App
345 # Resource Manager used to manage resources placed in the `res` folder of the app
346 fun resource_manager: ResourcesManager is cached do return new ResourcesManager(self.resources, self.package_name.to_s)
347
348 # Assets Manager used to manage resources placed in the `assets` folder of the app
349 fun asset_manager: AssetManager is cached do return new AssetManager(self)
350
351 # Get the native AssetsManager of the application, used to initialize the nit's AssetManager
352 private fun assets: NativeAssetManager import native_activity in "Java" `{ return App_native_activity(recv).getAssets(); `}
353
354 # Get the package name of the application
355 private fun package_name: JavaString import native_activity in "Java" `{ return App_native_activity(recv).getPackageName(); `}
356
357 # Get the native ResourceManager of the application, used to initialize the nit's ResourceManager
358 private fun resources: NativeResources import native_activity in "Java" `{ return App_native_activity(recv).getResources(); `}
359 end