@Override public void onClick(View v) { // Show rating dialog explicitly. RateThisApp.showRateDialog(MainActivity.this); } });
@Override public void onClick(DialogInterface dialog, int which) { if (sCallback != null) { sCallback.onCancelClicked(); } clearSharedPreferences(context); storeAskLaterDate(context); } });
@Override public void onClick(DialogInterface dialog, int which) { if (sCallback != null) { sCallback.onNoClicked(); } setOptOut(context, true); } });
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RateThisApp.init(new RateThisApp.Config(3, 5)); RateThisApp.setCallback(new RateThisApp.Callback() { @Override public void onYesClicked() { Button button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override Button button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override Button button3 = (Button) findViewById(R.id.button3); button3.setOnClickListener(new View.OnClickListener() { @Override RateThisApp.onCreate(this); RateThisApp.showRateDialogIfNeeded(this);
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); prefManager = PreferenceManager.getDefaultSharedPreferences(this); onFirstInstall(); onFirstUpdate(); handleIntent(getIntent()); if (Utils.tempBool && getIntent().getStringExtra("uri") != null) uri = Uri.parse(getIntent().getStringExtra("uri")); // Custom condition: 5 days and 5 launches RateThisApp.Config config = new RateThisApp.Config(5, 5); RateThisApp.init(config); RateThisApp.onCreate(this); RateThisApp.showRateDialogIfNeeded(this); }
@Test public void shouldRateDialogIfNeeded_LaunchTimeIsCorrect() { Context context = RuntimeEnvironment.application.getApplicationContext(); RateThisApp.init(new RateThisApp.Config(1, 3)); RateThisApp.onStart(context); Assert.assertFalse(RateThisApp.shouldShowRateDialog()); RateThisApp.onStart(context); Assert.assertFalse(RateThisApp.shouldShowRateDialog()); RateThisApp.onStart(context); Assert.assertTrue(RateThisApp.shouldShowRateDialog()); RateThisApp.onStart(context); Assert.assertTrue(RateThisApp.shouldShowRateDialog()); }
/** * Show the rate dialog if the criteria is satisfied. * @param context Context * @return true if shown, false otherwise. */ public static boolean showRateDialogIfNeeded(final Context context) { if (shouldShowRateDialog()) { showRateDialog(context); return true; } else { return false; } }
/** * This API is deprecated. * You should call onCreate instead of this API in Activity's onCreate(). * @param context */ @Deprecated public static void onStart(Context context) { onCreate(context); }
/** * Call this API when the launcher activity is launched.<br> * It is better to call this API in onCreate() of the launcher activity. * @param context Context */ public static void onCreate(Context context) { SharedPreferences pref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); Editor editor = pref.edit(); // If it is the first launch, save the date in shared preference. if (pref.getLong(KEY_INSTALL_DATE, 0) == 0L) { storeInstallDate(context, editor); } // Increment launch times int launchTimes = pref.getInt(KEY_LAUNCH_TIMES, 0); launchTimes++; editor.putInt(KEY_LAUNCH_TIMES, launchTimes); log("Launch times; " + launchTimes); editor.apply(); mInstallDate = new Date(pref.getLong(KEY_INSTALL_DATE, 0)); mLaunchTimes = pref.getInt(KEY_LAUNCH_TIMES, 0); mOptOut = pref.getBoolean(KEY_OPT_OUT, false); mAskLaterDate = new Date(pref.getLong(KEY_ASK_LATER_DATE, 0)); printStatus(context); }
@Override public void onClick(View v) { RateThisApp.stopRateDialog(MainActivity.this); } });
@Test public void getLaunchCount_IsCorrect() { Context context = RuntimeEnvironment.application.getApplicationContext(); Assert.assertEquals(0, RateThisApp.getLaunchCount(context)); RateThisApp.onStart(context); Assert.assertEquals(1, RateThisApp.getLaunchCount(context)); RateThisApp.onStart(context); Assert.assertEquals(2, RateThisApp.getLaunchCount(context)); }
/** * Stop showing the rate dialog * @param context */ public static void stopRateDialog(final Context context){ setOptOut(context, true); }
/** * Print values in SharedPreferences (used for debug) * @param context */ private static void printStatus(final Context context) { SharedPreferences pref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); log("*** RateThisApp Status ***"); log("Install Date: " + new Date(pref.getLong(KEY_INSTALL_DATE, 0))); log("Launch Times: " + pref.getInt(KEY_LAUNCH_TIMES, 0)); log("Opt out: " + pref.getBoolean(KEY_OPT_OUT, false)); }
@Override public void onClick(DialogInterface dialog, int which) { if (sCallback != null) { sCallback.onYesClicked(); } String appPackage = context.getPackageName(); String url = "market://details?id=" + appPackage; if (!TextUtils.isEmpty(sConfig.mUrl)) { url = sConfig.mUrl; } try { context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); } catch (android.content.ActivityNotFoundException anfe) { context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + context.getPackageName()))); } setOptOut(context, true); } });
/** * Show the rate dialog if the criteria is satisfied. * @param context Context * @param themeId Theme ID * @return true if shown, false otherwise. */ public static boolean showRateDialogIfNeeded(final Context context, int themeId) { if (shouldShowRateDialog()) { showRateDialog(context, themeId); return true; } else { return false; } }
@Override public void onClick(View v) { // Show rating dialog explicitly. RateThisApp.showRateDialog(MainActivity.this, R.style.MyAlertDialogStyle2); } });
@Test public void stopRateDialog_IsSuccess() { Context context = RuntimeEnvironment.application.getApplicationContext(); RateThisApp.stopRateDialog(context); // check shared pref SharedPreferences sharedPreferences = RuntimeEnvironment.application.getSharedPreferences( PREF_NAME, Context.MODE_PRIVATE); Assert.assertTrue(sharedPreferences.getBoolean(KEY_OPT_OUT, false)); } }
@Override public void onCancel(DialogInterface dialog) { if (sCallback != null) { sCallback.onCancelClicked(); } clearSharedPreferences(context); storeAskLaterDate(context); } });
/** * Show the rate dialog * @param context * @param themeId */ public static void showRateDialog(final Context context, int themeId) { AlertDialog.Builder builder = new AlertDialog.Builder(context, themeId); showRateDialog(context, builder); }
/** * Show the rate dialog * @param context */ public static void showRateDialog(final Context context) { AlertDialog.Builder builder = new AlertDialog.Builder(context); showRateDialog(context, builder); }