diff --git a/app/src/main/java/com/jens/automation2/ActivityMaintenance.java b/app/src/main/java/com/jens/automation2/ActivityMaintenance.java index 75c74898..85810a19 100644 --- a/app/src/main/java/com/jens/automation2/ActivityMaintenance.java +++ b/app/src/main/java/com/jens/automation2/ActivityMaintenance.java @@ -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(); } } diff --git a/app/src/main/java/com/jens/automation2/Miscellaneous.java b/app/src/main/java/com/jens/automation2/Miscellaneous.java index d79d7f9b..b0d22ea9 100644 --- a/app/src/main/java/com/jens/automation2/Miscellaneous.java +++ b/app/src/main/java/com/jens/automation2/Miscellaneous.java @@ -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) { diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 68bc0c07..f4327eee 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -9,7 +9,7 @@ Crear lugar Crear regla Listo de lugares: - Listo del reglas: + Lista de reglas: Inserta un nombre válido, por favor. Inserta al menos un disparador, por favor. Inserta al menos un acción, por favor. @@ -118,4 +118,50 @@ Todavia hay reglas cuales usan este lugar (%1$s). No puedo borrar el. Reglajes generales. Inicializar al boot. + Guardar un archivo protocolo + Estado wifi + Descripción + Reglas + Intervalo + Configuración de ahorro de energia + Tiempo en minutos + exede + es menos que + Lugar + Intervalo + Velocidad + Wifi + Bluetooth + Enrutador wifi + Enrutador USB + encender Bluetooth + encender wifi + desactivar Bluetooth + desactivar Bluetooth + Abrir URL en antecedentes + Cambiar perfil sonido + encender enrutador USB + desactivar enrutador USB + encender enrutatdor wifi + desactivar enrutador wifi + encender modo de vuelo + desactivar modo de vuelo + Lugar activo + Lugar mas cerca + Posición + posiciónes + Servicio not esta activo + General + Encender servicio después un update si estuve activado + Encender servicio después un update + Cancelar + Nombre de wifi + Coneción a un wifi + exedendo + estendo menos que + algun wifi + Elega la app + Elega el paquete de la app + Elega la actividad de la app + Error encendiendo otra app \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4110439d..d46b73ce 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -650,4 +650,8 @@ Import configuration Export configuration More settings + Rules and locations have been imported successfully. + There was an error importing rules and locations. + Rules and locations exported successfully. + Error exporting rules and locations. \ No newline at end of file