Import/export configuration and Spanish translation.

This commit is contained in:
2021-05-10 22:42:16 +02:00
parent 09298bce55
commit 514a9ae0e4
4 changed files with 196 additions and 20 deletions

View File

@ -119,21 +119,62 @@ public class ActivityMaintenance extends Activity
if(resultCode == RESULT_OK)
{
Uri uriTree = data.getData();
DocumentFile directory = DocumentFile.fromTreeUri(this, uriTree);
for(DocumentFile file : directory.listFiles())
{
if(file.canRead() && file.getName().equals(XmlFileInterface.settingsFileName))
{
// import rules, locations, etc.
Miscellaneous.copyFileUsingStream(file, Miscellaneous.getWriteableFolder() + "/" + XmlFileInterface.settingsFileName);
}
else if(false && file.canRead() && file.getName().equals(XmlFileInterface.settingsFileName))
{
// import rules, locations, etc.
}
}
importFiles(uriTree);
}
break;
case requestCodeExport:
if(resultCode == RESULT_OK)
{
Uri uriTree = data.getData();
exportFiles(uriTree);
}
break;
}
}
void importFiles(Uri uriTree)
{
// https://stackoverflow.com/questions/46237558/android-strange-behavior-of-documentfile-inputstream
DocumentFile directory = DocumentFile.fromTreeUri(this, uriTree);
for(DocumentFile file : directory.listFiles())
{
if(file.canRead() && file.getName().equals(XmlFileInterface.settingsFileName))
{
// import rules, locations, etc.
if(Miscellaneous.copyDocumentFileToFile(file, new File(Miscellaneous.getWriteableFolder() + "/" + XmlFileInterface.settingsFileName + "2")))
{
// reload file
Toast.makeText(ActivityMaintenance.this, getResources().getString(R.string.rulesImportedSuccessfully), Toast.LENGTH_LONG).show();
}
else
Toast.makeText(ActivityMaintenance.this, getResources().getString(R.string.rulesImportError), Toast.LENGTH_LONG).show();
}
else if(false && file.canRead() && file.getName().equals(XmlFileInterface.settingsFileName))
{
// import rules, locations, etc.
}
}
}
void exportFiles(Uri uriTree)
{
DocumentFile directory = DocumentFile.fromTreeUri(this, uriTree);
File src = new File(Miscellaneous.getWriteableFolder() + "/" + XmlFileInterface.settingsFileName);
DocumentFile dst = directory.createFile("text/xml", XmlFileInterface.settingsFileName);
if(dst.canWrite())
{
// import rules, locations, etc.
if(Miscellaneous.copyFileToDocumentFile(src, dst))
{
// reload file
Toast.makeText(ActivityMaintenance.this, getResources().getString(R.string.rulesExportedSuccessfully), Toast.LENGTH_LONG).show();
}
else
Toast.makeText(ActivityMaintenance.this, getResources().getString(R.string.rulesExportError), Toast.LENGTH_LONG).show();
}
}

View File

@ -1215,7 +1215,85 @@ public class Miscellaneous extends Service
return returnValue;
}
public static String copyDocumentFile(String inputPath, String inputFile, Uri treeUri)
public static boolean copyDocumentFileToFile(DocumentFile src, File dst)
{
InputStream in = null;
OutputStream out = null;
String error = null;
try
{
in = Miscellaneous.getAnyContext().getContentResolver().openInputStream(src.getUri());
out = new FileOutputStream(dst);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
in.close();
// write the output file (You have now copied the file)
out.flush();
out.close();
return true;
}
catch (FileNotFoundException fnfe1)
{
error = fnfe1.getMessage();
}
catch (Exception e)
{
error = e.getMessage();
}
return false;
// return error;
}
public static boolean copyFileToDocumentFile(File src, DocumentFile dst)
{
InputStream in = null;
OutputStream out = null;
String error = null;
try
{
in = new FileInputStream(src);
out = Miscellaneous.getAnyContext().getContentResolver().openOutputStream(dst.getUri());
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
in.close();
// write the output file (You have now copied the file)
out.flush();
out.close();
return true;
}
catch (FileNotFoundException fnfe1)
{
error = fnfe1.getMessage();
}
catch (Exception e)
{
error = e.getMessage();
}
return false;
// return error;
}
/*public static String copyDocumentFile(String inputPath, String inputFile, Uri treeUri)
{
InputStream in = null;
OutputStream out = null;
@ -1223,14 +1301,16 @@ public class Miscellaneous extends Service
DocumentFile pickedDir = DocumentFile.fromTreeUri(getActivity(), treeUri);
String extension = inputFile.substring(inputFile.lastIndexOf(".")+1,inputFile.length());
try {
try
{
DocumentFile newFile = pickedDir.createFile("audio/"+extension, inputFile);
out = getActivity().getContentResolver().openOutputStream(newFile.getUri());
in = new FileInputStream(inputPath + inputFile);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
in.close();
@ -1238,13 +1318,18 @@ public class Miscellaneous extends Service
out.flush();
out.close();
} catch (FileNotFoundException fnfe1) {
}
catch (FileNotFoundException fnfe1)
{
error = fnfe1.getMessage();
} catch (Exception e) {
}
catch (Exception e)
{
error = e.getMessage();
}
return error;
}
}*/
public static boolean googleToBlameForLocation(boolean checkExistingRules)
{