Skip to content
This repository was archived by the owner on Feb 15, 2019. It is now read-only.
This repository was archived by the owner on Feb 15, 2019. It is now read-only.

Convert to using SharedPreferences? #8

@easyaspi314

Description

@easyaspi314

I was looking in the code, and currently you are using an ObjectOutputStream to save your data. This is unintuitive; it requires parsing the entire file and that is inefficient. It also isn't very forwards-compatible, seen as you have blank in.readInt(); inside the Init().

I was thinking that we could do a SharedPreferences implementation.
SharedPreferences are much easier.

  • They are stored in a key-value XML file, just like a strings.xml file.
  • It is useful for debugging because it is a readable XML file (when it is accessed either with root access or a backup).
  • They can be accessed independently using:
public static SharedPreferences prefs;
public static SharedPreferences.Editor editor;

public static void doStuff(Context ctx) {
    prefs = PreferenceManager.getDefaultSharedPreferences(ctx);

    // Get a value
    String str = prefs.getString("key1", "def_value");
    boolean bool = prefs.getBoolean("key2", true);

    // Set a value
    editor = prefs.edit();
    // put as many or as little as you want, just keep the same editor instance
    editor.putString("key1", "new_value");
    // it can be chained
    editor.putBoolean("key2", false)
          .putInt("key3", 314)
          .putString("key4", "new_value");
    // when you are done
    editor.apply();
}
  • If we were to soon migrate to a PreferenceFragment implementation, it is very easy to do.
  • Setting default values is easy.

The only thing that would be a slight issue is the custom patterns. But we can store it as a string of 1's and 0's.

Converting over the prefs should be rather easy:

public void importOldPrefs(Context ctx) {
    try {
        ObjectInputStream in = /* you know what I mean */ ...;
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt("pattern", in.getInt());
        .....
        editor.apply(); 
        // We don't need this anymore
        File dir = getFilesDir();
        File file = new File(dir, SettingsFileName);
        boolean deleted = file.delete();
    catch (AnyExceptionsNeeded e) {
        e.printStackTrace();
    }
} 

I could help on this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions