Skip to content

Export/Import settings#1337

Open
matthiakl wants to merge 5 commits into
Julow:masterfrom
matthiakl:settings
Open

Export/Import settings#1337
matthiakl wants to merge 5 commits into
Julow:masterfrom
matthiakl:settings

Conversation

@matthiakl

Copy link
Copy Markdown
Contributor

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

@Julow Julow left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the slow reply. Thank you very much for working on this very nice feature :)

Comment thread res/values/strings.xml Outdated
Comment on lines +171 to +174
<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>

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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");

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's name it "unexpected-keyboard-prefs.txt" ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.startActivity(intent);
this.finish();
Runtime.getRuntime().exit(0);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the settings updated automatically after editor.apply() ? I'd prefer if the activity was not restarted.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it somehow is updated in memory, but the UI does not receive an update. I will try to find another way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some searching I found a simple solution. Instead of restarting, only the activity is refreshed by recalling onCreate().

Comment thread srcs/juloo.keyboard2/SettingsActivity.java Outdated
Comment thread srcs/juloo.keyboard2/SettingsActivity.java Outdated
Comment thread srcs/juloo.keyboard2/SettingsActivity.java Outdated
Object value = allPrefs.get(key);
if (value == null) continue;
String valueType = value.getClass().getSimpleName();
writer.write(key + "=" + valueType + ";" + value + "\n");

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced the format with a JSONArray.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backup/Restore settings

2 participants