diff --git a/app/src/main/java/com/frc63175985/csp/FileManager.java b/app/src/main/java/com/frc63175985/csp/FileManager.java index d382f6e..1ae5e3a 100644 --- a/app/src/main/java/com/frc63175985/csp/FileManager.java +++ b/app/src/main/java/com/frc63175985/csp/FileManager.java @@ -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; @@ -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"); @@ -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) { @@ -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(); } @@ -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; - } } diff --git a/app/src/main/java/com/frc63175985/csp/PitScoutingActivity.java b/app/src/main/java/com/frc63175985/csp/PitScoutingActivity.java index 054e910..3e5b626 100644 --- a/app/src/main/java/com/frc63175985/csp/PitScoutingActivity.java +++ b/app/src/main/java/com/frc63175985/csp/PitScoutingActivity.java @@ -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; } @@ -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; } diff --git a/app/src/main/java/com/frc63175985/csp/QrAggregatorActivity.java b/app/src/main/java/com/frc63175985/csp/QrAggregatorActivity.java index 20d06ff..c914595 100644 --- a/app/src/main/java/com/frc63175985/csp/QrAggregatorActivity.java +++ b/app/src/main/java/com/frc63175985/csp/QrAggregatorActivity.java @@ -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; @@ -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; @@ -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(); @@ -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(); } } }