Add loading dialog
This commit is contained in:
parent
2859410d4b
commit
fb7f36007f
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
@ -25,65 +23,47 @@ public class ImportActivity extends Activity {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onActivityResult(int requestCode, int resultCode, Intent resultData) {
|
protected void onActivityResult(int requestCode, int resultCode, Intent resultData) {
|
||||||
if (requestCode == IMPORT_REQUEST_CODE) {
|
if (requestCode != IMPORT_REQUEST_CODE || resultCode != Activity.RESULT_OK) {
|
||||||
if (resultCode == Activity.RESULT_OK) {
|
finish();
|
||||||
|
}
|
||||||
|
|
||||||
final Uri treeUri = resultData.getData();
|
final Uri treeUri = resultData.getData();
|
||||||
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
final Intent intent = new Intent(this, MainActivity.class);
|
private void copyFiles(DocumentFile treeDocument) {
|
||||||
|
ProgressDialog dialog = createProgressDialog();
|
||||||
|
dialog.show();
|
||||||
|
|
||||||
|
new Thread(() -> {
|
||||||
|
ContentResolver contentResolver = getContentResolver();
|
||||||
|
File externalFilesDir = getExternalFilesDir(null);
|
||||||
|
FileUtils.copyRecursively(contentResolver, treeDocument, externalFilesDir);
|
||||||
|
|
||||||
|
startMainActivity();
|
||||||
|
dialog.dismiss();
|
||||||
|
finish();
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startMainActivity() {
|
||||||
|
Intent intent = new Intent(this, MainActivity.class);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
finish();
|
private ProgressDialog createProgressDialog() {
|
||||||
} else {
|
ProgressDialog progressDialog = new ProgressDialog(this,
|
||||||
super.onActivityResult(requestCode, resultCode, resultData);
|
android.R.style.Theme_Material_Light_Dialog);
|
||||||
}
|
progressDialog.setTitle(R.string.loading_dialog_title);
|
||||||
}
|
progressDialog.setMessage(getString(R.string.loading_dialog_message));
|
||||||
|
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
|
||||||
|
progressDialog.setCancelable(false);
|
||||||
|
|
||||||
private boolean copyRecursively(DocumentFile src, File dest) {
|
return progressDialog;
|
||||||
final DocumentFile[] documentFiles = src.listFiles();
|
|
||||||
for (final DocumentFile documentFile : documentFiles) {
|
|
||||||
if (documentFile.isFile()) {
|
|
||||||
if (!copyFile(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(documentFile, subdirectory)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean copyFile(DocumentFile src, File dest) {
|
|
||||||
try {
|
|
||||||
final InputStream inputStream = getContentResolver().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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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>
|
||||||
|
|
Loading…
Reference in New Issue