Skip to content

Commit

Permalink
Fix Aggregator Critital Error
Browse files Browse the repository at this point in the history
  • Loading branch information
bqrichards committed Mar 8, 2019
1 parent 52f2140 commit 5874304
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 35 deletions.
29 changes: 10 additions & 19 deletions app/src/main/java/com/frc63175985/csp/FileManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.frc63175985.csp;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
Expand All @@ -23,6 +25,8 @@ public class FileManager {
private File aggregateFolder;
private SimpleDateFormat FILENAME_FORMAT = new SimpleDateFormat("y-M-d-k-h-m-s-S", Locale.US);

public static final int CHOOSE_AGGREGATOR_FILE_REQUEST_CODE = 1;

private FileManager() {
rootFolder = new File(Environment.getExternalStorageDirectory(), "CSP");
pitFolder = new File(rootFolder, "PIT");
Expand Down Expand Up @@ -53,8 +57,8 @@ public Bitmap readImage(@NonNull String filename) {
return bitmap;
}

public File getNewImageFile(@NonNull String prefix) {
File imageFile = new File(pitFolder, prefix + generateRandomFilename("jpg"));
public File getNewImageFile(@NonNull String filename) {
File imageFile = new File(pitFolder, filename + ".jpg");
try {
imageFile.createNewFile();
} catch (IOException e) {
Expand All @@ -66,17 +70,17 @@ public File getNewImageFile(@NonNull String prefix) {

/**
* Save the aggregation data to the disk.
* @return the absolute path the file was saved to
* @return the file was saved to
*/
public String saveAggregation(String data) {
public File saveAggregation(String data) {
File newAggregateFile = new File(aggregateFolder, FILENAME_FORMAT.format(new Date()) + ".csv");
try {
newAggregateFile.createNewFile();
FileWriter writer = new FileWriter(newAggregateFile);
writer.write(ScoutAuthState.shared.currentMatch.export());
writer.write(data);
writer.close();

return newAggregateFile.getAbsolutePath();
return newAggregateFile;
} catch (IOException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -113,17 +117,4 @@ public void savePit() {
e.printStackTrace();
}
}

private String generateRandomFilename(String ext) {
int length = 8;
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
Random random = new Random();
StringBuilder filename = new StringBuilder();
for (int i = 0; i < length; i++) {
int index = (int)(random.nextFloat() * characters.length());
filename.append(characters.charAt(index));
}

return filename.toString() + "." + ext;
}
}
15 changes: 13 additions & 2 deletions app/src/main/java/com/frc63175985/csp/PitScoutingActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,16 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
findViewById(R.id.pit_image_front_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String filename = ScoutAuthState.shared.pitScoutRecord.str(PitScoutRecord.TEAM_NUMBER);
if (filename.isEmpty()) {
Toast.makeText(PitScoutingActivity.this, "Supply Team Number First", Toast.LENGTH_LONG).show();
return;
}

Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

if (pictureIntent.resolveActivity(getPackageManager()) != null) {
File imageFile = FileManager.shared.getNewImageFile("FRONT-");
File imageFile = FileManager.shared.getNewImageFile("FRONT-" + filename);
if (imageFile == null) {
return;
}
Expand All @@ -121,10 +127,15 @@ public void onClick(View v) {
findViewById(R.id.pit_image_side_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String filename = ScoutAuthState.shared.pitScoutRecord.str(PitScoutRecord.TEAM_NUMBER);
if (filename.isEmpty()) {
Toast.makeText(PitScoutingActivity.this, "Supply Team Number First", Toast.LENGTH_LONG).show();
return;
}
Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

if (pictureIntent.resolveActivity(getPackageManager()) != null) {
File imageFile = FileManager.shared.getNewImageFile("SIDE-");
File imageFile = FileManager.shared.getNewImageFile("SIDE-" + filename);
if (imageFile == null) {
return;
}
Expand Down
46 changes: 32 additions & 14 deletions app/src/main/java/com/frc63175985/csp/QrAggregatorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
Expand All @@ -15,11 +17,13 @@
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.ShareActionProvider;
import android.widget.Toast;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

Expand Down Expand Up @@ -147,7 +151,12 @@ public void handleResult(Result result) {
"FALSE,FALSE,-1,-1,FALSE,0,-1,FALSE,-1,FALSE,-1,FALSE,-1,FALSE,-1,FALSE" +
",-1,FALSE,-1,FALSE,-1,FALSE,-1,FALSE,-1,FALSE,FALSE,FALSE,FALSE,FALSE," +
"-1,FALSE,-1,FALSE,-1,FALSE,-1,FALSE,-1,FALSE,-1,FALSE,-1,FALSE,-1,FALSE," +
"-1,FALSE,-1,FALSE,,FALSE,FALSE,0,FALSE,FALSE,FALSE,FALSE,FALSE,");
"-1,FALSE,-1,FALSE,,FALSE,FALSE,0,FALSE,FALSE,FALSE,FALSE,FALSE");
listViewValues.add("DEFAULT,Cedar+Falls,,,1,1,Richards,+Brandon,TRUE,TRUE," +
"TRUE,TRUE,1,1,TRUE,0,1,TRUE,1,TRUE,1,TRUE,1,TRUE,1,TRUE" +
",1,TRUE,1,TRUE,1,TRUE,1,TRUE,1,TRUE,TRUE,TRUE,TRUE,TRUE," +
"1,TRUE,1,TRUE,1,TRUE,1,TRUE,1,TRUE,1,TRUE,1,TRUE,1,TRUE," +
"1,TRUE,1,TRUE,,TRUE,TRUE,0,TRUE,TRUE,TRUE,TRUE,TRUE");
listViewValues.notifyDataSetChanged();
showScanned();
Toast.makeText(this, "Added new match data!", Toast.LENGTH_SHORT).show();
Expand Down Expand Up @@ -203,22 +212,31 @@ public void onClick(DialogInterface dialog, int which) {
return;
}

String path = FileManager.shared.saveAggregation(allEntriesContent);
String title, message;
final File aggregationFile = FileManager.shared.saveAggregation(allEntriesContent);

if (path == null) {
title = "Error";
message = "There was an error saving the file";
if (aggregationFile == null) {
new AlertDialog.Builder(this)
.setTitle("Error")
.setMessage("There was an error saving the file")
.setNegativeButton(android.R.string.ok, null)
.show();
} else {
title = "File saved";
message = "Successfully exported to path:\n" + path;
new AlertDialog.Builder(this)
.setTitle("File saved")
.setMessage("Successfully exported to path:\n" + aggregationFile.getAbsolutePath() + "\nWould you like to share this file?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
Uri uri = Uri.fromFile(aggregationFile);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share file using"));
}
})
.show();
}

new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(message)
.setNegativeButton(android.R.string.ok, null)
.show();
}
}
}

0 comments on commit 5874304

Please sign in to comment.