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?

6
  • 1
    Have u tried using contains instead of equals? or use trim() to remove unnecessary space from both strings before comparing Commented Jun 24, 2015 at 16:13
  • runOnUiThread(new Runnable { gross. Update the UI in the methods that AsyncTask provides Commented Jun 24, 2015 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. Commented Jun 24, 2015 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. Commented Jun 24, 2015 at 16:16
  • currentEula.trim() does seem to solve the problem, so that's good news. Commented Jun 24, 2015 at 16:24

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
@user3174484 It should be sufficient. Give it a try. Alternatively check out this question stackoverflow.com/questions/6120657/…
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.