Implementing 'profiles' that load/save sets of preferences? - java

I'm working on a live wallpaper that is configured using a preferences screen. I use shared preferences to store things like color, speed and movement of the wallpaper. As I've got a lot of settings, I'd like users to be able to:
save all the current preferences under a profile, where the profile is assign a name by the user.
the user can then select a profile by name from a list and have all their settings restored.
the user can also delete profiles.
Can anyone recommend a nice way of doing this?
One idea I had was to save all the current preferences to an XML file and selecting a profile would just load the file and set the shared preferences based on this. However, if I had 20 or so profiles, I would need to inspect all the files to produce a list of profile names (as I'd have to store the profile name in each file) which seem inefficient.

I implemented a profile system in one of my apps with the help of an SQLiteDatabase (basics are explained in the notepad tutorial). You only need one table to store profile name/id, and all of the settings you want.
save each profile as a new record in your table, with an appropriate name field
retrieve all records and display the list of names when you want to user to be able to select a profile (e.g. SELECT * FROM Profile)
delete a profile in your system by deleting the corresponding record (e.g. DELETE FROM Profile WHERE name='john')
I also saved the name/identifier of the current profile using SharedPreferences so that my system had an easy way to know which profile was currently/most recently active, outside of the context of profile loading.

Related

pre-populate shared preference file

I'm just starting to write a new android app and wanted to have a clean design.
I want some variables to be initiated with a default value. Those default values should all be in one file called settings (or something similar). However the user should be able to change some of those values and save them.
Changing user settings is normally done in shared preferences files. However I did not found out how I can pre-populate them with my default values.
Is there any other way how I can have some sort of a settings file that already has values in it? I don't want to hardcode values in my java application as I want to have a single file to adjust when I need to change the default values.
Secondary goal: I would like if the user would not lose his saved values when the app updates.
Can anyone give me some hints where to search?

Methodology for saving field data between application sessions

I'm working on an application that requires I save a field between application uses, so that I can retrieve the data during the next use (ex. save username when logged in, retrieve it and pre-populate username field when application is next launched). I've looked around and found a couple ways to accomplish this goal, such as encryption & saving in a file on the local machine, and saving the value in the Windows registry. Are any of these methods better than others for accomplishing this goal? Any other suggestions?
You can store it using the Java Preferences API which will store a small amount of data in a system-specific way. On Windows, I believe it stores it in the registry, but on a Mac or on Linux it will be stored in a manner appropriate to those systems.
The data is stored, for the current user, under a key that is specific to the package of your class plus a string of your choosing. See the Preferences API Overview for more details.

How can I load a predefined rule set to a sonar profile

i have a local sonar server running. i would like to create a new profile with a set of rules that were predefined by someone else. i have the XML file containing all the rules.
is there a way to upload the XML file to the profile and not define the rules manually?
thanks
Make sure you are logged into Sonar.
Click on Quality Profiles in the top navigation bar, then click on "Restore Profile" on the right hand side under the search bar. Make sure you have all the applicable quality plugins that the export is using or the restore will choke.
May be a bit old thread but worth answering this question with exact steps as I struggled alot with this problem.
Though its a hacky way to import rulebook from prod to your local. But it works and can save valuable time.
Here are the steps:
Login to SonarQube as admin.
Create a new Quality Profile as below:
Download the default xml from local sonarqube by clicking on Backup button. Lets call this file local-rulebook.xml
Open both the prod-rulebook.xml file & local-rulebook.xml in notepad.
Delete the selected part from local-rulebook.xml & replace with the content from prod-rulebook.xml. Save the file.
P.S: Do not change the value of <profile><name>{yourProfileName}</name>.
Go back to sonarQube and Click Restore at top-right corner of the screen.
Import the newly updated file
Change the default profile type.
Congrats you successfully imported the prod-rulebook.xml to your local sonarqube.

Java How do you use the preference API? Where do these variables store?

Say I have the following
Preferences prefs = Preferences.userRoot().node(this.getClass().getName());
String ID1 = "Test1";
System.out.println(prefs.getBoolean(ID1, true));
prefs.putBoolean(ID1, false);
//prefs.remove(ID1);
Is this variable persistent the next time I execute my program?
Where do these variables store?
What is the proper way of utilizing this?
Is the approach better than using properties files?
Yes, the value is persistent but only for the user. It won't be there for other users.
This is OS specific. For Windows it uses the registry, for Linux I believe it uses hidden files in the user root, although I'm not 100% sure.
You've got a pretty good example in your question.
It is different, not better. Preferences are a way of transparently storing settings for an application. These settings may be updated in run time by a user (for example you could use prefs to store user specific settings). Preferences are not meant to be editable outside of the application. Properties files tend to store hard setting specific to an application. These settings are the same for each user and tend not to change often. Properties files are text files and tend to accompany an application on deployment. You can edit them easily using a text editor. It is fairly rare for an application to update properties files.

How to deploy a Java Swing application with an embedded JavaDB database?

I have implemented an Java Swing application that uses an embedded JavaDB database. The database need to be stored somewhere and the database tables need to be created at the first run. What is the preferred way to do these procedures?
Should I always create the database in the local directory, and first check if the database file exist, and if it doesn't exist let the user create the tables (or at least show a message that the tables will be created).
Or should I let the user choose a path? but then I have to save the path somewhere. Should I save the path with Preferences.systemRoot();, and check if that variable is set on startup?
If the user choses a path and save it in the Preferences, can I get any problems with user permissions? or should it be safe wherever the user store the database? Or how do I handle this?
Any other suggestions for this procedure?
I would let the user choose. That way they can run multiple instances (simultaneously or otherwise). Supplying a sensible default would be a good move.
Store the path using the Preferences API (I would store per-user but that may differ depending on your application), and use the File object to determine if directories exist and/or are permissioned properly.

Categories

Resources