From b8e562d03695f5a73809feb0751a9c296c9c5fa4 Mon Sep 17 00:00:00 2001 From: Janak Amarasena Date: Sat, 23 Jun 2018 16:25:38 +0530 Subject: Modified donation reminder --- .../main/java/se/leap/bitmaskclient/Constants.java | 1 + .../java/se/leap/bitmaskclient/EipFragment.java | 38 +++++++++++++--------- .../se/leap/bitmaskclient/utils/DateHelper.java | 30 +++++++++++++++++ 3 files changed, 53 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/se/leap/bitmaskclient/utils/DateHelper.java (limited to 'app') diff --git a/app/src/main/java/se/leap/bitmaskclient/Constants.java b/app/src/main/java/se/leap/bitmaskclient/Constants.java index af1d55ec..8ec3fcc7 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Constants.java +++ b/app/src/main/java/se/leap/bitmaskclient/Constants.java @@ -99,6 +99,7 @@ public interface Constants { String DONATION_URL = TextUtils.isEmpty(BuildConfig.donation_url) ? BuildConfig.default_donation_url : BuildConfig.donation_url; String LAST_DONATION_REMINDER_DATE = "last_donation_reminder_date"; + String FIRST_TIME_USER_DATE = "first_time_user_date"; } diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index 535322e5..51f787b7 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -58,6 +58,7 @@ import de.blinkt.openvpn.core.OpenVPNService; import de.blinkt.openvpn.core.ProfileManager; import se.leap.bitmaskclient.eip.EipCommand; import se.leap.bitmaskclient.eip.EipStatus; +import se.leap.bitmaskclient.utils.DateHelper; import se.leap.bitmaskclient.views.VpnStateImage; import static android.view.View.GONE; @@ -65,6 +66,7 @@ import static android.view.View.VISIBLE; import static de.blinkt.openvpn.core.ConnectionStatus.LEVEL_NONETWORK; import static se.leap.bitmaskclient.Constants.DONATION_REMINDER_DURATION; import static se.leap.bitmaskclient.Constants.EIP_RESTART_ON_BOOT; +import static se.leap.bitmaskclient.Constants.FIRST_TIME_USER_DATE; import static se.leap.bitmaskclient.Constants.PROVIDER_KEY; import static se.leap.bitmaskclient.Constants.REQUEST_CODE_LOG_IN; import static se.leap.bitmaskclient.Constants.REQUEST_CODE_SWITCH_PROVIDER; @@ -530,7 +532,8 @@ public class EipFragment extends Fragment implements Observer { }).setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { - saveLastDonationReminderDate(); + preferences.edit().putString(LAST_DONATION_REMINDER_DATE, + DateHelper.getCurrentDateString()).apply(); } }).show(); } @@ -545,29 +548,32 @@ public class EipFragment extends Fragment implements Observer { return false; } - String lastDonationReminderDate = preferences.getString(LAST_DONATION_REMINDER_DATE, null); - if (lastDonationReminderDate == null) { - return true; + String firstTimeUserDate = preferences.getString(FIRST_TIME_USER_DATE, null); + if (firstTimeUserDate == null) { + preferences.edit().putString(FIRST_TIME_USER_DATE, DateHelper.getCurrentDateString()).apply(); + return false; } - SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN, Locale.US); - Date lastDate; try { - lastDate = sdf.parse(lastDonationReminderDate); + long diffDays; + + diffDays = DateHelper.getDateDiffToCurrentDateInDays(firstTimeUserDate); + if (diffDays < 1) { + return false; + } + + String lastDonationReminderDate = preferences.getString(LAST_DONATION_REMINDER_DATE, null); + if (lastDonationReminderDate == null) { + return true; + } + diffDays = DateHelper.getDateDiffToCurrentDateInDays(lastDonationReminderDate); + return diffDays >= DONATION_REMINDER_DURATION; + } catch (ParseException e) { e.printStackTrace(); Log.e(TAG, e.getMessage()); return false; } - - Date currentDate = new Date(); - long diffDays = (currentDate.getTime() - lastDate.getTime()) / ONE_DAY; - return diffDays >= DONATION_REMINDER_DURATION; } - private void saveLastDonationReminderDate() { - SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN, Locale.US); - Date lastDate = new Date(); - preferences.edit().putString(LAST_DONATION_REMINDER_DATE, sdf.format(lastDate)).apply(); - } } diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/DateHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/DateHelper.java new file mode 100644 index 00000000..a8649132 --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/utils/DateHelper.java @@ -0,0 +1,30 @@ +package se.leap.bitmaskclient.utils; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +/** + * Contains helper methods related to date manipulation. + * + * @author Janak + */ +public class DateHelper { + private static final String DATE_PATTERN = "dd/MM/yyyy"; + private static final int ONE_DAY = 86400000; //1000*60*60*24 + + public static long getDateDiffToCurrentDateInDays(String startDate) throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN, Locale.US); + Date lastDate; + lastDate = sdf.parse(startDate); + Date currentDate = new Date(); + return (currentDate.getTime() - lastDate.getTime()) / ONE_DAY; + } + + public static String getCurrentDateString() { + SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN, Locale.US); + Date lastDate = new Date(); + return sdf.format(lastDate); + } +} -- cgit v1.2.3 From 9cbcd3bad1f0aa33b7c08dc18f7065a126af7eb5 Mon Sep 17 00:00:00 2001 From: Janak Amarasena Date: Tue, 26 Jun 2018 22:53:43 +0530 Subject: Minor update according to received feedback --- app/src/main/java/se/leap/bitmaskclient/utils/DateHelper.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'app') diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/DateHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/DateHelper.java index a8649132..523c8c4c 100644 --- a/app/src/main/java/se/leap/bitmaskclient/utils/DateHelper.java +++ b/app/src/main/java/se/leap/bitmaskclient/utils/DateHelper.java @@ -16,8 +16,7 @@ public class DateHelper { public static long getDateDiffToCurrentDateInDays(String startDate) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN, Locale.US); - Date lastDate; - lastDate = sdf.parse(startDate); + Date lastDate = sdf.parse(startDate); Date currentDate = new Date(); return (currentDate.getTime() - lastDate.getTime()) / ONE_DAY; } -- cgit v1.2.3