src: remove remaining references of subclasses on AMethPropdef
[nit.git] / src / android_platform.nit
index 60d4ca0..e562918 100644 (file)
@@ -19,6 +19,8 @@ module android_platform
 
 import platform
 import abstract_compiler
+import common_ffi
+import android_annotations
 
 redef class ToolContext
        redef fun platform_from_name(name)
@@ -55,6 +57,8 @@ end
 class AndroidPlatform
        super Platform
 
+       redef fun supports_libunwind do return false
+
        redef fun toolchain(toolcontext) do return new AndroidToolchain(toolcontext)
 end
 
@@ -67,24 +71,40 @@ class AndroidToolchain
        do
                var normal_compile_dir = super
                android_project_root = normal_compile_dir
-               return "{normal_compile_dir}/jni/"
+               return "{normal_compile_dir}/jni/nit_compile/"
        end
 
        redef fun write_files(compiler, compile_dir, cfiles)
        do
-               var app_name = compiler.mainmodule.name
-               var app_package = "org.nitlanguage.{app_name}"
-               var app_version = "0.1"
+               var project = toolcontext.modelbuilder.android_project_for(compiler.mainmodule)
+               var short_project_name = compiler.mainmodule.name
+
+               var app_name = project.name
+               if app_name == null then app_name = compiler.mainmodule.name
+               print app_name
+
+               var app_package = project.java_package
+               if app_package == null then app_package = "org.nitlanguage.{short_project_name}"
+
+               var app_version = project.version
+               if app_version == null then app_version = "1.0"
 
-               var args = ["android", "-s", "create", "project", "--name", app_name,
-                       "--target", "android-10", "--path", android_project_root,
-                       "--package", app_package, "--activity", app_name]
+               var args = ["android", "-s",
+                       "create", "project",
+                       "--name", short_project_name,
+                       "--target", "android-10",
+                       "--path", android_project_root,
+                       "--package", app_package,
+                       "--activity", short_project_name]
                toolcontext.exec_and_check(args)
 
                # create compile_dir
                var dir = "{android_project_root}/jni/"
                if not dir.file_exists then dir.mkdir
 
+               dir = compile_dir
+               if not dir.file_exists then dir.mkdir
+
                # compile normal C files
                super(compiler, compile_dir, cfiles)
 
@@ -93,10 +113,18 @@ class AndroidToolchain
                        if f isa ExternCFile then cfiles.add(f.filename.basename(""))
                end
 
-               ### generate makefile into "{compile_dir}/Android.mk"
-               if not dir.file_exists then dir.mkdir
+               ## Generate delagating makefile
+               dir = "{android_project_root}/jni/"
                var file = new OFStream.open("{dir}/Android.mk")
                file.write """
+include $(call all-subdir-makefiles)
+"""
+               file.close
+
+               ### generate makefile into "{compile_dir}/Android.mk"
+               dir = compile_dir
+               file = new OFStream.open("{dir}/Android.mk")
+               file.write """
 LOCAL_PATH := $(call my-dir)
 include $(CLEAR_VARS)
 
@@ -105,7 +133,7 @@ LOCAL_MODULE    := main
 LOCAL_SRC_FILES := \\
 {{{cfiles.join(" \\\n")}}}
 LOCAL_LDLIBS    := -llog -landroid -lEGL -lGLESv1_CM -lz
-LOCAL_STATIC_LIBRARIES := android_native_app_glue
+LOCAL_STATIC_LIBRARIES := android_native_app_glue png
 
 include $(BUILD_SHARED_LIBRARY)
 
@@ -122,19 +150,23 @@ $(call import-module,android/native_app_glue)
         package="{{{app_package}}}"
         android:versionCode="1"
         android:versionName="{{{app_version}}}"
-       android:debuggable="true">
+        android:debuggable="true">
 
     <!-- This is the platform API where NativeActivity was introduced. -->
     <uses-sdk android:minSdkVersion="9" />
 
-    <!-- This .apk has no Java code itself, so set hasCode to false. -->
-    <application android:label="@string/app_name" android:hasCode="false" android:debuggable="true">
+    <application
+               android:label="@string/app_name"
+               android:hasCode="true"
+               android:debuggable="true">
 
         <!-- Our activity is the built-in NativeActivity framework class.
              This will take care of integrating with our NDK code. -->
         <activity android:name="android.app.NativeActivity"
                 android:label="@string/app_name"
-                android:configChanges="orientation|keyboardHidden">
+                android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
+                android:configChanges="orientation|keyboardHidden"
+                android:screenOrientation="portrait">
             <!-- Tell NativeActivity the name of or .so -->
             <meta-data android:name=\"{{{app_package}}}\"
                     android:value=\"{{{app_name}}}\" />
@@ -143,8 +175,13 @@ $(call import-module,android/native_app_glue)
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+
+{{{project.manifest_application_lines.join("\n")}}}
+
     </application>
 
+{{{project.manifest_lines.join("\n")}}}
+
 </manifest> 
 <!-- END_INCLUDE(manifest) -->
 """
@@ -161,6 +198,34 @@ $(call import-module,android/native_app_glue)
     <string name="app_name">{{{app_name}}}</string>
 </resources>"""
                file.close
+
+               ### Link to png sources
+               # libpng is not available on Android NDK
+               # FIXME make obtionnal when we have alternatives to mnit
+               var nit_dir = toolcontext.nit_dir
+               var share_dir =  "{nit_dir}/share/"
+               if nit_dir == null or not share_dir.file_exists then
+                       print "Android project error: Nit share directory not found, please use the environment variable NIT_DIR"
+                       exit 1
+               end
+               share_dir = share_dir.realpath
+               var target_png_dir = "{android_project_root}/jni/png"
+               if not target_png_dir.file_exists then
+                       toolcontext.exec_and_check(["ln", "-s", "{share_dir}/png/", target_png_dir])
+               end
+
+               ### Link to assets (for mnit and others)
+               # This will be accessed from `android_project_root`
+               var mainmodule_dir = compiler.mainmodule.location.file.filename.dirname
+               var assets_dir = "{mainmodule_dir}/../assets"
+               if not assets_dir.file_exists then assets_dir = "{mainmodule_dir}/assets"
+               if assets_dir.file_exists then
+                       assets_dir = assets_dir.realpath
+                       var target_assets_dir = "{android_project_root}/assets"
+                       if not target_assets_dir.file_exists then
+                               toolcontext.exec_and_check(["ln", "-s", assets_dir, target_assets_dir])
+                       end
+               end
        end
 
        redef fun write_makefile(compiler, compile_dir, cfiles)
@@ -175,5 +240,21 @@ $(call import-module,android/native_app_glue)
 
                # Generate the apk
                toolcontext.exec_and_check(["ant", "-q", "debug", "-f", android_project_root+"/build.xml"])
+
+               # Move the apk to the target
+               var outname = toolcontext.opt_output.value
+               if outname == null then outname = "{compiler.mainmodule.name}.apk"
+               toolcontext.exec_and_check(["mv", "{android_project_root}/bin/{compiler.mainmodule.name}-debug.apk", outname])
+       end
+end
+
+redef class JavaClassTemplate
+       redef fun write_to_files(compdir)
+       do
+               var jni_path = "jni/nit_compile/"
+               if compdir.has_suffix(jni_path) then
+                       var path = "{compdir.substring(0, compdir.length-jni_path.length)}/src/"
+                       return super(path)
+               else return super
        end
 end