forked from starifly/NekoBoxForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatsBar.kt
More file actions
215 lines (196 loc) · 7.81 KB
/
Copy pathStatsBar.kt
File metadata and controls
215 lines (196 loc) · 7.81 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package io.nekohasekai.sagernet.widget
import android.annotation.SuppressLint
import android.content.Context
import android.text.SpannableStringBuilder
import android.text.format.Formatter
import android.text.style.ForegroundColorSpan
import android.util.AttributeSet
import android.view.View
import android.widget.TextView
import androidx.appcompat.widget.TooltipCompat
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.withStarted
import com.google.android.material.bottomappbar.BottomAppBar
import io.nekohasekai.sagernet.R
import io.nekohasekai.sagernet.bg.BaseService
import io.nekohasekai.sagernet.database.DataStore
import io.nekohasekai.sagernet.ktx.*
import io.nekohasekai.sagernet.ui.MainActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
class StatsBar @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null,
defStyleAttr: Int = R.attr.bottomAppBarStyle,
) : BottomAppBar(context, attrs, defStyleAttr) {
private lateinit var statusText: TextView
private lateinit var txText: TextView
private lateinit var rxText: TextView
private lateinit var behavior: YourBehavior
var allowShow = true
override fun getBehavior(): YourBehavior {
if (!this::behavior.isInitialized) behavior = YourBehavior { allowShow }
return behavior
}
class YourBehavior(val getAllowShow: () -> Boolean) : Behavior() {
override fun onNestedScroll(
coordinatorLayout: CoordinatorLayout, child: BottomAppBar, target: View,
dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int,
type: Int, consumed: IntArray,
) {
super.onNestedScroll(
coordinatorLayout,
child,
target,
dxConsumed,
dyConsumed + dyUnconsumed,
dxUnconsumed,
0,
type,
consumed
)
}
override fun slideUp(child: BottomAppBar) {
if (!getAllowShow()) return
super.slideUp(child)
}
override fun slideDown(child: BottomAppBar) {
if (!getAllowShow()) return
super.slideDown(child)
}
}
override fun setOnClickListener(l: OnClickListener?) {
statusText = findViewById(R.id.status)
txText = findViewById(R.id.tx)
rxText = findViewById(R.id.rx)
super.setOnClickListener(l)
}
private fun setStatus(text: CharSequence) {
statusText.text = text
TooltipCompat.setTooltipText(this, text)
}
// Two-tone status: color the lead segment (split at [sep], kept with the lead)
// and the remainder separately. Used for "Connected, …" and "Success: …".
private fun setStatusTwoTone(
full: CharSequence, sep: Char, leadAttr: Int, restAttr: Int
) {
val s = full.toString()
val idx = s.indexOf(sep)
if (idx < 0) {
statusText.setTextColor(context.getColorAttr(leadAttr))
setStatus(full)
return
}
val cut = idx + 1 // keep the separator char with the lead segment
val span = SpannableStringBuilder(s)
span.setSpan(
ForegroundColorSpan(context.getColorAttr(leadAttr)),
0, cut, SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE
)
span.setSpan(
ForegroundColorSpan(context.getColorAttr(restAttr)),
cut, s.length, SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE
)
setStatus(span)
}
private fun setStatusColorByState(state: BaseService.State) {
// Connected = statusConnectedColor (green), Stopped/Stopping = statusStoppedColor
// (red — "Shutting down…" reads as red), Connecting = statusConnectingColor (Dracula
// yellow; colorOnPrimary elsewhere), other = colorOnPrimary.
// Non-Dracula themes default these attrs to colorOnPrimary, so no change.
val attr = when (state) {
BaseService.State.Connected -> R.attr.statusConnectedColor
BaseService.State.Stopped, BaseService.State.Stopping -> R.attr.statusStoppedColor
BaseService.State.Connecting -> R.attr.statusConnectingColor
else -> com.google.android.material.R.attr.colorOnPrimary
}
statusText.setTextColor(context.getColorAttr(attr))
}
fun changeState(state: BaseService.State) {
val activity = context as MainActivity
fun postWhenStarted(what: () -> Unit) = activity.lifecycleScope.launch(Dispatchers.Main) {
delay(100L)
activity.withStarted { what() }
}
if ((state == BaseService.State.Connected).also { hideOnScroll = it }) {
postWhenStarted {
if (allowShow) performShow()
// "Connected," in green; the "tap to check connection" hint in detail color.
setStatusTwoTone(
app.getText(R.string.vpn_connected), ',',
R.attr.statusConnectedColor, R.attr.statusDetailColor
)
}
} else {
postWhenStarted {
performHide()
}
updateSpeed(0, 0)
setStatusColorByState(state)
setStatus(
context.getText(
when (state) {
BaseService.State.Connecting -> R.string.connecting
BaseService.State.Stopping -> R.string.stopping
else -> R.string.not_connected
}
)
)
}
}
@SuppressLint("SetTextI18n")
fun updateSpeed(txRate: Long, rxRate: Long) {
val speedColor = context.getColorAttr(R.attr.speedTextColor)
txText.setTextColor(speedColor)
rxText.setTextColor(speedColor)
txText.text = "▲ ${
context.getString(
R.string.speed, Formatter.formatFileSize(context, txRate)
)
}"
rxText.text = "▼ ${
context.getString(
R.string.speed, Formatter.formatFileSize(context, rxRate)
)
}"
}
fun testConnection() {
val activity = context as MainActivity
isEnabled = false
// "Testing…" in the testing color.
statusText.setTextColor(context.getColorAttr(R.attr.statusTestingColor))
setStatus(app.getText(R.string.connection_test_testing))
runOnDefaultDispatcher {
try {
val elapsed = activity.urlTest()
onMainDispatcher {
isEnabled = true
// "Success:" in green; the handshake detail in detail color.
setStatusTwoTone(
app.getString(
if (DataStore.connectionTestURL.startsWith("https://")) {
R.string.connection_test_available
} else {
R.string.connection_test_available_http
}, elapsed
), ':',
R.attr.statusConnectedColor, R.attr.statusDetailColor
)
}
} catch (e: Exception) {
Logs.w(e.toString())
onMainDispatcher {
isEnabled = true
statusText.setTextColor(context.getColorAttr(R.attr.statusTestingColor))
setStatus(app.getText(R.string.connection_test_testing))
activity.snackbar(
app.getString(
R.string.connection_test_error, e.readableMessage
)
).show()
}
}
}
}
}