Last active
October 30, 2016 22:02
-
-
Save paulocoutinhox/c824c85e3f93670c3ac20fd3654945b4 to your computer and use it in GitHub Desktop.
Android - Detect first time app opened
This file contains hidden or 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
/** | |
* Distinguishes different kinds of app starts: <li> | |
* <ul> | |
* First start ever ({@link #FIRST_TIME}) | |
* </ul> | |
* <ul> | |
* First start in this version ({@link #FIRST_TIME_VERSION}) | |
* </ul> | |
* <ul> | |
* Normal app start ({@link #NORMAL}) | |
* </ul> | |
* | |
* @author schnatterer | |
* | |
*/ | |
public enum AppStart { | |
FIRST_TIME, FIRST_TIME_VERSION, NORMAL; | |
} | |
/** | |
* The app version code (not the version name!) that was used on the last | |
* start of the app. | |
*/ | |
private static final String LAST_APP_VERSION = "last_app_version"; | |
/** | |
* Finds out started for the first time (ever or in the current version).<br/> | |
* <br/> | |
* Note: This method is <b>not idempotent</b> only the first call will | |
* determine the proper result. Any subsequent calls will only return | |
* {@link AppStart#NORMAL} until the app is started again. So you might want | |
* to consider caching the result! | |
* | |
* @return the type of app start | |
*/ | |
public AppStart checkAppStart() { | |
PackageInfo pInfo; | |
SharedPreferences sharedPreferences = PreferenceManager | |
.getDefaultSharedPreferences(this); | |
AppStart appStart = AppStart.NORMAL; | |
try { | |
pInfo = getPackageManager().getPackageInfo(getPackageName(), 0); | |
int lastVersionCode = sharedPreferences | |
.getInt(LAST_APP_VERSION, -1); | |
int currentVersionCode = pInfo.versionCode; | |
appStart = checkAppStart(currentVersionCode, lastVersionCode); | |
// Update version in preferences | |
sharedPreferences.edit() | |
.putInt(LAST_APP_VERSION, currentVersionCode).commit(); | |
} catch (NameNotFoundException e) { | |
Log.w(Constants.LOG, | |
"Unable to determine current app version from pacakge manager. Defenisvely assuming normal app start."); | |
} | |
return appStart; | |
} | |
public AppStart checkAppStart(int currentVersionCode, int lastVersionCode) { | |
if (lastVersionCode == -1) { | |
return AppStart.FIRST_TIME; | |
} else if (lastVersionCode < currentVersionCode) { | |
return AppStart.FIRST_TIME_VERSION; | |
} else if (lastVersionCode > currentVersionCode) { | |
Log.w(Constants.LOG, "Current version code (" + currentVersionCode | |
+ ") is less then the one recognized on last startup (" | |
+ lastVersionCode | |
+ "). Defenisvely assuming normal app start."); | |
return AppStart.NORMAL; | |
} else { | |
return AppStart.NORMAL; | |
} | |
} |
This file contains hidden or 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
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
switch (checkAppStart()) { | |
case NORMAL: | |
// We don't want to get on the user's nerves | |
break; | |
case FIRST_TIME_VERSION: | |
// TODO show what's new | |
break; | |
case FIRST_TIME: | |
// TODO show a tutorial | |
break; | |
default: | |
break; | |
} | |
// ... | |
} | |
// ... | |
} |
This file contains hidden or 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
public void testCheckAppStart() { | |
// First start | |
int oldVersion = -1; | |
int newVersion = 1; | |
assertEquals("Unexpected result", AppStart.FIRST_TIME, | |
service.checkAppStart(newVersion, oldVersion)); | |
// First start this version | |
oldVersion = 1; | |
newVersion = 2; | |
assertEquals("Unexpected result", AppStart.FIRST_TIME_VERSION, | |
service.checkAppStart(newVersion, oldVersion)); | |
// Normal start | |
oldVersion = 2; | |
newVersion = 2; | |
assertEquals("Unexpected result", AppStart.NORMAL, | |
service.checkAppStart(newVersion, oldVersion)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment