Commit | Line | Data |
---|---|---|
9190adc6 MW |
1 | package uk.org.distorted.tripe; |
2 | ||
3 | import java.io.{File, FileOutputStream, InputStream, IOException}; | |
4 | ||
5 | import android.app.{Activity, Application}; | |
6 | import android.content.Context; import Context.MODE_WORLD_READABLE; | |
7 | import android.content.res.AssetManager; | |
8 | import android.os.Build; import Build.{CPU_ABI, CPU_ABI2}; | |
9 | import android.os.Bundle; | |
10 | import android.util.Log; | |
11 | import android.view.View; | |
12 | ||
13 | import scala.util.control.Breaks; | |
14 | ||
15 | object Setup { | |
16 | private final val TAG = "Setup"; | |
17 | private val BREAK = new Breaks; | |
18 | import BREAK.{breakable, break}; | |
19 | ||
20 | def setup(ctx: Context) { | |
21 | val bindir = ctx.getDir("bin", MODE_WORLD_READABLE); | |
22 | val assets = ctx.getAssets; | |
23 | ||
24 | val abis = | |
25 | try { classOf[Build].getField("SUPPORTED_ABIS").get(null).asInstanceOf[Array[String]] } | |
26 | catch { | |
27 | case _: NoSuchFieldException => Array(CPU_ABI, CPU_ABI2) flatMap { | |
28 | case null | "" => None | |
29 | case s => Some(s) | |
30 | } | |
31 | }; | |
32 | ||
33 | Log.d(TAG, s"abis = ${abis.mkString(", ")}"); | |
34 | Log.d(TAG, s"assets: ${assets.list("bin").mkString(", ")}"); | |
35 | ||
36 | for (abi <- abis) { | |
37 | val binsrc = s"bin/$abi"; | |
38 | for (base <- assets.list(binsrc)) { | |
39 | val prog = new File(bindir, base); | |
40 | if (!prog.exists) try { | |
41 | Log.d(TAG, s"creating $prog..."); | |
42 | val in = assets.open(s"$binsrc/$base"); | |
43 | Log.d(TAG, "opened source..."); | |
44 | val out = new FileOutputStream(prog); | |
45 | Log.d(TAG, "opened target..."); | |
46 | val buf = new Array[Byte](4096); | |
47 | breakable { | |
48 | while (true) { | |
49 | val n = in.read(buf); | |
50 | Log.d(TAG, s"read $n bytes..."); | |
51 | if (n <= 0) break; | |
52 | out.write(buf, 0, n); | |
53 | } | |
54 | } | |
55 | in.close(); | |
56 | out.close(); | |
57 | Log.d(TAG, "set permissions..."); | |
58 | if (!prog.setReadable(true, false) || | |
59 | !prog.setExecutable(true, false)) | |
60 | throw new IOException("failed to set program permissions"); | |
61 | } catch { | |
62 | case exc: IOException => | |
63 | Log.wtf(TAG, "fuck, failed to create prog", exc); | |
64 | } | |
65 | } | |
66 | } | |
67 | Log.d(TAG, "all OK"); | |
68 | } | |
69 | } | |
70 | ||
71 | object ToyActivity { | |
72 | private final val TAG = "ToyActivity"; | |
73 | System.loadLibrary("jni"); | |
74 | @native protected def foo(); | |
75 | } | |
76 | ||
77 | class ToyActivity extends Activity { | |
78 | import ToyActivity._; | |
79 | ||
80 | override protected def onCreate(joy: Bundle) { | |
81 | super.onCreate(joy); | |
82 | Setup.setup(this); | |
83 | setContentView(R.layout.toy); | |
0157de02 | 84 | Log.d(TAG, s"created ${this}"); |
9190adc6 MW |
85 | } |
86 | def clickOk(v: View) { | |
87 | Log.d(TAG, "OK, OK. (Scala was here.)"); | |
88 | foo(); | |
89 | ||
90 | val bindir = getDir("bin", MODE_WORLD_READABLE); | |
91 | Runtime.getRuntime.exec(Array(new File(bindir, "prog").getPath, | |
92 | "testing", "1", "2", "3")); | |
93 | } | |
94 | } |