Migration of old files unfinished.

This commit is contained in:
Jens 2021-03-12 22:38:52 +01:00
parent d803d57af1
commit bdfded960c
3 changed files with 49 additions and 38 deletions

View File

@ -312,17 +312,19 @@ public class Miscellaneous extends Service
public static String getWriteableFolder() public static String getWriteableFolder()
{ {
if(writeableFolderStringCache == null) if(writeableFolderStringCache == null)
{
if(!ActivityPermissions.havePermission(ActivityPermissions.writeExternalStoragePermissionName, Miscellaneous.getAnyContext()))
{ {
// Use the app-specific folder as new default. // Use the app-specific folder as new default.
writeableFolderStringCache = Miscellaneous.getAnyContext().getFilesDir().getAbsolutePath(); writeableFolderStringCache = Miscellaneous.getAnyContext().getFilesDir().getAbsolutePath();
return writeableFolderStringCache;
} File newConfigFile = new File(writeableFolderStringCache + "/" + XmlFileInterface.settingsFileName);
else
migration:
if (!newConfigFile.exists())
{ {
//TODO: We have the storage permission, probably because it's an old installation. Files should be migrated to app-specific folder. if (ActivityPermissions.havePermission(ActivityPermissions.writeExternalStoragePermissionName, Miscellaneous.getAnyContext()))
{
// We have the storage permission, probably because it's an old installation. Files should be migrated to app-specific folder.
String testPath = null; String testPath = null;
File folder = null; File folder = null;
@ -341,26 +343,33 @@ public class Miscellaneous extends Service
if (testFolder(f)) if (testFolder(f))
{ {
String pathToUse = f + "/" + Settings.folderName; String pathToUse = f + "/" + Settings.folderName;
Miscellaneous.logEvent("i", "Path", "Using " + pathToUse + " to store settings and log.", 2);
// Toast.makeText(getAnyContext(), "Using " + pathToUse + " to store settings and log.", Toast.LENGTH_LONG).show(); // Toast.makeText(getAnyContext(), "Using " + pathToUse + " to store settings and log.", Toast.LENGTH_LONG).show();
return pathToUse; // Migrate existing files
File oldDirectory = new File(pathToUse);
File newDirectory = new File(writeableFolderStringCache);
File oldConfigFilePath = new File(pathToUse + "/" + XmlFileInterface.settingsFileName);
if (oldConfigFilePath.exists() && oldConfigFilePath.canWrite())
{
Miscellaneous.logEvent("i", "Path", "Found old path " + pathToUse + " for settings and logs. Migrating old files to new directory.", 2);
for (File moveFile : oldDirectory.listFiles())
moveFile.renameTo(newDirectory);
String message = String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.filesHaveBeenMovedTo), newDirectory.getAbsolutePath());
Miscellaneous.writeStringToFile(oldDirectory.getAbsolutePath() + "readme.txt", message);
break migration;
}
} }
else
Miscellaneous.logEvent("e", "getWritableFolder", folder.getAbsolutePath() + " does not exist and could not be created.", 3);
} }
} catch (Exception e) } catch (Exception e)
{ {
Log.w("getWritableFolder", folder + " not writable."); Log.w("getWritableFolder", folder + " not writable.");
} }
// do not change to logEvent() - we can't write
Toast.makeText(getAnyContext(), "No writable folder could be found.", Toast.LENGTH_LONG).show();
Log.e("getWritableFolder", "No writable folder could be found.");
return null;
} }
} }
else }
return writeableFolderStringCache; return writeableFolderStringCache;
} }

View File

@ -25,7 +25,8 @@ import java.util.Collections;
public class XmlFileInterface public class XmlFileInterface
{ {
public static File settingsFile = new File(Miscellaneous.getWriteableFolder() + "/Automation_settings.xml"); public static String settingsFileName = "Automation_settings.xml";
public static File settingsFile = new File(Miscellaneous.getWriteableFolder() + "/" + settingsFileName);
public static Context context; public static Context context;
protected static final String encryptionKey = "Y1vsP12L2S3NkTJbDOR4bQ6i02hsoo"; protected static final String encryptionKey = "Y1vsP12L2S3NkTJbDOR4bQ6i02hsoo";

View File

@ -605,4 +605,5 @@
<string name="publishedOn">published on</string> <string name="publishedOn">published on</string>
<string name="displayNewsOnMainScreen">Display application news on main screen</string> <string name="displayNewsOnMainScreen">Display application news on main screen</string>
<string name="displayNewsOnMainScreenDescription">Announcements about this app only, we\'re probably talking about 1-2 per year, not more.</string> <string name="displayNewsOnMainScreenDescription">Announcements about this app only, we\'re probably talking about 1-2 per year, not more.</string>
<string name="filesHaveBeenMovedTo">Automation now uses another path to store your files. The existing ones have been moved. You can find them here: %s.</string>
</resources> </resources>