/
TimeGraphView.java
130 lines (113 loc) · 5.07 KB
/
TimeGraphView.java
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
/*
* WiFi Analyzer
* Copyright (C) 2017 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.graph.time;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.view.View;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import com.vrem.wifianalyzer.Configuration;
import com.vrem.wifianalyzer.MainActivity;
import com.vrem.wifianalyzer.MainContext;
import com.vrem.wifianalyzer.R;
import com.vrem.wifianalyzer.settings.Settings;
import com.vrem.wifianalyzer.wifi.band.WiFiBand;
import com.vrem.wifianalyzer.wifi.graph.tools.GraphColor;
import com.vrem.wifianalyzer.wifi.graph.tools.GraphConstants;
import com.vrem.wifianalyzer.wifi.graph.tools.GraphViewBuilder;
import com.vrem.wifianalyzer.wifi.graph.tools.GraphViewNotifier;
import com.vrem.wifianalyzer.wifi.graph.tools.GraphViewWrapper;
import com.vrem.wifianalyzer.wifi.model.WiFiData;
import com.vrem.wifianalyzer.wifi.model.WiFiDetail;
import java.util.Set;
import java.util.TreeSet;
class TimeGraphView implements GraphViewNotifier, GraphConstants {
private final WiFiBand wiFiBand;
private GraphViewWrapper graphViewWrapper;
private int scanCount;
private int xValue;
TimeGraphView(@NonNull WiFiBand wiFiBand) {
this.wiFiBand = wiFiBand;
this.scanCount = this.xValue = 0;
this.graphViewWrapper = makeGraphViewWrapper();
}
@Override
public void update(@NonNull WiFiData wiFiData) {
Settings settings = MainContext.INSTANCE.getSettings();
Set<WiFiDetail> newSeries = new TreeSet<>();
for (WiFiDetail wiFiDetail : wiFiData.getWiFiDetails(wiFiBand, settings.getSortBy())) {
newSeries.add(wiFiDetail);
addData(wiFiDetail);
}
graphViewWrapper.removeSeries(newSeries);
graphViewWrapper.updateLegend(settings.getTimeGraphLegend());
graphViewWrapper.setVisibility(isSelected() ? View.VISIBLE : View.GONE);
xValue++;
if (scanCount < MAX_SCAN_COUNT) {
scanCount++;
}
}
private boolean isSelected() {
return wiFiBand.equals(MainContext.INSTANCE.getSettings().getWiFiBand());
}
private void addData(@NonNull WiFiDetail wiFiDetail) {
DataPoint dataPoint = new DataPoint(xValue, wiFiDetail.getWiFiSignal().getLevel());
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[]{dataPoint});
if (graphViewWrapper.appendSeries(wiFiDetail, series, dataPoint, scanCount)) {
series.setColor((int) graphViewWrapper.getColor().getPrimary());
series.setDrawBackground(false);
}
}
@Override
public GraphView getGraphView() {
return graphViewWrapper.getGraphView();
}
private int getNumX() {
return NUM_X_TIME;
}
void setGraphViewWrapper(@NonNull GraphViewWrapper graphViewWrapper) {
this.graphViewWrapper = graphViewWrapper;
}
private GraphView makeGraphView(@NonNull MainActivity mainActivity, int graphMaximumY) {
Resources resources = mainActivity.getResources();
return new GraphViewBuilder(mainActivity, getNumX(), graphMaximumY)
.setLabelFormatter(new TimeAxisLabel())
.setVerticalTitle(resources.getString(R.string.graph_axis_y))
.setHorizontalTitle(resources.getString(R.string.graph_time_axis_x))
.build();
}
private GraphViewWrapper makeGraphViewWrapper() {
MainContext mainContext = MainContext.INSTANCE;
MainActivity mainActivity = mainContext.getMainActivity();
Settings settings = mainContext.getSettings();
Configuration configuration = mainContext.getConfiguration();
GraphView graphView = makeGraphView(mainActivity, settings.getGraphMaximumY());
graphViewWrapper = new GraphViewWrapper(graphView, settings.getTimeGraphLegend());
configuration.setSize(graphViewWrapper.getSize(graphViewWrapper.calculateGraphType()));
graphViewWrapper.setViewport();
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[]{
new DataPoint(0, MIN_Y),
new DataPoint(getNumX() - 1, MIN_Y)
});
series.setColor((int) GraphColor.TRANSPARENT.getPrimary());
series.setThickness(THICKNESS_INVISIBLE);
graphViewWrapper.addSeries(series);
return graphViewWrapper;
}
}