package hotchemi.android.rate;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Build;
import android.support.v4.app.FragmentActivity;

import java.util.Date;

/**
 * Help you promote your app by prompting users to rate the app after using it for a few days.
 */
public class AppRate {

    private static final String TAG = AppRate.class.getName();

    private static final AppRate INSTANCE = new AppRate();

    private static int sLaunchTimesThreshold = 10;

    private static int sInstallDaysThreshold = 10;

    private static long sInstallDateTime = new Date().getTime();

    private static int sLaunchTimes = 0;

    private static boolean sIsAgreeShoWDialog = true;

    private static boolean sIsShoWNeutralButton = true;

    private AppRate() {
    }

    public static AppRate setLaunchTimes(final int launchTimesThreshold) {
        sLaunchTimesThreshold = launchTimesThreshold;
        return INSTANCE;
    }

    public static AppRate setInstallDays(final int installDaysThreshold) {
        sInstallDaysThreshold = installDaysThreshold;
        return INSTANCE;
    }

    public static AppRate setShowNeutralButton(final boolean isShowNeutralButton) {
        sIsShoWNeutralButton = isShowNeutralButton;
        return INSTANCE;
    }

    public static AppRate clearAgreeShowDialog(final Context context) {
        PreferenceUtils.setAgreeShowDialog(context, true);
        return INSTANCE;
    }

    /**
     * Monitor launch times and interval from installation.<br/>
     * Call this method when the launcher activity's onCreate() is launched.
     */
    public static void monitor(final Context context) {
        final SharedPreferences pref = PreferenceUtils.getPreferences(context);
        final Editor editor = pref.edit();
        if (isFirstLaunch(pref)) {
            editor.putLong(Constants.PREF_KEY_INSTALL_DATE, sInstallDateTime);
        }
        int launchTimes = pref.getInt(Constants.PREF_KEY_LAUNCH_TIMES, 0);
        editor.putInt(Constants.PREF_KEY_LAUNCH_TIMES, launchTimes + 1);
        editor.commit();

        sInstallDateTime = new Date(pref.getLong(Constants.PREF_KEY_INSTALL_DATE, 0)).getTime();
        sLaunchTimes = pref.getInt(Constants.PREF_KEY_LAUNCH_TIMES, 0);
        sIsAgreeShoWDialog = pref.getBoolean(Constants.PREF_KEY_IS_AGREE_SHOW_DIALOG, true);
    }

    /**
     * Show rate dialog when meets conditions.
     *
     * @param activity activity
     */
    public static void showRateDialogIfMeetsConditions(final Activity activity) {
        if (shouldShowRateDialog()) {
            if (activity instanceof FragmentActivity) {
                showRateDialog((FragmentActivity) activity);
            } else {
                showRateDialog(activity);
            }
        }
    }

    /**
     * Show rate dialog for preHoneycomb devices.
     *
     * @param activity fragment activity
     */
    public static void showRateDialog(final FragmentActivity activity) {
        final RateDialogSupportFragment fragment = RateDialogSupportFragment
                .newInstance(sIsShoWNeutralButton);
        fragment.show(activity.getSupportFragmentManager(), TAG);
    }

    /**
     * Show rate dialog for Honeycomb devices
     *
     * @param activity fragment activity
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static void showRateDialog(final Activity activity) {
        final RateDialogFragment fragment = RateDialogFragment.newInstance(sIsShoWNeutralButton);
        fragment.show(activity.getFragmentManager(), TAG);
    }

    private static boolean isFirstLaunch(final SharedPreferences pref) {
        return pref.getLong(Constants.PREF_KEY_INSTALL_DATE, 0) == 0L;
    }

    private static boolean isOverLaunchTimes() {
        return sLaunchTimes >= sLaunchTimesThreshold;
    }

    private static boolean isOverInstallDate() {
        // msec
        return new Date().getTime() - sInstallDateTime >= sInstallDaysThreshold * 24 * 60 * 60 * 1000;
    }

    private static boolean shouldShowRateDialog() {
        return sIsAgreeShoWDialog && isOverLaunchTimes() && isOverInstallDate();
    }

}
@@ -586,38 +586,52 @@
 tic 
-int sLaunchTimesThreshold = 10
+long sInstallDateTime = new Date().getTime()
 ;%0A%0A 
@@ -704,52 +704,38 @@
 tic 
-long sInstallDateTime = new Date().getTime()
+int sLaunchTimesThreshold = 10
 ;%0A%0A 
@@ -879,32 +879,78 @@
 Button = true;%0A%0A
+    private static boolean sIsDebug = false;%0A%0A
     private AppR
@@ -1609,32 +1609,306 @@
 NSTANCE;%0A    %7D%0A%0A
+    /**%0A     * Set debug flag.%3Cbr/%3E%0A     * when debug flag is true, always show rating dialog.%0A     *%0A     * @param isDebug set debug mode or not%0A     */%0A    public static AppRate setDebug(final boolean isDebug) %7B%0A        sIsDebug = isDebug;%0A        return INSTANCE;%0A    %7D%0A%0A
     /**%0A     * M
@@ -2991,16 +2991,28 @@
     if (
+sIsDebug %7C%7C 
 shouldSh
@@ -4267,32 +4267,32 @@
 reshold;%0A    %7D%0A%0A
+
     private stat
@@ -4328,24 +4328,8 @@
 ) %7B%0A
-        // msec%0A
     

add debug flag.