add activity support for android sample (#43504)
authorEgor Bogatov <egorbo@gmail.com>
Tue, 20 Oct 2020 06:46:15 +0000 (09:46 +0300)
committerGitHub <noreply@github.com>
Tue, 20 Oct 2020 06:46:15 +0000 (09:46 +0300)
src/mono/netcore/sample/Android/AndroidSampleApp.csproj
src/mono/netcore/sample/Android/Makefile
tools-local/tasks/mobile.tasks/AndroidAppBuilder/ApkBuilder.cs
tools-local/tasks/mobile.tasks/AndroidAppBuilder/Templates/MainActivity.java
tools-local/tasks/mobile.tasks/AndroidAppBuilder/Templates/MonoRunner.java

index 3eddf9f..ca078c2 100644 (file)
@@ -56,7 +56,8 @@
     <Message Condition="'$(DeployAndRun)' == 'true'" Importance="High" Text="Uninstalling apk (ignore errors if any):"/>
     <Exec Condition="'$(DeployAndRun)' == 'true'" Command="$(AdbTool) uninstall net.dot.HelloAndroid" ContinueOnError="WarnAndContinue" />
     <Exec Condition="'$(DeployAndRun)' == 'true'" Command="$(AdbTool) install $(ApkDir)/bin/HelloAndroid.apk" />
-    <Exec Condition="'$(DeployAndRun)' == 'true'" Command="$(AdbTool) shell am instrument -w net.dot.HelloAndroid/net.dot.MonoRunner" />
+    <Exec Condition="'$(DeployAndRun)' == 'true' and '$(RunActivity)' != 'true'" Command="$(AdbTool) shell am instrument -w net.dot.HelloAndroid/net.dot.MonoRunner" />
+    <Exec Condition="'$(DeployAndRun)' == 'true' and '$(RunActivity)' == 'true'" Command="$(AdbTool) shell am start -n net.dot.HelloAndroid/net.dot.MainActivity" />
     <Exec Condition="'$(DeployAndRun)' == 'true'" Command="$(AdbTool) logcat -d -s DOTNET" />
   </Target>
 
index 05ffeb2..02aab8f 100644 (file)
@@ -12,7 +12,8 @@ run:
        /p:TargetArchitecture=$(MONO_ARCH) \
        /p:TargetOS=Android \
        /p:Configuration=$(MONO_CONFIG) \
-       /p:DeployAndRun=true
+       /p:DeployAndRun=true \
+       /p:RunActivity=false
 
 clean:
        rm -rf ../../../../../artifacts/bin/AndroidSampleApp
index 919cf0a..5231251 100644 (file)
@@ -180,7 +180,8 @@ public class ApkBuilder
         string packageId = $"net.dot.{ProjectName}";
 
         File.WriteAllText(Path.Combine(javaSrcFolder, "MainActivity.java"),
-            Utils.GetEmbeddedResource("MainActivity.java"));
+            Utils.GetEmbeddedResource("MainActivity.java")
+                .Replace("%EntryPointLibName%", Path.GetFileName(entryPointLib)));
 
         string monoRunner = Utils.GetEmbeddedResource("MonoRunner.java")
             .Replace("%EntryPointLibName%", Path.GetFileName(entryPointLib));
index e864edc..e84eec3 100644 (file)
@@ -3,9 +3,13 @@
 
 package net.dot;
 
-import android.app.AlertDialog;
 import android.app.Activity;
 import android.os.Bundle;
+import android.os.Handler;
+import android.os.Looper;
+import android.widget.RelativeLayout;
+import android.widget.TextView;
+import android.graphics.Color;
 
 public class MainActivity extends Activity
 {
@@ -14,8 +18,35 @@ public class MainActivity extends Activity
     {
         super.onCreate(savedInstanceState);
 
-        AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
-        dlgAlert.setMessage("Use `adb shell am instrument -w " + getApplicationContext().getPackageName() + "net.dot.MonoRunner` to run the tests.");
-        dlgAlert.create().show();
+        final String entryPointLibName = "%EntryPointLibName%";
+        final TextView textView = new TextView(this);
+        textView.setTextSize(20);
+
+        RelativeLayout rootLayout = new RelativeLayout(this);
+        RelativeLayout.LayoutParams tvLayout =
+                new RelativeLayout.LayoutParams(
+                        RelativeLayout.LayoutParams.WRAP_CONTENT,
+                        RelativeLayout.LayoutParams.WRAP_CONTENT);
+
+        tvLayout.addRule(RelativeLayout.CENTER_HORIZONTAL);
+        tvLayout.addRule(RelativeLayout.CENTER_VERTICAL);
+        rootLayout.addView(textView, tvLayout);
+        setContentView(rootLayout);
+
+        if (entryPointLibName == "" || entryPointLibName.startsWith("%")) {
+            textView.setText("ERROR: EntryPointLibName was not set.");
+            return;
+        } else {
+            textView.setText("Running " + entryPointLibName + "...");
+        }
+
+        final Activity ctx = this;
+        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
+            @Override
+            public void run() {
+                int retcode = MonoRunner.initialize(entryPointLibName, ctx);
+                textView.setText("Mono Runtime returned: " + retcode);
+            }
+        }, 1000);
     }
 }
index 1181052..b22be8c 100644 (file)
@@ -27,13 +27,12 @@ import java.util.zip.ZipInputStream;
 
 public class MonoRunner extends Instrumentation
 {
-    static MonoRunner inst;
-    static String entryPointLibName = "%EntryPointLibName%";
-
     static {
         System.loadLibrary("monodroid");
     }
 
+    static String entryPointLibName = "%EntryPointLibName%";
+
     @Override
     public void onCreate(Bundle arguments) {
         if (arguments != null) {
@@ -47,42 +46,46 @@ public class MonoRunner extends Instrumentation
         start();
     }
 
-    @Override
-    public void onStart() {
-        super.onStart();
+    private static String getDocsDir(Context ctx) {
+        File docsPath  = ctx.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
+        if (docsPath == null) {
+            docsPath = ctx.getCacheDir();
+        }
+        return docsPath.getAbsolutePath();
+    }
 
-        MonoRunner.inst = this;
-        Context context = getContext();
+    public static int initialize(String entryPointLibName, Context context) {
         String filesDir = context.getFilesDir().getAbsolutePath();
         String cacheDir = context.getCacheDir().getAbsolutePath();
-        File docsPath  = context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
-        if (docsPath == null) {
-            docsPath = context.getCacheDir();
-        }
-        String docsDir = docsPath.getAbsolutePath();
+        String docsDir = getDocsDir(context);
 
         // unzip libs and test files to filesDir
         unzipAssets(context, filesDir, "assets.zip");
 
+        Log.i("DOTNET", "initRuntime, entryPointLibName=" + entryPointLibName);
+        return initRuntime(filesDir, cacheDir, docsDir, entryPointLibName);
+    }
+
+    @Override
+    public void onStart() {
+        super.onStart();
+
         if (entryPointLibName == "") {
             Log.e("DOTNET", "Missing entryPointLibName argument, pass '-e entryPointLibName <name.dll>' to adb to specify which program to run.");
             finish(1, null);
             return;
         }
-
-        Log.i("DOTNET", "initRuntime");
-        int retcode = initRuntime(filesDir, cacheDir, docsDir, entryPointLibName);
+        int retcode = initialize(entryPointLibName, getContext());
         runOnMainSync(new Runnable() {
             public void run() {
                 Bundle result = new Bundle();
                 result.putInt("return-code", retcode);
 
                 // Xharness cli expects "test-results-path" with test results
-                File testResults = new File(docsDir + "/testResults.xml");
+                File testResults = new File(getDocsDir(getContext()) + "/testResults.xml");
                 if (testResults.exists()) {
                     result.putString("test-results-path", testResults.getAbsolutePath());
                 }
-
                 finish(retcode, result);
             }
         });
@@ -122,5 +125,5 @@ public class MonoRunner extends Instrumentation
         }
     }
 
-    native int initRuntime(String libsDir, String cacheDir, String docsDir, String entryPointLibName);
+    static native int initRuntime(String libsDir, String cacheDir, String docsDir, String entryPointLibName);
 }