aboutsummaryrefslogtreecommitdiff
path: root/app/src/debug/java/moe/yuuta/workmode/debug/DebugActivity.kt
blob: b8b5af8731ec6163a19c4e2ae8fa2f5ebad9e474 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package moe.yuuta.workmode.debug

import android.graphics.Typeface
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.method.ScrollingMovementMethod
import android.util.Log
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import moe.yuuta.ext.HandlerThreadExecutor
import moe.yuuta.workmode.access.DumpResult
import moe.yuuta.workmode.suspend.AsyncSuspender
import moe.yuuta.workmode.suspend.data.TransferableSuspendedApp
import moe.yuuta.workmode.utils.Utils
import java.util.concurrent.CompletableFuture

class DebugActivity : AppCompatActivity() {
    companion object {
        private const val TAG = "Debug"
    }

    private var mCurrentFuture: CompletableFuture<*>? = null
    private lateinit var mLogText: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.i(TAG, "Starting")
        val rootView = LinearLayout(this)
        rootView.orientation = LinearLayout.VERTICAL

        mLogText = TextView(this)
        mLogText.typeface = Typeface.MONOSPACE
        mLogText.movementMethod = ScrollingMovementMethod()
        rootView.addView(mLogText)

        registerTestingFunction(rootView, "isSuspended", object : Callback {
            override fun call(): CompletableFuture<*> {
                return AsyncSuspender(this@DebugActivity).isSuspended(listOf(TransferableSuspendedApp.of("kh.android.dir"),
                    TransferableSuspendedApp.of("com.android.chrome")))
            }
        })

        registerTestingFunction(rootView, "getSuspendedPackageAppExtras", object : Callback {
            override fun call(): CompletableFuture<*> {
                return AsyncSuspender(this@DebugActivity).getSuspendedPackageAppExtras(TransferableSuspendedApp.of("com.android.chrome"))
            }
        })

        registerTestingFunction(rootView, "getSuspendedPackageLauncherExtras", object : Callback {
            override fun call(): CompletableFuture<*> {
                return AsyncSuspender(this@DebugActivity).getSuspendedPackageLauncherExtras(TransferableSuspendedApp.of("com.android.chrome"))
            }
        })

        registerTestingFunction(rootView, "dump", object : Callback {
            override fun call(): CompletableFuture<*> {
                return AsyncSuspender(this@DebugActivity).dump(TransferableSuspendedApp.of("com.google.android.dialer", 11))
            }
        })

        registerTestingFunction(rootView, "suspend (true)", object : Callback {
            override fun call(): CompletableFuture<*> {
                return AsyncSuspender(this@DebugActivity).suspend(listOf(TransferableSuspendedApp.of("kh.android.dir"),
                    TransferableSuspendedApp.of("com.android.chrome")), true)
            }
        })

        registerTestingFunction(rootView, "suspend (false)", object : Callback {
            override fun call(): CompletableFuture<*> {
                return AsyncSuspender(this@DebugActivity).suspend(listOf(TransferableSuspendedApp.of("kh.android.dir"),
                    TransferableSuspendedApp.of("com.android.chrome")), false)
            }
        })

        registerTestingFunction(rootView, "apply", object : Callback {
            override fun call(): CompletableFuture<*> {
                return AsyncSuspender(this@DebugActivity).applyFromSettings()
            }
        })

        registerTestingFunction(rootView, "getInstalledApplicationsAcrossUser", object : Callback {
            override fun call(): CompletableFuture<*> {
                return AsyncSuspender(this@DebugActivity).getInstalledApplicationsAcrossUser(0)
            }
        })

        Log.d(TAG, "Done, children amount: ${rootView.childCount}")
        setContentView(rootView)
    }

    private fun registerTestingFunction(rootView: ViewGroup, tag: String, onClickListener: Callback) {
        val btn = Button(this)
        btn.text = tag
        btn.setOnClickListener {
            if (mCurrentFuture != null && !(mCurrentFuture as CompletableFuture<*>).isDone) {
                Toast.makeText(this, "Current running task is not done yet.", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            mLogText.text = ""
            mLogText.append("Started task.\n")
            mCurrentFuture = onClickListener
                .call()
                /* .exceptionally {
                    runOnUiThread {
                        Log.e(TAG, "Exceptionally ${Thread.currentThread().name}", it)
                        mLogText.append("\n")
                        mLogText.append("Exceptionally: $it\n")
                    }
                    return@exceptionally null
                } */
                .handle { result, e ->
                    // handleAsync() doesn't work
                    runOnUiThread {
                        if (result != null && result is DumpResult) {
                            mLogText.append("DumpResult - Launcher Extras: ${Utils.dumpExtras(result.launcherExtras)}\n")
                        }
                        mLogText.append("Done!\n")
                        mLogText.append("Result: $result\n")
                        mLogText.append("Error: $e")
                    }
                }
                .thenRunAsync(Runnable {
                    Log.e(TAG, "Then run ${Thread.currentThread().name}")
                    mLogText.append("\n")
                    mLogText.append("Run.")
                }, HandlerThreadExecutor(Handler(Looper.getMainLooper())))
        }
        rootView.addView(btn)
    }

    private interface Callback {
        fun call(): CompletableFuture<*>
    }
}