android: search for an icon in the res folder
[nit.git] / src / android_platform.nit
index 79be326..e3afc71 100644 (file)
@@ -67,14 +67,14 @@ class AndroidToolchain
                var app_version = project.version
                if app_version == null then app_version = "1.0"
 
-               var app_min_sdk = project.min_sdk
-               if app_min_sdk == null then app_min_sdk = 10
+               var app_min_api = project.min_api
+               if app_min_api == null then app_min_api = 10
 
-               var app_target_sdk = project.target_sdk
-               if app_target_sdk == null then app_target_sdk = app_min_sdk
+               var app_target_api = project.target_api
+               if app_target_api == null then app_target_api = app_min_api
 
-               var app_max_sdk = ""
-               if project.max_sdk != null then app_max_sdk = "android:maxSdkVersion=\"{app_max_sdk}\""
+               var app_max_api = ""
+               if project.max_api != null then app_max_api = "android:maxSdkVersion=\"{project.max_api.as(not null)}\""
 
                # Clear the previous android project, so there is no "existing project warning"
                # or conflict between Java files of different projects
@@ -83,7 +83,7 @@ class AndroidToolchain
                var args = ["android", "-s",
                        "create", "project",
                        "--name", short_project_name,
-                       "--target", "android-{app_target_sdk}",
+                       "--target", "android-{app_target_api}",
                        "--path", android_project_root,
                        "--package", app_package,
                        "--activity", short_project_name]
@@ -104,6 +104,22 @@ class AndroidToolchain
                        if f isa ExternCFile then cfiles.add(f.filename.basename(""))
                end
 
+               # Is there an icon?
+               var resolutions = ["ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"]
+               var icon_available = false
+               for res in resolutions do
+                       var path = "res/drawable-{res}/icon.png"
+                       if path.file_exists then
+                               icon_available = true
+                               break
+                       end
+               end
+
+               var icon_declaration
+               if icon_available then
+                       icon_declaration = "android:icon=\"@drawable/icon\""
+               else icon_declaration = ""
+
                ## Generate delagating makefile
                dir = "{android_project_root}/jni/"
                """
@@ -138,10 +154,10 @@ $(call import-module,android/native_app_glue)
         android:versionName="{{{app_version}}}">
 
     <!-- This is the platform API where NativeActivity was introduced. -->
-    <uses-sdk 
-        android:minSdkVersion="{{{app_min_sdk}}}" 
-        android:targetSdkVersion="{{{app_target_sdk}}}" 
-        {{{app_max_sdk}}} /> 
+    <uses-sdk
+        android:minSdkVersion="{{{app_min_api}}}"
+        android:targetSdkVersion="{{{app_target_api}}}"
+        {{{app_max_api}}} />
 
     <application
                android:label="@string/app_name"
@@ -154,7 +170,8 @@ $(call import-module,android/native_app_glue)
                 android:label="@string/app_name"
                 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                 android:configChanges="orientation|keyboardHidden"
-                android:screenOrientation="portrait">
+                android:screenOrientation="portrait"
+                {{{icon_declaration}}}>
             <!-- Tell NativeActivity the name of or .so -->
             <meta-data android:name=\"{{{app_package}}}\"
                     android:value=\"{{{app_name}}}\" />
@@ -170,7 +187,7 @@ $(call import-module,android/native_app_glue)
 
 {{{project.manifest_lines.join("\n")}}}
 
-</manifest> 
+</manifest>
 <!-- END_INCLUDE(manifest) -->
                """.write_to_file("{dir}/AndroidManifest.xml")
 
@@ -206,7 +223,7 @@ $(call import-module,android/native_app_glue)
                                toolcontext.exec_and_check(["ln", "-s", assets_dir, target_assets_dir], "Android project error")
                        end
                end
-               
+
                ### copy resources  (for android)
                # This will be accessed from `android_project_root`
                var res_dir
@@ -218,14 +235,17 @@ $(call import-module,android/native_app_glue)
                        res_dir = "res"
                end
                if res_dir.file_exists then
+                       # copy the res folder to .nit_compile
                        res_dir = res_dir.realpath
                        var target_res_dir = "{android_project_root}"
-                       if target_res_dir.file_exists then
-                               # copy the res folder to .nit_compile
-                               toolcontext.exec_and_check(["cp", "-R", res_dir, target_res_dir], "Android project error")
-                       end
+                       toolcontext.exec_and_check(["cp", "-R", res_dir, target_res_dir], "Android project error")
+               else
+                       # Create our own custom `res/values/string.xml` with the App name
+"""<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <string name="app_name">{{{app_name}}}</string>
+</resources>""".write_to_file "{dir}/res/values/strings.xml"
                end
-
        end
 
        redef fun write_makefile(compiler, compile_dir, cfiles)