Skip to content

Commit

Permalink
Fix time between calls
Browse files Browse the repository at this point in the history
  • Loading branch information
guy-4444 committed Mar 5, 2019
1 parent 56b3860 commit f64ee94
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 45 deletions.
17 changes: 14 additions & 3 deletions app/src/main/java/guy/rateapplication/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,27 @@ protected void onCreate(Bundle savedInstanceState) {
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// SmartRate.Rate(MainActivity.this
// , "Rate Us"
// , "Tell others what you think about this app"
// , "Continue"
// , "Ask me later"
// , "Never ask again"
// , "Thanks for the feedback"
// , Color.parseColor("#2196F3")
// , 4
// , 48
// , 72
// );

SmartRate.Rate(MainActivity.this
, "Rate Us"
, "Tell others what you think about this app"
, "Continue"
, "Ask me later"
, "Never ask again"
, "Cancel"
, "Thanks for the feedback"
, Color.parseColor("#2196F3")
, 4
, 3
);
}
});
Expand Down
Binary file modified desc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed desc_s.png
Binary file not shown.
Binary file modified description.docx
Binary file not shown.
Binary file added screen shot 2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
136 changes: 94 additions & 42 deletions smartrate/src/main/java/guy4444/smartrate/SmartRate.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,40 @@ public class SmartRate {
private static final long DONT_ASK_AGAIN_VALUE = -1;
private static final String SP_LIBRARY_NAME = "SP_RATE_LIBRARY";
private static final String SP_KEY_LAST_ASK_TIME = "SP_KEY_LAST_ASK_TIME";
private static final String SP_KEY_INIT_TIME = "SP_KEY_INIT_TIME";
private static int selectedStar = 1;
private static final long DEFAULT_TIME_BETWEEN_DIALOG_MS = 1000l * 60 * 60 * 24 * 3; // 3 days
private static final long DEFAULT_TIME_BETWEEN_DIALOG_MS = 1000l * 60 * 60 * 24 * 6; // 3 days
private static final long DEFAULT_DELAY_TO_ACTIVATE_MS = 1000l * 60 * 60 * 24 * 3; // 3 days
private static String DEFAULT_TEXT_TITLE = "Rate Us";
private static String DEFAULT_TEXT_CONTENT = "Tell others what you think about this app";
private static String DEFAULT_TEXT_OK = "Continue";
private static String DEFAULT_TEXT_LATER = "Ask me later";
private static String DEFAULT_TEXT_STOP = "Never ask again";
private static String DEFAULT_TEXT_THANKS = "Thanks for the feedback";

public static void Rate(
final Activity activity
, final String _title
, final String _content
, final String _ok_text
, final String _cancel_text
, final String _thanksForFeedback
, final int mainColor
, final int openStoreFromXStars
) {
Rate(activity
, _title
, _content
, _ok_text
, _cancel_text
, ""
, _thanksForFeedback
, mainColor
, openStoreFromXStars
, -1
, -1);
}

public static void Rate(
final Activity activity
, final String _title
Expand All @@ -43,27 +68,41 @@ public static void Rate(
, final int mainColor
, final int openStoreFromXStars
, final int _hoursBetweenCalls
, final int _hoursDelayToActivate
) {

final String title = (_title != null && !_title.equals("")) ? _title : DEFAULT_TEXT_TITLE;
final String content = (_content != null && !_content.equals("")) ? _content : DEFAULT_TEXT_CONTENT;
final String ok_text = (_ok_text != null && !_ok_text.equals("")) ? _ok_text : DEFAULT_TEXT_OK;
final String later_text = (_later_text != null && !_later_text.equals("")) ? _later_text : DEFAULT_TEXT_LATER;
final String stop_text = (_stop_text != null && !_stop_text.equals("")) ? _stop_text : DEFAULT_TEXT_STOP;
final String thanksForFeedback = (_thanksForFeedback != null && !_thanksForFeedback.equals("")) ? _thanksForFeedback : DEFAULT_TEXT_THANKS;
final long timeBetweenCalls_Ms = (_hoursBetweenCalls >= 1 && _hoursBetweenCalls < 366 * 24) ? 1000l * 60 * 60 * _hoursBetweenCalls : DEFAULT_TIME_BETWEEN_DIALOG_MS;
final long timeDelayToActivate_Ms = (_hoursDelayToActivate >= 1 && _hoursDelayToActivate < 366 * 24) ? 1000l * 60 * 60 * _hoursDelayToActivate : DEFAULT_DELAY_TO_ACTIVATE_MS;


if (_hoursBetweenCalls != -1 && _hoursDelayToActivate != -1) {
// no force asking mode
long initTime = getInitTime(activity);
if (initTime == 0) {
setInitTime(activity, System.currentTimeMillis());
return;
}
if (System.currentTimeMillis() < initTime + timeDelayToActivate_Ms) {
return;
}

final String title = (_title!=null && !_title.equals("")) ? _title : DEFAULT_TEXT_TITLE;
final String content = (_content!=null && !_content.equals("")) ? _content : DEFAULT_TEXT_CONTENT;
final String ok_text = (_ok_text!=null && !_ok_text.equals("")) ? _ok_text : DEFAULT_TEXT_OK;
final String later_text = (_later_text!=null && !_later_text.equals("")) ? _later_text : DEFAULT_TEXT_LATER;
final String stop_text = (_stop_text!=null && !_stop_text.equals("")) ? _stop_text : DEFAULT_TEXT_STOP;
final String thanksForFeedback = (_thanksForFeedback!=null && !_thanksForFeedback.equals("")) ? _thanksForFeedback : DEFAULT_TEXT_THANKS;
final long timeBetweenCalls_Ms = (_hoursBetweenCalls >=1 && _hoursBetweenCalls < 366*24) ? 1000l * 60 * 60 *_hoursBetweenCalls : DEFAULT_TIME_BETWEEN_DIALOG_MS;

if (getLastAskTime(activity) == DONT_ASK_AGAIN_VALUE) {
// user already rate or click on never ask button
return;
}
if (System.currentTimeMillis() < getLastAskTime(activity) + timeBetweenCalls_Ms) {
// There was not enough time between the calls.
return;
if (getLastAskTime(activity) == DONT_ASK_AGAIN_VALUE) {
// user already rate or click on never ask button
return;
}
if (System.currentTimeMillis() < getLastAskTime(activity) + timeBetweenCalls_Ms) {
// There was not enough time between the calls.
return;
}
}

saveLastAskTime(activity, System.currentTimeMillis());
setLastAskTime(activity, System.currentTimeMillis());

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
LayoutInflater inflater = activity.getLayoutInflater();
Expand Down Expand Up @@ -131,22 +170,20 @@ public void onClick(View v) {
alert_BTN_star_5.setOnClickListener(starsClickListener);



alert_LBL_title.setText(title);
alert_LBL_content.setText(content);



if (ok_text!=null && !ok_text.equals("")) {
if (ok_text != null && !ok_text.equals("")) {
alert_BTN_ok.setText(ok_text);
alert_BTN_ok.setText("?/5\n" + ok_text);
alert_BTN_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveLastAskTime(activity, DONT_ASK_AGAIN_VALUE);
setLastAskTime(activity, DONT_ASK_AGAIN_VALUE);

int _openStoreFrom_Stars = openStoreFromXStars;
if (openStoreFromXStars < 1 || openStoreFromXStars > 5) {
if (openStoreFromXStars < 1 || openStoreFromXStars > 5) {
_openStoreFrom_Stars = 1;
}
if (selectedStar >= _openStoreFrom_Stars) {
Expand All @@ -157,13 +194,12 @@ public void onClick(View view) {
alertDialog.dismiss();
}
});
}
else {
} else {
alert_BTN_ok.setVisibility(View.INVISIBLE);
}
alert_BTN_ok.setEnabled(false);

if (later_text!=null && !later_text.equals("")) {
if (later_text != null && !later_text.equals("")) {
alert_BTN_later.setText(later_text);
alert_BTN_later.setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -172,25 +208,29 @@ public void onClick(View view) {
alertDialog.dismiss();
}
});
}
else {
} else {
alert_BTN_later.setVisibility(View.INVISIBLE);
}

if (stop_text!=null && !stop_text.equals("")) {
if (stop_text != null && !stop_text.equals("")) {
alert_BTN_stop.setText(stop_text);
alert_BTN_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveLastAskTime(activity, DONT_ASK_AGAIN_VALUE);
setLastAskTime(activity, DONT_ASK_AGAIN_VALUE);
alertDialog.dismiss();
}
});
}
else {
} else {
alert_BTN_stop.setVisibility(View.INVISIBLE);
}


if (_hoursBetweenCalls == -1 && _hoursDelayToActivate == -1) {
// force asking mode
alert_BTN_stop.setVisibility(View.GONE);
}

alertDialog.show();
}

Expand All @@ -210,30 +250,42 @@ private static long getLastAskTime(Activity activity) {
return val;
}

private static void saveLastAskTime(Activity activity, long time) {
private static void setLastAskTime(Activity activity, long time) {
SharedPreferences.Editor editor = activity.getSharedPreferences(SP_LIBRARY_NAME, Context.MODE_PRIVATE).edit();
editor.putLong(SP_KEY_LAST_ASK_TIME, time);
editor.apply();
}

private static long getInitTime(Activity activity) {
SharedPreferences sharedPreferences = activity.getSharedPreferences(SP_LIBRARY_NAME, Context.MODE_PRIVATE);
long val = sharedPreferences.getLong(SP_KEY_INIT_TIME, 0);
return val;
}

private static void setInitTime(Activity activity, long time) {
SharedPreferences.Editor editor = activity.getSharedPreferences(SP_LIBRARY_NAME, Context.MODE_PRIVATE).edit();
editor.putLong(SP_KEY_INIT_TIME, time);
editor.apply();
}

private static String shadeColor(String color, int percent) {

int R = Integer.parseInt(color.substring(1,3),16);
int G = Integer.parseInt(color.substring(3,5),16);
int B = Integer.parseInt(color.substring(5,7),16);
int R = Integer.parseInt(color.substring(1, 3), 16);
int G = Integer.parseInt(color.substring(3, 5), 16);
int B = Integer.parseInt(color.substring(5, 7), 16);

R = R * (100 + percent) / 100;
G = G * (100 + percent) / 100;
B = B * (100 + percent) / 100;

R = (R<255)?R:255;
G = (G<255)?G:255;
B = (B<255)?B:255;
R = (R < 255) ? R : 255;
G = (G < 255) ? G : 255;
B = (B < 255) ? B : 255;

String RR = (Integer.toString(R, 16).length()==1) ? "0" + Integer.toString(R, 16) : Integer.toString(R, 16);
String GG = (Integer.toString(G, 16).length()==1) ? "0" + Integer.toString(G, 16) : Integer.toString(G, 16);
String BB = (Integer.toString(B, 16).length()==1) ? "0" + Integer.toString(B, 16) : Integer.toString(B, 16);
String RR = (Integer.toString(R, 16).length() == 1) ? "0" + Integer.toString(R, 16) : Integer.toString(R, 16);
String GG = (Integer.toString(G, 16).length() == 1) ? "0" + Integer.toString(G, 16) : Integer.toString(G, 16);
String BB = (Integer.toString(B, 16).length() == 1) ? "0" + Integer.toString(B, 16) : Integer.toString(B, 16);

return "#"+RR+GG+BB;
return "#" + RR + GG + BB;
}
}

0 comments on commit f64ee94

Please sign in to comment.