Skip to content

Commit

Permalink
Add confirmation popup for overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenCWills authored and AJenbo committed Nov 4, 2023
1 parent 9d7c6e3 commit ed1a2a4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Objects;

public class ImportActivity extends Activity {
Expand Down Expand Up @@ -52,31 +53,88 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
return;

if (resultCode == Activity.RESULT_OK && data != null) {
importFile(data.getData());
handleClipData(data.getClipData());
}
ArrayList<Uri> uriList = getItemUris(data.getClipData());

Uri dataUri = data.getData();
if (dataUri != null)
uriList.add(dataUri);

ArrayList<String> fileNames = getFileNames(uriList);
ArrayList<String> overwrittenFiles = getOverwrittenFiles(fileNames);
if (overwrittenFiles.isEmpty()) {
importFiles(uriList);
finish();
return;
}

String overwrittenFileList = String.join("\n", overwrittenFiles);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.overwrite_query, overwrittenFileList));
builder.setPositiveButton(R.string.continue_button, (dialog, which) -> { importFiles(uriList); });
builder.setNegativeButton(R.string.cancel_button, null);
builder.setOnDismissListener(dialog -> finish());

finish();
AlertDialog dialog = builder.create();
dialog.show();
}
}

private void handleClipData(ClipData clipData) {
private ArrayList<Uri> getItemUris(ClipData clipData) {
ArrayList<Uri> uriList = new ArrayList<>();
if (clipData == null)
return;
return uriList;

for (int i = 0; i < clipData.getItemCount(); i++) {
ClipData.Item item = clipData.getItemAt(i);
if (item == null)
continue;

importFile(item.getUri());
Uri itemUri = item.getUri();
if (itemUri == null)
continue;

uriList.add(itemUri);
}

return uriList;
}

private ArrayList<String> getFileNames(ArrayList<Uri> uriList) {
ArrayList<String> fileNames = new ArrayList<>();
for (Uri uri : uriList) {
DocumentFile file = DocumentFile.fromSingleUri(getApplicationContext(), uri);
if (file == null)
continue;

String fileName = file.getName();
fileNames.add(fileName);
}
return fileNames;
}

private ArrayList<String> getOverwrittenFiles(ArrayList<String> fileNames) {
ArrayList<String> overwrittenFiles = new ArrayList<>();
ExternalFilesManager fileManager = new ExternalFilesManager(this);
for (String fileName : fileNames) {
if (fileManager.hasFile(fileName))
overwrittenFiles.add(fileName);
}
return overwrittenFiles;
}

private void importFiles(ArrayList<Uri> uriList) {
for (Uri uri : uriList)
importFile(uri);
}

private void importFile(Uri fileUri) {
if (fileUri == null)
return;

DocumentFile file = Objects.requireNonNull(DocumentFile.fromSingleUri(getApplicationContext(), fileUri));
DocumentFile file = DocumentFile.fromSingleUri(getApplicationContext(), fileUri);
if (file == null)
return;

String fileName = file.getName();
ExternalFilesManager fileManager = new ExternalFilesManager(this);
File externalFile = fileManager.getFile(fileName);
Expand Down
3 changes: 3 additions & 0 deletions android-project/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
<string name="import_short_label">Import Data</string>
<string name="gamepad_tv_text">This game requires a gamepad to play</string>
<string name="import_data_info">On the next screen, select files that will be imported into:\n\n%1$s</string>
<string name="overwrite_query">The following files will be overwritten. Is this okay?\n\n%1$s</string>
<string name="ok_button">OK</string>
<string name="continue_button">Continue</string>
<string name="cancel_button">Cancel</string>
</resources>

0 comments on commit ed1a2a4

Please sign in to comment.