Skip to content
This repository was archived by the owner on May 22, 2021. It is now read-only.

New feature significative event counter #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 83 additions & 18 deletions AppRater/src/main/java/org/codechimp/apprater/AppRater.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,30 @@
import android.content.SharedPreferences;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;

import java.lang.Long;
import java.lang.System;

public class AppRater {
// Preference Constants
private final static String PREF_NAME = "apprater";
private final static String PREF_LAUNCH_COUNT = "launch_count";
private final static String PREF_SIGNIFICANT_EVENT_COUNT = "significant_event_count";
private final static String PREF_FIRST_LAUNCHED = "date_firstlaunch";
private final static String PREF_DONT_SHOW_AGAIN = "dontshowagain";
private final static String PREF_REMIND_LATER = "remindmelater";
private final static String PREF_DAYS_UNTIL = "pref_days_until";
private final static String PREF_LAUNCHES_UNTIL = "pref_launches_until";
private final static String PREF_EVENTS_UNTIL = "pref_events_until";
private final static String PREF_APP_VERSION_NAME = "app_version_name";
private final static String PREF_APP_VERSION_CODE = "app_version_code";

private final static int DAYS_UNTIL_PROMPT = 3;
private final static int LAUNCHES_UNTIL_PROMPT = 7;
public final static int DAYS_UNTIL_PROMPT = 3;
public final static int LAUNCHES_UNTIL_PROMPT = 7;
public final static int SIGNIFICANT_EVENT_UNTIL_PROMPT = -1;
private static int DAYS_UNTIL_PROMPT_FOR_REMIND_LATER = 3;
private static int LAUNCHES_UNTIL_PROMPT_FOR_REMIND_LATER = 7;
private static int SIGNIFICANT_EVENT_UNTIL_PROMPT_FOR_REMIND_LATER = -1;
private static boolean isDark;
private static boolean themeSet;
private static boolean hideNoButton;
Expand Down Expand Up @@ -75,6 +83,16 @@ public static void setNumLaunchesForRemindLater(int launchesUntilPrompt) {
LAUNCHES_UNTIL_PROMPT_FOR_REMIND_LATER = launchesUntilPrompt;
}

/**
* sets number of significant events until rating dialog pops up for next time
* when remind me later option is chosen
*
* @param significantEventUntilPrompt
*/
public static void setSignificantEventForRemindLater(int significantEventUntilPrompt) {
SIGNIFICANT_EVENT_UNTIL_PROMPT_FOR_REMIND_LATER = significantEventUntilPrompt;
}

/**
* decides if No thanks button appear in dialog or not
*
Expand All @@ -101,7 +119,7 @@ public static void setCancelable(boolean cancelable) {
* @param context
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacing methods with the additional parameters will break existing implementations that use this particular calling method. It would be better to implement your own additional overloaded methods though I'm worried about telescoping this too far and getting a very messy implementation (more than it is today)
Commenting on just this one but there are further breaking overloads.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see it
At this point, the better choice is change all to pattern builder.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, it would break existing compatibility but with yours and other feature requests it makes sense.

Unfortunately I don't have time to do this immediately and so will not only be maintaining current features for now but will start a new branch for a V2 in future.

*/
public static void app_launched(Context context) {
app_launched(context, DAYS_UNTIL_PROMPT, LAUNCHES_UNTIL_PROMPT);
app_launched(context, DAYS_UNTIL_PROMPT, LAUNCHES_UNTIL_PROMPT, SIGNIFICANT_EVENT_UNTIL_PROMPT);
}

/**
Expand All @@ -113,13 +131,16 @@ public static void app_launched(Context context) {
* @param context
* @param daysUntilPrompt
* @param launchesUntilPrompt
* @param significantEventUntilPrompt
* @param daysForRemind
* @param launchesForRemind
* @param significantEventForRemind
*/
public static void app_launched(Context context, int daysUntilPrompt, int launchesUntilPrompt, int daysForRemind, int launchesForRemind) {
public static void app_launched(Context context, int daysUntilPrompt, int launchesUntilPrompt, int significantEventUntilPrompt, int daysForRemind, int launchesForRemind, int significantEventForRemind) {
setNumDaysForRemindLater(daysForRemind);
setNumLaunchesForRemindLater(launchesForRemind);
app_launched(context, daysUntilPrompt, launchesUntilPrompt);
setSignificantEventForRemindLater(significantEventForRemind);
app_launched(context, daysUntilPrompt, launchesUntilPrompt, significantEventUntilPrompt);
}

/**
Expand All @@ -129,13 +150,12 @@ public static void app_launched(Context context, int daysUntilPrompt, int launch
* @param context
* @param daysUntilPrompt
* @param launchesUntilPrompt
* @param significantEventUntilPrompt
*/
public static void app_launched(Context context, int daysUntilPrompt, int launchesUntilPrompt) {
public static void app_launched(Context context, int daysUntilPrompt, int launchesUntilPrompt, int significantEventUntilPrompt) {
SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
ApplicationRatingInfo ratingInfo = ApplicationRatingInfo.createApplicationInfo(context);
int days;
int launches;
if (isVersionNameCheckEnabled) {
if (!ratingInfo.getApplicationVersionName().equals(prefs.getString(PREF_APP_VERSION_NAME, "none"))) {
editor.putString(PREF_APP_VERSION_NAME, ratingInfo.getApplicationVersionName());
Expand All @@ -150,31 +170,64 @@ public static void app_launched(Context context, int daysUntilPrompt, int launch
commitOrApply(editor);
}
}

// Increment launch counter
long launch_count = prefs.getLong(PREF_LAUNCH_COUNT, 0) + 1;
editor.putLong(PREF_LAUNCH_COUNT, launch_count);
// Get date of first launch
Long date_firstLaunch = prefs.getLong(PREF_FIRST_LAUNCHED, 0);
if (date_firstLaunch == 0) {
date_firstLaunch = System.currentTimeMillis();
editor.putLong(PREF_FIRST_LAUNCHED, date_firstLaunch);
}

int days;
int launches;
int significantEvents;

if (prefs.getBoolean(PREF_DONT_SHOW_AGAIN, false)) {
return;
} else if (prefs.getBoolean(PREF_REMIND_LATER, false)) {
days = DAYS_UNTIL_PROMPT_FOR_REMIND_LATER;
launches = LAUNCHES_UNTIL_PROMPT_FOR_REMIND_LATER;
significantEvents = SIGNIFICANT_EVENT_UNTIL_PROMPT_FOR_REMIND_LATER;
} else {
days = daysUntilPrompt;
launches = launchesUntilPrompt;
significantEvents = significantEventUntilPrompt;
}

// Increment launch counter
long launch_count = prefs.getLong(PREF_LAUNCH_COUNT, 0) + 1;
editor.putLong(PREF_LAUNCH_COUNT, launch_count);
editor.putLong(PREF_DAYS_UNTIL, days);
editor.putLong(PREF_LAUNCHES_UNTIL, launches);
editor.putLong(PREF_EVENTS_UNTIL, significantEvents);

commitOrApply(editor);

showRateAlertIfIsNeeded(context);

}

private static void showRateAlertIfIsNeeded(Context context) {
SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();

long days = prefs.getLong(PREF_DAYS_UNTIL, 0);
long launches = prefs.getLong(PREF_LAUNCHES_UNTIL, 0);
long significantEvents = prefs.getLong(PREF_EVENTS_UNTIL, 0);

// Get launch counter
long launch_count = prefs.getLong(PREF_LAUNCH_COUNT, 0);
// Get date of first launch
Long date_firstLaunch = prefs.getLong(PREF_FIRST_LAUNCHED, 0);
if (date_firstLaunch == 0) {
date_firstLaunch = System.currentTimeMillis();
editor.putLong(PREF_FIRST_LAUNCHED, date_firstLaunch);
}
// Get Significant events counter
long significant_events_count = prefs.getLong(PREF_SIGNIFICANT_EVENT_COUNT, 0);
// Wait for at least the number of launches or the number of days used
// until prompt
if (launch_count >= launches || (System.currentTimeMillis() >= date_firstLaunch + (days * 24 * 60 * 60 * 1000))) {
if (launch_count >= launches
|| (System.currentTimeMillis() >= date_firstLaunch + (days * 24 * 60 * 60 * 1000))
|| (significantEvents > 0 && significant_events_count >= significantEvents)) {
showRateAlertDialog(context, editor);
}
commitOrApply(editor);
}

/**
Expand Down Expand Up @@ -273,6 +326,7 @@ public void onClick(DialogInterface dialog, int id) {
Long date_firstLaunch = System.currentTimeMillis();
editor.putLong(PREF_FIRST_LAUNCHED, date_firstLaunch);
editor.putLong(PREF_LAUNCH_COUNT, 0);
editor.putLong(PREF_SIGNIFICANT_EVENT_COUNT, 0);
editor.putBoolean(PREF_REMIND_LATER, true);
editor.putBoolean(PREF_DONT_SHOW_AGAIN, false);
commitOrApply(editor);
Expand All @@ -290,6 +344,7 @@ public void onClick(DialogInterface dialog, int id) {
long date_firstLaunch = System.currentTimeMillis();
editor.putLong(PREF_FIRST_LAUNCHED, date_firstLaunch);
editor.putLong(PREF_LAUNCH_COUNT, 0);
editor.putLong(PREF_SIGNIFICANT_EVENT_COUNT, 0);
commitOrApply(editor);
}
dialog.dismiss();
Expand All @@ -316,6 +371,16 @@ public static void resetData(Context context) {
editor.putLong(PREF_LAUNCH_COUNT, 0);
long date_firstLaunch = System.currentTimeMillis();
editor.putLong(PREF_FIRST_LAUNCHED, date_firstLaunch);
editor.putLong(PREF_SIGNIFICANT_EVENT_COUNT, 0);
commitOrApply(editor);
}

public static void userDidSignificantEvent(Context context) {
SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
long significant_events_count = prefs.getLong(PREF_SIGNIFICANT_EVENT_COUNT, 0) + 1;
editor.putLong(PREF_SIGNIFICANT_EVENT_COUNT, significant_events_count);
commitOrApply(editor);
showRateAlertIfIsNeeded(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.codechimp.apprater.AppRater;
import org.codechimp.apprater.GoogleMarket;

import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
Expand All @@ -15,35 +16,49 @@

public class MainActivity extends Activity {

private Button buttonTest;
private Button buttonTest;
private Button buttonSignificantEvent;
private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

buttonTest = (Button) findViewById(R.id.button1);

buttonTest.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

// This forces display of the rate prompt.
// It should only be used for testing purposes
AppRater.showRateDialog(v.getContext());
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;

buttonTest = (Button) findViewById(R.id.button1);

buttonTest.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

// This forces display of the rate prompt.
// It should only be used for testing purposes
AppRater.showRateDialog(v.getContext());
}
});


buttonSignificantEvent = (Button) findViewById(R.id.button2);

buttonSignificantEvent.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

// This adds increase de significant event
AppRater.userDidSignificantEvent(context);
}
});

// Optionally you can set the Market you want to use prior to calling app_launched
// If setMarket not called it will default to Google Play
// Current implementations are Google Play and Amazon App Store, you can add your own by implementing Market
// AppRater.setMarket(new GoogleMarket());
// AppRater.setMarket(new AmazonMarket());


// This will keep a track of when the app was first used and whether to show a prompt
// It should be the default implementation of AppRater
AppRater.app_launched(this);
}
// It should be the default implementation of AppRater
AppRater.app_launched(this, AppRater.DAYS_UNTIL_PROMPT, AppRater.LAUNCHES_UNTIL_PROMPT, 3);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
Expand Down
23 changes: 21 additions & 2 deletions AppRaterDemo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".MainActivity" >

<TextView
Expand All @@ -20,4 +22,21 @@
android:layout_below="@id/textview1"
android:text="@string/force_rate_prompt" />

</RelativeLayout>

<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:padding="16dp"
android:text="@string/details_events" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/textview1"
android:text="@string/significant_event" />

</LinearLayout>
2 changes: 2 additions & 0 deletions AppRaterDemo/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
<string name="details">By default a prompt will appear after 3 days and the app must have been activated at least 7 times.</string>
<string name="force_rate_prompt">Force Rate Prompt</string>
<string name="rate_now">Rate Now</string>
<string name="details_events">In this example the prompt will appears at the third significant event.</string>
<string name="significant_event">Do a significant event</string>
</resources>