use of com.cpjd.roblu.models.metrics.RStopwatch in project Roblu by wdavies973.
the class MetricEditor method buildConfigLayout.
/**
* Adds the config text fields below the metric preview
*/
private void buildConfigLayout() {
// clear old config items (don't clear toolbar, preview metric, or metric type selector)
for (int i = 3; i < this.layout.getChildCount(); i++) {
this.layout.removeViewAt(i);
}
RelativeLayout layout = new RelativeLayout(this);
layout.addView(getConfigField("Title", layout, 0));
if (metric instanceof RCheckbox || metric instanceof RChooser) {
layout.addView(getConfigField("Comma separated list", layout, 1));
} else if (metric instanceof RCounter) {
final TextInputLayout til = getConfigField("Increment", layout, 1);
til.setId(Utils.generateViewId());
layout.addView(til);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, til.getId());
CheckBox checkBox = new CheckBox(getApplicationContext());
checkBox.setId(Utils.generateViewId());
checkBox.setLayoutParams(params);
checkBox.setHighlightColor(rui.getAccent());
checkBox.setTextColor(rui.getText());
checkBox.setText("Verbose input");
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
((RCounter) metric).setVerboseInput(isChecked);
addMetricPreviewToToolbar();
}
});
ColorStateList colorStateList = new ColorStateList(new int[][] { // unchecked
new int[] { -android.R.attr.state_checked }, // checked
new int[] { android.R.attr.state_checked } }, new int[] { rui.getButtons(), rui.getAccent() });
CompoundButtonCompat.setButtonTintList(checkBox, colorStateList);
layout.addView(checkBox);
} else if (metric instanceof RSlider) {
layout.addView(getConfigField("Minimum", layout, 1));
layout.addView(getConfigField("Maximum", layout, 2));
} else if (metric instanceof RCalculation) {
final TextInputLayout til = getConfigField("Calculation", layout, 1);
layout.addView(til);
// Also add a button for inputting metric names
Button b = new Button(getApplicationContext());
b.setText("Add metric");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, til.getId());
b.setLayoutParams(params);
layout.addView(b);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog d = new Dialog(MetricEditor.this);
d.setTitle("Pick metric:");
d.setContentView(R.layout.metric_chooser);
final Spinner spinner = d.findViewById(R.id.type);
String[] values;
final ArrayList<RMetric> metrics;
if (tab == 0)
metrics = form.getPit();
else
metrics = form.getMatch();
// Remove all but counters, stopwatches, and sliders
for (int i = 0; i < metrics.size(); i++) {
if (!(metrics.get(i) instanceof RCounter) && !(metrics.get(i) instanceof RStopwatch && !(metrics.get(i) instanceof RSlider)) && !(metrics.get(i) instanceof RCalculation)) {
metrics.remove(i);
i--;
}
}
values = new String[metrics.size()];
for (int i = 0; i < metrics.size(); i++) {
values[i] = metrics.get(i).getTitle();
}
ArrayAdapter<String> adp = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, values);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);
Button button = d.findViewById(R.id.button7);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
RMetric m = new ArrayList<>(metrics).get(spinner.getSelectedItemPosition());
til.getEditText().setText(til.getEditText().getText().toString() + " " + m.getTitle());
} catch (Exception e) {
Log.d("RBS", "Failed to select metric");
} finally {
d.dismiss();
}
}
});
if (d.getWindow() != null)
d.getWindow().getAttributes().windowAnimations = new IO(getApplicationContext()).loadSettings().getRui().getAnimation();
d.show();
}
});
}
this.layout.addView(getCardView(layout));
}
use of com.cpjd.roblu.models.metrics.RStopwatch in project Roblu by wdavies973.
the class MetricEditor method onItemSelected.
/**
* Called when the user selects a metric type
* @param adapterView the adapter containing all the choices
* @param view the view that was tapped
* @param i the position of the view
* @param l id of the view
*/
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
((TextView) adapterView.getChildAt(0)).setTextColor(rui.getText());
/*
* User selected a new metric, let's create it
*/
String stringOfSelected = METRIC_TYPES[i];
if (stringOfSelected.equals(METRIC_TYPES[0])) {
metric = new RBoolean(0, "Boolean", false);
} else if (stringOfSelected.equals(METRIC_TYPES[1])) {
metric = new RCounter(0, "Counter", 1, 0);
} else if (stringOfSelected.equals(METRIC_TYPES[2])) {
metric = new RSlider(0, "Slider", 0, 100, 0);
} else if (stringOfSelected.equals(METRIC_TYPES[3])) {
metric = new RChooser(0, "Chooser", null, 0);
} else if (stringOfSelected.equals(METRIC_TYPES[4])) {
metric = new RCheckbox(0, "Checkbox", null);
} else if (stringOfSelected.equals(METRIC_TYPES[5])) {
metric = new RStopwatch(0, "Stopwatch", 0);
} else if (stringOfSelected.equals(METRIC_TYPES[6])) {
metric = new RTextfield(0, "Text field", "");
} else if (stringOfSelected.equals(METRIC_TYPES[7])) {
metric = new RGallery(0, "Gallery");
} else if (stringOfSelected.equalsIgnoreCase(METRIC_TYPES[8])) {
metric = new RDivider(0, "Divider");
} else if (stringOfSelected.equals(METRIC_TYPES[9])) {
metric = new RFieldDiagram(0, R.drawable.field2018, null);
} else if (stringOfSelected.equals(METRIC_TYPES[10])) {
metric = new RCalculation(0, "Custom calculation");
} else if (stringOfSelected.equals(METRIC_TYPES[11])) {
metric = new RFieldData(0, "Match data");
}
metric.setModified(true);
addMetricPreviewToToolbar();
buildConfigLayout();
}
use of com.cpjd.roblu.models.metrics.RStopwatch in project Roblu by wdavies973.
the class Overview method onCreateView.
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.overview_tab, container, false);
Bundle bundle = this.getArguments();
layout = view.findViewById(R.id.overview_layout);
REvent event = (REvent) getArguments().getSerializable("event");
RTeam team = new IO(getActivity()).loadTeam(event.getID(), TeamViewer.team.getID());
rMetricToUI = new RMetricToUI(getActivity(), new IO(getActivity()).loadSettings().getRui(), true);
try {
/*
* Do statistics generation, this will generate graphs for certain metrics
*/
// Stores pie chart values, with the sub linked hash map <item,occurrences>, this will need to be processed later into a percent
LinkedHashMap<String, LinkedHashMap<String, Double>> pieValues = new LinkedHashMap<>();
// Stores line chart values, with the sub linked hash map <matchName,value>
LinkedHashMap<String, LinkedHashMap<String, Double>> lineValues = new LinkedHashMap<>();
// This isn't directly related, more of a side project
ArrayList<RGallery> galleries = new ArrayList<>();
for (RTab tab : team.getTabs()) {
// Rule out disallowed tabs
if (tab.getTitle().equalsIgnoreCase("PIT"))
continue;
// Start processing metrics
for (RMetric metric : tab.getMetrics()) {
if (metric instanceof RGallery) {
galleries.add((RGallery) metric);
}
if (!metric.isModified())
continue;
// Pie graph metrics, scan these here
if (metric instanceof RBoolean) {
LinkedHashMap<String, Double> temp = pieValues.get(metric.getTitle());
if (temp == null)
temp = new LinkedHashMap<>();
String key = ((RBoolean) metric).isValue() ? "Yes" : "No";
if (temp.get(key) == null)
temp.put(key, 1.0);
else
temp.put(key, temp.get(key) + 1);
pieValues.put(metric.getTitle(), temp);
} else if (metric instanceof RCheckbox) {
if (((RCheckbox) metric).getValues() != null) {
for (Object key : ((RCheckbox) metric).getValues().keySet()) {
LinkedHashMap<String, Double> temp = pieValues.get(metric.getTitle());
if (temp == null)
temp = new LinkedHashMap<>();
if (temp.get(key.toString()) == null)
temp.put(key.toString(), 1.0);
else
temp.put(key.toString(), temp.get(key.toString()) + 1);
pieValues.put(metric.getTitle(), temp);
}
}
} else if (metric instanceof RChooser) {
LinkedHashMap<String, Double> temp = pieValues.get(metric.getTitle());
if (temp == null)
temp = new LinkedHashMap<>();
if (temp.get(metric.toString()) == null)
temp.put(metric.toString(), 1.0);
else
temp.put(metric.toString(), temp.get(metric.toString()) + 1);
pieValues.put(metric.getTitle(), temp);
} else // Line chart metrics
if (metric instanceof RCounter || metric instanceof RSlider || metric instanceof RStopwatch || metric instanceof RCalculation) {
LinkedHashMap<String, Double> temp = lineValues.get(metric.getTitle());
if (temp == null)
temp = new LinkedHashMap<>();
temp.put(tab.getTitle(), Double.parseDouble(metric.toString()));
lineValues.put(metric.getTitle(), temp);
}
}
}
// Add the divider metrics by position, -1 if no metric after it, or at the end
ArrayList<RDivider> addedDividers = new ArrayList<>();
/*
* Add the charts!
*/
for (Object key : lineValues.keySet()) {
if (lineValues.get(key.toString()).size() >= 2) {
loop: for (RTab tab : team.getTabs()) {
for (int i = 0; i < tab.getMetrics().size(); i++) {
if (tab.getMetrics().get(i).getTitle().equals(key.toString())) {
// See if there is a RDivider hiding above this metric
for (int j = i; j >= 0; j--) {
if (tab.getMetrics().get(j) instanceof RDivider && !addedDividers.contains(tab.getMetrics().get(j))) {
layout.addView(rMetricToUI.getDivider((RDivider) tab.getMetrics().get(j)));
addedDividers.add((RDivider) tab.getMetrics().get(j));
break loop;
}
}
}
}
}
layout.addView(rMetricToUI.generateLineChart(key.toString(), lineValues.get(key.toString())));
}
}
// Process the pie charts
for (Object key : pieValues.keySet()) {
if (pieValues.get(key.toString()).size() <= 1)
continue;
int metricID = 0;
loop: for (RTab tab : team.getTabs()) {
for (int i = 0; i < tab.getMetrics().size(); i++) {
if (tab.getMetrics().get(i).getTitle().equals(key.toString())) {
metricID = tab.getMetrics().get(i).getID();
// See if there is a RDivider hiding above this metric
for (int j = i; j >= 0; j--) {
if (tab.getMetrics().get(j) instanceof RDivider && !addedDividers.contains(tab.getMetrics().get(j))) {
layout.addView(rMetricToUI.getDivider((RDivider) tab.getMetrics().get(j)));
addedDividers.add((RDivider) tab.getMetrics().get(j));
break loop;
}
}
}
}
}
for (Object key2 : pieValues.get(key.toString()).keySet()) {
if (numModified(team.getTabs(), metricID) != 0)
pieValues.get(key.toString()).put(key2.toString(), pieValues.get(key.toString()).get(key2.toString()) / (double) numModified(team.getTabs(), metricID));
}
layout.addView(rMetricToUI.generatePieChart(key.toString(), pieValues.get(key.toString())));
}
/*
* Find the image with the most entropy, and add
* it as the "featured" image
*/
galleryLoop: for (int j = galleries.size() - 1; j >= 0; j--) {
if (galleries.get(j).getImages() != null && galleries.get(j).getImages().size() > 0) {
for (int i = galleries.get(j).getImages().size() - 1; i >= 0; i--) {
try {
layout.addView(rMetricToUI.getImageView("Featured image", BitmapFactory.decodeByteArray(galleries.get(j).getImages().get(i), 0, galleries.get(j).getImages().get(i).length)));
break galleryLoop;
} catch (Exception e) {
Log.d("RBS", "Failed to load featured image: " + e.getMessage());
}
}
}
}
} catch (Exception e) {
Log.d("RBS", "Failed to generate graphs for this team profile.");
}
/*
* Attempt to download TBA info for this team
*/
if (!team.hasTBAInfo()) {
if (event.getKey() != null && event.getKey().length() >= 4)
new TBATeamInfoTask(view.getContext(), team.getNumber(), event.getKey().substring(0, 4), this);
} else {
// TBA info card
layout.addView(rMetricToUI.getInfoField("TBA.com information", TeamViewer.team.getTbaInfo(), TeamViewer.team.getWebsite(), TeamViewer.team.getNumber()), 0);
if (TeamViewer.team.getImage() != null) {
// Image view
Bitmap bitmap = BitmapFactory.decodeByteArray(TeamViewer.team.getImage(), 0, TeamViewer.team.getImage().length);
layout.addView(rMetricToUI.getImageView("Robot", bitmap));
}
}
/*
* Add UI cards to the layout
*/
// "Other" card
layout.addView(rMetricToUI.getInfoField("Other", "Last edited: " + Utils.convertTime(team.getLastEdit()) + "\nSize on disk: " + new IO(view.getContext()).getTeamSize(bundle.getInt("eventID"), team.getID()) + " KB", "", 0));
return view;
}
use of com.cpjd.roblu.models.metrics.RStopwatch in project Roblu by wdavies973.
the class CheckoutEncoder method decodeCheckout.
/**
* Decodes a checkout encoded in the CheckoutEncoder format
* @param string the string to deserialize
* @return an instantiated checkout from the string
*/
public RCheckout decodeCheckout(String string) {
try {
String[] lines = string.split("\n");
for (String s : lines) {
Log.d("RBS", "Line: " + s);
}
// Meta
RCheckout checkout = new RCheckout();
checkout.setID(Integer.parseInt(lines[0]));
checkout.setNameTag(lines[1]);
RTeam team = new RTeam();
team.setID(Integer.parseInt(lines[2]));
team.setLastEdit(Long.parseLong(lines[3]));
team.setTabs(new ArrayList<RTab>());
// Tabs
for (int i = 0; i < lines.length; i++) {
if (!lines[i].startsWith("TAB"))
continue;
// Tab meta
RTab tab = new RTab();
tab.setTitle(lines[i].substring(3));
tab.setWon(Boolean.parseBoolean(lines[i + 1]));
tab.setMetrics(new ArrayList<RMetric>());
String[] tokens = lines[i + 2].split(",");
LinkedHashMap<String, Long> edits = new LinkedHashMap<>();
for (int k = 1; k < tokens.length; k++) {
edits.put(tokens[1], Long.parseLong(tokens[2]));
}
tab.setEdits(edits);
// Metrics
for (int k = i + 1; k < lines.length; k++) {
if (lines[k].startsWith("TAB"))
break;
if (lines[i].startsWith("null"))
continue;
String[] mTokens = lines[k].split(String.valueOf(DELIMITER));
RMetric metric = null;
switch(mTokens[0]) {
case // boolean
"B":
metric = new RBoolean();
((RBoolean) metric).setValue(Boolean.parseBoolean(mTokens[4]));
break;
case "CH":
{
// checkbox
metric = new RCheckbox();
LinkedHashMap<String, Boolean> values = new LinkedHashMap<>();
for (int l = 4; l < mTokens.length; l++) {
values.put(mTokens[l].split(",")[0].substring(1), Boolean.parseBoolean(mTokens[l].split(",")[1].replace(")", "")));
}
((RCheckbox) metric).setValues(values);
break;
}
case "CO":
{
// chooser
metric = new RChooser();
((RChooser) metric).setSelectedIndex(Integer.parseInt(mTokens[4]));
// the amount of values, with the header info removed
String[] values = new String[mTokens.length - 6];
for (int l = 5; l < mTokens.length - 1; l++) {
if (mTokens[l] != null && !mTokens[l].equals(""))
values[l - 5] = mTokens[l];
}
((RChooser) metric).setValues(values);
break;
}
case // counter
"C":
metric = new RCounter();
((RCounter) metric).setVerboseInput(Boolean.parseBoolean(mTokens[4]));
((RCounter) metric).setValue(Double.parseDouble(mTokens[5]));
((RCounter) metric).setIncrement(Double.parseDouble(mTokens[6]));
break;
case // slider
"S":
metric = new RSlider();
((RSlider) metric).setValue(Integer.parseInt(mTokens[4]));
((RSlider) metric).setMin(Integer.parseInt(mTokens[5]));
((RSlider) metric).setMax(Integer.parseInt(mTokens[6]));
break;
case // stopwatch
"ST":
metric = new RStopwatch();
((RStopwatch) metric).setTime(Double.parseDouble(mTokens[4]));
((RStopwatch) metric).setTimes(new ArrayList<Double>());
for (int l = 5; l < mTokens.length; l++) {
if (!mTokens[l].equals(""))
((RStopwatch) metric).getTimes().add(Double.parseDouble(mTokens[5]));
}
break;
case // textfield
"T":
metric = new RTextfield();
((RTextfield) metric).setText((mTokens[4]));
break;
}
if (metric != null) {
metric.setID(Integer.parseInt(mTokens[1]));
metric.setTitle(mTokens[2]);
metric.setModified(Boolean.parseBoolean(mTokens[3]));
tab.getMetrics().add(metric);
// Adding metric
Log.d("RBS", "Adding metric " + metric.toString());
}
}
team.getTabs().add(tab);
}
checkout.setTeam(team);
return checkout;
} catch (Exception e) {
e.printStackTrace();
Log.d("RBS", "An error occurred while decoding a checkout. " + e.getMessage());
return null;
}
}
use of com.cpjd.roblu.models.metrics.RStopwatch in project Roblu by wdavies973.
the class Utils method randomizeTeamMetrics.
/**
* This code will randomize an event with random metric data for the purposes of show casing an app
* or testing
*/
public static void randomizeTeamMetrics(ArrayList<RTab> tabs) {
Random r = new Random();
if (tabs != null) {
for (RTab tab : tabs) {
for (RMetric metric : tab.getMetrics()) {
metric.setModified(true);
if (metric instanceof RSlider) {
((RSlider) metric).setMax(100);
((RSlider) metric).setValue(r.nextInt(100));
} else if (metric instanceof RCounter) {
((RCounter) metric).setValue(r.nextDouble() * 100);
} else if (metric instanceof RStopwatch) {
((RStopwatch) metric).setTime(r.nextDouble() * 10);
((RStopwatch) metric).setTimes(new ArrayList<Double>());
for (int i = 0; i < r.nextInt(5); i++) {
((RStopwatch) metric).getTimes().add(Utils.round(r.nextDouble() * 8.2, 2));
}
} else if (metric instanceof RBoolean) {
((RBoolean) metric).setValue(r.nextDouble() <= 0.5);
} else if (metric instanceof RCheckbox) {
for (Object o : ((RCheckbox) metric).getValues().keySet()) {
((RCheckbox) metric).getValues().put(o.toString(), r.nextDouble() <= 0.50);
}
} else if (metric instanceof RChooser) {
((RChooser) metric).setSelectedIndex(r.nextInt(((RChooser) metric).getValues().length - 1));
} else if (metric instanceof RTextfield) {
if (!((RTextfield) metric).isOneLine())
((RTextfield) metric).setText("RTextfield has been randomized to: " + getSaltString());
}
}
}
}
}
Aggregations