diff --git a/app/src/main/java/com/jens/automation2/ActivityMaintenance.java b/app/src/main/java/com/jens/automation2/ActivityMaintenance.java index 85810a19..8b5a217a 100644 --- a/app/src/main/java/com/jens/automation2/ActivityMaintenance.java +++ b/app/src/main/java/com/jens/automation2/ActivityMaintenance.java @@ -7,6 +7,7 @@ import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; +import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; @@ -24,6 +25,8 @@ public class ActivityMaintenance extends Activity final static int requestCodeExport = 1002; final static int requestCodeMoreSettings = 6000; + final static String prefsFileName = "com.jens.automation2_preferences.xml"; + TextView tvFileStoreLocation; Button bVolumeTest, bMoreSettings, bSettingsSetToDefault, bShareConfigAndLog, bImportConfiguration, bExportConfiguration; @@ -136,46 +139,106 @@ public class ActivityMaintenance extends Activity { // https://stackoverflow.com/questions/46237558/android-strange-behavior-of-documentfile-inputstream + File dstRules = new File(Miscellaneous.getWriteableFolder() + "/" + XmlFileInterface.settingsFileName); + File dstPrefs = new File(Miscellaneous.getWriteableFolder() + "/../shared_prefs/" + prefsFileName); + DocumentFile directory = DocumentFile.fromTreeUri(this, uriTree); - for(DocumentFile file : directory.listFiles()) + + int applicableFilesFound = 0; + int filesImported = 0; + + if(directory.listFiles().length > 0) { - if(file.canRead() && file.getName().equals(XmlFileInterface.settingsFileName)) + for (DocumentFile file : directory.listFiles()) { - // import rules, locations, etc. - if(Miscellaneous.copyDocumentFileToFile(file, new File(Miscellaneous.getWriteableFolder() + "/" + XmlFileInterface.settingsFileName + "2"))) + if (file.getName().equals(XmlFileInterface.settingsFileName)) { - // reload file - Toast.makeText(ActivityMaintenance.this, getResources().getString(R.string.rulesImportedSuccessfully), Toast.LENGTH_LONG).show(); + applicableFilesFound++; + + if(file.canRead()) + { + // import rules, locations, etc. + if (Miscellaneous.copyDocumentFileToFile(file, dstRules)) + filesImported++; + else + Toast.makeText(ActivityMaintenance.this, getResources().getString(R.string.rulesImportError), Toast.LENGTH_LONG).show(); + } + } + else if (file.getName().equals(prefsFileName)) + { + applicableFilesFound++; + + if(file.canRead()) + { + // import rules, locations, etc. + if (Miscellaneous.copyDocumentFileToFile(file, dstPrefs)) + filesImported++; + else + Toast.makeText(ActivityMaintenance.this, getResources().getString(R.string.prefsImportError), Toast.LENGTH_LONG).show(); + } + } + } + + if(applicableFilesFound > 0) + { + if(filesImported == 0) + Toast.makeText(ActivityMaintenance.this, getResources().getString(R.string.noFilesImported), Toast.LENGTH_LONG).show(); + else if(filesImported < applicableFilesFound) + Toast.makeText(ActivityMaintenance.this, getResources().getString(R.string.notAllFilesImported), Toast.LENGTH_LONG).show(); + else if (filesImported == applicableFilesFound) + { + Toast.makeText(ActivityMaintenance.this, getResources().getString(R.string.configurationImportedSuccessfully), Toast.LENGTH_LONG).show(); + + try + { + XmlFileInterface.readFile(); + } + catch (Exception e) + { + Miscellaneous.logEvent("e", "Reading import", "Rules re-read failed: " + Log.getStackTraceString(e), 1); + Toast.makeText(ActivityMaintenance.this, getResources().getString(R.string.errorReadingPoisAndRulesFromFile), Toast.LENGTH_LONG).show(); + } + + Settings.readFromPersistentStorage(ActivityMaintenance.this); } 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. + Toast.makeText(ActivityMaintenance.this, getResources().getString(R.string.noFilesImported), Toast.LENGTH_LONG).show(); } + else + Toast.makeText(ActivityMaintenance.this, getResources().getString(R.string.noApplicableFilesFoundInDirectory), Toast.LENGTH_LONG).show(); } + else + Toast.makeText(ActivityMaintenance.this, getResources().getString(R.string.noApplicableFilesFoundInDirectory), Toast.LENGTH_LONG).show(); } void exportFiles(Uri uriTree) { DocumentFile directory = DocumentFile.fromTreeUri(this, uriTree); - File src = new File(Miscellaneous.getWriteableFolder() + "/" + XmlFileInterface.settingsFileName); + File srcRules = new File(Miscellaneous.getWriteableFolder() + "/" + XmlFileInterface.settingsFileName); + File srcPrefs = new File(Miscellaneous.getWriteableFolder() + "/../shared_prefs/" + prefsFileName); - DocumentFile dst = directory.createFile("text/xml", XmlFileInterface.settingsFileName); - - if(dst.canWrite()) + // Clean up + for(DocumentFile file : directory.listFiles()) { - // 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(); + if(file.getName().equals(XmlFileInterface.settingsFileName) && file.canWrite()) + file.delete(); + else if(file.getName().equals(prefsFileName) && file.canWrite()) + file.delete(); } + + DocumentFile dstRules = directory.createFile("text/xml", XmlFileInterface.settingsFileName); + DocumentFile dstPrefs = directory.createFile("text/xml", prefsFileName); + + if(dstRules.canWrite() && dstPrefs.canWrite()) + { + if(Miscellaneous.copyFileToDocumentFile(srcRules, dstRules) && Miscellaneous.copyFileToDocumentFile(srcPrefs, dstPrefs)) + Toast.makeText(ActivityMaintenance.this, getResources().getString(R.string.configurationExportedSuccessfully), Toast.LENGTH_LONG).show(); + else + Toast.makeText(ActivityMaintenance.this, getResources().getString(R.string.ConfigurationExportError), Toast.LENGTH_LONG).show(); + } + else + Toast.makeText(ActivityMaintenance.this, getResources().getString(R.string.ConfigurationExportError), Toast.LENGTH_LONG).show(); } private static AlertDialog getDefaultSettingsDialog(final Context context) diff --git a/app/src/main/res/layout/activity_maintenance.xml b/app/src/main/res/layout/activity_maintenance.xml index 92b30d28..4882733c 100644 --- a/app/src/main/res/layout/activity_maintenance.xml +++ b/app/src/main/res/layout/activity_maintenance.xml @@ -42,6 +42,12 @@ android:layout_height="wrap_content" android:text="@string/defaultSettings" /> +