Created
December 11, 2014 01:45
Revisions
-
williscool created this gist
Dec 11, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,75 @@ /** * 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 williscool * inspired by * @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 = "1"; /** * Caches the result of {@link #checkAppStart(Context context, SharedPreferences sharedPreferences)}. To allow idempotent method * calls. */ private static AppStart appStart = null; /** * Finds out started for the first time (ever or in the current version). * * @return the type of app start */ public AppStart checkAppStart(Context context, SharedPreferences sharedPreferences) { PackageInfo pInfo; try { pInfo = context.getPackageManager().getPackageInfo( context.getPackageName(), PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); int lastVersionCode = sharedPreferences.getInt( LAST_APP_VERSION, -1); // String versionName = pInfo.versionName; int currentVersionCode = pInfo.versionCode; appStart = checkAppStart(currentVersionCode, lastVersionCode); // Update version in preferences sharedPreferences.edit() .putInt(LAST_APP_VERSION, currentVersionCode).commit(); // must use commit here or app may not update prefs in time and app will loop into walkthrough } catch (PackageManager.NameNotFoundException e) { Log.w(TAG, "Unable to determine current app version from package manager. Defensively 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(TAG, "Current version code (" + currentVersionCode + ") is less then the one recognized on last startup (" + lastVersionCode + "). Defensively 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ public class MainActivity extends Activity { private SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // you dont actually need the context variable // inside of an activity class you can pass it as this and that will work // it just reads a bit better context = this; sharedPreferences = PreferenceManager .getDefaultSharedPreferences(context); switch (checkAppStart(this,sharedPreferences)) { 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; } // ... } // ... }