-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathExampleActivity.kt
107 lines (90 loc) · 3.54 KB
/
ExampleActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.alexii.j2v8debugging.sample
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import com.alexii.j2v8debugger.StethoHelper
import com.alexii.j2v8debugger.V8Debugger
import com.alexii.j2v8debugging.R
import com.eclipsesource.v8.V8
import com.google.android.material.snackbar.Snackbar
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_example.fab
import kotlinx.android.synthetic.main.activity_example.toolbar
import java.util.Random
import java.util.concurrent.ExecutorService
import java.util.concurrent.Future
import javax.inject.Inject
class ExampleActivity : AppCompatActivity() {
@Inject
lateinit var simpleScriptProvider: SimpleScriptProvider
/** V8 should be initialized and further called on the same thread.*/
@Inject
lateinit var v8Executor: ExecutorService
lateinit var v8Future: Future<V8>
/** Must be called only in v8's thread only. */
private val v8: V8 by lazy { v8Future.get() }
override fun onCreate(savedInstanceState: Bundle?) {
val scriptName = "hello-world"
var scriptPostfix = 1
AndroidInjection.inject(this)
updateUserToRandom()
v8Future = initDebuggableV8()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_example)
setSupportActionBar(toolbar)
fab.setOnClickListener { view ->
scriptPostfix = 1 - scriptPostfix
val scriptToDebug = "${scriptName}${scriptPostfix}"
val jsScript = simpleScriptProvider.getSource(scriptToDebug)
v8Executor.submit {
val result = v8.executeScript(jsScript, scriptToDebug, 0)
println("[v8 execution result: ] $result")
Snackbar.make(view, "V8 answers: $result", Snackbar.LENGTH_SHORT)
.setAction("V8Action", null).show()
}
}
}
private fun initDebuggableV8(): Future<V8> {
return V8Debugger.createDebuggableV8Runtime(v8Executor, "demo", true)
}
override fun onDestroy() {
releaseDebuggableV8()
super.onDestroy()
}
private fun releaseDebuggableV8() {
v8Executor.execute {
v8.close()
}
}
private fun updateUserToRandom() {
val newUser = "user" + Random().nextInt(10)
//following assumes, that some JS sources are different per user
StethoHelper.scriptsPathPrefix = newUser
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_example, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_scripts_changed -> {
simpleScriptProvider.updateTimeToNow()
StethoHelper.notifyScriptsChanged()
true
}
R.id.action_user_and_scripts_changed -> {
//here user and related scripts should be changed
updateUserToRandom()
simpleScriptProvider.updateTimeToNow()
StethoHelper.notifyScriptsChanged()
true
}
else -> super.onOptionsItemSelected(item)
}
}
}