Add loading dialog (#125)

Co-authored-by: Alexander Batalov <alex.batalov@gmail.com>
This commit is contained in:
Edgar Miró 2022-08-17 08:32:08 +02:00 committed by GitHub
parent 354b0812c9
commit 4eb5e39946
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 43 deletions

View File

@ -0,0 +1,56 @@
package com.alexbatalov.fallout2ce;
import android.content.ContentResolver;
import androidx.documentfile.provider.DocumentFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileUtils {
static boolean copyRecursively(ContentResolver contentResolver, DocumentFile src, File dest) {
final DocumentFile[] documentFiles = src.listFiles();
for (final DocumentFile documentFile : documentFiles) {
if (documentFile.isFile()) {
if (!copyFile(contentResolver, documentFile, new File(dest, documentFile.getName()))) {
return false;
}
} else if (documentFile.isDirectory()) {
final File subdirectory = new File(dest, documentFile.getName());
if (!subdirectory.exists()) {
subdirectory.mkdir();
}
if (!copyRecursively(contentResolver, documentFile, subdirectory)) {
return false;
}
}
}
return true;
}
private static boolean copyFile(ContentResolver contentResolver, DocumentFile src, File dest) {
try {
final InputStream inputStream = contentResolver.openInputStream(src.getUri());
final OutputStream outputStream = new FileOutputStream(dest);
final byte[] buffer = new byte[16384];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
}

View File

@ -1,6 +1,8 @@
package com.alexbatalov.fallout2ce; package com.alexbatalov.fallout2ce;
import android.app.Activity; import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
@ -8,10 +10,6 @@ import android.os.Bundle;
import androidx.documentfile.provider.DocumentFile; import androidx.documentfile.provider.DocumentFile;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class ImportActivity extends Activity { public class ImportActivity extends Activity {
private static final int IMPORT_REQUEST_CODE = 1; private static final int IMPORT_REQUEST_CODE = 1;
@ -31,10 +29,8 @@ public class ImportActivity extends Activity {
if (treeUri != null) { if (treeUri != null) {
final DocumentFile treeDocument = DocumentFile.fromTreeUri(this, treeUri); final DocumentFile treeDocument = DocumentFile.fromTreeUri(this, treeUri);
if (treeDocument != null) { if (treeDocument != null) {
copyRecursively(treeDocument, getExternalFilesDir(null)); copyFiles(treeDocument);
return;
final Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
} }
} }
} }
@ -45,45 +41,34 @@ public class ImportActivity extends Activity {
} }
} }
private boolean copyRecursively(DocumentFile src, File dest) { private void copyFiles(DocumentFile treeDocument) {
final DocumentFile[] documentFiles = src.listFiles(); ProgressDialog dialog = createProgressDialog();
for (final DocumentFile documentFile : documentFiles) { dialog.show();
if (documentFile.isFile()) {
if (!copyFile(documentFile, new File(dest, documentFile.getName()))) { new Thread(() -> {
return false; ContentResolver contentResolver = getContentResolver();
} File externalFilesDir = getExternalFilesDir(null);
} else if (documentFile.isDirectory()) { FileUtils.copyRecursively(contentResolver, treeDocument, externalFilesDir);
final File subdirectory = new File(dest, documentFile.getName());
if (!subdirectory.exists()) { startMainActivity();
subdirectory.mkdir(); dialog.dismiss();
finish();
}).start();
} }
if (!copyRecursively(documentFile, subdirectory)) { private void startMainActivity() {
return false; Intent intent = new Intent(this, MainActivity.class);
} startActivity(intent);
}
}
return true;
} }
private boolean copyFile(DocumentFile src, File dest) { private ProgressDialog createProgressDialog() {
try { ProgressDialog progressDialog = new ProgressDialog(this,
final InputStream inputStream = getContentResolver().openInputStream(src.getUri()); android.R.style.Theme_Material_Light_Dialog);
final OutputStream outputStream = new FileOutputStream(dest); progressDialog.setTitle(R.string.loading_dialog_title);
progressDialog.setMessage(getString(R.string.loading_dialog_message));
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCancelable(false);
final byte[] buffer = new byte[16384]; return progressDialog;
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
} }
} }

View File

@ -1,3 +1,5 @@
<resources> <resources>
<string name="app_name">Fallout 2</string> <string name="app_name">Fallout 2</string>
<string name="loading_dialog_title">PLEASE STAND BY</string>
<string name="loading_dialog_message">Copying files…</string>
</resources> </resources>