Skip to content

Instantly share code, notes, and snippets.

@rharter
Created September 5, 2014 14:51
Show Gist options
  • Save rharter/faa12b749afe23301b49 to your computer and use it in GitHub Desktop.
Save rharter/faa12b749afe23301b49 to your computer and use it in GitHub Desktop.
A simple utility class to manage rating (or really anything) call to action display.
package com.ryanharter.android.util;
import android.content.Context;
import android.content.SharedPreferences;
/**
* Keeps track of the number of app launches, and days since first launch, and
* provides an easy way to determine whether you should show a rating prompt
* or not.
*/
public class AppRater {
private static final String PREF_NAME = "app_rater";
private static final String LAUNCHES_REQUIRED = "launches_required";
private static final String DAYS_REQUIRED = "days_required";
private static final String KEY_FIRST_LAUNCH_TIME = "first_launch_time";
private static final String KEY_LAUNCH_COUNT = "launch_count";
private static final String KEY_DONT_SHOW_AGAIN = "dont_show_again";
/**
* Sets the number of launches and days required before we prompt.
* @param launches The number of app launches required.
* @param days The number of days since first launch required.
*/
public static void setPromptRequirements(Context context, int launches, int days) {
SharedPreferences prefs = getPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(LAUNCHES_REQUIRED, launches);
editor.putInt(DAYS_REQUIRED, days);
editor.apply();
}
/**
* Increments the app launch counter.
*/
public static void trackAppLaunch(Context context) {
SharedPreferences prefs = getPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
int currentCount = prefs.getInt(KEY_LAUNCH_COUNT, 0);
editor.putInt(KEY_LAUNCH_COUNT, currentCount + 1);
if (currentCount <= 0) {
editor.putLong(KEY_FIRST_LAUNCH_TIME, System.currentTimeMillis());
}
editor.apply();
}
/**
* Returns true if the rating prompt requirements have been met.
*
* @return True if you should show a prompt.
*/
public static boolean shouldPrompt(Context context) {
SharedPreferences prefs = getPreferences(context);
if (prefs.getBoolean(KEY_DONT_SHOW_AGAIN, false)) {
return false;
}
int launchCount = prefs.getInt(KEY_LAUNCH_COUNT, 0);
if (launchCount < prefs.getInt(LAUNCHES_REQUIRED, 3)) {
return false;
}
long firstLaunch = prefs.getLong(KEY_FIRST_LAUNCH_TIME, System.currentTimeMillis());
int daysRequired = prefs.getInt(DAYS_REQUIRED, 0);
if (System.currentTimeMillis() < firstLaunch + (daysRequired * 24 * 60 * 60 * 1000)) {
return false;
}
return true;
}
/**
* Sets the rater to always return false and never show a prompt again.
*/
public static void dontShowAgain(Context context) {
SharedPreferences prefs = getPreferences(context);
prefs.edit().putBoolean(KEY_DONT_SHOW_AGAIN, true).apply();
}
private static SharedPreferences getPreferences(Context context) {
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment