pelya / screen-dimmer-pixel-filter

Android screen dimmer, which disables individual pixels, for AMOLED screens
BSD 3-Clause "New" or "Revised" License
58 stars 14 forks source link

Convert to using SharedPreferences? #8

Open easyaspi314 opened 8 years ago

easyaspi314 commented 8 years ago

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.

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();
}

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 commented 8 years ago

@pelya any thoughts on this?

pelya commented 8 years ago

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) < notifications@github.com> 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

easyaspi314 commented 8 years ago

I was planning to work on quite a few changes.

Is that okay with you?

pelya commented 8 years ago

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) < notifications@github.com> 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

easyaspi314 commented 8 years ago

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.