From d69d40631864db793c48151b1007718dd9669bac Mon Sep 17 00:00:00 2001 From: Manoj Date: Mon, 2 May 2016 17:30:44 +0530 Subject: [PATCH 1/3] added apis to specify mimimum custom event values for showing rate dailog, along with methods to increment and set these custom event values/counters --- .../java/hotchemi/android/rate/AppRate.java | 32 ++++++++++++++++++- .../android/rate/PreferenceHelper.java | 18 +++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/hotchemi/android/rate/AppRate.java b/library/src/main/java/hotchemi/android/rate/AppRate.java index b9a4907..c200d49 100644 --- a/library/src/main/java/hotchemi/android/rate/AppRate.java +++ b/library/src/main/java/hotchemi/android/rate/AppRate.java @@ -5,6 +5,8 @@ import android.view.View; import java.util.Date; +import java.util.HashMap; +import java.util.Map; import static hotchemi.android.rate.DialogManager.create; import static hotchemi.android.rate.PreferenceHelper.getInstallDate; @@ -13,6 +15,7 @@ import static hotchemi.android.rate.PreferenceHelper.getRemindInterval; import static hotchemi.android.rate.PreferenceHelper.isFirstLaunch; import static hotchemi.android.rate.PreferenceHelper.setInstallDate; +import static hotchemi.android.rate.PreferenceHelper.getCustomEventCount; public final class AppRate { @@ -28,6 +31,8 @@ public final class AppRate { private int remindInterval = 1; + private final HashMap customEventCounts = new HashMap<>(); + private boolean isDebug = false; private AppRate(Context context) { @@ -72,6 +77,11 @@ public AppRate setRemindInterval(int remindInterval) { return this; } + public AppRate setMinimumEventCount(String eventName, long count) { + this.customEventCounts.put(eventName, count); + return this; + } + public AppRate setShowLaterButton(boolean isShowNeutralButton) { options.setShowNeutralButton(isShowNeutralButton); return this; @@ -173,6 +183,15 @@ public AppRate setStoreType(StoreType appstore) { return this; } + public AppRate incrementEventCount(String eventName) { + return setEventCount(eventName, getCustomEventCount(context,eventName) + 1); + } + + public AppRate setEventCount(String eventName, long count) { + PreferenceHelper.setCustomEventCount(context, eventName, count); + return this; + } + public void monitor() { if (isFirstLaunch(context)) { setInstallDate(context); @@ -190,7 +209,8 @@ public boolean shouldShowRateDialog() { return getIsAgreeShowDialog(context) && isOverLaunchTimes() && isOverInstallDate() && - isOverRemindDate(); + isOverRemindDate() && + isOverCustomEventRequirements(); } private boolean isOverLaunchTimes() { @@ -205,6 +225,16 @@ private boolean isOverRemindDate() { return isOverDate(getRemindInterval(context), remindInterval); } + private boolean isOverCustomEventRequirements() { + for(Map.Entry eventRequirement: customEventCounts.entrySet()) { + Long currentCount = getCustomEventCount(context, eventRequirement.getKey()); + if(currentCount < eventRequirement.getValue()) { + return false; + } + } + return true; + } + public boolean isDebug() { return isDebug; } diff --git a/library/src/main/java/hotchemi/android/rate/PreferenceHelper.java b/library/src/main/java/hotchemi/android/rate/PreferenceHelper.java index 6856a87..8e17a0f 100644 --- a/library/src/main/java/hotchemi/android/rate/PreferenceHelper.java +++ b/library/src/main/java/hotchemi/android/rate/PreferenceHelper.java @@ -18,6 +18,8 @@ final class PreferenceHelper { private static final String PREF_KEY_REMIND_INTERVAL = "android_rate_remind_interval"; + private static final String PREF_CUSTOM_EVENT_KEY_PREFIX = "android_rate_custom_event_prefix_"; + private PreferenceHelper() { } @@ -93,4 +95,20 @@ static boolean isFirstLaunch(Context context) { return getPreferences(context).getLong(PREF_KEY_INSTALL_DATE, 0) == 0L; } + /** + * Add a prefix for the key for each custom event, + * so that there is no clash with existing keys (PREF_KEY_LAUNCH_TIME, PREF_KEY_INSTALL_DATE, etc.) + */ + static long getCustomEventCount(Context context, String eventName) { + String eventKey = PREF_CUSTOM_EVENT_KEY_PREFIX + eventName; + return getPreferences(context).getLong(eventKey, 0); + } + + static void setCustomEventCount(Context context, String eventName, long eventCount) { + String eventKey = PREF_CUSTOM_EVENT_KEY_PREFIX + eventName; + SharedPreferences.Editor editor = getPreferencesEditor(context); + editor.putLong(eventKey, eventCount); + editor.apply(); + } + } \ No newline at end of file From a5c6194e6ced0b89c7ef844accf7ba93433fd69d Mon Sep 17 00:00:00 2001 From: Manoj Date: Mon, 2 May 2016 22:01:28 +0530 Subject: [PATCH 2/3] refactor --- .../src/main/java/hotchemi/android/rate/AppRate.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/library/src/main/java/hotchemi/android/rate/AppRate.java b/library/src/main/java/hotchemi/android/rate/AppRate.java index c200d49..5cb39f6 100644 --- a/library/src/main/java/hotchemi/android/rate/AppRate.java +++ b/library/src/main/java/hotchemi/android/rate/AppRate.java @@ -16,6 +16,7 @@ import static hotchemi.android.rate.PreferenceHelper.isFirstLaunch; import static hotchemi.android.rate.PreferenceHelper.setInstallDate; import static hotchemi.android.rate.PreferenceHelper.getCustomEventCount; +import static hotchemi.android.rate.PreferenceHelper.setCustomEventCount; public final class AppRate { @@ -77,8 +78,8 @@ public AppRate setRemindInterval(int remindInterval) { return this; } - public AppRate setMinimumEventCount(String eventName, long count) { - this.customEventCounts.put(eventName, count); + public AppRate setMinimumEventCount(String eventName, long minimumCount) { + this.customEventCounts.put(eventName, minimumCount); return this; } @@ -184,11 +185,11 @@ public AppRate setStoreType(StoreType appstore) { } public AppRate incrementEventCount(String eventName) { - return setEventCount(eventName, getCustomEventCount(context,eventName) + 1); + return setEventCountValue(eventName, getCustomEventCount(context,eventName) + 1); } - public AppRate setEventCount(String eventName, long count) { - PreferenceHelper.setCustomEventCount(context, eventName, count); + public AppRate setEventCountValue(String eventName, long countValue) { + setCustomEventCount(context, eventName, countValue); return this; } From 38e48a43fc7ffd1a4c522689facbe03dc28cdb6d Mon Sep 17 00:00:00 2001 From: Manoj Date: Mon, 2 May 2016 22:04:52 +0530 Subject: [PATCH 3/3] refactor --- library/src/main/java/hotchemi/android/rate/AppRate.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/hotchemi/android/rate/AppRate.java b/library/src/main/java/hotchemi/android/rate/AppRate.java index 5cb39f6..2c6c6a5 100644 --- a/library/src/main/java/hotchemi/android/rate/AppRate.java +++ b/library/src/main/java/hotchemi/android/rate/AppRate.java @@ -227,7 +227,7 @@ private boolean isOverRemindDate() { } private boolean isOverCustomEventRequirements() { - for(Map.Entry eventRequirement: customEventCounts.entrySet()) { + for(Map.Entry eventRequirement : customEventCounts.entrySet()) { Long currentCount = getCustomEventCount(context, eventRequirement.getKey()); if(currentCount < eventRequirement.getValue()) { return false;