forked from VREMSoftwareDevelopment/WiFiAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphViewWrapper.kt
More file actions
172 lines (152 loc) · 6.12 KB
/
GraphViewWrapper.kt
File metadata and controls
172 lines (152 loc) · 6.12 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
/*
* WiFiAnalyzer
* Copyright (C) 2015 - 2026 VREM Software Development <VREMSoftwareDevelopment@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package com.vrem.wifianalyzer.wifi.graphutils
import com.jjoe64.graphview.GraphView
import com.jjoe64.graphview.LegendRenderer
import com.jjoe64.graphview.series.BaseSeries
import com.jjoe64.graphview.series.DataPointInterface
import com.jjoe64.graphview.series.Series
import com.vrem.wifianalyzer.MainContext
import com.vrem.wifianalyzer.SIZE_MAX
import com.vrem.wifianalyzer.SIZE_MIN
import com.vrem.wifianalyzer.settings.ThemeStyle
import com.vrem.wifianalyzer.wifi.accesspoint.AccessPointDetail
import com.vrem.wifianalyzer.wifi.accesspoint.AccessPointPopup
import com.vrem.wifianalyzer.wifi.model.WiFiDetail
import java.security.MessageDigest
class GraphViewWrapper(
val graphView: GraphView,
var graphLegend: GraphLegend,
private val themeStyle: ThemeStyle,
private val seriesCache: SeriesCache = SeriesCache(),
private val seriesOptions: SeriesOptions = SeriesOptions(),
) {
fun removeSeries(newSeries: Set<WiFiDetail>): Unit =
seriesCache.remove(differenceSeries(newSeries)).forEach {
seriesOptions.removeSeriesColor(it)
graphView.removeSeries(it)
}
fun differenceSeries(newSeries: Set<WiFiDetail>): List<WiFiDetail> = seriesCache.difference(newSeries)
fun addSeries(
wiFiDetail: WiFiDetail,
series: BaseSeries<GraphDataPoint>,
drawBackground: Boolean,
): Boolean =
if (seriesExists(wiFiDetail)) {
false
} else {
seriesCache.put(wiFiDetail, series)
series.title = wiFiDetail.wiFiIdentifier.ssid + " " + wiFiDetail.wiFiSignal.channelDisplay()
series.setOnDataPointTapListener { value, _ -> this.popup(value) }
seriesOptions.highlightConnected(series, wiFiDetail.wiFiAdditional.wiFiConnection.connected)
seriesOptions.setSeriesColor(series)
seriesOptions.drawBackground(series, drawBackground)
graphView.addSeries(series)
true
}
fun updateSeries(
wiFiDetail: WiFiDetail,
data: Array<GraphDataPoint>,
drawBackground: Boolean,
): Boolean =
if (seriesExists(wiFiDetail)) {
val series = seriesCache[wiFiDetail]
series.resetData(data)
series.title = wiFiDetail.wiFiIdentifier.ssid + " " + wiFiDetail.wiFiSignal.channelDisplay()
seriesOptions.highlightConnected(series, wiFiDetail.wiFiAdditional.wiFiConnection.connected)
seriesOptions.drawBackground(series, drawBackground)
true
} else {
false
}
fun appendToSeries(
wiFiDetail: WiFiDetail,
data: GraphDataPoint,
count: Int,
drawBackground: Boolean,
): Boolean =
if (seriesExists(wiFiDetail)) {
val series = seriesCache[wiFiDetail]
series.appendData(data, true, count + 1)
seriesOptions.highlightConnected(series, wiFiDetail.wiFiAdditional.wiFiConnection.connected)
seriesOptions.drawBackground(series, drawBackground)
true
} else {
false
}
fun newSeries(wiFiDetail: WiFiDetail): Boolean = !seriesExists(wiFiDetail)
fun setViewport() {
val viewport = graphView.viewport
viewport.setMinX(0.0)
viewport.setMaxX(viewportCntX.toDouble())
}
fun setViewport(
minX: Int,
maxX: Int,
) {
val viewport = graphView.viewport
viewport.setMinX(minX.toDouble())
viewport.setMaxX(maxX.toDouble())
}
val viewportCntX: Int get() = graphView.gridLabelRenderer.numHorizontalLabels - 1
fun addSeries(series: BaseSeries<GraphDataPoint>) {
graphView.addSeries(series)
}
fun updateLegend(graphLegend: GraphLegend) {
resetLegendRenderer(graphLegend)
val legendRenderer = graphView.legendRenderer
legendRenderer.resetStyles()
legendRenderer.width = 0
legendRenderer.textSize = graphView.titleTextSize
legendRenderer.textColor = themeStyle.colorGraphText
graphLegend.display(legendRenderer)
}
fun calculateGraphType(): Int =
runCatching {
with(MessageDigest.getInstance("MD5")) {
update(
MainContext.INSTANCE.mainActivity.packageName
.toByteArray(),
)
val digest: ByteArray = digest()
digest.contentHashCode()
}
}.getOrDefault(TYPE1)
fun setHorizontalLabelsVisible(horizontalLabelsVisible: Boolean) {
graphView.gridLabelRenderer.isHorizontalLabelsVisible = horizontalLabelsVisible
}
fun visibility(visibility: Int) {
graphView.visibility = visibility
}
fun size(value: Int): Int = if (value == TYPE1 || value == TYPE2 || value == TYPE3) SIZE_MAX else SIZE_MIN
fun newLegendRenderer(): LegendRenderer = LegendRenderer(graphView)
private fun resetLegendRenderer(graphLegend: GraphLegend) {
if (this.graphLegend != graphLegend) {
graphView.legendRenderer = newLegendRenderer()
this.graphLegend = graphLegend
}
}
private fun seriesExists(wiFiDetail: WiFiDetail): Boolean = seriesCache.contains(wiFiDetail)
private fun popup(series: Series<DataPointInterface>) {
seriesCache.find(series).let {
runCatching {
AccessPointPopup().show(AccessPointDetail().makeViewDetailed(it))
}
}
}
}