Created
October 22, 2013 20:30
-
-
Save adennie/7107587 to your computer and use it in GitHub Desktop.
detect first run after install/upgrade
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void handleNewInstallsAndUpgrades() { | |
int savedVersion = readVersionCodeFromPref(PREF_TAG_CURRENT_APP_VERSION); | |
if (savedVersion == -1) | |
processNewInstall(getVersionCode()); | |
else if (savedVersion != getVersionCode()) | |
processUpgrade(getVersionCode(), readVersionCodeFromPref(PREF_TAG_PREVIOUS_APP_VERSION)); | |
} | |
protected void processNewInstall(final int newVersionCode) { | |
updateSavedVersionCodes(); | |
} | |
protected void processUpgrade(final int newVersionCode, | |
final int prevInstallVersionCode) { | |
updateSavedVersionCodes(); | |
} | |
private void updateSavedVersionCodes() { | |
// copy the previous value of the version code for later reference before we overwrite it | |
saveVersionCodeToPref(PREF_TAG_PREVIOUS_APP_VERSION, readVersionCodeFromPref(PREF_TAG_CURRENT_APP_VERSION)); | |
// save the current version code so it can be used by the line of code above, the next time we upgrade. | |
saveVersionCodeToPref(PREF_TAG_CURRENT_APP_VERSION, getVersionCode()); | |
} | |
private void saveVersionCodeToPref(final String prefTag, | |
final int versionCode) { | |
mSharedPrefHelper.setLongNoStrict(this, prefTag, versionCode); | |
} | |
private int readVersionCodeFromPref(final String prefTag) { | |
return (int) mSharedPrefHelper.getLongNoStrict(this, prefTag, -1); | |
} | |
private int getVersionCode() { | |
int result = 0; | |
try { | |
result = getApplicationContext().getPackageManager().getPackageInfo( | |
getApplicationContext().getPackageName(), 0).versionCode; | |
} catch (NameNotFoundException e) { | |
// squelch | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment