package com.nf2m.rater;
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.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.nf2m.R;
public class AppRater {
private final static String APP_PNAME = "com.nf2m";
private final static int DAYS_UNTIL_PROMPT = 3;
private final static int LAUNCHES_UNTIL_PROMPT = 7;
private static final String KEY_DONT_SHOW_AGAIN = "dontshowagain";
private static final String KEY_LAUNCH_COUNT = "launch_count";
private static final String KEY_DATE_FIRST_LAUNCH = "date_firstlaunch";
private static final String KEY_FIRST_SHOW = "key_first_show";
public static void app_launched(@NonNull Context mContext) {
SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0);
if (prefs.getBoolean(KEY_DONT_SHOW_AGAIN, false)) {
return;
}
SharedPreferences.Editor editor = prefs.edit();
// Increment launch counter
long launch_count = prefs.getLong(KEY_LAUNCH_COUNT, 0) + 1;
editor.putLong(KEY_LAUNCH_COUNT, launch_count);
// Get date of first launch
Long date_firstLaunch = prefs.getLong(KEY_DATE_FIRST_LAUNCH, 0);
if (date_firstLaunch == 0) {
date_firstLaunch = System.currentTimeMillis();
editor.putLong(KEY_DATE_FIRST_LAUNCH, date_firstLaunch);
}
// Wait at least n days before opening
if (launch_count >= LAUNCHES_UNTIL_PROMPT) {
boolean isFirstShow = prefs.getBoolean(KEY_FIRST_SHOW, true);
if (isFirstShow) {
editor.putBoolean(KEY_FIRST_SHOW, false);
showRateDialog(mContext, editor);
} else if (System.currentTimeMillis() >= date_firstLaunch +
(DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)) {
showRateDialog(mContext, editor);
}
}
editor.apply();
}
private static void showRateDialog(@NonNull final Context mContext, @Nullable final SharedPreferences.Editor editor) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setTitle(mContext.getString(R.string.rate_dialog_title));
alertDialog.setMessage(mContext.getString(R.string.rate_tar_message));
alertDialog.setPositiveButton(mContext.getString(R.string.rate_tar_rate_button), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
} catch (ActivityNotFoundException e) {
mContext.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + APP_PNAME + mContext.getPackageName())));
}
if (editor != null) {
editor.putBoolean(KEY_DONT_SHOW_AGAIN, true);
editor.commit();
}
}
});
alertDialog.setNegativeButton(mContext.getString(R.string.rate_tar_remind_button), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (editor != null) {
editor.putLong(KEY_DATE_FIRST_LAUNCH, 0);
editor.commit();
}
}
});
alertDialog.setNeutralButton(mContext.getString(R.string.rate_tar_no_button), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (editor != null) {
editor.putBoolean(KEY_DONT_SHOW_AGAIN, true);
editor.commit();
}
}
});
alertDialog.create().show();
}
}