|
| 1 | +package software.amazon.awssdk.iotsamples |
| 2 | + |
| 3 | +import androidx.appcompat.app.AppCompatActivity |
| 4 | +import android.os.Bundle |
| 5 | +import android.view.View |
| 6 | +import android.widget.AdapterView |
| 7 | +import android.widget.ArrayAdapter |
| 8 | +import android.widget.Spinner |
| 9 | +import android.widget.TextView |
| 10 | +import java.io.FileNotFoundException |
| 11 | +import java.io.FileOutputStream |
| 12 | +import java.io.OutputStream |
| 13 | +import java.io.PrintStream |
| 14 | +import java.lang.Exception |
| 15 | +import kotlin.concurrent.thread |
| 16 | + |
| 17 | +val SAMPLES = mapOf( |
| 18 | + "Publish/Subscribe Sample" to "pubsub.PubSub", |
| 19 | + "Jobs Client Sample" to "jobs.JobsSample", |
| 20 | + "Shadow Client Sample" to "shadow.ShadowSample", |
| 21 | + "Publish/Subscribe Load Test" to "pubsubstress.PubSubStress" |
| 22 | +) |
| 23 | + |
| 24 | +class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener { |
| 25 | + |
| 26 | + private class StreamTee(val source: OutputStream, val log: (message: String) -> Unit) |
| 27 | + : PrintStream(source, true) { |
| 28 | + init { |
| 29 | + if (source == System.out) { |
| 30 | + System.setOut(this) |
| 31 | + } else if (source == System.err) { |
| 32 | + System.setErr(this) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + override fun write(buf: ByteArray, off: Int, len: Int) { |
| 37 | + source.write(buf, off, len) |
| 38 | + log(String(buf.slice(IntRange(off, off+len-1)).toByteArray())) |
| 39 | + } |
| 40 | + |
| 41 | + override fun write(b: ByteArray) { |
| 42 | + source.write(b) |
| 43 | + log(String(b)) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private val stdout : StreamTee; |
| 48 | + private val stderr : StreamTee; |
| 49 | + |
| 50 | + private var console: TextView? = null; |
| 51 | + private var sampleSelect: Spinner? = null; |
| 52 | + |
| 53 | + init { |
| 54 | + stdout = StreamTee(System.out) { writeToConsole(it) } |
| 55 | + stderr = StreamTee(System.err) { writeToConsole(it) } |
| 56 | + } |
| 57 | + |
| 58 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 59 | + super.onCreate(savedInstanceState) |
| 60 | + setContentView(R.layout.activity_main) |
| 61 | + |
| 62 | + console = findViewById<TextView>(R.id.console) |
| 63 | + console?.isEnabled = false |
| 64 | + |
| 65 | + sampleSelect = findViewById<Spinner>(R.id.sampleSelect); |
| 66 | + |
| 67 | + val samples = SAMPLES.keys.toMutableList() |
| 68 | + samples.add(0, "Please select a sample") |
| 69 | + val samplesAdapter = ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, samples) |
| 70 | + sampleSelect?.adapter = samplesAdapter |
| 71 | + |
| 72 | + sampleSelect?.onItemSelectedListener = this |
| 73 | + } |
| 74 | + |
| 75 | + private fun clearConsole() { |
| 76 | + runOnUiThread() { |
| 77 | + console?.text = "" |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private fun writeToConsole(message: String) { |
| 82 | + runOnUiThread() { |
| 83 | + console?.append(message) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private fun onSampleComplete() { |
| 88 | + runOnUiThread() { |
| 89 | + sampleSelect?.isEnabled = true |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private fun assetContents(assetName: String) : String { |
| 94 | + resources.assets.open(assetName).use {res -> |
| 95 | + val bytes = ByteArray(res.available()) |
| 96 | + res.read(bytes) |
| 97 | + return String(bytes).trim() |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private fun assetContentsOr(assetName: String, defaultValue: String) : String { |
| 102 | + return try { |
| 103 | + assetContents(assetName) |
| 104 | + } catch (fnf: FileNotFoundException) { |
| 105 | + defaultValue |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private fun runSample(name: String) { |
| 110 | + val classLoader = Thread.currentThread().contextClassLoader |
| 111 | + val sampleClass = classLoader.loadClass(name); |
| 112 | + if (sampleClass == null) { |
| 113 | + clearConsole() |
| 114 | + writeToConsole("Could not find sample '${name}'") |
| 115 | + } |
| 116 | + |
| 117 | + thread(name="sample_runner", contextClassLoader = classLoader) { |
| 118 | + // find resources, copy them into the cache so samples can get paths to them |
| 119 | + val resourceNames = listOf("AmazonRootCA1.pem", "certificate.pem", "privatekey.pem") |
| 120 | + val resourceMap = HashMap<String, String>() |
| 121 | + for (resourceName in resourceNames) { |
| 122 | + resources.assets.open(resourceName).use { res -> |
| 123 | + val cachedName = "${externalCacheDir}/${resourceName}" |
| 124 | + FileOutputStream(cachedName).use { cachedRes -> |
| 125 | + res.copyTo(cachedRes) |
| 126 | + } |
| 127 | + |
| 128 | + resourceMap[resourceName] = cachedName |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + val args = mutableListOf( |
| 133 | + "--endpoint", assetContents("endpoint.txt"), |
| 134 | + "--rootca", resourceMap["AmazonRootCA1.pem"], |
| 135 | + "--cert", resourceMap["certificate.pem"], |
| 136 | + "--key", resourceMap["privatekey.pem"], |
| 137 | + "--port", assetContentsOr("port.txt", "8883"), |
| 138 | + "--clientId", assetContentsOr("clientId.txt", "android-java-crt-test") |
| 139 | + ) |
| 140 | + if (name == "pubsub.PubSub") { |
| 141 | + args.addAll(arrayOf( |
| 142 | + "--topic", assetContentsOr("topic.txt", "/samples/test"), |
| 143 | + "--message", assetContentsOr("message.txt", "Hello World From Android"))) |
| 144 | + } else if (name in arrayOf("jobs.JobsSample", "shadow.ShadowSample")) { |
| 145 | + args.addAll(arrayOf( |
| 146 | + "--thingName", assetContentsOr("thingName.txt", "aws-iot-unit-test") |
| 147 | + )) |
| 148 | + } |
| 149 | + val main = sampleClass.getMethod("main", Array<String>::class.java) |
| 150 | + try { |
| 151 | + main.invoke(null, args.toTypedArray()) |
| 152 | + } catch (e: Exception) { |
| 153 | + writeToConsole(e.toString()) |
| 154 | + } |
| 155 | + onSampleComplete(); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + override fun onNothingSelected(p0: AdapterView<*>?) { |
| 160 | + clearConsole() |
| 161 | + writeToConsole("Please select a sample above") |
| 162 | + } |
| 163 | + |
| 164 | + override fun onItemSelected(parent: AdapterView<*>?, view: View?, pos: Int, id: Long) { |
| 165 | + clearConsole() |
| 166 | + val sampleName = parent?.getItemAtPosition(pos).toString() |
| 167 | + val sampleClassName = SAMPLES[sampleName] |
| 168 | + if (sampleClassName != null) { |
| 169 | + return runSample(sampleClassName) |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments