screen-dimmer-pixel-filter icon indicating copy to clipboard operation
screen-dimmer-pixel-filter copied to clipboard

Convert to using SharedPreferences?

Open easyaspi314 opened this issue 9 years ago • 5 comments

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.

easyaspi314 avatar May 01 '16 23:05 easyaspi314

@pelya any thoughts on this?

easyaspi314 avatar May 04 '16 21:05 easyaspi314

There were too few preferences, so I did not bother implementing it properly. If you'll send me a merge request, I'll merge it.

On Thu, May 5, 2016 at 12:17 AM, easyaspi314 (Devin) < [email protected]> wrote:

@pelya https://github.com/pelya any thoughts on this?

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/pelya/screen-dimmer-pixel-filter/issues/8#issuecomment-217005449

pelya avatar May 04 '16 21:05 pelya

I was planning to work on quite a few changes.

  • SharedPreferences
  • 4.1 support
  • AppCompat
  • Disabling the overlay for SuperSU
  • Testing screen

Is that okay with you?

easyaspi314 avatar May 04 '16 21:05 easyaspi314

Yes it's okay. It would be nice to also disable the overlay when Play Store app is launched, but I don't think you can get foreground app without root.

On Thu, May 5, 2016 at 12:25 AM, easyaspi314 (Devin) < [email protected]> wrote:

I was planning to work on quite a few changes.

  • SharedPreferences
  • 4.1 support
  • AppCompat
  • Disabling the overlay for SuperSU

Is that okay with you?

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/pelya/screen-dimmer-pixel-filter/issues/8#issuecomment-217007988

pelya avatar May 04 '16 21:05 pelya

You can too! :smile: 4.x and 5+ have different methods, though. I will have to figure it out.

I know Lux Lite could detect the foreground app and disable it if it was running.

easyaspi314 avatar May 04 '16 22:05 easyaspi314