Export/Import settings#1337
Conversation
Julow
left a comment
There was a problem hiding this comment.
Sorry for the slow reply. Thank you very much for working on this very nice feature :)
| <string name="export_success">Settings exported</string> | ||
| <string name="export_fail">Settings export failed</string> | ||
| <string name="import_success">Settings imported, app will restart</string> | ||
| <string name="import_fail">Settings import failed</string> |
There was a problem hiding this comment.
I think there's too many strings. Do we need all these toast message ? Importing and exporting should be instantaneous.
I think only the error case need a toast message.
There was a problem hiding this comment.
Done. I also merged the failure into a single message.
| Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); | ||
| intent.addCategory(Intent.CATEGORY_OPENABLE); | ||
| intent.setType("text/plain"); | ||
| intent.putExtra(Intent.EXTRA_TITLE, "prefs.txt"); |
There was a problem hiding this comment.
Let's name it "unexpected-keyboard-prefs.txt" ?
| intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | ||
| this.startActivity(intent); | ||
| this.finish(); | ||
| Runtime.getRuntime().exit(0); |
There was a problem hiding this comment.
Isn't the settings updated automatically after editor.apply() ? I'd prefer if the activity was not restarted.
There was a problem hiding this comment.
No, it somehow is updated in memory, but the UI does not receive an update. I will try to find another way.
There was a problem hiding this comment.
After some searching I found a simple solution. Instead of restarting, only the activity is refreshed by recalling onCreate().
| Object value = allPrefs.get(key); | ||
| if (value == null) continue; | ||
| String valueType = value.getClass().getSimpleName(); | ||
| writer.write(key + "=" + valueType + ";" + value + "\n"); |
There was a problem hiding this comment.
The format breaks if value ever contains a newline character. For some reason this doesn't happen but nothing in this code prevents it.
Custom layouts can contain newlines but by chance, when converted to JSON (in ListGroupPreference), we use the compact encoding. This was not made on purpose and there's chance that we break this in the future.
To avoid any format problems, I suggest switching to JSON. It might be worth looking at this constructor, which takes a Map as input: https://developer.android.com/reference/kotlin/org/json/JSONObject#JSONObject(kotlin.collections.MutableMap)
There was a problem hiding this comment.
I replaced the format with a JSONArray.
Provide menu entries to export and import settings to a text file, including custom layout definitions.
Resolves #856 and #636
Code inspired by grocy-android