From a1be081c4f4d128f57a44d60b3641e84168cd48e Mon Sep 17 00:00:00 2001 From: eylen Date: Thu, 31 Mar 2016 12:09:11 +0200 Subject: [PATCH 1/2] Add the possibility to ask for new reviews if the application is updated --- .../com/kobakei/ratethisapp/RateThisApp.java | 49 +++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/ratethisapp/src/main/java/com/kobakei/ratethisapp/RateThisApp.java b/ratethisapp/src/main/java/com/kobakei/ratethisapp/RateThisApp.java index d0f533d..c0b631e 100644 --- a/ratethisapp/src/main/java/com/kobakei/ratethisapp/RateThisApp.java +++ b/ratethisapp/src/main/java/com/kobakei/ratethisapp/RateThisApp.java @@ -42,10 +42,16 @@ public class RateThisApp { private static final String KEY_INSTALL_DATE = "rta_install_date"; private static final String KEY_LAUNCH_TIMES = "rta_launch_times"; private static final String KEY_OPT_OUT = "rta_opt_out"; + private static final String KEY_ALREADY_REVIEWED = "rta_user_reviewed"; + private static final String KEY_APP_COUNTER_VERSION = "rta_counter_version"; + private static final String KEY_APP_VERSION = "rta_reviewed_version"; private static Date mInstallDate = new Date(); private static int mLaunchTimes = 0; private static boolean mOptOut = false; + private static boolean mAlreadyReviewed = false; + private static int mCounterVersion = -1; + private static int mReviewedVersion = -1; private static Config sConfig = new Config(); private static Callback sCallback = null; @@ -97,6 +103,15 @@ public static void onStart(Context context) { mInstallDate = new Date(pref.getLong(KEY_INSTALL_DATE, 0)); mLaunchTimes = pref.getInt(KEY_LAUNCH_TIMES, 0); mOptOut = pref.getBoolean(KEY_OPT_OUT, false); + mAlreadyReviewed = pref.getBoolean(KEY_ALREADY_REVIEWED, false); + mCounterVersion = pref.getInt(KEY_APP_COUNTER_VERSION, -1); + mReviewedVersion = pref.getInt(KEY_APP_VERSION, -1); + + if (!mOptOut && sConfig.mPromptForNewVersion && mReviewedVersion < BuildConfig.VERSION_CODE && mCounterVersion < BuildConfig.VERSION_CODE){ + clearSharedPreferences(context); + } else if (mCounterVersion != BuildConfig.VERSION_CODE){ //let's just update the counter version just in case we need it later + pref.edit().putInt(KEY_APP_COUNTER_VERSION, BuildConfig.VERSION_CODE).commit(); + } printStatus(context); } @@ -137,7 +152,7 @@ public static boolean showRateDialogIfNeeded(final Context context, int themeId) * @return */ public static boolean shouldShowRateDialog() { - if (mOptOut) { + if (mOptOut || mAlreadyReviewed) { return false; } else { if (mLaunchTimes >= sConfig.mCriteriaLaunchTimes) { @@ -195,7 +210,7 @@ public void onClick(DialogInterface dialog, int which) { String appPackage = context.getPackageName(); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackage)); context.startActivity(intent); - setOptOut(context, true); + setAlreadyReviewed(context, true); } }); builder.setNeutralButton(cancelButtonID, new OnClickListener() { @@ -227,7 +242,8 @@ public void onCancel(DialogInterface dialog) { /** * Clear data in shared preferences.
- * This API is called when the rate dialog is approved or canceled. + * This API is called when the rate dialog is approved, canceled. or with new app version if the + * prompt for newer version flag is set in the configuration * @param context */ private static void clearSharedPreferences(Context context) { @@ -235,6 +251,7 @@ private static void clearSharedPreferences(Context context) { Editor editor = pref.edit(); editor.remove(KEY_INSTALL_DATE); editor.remove(KEY_LAUNCH_TIMES); + editor.putInt(KEY_APP_COUNTER_VERSION, BuildConfig.VERSION_CODE); editor.commit(); } @@ -251,6 +268,20 @@ private static void setOptOut(final Context context, boolean optOut) { mOptOut = optOut; } + /** + * Set already reviewed flag. If it is true, the rate dialog will not be shown unless the config + * is set to ask again for newer versions + * @param context + * @param alreadyReviewed + */ + private static void setAlreadyReviewed(final Context context, boolean alreadyReviewed){ + SharedPreferences pref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); + Editor editor = pref.edit(); + editor.putBoolean(KEY_ALREADY_REVIEWED, alreadyReviewed); + editor.commit(); + mAlreadyReviewed = alreadyReviewed; + } + /** * Print values in SharedPreferences (used for debug) * @param context @@ -258,9 +289,12 @@ private static void setOptOut(final Context context, boolean optOut) { private static void printStatus(final Context context) { SharedPreferences pref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); log("*** RateThisApp Status ***"); + log("Counter App version" + pref.getInt(KEY_APP_COUNTER_VERSION, -1)); log("Install Date: " + new Date(pref.getLong(KEY_INSTALL_DATE, 0))); log("Launch Times: " + pref.getInt(KEY_LAUNCH_TIMES, 0)); log("Opt out: " + pref.getBoolean(KEY_OPT_OUT, false)); + log("Already reviewed: " + pref.getBoolean(KEY_ALREADY_REVIEWED, false)); + log("Reviewed version: " + pref.getInt(KEY_APP_VERSION, -1)); } /** @@ -285,6 +319,7 @@ public static class Config { private int mThanksButton = 0; private int mCancelButton = 0; + private boolean mPromptForNewVersion = false; /** * Constructor with default criteria. */ @@ -341,6 +376,14 @@ public void setThanksButton(int stringId) { public void setCancelButton(int stringId) { this.mCancelButton = stringId; } + + /** + * Set promptForNewVersion if user rated + * @param promptForNewVersion + */ + public void setPromptForNewVersion(boolean promptForNewVersion){ + this.mPromptForNewVersion = promptForNewVersion; + } } public interface Callback { From 986b5b4001616ead1d933bbfd1fc4a90190ed848 Mon Sep 17 00:00:00 2001 From: eylen Date: Thu, 31 Mar 2016 13:10:33 +0200 Subject: [PATCH 2/2] Receive the main application code by parameter Correctly clear data --- .../com/kobakei/ratethisapp/RateThisApp.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/ratethisapp/src/main/java/com/kobakei/ratethisapp/RateThisApp.java b/ratethisapp/src/main/java/com/kobakei/ratethisapp/RateThisApp.java index c0b631e..d7b6a88 100644 --- a/ratethisapp/src/main/java/com/kobakei/ratethisapp/RateThisApp.java +++ b/ratethisapp/src/main/java/com/kobakei/ratethisapp/RateThisApp.java @@ -107,10 +107,10 @@ public static void onStart(Context context) { mCounterVersion = pref.getInt(KEY_APP_COUNTER_VERSION, -1); mReviewedVersion = pref.getInt(KEY_APP_VERSION, -1); - if (!mOptOut && sConfig.mPromptForNewVersion && mReviewedVersion < BuildConfig.VERSION_CODE && mCounterVersion < BuildConfig.VERSION_CODE){ + if (!mOptOut && sConfig.mPromptForNewVersion && mReviewedVersion < sConfig.mCurrentAppVersion && mCounterVersion < sConfig.mCurrentAppVersion){ clearSharedPreferences(context); - } else if (mCounterVersion != BuildConfig.VERSION_CODE){ //let's just update the counter version just in case we need it later - pref.edit().putInt(KEY_APP_COUNTER_VERSION, BuildConfig.VERSION_CODE).commit(); + } else if (mCounterVersion != sConfig.mCurrentAppVersion){ //let's just update the counter version just in case we need it later + pref.edit().putInt(KEY_APP_COUNTER_VERSION, sConfig.mCurrentAppVersion).commit(); } printStatus(context); @@ -250,8 +250,13 @@ private static void clearSharedPreferences(Context context) { SharedPreferences pref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); Editor editor = pref.edit(); editor.remove(KEY_INSTALL_DATE); + mInstallDate = new Date(); editor.remove(KEY_LAUNCH_TIMES); - editor.putInt(KEY_APP_COUNTER_VERSION, BuildConfig.VERSION_CODE); + mLaunchTimes = 0; + editor.putInt(KEY_APP_COUNTER_VERSION, sConfig.mCurrentAppVersion); + mCounterVersion = sConfig.mCurrentAppVersion; + editor.putBoolean(KEY_ALREADY_REVIEWED, false); + mAlreadyReviewed = false; editor.commit(); } @@ -278,6 +283,7 @@ private static void setAlreadyReviewed(final Context context, boolean alreadyRev SharedPreferences pref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); Editor editor = pref.edit(); editor.putBoolean(KEY_ALREADY_REVIEWED, alreadyReviewed); + editor.putInt(KEY_APP_VERSION, sConfig.mCurrentAppVersion); editor.commit(); mAlreadyReviewed = alreadyReviewed; } @@ -320,6 +326,7 @@ public static class Config { private int mCancelButton = 0; private boolean mPromptForNewVersion = false; + private int mCurrentAppVersion = -1; /** * Constructor with default criteria. */ @@ -380,9 +387,11 @@ public void setCancelButton(int stringId) { /** * Set promptForNewVersion if user rated * @param promptForNewVersion + * @param currentAppVersion */ - public void setPromptForNewVersion(boolean promptForNewVersion){ + public void setPromptForNewVersion(boolean promptForNewVersion, int currentAppVersion){ this.mPromptForNewVersion = promptForNewVersion; + this.mCurrentAppVersion = currentAppVersion; } }