Skip to content

Instantly share code, notes, and snippets.

@Pamblam
Created September 12, 2016 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pamblam/c3011df411f81bd057eb8a65bace6085 to your computer and use it in GitHub Desktop.
Save Pamblam/c3011df411f81bd057eb8a65bace6085 to your computer and use it in GitHub Desktop.
A class to ask user to rate appp if they have not done so.
package com.package.name;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Log;
public class rateBeggar {
private String displayMessage = "Thank you for using our app. We rely on your feedback to continue making improvements.\n\nRate us on the Play Store?";
private int usageThreshold = 5;
private int delayHours = 48;
private int usageCount;
private Context context;
private SharedPreferences prefs;
private boolean neverAgain;
private long lastAskedTime;
public rateBeggar(Context context){
this.context = context;
prefs = context.getSharedPreferences("rateBeggar", 0);
usageCount = prefs.getInt("usageCount", 1);
lastAskedTime = prefs.getLong("lastAskedTime", 0);
neverAgain = prefs.getBoolean("neverAgain", false);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("usageCount", usageCount+1);
editor.commit();
}
/**
* Should user be prompted to rate
* @return
*/
public boolean shouldPromptForRating(){
long currentTime = System.currentTimeMillis() / 1000L;
long askAgainTime = currentTime - (delayHours * 60 * 60);
boolean tooSoon = lastAskedTime != 0 && lastAskedTime < askAgainTime;
return usageCount > usageThreshold && !neverAgain && !tooSoon;
}
/**
* Set the message that is displayed to user
* @param displayMessage
*/
public rateBeggar setDisplayMessage(String displayMessage){
this.displayMessage = displayMessage;
return this;
}
/**
* How many uses before prompting
* @param usageThreshold
*/
public rateBeggar setUsageThreshold(int usageThreshold){
this.usageThreshold = usageThreshold;
return this;
}
/**
* How many hours to wait before prompting again when user chooses "Later"
* @param delayHours
*/
public rateBeggar setDelayHours(int delayHours){
this.delayHours = delayHours;
return this;
}
/**
* Beg user for rating, if appropriate
*/
public void beg(){
Log.e("rateBeggar", "begging...");
if(shouldPromptForRating()) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setCancelable(false);
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.setTitle("Please rate our app!");
alertDialog.setMessage(this.displayMessage);
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Okay",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
goToStore();
setNeverAgain();
dialog.dismiss();
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Already Did",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
setNeverAgain();
dialog.dismiss();
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Later",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
setLastAskedTime();
}
}
private void setLastAskedTime() {
long lastAskedTime = System.currentTimeMillis() / 1000L;
this.lastAskedTime = lastAskedTime;
SharedPreferences.Editor editor = prefs.edit();
editor.putLong("lastAskedTime", lastAskedTime);
editor.commit();
}
private void setNeverAgain(){
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("neverAgain", true);
editor.commit();
}
private void goToStore(){
Uri uri = Uri.parse("market://details?id=" + context.getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
try {
context.startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + context.getPackageName())));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment