0

I'm writing some Android code that downloads the application EULA as a String and stores it in the SharedPreferences so we can check it later. The general idea is that the user should only have to agree to the EULA once, but if the terms are updated we want to notify the user and have them agree again.

Here is some example code, which takes place within an AsyncTask's doInBackground method:

String currentEula = getEulaText(); //I've confirmed this method correctly downloads the text
String oldEula = mPrefs.getString("eula_text", "");
if(!(oldEula.equals(currentEula)){
  SharedPreferences.Editor editor = mPrefs.edit();
  editor.putString("eula_text", currentEula);
  editor.commit();
  runOnUiThread(new Runnable {
    @Override
    public void run() {
      showEula() //this handles the actual agreement stuff, works correctly
    }
  }
}

However, currentEula and oldEula are never equal in this example, so every time I open the app the EULA gets displayed. When I printed the length of both Strings, I found that the String coming from mPrefs is 4 characters longer than the one that gets downloaded. I added a log statement to print the 4 extra characters, and they are blank. If I compare the remainder of the Strings, they are equal.

To make matters even stranger, after calling editor.commit(), this comparison is true:

mPrefs.getString("eula_text", "").equals(currentEula);

So the value of the String retrieved from mPrefs changes between the end of this function and the time when it gets called again, but I've confirmed that no other part of the application touches the "eula_text" value (and even if it did, why on Earth would it just add four blank spaces to the end?).

I could just strip the last 4 characters from the String after retrieving it from mPrefs, but I'm worried they may not always be blank. Alternately, I could do this comparison:

oldEula.substring(0, currentEula.length()).equals(currentEula)

However, this yields a false positive in the case where a EULA update simply strips the last item (not super likely, but still possible). Does anyone know why my String would randomly have 4 blank characters tacked onto it?

Patrick Grayson
  • 548
  • 3
  • 17
  • 1
    Have u tried using `contains` instead of `equals`? or use `trim()` to remove unnecessary space from both strings before comparing – ρяσѕρєя K Jun 24 '15 at 16:13
  • `runOnUiThread(new Runnable {` gross. Update the UI in the methods that `AsyncTask` provides – codeMagic Jun 24 '15 at 16:13
  • Not sure why the extra characters would be there, but have you tried calling `currentEula = currentEula.trim()` to remove the spaces? If they're just blank spaces, you can safely remove them with trim, and if they happen to not be blank spaces, trim won't remove them. – Joseph Roque Jun 24 '15 at 16:14
  • Also, the reason `mPrefs.getString("eula_text", "").equals(currentEula);` evaluates to true after calling `editor.commit()` is because what you've done is stored `currentEula` in the preferences, overriding the version without the spaces, and retrieving the exact string you just put in, moments later, so of course it's going to evaluate to true now. – Joseph Roque Jun 24 '15 at 16:16
  • currentEula.trim() does seem to solve the problem, so that's good news. – Patrick Grayson Jun 24 '15 at 16:24
  • @JosephRoque I understand that I just stored it and that it should be equal when I pull it out again, but then since it isn't modified between being stored and being read again the next time the AsyncTask starts, it should be equal then as well. I'm remarking on the oddness that the extra characters (apparently) don't get added during the storage or retrieval methods, but rather some time in between. – Patrick Grayson Jun 24 '15 at 16:29

1 Answers1

1

You can store the current EULA + hash of your current EULA.

Then you can compare the hashes to see if there has been a change in the EULA.

LordRaydenMK
  • 13,074
  • 5
  • 50
  • 56
  • I thought this might be the best way to go originally, actually. Is String.hashCode() sufficient for this, or would I need to write my own hash function? – Patrick Grayson Jun 24 '15 at 16:20
  • @user3174484 It should be sufficient. Give it a try. Alternatively check out this question http://stackoverflow.com/questions/6120657/how-to-generate-a-unique-hash-code-for-string-input-in-android – LordRaydenMK Jun 24 '15 at 16:23
  • Seems that they hash differently as well due to extra characters, but I'll probably combine this with the String.trim() recommendation from above to get a working solution that doesn't involve comparing 9000+ character Strings haha. Thanks for the help! – Patrick Grayson Jun 24 '15 at 16:36