From 612baf18264c13e8746c7e4d7be3ea6cb8908906 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Wed, 30 Dec 2015 16:26:43 -0500 Subject: [PATCH] lib/android: implement `TextAsset` MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/android/android.nit | 1 + lib/android/assets.nit | 70 +++++++++++++++++++++++++++++++++++++++++++++++ lib/android/ui/ui.nit | 1 + lib/java/io.nit | 11 ++++++++ 4 files changed, 83 insertions(+) create mode 100644 lib/android/assets.nit diff --git a/lib/android/android.nit b/lib/android/android.nit index f083dd9..b5aa04f 100644 --- a/lib/android/android.nit +++ b/lib/android/android.nit @@ -24,6 +24,7 @@ import platform import native_app_glue import dalvik private import log +private import assets redef class App redef fun init_window diff --git a/lib/android/assets.nit b/lib/android/assets.nit new file mode 100644 index 0000000..84ec235 --- /dev/null +++ b/lib/android/assets.nit @@ -0,0 +1,70 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Implementation of `app::assets` +# +# This module is a client of `assets_and_resources` as the latter +# covers the services of Android. +module assets + +intrude import assets_and_resources + +redef class TextAsset + redef fun load + do + jni_env.push_local_frame 8 + + var manager = app.asset_manager + var stream = manager.open(path) + if stream.is_java_null then + self.error = new Error("Failed to open asset at '{path}'") + jni_env.pop_local_frame + self.to_s = "" + return "" + end + + var text = stream.read_all + stream.close + if text.is_java_null then + self.error = new Error("Failed to read content of asset file at '{path}'") + jni_env.pop_local_frame + self.to_s = "" + return "" + end + + var content = text.to_s + jni_env.pop_local_frame + self.to_s = content + return content + end +end + +redef class NativeInputStream + + # Read and return all the content of this stream + private fun read_all: JavaString in "Java" `{ + // `available` returns the n of bytes currently available, not the total. + // This may cause problems in the future with large asset files. + try { + int size = self.available(); + byte[] bytes = new byte[size]; + self.read(bytes, 0, size); + + return new String(bytes); + } catch (java.lang.Exception exception) { + exception.printStackTrace(); + return null; + } + `} +end diff --git a/lib/android/ui/ui.nit b/lib/android/ui/ui.nit index 656e209..90c513b 100644 --- a/lib/android/ui/ui.nit +++ b/lib/android/ui/ui.nit @@ -27,6 +27,7 @@ import nit_activity import app::ui private import data_store +import assets redef class Control # The Android element used to implement `self` diff --git a/lib/java/io.nit b/lib/java/io.nit index 40d3f02..c13abe0 100644 --- a/lib/java/io.nit +++ b/lib/java/io.nit @@ -168,4 +168,15 @@ extern class NativeInputStream in "Java" `{ java.io.InputStream `} e.printStackTrace(); } `} + + # HACK for bug #845 + redef fun new_global_ref import sys, Sys.jni_env `{ + Sys sys = NativeInputStream_sys(self); + JNIEnv *env = Sys_jni_env(sys); + return (*env)->NewGlobalRef(env, self); + `} + + redef fun pop_from_local_frame_with_env(jni_env) `{ + return (*jni_env)->PopLocalFrame(jni_env, self); + `} end -- 1.7.9.5