Skip to content

Commit

Permalink
Do tiny refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shintaro Katafuchi committed Jun 28, 2015
1 parent 41be586 commit 45cbdad
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 48 deletions.
38 changes: 23 additions & 15 deletions library/src/main/java/hotchemi/android/rate/AppRate.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

import java.util.Date;

import static hotchemi.android.rate.DialogManager.create;
import static hotchemi.android.rate.PreferenceHelper.getEventTimes;
import static hotchemi.android.rate.PreferenceHelper.getInstallDate;
import static hotchemi.android.rate.PreferenceHelper.getIsAgreeShowDialog;
import static hotchemi.android.rate.PreferenceHelper.getLaunchTimes;
import static hotchemi.android.rate.PreferenceHelper.getRemindInterval;
import static hotchemi.android.rate.PreferenceHelper.isFirstLaunch;
import static hotchemi.android.rate.PreferenceHelper.setInstallDate;

public class AppRate {

private static AppRate singleton;
Expand Down Expand Up @@ -96,10 +105,10 @@ public AppRate setOnClickButtonListener(OnClickButtonListener listener) {
}

public void monitor() {
if (PreferenceHelper.isFirstLaunch(context)) {
PreferenceHelper.setInstallDate(context);
if (isFirstLaunch(context)) {
setInstallDate(context);
}
PreferenceHelper.setLaunchTimes(context, PreferenceHelper.getLaunchTimes(context) + 1);
PreferenceHelper.setLaunchTimes(context, getLaunchTimes(context) + 1);
}

public static boolean showRateDialogIfMeetsConditions(Activity activity) {
Expand All @@ -119,9 +128,8 @@ public static boolean passSignificantEventAndConditions(Activity activity) {
}

private static boolean passSignificantEvent(Activity activity, boolean shouldShow) {
Context context = activity.getApplicationContext();
int eventTimes = PreferenceHelper.getEventTimes(context);
PreferenceHelper.setEventTimes(context, ++eventTimes);
int eventTimes = getEventTimes(activity);
PreferenceHelper.setEventTimes(activity, ++eventTimes);
boolean isMeetsConditions = singleton.isDebug || (singleton.isOverEventPass() && shouldShow);
if (isMeetsConditions) {
singleton.showRateDialog(activity);
Expand All @@ -130,38 +138,38 @@ private static boolean passSignificantEvent(Activity activity, boolean shouldSho
}

public void showRateDialog(Activity activity) {
if(!activity.isFinishing()) {
DialogManager.create(activity, isShowNeutralButton, isShowTitle, listener, view).show();
if (!activity.isFinishing()) {
create(activity, isShowNeutralButton, isShowTitle, listener, view).show();
}
}

public boolean isOverEventPass() {
return eventsTimes != -1 && PreferenceHelper.getEventTimes(context) > eventsTimes;
return eventsTimes != -1 && getEventTimes(context) > eventsTimes;
}

public boolean shouldShowRateDialog() {
return PreferenceHelper.getIsAgreeShowDialog(context) &&
return getIsAgreeShowDialog(context) &&
isOverLaunchTimes() &&
isOverInstallDate() &&
isOverRemindDate();
}

private boolean isOverLaunchTimes() {
return PreferenceHelper.getLaunchTimes(context) >= launchTimes;
return getLaunchTimes(context) >= launchTimes;
}

private boolean isOverInstallDate() {
return isOverDate(PreferenceHelper.getInstallDate(context), installDate);
return isOverDate(getInstallDate(context), installDate);
}

private boolean isOverRemindDate() {
return isOverDate(PreferenceHelper.getRemindInterval(context), remindInterval);
return isOverDate(getRemindInterval(context), remindInterval);
}

private boolean isOverDate(long targetDate, int threshold) {
private static boolean isOverDate(long targetDate, int threshold) {
return new Date().getTime() - targetDate >= threshold * 24 * 60 * 60 * 1000;
}

private boolean isDebug() {
return isDebug;
}
Expand Down
28 changes: 12 additions & 16 deletions library/src/main/java/hotchemi/android/rate/DialogManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,48 @@
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.Window;

import static hotchemi.android.rate.IntentHelper.createIntentForGooglePlay;
import static hotchemi.android.rate.PreferenceHelper.setAgreeShowDialog;
import static hotchemi.android.rate.PreferenceHelper.setRemindInterval;

final class DialogManager {
private static final String GOOGLE_PLAY_PACKAGE_NAME = "com.android.vending";

private DialogManager() {
}

static Dialog create(final Context context, final boolean isShowNeutralButton,
final boolean isShowTitle, final OnClickButtonListener listener,
final View view) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
static Dialog create(final Context context, boolean isShowNeutralButton,
boolean isShowTitle, final OnClickButtonListener listener, View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.rate_dialog_message);
if (view != null) builder.setView(view);
builder.setPositiveButton(R.string.rate_dialog_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String packageName = context.getPackageName();
Intent intent = new Intent(Intent.ACTION_VIEW, UriHelper.getGooglePlay(packageName));
if (UriHelper.isPackageExists(context, GOOGLE_PLAY_PACKAGE_NAME)) {
intent.setPackage(GOOGLE_PLAY_PACKAGE_NAME);
}
context.startActivity(intent);
PreferenceHelper.setAgreeShowDialog(context, false);
context.startActivity(createIntentForGooglePlay(context));
setAgreeShowDialog(context, false);
if (listener != null) listener.onClickButton(which);
}
});
if (isShowNeutralButton) {
builder.setNeutralButton(R.string.rate_dialog_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
PreferenceHelper.setRemindInterval(context);
setRemindInterval(context);
if (listener != null) listener.onClickButton(which);
}
});
}
builder.setNegativeButton(R.string.rate_dialog_no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
PreferenceHelper.setAgreeShowDialog(context, false);
setAgreeShowDialog(context, false);
if (listener != null) listener.onClickButton(which);
}
});
final AlertDialog dialog = builder.create();
AlertDialog dialog = builder.create();
if (!isShowTitle) {
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
} else {
Expand Down
25 changes: 25 additions & 0 deletions library/src/main/java/hotchemi/android/rate/IntentHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package hotchemi.android.rate;

import android.content.Context;
import android.content.Intent;

import static hotchemi.android.rate.UriHelper.getGooglePlay;
import static hotchemi.android.rate.UriHelper.isPackageExists;

final class IntentHelper {

private static final String GOOGLE_PLAY_PACKAGE_NAME = "com.android.vending";

private IntentHelper() {
}

static Intent createIntentForGooglePlay(Context context) {
String packageName = context.getPackageName();
Intent intent = new Intent(Intent.ACTION_VIEW, getGooglePlay(packageName));
if (isPackageExists(context, GOOGLE_PLAY_PACKAGE_NAME)) {
intent.setPackage(GOOGLE_PLAY_PACKAGE_NAME);
}
return intent;
}

}
25 changes: 8 additions & 17 deletions library/src/main/java/hotchemi/android/rate/PreferenceHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Build;

import java.util.Date;

Expand Down Expand Up @@ -38,10 +37,10 @@ static Editor getPreferencesEditor(Context context) {
* @param context context
*/
static void clearSharedPreferences(Context context) {
final SharedPreferences.Editor editor = getPreferencesEditor(context);
SharedPreferences.Editor editor = getPreferencesEditor(context);
editor.remove(PREF_KEY_INSTALL_DATE);
editor.remove(PREF_KEY_LAUNCH_TIMES);
commitOrApply(editor);
editor.apply();
}

/**
Expand All @@ -52,9 +51,9 @@ static void clearSharedPreferences(Context context) {
* @param isAgree agree with showing rate dialog
*/
static void setAgreeShowDialog(Context context, boolean isAgree) {
final SharedPreferences.Editor editor = getPreferencesEditor(context);
SharedPreferences.Editor editor = getPreferencesEditor(context);
editor.putBoolean(PREF_KEY_IS_AGREE_SHOW_DIALOG, isAgree);
commitOrApply(editor);
editor.apply();
}

static boolean getIsAgreeShowDialog(Context context) {
Expand All @@ -65,7 +64,7 @@ static void setRemindInterval(Context context) {
SharedPreferences.Editor editor = getPreferencesEditor(context);
editor.remove(PREF_KEY_REMIND_INTERVAL);
editor.putLong(PREF_KEY_REMIND_INTERVAL, new Date().getTime());
commitOrApply(editor);
editor.apply();
}

static long getRemindInterval(Context context) {
Expand All @@ -75,7 +74,7 @@ static long getRemindInterval(Context context) {
static void setInstallDate(Context context) {
SharedPreferences.Editor editor = getPreferencesEditor(context);
editor.putLong(PREF_KEY_INSTALL_DATE, new Date().getTime());
commitOrApply(editor);
editor.apply();
}

static long getInstallDate(Context context) {
Expand All @@ -85,7 +84,7 @@ static long getInstallDate(Context context) {
static void setLaunchTimes(Context context, int launchTimes) {
SharedPreferences.Editor editor = getPreferencesEditor(context);
editor.putInt(PREF_KEY_LAUNCH_TIMES, launchTimes);
commitOrApply(editor);
editor.apply();
}

static int getLaunchTimes(Context context) {
Expand All @@ -103,15 +102,7 @@ static int getEventTimes(Context context) {
static void setEventTimes(Context context, int eventTimes) {
SharedPreferences.Editor editor = getPreferencesEditor(context);
editor.putInt(PREF_KEY_EVENT_TIMES, eventTimes);
commitOrApply(editor);
}

private static void commitOrApply(SharedPreferences.Editor editor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
editor.apply();
} else {
editor.commit();
}
editor.apply();
}

}

0 comments on commit 45cbdad

Please sign in to comment.